<?php

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

class M_bank extends CI_Model {

    public $table = "am_bank";

    function get_all() {
        /* ------------------------------lemparan dari halaman view----------------------------- */
        $Code = ($this->input->post('BankCode')) ? $this->input->post('BankCode') : '';
        $Name = ($this->input->post('BankName')) ? $this->input->post('BankName') : '';

        $CompanyCode = $this->input->post('CompanyCode') ? $this->input->post('CompanyCode') : '';
        $BranchCode = $this->input->post('BranchCode') ? $this->input->post('BranchCode') : '';
        $TransStatus = ($this->input->post('TransStatus')) ? $this->input->post('TransStatus') : '';
        /* ------------------------------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("br.BranchName, ab.BankCode, ab.BankName, ab.BankInCode, ab.BankOutCode,ab.BankDesc, ab.BankNumber, 
						(case 
                            when ab.TransStatus = 0 then 'Waiting' 
                            when ab.TransStatus = 1 then 'On Progress' 
                            when ab.TransStatus = 2 then 'Active' 
                            when ab.TransStatus = 3 then 'Suspended' end) as Status,
						ab.TransStatus, ab.BankID, ab.BankNumber");
        $this->db->join('am_branch br', 'br.BranchCode = ab.BranchCode', 'INNER');

        /*
          get data dari  combobox find di view
         */
        if ($BranchCode != "") {
            /* ======================= ini harus pake logic ========================= */
            $this->db->where('ab.BranchCode', $BranchCode);
            /* ======================= end ========================================== */
        } else {
            /*
              inisialisasi untuk loading pertama page branch info
             */
            //----------------kondisi 1 untuk branch-------------------------------------//
            /*
              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
              ini harus diganti logicnya
             */
            $this->db->where('ab.BranchCode', $sess_branch);
            //-----------------------end-------------------------------------------------//
        }

        /* ============================== ini untuk inputan text field ======================== */
        if ($Code != "") {
            $this->db->like('ab.BankCode', $Code);
        }
        if ($Name != "") {
            $this->db->like('ab.BankName', $Name);
        }
        /* ============================== end =========================================== */

        //status itu mengikuti lempar dari halaman view di by status
        /*
          contoh : 	1.Active		=> status[0]
          2.Finish		=> status[1]
          3.Suspended		=> status[2]
          4.Need App. Mgr => status[3]
         */

        if ($TransStatus != '') {
            $this->db->where_in('ab.TransStatus', $TransStatus);
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in('ab.TransStatus', array(0, 1));
        }
        /* ======================= sort =============================================== */
        $this->db->order_by('ab.BranchCode', 'ASC');
        $this->db->order_by('ab.TransStatus', 'ASC');
        /* ======================= untuk limit 150 rows per page ====================== */
        $query = $this->db->limit($end, $beginning);
        /* ======================= end ================================================ */

        $query = $this->db->get($this->table . ' ab');

        return $query->result();
    }

