<?php

class m_payment_listing extends CI_Model {

    function get_all($BranchCode) {
        $param = $this->input->post('param');

        $jobtitle = $this->session->userdata("jobtitle");
        $BranchStatus = $this->session->userdata('branch_status');
        $CompanyCode = $this->session->userdata('company');

        $EPayDateFrom = isset($param['txt_DateFrom']) ? $param['txt_DateFrom'] : '';
        $EPayDateTo = isset($param['txt_DateTo']) ? $param['txt_DateTo'] : '';
        $TransStatus = isset($param['chk_TransStatus']) ? $param['chk_TransStatus'] : '';

        $this->db->select('am_branch.BranchName, am_department.DepartmentName, am_employee.EmployeeName, at_expensecashbon.EBonDate,
						   at_ebondetail.EBonAmount, at_expensepayment.EPayDate, sum(at_epaydetail.ExpenseAmount) as ExpenseAmount , at_epaymentreal.EPRealAmount, 
						   at_epaymentreal.EPRealTransType,at_expensepayment.EPayNumber');
        $this->db->from('at_expensepayment');
        $this->db->join('at_epaydetail', 'at_expensepayment.EPayNumber=at_epaydetail.EPayNumber', 'INNER');
        $this->db->join('at_epaymentreal', 'at_expensepayment.EPayNumber=at_epaymentreal.EPayNumber', 'INNER');
        $this->db->join('at_expensecashbon', 'at_expensepayment.EBonNumber=at_expensecashbon.EBonNumber', 'INNER');
        $this->db->join('at_ebondetail', 'at_expensecashbon.EBonNumber=at_ebondetail.EBonNumber', 'INNER');
        $this->db->join('am_branch', 'at_expensecashbon.BranchCode=am_branch.BranchCode', 'INNER');
        $this->db->join('am_department', 'at_expensecashbon.DepartmentCode=am_department.DepartmentCode', 'INNER');
        $this->db->join('am_employee', 'at_expensecashbon.EmployeeCode=am_employee.EmployeeCode', 'INNER');
        $this->db->where('at_expensepayment.TransStatus', 2);

        if ($BranchStatus == 1) {
            $this->db->where('at_expensepayment.BranchCode', $BranchCode);
            $this->db->where('am_branch.CompanyCode', $CompanyCode);
        } else {
            $this->db->where('am_branch.CompanyCode', $CompanyCode);
        }

        if ($EPayDateFrom != '' && $EPayDateTo != '' && $EPayDateFrom < $EPayDateTo) {
            $this->db->where('at_expensepayment.EPayDate >=', date("Y-m-d", strtotime($EPayDateFrom)));
            $this->db->where('at_expensepayment.EPayDate <=', date("Y-m-d", strtotime($EPayDateTo)));
        }

        if ($TransStatus != '') {
            $this->db->where_in('at_epaymentreal.EPRealTransType', $TransStatus);
        }

        $this->db->group_by('at_expensepayment.EPayNumber');
        $this->db->order_by('at_expensepayment.EPayDate', 'ASC');
        $query = $this->db->get();
        return $query->result();
    }
        
    function get_all_to_print($data_filter) {
        /* ===========================declar post by find================================================ */
        $EPayDateFrom   = $data_filter['txt_DateFrom'];
        $EPayDateTo     = $data_filter['txt_DateTo'];
        $TransStatus    = $data_filter['chk_TransStatus'];
//        echo "<hr>EPayDateFrom = $EPayDateFrom <hr>";
//        echo "model data_filter = ".  json_encode($data_filter)."<hr>";
        /* ========================================end================================================== */
        
        $jobtitle = $this->session->userdata("jobtitle");
        $BranchStatus = $this->session->userdata('branch_status');
        $CompanyCode = $this->session->userdata('company');
        $BranchCode = $this->session->userdata('branch');


        $this->db->select('am_branch.BranchName, am_department.DepartmentName, am_employee.EmployeeName, at_expensecashbon.EBonDate,
            at_ebondetail.EBonAmount, at_expensepayment.EPayDate, sum(at_epaydetail.ExpenseAmount) as ExpenseAmount , at_epaymentreal.EPRealAmount, 
            at_epaymentreal.EPRealTransType,at_expensepayment.EPayNumber');
        $this->db->from('at_expensepayment');
        $this->db->join('at_epaydetail', 'at_expensepayment.EPayNumber=at_epaydetail.EPayNumber', 'INNER');
        $this->db->join('at_epaymentreal', 'at_expensepayment.EPayNumber=at_epaymentreal.EPayNumber', 'INNER');
        $this->db->join('at_expensecashbon', 'at_expensepayment.EBonNumber=at_expensecashbon.EBonNumber', 'INNER');
        $this->db->join('at_ebondetail', 'at_expensecashbon.EBonNumber=at_ebondetail.EBonNumber', 'INNER');
        $this->db->join('am_branch', 'at_expensecashbon.BranchCode=am_branch.BranchCode', 'INNER');
        $this->db->join('am_department', 'at_expensecashbon.DepartmentCode=am_department.DepartmentCode', 'INNER');
        $this->db->join('am_employee', 'at_expensecashbon.EmployeeCode=am_employee.EmployeeCode', 'INNER');
        $this->db->where('at_expensepayment.TransStatus', 2);

        if ($BranchStatus == 1) {
            $this->db->where('at_expensepayment.BranchCode', $BranchCode);
            $this->db->where('am_branch.CompanyCode', $CompanyCode);
        } else {
            $this->db->where('am_branch.CompanyCode', $CompanyCode);
        }

        if ($EPayDateFrom != '' && $EPayDateTo != '') {
            if($EPayDateFrom < $EPayDateTo){
                $this->db->where('at_expensepayment.EPayDate >=', date("Y-m-d", strtotime($EPayDateFrom)));
                $this->db->where('at_expensepayment.EPayDate <=', date("Y-m-d", strtotime($EPayDateTo)));
            }else{
                $this->db->where('at_expensepayment.EPayDate <=', date("Y-m-d", strtotime($EPayDateTo)));
                $this->db->where('at_expensepayment.EPayDate >=', date("Y-m-d", strtotime($EPayDateFrom)));
            }
        }

        if (!empty($TransStatus)) {
            $this->db->where_in('at_epaymentreal.EPRealTransType', $TransStatus);
        }

        $this->db->group_by('at_expensepayment.EPayNumber');
        $this->db->order_by('at_expensepayment.EPayDate', 'ASC');
        $query = $this->db->get();
//        echo "<hr>".$this->db->last_query()."<hr>";
        return $query->result();
    }
}
