<?php

/*
 * Author : Ferry / Tanggal : 20-03-2015 
 * Author : Dhidhik / Tanggal : 18-02-2016/ Menambah function check_currency_exist 
 * 
 */

class M_currency extends CI_Model {

    public $table = "am_currency";
    public $country = "am_country";
    public $company = "am_company";

    // Function Join
    function join_all() {
        /* ===========================declar post by find================================================ */
        $Code = ($this->input->post('CurrencyCode')) ? $this->input->post('CurrencyCode') : '';
        $Name = ($this->input->post('CurrencyName')) ? $this->input->post('CurrencyName') : '';

        $company_code = ($this->input->post('CompanyCode')) ? $this->input->post('CompanyCode') : '';
        $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("CurrencyCode, CurrencyName, CurrencyDesc, CurrencyCountry, CurrencyRate, CurrencySymbol, TransStatus, CurrencyId, CompanyCode, CountryName");
        $this->db->from($this->table);
        $this->db->join('am_country', 'am_currency.CurrencyCountry=am_country.CountryCode', 'INNER');

        /*
          get data dari  combobox find di view
         */
        if ($company_code != "") {
            $this->db->where('am_currency.CompanyCode', $company_code);
        } 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('am_currency.CompanyCode', $sess_company);
            //-----------------------end-------------------------------------------------//
        }

        if ($Code != "") {
            $this->db->like('am_currency.CurrencyCode', $Code);
        }
        if ($Name != "") {
            $this->db->like('am_currency.CurrencyName', $Name);
        }

        if ($TransStatus != '') {
            $this->db->where_in('am_currency.TransStatus', $TransStatus);
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in('am_currency.TransStatus', array(0, 1));
        }

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

    function get_all_data($company_code = '') {

        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('CompanyCode', $company_code);
        $query = $this->db->get();

        return $query->result();
    }

    function get_all_data_new($company_code) {

        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('CompanyCode', $company_code);
        $this->db->where('TransStatus', 1);
        $query = $this->db->get();

        return $query->result();
    }

    function get_detail($currency_code) {
        $this->db->select('cr.*, ct.CountryName');
        $this->db->from($this->table . ' as cr');
        $this->db->join($this->country . ' as ct', 'ct.CountryCode = cr.CurrencyCountry', 'INNER');
        $this->db->where('cr.CurrencyCode', $currency_code);
        $query = $this->db->get();

        return $query->row();
    }

    function get_detail_by_company($currency_code, $company_code) {
        $this->db->select('cr.*');
        $this->db->from($this->table . ' as cr');
        $this->db->where('cr.CurrencyCode', $currency_code);
        $this->db->where('cr.CompanyCode', $company_code);
        $query = $this->db->get();

        return $query->row();
    }

    function get_detail_new($currency_code) {
        $this->db->select('cr.*, ct.CountryName');
        $this->db->from($this->table . ' as cr');
        $this->db->join($this->country . ' as ct', 'ct.CountryCode = cr.CurrencyCountry', 'INNER');
        $this->db->where('cr.CurrencyId', $currency_code);
        $query = $this->db->get();

        return $query->row();
    }