    function get_all_print($branch_code) {

        $this->db->select("br.BranchName, ab.BankCode, ab.BankName, ab.BankInCode, ab.BankOutCode,ab.BankDesc, ab.BankNumber, 
						(case 
                            when ab.TransStatus = 0 then 'Waiting' 
                            when ab.TransStatus = 1 then 'On Progress' 
                            when ab.TransStatus = 2 then 'Active' 
                            when ab.TransStatus = 3 then 'Suspended' end) as Status,
						ab.TransStatus, ab.BankID, ab.BankNumber");
        $this->db->join('am_branch br', 'br.BranchCode = ab.BranchCode', 'INNER');
        $this->db->where('ab.BranchCode', $branch_code);


        $this->db->order_by('ab.TransStatus', 'ASC');

        $query = $this->db->get($this->table . ' ab');

        return $query->result();
    }

    function get_detail($id) {
        $query = $this->db->get_where($this->table, array('BankID' => $id));

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

    function insert($data) {
        $this->db->insert($this->table, $data);

//        if ($this->db->trans_status() == FALSE) {
//            return 0;
//        }
//        else {
//            return 1;
//        }
    }

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

//        if ($this->db->trans_status() == FALSE) {
//            return 0;
//        }
//        else {
//            return 1;
//        }
    }

    function delete($id) {
        $this->db->delete($this->table, array('BankID' => $id));

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

    function check_name_exist($BankName) {
        $sess_company = $this->session->userdata('company');
        $sess_branch = $this->session->userdata('branch');

        $query = $this->db->get_where($this->table, array('BankName' => $BankName, 'CompanyCode' => $sess_company), 1, 0);

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

    function check_number_exist($BankCode) {
        $sess_company = $this->session->userdata('company');
        $sess_branch = $this->session->userdata('branch');

        $query = $this->db->get_where($this->table, array('BankCode' => $BankCode, 'CompanyCode' => $sess_company), 1, 0);

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

    function check_data_by_company_from_bank($id, $company_code, $branch_code) {
        $this->db->_protect_identifiers = FALSE;
        $this->db->select('at_compaccountgl.TransStatus, at_compaccountgl.TransStatus, sum(at_compaccountgl.GLSaldoBalance' . CURRENT_PERIOD . ') as GLSaldoBalance, sum(at_compaccountgl.GLSaldoBSheet) as GLBSheet');
        $this->db->from('at_compaccountgl');
        $this->db->join('am_accountgl', 'am_accountgl.AccountNumber = at_compaccountgl.AccountNumber', 'INNER');

        $this->db->where('am_accountgl.BankID', $id);
        $this->db->where('at_compaccountgl.CompanyCode', $company_code);
        $this->db->where('at_compaccountgl.BranchCode', $branch_code);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));

        $this->db->_protect_identifiers = TRUE;

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

    function get_akungl_by_bank_id($id) {
        $this->db->select('am_accountgl.AccountNumber');
        $this->db->from('am_accountgl');
        $this->db->where('am_accountgl.BankID', $id);

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

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

    function get_company_bank_number($BankCode, $CompanyCode) {
        $this->db->select('BankCode, CompanyName');
        $this->db->from($this->table);
        $this->db->join('am_company', 'am_company.CompanyCode = am_bank.CompanyCode', 'INNER');

        $this->db->where($this->table . '.BankCode', $BankCode);
		$this->db->where($this->table . '.TransStatus', 1);
		$this->db->where($this->table . '.CompanyCode', $CompanyCode);
		
        $query = $this->db->get();

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

    //============= DHIDHIK, 21 Maret 2016 ===================================//
    function data_send_bank_insert($dataSend) {
        $this->db->trans_start();
        foreach ($dataSend['data_insert'] as $key) {
            $this->insert($key);
        }
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }

    function data_send_bank_update($dataSend) {
        $this->db->trans_start();
        foreach ($dataSend['data_bank_update'] as $key) {
            $data_bank_update = array(
                'CompanyCode' => $key['CompanyCode'],
                'BranchCode' => $key['BranchCode'],
                'BankCode' => $key['BankCode'],
                'BankName' => $key['BankName'],
                'BankNumber' => $key['BankNumber'],
                'BankAddress' => $key['BankAddress'],
                'BankCity' => $key['BankCity'],
                'BankState' => $key['BankState'],
                'BankCountry' => $key['BankCountry'],
                'BankDesc' => $key['BankDesc'],
                'BankInCode' => $key['BankInCode'],
                'BankOutCode' => $key['BankOutCode'],
                'CurrencyCode' => $key['CurrencyCode'],
                'TransStatus' => $key['TransStatus'],
                'LastUser' => $key['LastUser']
            );
            $this->update($data_bank_update, $key['idcode']);
        }
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }

    function data_send_bank_suspend($dataSend) {
        $this->db->trans_start();
        $this->update($dataSend['data_bank_suspend'], $dataSend['idcode']);
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }
    
    function get_all_to_print($data_filter) {
        /* ------------------------------lemparan dari halaman view----------------------------- */
        $CompanyCode    = $data_filter['cmb_company'];
        $BranchCode     = $data_filter['cmb_branch'];
        $Code           = $data_filter['txt_BankCode'];
        $Name           = $data_filter['txt_BankName'];
        $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("br.BranchName, ab.BankCode, ab.BankName, ab.BankInCode, ab.BankOutCode,ab.BankDesc, ab.BankNumber, 
						(case 
                            when ab.TransStatus = 0 then 'Waiting' 
                            when ab.TransStatus = 1 then 'On Progress' 
                            when ab.TransStatus = 2 then 'Active' 
                            when ab.TransStatus = 3 then 'Suspended' end) as Status,
						ab.TransStatus, ab.BankID, ab.BankNumber");
        $this->db->join('am_branch br', 'br.BranchCode = ab.BranchCode', 'INNER');

        /*
          get data dari  combobox find di view
         */
        if ($BranchCode != "") {
            /* ======================= ini harus pake logic ========================= */
            $this->db->where('ab.BranchCode', $BranchCode);
            /* ======================= end ========================================== */
        } else {
            /*
              inisialisasi untuk loading pertama page branch info
             */
            //----------------kondisi 1 untuk branch-------------------------------------//
            /*
              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
              ini harus diganti logicnya
             */
            $this->db->where('ab.BranchCode', $sess_branch);
            //-----------------------end-------------------------------------------------//
        }

        /* ============================== ini untuk inputan text field ======================== */
        if ($Code != "") {
            $this->db->like('ab.BankCode', $Code);
        }
        if ($Name != "") {
            $this->db->like('ab.BankName', $Name);
        }
        /* ============================== end =========================================== */

        //status itu mengikuti lempar dari halaman view di by status
        /*
          contoh : 	1.Active		=> status[0]
          2.Finish		=> status[1]
          3.Suspended		=> status[2]
          4.Need App. Mgr => status[3]
         */

        if (!empty($TransStatus)) {
            $this->db->where_in('ab.TransStatus', $TransStatus);
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in('ab.TransStatus', array(0, 1));
        }
        /* ======================= sort =============================================== */
        $this->db->order_by('ab.BranchCode', 'ASC');
        $this->db->order_by('ab.TransStatus', 'ASC');
        $query = $this->db->get($this->table . ' ab');

        return $query->result();
    }

    //============= End DHIDHIK, 21 Maret 2016 ===================================//
}

/* End of file m_bank.php */
/* Location: ./application/modules_core/list/models/m_bank.php */