<?php

class M_piutang_karyawan extends CI_Model {

    public $table = "at_employeedebt";
    public $company = "am_company";
    public $account = "at_compaccountgl";
    public $journal = "at_edebtjournal";
    public $reconcile = "at_bankreconcille";
    public $period = "at_periodend";
    public $employee = "am_employee";
    public $department = "am_department";
    public $payment = "at_employeeinstallment";
    public $payment_journal = "at_employeeinstallment_journal";

    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'] : '';
        /* ============================================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_employeedebt.EDebtNumber, at_employeedebt.EDebtDate, at_employeedebt.CompTransCode, 
			at_employeedebt.EmplRecpCode, at_employeedebt.EDebtAmount, at_employeedebt.EDebtDesc, at_employeedebt.TransStatus, 
			am_employee.EmployeeName, created.EmployeeName as created_name');
        $this->db->from($this->table);
        $this->db->join('am_branch', 'at_employeedebt.BranchCode=am_branch.BranchCode', 'INNER');
        $this->db->join('am_employee', 'at_employeedebt.EmplRecpCode=am_employee.EmployeeCode', 'INNER');
        $this->db->join('am_employee as created', 'at_employeedebt.CreatedBy=created.EmployeeCode', 'LEFT');

        /* ===================================find by number dan desc========================================================= */
        if ($Number != "") {
            $this->db->like('at_employeedebt.EDebtNumber', $Number);
        }
        if ($Desc != "") {
            $this->db->like('at_employeedebt.EDebtDesc', $Desc);
        }
        /* =============================================end=================================================================== */

        /* ===================================find by branch code========================================================= */
        /*
          get data dari  combobox find di view
         */
        if ($BranchCode != "") {
            $this->db->where('at_employeedebt.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_employeedebt.BranchCode', $sess_branch);
            //-----------------------end-------------------------------------------------//
        }
        /* =======================================================end======================================================== */