    function check_code_exist($id) {
        $query = $this->db->get_where($this->table, array('CurrencyCode' => $id, 'CompanyCode' => $this->session->userdata("company"), 'TransStatus' => 1), 1, 0);

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

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

    function update($data, $id, $company_code) {
        $this->db->update($this->table, $data, array('CurrencyId' => $id, 'CompanyCode' => $company_code));
    }

    function update_sub($data, $id) {
        $this->db->update('am_accountsubtype', $data, array('AccountSubTypeID' => $id));
    }

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

    function check_currency($transstatus) {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('TransStatus', $transstatus);
        $this->db->group_by("CurrencyCode");
        $query = $this->db->get();

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

    function check_currency_exist($currency_code, $branch_code, $company_code) {
        $query = $this->db->get_where("am_currency inner join am_branch on(am_branch.CompanyCode=am_currency.CompanyCode AND am_currency.CompanyCode='" . $company_code . "' AND am_branch.BranchCode='" . $branch_code . "' )", array('am_currency.CurrencyCode' => $currency_code, 'am_currency.TransStatus' => 1));
        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    //============ DHIDHIK, 21 Maret 2016 ====================================//
    function data_send_currency_insert($dataSend) {
        $this->db->trans_start();
        foreach ($dataSend['data_currency_insert'] as $key) {
            $this->insert($key);
        }
        foreach ($dataSend['data_coa_insert'] as $key) {
            $account_number = $this->m_coa->create_account_number($key['AccountParent']);
            $data_coa_insert = array(
                'AccountSubTypeID' => $key['AccountSubTypeID'],
                'AccountNumber' => $account_number,
                'AccountName' => $key['AccountName'],
                'CurrencyCode' => $key['CurrencyCode'],
                'AccountParent' => $key['AccountParent'],
                'AccountLevel' => $key['AccountLevel'],
                'BankID' => $key['BankID'],
                'AccountChild' => $key['AccountChild'],
                'AppLevel1Status' => $key['AppLevel1Status'],
                'AppLevel2Status' => $key['AppLevel2Status'],
                'TransStatus' => $key['TransStatus'],
                'LastUser' => $key['LastUser']
            );
            $this->m_coa->insert($data_coa_insert);

            foreach ($dataSend['branch_info'] as $row_branch) {
                $data_glcompany_insert = array(
                    'CompanyCode' => $row_branch->CompanyCode,
                    'BranchCode' => $row_branch->BranchCode,
                    'AccountNumber' => $account_number,
                    'TransStatus' => $key['trans_status'],
                    'PLStatus' => 1, // change PL status 1 ()
                    'GLSaldoYears' => date('Y')
                );
                $this->m_compaccountgl->insert($data_glcompany_insert); //masuk ke model                 
            }
        }

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

    function data_send_currency_update($dataSend) {
        $this->db->trans_start();
        $this->update($dataSend['data_currency_update'], $dataSend['currency_id'], $dataSend['company_code']);
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }

    function data_send_currency_suspend($dataSend) {
        $this->db->trans_start();
        $this->update($dataSend['data_insert'], $dataSend['idcode'], $dataSend['company']);
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }

    function insert_transaction($trans_data) {//ga di pake
        $this->db->trans_start();
        for ($x = 0; $x < count($trans_data['insert_group']['data']); $x++) {
            $this->insert($trans_data['insert_group']['data'][$x]);
        }

        for ($x = 0; $x < count($trans_data['insert_coa']['data']); $x++) {
            $this->m_coa->insert($trans_data['insert_coa']['data'][$x]);
        }

        for ($x = 0; $x < count($trans_data['insert_coa_company']['data']); $x++) {
            $this->m_compaccountgl->insert($trans_data['insert_coa_company']['data'][$x]);
        }

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

    function get_all_to_print($data_filter) {
        /* ===========================declar post by find================================================ */
        $company_code   = $data_filter['cmb_company'];
        $Code           = $data_filter['txt_currency_code'];
        $Name           = $data_filter['txt_Description'];
        $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("CurrencyCode, CurrencyName, CurrencyDesc, CurrencyCountry, CurrencyRate, CurrencySymbol, TransStatus, CurrencyId, CompanyCode");
        $this->db->from($this->table);
        $this->db->join('am_country', 'am_currency.CurrencyCountry=am_country.CountryCode', 'INNER');

        /*
          get data dari  combobox find di view
         */
        if ($company_code != "") {
            $this->db->where('am_currency.CompanyCode', $company_code);
        } 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('am_currency.CompanyCode', $sess_company);
            //-----------------------end-------------------------------------------------//
        }

        if ($Code != "") {
            $this->db->like('am_currency.CurrencyCode', $Code);
        }
        if ($Name != "") {
            $this->db->like('am_currency.CurrencyName', $Name);
        }

        if (!empty($TransStatus)) {
            $this->db->where_in('am_currency.TransStatus', $TransStatus);
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in('am_currency.TransStatus', array(0, 1));
        }

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

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