<?php
	defined('BASEPATH') OR exit('No direct script access allowed');
	
	class Kas_model extends CI_Model
	{
		// menampilkan semua
		public function getAllData()
		{
			$this->db->select('*');
			$this->db->from('set_account_kas');
			$this->db->join('akun', 'set_account_kas.id_akun = akun.id_akun');
			$this->db->order_by("id_sak", "asc");
			$query = $this->db->get()->result_array();
			return $query;
		}
		
		public function getTotalSaldo()
		{
			$dt_kas = $this->getAllData();
			$tot_saldo = 0;
			foreach($dt_kas as $row){
				$tot_saldo += $row['saldo_awal'];
			}
			
			return $tot_saldo;
		}
		
		function getIdAkunKas()
		{
			$this->db->select('set_account_kas.id_akun');
			$this->db->from('set_account_kas');
			$this->db->order_by("id_sak", "asc");
			$query = $this->db->get()->result_array();
			return $query;
		}
		
		public function getArrayIdKas()
		{
			$akun = $this->getIdAkunKas();
			
			$array_result = array();
			foreach($akun as $row){
				$array_result[] = intval($row['id_akun']);
			}
			
			return $array_result;
		}
		
		// menampilkan data berdasarkan id
		public function getDataById($id)
		{
			return $this->db->get_where('set_account_kas', ['id_sak' => $id])->row_array();
		}
		
		// menambah data kas
		public function insert()
		{
			$data = [
			'nm' => $this->input->post('nm_kas', true),
			'desk' => $this->input->post('desk', true),
			'id_akun' => $this->input->post('id_akun', true),
			'date_created' => time(),
			];
			
			$this->db->insert('set_account_kas', $data);
		}
		
		// mengubah data kas
		public function update($id)
		{
			$data = [
			'nm' => $this->input->post('nm', true),
			'desk' => $this->input->post('desk', true),
			'id_akun' => $this->input->post('id_akun', true),
			];
			
			$this->db->where('id_sak', $id);
			$this->db->update('set_account_kas', $data);
		}
		// hapus data kas
		public function delete($id)
		{
			return $this->db->delete('set_account_kas', ['id_sak' => $id]);
		}
		function getKasKeluar($params = array()){
			// print_r($params);
			
			$this->db->select('*');
			$this->db->from('kas_keluar');
			
			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']['user'])){ 
					$this->db->where('report_pesan.target', $params['search']['user']); 
				} 
				if(!empty($params['search']['keywords'])){ 
					$this->db->like('report_pesan.message', $params['search']['keywords']); 
				} 
				
			} 
			if(!empty($params['search']['sortBy'])){ 
				$this->db->order_by('`report_pesan`.`id`', $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')){
					
					$this->db->order_by('report_pesan.id', 'DESC'); 
					$query = $this->db->get(); 
					$result = ($query->num_rows() > 0)?$query->row():FALSE; 
					
					}else{
					
					$this->db->order_by('report_pesan.id', 'DESC'); 
					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():FALSE; 
				} 
			} 
			
			// Return fetched data 
			return $result; 
		}
	}	