<?php
	defined('BASEPATH') OR exit('No direct script access allowed');
	
	class Masterdata_model extends CI_Model
	{
		protected $table = "akun";
		
		public function check_number_noakun()
		{
			$query = $this->db->query("SELECT MAX(noakun) as no_trans from akun");
			$hasil = $query->row();
			return $hasil->no_trans;
		}
		
		function get_Akun($params = array()){
			$this->db->select('akun.*, kelompok_akun.nama_kel_akun, kelompok_akun.kel_akun');
			$this->db->from('akun');
			$this->db->join('kelompok_akun', 'akun.id_kelompok_akun = kelompok_akun.id_kelompok_akun');
			
			if(array_key_exists("where", $params)){
				foreach($params['where'] as $key => $val){ 
					$this->db->where($key, $val); 
				} 
			}  
			
			if(array_key_exists("search", $params)){
				if(!empty($params['search']['keywords'])){ 
					$this->db->like('akun.noakun', $params['search']['keywords']); 
					$this->db->or_like('akun.nama', $params['search']['keywords']); 
					// $this->db->group_by('jurnal_detail.id_jurnal');
				} 
			}
			
			// Sort data by ascending or desceding order 
			if(!empty($params['search']['sortBy'])){ 
				$this->db->order_by('kelompok_akun.kel_akun', $params['search']['sortBy']);
				$this->db->order_by('akun.noakun', $params['search']['sortBy']);
			}
			
			if(array_key_exists("returnType",$params) && $params['returnType'] == 'count'){
				$result = $this->db->count_all_results(); 
				}else{ 
				if(array_key_exists("id", $params) || (array_key_exists("returnType", $params) && $params['returnType'] == 'single')){ 
					
					$query = $this->db->get(); 
					$result = ($query->num_rows() > 0)?$query->result_array():FALSE; 
					}else{
					$this->db->order_by('kelompok_akun.kel_akun', 'ASC');
					$this->db->order_by('akun.noakun', 'ASC');
					
					if(array_key_exists("start",$params) && array_key_exists("limit",$params)){ 
						
						$this->db->limit($params['limit'],$params['start']); 
						}elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){ 
						
						$this->db->limit($params['limit']); 
					}
					
					$query = $this->db->get(); 
					$result = ($query->num_rows() > 0)?$query->result_array():FALSE; 
				} 
			} 
			
			// Return fetched data 
			return $result; 
		}
		
		public function getAccountData()
		{
			$this->db->select('akun.*, kelompok_akun.nama_kel_akun, kelompok_akun.kel_akun');
			$this->db->from('akun');
			$this->db->join('kelompok_akun', 'akun.id_kelompok_akun = kelompok_akun.id_kelompok_akun');
			$this->db->order_by('kelompok_akun.kel_akun', 'ASC');
			$this->db->order_by('akun.noakun', 'ASC');
			$query = $this->db->get()->result_array();
			return $query;
		}
		
		public function set_account_group_id($id){
			$this->db->where_in('kelompok_akun.kel_akun', $id);
		}
		
		public function insertData($saldoAwal)
		{
			$data = [
	        "noakun" => $this->input->post('noAkun', true),
	        "id_kelompok_akun" => $this->input->post('tipeAkun', true),
	        "nama" => $this->input->post('namaAkun', true),
	        "id_perkiraan" => $this->input->post('perkiraan', true),
	        "saldo_awal" => $saldoAwal,
        	"id_user" => $this->input->post('idUser', true)
			];
			
			if($this->db->insert('akun', $data))
            {
                $arr['status'] =  true;
                $arr['msg'] =  "Input berhasil";
                $arr['id'] = $this->db->insert_id();
                }else{
                $arr['status'] =false;
                $arr['msg'] =  "Gagal input data";
                $arr['id'] =  "";
			}
			
            return $arr;
			
		}
		
		public function getAccountDataById($id)
		{	
			$this->db->select('akun.*, kelompok_akun.nama_kel_akun, kelompok_akun.kel_akun');
			$this->db->from('akun');
			$this->db->join('kelompok_akun', 'akun.id_kelompok_akun = kelompok_akun.id_kelompok_akun');
			$this->db->where('akun.id_akun', $id);
			$query = $this->db->get()->row_array();
			return $query;
		}
		
		public function getAccountGroupName($id)
		{
			return $this->db->get_where('kelompok_akun', ['id_kelompok_akun' => $id])->row_array();
		}
		
		public function deleteAccountData($id)
		{
			return $this->db->delete('akun', ['id_akun' => $id]);
		}
		public function deleteRekening($id)
		{
			return $this->db->delete('rekening_bank', ['id_akun' => $id]);
		}
		public function getPerkiraan()
		{
			return $this->db->get('perkiraan')->result_array();
		}
		
		public function getIdPerkiraan($id)
		{
			return $this->db->get_where('perkiraan', ['id_perkiraan' => $id])->row_array();
		}
		
		public function editAccountData($saldoAwal)
		{
			 
			$data = [
			"noAkun" => $this->input->post('noAkun', true),
			"nama" => $this->input->post('namaAkun', true),
			"id_kelompok_akun" => $this->input->post('tipeAkun', true),
			"id_perkiraan" => $this->input->post('perkiraan', true),
			"saldo_awal" => $saldoAwal,
			"id_user" => $this->input->post('idUser', true)
			];
			
			$this->db->where('id_akun', decrypt_url($this->input->post('id')));
			if($this->db->update($this->table, $data))
            {
                $arr['status'] =  true;
                $arr['msg'] =  "Update Berhasil";
                }else{
                $arr['status'] =false;
                $arr['msg'] =  "Update Gagal";
			}
			return $arr;
		}
		public function update($params = array(), $where = array())
		{
			$this->db->update($this->table, $params, $where);
			return $this->db->affected_rows() > 0 ? true : false;
		}
		public function cek_id($id)
		{
			$query = $this->db->select('id_akun');
			$query = $this->db->where('id_akun', $id);
			$query = $this->db->get($this->table);
			$result = $query->num_rows();
			
			if ( $result < 1 )
			$result = 0;
			
			return $result;
		}
		
		public function cek_no_akun($id, $noakun)
		{
			$query = $this->db
			->select('id_akun')
			->where("BINARY noakun = '$noakun'", NULL, FALSE)
			->get($this->table);
			
			if (
		    $query->num_rows() == 1 && 
		    $query->row_array()['id_akun'] == $id || 
		    $query->num_rows() == 0
			) 
			{
				return TRUE;
			}
			else 
			{
				return FALSE;
			}
		}
		public function cek_nama_akun($id, $nama)
		{
			$query = $this->db
			->select('id_akun')
			->like('nama',$nama)
			->get($this->table);
			
			if (
		    $query->num_rows() == 1 && 
		    $query->row_array()['id_akun'] == $id || 
		    $query->num_rows() == 0
			) 
			{
				return TRUE;
			}
			else 
			{
				return FALSE;
			}
		}
	}										