        /* =====================================================find by date================================================== */
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_employeedebt.EDebtDate >=', date("Y-m-d", strtotime($DateFrom)));
                $this->db->where('at_employeedebt.EDebtDate <=', date("Y-m-d", strtotime($DateTo)));
            }
        }
        /* ======================================================end=========================================================== */

        /* =====================================================find by status================================================== */
        if ($TransStatus != '') {
            $this->db->where_in('at_employeedebt.TransStatus', $TransStatus);
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in('at_employeedebt.TransStatus', array(0, 1, 4, 5, 6, 7, 8));
        }
        /* =======================================================end======================================================== */

        /* ==============================================sort tabel asc========================================================== */
        $this->db->order_by($this->table . '.TransStatus', 'ASC');
        $this->db->order_by($this->table . '.EDebtNumber', 'ASC');
        /* ===============================end================================================================================= */
        $query = $this->db->limit($end, $beginning);
        $query = $this->db->get();
        return $query->result();
    }

    function count_piutang_number($bulan, $cek_tahun, $BranchCode) {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('MONTH(EDebtDate)', $bulan);
        $this->db->where('YEAR(EDebtDate)', $cek_tahun);
        $this->db->where('BranchCode', $BranchCode);
        return $this->db->get()->num_rows();
    }

    function suspend_data($data, $id) {
        $this->db->update($this->table, $data, array('EDebtNumber' => $id));
    }

    function get_last_piutang_number($BranchCode) {
        $qty = "SELECT MAX(EDebtNumber) as EDebtNumber FROM at_employeedebt WHERE BranchCode='$BranchCode'";
        return $this->db->query($qty)->result();
    }

    //custom data
    function get_company_edit($branch_explode) {
        //$jobtitle = $this->session->userdata("jobtitle");
        $this->db->select('*');
        $this->db->from('am_branch');

        //$this->db->where('CompanyCode', $CompanyCode);
        $this->db->where('BranchCode', $branch_explode);

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

        $result = $query->result();
        return json_encode($result);
    }

    function get_company($CompanyCode, $branch) {
        $this->db->select('*');
        $this->db->from('am_branch');

        $this->db->where('CompanyCode', $CompanyCode);
        $this->db->where('BranchCode', $branch);

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

        $result = $query->result();
        return json_encode($result);
    }

    function get_akun_bank_edit($branch_explode) {
        $company = $this->session->userdata('company');
        $BranchStatus = $this->session->userdata('branch_status');

        $this->db->select('*');
        $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->where('at_compaccountgl.BranchCode', $branch_explode);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));

        $this->db->where('am_accountgl.AccountSubTypeID', 7);
        $this->db->where('am_accountgl.AccountChild', 1);
        $this->db->where('am_accountgl.CurrencyCode', 'IDR');
        $this->db->where('am_accountgl.TransStatus', 2);

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

        $result = $query->result();
        return json_encode($result);
    }

    function get_karyawan_bycompany_edit($branch_explode) {

        $this->db->select('am_employee.EmployeeCode, am_employee.EmployeeName');
        $this->db->from($this->employee);
        $this->db->join('am_department', 'am_employee.DepartmentCode=am_department.DepartmentCode');
        $this->db->join('am_branch', 'am_employee.BranchCode=am_branch.BranchCode');

        //if($BranchStatus == 1){
        $this->db->where('am_employee.BranchCode', $branch_explode);
        //}
        $this->db->where('am_employee.ResignDate', NULL);
        $query = $this->db->get();

        $result = $query->result();
        return json_encode($result);
    }

    function get_akun_bank($BranchCode) {
        $jobtitle = $this->session->userdata("jobtitle");
        $BranchCode = $this->session->userdata('branch');
        $company = $this->session->userdata('company');
        $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.BranchCode', $BranchCode);
        //$this->db->where('am_branch.BranchStatus', 0);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));

        $this->db->where('am_accountsubtype.AccountTypeName', 'Bank');
        $this->db->where('am_accountgl.CurrencyCode', 'IDR');
        $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_coa_piutang_karyawan($com_trnas, $branch_code) {
        $this->db->select('at_compaccountgl.AccountNumber');
        $this->db->from('at_compaccountgl');
        $this->db->join('am_accountgl', 'at_compaccountgl.AccountNumber=am_accountgl.AccountNumber', 'inner');

        $this->db->where('at_compaccountgl.CompanyCode', $com_trnas);
        $this->db->where('at_compaccountgl.BranchCode', $branch_code);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        $this->db->where('at_compaccountgl.TransStatus', 2);
        $this->db->like('am_accountgl.AccountName', 'Piutang Karyawan');

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

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AccountNumber = $row->AccountNumber;
            }

            return $AccountNumber;
        } else {
            return FALSE;
        }
    }

    function get_coa_piutang_karyawan_edit($com_trnas) {
        $this->db->select('at_compaccountgl.AccountNumber');
        $this->db->from('at_compaccountgl');
        $this->db->join('am_accountgl', 'at_compaccountgl.AccountNumber=am_accountgl.AccountNumber', 'inner');

        //$this->db->where('at_compaccountgl.CompanyCode', $com_trnas);
        $this->db->where('at_compaccountgl.BranchCode', $com_trnas);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        $this->db->where('at_compaccountgl.TransStatus', 2);
        $this->db->like('am_accountgl.AccountName', 'Piutang Karyawan');

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

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AccountNumber = $row->AccountNumber;
            }

            return $AccountNumber;
        } else {
            return FALSE;
        }
    }

    function get_karyawan_bycompany($branch, $company_perusahaan) {
        $jobtitle = $this->session->userdata("jobtitle");
        $BranchStatus = $this->session->userdata('branch_status');

        $this->db->select('am_employee.EmployeeCode, am_employee.EmployeeName');
        $this->db->from($this->employee);
        $this->db->join('am_department', 'am_employee.DepartmentCode=am_department.DepartmentCode');
        $this->db->join('am_branch', 'am_employee.BranchCode=am_branch.BranchCode');
        $this->db->where('am_branch.CompanyCode', $company_perusahaan);
        $this->db->where('am_department.CompanyCode', $company_perusahaan);
        $this->db->where('am_employee.ResignDate', NULL);

        //if($BranchStatus == 1){
        $this->db->where('am_employee.BranchCode', $branch);
        //}

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

        $result = $query->result();
        return json_encode($result);
    }

    // end custom

    function insert_group($data_piutang) {
        $this->db->insert($this->table, $data_piutang);
    }

    function get_expt_piutang($CompanyCode) {
        $this->db->select('ExpCategoryID');
        $this->db->from('sys_expensecategory');
        $this->db->where('ExpCategoryName', 'Piutang Karyawan');
        $this->db->where('CompanyCode', $CompanyCode);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $ExpCategoryID = $row->ExpCategoryID;
            }

            return $ExpCategoryID;
        } else {
            return FALSE;
        }
    }

    function get_account_piutang($CompanyCode, $ExpCategoryID) {
        $this->db->select('AccountNumber');
        $this->db->from('sys_expenseaccount');
        $this->db->join('sys_expensecategory', 'sys_expenseaccount.ExpCategoryID=sys_expensecategory.ExpCategoryID', 'INNER');
        $this->db->where('sys_expenseaccount.ExpCategoryID', $ExpCategoryID);
        $this->db->where('sys_expenseaccount.AccountCode', 'Expense');
        $this->db->where('sys_expensecategory.CompanyCode', $CompanyCode);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $AccountNumber = $row->AccountNumber;
            }

            return $AccountNumber;
        } else {
            return FALSE;
        }
    }

    function insert_journal($journal_data) {
        $this->db->insert($this->journal, $journal_data);
    }

    /* ======================penomeran transaksi new======================================================= */

    function check_code_exist($new_jc_number) {
        $query = $this->db->get_where($this->table, array('EDebtNumber' => $new_jc_number), 1, 0);

        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function create_piutang_karyawan_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');
        }

        $jc_number = $branch . "/" . $month . $year . "AE/";

        $this->db->_protect_identifiers = false;
        $this->db->select('(CASE 
                              WHEN MAX(CONVERT(SUBSTR(EDebtNumber, -3),signed)) IS NULL then 1
                              ELSE MAX(CONVERT(SUBSTR(EDebtNumber, -3),signed)) + 1 END) as New_Number');
        $this->db->like('EDebtNumber', $jc_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_jc_number = $jc_number . $number;

        while ($this->check_code_exist($new_jc_number)) {
            $num++;
            $long = strlen($num);

            if ($long == 1) {
                $number = '00' . $num;
            } elseif ($long == 2) {
                $number = '0' . $num;
            } else {
                $number = $num;
            }

            $new_jc_number = $jc_number . $number;
        }

        return $new_jc_number;
    }

    /* ==============================================================end================================= */

    function insert_journal_credit_piutang($journal_credit) {

        $this->db->insert('at_edebtjournal', $journal_credit);
    }

    function update_group($data_piutang, $EDebtNumber) {
        $this->db->where('EDebtNumber', $EDebtNumber);
        $this->db->update($this->table, $data_piutang);
    }

    function update_journal_debit_piutang($journal_debet, $EDebtNumber) {
        $this->db->where('EDebtNumber', $EDebtNumber);
        $this->db->where('EDebtSaldoCredit', 0);
        $this->db->update('at_edebtjournal', $journal_debet);
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return 0;
        } else {
            return 1;
        }
    }

    function update_journal_credit_piutang($journal_credit, $EDebtNumber) {
        $this->db->where('EDebtNumber', $EDebtNumber);
        $this->db->where('EDebtSaldoDebet', 0);
        $this->db->update('at_edebtjournal', $journal_credit);
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return 0;
        } else {
            return 1;
        }
    }

    function get_detail($id) {
        $this->db->select('gr.*,em.DepartmentCode');
        $this->db->from($this->table . ' as gr');
        $this->db->join($this->employee . ' as em', 'em.EmployeeCode=gr.CreatedBy', 'inner');
        $this->db->where('gr.EDebtNumber', $id);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }

    function get_date_journal_piutang($piutang_number) {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('EDebtNumber', $piutang_number);
        $query = $this->db->get();

        $result = $query->result();
        return json_encode($result);
    }

    function get_all_journal_piutang($piutang_number, $branch_explode) {
        $this->db->select('am_accountgl.AccountNumber, am_accountgl.AccountName, at_edebtjournal.EDebtSaldoCredit, 
						   at_edebtjournal.EDebtSaldoDebet, at_edebtjournal.EDebtDesc, at_edebtjournal.EDebtNumber, at_edebtjournal.EDebtAccountNumber');
        $this->db->from('at_compaccountgl');
        $this->db->join('at_edebtjournal', 'at_compaccountgl.AccountNumber=at_edebtjournal.EDebtAccountNumber');
        $this->db->join('am_accountgl', 'at_compaccountgl.AccountNumber=am_accountgl.AccountNumber');
        $this->db->where('at_compaccountgl.BranchCode', $branch_explode);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        $this->db->where('at_edebtjournal.EDebtNumber', $piutang_number);
        $query = $this->db->get();

        $result = $query->result();
        return json_encode($result);
    }

    function approval_status($data, $sw_number) {
        $this->db->where('EDebtNumber', $sw_number);
        $this->db->update($this->table, $data);
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return 0;
        } else {
            return 1;
        }
    }

    function cek_app($piutang_no) {
        $this->db->select('AppLevel1Status, AppLevel2Status');
        $this->db->from('at_employeedebt');
        $this->db->where('EDebtNumber', $piutang_no);
        $this->db->where('AppLevel1Status', 1);
        $this->db->where('AppLevel2Status', 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($piutang_no) {
        $this->db->select('AppLevel1Status');
        $this->db->from('at_employeedebt');
        $this->db->where('EDebtNumber', $piutang_no);
        $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($piutang_no) {
        $this->db->select('AppLevel2Status');
        $this->db->from('at_employeedebt');
        $this->db->where('EDebtNumber', $piutang_no);
        $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, $piutang_no) {
        $this->db->where('EDebtNumber', $piutang_no);
        $this->db->update('at_employeedebt', $app_finished);
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return 0;
        } else {
            return 1;
        }
    }

    function cek_trans_status($id) {
        $this->db->select('TransStatus');
        $this->db->from('at_employeedebt');
        $this->db->where('EDebtNumber', $id);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $TransStatus = $row->TransStatus;
            }
            return $TransStatus;
        } else {
            return FALSE;
        }
    }

    function pembatalan_transaksi($id, $data) {
        $this->db->where('EDebtNumber', $id);
        return $this->db->update('at_employeedebt', $data);
        /* if ($this->db->trans_status() === FALSE) {
          $this->db->trans_rollback();
          return 0;
          }
          else {
          return 1;
          } */
    }

    function m_delete_journal($piutang_no) {
        $this->db->where('EDebtNumber', $piutang_no);
        $this->db->delete($this->journal);
    }

    function update_approved_data($data, $piutang_number) {
        $this->db->update($this->table, $data, array('EDebtNumber' => $piutang_number));
    }

    function update_to_reverse_payment($piutang_number, $data_piutang) {
        $this->db->update($this->table, $data_piutang, array("EDebtNumber" => $piutang_number));
    }

    function get_company_by_branch($sess_branch) {
        $this->db->select('CompanyName,BranchName');
        $this->db->from('am_company');
        $this->db->join('am_branch', 'am_branch.CompanyCode = am_company.CompanyCode', 'inner');
        // $this->db->join($this->table,$this->table.'.BranchCode = am_branch.BranchCode','inner');
        $this->db->where('am_branch.BranchCode', $sess_branch);
        $query = $this->db->get();
        return $query->row();
    }

    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_employeedebt.EDebtNumber, at_employeedebt.EDebtDate, at_employeedebt.CompTransCode, 
			at_employeedebt.EmplRecpCode, at_employeedebt.EDebtAmount, at_employeedebt.EDebtDesc, at_employeedebt.TransStatus, 
			am_employee.EmployeeName, created.EmployeeName as created_name');
        $this->db->from($this->table);
        $this->db->join('am_branch', 'at_employeedebt.BranchCode=am_branch.BranchCode', 'INNER');
        $this->db->join('am_employee', 'at_employeedebt.EmplRecpCode=am_employee.EmployeeCode', 'INNER');
        $this->db->join('am_employee as created', 'at_employeedebt.CreatedBy=created.EmployeeCode', 'LEFT');

        /* ===================================find by number dan desc========================================================= */
        if ($Number != "") {
            $this->db->like('at_employeedebt.EDebtNumber', $Number);
        }
        if ($Desc != "") {
            $this->db->like('at_employeedebt.EDebtDesc', $Desc);
        }
        /* =============================================end=================================================================== */

        /* ===================================find by branch code========================================================= */
        /*
          get data dari  combobox find di view
         */
        if ($BranchCode != "") {
            $this->db->where('at_employeedebt.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_employeedebt.BranchCode', $sess_branch);
            //-----------------------end-------------------------------------------------//
        }
        /* =======================================================end======================================================== */

        /* =====================================================find by date================================================== */
        if ((($DateFrom != '') && ($DateTo != ''))) {
            if ($DateFrom <= $DateTo) {
                $this->db->where('at_employeedebt.EDebtDate >=', date("Y-m-d", strtotime($DateFrom)));
                $this->db->where('at_employeedebt.EDebtDate <=', date("Y-m-d", strtotime($DateTo)));
            }
        }
        /* ======================================================end=========================================================== */

        /* =====================================================find by status================================================== */
        if (!empty($TransStatus)) {
            $this->db->where_in('at_employeedebt.TransStatus', $TransStatus);
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in('at_employeedebt.TransStatus', array(0, 1, 4, 5, 6, 7, 8));
        }
        /* =======================================================end======================================================== */

        /* ==============================================sort tabel asc========================================================== */
        $this->db->order_by($this->table . '.TransStatus', 'ASC');
        $this->db->order_by($this->table . '.EDebtNumber', 'ASC');
        /* ===============================end================================================================================= */
        $query = $this->db->get();
        return $query->result();
    }

    function get_all_to_print_LAMA($sess_branch) {
        $this->db->select('at_employeedebt.BranchCode,at_employeedebt.CompTransCode,am_accountgl.AccountName, am_employee.EmployeeName, at_employeedebt.EDebtNumber,at_employeedebt.EDebtDate,
						at_employeedebt.EDebtAmount,at_employeedebt.TransStatus as status,at_employeedebt.EDebtDesc,am_branch.BranchName');
        $this->db->from($this->table);
        $this->db->join('am_branch', 'am_branch.BranchCode = at_employeedebt.BranchCode');
        $this->db->join('am_accountgl', 'am_accountgl.AccountNumber = ' . $this->table . '.BankTransfer');
        $this->db->join('am_employee', 'am_employee.EmployeeCode = ' . $this->table . '.EmplRecpCode');
        $this->db->where($this->table . '.BranchCode', $sess_branch);

        $query = $this->db->get();
        return $query->result();
    }

    function get_detail_item_print($piutang_no, $BranchCode, $operasi) {
        $this->db->select($this->table . '.*,b_transfer.AccountName as Bank_transfer, am_receipt.EmployeeName as Emp_Receipt, branch.BranchName,branch.BranchAddress,am_prepared.EmployeeName as nm_prepared,am_dir.EmployeeName as nm_direksi');
        $this->db->from($this->table);
        $this->db->join('am_accountgl as b_transfer', 'b_transfer.AccountNumber = ' . $this->table . '.BankTransfer', 'INNER');
        $this->db->join('am_branch as branch', 'branch.BranchCode = ' . $this->table . '.BranchCode', 'INNER');
        $this->db->join('am_employee as am_receipt', $this->table . '.EmplRecpCode=am_receipt.EmployeeCode', 'LEFT');
        $this->db->join('am_employee as am_prepared', $this->table . '.CreatedBy=am_prepared.EmployeeCode', 'LEFT');
        $this->db->join('am_employee as am_dir', $this->table . '.AppLevel1Code=am_dir.EmployeeCode', 'LEFT');
        $this->db->where('EDebtNumber', $piutang_no);
        $this->db->where($this->table . '.BranchCode ' . $operasi, $BranchCode);
        $query = $this->db->get();
        return $query->row();
    }

    function m_cek_payment_karyawan($piutang_no) {
        $this->db->where('EDebtNumber', $piutang_no);
        $this->db->where_in('TransStatus', array(1, 2, 4));
        $query = $this->db->get($this->payment)->result_array();
        if (count($query) > 0) {
            return true;
        } else {
            return false;
        }
    }

    function m_cek_payment_karyawan_journal($piutang_no) {
        $this->db->where('EDebtNumber', $piutang_no);
        $this->db->where_in('TransStatus', array(1, 2, 4));
        $query = $this->db->get($this->payment_journal)->result_array();
        if (count($query) > 0) {
            return true;
        } else {
            return false;
        }
    }

    function get_no_payment_karyawan($piutang_no) {
        $this->db->select('EInsNumber');
        $this->db->where('EDebtNumber', $piutang_no);
        $query = $this->db->get($this->payment)->result_array();
        return $query[0]['EInsNumber'];
    }

    function get_no_payment_karyawan_journal($piutang_no) {
        $this->db->select('EInjNumber');
        $this->db->where('EDebtNumber', $piutang_no);
        $query = $this->db->get($this->payment_journal)->result_array();
        return $query[0]['EInjNumber'];
    }

    function delete_reconcile($TransNumber) {
        $this->db->where('TransNumber', $TransNumber);
        $this->db->delete($this->reconcile);
    }

    function check_period_end($periodend) {
        $this->db->select('PeriodEnd');
        $this->db->from($this->period);
        $this->db->where('PeriodEnd', $periodend);

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

        if (count($query) > 0) {
            return true;
        } else {
            return false;
        }
    }

    function insert_transsaction($trans_data) {

        $this->db->trans_start();

        /* ======================= insert group transaction ============================ */
        $this->insert_group($trans_data['insert_group']['data']);

        /* ===================== insert journal ================================ */
        for ($x = 0; $x < count($trans_data['insert_journal']['data']); $x++) {
            $this->insert_journal($trans_data['insert_journal']['data'][$x]);
        }
        /* ===================== end insert journal =============================== */

        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            return false;
        } else {
            return true;
        }
    }

    function update_transsaction($trans_data) {

        $this->db->trans_start();

        /* ====================== update group ==================== */
        $this->update_group($trans_data['update_group']['data'], $trans_data['update_group']['TransNumber']);

        /* ========================= delete journal =============================== */
        $this->m_delete_journal($trans_data['delete_journal']['TransNumber']);

        /* ===================== insert journal ================================ */
        for ($x = 0; $x < count($trans_data['insert_journal']['data']); $x++) {
            $this->insert_journal($trans_data['insert_journal']['data'][$x]);
        }

        /* ===================== end insert journal =============================== */
        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            return false;
        } else {
            return true;
        }
    }

    function approve_transsaction($trans_data) {

        $this->db->trans_start();

        /* ============================================= approval transaction =================================================== */
        $this->update_approved_data($trans_data['approve_data']['data'], $trans_data['approve_data']['TransNumber']);

        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            return false;
        } else {
            return true;
        }
    }

    function reverse_transsaction($trans_data) {
        $this->db->trans_start();

        /* ====================================================== reverse payment ==================================================== */
        $this->update_to_reverse_payment($trans_data['reverse_data']['TransNumber'], $trans_data['reverse_data']['data']);

        /* ====================================================== delete reconcile ============================================= */
        $this->delete_reconcile($trans_data['delete_reconcile']['TransNumber']);

        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            return false;
        } else {
            return true;
        }
    }

    function suspend_transsaction($trans_data) {

        $this->db->trans_start();

        /* ===================================== suspend payment ============================================ */
        $this->suspend_data($trans_data['suspend_data']['data'], $trans_data['suspend_data']['TransNumber']);

        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            return false;
        } else {
            return true;
        }
    }

    function get_branch_transaction($piutang_no){
        $this->db->select('gr.BranchCode');
        $this->db->from($this->table.' as gr');
        $this->db->where('gr.EDebtNumber',$piutang_no);

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

        if($query->num_rows()>0){
            $row = $query->row();
            return $row->BranchCode;
        }else{
            return false;
        }
    }

}
