<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_sales_dp extends CI_Model {
	public $table 			= "at_salesdp";
	public $detail	 		= "at_sdpdetail";
	public $journal	 		= "at_sdpjournal";
	public $detail_dp 		= "at_sorddetaildp";
	public $branch          = "am_branch";
	public $invoicereceive  = "at_invoicereceipt";
	public $account 		= "at_compaccountgl";
	public $m_account 		= "am_accountgl";
    public $customer        = "am_customer";
	public $employee        = "am_employee";
	
	function get_down_payment($CustomerCode){
		$this->db->select('dp.SOrdNumber, sum(dp.SaldoDPBalance - dp.DPUsed) as SaldoDPBalance, dp.CurrencyRate, sum((dp.SaldoDPBalance - dp.DPUsed) * dp.CurrencyRate) as SumDp_rate,
					sum((dp.SaldoDPBalance - dp.DPUsed) / dp.CurrencyRate) as SumDp_norate, cust.CurrencyCode
					');
        $this->db->from($this->detail_dp . ' as dp');
		$this->db->join($this->customer . ' as cust', "dp.CustomerCode = cust.CustomerCode", "INNER");
        //$this->db->join($this->invoicereceive . ' as invr', "invr.InvoiceNumber = dp.SOrdNumber", "INNER");
		
        $this->db->where('dp.CustomerCode', $CustomerCode); 
		//$this->db->where_in('dp.IsEdit', array(1,0));
		$this->db->where('(dp.SaldoDPBalance - dp.DPUsed) > ', 0);
		$this->db->group_by('dp.SOrdNumber');
		
        $query = $this->db->get();
        
        if ($query->num_rows() > 0) {
			return $query->result();
		}
		else {
			return FALSE;
		}	
	}
	
	function get_down_payment_for_row($CustomerCode, $SordNumber){
		$this->db->select("dp.SOrdNumber, sum(dp.SaldoDPBalance - dp.DPUsed) as SaldoDPBalance, dp.CurrencyRate, sum((dp.DPUsed) * dp.CurrencyRate) as SumDp_rate,
				sum((dp.DPUsed) / dp.CurrencyRate) as SumDp_norate");
        $this->db->from($this->detail_dp . ' as dp');
        $this->db->join($this->customer . ' as cust', "dp.CustomerCode = cust.CustomerCode", "INNER");
		
        $this->db->where('dp.CustomerCode', $CustomerCode); 
		$this->db->where('dp.SOrdNumber', $SordNumber); 
		 
		//$this->db->where_in('dp.IsEdit', array(1,0));
		//$this->db->where('(dp.SaldoDPBalance - dp.DPUsed) > ', 0);
		$this->db->group_by('dp.SOrdNumber');
		
        $query = $this->db->get();
        
        if ($query->num_rows() > 0) {
			return $query->row();
		}
		else {
			return FALSE;
		}	
	}
	
	function get_all(){
		/*=========================lemparan parah dari custom.js untuk searching_data_transaksi=====================*/
		$param = $this->input->post('param');
		/*====================================end===================================================================*/
		
		/*=================================post dari find di kiri====================================================*/
		$Number				= isset($param['txt_IDNumber']) ? $param['txt_IDNumber'] : '';
		$Desc				= isset($param['txt_Description']) ? $param['txt_Description'] : '';
		
		$DateFrom			= isset($param['txt_DateFrom']) ? $param['txt_DateFrom'] : '';
		$DateTo				= isset($param['txt_DateTo']) ? $param['txt_DateTo'] : '';
		$TransStatus		= isset($param['chk_TransStatus']) ? $param['chk_TransStatus'] : '';
		$BranchCode			= isset($param['cmb_branch']) ? $param['cmb_branch'] : '';
		$cmb_CustomerCode	= isset($param['cmb_CustomerCode']) ? $param['cmb_CustomerCode'] : '';
		/*============================================end============================================================*/
		
		/*======================================scrol view 1-150=======================================*/
		$page		= ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
		$rows		= ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;
		
		$beginning 	= ($page - 1) * $rows;
		$end 		= $page * $rows;
		/*==============================================================================================*/
		
		/*==========================================by session=======================================================*/
		$sess_company		= $this->session->userdata('company');
        $sess_branch		= $this->session->userdata('branch');
        $sess_branch_status	= $this->session->userdata('branch_status');
		/*===========================================================================================================*/

        $this->db->select("at_salesdp.TransStatus, at_salesdp.SDpayNumber, at_salesdp.SDpayDate, at_salesdp.SDpayDesc, at_salesdp.CustomerCode,at_salesdp.CreatedBy, 
		at_salesdp.BranchCode, at_salesdp.SDpayAmount, am_customer.CustomerName, am_branch.BranchName,am_company.CompanyName,at_salesdp.PayAmount");
		$this->db->from('at_salesdp');
        $this->db->join('am_customer', 'am_customer.CustomerCode = at_salesdp.CustomerCode', 'INNER');
        $this->db->join('am_branch', 'am_branch.BranchCode = at_salesdp.BranchCode', 'INNER');
        $this->db->join('am_company', 'am_company.CompanyCode = am_branch.CompanyCode', 'INNER');

       /*===================================find by number dan desc=========================================================*/
		if($Number != ""){$this->db->like('at_salesdp.SDpayNumber', $Number);}
		if($Desc != ""){$this->db->like('at_salesdp.SDpayDesc', $Desc);}
		if($cmb_CustomerCode != ""){$this->db->where('am_customer.CustomerCode', $cmb_CustomerCode);}
		/*=============================================end===================================================================*/
		
		/*===================================find by branch code=========================================================*/
		/*
			get data dari  combobox find di view
		*/
        if($BranchCode != ""){
			$this->db->where('at_salesdp.BranchCode', $BranchCode);
		}
		else{
			/*
				inisialisasi untuk loading pertama page branch info 
			*/
			//----------------kondisi by company-------------------------------------//
			/*
				branch status 0 = HO
				branch status 1 = depo/branch
				jangan lupa sesion data di atas untuk setiap kondisi
				jangan lupa tambahin case when trans status
			*/
			$this->db->where('at_salesdp.BranchCode', $sess_branch);
			//-----------------------end-------------------------------------------------//
		}
		/*=======================================================end========================================================*/
		
		/*=====================================================find by date==================================================*/
		if((($DateFrom != '') && ($DateTo != ''))){
            if($DateFrom <= $DateTo){
				$this->db->where('at_salesdp.SDpayDate >=', date("Y-m-d", strtotime($DateFrom)));
            	$this->db->where('at_salesdp.SDpayDate <=', date("Y-m-d", strtotime($DateTo)));
			}
        }
		/*======================================================end===========================================================*/
		
		/*=====================================================find by status==================================================*/
		if($TransStatus!=''){ 
			$this->db->where_in('at_salesdp.TransStatus',$TransStatus); 
		}
		else{
			/*
				trans status 0,1 adalah aktif
			*/
			$this->db->where_in('at_salesdp.TransStatus',array(0,1,4,5,6,7)); 
		}
		/*=======================================================end========================================================*/
		
		/*==============================================sort tabel asc==========================================================*/
		$this->db->order_by($this->table.'.TransStatus', 'ASC');
		$this->db->order_by($this->table.'.SDpayNumber', 'ASC');
		/*===============================end=================================================================================*/
		$query  = $this->db->limit($end, $beginning);
		$query = $this->db->get();
		return $query->result();
    }
	
	function get_all_to_print($data_filter){
		/*=================================post dari find di kiri====================================================*/
		$BranchCode     = $data_filter['cmb_branch'];
        $Number         = $data_filter['txt_IDNumber'];
        $Desc           = $data_filter['txt_Description'];
        $DateFrom       = $data_filter['txt_DateFrom'];
        $DateTo         = $data_filter['txt_DateTo'];
        $TransStatus    = $data_filter['chk_TransStatus'];
		/*============================================end============================================================*/
		
		/*==========================================by session=======================================================*/
		$sess_company		= $this->session->userdata('company');
        $sess_branch		= $this->session->userdata('branch');
        $sess_branch_status	= $this->session->userdata('branch_status');
		/*===========================================================================================================*/

        $this->db->select("at_salesdp.TransStatus, at_salesdp.SDpayNumber, at_salesdp.SDpayDate, at_salesdp.SDpayDesc, at_salesdp.CustomerCode,at_salesdp.CreatedBy, 
		at_salesdp.BranchCode, at_salesdp.SDpayAmount, am_customer.CustomerName, am_branch.BranchName,am_company.CompanyName,at_salesdp.PayAmount");
		$this->db->from('at_salesdp');
        $this->db->join('am_customer', 'am_customer.CustomerCode = at_salesdp.CustomerCode', 'INNER');
        $this->db->join('am_branch', 'am_branch.BranchCode = at_salesdp.BranchCode', 'INNER');
        $this->db->join('am_company', 'am_company.CompanyCode = am_branch.CompanyCode', 'INNER');

       /*===================================find by number dan desc=========================================================*/
		if($Number != ""){$this->db->like('at_salesdp.SDpayNumber', $Number);}
		if($Desc != ""){$this->db->like('at_salesdp.SDpayDesc', $Desc);}
//		if($cmb_CustomerCode != ""){$this->db->where('am_customer.CustomerCode', $cmb_CustomerCode);}
		/*=============================================end===================================================================*/
		
		/*===================================find by branch code=========================================================*/
		/*
			get data dari  combobox find di view
		*/
        if($BranchCode != ""){
			$this->db->where('at_salesdp.BranchCode', $BranchCode);
		}
		else{
			/*
				inisialisasi untuk loading pertama page branch info 
			*/
			//----------------kondisi by company-------------------------------------//
			/*
				branch status 0 = HO
				branch status 1 = depo/branch
				jangan lupa sesion data di atas untuk setiap kondisi
				jangan lupa tambahin case when trans status
			*/
			$this->db->where('at_salesdp.BranchCode', $sess_branch);
			//-----------------------end-------------------------------------------------//
		}
		/*=======================================================end========================================================*/
		
		/*=====================================================find by date==================================================*/
		if((($DateFrom != '') && ($DateTo != ''))){
            if($DateFrom <= $DateTo){
				$this->db->where('at_salesdp.SDpayDate >=', date("Y-m-d", strtotime($DateFrom)));
            	$this->db->where('at_salesdp.SDpayDate <=', date("Y-m-d", strtotime($DateTo)));
			}
        }
		/*======================================================end===========================================================*/
		
		/*=====================================================find by status==================================================*/
		if($TransStatus!=''){ 
			$this->db->where_in('at_salesdp.TransStatus',$TransStatus); 
		}
		else{
			/*
				trans status 0,1 adalah aktif
			*/
			$this->db->where_in('at_salesdp.TransStatus',array(0,1,4,5,6,7)); 
		}
		/*=======================================================end========================================================*/
		
		/*==============================================sort tabel asc==========================================================*/
		$this->db->order_by($this->table.'.TransStatus', 'ASC');
		$this->db->order_by($this->table.'.SDpayNumber', 'ASC');
		/*===============================end=================================================================================*/
		$query = $this->db->get();
		return $query->result();
    }
	
	function get_akun($Currency) {
        /* ======================================scrol view 1-150======================================= */
        $page = ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
        $rows = ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;

        $beginning = ($page - 1) * $rows;
        $end = $page * $rows;
        /* ============================================================================================== */

        /* =================================post dari find di atas pop up==================================================== */
        $param = $this->input->post('param');
        $AccountNumber = isset($param['txt_AccountNumber']) ? $param['txt_AccountNumber'] : '';
        $AccountName = isset($param['txt_AccountName']) ? $param['txt_AccountName'] : '';
        /* ============================================end============================================================ */

        /* =========================by session==================================================================== */
        $sess_branch = $this->session->userdata('branch');
        $sess_company = $this->session->userdata('company');
        /* =============================end====================================================================== */

        $this->db->select('at_compaccountgl.AccountNumber, am_accountgl.AccountName,am_accountgl.AccountLevel, am_accountgl.AccountSubTypeID, am_accountgl.CurrencyCode, am_currency.CurrencyRate, am_accountsubtype.AccountTypeName as SubtypeName, am_accounttype.AccountTypeName as TypeName');
        $this->db->from($this->account);
        $this->db->join('am_accountgl', 'at_compaccountgl.AccountNumber=am_accountgl.AccountNumber');
        $this->db->join('am_accountsubtype', 'am_accountgl.AccountSubTypeID=am_accountsubtype.AccountSubTypeID');
        $this->db->join('am_currency', 'am_currency.CurrencyCode = am_accountgl.CurrencyCode');
        $this->db->join('am_accounttype', 'am_accounttype.AccountTypeID = am_accountsubtype.AccountTypeID');

        $this->db->where('at_compaccountgl.BranchCode', $sess_branch);
        $this->db->where('am_currency.CompanyCode', $sess_company);
        $this->db->where('am_accountgl.AccountChild', 1);
        $this->db->where('at_compaccountgl.TransStatus', 2);
		if($Currency == 'IDR'){

        }else{
            $this->db->where_in('am_accountgl.CurrencyCode', array($Currency,'IDR'));
        }
        
        //$this->db->where('am_accountgl.CurrencyCode', 'IDR');
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));

        /* ===============================================pencarian pas pop up jika kondisi tidak sama dengan kosong ================== */
        if ($AccountNumber != '') {
            $this->db->like('am_accountgl.AccountNumber', $AccountNumber);
        }
        if ($AccountName != '') {
            $this->db->like('am_accountgl.AccountName', $AccountName);
        }
        /* ====================================end====================================================================================== */
		
        $this->db->order_by('at_compaccountgl.AccountNumber', 'asc');
		
        $query =  $this->db->get()->result();
		/*$return = array();
		foreach($query as $row){
            if(in_array($row->TypeName,array('Revenue','Expense','COGS / COGM (Cost Of Goods Made)','Other Revenue','Other Expense'))){
                $return[] = $row;
            }else{
                if($row->CurrencyCode == $Currency){
                    $return[] = $row;
                }
            }
        }*/

        return $query;
    }
	
	function check_code_exist($sdp_number) {
        $query = $this->db->get_where($this->table, array('SDpayNumber' => $sdp_number), 1, 0);
		
        if ($query->num_rows() > 0) {
            return TRUE;
        }
        else {
            return FALSE;
        }	
    }
    
    function create_sdp_number($date = ''){
        $branch = $this->session->userdata('branch');
		
        if($date != ''){
            $month  = date('m', strtotime($date));
            $year   = date('y', strtotime($date));
        }
        else{
            $month  = date('m');
            $year   = date('y');
        }
		
        $sdp_number	= $branch ."/". $month . $year ."DP/";
		
        $this->db->_protect_identifiers = false;
        $this->db->select('(CASE 
                              WHEN MAX(CONVERT(SUBSTR(SOrdNumber, -3),signed)) IS NULL then 1
                              ELSE MAX(CONVERT(SUBSTR(SOrdNumber, -3),signed)) + 1 END) as New_Number');
        $this->db->like('SDpayNumber', $sdp_number);
        $query = $this->db->get($this->table);
        $this->db->_protect_identifiers = true;
                
        $num    = $query->row()->New_Number;
        $long   = strlen($num);
        
        if($long == 1){
            $number = '00' . $num;
        }
        elseif($long == 2){
            $number = '0' . $num;
        }
        else{
            $number = $num;
        }

        $new_dp_number = $sdp_number . $number;

        while($this->check_code_exist($new_dp_number)){
            $num++;
            $long = strlen($num);
    
            if($long == 1){
                $number = '00' . $num;
            }
            elseif($long == 2){
                $number = '0' . $num;
            }
            else{
                $number = $num;
            }
            
            $new_dp_number = $sdp_number . $number;		
        }
        
        return  $new_dp_number;
    }
	
	function insert_transaction($trans_data){
		$this->db->trans_start();
		
		$this->insert($trans_data['insert_group']['data']);
		
		for($x=0;$x<count($trans_data['insert_detail']['data']);$x++){
			$this->insert_detail($trans_data['insert_detail']['data'][$x]);
		}
		
		for($x=0;$x<count($trans_data['insert_journal']['data']);$x++){
			$this->insert_journal($trans_data['insert_journal']['data'][$x]);
		}
		
		for($x=0;$x<count($trans_data['update_dp']['data']);$x++){
			$this->m_sales_receipt->update_so_detail_dp_by_customer($trans_data['update_dp']['data'][$x], $trans_data['update_dp']['CustomerCode'][$x], $trans_data['update_dp']['SOrdDetailDPID'][$x], $trans_data['update_dp']['Select_DP'][$x]);
		}
		
		$this->m_customer->update($trans_data['update_customer']['data'], $trans_data['update_customer']['CustomerCode']);
		$this->db->trans_complete();
        if ($this->db->trans_status() === FALSE) {
            return 0;
        } else {
            return 1;
        }
	}
	
	function update_transaction($trans_data){
		$this->db->trans_start();
		
		$this->update($trans_data['update_group']['data'], $trans_data['update_group']['TransNumber']);
		
		for($x=0;$x<count($trans_data['update_detail']['data']);$x++){
			$this->update_detail($trans_data['update_detail']['data'][$x], $trans_data['update_detail']['TransNumber'][$x]);
		}
		
		$this->delete_journal($trans_data['delete_journal']['TransNumber']);
		
		for($x=0;$x<count($trans_data['insert_journal']['data']);$x++){
			$this->insert_journal($trans_data['insert_journal']['data'][$x]);
		}
		
		for($x=0;$x<count($trans_data['update_dp']['data']);$x++){
			$this->m_sales_receipt->update_so_detail_dp_by_customer($trans_data['update_dp']['data'][$x], $trans_data['update_dp']['CustomerCode'][$x], $trans_data['update_dp']['SOrdDetailDPID'][$x], $trans_data['update_dp']['Select_DP'][$x]);
		}
		
		for($x=0;$x<count($trans_data['update_dpused']['data']);$x++){
			$this->m_sales_receipt->update_so_detail_dp_for_sr($trans_data['update_dpused']['data'][$x], $trans_data['update_dpused']['CustomerCode'][$x], $trans_data['update_dpused']['SOrdDetailDPID'][$x]);
		}
		
		$this->db->trans_complete();
        if ($this->db->trans_status() === FALSE) {
            return 0;
        } else {
            return 1;
        }
	}
	
	function suspend_transaction($trans_data){
		$this->db->trans_start();
		for($x=0;$x<count($trans_data['update_dp']['data']);$x++){
			$this->m_sales_receipt->update_sr_detail_dp_rec_before($trans_data['update_dp']['data'][$x], $trans_data['update_dp']['CustomerCode'][$x], $trans_data['update_dp']['SOrdNumber'][$x], $trans_data['update_dp']['POrdDetailDPID'][$x]);
		}
		
		$this->update($trans_data['update_group']['data'], $trans_data['update_group']['TransNumber']);
		
		$this->m_customer->update($trans_data['update_customer']['data'], $trans_data['update_customer']['CustomerCode']);
		
		$this->db->trans_complete();
        if ($this->db->trans_status() === FALSE) {
            return 0;
        } else {
            return 1;
        }
	}
	
	function approval_transaction($trans_data){
		$this->db->trans_start();
		$this->update($trans_data['update_group']['data'], $trans_data['update_group']['TransNumber']);
		
		$this->m_customer->update($trans_data['update_customer']['data'], $trans_data['update_customer']['CustomerCode']);
		
		$this->db->trans_complete();
        if ($this->db->trans_status() === FALSE) {
            return 0;
        } else {
            return 1;
        }
	}
	
	function reverse_transaction($trans_data){
		$this->db->trans_start();
		$this->update($trans_data['update_group']['data'], $trans_data['update_group']['TransNumber']);
		
		$this->m_customer->update($trans_data['update_customer']['data'], $trans_data['update_customer']['CustomerCode']);
		
		$this->db->trans_complete();
        if ($this->db->trans_status() === FALSE) {
            return 0;
        } else {
            return 1;
        }
	}
	
	function insert($data) {
        $this->db->insert($this->table, $data);
    }
	
	function insert_detail($data_detail) {
        $this->db->insert($this->detail, $data_detail);
    }
	
	function insert_journal($data_journal) {
        $this->db->insert($this->journal, $data_journal);
    }
	
	function update($data, $TRansNumber) {
		$this->db->where('SDpayNumber', $TRansNumber);
        $this->db->update($this->table, $data);
    }
	
	function update_detail($data, $TRansNumber_detail) {
		$this->db->where('SDpayDetailID', $TRansNumber_detail);
        $this->db->update($this->detail, $data);
    }
	
	function delete_journal($id){
        $this->db->delete($this->journal, array('SDpayNumber' => $id));
    }
	
	function delete_data_grid($SDpayDetailID){
        $this->db->delete($this->detail, array('SDpayDetailID' => $SDpayDetailID));
    }

	function get_detail($id){
		$this->db->select('BranchCode,SDpayDate,SDpayNumber, SOrdNumber, SDpayAmount, CustomerCode
							,CurrencyCode, CurrencyRate, SDpayDesc, AppLevel1Code, AppLevel1Status, AppLevel1Date,
							TransStatus,CreatedBy,CreatedAt');
        $this->db->from($this->table);
        $this->db->where('SDpayNumber', $id);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
	}
	
	function get_detail_item($SDpayNumber){
	    $this->db->_protect_identifiers = FALSE;
	    $this->db->select('at_sdpdetail.SDpayAmount as Amount, at_sdpdetail.SDpayMemo as Memo, at_sdpdetail.SDpayNumber,at_sdpdetail.CurrencyRate as Rate, am_accountsubtype.AccountTypeName as SubtypeName, am_accounttype.AccountTypeName as TypeName, 
							at_sdpdetail.SDpayAccountNumber as AccountNumber, am_accountgl.AccountName, at_sdpdetail.SDpayDetailID, am_accountgl.CurrencyCode');
		$this->db->join('am_accountgl', 'am_accountgl.AccountNumber = at_sdpdetail.SDpayAccountNumber');
		$this->db->join('am_accountsubtype', 'am_accountsubtype.AccountSubTypeID = am_accountgl.AccountSubTypeID');
		$this->db->join('am_accounttype', 'am_accounttype.AccountTypeID = am_accountsubtype.AccountTypeID');
		
		$this->db->where('at_sdpdetail.SDpayNumber', $SDpayNumber);
		
		$query = $this->db->get('at_sdpdetail');
		$this->db->_protect_identifiers = TRUE;
        return $query->result();
    }
	
	function get_payment_detail_dp_suspend($id, $SOrdNumber, $akun_number) {
		$this->db->_protect_identifiers = FALSE;
		$this->db->select("at_sorddetaildp.SOrdStatus, at_sorddetaildp.JVoucherBalance, at_sorddetaildp.SOrdTotalDP, at_sorddetaildp.Owing, 
							at_sorddetaildp.PaymentAmount, at_sorddetaildp.CustomerCode, at_sorddetaildp.SaldoDPBalance, at_sorddetaildp.SOrdNumber, 
							at_sorddetaildp.CurrencyRate, at_sorddetaildp.DPUsedBook, at_sorddetaildp.IsEdit, at_sorddetaildp.SOrdDetailDPID, 
							at_sorddetaildp.DPUsed, at_sdpjournal.AmountCurrency, at_sdpjournal.SDpayJDebet, at_sdpjournal.SDpayJCredit");
		$this->db->join('at_salesdp', 'at_salesdp.SDpayNumber = at_sdpjournal.SDpayNumber', 'INNER');
		$this->db->join('at_sorddetaildp', 'at_sorddetaildp.SOrdDetailDPID = at_sdpjournal.SOrdDetailDPID', 'INNER');
		
        $this->db->from('at_sdpjournal');
        $this->db->where('at_salesdp.CustomerCode', $id); 
		
		$this->db->where('at_sorddetaildp.SOrdNumber', $SOrdNumber); 
		$this->db->where('at_sdpjournal.SDpayAccountNumber', $akun_number); 
		//$this->db->where_in('at_salesdp.TransStatus', array(4));
		
        $query = $this->db->get();    
		$this->db->_protect_identifiers = TRUE;
		
        return $query->result();
    }
	
	function get_receipt_detail_dp_update($id, $akun_number) {
		$this->db->_protect_identifiers = FALSE;
		$this->db->select("at_sorddetaildp.SOrdStatus, at_sorddetaildp.JVoucherBalance, at_sorddetaildp.SOrdTotalDP, at_sorddetaildp.Owing, 
							at_sorddetaildp.PaymentAmount, at_sorddetaildp.CustomerCode, at_sorddetaildp.SaldoDPBalance, at_sorddetaildp.SOrdNumber, 
							at_sorddetaildp.CurrencyRate, at_sorddetaildp.DPUsedBook, at_sorddetaildp.IsEdit, at_sorddetaildp.SOrdDetailDPID, 
							at_sorddetaildp.DPUsed, at_sdpjournal.AmountCurrency, at_sdpjournal.SDpayJDebet, at_sdpjournal.SDpayJCredit");
		$this->db->join('at_salesdp', 'at_salesdp.SDpayNumber = at_sdpjournal.SDpayNumber', 'INNER');
		$this->db->join('at_sorddetaildp', 'at_sorddetaildp.SOrdDetailDPID = at_sdpjournal.SOrdDetailDPID', 'INNER');
		
        $this->db->from('at_sdpjournal');
        $this->db->where('at_salesdp.CustomerCode', $id); 
		
		$this->db->where('at_sdpjournal.SDpayAccountNumber', $akun_number); 
		$this->db->where_in('at_salesdp.TransStatus', array(5));
		
        $query = $this->db->get();    
		$this->db->_protect_identifiers = TRUE;
		
        return $query->result();
    }
	
	function get_detail_journal($id){
        $this->db->select($this->table.".*,".$this->branch.".BranchName, ".$this->customer.".CustomerName,cp.CompanyName,
							".$this->customer.".CustomerAddress, ".$this->customer.".CurrencyCode as CurrCustomer, ".$this->customer.".SaldoDPPartial,
                          ".$this->customer.".SaldoDPBalance");
        $this->db->join($this->branch,$this->branch.".BranchCode=".$this->table.".BranchCode","INNER");
        $this->db->join($this->customer,$this->customer.".CustomerCode=".$this->table.".CustomerCode","INNER");
		$this->db->join('am_company as cp', 'cp.CompanyCode = am_branch.CompanyCode', 'INNER');
		
        $query = $this->db->get_where($this->table, array('SDpayNumber' => $id));
	
        if($query->num_rows()>0){
            return $query->row();
        }
        else{
            return false;
        }      
    }
	
	function get_journal_by_number($Number){
        $this->db->_protect_identifiers = FALSE;
        $this->db->select("at_sdpjournal.*,am_accountgl.AccountNumber,am_accountgl.AccountName");
        $this->db->join("am_accountgl","am_accountgl.AccountNumber=at_sdpjournal.SDpayAccountNumber","INNER");
        //$this->db->order_by('at_srecjournal.SRecAccountNumber','ASC');
		
        $query = $this->db->get_where($this->journal, array('SDpayNumber' => $Number));
        $this->db->_protect_identifiers = TRUE;
        
        return $query->result();
    }
	
	function get_customer_code($CustomerCode){
        $this->db->select("LockStatus");
        $query = $this->db->get_where($this->customer, array('CustomerCode' => $CustomerCode));
	
        if($query->num_rows()>0){
            return $query->row();
        }
        else{
            return false;
        }      
    }
	
	function get_detail_item_print($payment_no) {
        $this->db->select('gr.*,,br.BranchName,br.BranchAddress,pr.EmployeeName as nm_prepared,dr.EmployeeName as nm_manager, cs.CustomerName, cs.CustomerAddress');
        $this->db->from($this->table . ' as gr');
        $this->db->join($this->branch . ' as br', 'br.BranchCode = gr.BranchCode', 'INNER');
        $this->db->join($this->employee . ' as pr', 'pr.EmployeeCode=gr.CreatedBy', 'LEFT');
        $this->db->join($this->employee . ' as dr', 'dr.EmployeeCode=gr.AppLevel1Code', 'LEFT');
		$this->db->join($this->customer . ' as cs', 'gr.CustomerCode = cs.CustomerCode', 'INNER');
        $this->db->where('SDpayNumber', $payment_no);
        $query = $this->db->get();
        return $query->row();
    }

    function get_detail_item_print_journal($payment_no) {
        $this->db->select('ag.AccountName as Account_Name,dt.SDpayMemo as Memo,dt.SDpayAmount as Amount');
        $this->db->from($this->detail . ' as dt');
        $this->db->join($this->m_account . ' as ag', 'ag.AccountNumber=dt.SDpayAccountNumber', 'inner');
        $this->db->where('dt.SDpayNumber', $payment_no);
        $this->db->order_by('dt.SDpayDetailID', 'ASC');

        $query = $this->db->get()->result_array();

        return $query;
    }
	
	function get_rate_from_m_currency($company_code) {

        $this->db->select('CurrencyRate');
        $this->db->from("am_currency");
        $this->db->where('CompanyCode', $company_code);
        $query = $this->db->get();

        return $query->row();
    }
}

/* End of file m_sales_order.php */
/* Location: ./application/models/m_sales_order.php */