<?php
class M_deposit extends CI_Model {
    
	public $table 	= "at_cashdeposit";
	public $account = "at_compaccountgl";
	
	//custom
	function get_coa_bank($replace_branch, $currency_idr, $currency_code){
	    $jobtitle = $this->session->userdata("jobtitle");
		//$BranchCode 	= $this->session->userdata('branch');
		$BranchStatus 	= $this->session->userdata('branch_status');
		
		$this->db->select('am_accountgl.AccountNumber, am_accountgl.AccountName, am_accountgl.CurrencyCode');
		$this->db->from($this->account);
		$this->db->join('am_accountgl','at_compaccountgl.AccountNumber=am_accountgl.AccountNumber','inner');		
		$this->db->join('am_branch','at_compaccountgl.BranchCode=am_branch.BranchCode','inner');
		$this->db->join('am_accountsubtype','am_accountsubtype.AccountSubTypeID=am_accountgl.AccountSubTypeID','inner');		
		
		$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
		$this->db->where('at_compaccountgl.BranchCode', $replace_branch);
		
		if($currency_code != "IDR"){
			$arry_currency = array($currency_idr,$currency_code);
			$this->db->where_in('am_accountgl.CurrencyCode', $arry_currency);
		}
		
		$this->db->where('am_accountsubtype.AccountSubTypeID', 'Bank');
		$this->db->where('am_accountgl.AccountChild', 1);
		$this->db->where('am_accountgl.TransStatus', 2);
		
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	
	function get_coa_bank_add($CompanyCode, $str_replace_branch, $currency_idr, $currency_code){
	    $jobtitle = $this->session->userdata("jobtitle");
		$BranchCode 	= $this->session->userdata('branch');
		$BranchStatus 	= $this->session->userdata('branch_status');
		
		$this->db->select('am_accountgl.AccountNumber, am_accountgl.AccountName, am_accountgl.CurrencyCode');
		$this->db->from($this->account);
		$this->db->join('am_accountgl','at_compaccountgl.AccountNumber=am_accountgl.AccountNumber','inner');		
		$this->db->join('am_branch','at_compaccountgl.BranchCode=am_branch.BranchCode','inner');
		$this->db->join('am_accountsubtype','am_accountsubtype.AccountSubTypeID=am_accountgl.AccountSubTypeID','inner');		
		
		if($this->session->userdata('jobtitle_status') != 0){
			if($BranchStatus == 0){
				$this->db->where('at_compaccountgl.CompanyCode', $CompanyCode);
				$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
				$this->db->where('am_branch.BranchStatus', 0);
			} else {
				$this->db->where('at_compaccountgl.CompanyCode', $CompanyCode);
				$this->db->where('at_compaccountgl.BranchCode', $str_replace_branch);
				$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
			}	
		}
		else{
			$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
			$this->db->where('at_compaccountgl.BranchCode', $str_replace_branch);
		}
		
		if($currency_code != "IDR"){
			$arry_currency = array($currency_idr,$currency_code);
			$this->db->where_in('am_accountgl.CurrencyCode', $arry_currency);
		}
		
		$this->db->where('am_accountsubtype.AccountTypeName', 'Bank');
		$this->db->where('am_accountgl.AccountChild', 1);
		$this->db->where('at_compaccountgl.TransStatus', 2);
		
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	
	function get_petty_cash_add($CompanyCode, $BranchCode){
	    $jobtitle = $this->session->userdata("jobtitle");
		$BranchStatus 	= $this->session->userdata('branch_status');
		
		$this->db->select('am_accountgl.AccountNumber, am_accountgl.AccountName, am_accountgl.CurrencyCode');
		$this->db->from($this->account);
		$this->db->join('am_accountgl','at_compaccountgl.AccountNumber=am_accountgl.AccountNumber','inner');
		$this->db->join('am_branch','at_compaccountgl.BranchCode=am_branch.BranchCode','inner');
		$this->db->join('am_accountsubtype','am_accountsubtype.AccountSubTypeID=am_accountgl.AccountSubTypeID','inner');
		
		if($BranchStatus == 0){
			if($this->session->userdata('jobtitle_status') != 0){
				$this->db->where('at_compaccountgl.CompanyCode', $CompanyCode);
				$this->db->where('at_compaccountgl.BranchCode', $BranchCode);
				$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
			}
			else{
				$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
				$this->db->where('at_compaccountgl.BranchCode', $BranchCode);
			}
		} else {
			if($this->session->userdata('jobtitle_status') != 0){
				$this->db->where('at_compaccountgl.CompanyCode', $CompanyCode);
				$this->db->where('at_compaccountgl.BranchCode', $BranchCode);
				$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
			}
			else{
				$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
				$this->db->where('at_compaccountgl.BranchCode', $BranchCode);
			}
        }			
		
		$this->db->where('am_accountsubtype.AccountTypeName', 'Petty Cash');
		$this->db->where('am_accountgl.AccountChild', 1);
		$this->db->where('at_compaccountgl.TransStatus', 2);
		
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	
	function get_petty_cash($CompanyCode, $replace_branch){
	    $jobtitle = $this->session->userdata("jobtitle");
		$BranchStatus 	= $this->session->userdata('branch_status');
		
		$this->db->select('am_accountgl.AccountNumber, am_accountgl.AccountName, am_accountgl.CurrencyCode');
		$this->db->from($this->account);
		$this->db->join('am_accountgl','at_compaccountgl.AccountNumber=am_accountgl.AccountNumber');
		$this->db->join('am_branch','at_compaccountgl.BranchCode=am_branch.BranchCode');
		$this->db->join('am_accountsubtype','am_accountsubtype.AccountSubTypeID=am_accountgl.AccountSubTypeID','inner');
		
		$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
		$this->db->where('at_compaccountgl.BranchCode', $replace_branch);			
		
		$this->db->where('am_accountsubtype.AccountTypeName', 'Petty Cash');
		$this->db->where('am_accountgl.AccountChild', 1);
		$this->db->where('am_accountgl.TransStatus', 2);
		
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	//end
	
	function count_deposit_number($bulan, $cek_tahun, $BranchCode){
	    $this->db->select('*');
		$this->db->from('at_cashdeposit');
		$this->db->where('MONTH(CDepDate)', $bulan);
		$this->db->where('YEAR(CDepDate)', $cek_tahun);
		$this->db->where('BranchCode', $BranchCode);
		return $this->db->get()->num_rows();
	}
	
	function get_last_deposit_number($BranchCode){
		$qty="SELECT MAX(CDepNumber) as CDepNumber FROM at_cashdeposit WHERE BranchCode='$BranchCode'";
		return $this->db->query($qty)->result();
	}
	
	function create_dep_number($date){        
        $branch_code    = $this->session->userdata('branch');
        $month          = date('m', strtotime($date));
        $year           = date('y', strtotime($date));
	
        $dep_number    = $branch_code."/".$month.$year."CD/";

        $this->db->like('CDepNumber',$dep_number);
	
        $num    = $this->db->get($this->table)->num_rows()+1;
        $long   = strlen($num);
		
        if($long == 1){
            $number='00'.$num;
        }
        elseif($long==2){
            $number='0'.$num;
        }
        else{
            $number=$num;
        }
		
        return  $dep_number.$number;
    }
	
	function insert_deposit($data_deposit){
		
		$this->db->insert($this->table, $data_deposit);
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			//$this->db->trans_commit();
			return 1;
		}
	}
	
	function insert_journal_debit_deposit($journal_debet){
	    
		$this->db->insert('at_cdepjournal', $journal_debet);
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			//$this->db->trans_commit();
			return 1;
		}
	}
	
	function insert_journal_credit_deposit($journal_credit){
	    
		$this->db->insert('at_cdepjournal', $journal_credit);
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			//$this->db->trans_commit();
			return 1;
		}
	}
	
	function update_deposit($data_deposit, $txt_deposit_no){
		$this->db->where('CDepNumber', $txt_deposit_no);
		$this->db->update($this->table, $data_deposit);
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			//$this->db->trans_commit();
			return 1;
		}
		
	}
	
