<?php
	defined('BASEPATH') OR exit('No direct script access allowed');
	
	class Journal extends CI_Controller 
	{
		public function __construct()
		{
			parent::__construct();
			cek_session_login();
			$this->load->model('Journal_model','journal');
			$this->load->model('Masterdata_model','masterdata');
			$this->load->model('Kas_model','kas');
			$this->iduser = $this->session->idu;
			$this->level = $this->session->level;
			$this->perPage = 10;
		}
		
		public function index()
		{
			cek_menu_akses();
			$data['title'] = 'Jurnal Umum';
			$data['user'] = $this->model_app->view_where('tb_users', array('id_user' => $this->iduser))->row_array();
			$now = date("Y-m-d");
			$data['tglAwal'] = date('Y-m-01',time());
			$data['tglAkhir'] = date('Y-m-t',time());
			
			// // $data['journal'] =  $this->journal->getJournal();
			$this->template->load('main/themes','journal/view_index',$data);
			
		}
		
		public function ajax_list_journal()
		{
			
			cek_nput_post('GET');
			$page = $this->input->post('page');
			if (!$page) {
				$offset = 0;
				} else {
				$offset = $page;
			}
			
			$limits = $this->input->post('limits');
			
			if (!empty($limits)) {
				$limit = $limits;
				} else {
				$limit = $this->perPage;
			}
			$sortBy = $this->input->post('sortBy');
			if (!empty($sortBy)) {
				$conditions['search']['sortBy'] = $sortBy;
			}
			
			$keywords = $this->input->post('keywords');
			if (!empty($keywords)) {
				$conditions['search']['keywords'] = $keywords;
			}
			
			$conditions['where'] = array(
			'jurnal_detail.id_perkiraan' => 1
			);
			// Get record count 
			$conditions['returnType'] = 'count';
			$totalRec = $this->journal->get_Journal($conditions);
			
			// Pagination configuration 
			$config['target']      = '#data_journal';
			$config['base_url']    = base_url('journal/ajax_list_journal');
			$config['total_rows']  = $totalRec;
			$config['per_page']    = $limit;
			$config['link_func']   = 'search_journal';
			
			// Initialize pagination library 
			$this->ajax_pagination->initialize($config);
			
			// Get records 
			$conditions['start'] = $offset;
			$conditions['limit'] = $limit;
			
			if (!empty($sortBy)) {
				$conditions['search']['sortBy'] = $sortBy;
			}
			
			if (!empty($keywords)) {
				$conditions['search']['keywords'] = $keywords;
			}
			$conditions['where'] = array(
			'jurnal_detail.id_perkiraan' => 1
			);
			unset($conditions['returnType']);
			$data['result'] = $this->journal->get_Journal($conditions);
			// Load the data list view 
			$this->load->view('journal/ajax-data', $data, false);
		}
		
		public function detail($id)
		{
			$data['title'] = 'Detail Transaksi';
			$data['user'] = $this->model_app->view_where('tb_users', array('id_user' => $this->iduser))->row_array();
			$id = decrypt_url($id);
			//menampilkan semua data
			$detail = $this->journal->get_jurnal_by_id($id);
			if(!empty($detail))
			{
				$data['data'] = $this->journal->get_jurnal_by_id($id);
				
				//menampilkan data akun
				$this->db->order_by('kelompok_akun.kel_akun', 'ASC');
				$data['akun'] = $this->masterdata->getAccountData();
				//perkiraan debit kredit
				$data['perkiraan'] = $this->journal->getPerkiraan();
				$this->template->load('main/themes','journal/detail_journal',$data);
				}else{
				$this->load->view('errors/404',$data);
			}
			
		}
		
		public function add()
		{
			$data['title'] = 'Tambah Data Transaksi';
			$data['user'] = $this->model_app->view_where('tb_users', array('id_user' => $this->iduser))->row_array();
			
			$data['date'] = date("Y-m-d");
			$data['no'] = date("ymd");
			$data['num'] = autoNumberJurnal('J','id_jurnal','jurnal');
			// dump($data['num']);
			//debit kredit
			$data['perkiraan'] = $this->journal->getPerkiraan();
			
			$this->db->order_by('kelompok_akun.kel_akun', 'ASC');
			$data['akun'] =  $this->masterdata->getAccountData();
			//no_transaksi
			$no_transaction =  $this->journal->check_number_tramsaction();
			$no_order = substr($no_transaction, 6, 4);
			$no_transaction_now = intval($no_order) + 1;
			if($no_transaction == null){
				$data['no_trans'] = '0001';
				} else {
				$data['no_trans'] = $no_transaction_now;
			}
			
			$this->form_validation->set_rules('noTransaksi','No Transaksi', 'required');
			$this->form_validation->set_rules('keterangan','Keterangan', 'trim|required');
			
			if($this->form_validation->run() == false) {
				$this->template->load('main/themes','journal/add_journal',$data);
			}
		}
		
		public function save_journal()
		{
			cek_nput_post('GET');
			
			simpan_demo("Simpan");
			$rules = array(
			array(
			'field' => 'noTransaksi',
			'label' => 'no Transaksi',
			'rules' => 'required|trim|numeric',
			),
			
			array(
			'field' => 'idAkun[]',
			'label' => 'Id Akun',
			'rules' => 'required|trim',
			),
			
			array(
			'field' => 'nominal[]',
			'label' => 'nominal',
			'rules' => 'required|trim',
			),
			
			array(
			'field' => 'perkiraan[]',
			'label' => 'perkiraan',
			'rules' => 'required|trim',
			),
			
			);
			$this->form_validation->set_rules($rules);
			
			if ( $this->form_validation->run() ) 
			{
				$idAkun 	= $this->input->post('idAkun[]', true);
				$perkiraan 	= $this->input->post('perkiraan[]', true);
				$nominal1 	= $this->input->post('nominal[]', true);
				
				$hide 		= array("Rp", ".", " ");
				$nominal 	= str_replace($hide, "", $nominal1);
				
				// dump($nominal);
				$result = array();
				foreach($idAkun AS $key => $val){
					$result[] = array(
					'id_akun'		=>$_POST['idAkun'][$key],
					'debit_kredit'	=>$_POST['perkiraan'][$key], 
					'nilai'			=>$nominal[$key], 
					);
				};
				
				// menampilkan data akun kas
				$data_kas = $this->kas->getArrayIdKas();
				$tipe_kas_array = array();
				foreach($data_kas as $row){
					if(in_array(strval($row), $idAkun)){
						$tipe_kas_array[] = 1;
						} else {
						$tipe_kas_array[] = 0;
					}
				}
				
				
				if(in_array(1, $tipe_kas_array)){
					$tipe_kas = 1;
					} else {
					$tipe_kas = 0;
				}
				
				// upload bukti transaksi
				$upload_bukti = $_FILES['bukti']['name'];
				$bukti_transaksi = "";
				if($upload_bukti AND info()['demo']=='N'){
					$new_name = time().rand();
					
					$config['allowed_types'] = 'pdf|jpg|jpeg|png|webp';
					$config['max_size']      = '10048';
					$config['upload_path']   = './uploads/lampiran/';
					$config['file_name']     = $new_name;
					
					$this->load->library('upload', $config);
					
					if($this->upload->do_upload('bukti')) {
						$uploadData = $this->upload->data();
						$finalUpload = bm_finalize_ci_upload_as_webp($uploadData, 80);
						if (!$finalUpload['status']) {
							echo $finalUpload['message'];
							return;
						}
						$bukti_transaksi = $finalUpload['file_name'];
						} else {
						echo $this->upload->dispay_errors();
					}
				}
				if($upload_bukti AND info()['demo']=='N'){
					$bukti_transaksi = 'lampiran.jpg';
				}
				
				
				$error = array();
				// jika terdapat error
				
				if (count($idAkun) > 1){
					//ngecek debit dan kredit
					$debit_sum = 0;
					$kredit_sum = 0;
					$i = 0;
					foreach ($result as $d){
						if ($d['debit_kredit'] == 1){
							$debit_sum += $d['nilai'];
							} else {
							$kredit_sum += $d['nilai'];
						}
					}
					
					//jika melewati semuanya
					if($debit_sum == $kredit_sum && ($debit_sum != 0 || $kredit_sum != 0)) {
						$saved = $this->journal->insertJournal($tipe_kas, $nominal, $bukti_transaksi);
						$response = $saved ? [
							'status'=>true,
							'title'=>'Simpan!!!',
							'msg'=>'Data transaksi baru berhasil ditambahkan!',
						] : [
							'status'=>false,
							'title'=>'Peringatan!!!',
							'msg'=>'Detail jurnal tidak valid atau akun tidak ditemukan.',
						];
					}
					
					//cek debit dan kredit
					if(in_array(1, $perkiraan) && !in_array(2, $perkiraan) || in_array(2, $perkiraan) && !in_array(1, $perkiraan)){
						// $error[] = "Harus ada akun diposisi debit maupun kredit!";
						$response = [
						'status'=>false,
						'title'=>'Peringatan!!!',
						'msg'   =>'Harus ada akun diposisi debit maupun kredit!',
						];
						$this->output
						->set_status_header(200)
						->set_content_type('application/json', 'utf-8')
						->set_output(json_encode($response, JSON_HEX_APOS | JSON_HEX_QUOT))
						->_display();
						exit();
					}
					
					//jika nominal debit 0 dan kredit 0
					if($debit_sum == 0 || $kredit_sum == 0){
						$response = [
						'status'=>false,
						'title'=>'Peringatan!!!',
						'msg'   =>'Jumlah data debit maupun kredit tidak boleh 0!',
						];
						$this->output
						->set_status_header(200)
						->set_content_type('application/json', 'utf-8')
						->set_output(json_encode($response, JSON_HEX_APOS | JSON_HEX_QUOT))
						->_display();
						exit();
					}
					
					//jika jumlah debit tidak balance dengan kredit
					if($debit_sum != $kredit_sum){
						$response = [
						'status'=>false,
						'title'=>'Peringatan!!!',
						'msg'   =>'Jumlah debit harus sama dengan jumlah kredit!',
						];
						$this->output
						->set_status_header(200)
						->set_content_type('application/json', 'utf-8')
						->set_output(json_encode($response, JSON_HEX_APOS | JSON_HEX_QUOT))
						->_display();
						exit();
					}
					
				}
				else {
					$error[] = "Data akun yang digunakan tidak boleh sama!";
					$response = [
					'status'=>false,
					'title'=>'Peringatan!!!',
					'msg'   =>'Data akun yang digunakan tidak boleh sama!',
					];
					$this->output
					->set_status_header(200)
					->set_content_type('application/json', 'utf-8')
					->set_output(json_encode($response, JSON_HEX_APOS | JSON_HEX_QUOT))
					->_display();
					exit();
				};
				
				}else{
				$response = [
				'status'=>false,
				'title'=>'Peringatan!!!',
				'msg'   =>validation_errors(),
				];
			}
			$this->output
			->set_status_header(200)
			->set_content_type('application/json', 'utf-8')
			->set_output(json_encode($response, JSON_HEX_APOS | JSON_HEX_QUOT))
			->_display();
			exit();
		}
		
		public function name_account()
		{
			
			$data = $this->masterdata->getAccountData();
			
			echo json_encode($data);
		}
		
		public function auto_account_id()
		{
			$name = $this->input->post('name',TRUE);
			
			
			$query = $this->masterdata->getAccountData();
			
			$result = array();
			foreach ($query as $row){
				$result[] = array(
				'id'		=>$row['id_akun'],
				'nama'		=>$row['nama'],
				);
			}
			
			$id = 0;
			foreach ($result as $res){
				if ($res['nama'] == $name){
					$id = $res['id'];
				}
			}
			
			echo json_encode($id);
		}
		
		public function auto_name()
		{
			$id = $this->input->post('id',TRUE);
			$query = $this->masterdata->getAccountDataById($id);
			echo json_encode($query);
		}
		
		public function auto_journal()
		{
			$id = $this->input->post('id');
			
			$data = $this->journal->get_jurnal_by_id($id);
			
			echo json_encode($data);
		}
		
		public function edit($id)
		{
			$data['title'] = 'Edit Data Transaksi';
			$data['user'] = $this->model_app->view_where('tb_users', array('id_user' => $this->iduser))->row_array();
			$id = decrypt_url($id);
			//menampilkan semua data
			$journal = $this->journal->get_jurnal_by_id($id);
			
			if(!empty($journal))
			{
				
				$data['data'] = $this->journal->get_jurnal_by_id($id);
				//menampilkan data akun
				$this->db->order_by('kelompok_akun.kel_akun', 'ASC');
				$data['akun'] = $this->masterdata->getAccountData();
				//perkiraan debit kredit
				$data['perkiraan'] = $this->journal->getPerkiraan();
				
				$this->form_validation->set_rules('noTransaksi','No Transaksi', 'trim|required');
				$this->form_validation->set_rules('keterangan','Keterangan', 'trim|required');
				
				if($this->form_validation->run() == false) {
					
					$this->template->load('main/themes','journal/edit_journal',$data);
					} else {
					// echo "Berhasil";
					$id 			= $this->input->post('id', true);
					$tglTransaksi 	= $this->input->post('tglTransaksi', true);
					$noTransaksi 	= $this->input->post('noTransaksi', true);
					$keterangan 	= $this->input->post('keterangan', true);
					$idAkun 		= $this->input->post('idAkun[]');
					$perkiraan 		= $this->input->post('perkiraan[]', true);
					$nominal1 		= $this->input->post('nominal[]', true);
					
					$hide 		= array("Rp", ".", " ");
					$nominal 	= str_replace($hide, "", $nominal1);
					
					$result = array();
					foreach($idAkun AS $key => $val){
						$result[] = array(
						'id_akun'		=>$_POST['idAkun'][$key],
						'debit_kredit'	=>$_POST['perkiraan'][$key], 
						'nilai'			=>$nominal[$key], 
						);
					};
					
					function findDuplicates($count) {
						return $count > 1;
					}
					
					// menampilkan data akun kas
					$data_kas = $this->kas->getArrayIdKas();
					$tipe_kas_array = array();
					foreach($data_kas as $row){
						if(in_array(strval($row), $idAkun)){
							$tipe_kas_array[] = 1;
							} else {
							$tipe_kas_array[] = 0;
						}
					}
					
					
					if(in_array(1, $tipe_kas_array)){
						$tipe_kas = 1;
						} else {
						$tipe_kas = 0;
					}
					
					
					// upload bukti transaksi
					$upload_bukti = $_FILES['bukti']['name'];
					$bukti_transaksi = "";
					if($upload_bukti){
						$new_name = time().rand();
						
						$config['allowed_types'] = 'pdf|jpg|jpeg|png|webp';
						$config['max_size']      = '10048';
						$config['upload_path']   = './uploads/lampiran/';
						$config['file_name']     = $new_name;
						
						$this->load->library('upload', $config);
						
						if($this->upload->do_upload('bukti')) {
							$uploadData = $this->upload->data();
							$finalUpload = bm_finalize_ci_upload_as_webp($uploadData, 80);
							if (!$finalUpload['status']) {
								echo $finalUpload['message'];
								return;
							}
							// hapus file lama
							unlink(FCPATH . 'uploads/lampiran/' . $data['data'][1]['bukti']);
							
							$bukti_transaksi = $finalUpload['file_name'];
							} else {
							echo $this->upload->dispay_errors();
						}
					}
					
					
					$error = array();
					// jika terdapat error
					
					if(count($idAkun) > 1){
						//akun tidak boleh sama
						$na_duplicat = array_unique(array_filter(array_count_values($idAkun), "findDuplicates"));
						
						if (!$na_duplicat){
							//ngecek debit dan kredit
							$debit_sum = 0;
							$kredit_sum = 0;
							$i = 0;
							foreach ($result as $d){
								if ($d['debit_kredit'] == 1){
									$debit_sum += $d['nilai'];
									} else {
									$kredit_sum += $d['nilai'];
								}
							}
							
							//jika melewati semuanya
							if($debit_sum == $kredit_sum && ($debit_sum != 0 || $kredit_sum != 0)) {
								// $array = array($id, $tglTransaksi, $noTransaksi, $keterangan, $idAkun, $nominal, $tipe_kas);
								// echo json_encode($array);
								$this->journal->changeJournal($id, $tglTransaksi, $noTransaksi, $keterangan, $idAkun, $nominal, $tipe_kas, $bukti_transaksi);
								$this->session->set_flashdata('message_jurnal','<div class="alert alert-success" role="alert">Data transaksi berhasil di update!
								<button type="button" class="close" data-dismiss="alert" aria-label="Close">
								<span aria-hidden="true">&times;</span>
								</button></div>');
								redirect('journal');
							}
							
							//cek debit dan kredit
							if(in_array(1, $perkiraan) && !in_array(2, $perkiraan) || in_array(2, $perkiraan) && !in_array(1, $perkiraan)){
								$error[] = "Harus ada akun diposisi debit maupun kredit!";
							}
							
							//jika nominal debit 0 dan kredit 0
							if($debit_sum == 0 || $kredit_sum == 0){
								$error[] = "Jumlah data debit maupun kredit tidak boleh 0!";
							}
							
							//jika jumlah debit tidak balance dengan kredit
							if($debit_sum != $kredit_sum){
								$error[] = "Jumlah debit harus sama dengan jumlah kredit!";
							}
							
						}
						else {
							$error[] = "Data akun yang digunakan tidak boleh sama!";
						}
						} else {
						//jika hanya ada satu data yang dikirim
						$error[] = "Akun yang dimasukkan minimal 2 akun!";
					};
					
					if(!empty($error)) {
						$id = encrypt_url($id);
						foreach ($error as $value) {
							$this->session->set_flashdata('message_jurnal','<div class="alert alert-danger" role="alert">'.$value.'
							<button type="button" class="close" data-dismiss="alert" aria-label="Close">
							<span aria-hidden="true">&times;</span>
							</button></div>');
							redirect('journal/edit/'.$id);
						}
					}
				}
				}else{
				$this->load->view('errors/404',$data);
			}
		}
		
		public function update_journal()
		{
			if ( $this->input->is_ajax_request() ) 
			{
				simpan_demo("Update");
				$rules = array(
				array(
				'field' => 'noTransaksi',
				'label' => 'no Transaksi',
				'rules' => 'required|trim|numeric',
				),
				
				array(
				'field' => 'idAkun[]',
				'label' => 'Id Akun',
				'rules' => 'required|trim',
				),
				
				array(
				'field' => 'nominal[]',
				'label' => 'nominal',
				'rules' => 'required|trim',
				),
				
				array(
				'field' => 'perkiraan[]',
				'label' => 'perkiraan',
				'rules' => 'required|trim',
				),
				
				);
				$this->form_validation->set_rules($rules);
				
				if ( $this->form_validation->run() ) 
				{
					$id = decrypt_url($this->input->post('id', true));
					$journal = $this->journal->get_jurnal_by_id($id);
					if(!empty($journal))
					{
						$data['data'] = $this->journal->get_jurnal_by_id($id);
						//menampilkan data akun
						$this->db->order_by('kelompok_akun.kel_akun', 'ASC');
						$data['akun'] = $this->masterdata->getAccountData();
						//perkiraan debit kredit
						$data['perkiraan'] = $this->journal->getPerkiraan();
						
						// echo "Berhasil";
						
						$tglTransaksi 	= $this->input->post('tglTransaksi', true);
						$noTransaksi 	= $this->input->post('noTransaksi', true);
						$keterangan 	= $this->input->post('keterangan', true);
						$idAkun 		= $this->input->post('idAkun[]');
						$perkiraan 		= $this->input->post('perkiraan[]', true);
						$nominal1 		= $this->input->post('nominal[]', true);
						
						$hide 		= array("Rp", ".", " ");
						$nominal 	= str_replace($hide, "", $nominal1);
						
						$result = array();
						foreach($idAkun AS $key => $val){
							$result[] = array(
							'id_akun'		=>$_POST['idAkun'][$key],
							'debit_kredit'	=>$_POST['perkiraan'][$key], 
							'nilai'			=>$nominal[$key], 
							);
						};
						
						function findDuplicates($count) {
							return $count > 1;
						}
						
						// menampilkan data akun kas
						$data_kas = $this->kas->getArrayIdKas();
						$tipe_kas_array = array();
						foreach($data_kas as $row){
							if(in_array(strval($row), $idAkun)){
								$tipe_kas_array[] = 1;
								} else {
								$tipe_kas_array[] = 0;
							}
						}
						
						
						if(in_array(1, $tipe_kas_array)){
							$tipe_kas = 1;
							} else {
							$tipe_kas = 0;
						}
						
						
						// upload bukti transaksi
						$upload_bukti = $_FILES['bukti']['name'];
						$bukti_transaksi = "";
						if($upload_bukti){
							$new_name = time().rand();
							
							$config['allowed_types'] = 'pdf|jpg|jpeg|png|webp';
							$config['max_size']      = '10048';
							$config['upload_path']   = './uploads/lampiran/';
							$config['file_name']     = $new_name;
							
							$this->load->library('upload', $config);
							
							if($this->upload->do_upload('bukti')) {
								$uploadData = $this->upload->data();
								$finalUpload = bm_finalize_ci_upload_as_webp($uploadData, 80);
								if (!$finalUpload['status']) {
									echo $finalUpload['message'];
									return;
								}
								// hapus file lama
								unlink(FCPATH . 'uploads/lampiran/' . $data['data'][1]['bukti']);
								
								$bukti_transaksi = $finalUpload['file_name'];
								} else {
								echo $this->upload->dispay_errors();
							}
						}
						
						
						$error = array();
						// jika terdapat error
						
						if(count($idAkun) > 1){
							//akun tidak boleh sama
							$na_duplicat = array_unique(array_filter(array_count_values($idAkun), "findDuplicates"));
							
							if (!$na_duplicat){
								//ngecek debit dan kredit
								$debit_sum = 0;
								$kredit_sum = 0;
								$i = 0;
								foreach ($result as $d){
									if ($d['debit_kredit'] == 1){
										$debit_sum += $d['nilai'];
										} else {
										$kredit_sum += $d['nilai'];
									}
								}
								
								//jika melewati semuanya
								if($debit_sum == $kredit_sum && ($debit_sum != 0 || $kredit_sum != 0)) {
									
									$this->journal->changeJournal($id, $tglTransaksi, $noTransaksi, $keterangan, $idAkun, $nominal, $tipe_kas, $bukti_transaksi);
									$response = [
									'status'=>true,
									'title'=>'Update!!!',
									'msg'   =>'Data transaksi berhasil di update!',
									];
									$this->output
									->set_status_header(200)
									->set_content_type('application/json', 'utf-8')
									->set_output(json_encode($response, JSON_HEX_APOS | JSON_HEX_QUOT))
									->_display();
									exit();
								}
								
								//cek debit dan kredit
								if(in_array(1, $perkiraan) && !in_array(2, $perkiraan) || in_array(2, $perkiraan) && !in_array(1, $perkiraan)){
									
									$response = [
									'status'=>false,
									'title'=>'Peringatan!!!',
									'msg'   =>'Harus ada akun diposisi debit maupun kredit!',
									];
									$this->output
									->set_status_header(200)
									->set_content_type('application/json', 'utf-8')
									->set_output(json_encode($response, JSON_HEX_APOS | JSON_HEX_QUOT))
									->_display();
									exit();
								}
								
								//jika nominal debit 0 dan kredit 0
								if($debit_sum == 0 || $kredit_sum == 0){
									
									$response = [
									'status'=>false,
									'title'=>'Peringatan!!!',
									'msg'   =>'Jumlah data debit maupun kredit tidak boleh 0!',
									];
									$this->output
									->set_status_header(200)
									->set_content_type('application/json', 'utf-8')
									->set_output(json_encode($response, JSON_HEX_APOS | JSON_HEX_QUOT))
									->_display();
									exit();
								}
								
								//jika jumlah debit tidak balance dengan kredit
								if($debit_sum != $kredit_sum){
									
									$response = [
									'status'=>false,
									'title'=>'Peringatan!!!',
									'msg'   =>'Jumlah debit harus sama dengan jumlah kredit!',
									];
									$this->output
									->set_status_header(200)
									->set_content_type('application/json', 'utf-8')
									->set_output(json_encode($response, JSON_HEX_APOS | JSON_HEX_QUOT))
									->_display();
									exit();
								}
								
							}
							else {
								$response = [
								'status'=>false,
								'title'=>'Peringatan!!!',
								'msg'   =>'Data akun yang digunakan tidak boleh sama!',
								];
								$this->output
								->set_status_header(200)
								->set_content_type('application/json', 'utf-8')
								->set_output(json_encode($response, JSON_HEX_APOS | JSON_HEX_QUOT))
								->_display();
								exit();
							}
							} else {
							//jika hanya ada satu data yang dikirim
							
							$response = [
							'status'=>false,
							'title'=>'Peringatan!!!',
							'msg'   =>'Akun yang dimasukkan minimal 2 akun!',
							];
							$this->output
							->set_status_header(200)
							->set_content_type('application/json', 'utf-8')
							->set_output(json_encode($response, JSON_HEX_APOS | JSON_HEX_QUOT))
							->_display();
							exit();
						};
						
						}else{
						$response = [
						'status'=>false,
						'title'=>'Peringatan!!!',
						'msg'   =>'Journal tidak ditemukan!',
						];
						$this->output
						->set_status_header(200)
						->set_content_type('application/json', 'utf-8')
						->set_output(json_encode($response, JSON_HEX_APOS | JSON_HEX_QUOT))
						->_display();
						exit();
					}
					
					}else{
					$response = [
					'status'=>false,
					'title'=>'Peringatan!!!',
					'msg'   =>validation_errors(),
					];
				}
				$this->output
				->set_status_header(200)
				->set_content_type('application/json', 'utf-8')
				->set_output(json_encode($response, JSON_HEX_APOS | JSON_HEX_QUOT))
				->_display();
				exit();
			}
		}
		
		public function hapus_journal()
		{
			// menghapus gambar
			cek_nput_post('GET');
			simpan_demo("Hapus");
			$id 	= $this->input->post('id', true);
			$id = decrypt_url($id);
			
			$data['data'] = $this->journal->get_jurnal_by_id($id);
			if(!empty($data['data'][1]['bukti'])){
				unlink(FCPATH . 'uploads/lampiran/' . $data['data'][1]['bukti']);
			}
			$hapus = $this->journal->delete_by_id($id);
			// dump($hapus);
			if($hapus==true)
			{
				$response = [
				'status'=>true,
				'title'=>'Peringatan!!!',
				'msg'   =>'Berhasil',
				];
				}else{
				$response = [
				'status'=>false,
				'title'=>'Peringatan!!!',
				'msg'   =>'Gagal',
				];
			}
			$this->output
			->set_status_header(200)
			->set_content_type('application/json', 'utf-8')
			->set_output(json_encode($response, JSON_HEX_APOS | JSON_HEX_QUOT))
			->_display();
			exit();
			
		}
		 
		public function printgeneraljournal()
		{
			$tglAwal 	= $this->input->post('tglAwal', true);
			$tglAkhir 	= $this->input->post('tglAkhir', true);
			
			$data['profile'] = $this->db->get_where('info', ['id' => 1])->row_array();
			
			$data['journal'] =  $this->journal->reportJournal($tglAwal,$tglAkhir);
			$data['tglAwal'] = $tglAwal;
			$data['tglAkhir'] = $tglAkhir;
			
			$this->load->library('fpdf');
			$this->load->view('report/pdf/general_journal', $data);
		}
	}
	