	function delete_deposit($txt_deposit_no){
		$this->db->where('CDepNumber', $txt_deposit_no);
		$this->db->delete('at_cdepjournal');
		if ($this->db->trans_status() === FALSE) {
			//$this->db->trans_rollback();
			return 0;
		}
		else {
			//$this->db->trans_commit();
			return 1;
		}
	}
	
	function update_journal_appfinis($stat_appfinished, $deposit_number){
		$this->db->where('CDepNumber', $deposit_number);
		$this->db->update('at_cdepjournal', $stat_appfinished);
		
		/*if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			//$this->db->trans_commit();
			return 1;
		}*/
	}
	
	function update_journal_debit_deposit($journal_debet, $txt_deposit_no){
		$this->db->where('CDepNumber', $txt_deposit_no);
		$this->db->where('CDepSaldoCredit', 0);
		$this->db->update('at_cdepjournal', $journal_debet);
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			//$this->db->trans_commit();
			return 1;
		}
	}
	
	function update_journal_credit_deposit($journal_credit, $txt_deposit_no){
		$this->db->where('CDepNumber', $txt_deposit_no);
		$this->db->where('CDepSaldoDebet', 0);
		$this->db->update('at_cdepjournal', $journal_credit);
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			//$this->db->trans_commit();
			return 1;
		}
	}
	
	function cek_trans_status($id){
		$this->db->select('TransStatus');
		$this->db->from('at_cashdeposit');
		$this->db->where('CDepNumber', $id);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $TransStatus = $row->TransStatus;
            }
            return $TransStatus;
        }
        else {
            return FALSE;
        }	
	}
	
	function cek_app_mgr($id){
		$this->db->select('AppLevel1Status');
		$this->db->from('at_cashdeposit');
		$this->db->where('CDepNumber', $id);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AppLevel1Status = $row->AppLevel1Status;
            }
            return $AppLevel1Status;
        }
        else {
            return FALSE;
        }	
	}
	
	function pembatalan_transaksi($id, $data){
		$this->db->where('CDepNumber', $id);
		$this->db->update($this->table, $data);
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			//$this->db->trans_commit();
			return 1;
		}
	}
	
	function suspend($data, $id) {
        $this->db->update($this->table, $data, array('CDepNumber' => $id));
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return 0;
		}
		else {
			//$this->db->trans_commit();
			return 1;
		}
	}
	
	
	function get_detail($id){
        $query = $this->db->get_where($this->table, array('CDepNumber' => $id));
		if($query->num_rows()>0){
			return $query->row();
		}
		else{
			return false;
		}      
    }
	
	function get_account_name_bank_payment($BankPayment){
		$this->db->select('am_accountgl.AccountName');
		$this->db->from('am_accountgl');
		$this->db->join('at_compaccountgl','am_accountgl.AccountNumber=at_compaccountgl.AccountNumber');
		$this->db->where('am_accountgl.AccountNumber', $BankPayment);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AccountName = $row->AccountName;
            }
            return $AccountName;
        }
        else {
		    $AccountName = 0;
            return $AccountName;
        }	
	}
	
	function get_account_name_bank_receipt($BankReceipt){
		$this->db->select('am_accountgl.AccountName');
		$this->db->from('am_accountgl');
		$this->db->join('at_compaccountgl','am_accountgl.AccountNumber=at_compaccountgl.AccountNumber');
		$this->db->where('am_accountgl.AccountNumber', $BankReceipt);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AccountName = $row->AccountName;
            }
            return $AccountName;
        }
        else {
		    $AccountName = 0;
            return $AccountName;
        }	
	}
	
	function get_date_journal_deposit($deposit_number){
		$this->db->select('*, am_company.CompanyName');
		$this->db->from($this->table);
		$this->db->join('am_branch','am_branch.BranchCode=at_cashdeposit.BranchCode');
		$this->db->join('am_company','am_company.CompanyCode=am_branch.CompanyCode');
		$this->db->where('CDepNumber', $deposit_number);
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	
	function get_branch($deposit_number){
		$this->db->select('at_cashdeposit.BranchCode');
		$this->db->from($this->table);
		$this->db->join('am_branch','am_branch.BranchCode=at_cashdeposit.BranchCode');
		$this->db->join('am_company','am_company.CompanyCode=am_branch.CompanyCode');
		$this->db->where('CDepNumber', $deposit_number);
		$query = $this->db->get();
		
		$result=$query->row();
		return $result;
	}
	
	function get_all_journal_deposit($deposit_number, $branch){
		$this->db->select('am_accountgl.AccountNumber, am_accountgl.AccountName, at_cdepjournal.CDepSaldoCredit, 
						   at_cdepjournal.CDepSaldoDebet, at_cdepjournal.CDepDesc, at_cdepjournal.CDepNumber');
		$this->db->from('at_compaccountgl');
		$this->db->join('at_cdepjournal','at_compaccountgl.AccountNumber=at_cdepjournal.CDepAccountNumber');
		$this->db->join('am_accountgl','at_compaccountgl.AccountNumber=am_accountgl.AccountNumber');
		
		$this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
		$this->db->where('at_compaccountgl.BranchCode', $branch);
			
		$this->db->where('at_cdepjournal.CDepNumber', $deposit_number);
		$query = $this->db->get();
		
		$result=$query->result();
		return json_encode($result);
	}
	
	function approval_status($data,$deposit_number){
		$this->db->where('CDepNumber', $deposit_number);
		return $this->db->update($this->table, $data);
	}

	//cancel status
	function cancel_status($data,$deposit_number){
		$this->db->where('CDepNumber', $deposit_number);
		$this->db->update($this->table, $data);

		$rows 	= $this->db->affected_rows();
        $return = array(
            'affected' => $rows
        );
        return $return;
	}
	
	function cek_app($deposit_number){
		$this->db->select('AppLevel1Status');
		$this->db->from('at_cashdeposit');
		$this->db->where('CDepNumber', $deposit_number);
		$this->db->where('AppLevel1Status', 1);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $app = 1;
            }
            return $app;
        }
        else {
		    $app = 0;
            return $app;
        }	
	}
	
	function cek_status_app_mgr($deposit_number){
		$this->db->select('AppLevel1Status');
		$this->db->from('at_cashdeposit');
		$this->db->where('CDepNumber', $deposit_number);
		$this->db->where('AppLevel1Status', 1);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AppLevel1Status = $row->AppLevel1Status;
            }
            return $AppLevel1Status;
        }
        else {
		    return FALSE;
        }	
	}
	
	function cek_status_app_dir($deposit_number){
		$this->db->select('AppLevel2Status');
		$this->db->from('at_cashdeposit');
		$this->db->where('CDepNumber', $deposit_number);
		$this->db->where('AppLevel2Status', 1);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AppLevel2Status = $row->AppLevel2Status;
            }
            return $AppLevel2Status;
        }
        else {
		    return FALSE;
        }	
	}
	
	function status_finished($app_finished, $deposit_number){
		$this->db->where('CDepNumber', $deposit_number);
		return $this->db->update('at_cashdeposit', $app_finished);
	}
}