<?php

class M_ibank_transfer extends CI_Model {

    public $table = "at_interbanktransfer";
    public $journal = "at_ibanktransferjournal";
    public $branch = "am_branch";
    public $account = "at_compaccountgl";
    public $acc_gl = "am_accountgl";
    public $reconcile = "at_bankreconcille";
    public $sub_type = "am_accountsubtype";
    public $period = "at_periodend";
    public $employee = "am_employee";
    public $department = "am_department";

    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_interbanktransfer.IBankNumber, at_interbanktransfer.IBankDate, cf.BranchName as CompTo, ct.BranchName as CompFrom,bf.AccountName as BankFrom, bt.AccountName as BankTo,
         at_interbanktransfer.IBankAmount, at_interbanktransfer.IBankDesc, at_interbanktransfer.TransStatus,am_employee.EmployeeName as Created');
        $this->db->from($this->table);
        $this->db->join('am_branch', 'at_interbanktransfer.BranchCode = am_branch.BranchCode', 'INNER');
        $this->db->join('am_accountgl as bf', 'bf.AccountNumber = at_interbanktransfer.BankTransfer', 'left');
        $this->db->join('am_accountgl as bt', 'bt.AccountNumber = at_interbanktransfer.BankReceipt', 'left');
        $this->db->join('am_branch as cf', 'cf.BranchCode = at_interbanktransfer.CompTransCode', 'left');
        $this->db->join('am_branch as ct', 'ct.BranchCode = at_interbanktransfer.CompRecpCode', 'left');
        $this->db->join('am_employee', $this->table . '.CreatedBy=am_employee.EmployeeCode', 'LEFT');

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

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

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

        /* =====================================================find by status================================================== */
        if ($TransStatus != '') {
            $this->db->where_in('at_interbanktransfer.TransStatus', $TransStatus);
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in('at_interbanktransfer.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 . '.IBankNumber', 'ASC');
        /* ===============================end================================================================================= */
        $query = $this->db->limit($end, $beginning);
        $query = $this->db->get();
        return $query->result();
    }

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

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

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

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

        $ib_number = $branch . "/" . $month . $year . "IB/";

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

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

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

            $new_ib_number = $ib_number . $number;
        }

        return $new_ib_number;
    }

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

    function get_company($company) {
        $this->db->select('BranchCode');
        $this->db->from('am_branch');
        $this->db->where('BranchStatus', 1);
        $this->db->where('CompanyCode', $company);
        $query = $this->db->get();

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

    function get_akun_number($cmb_bank_to, $company) {
        $this->db->select('*');
        $this->db->from('am_accountgl');
        $this->db->join('at_compaccountgl', 'am_accountgl.AccountNumber=at_compaccountgl.AccountNumber');
        $this->db->where('am_accountgl.AccountNumber', $cmb_bank_to);
        $this->db->where('at_compaccountgl.CompanyCode', $company);
        $query = $this->db->get();

        return $query->row();
    }

    function get_branch_by_name($CompanyCode, $branch_trans) {
        $this->db->select('BranchCode');
        $this->db->from('am_branch');
        $this->db->where('BranchName', $branch_trans);
        $this->db->where('CompanyCode', $CompanyCode);
        $query = $this->db->get();

        $row = $query->row();
        return $row->BranchCode;
    }

    function get_coa_bank_from_ho_add($CompanyCode) {


        $jobtitle = $this->session->userdata("jobtitle");
        $BranchCode = $this->session->userdata('branch');
        $BranchStatus = $this->session->userdata('branch_status');

        $this->db->select('ag.AccountNumber, ag.AccountName, ag.CurrencyCode, br.BranchName');
        $this->db->from($this->account . ' as ac');
        $this->db->join($this->acc_gl . ' as ag', 'ac.AccountNumber=ag.AccountNumber', 'inner');
        $this->db->join($this->branch . ' as br', 'ac.BranchCode=br.BranchCode', 'inner');
        $this->db->join($this->sub_type . ' as sub', 'ag.AccountSubTypeID=sub.AccountSubTypeID', 'inner');

        $this->db->where('ac.CompanyCode', $CompanyCode);
        //$this->db->where('at_compaccountgl.BranchCode', $BranchCode);
        $this->db->where('ac.GLSaldoYears', date('Y'));
        $this->db->where('ac.TransStatus', 2);

        $this->db->where('br.BranchStatus', 0);
        $this->db->where('sub.AccountTypeName', 'Bank');
        $this->db->where('ag.AccountChild', 1);

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

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

    function get_coa_bank_from_ho_edit($CompanyCode, $replace) {


        $jobtitle = $this->session->userdata("jobtitle");
        $BranchCode = $this->session->userdata('branch');
        $BranchStatus = $this->session->userdata('branch_status');

        $this->db->select('ag.AccountNumber, ag.AccountName, ag.CurrencyCode, br.BranchName');
        $this->db->from($this->account . ' as ac');
        $this->db->join($this->acc_gl . ' as ag', 'ac.AccountNumber=ag.AccountNumber', 'inner');
        $this->db->join($this->branch . ' as br', 'ac.BranchCode=br.BranchCode', 'inner');
        $this->db->join($this->sub_type . ' as sub', 'ag.AccountSubTypeID=sub.AccountSubTypeID', 'inner');

        $this->db->where('ac.CompanyCode', $CompanyCode);
        //$this->db->where('at_compaccountgl.BranchCode', $BranchCode);
        $this->db->where('ac.GLSaldoYears', date('Y'));
        $this->db->where('ac.TransStatus', 2);

        $this->db->where('br.BranchStatus', 0);
        $this->db->where('sub.AccountTypeName', 'Bank');
        $this->db->where('ag.AccountChild', 1);

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

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

    function get_branchCode($ibank_number) {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('IBankNumber', $ibank_number);
        $query = $this->db->get();

        return $query->row();
    }

    function get_date_journal_ibank_transfer($ibank_number) {
        $this->db->select('*, am_company.CompanyName,am_branch.BranchName');
        $this->db->from($this->table);
        $this->db->join('am_branch', 'am_branch.BranchCode=at_interbanktransfer.BranchCode');
        $this->db->join('am_company', 'am_company.CompanyCode=am_branch.CompanyCode');
        $this->db->where('IBankNumber', $ibank_number);
        $query = $this->db->get();

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

    function get_all_journal_ibank_trans($ibank_number, $CompTransCode) {
        $this->db->select('*, am_accountgl.AccountName');
        $this->db->from('at_ibanktransferjournal');
        $this->db->join('am_accountgl', 'am_accountgl.AccountNumber=at_ibanktransferjournal.IBankAccountNumber');
        $this->db->join('at_compaccountgl', 'at_compaccountgl.AccountNumber=am_accountgl.AccountNumber');
        //$this->db->where('at_bsweepjournal.BranchCode', $CompRecpCode);
        $this->db->where('at_compaccountgl.BranchCode', $CompTransCode);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        $this->db->where('at_ibanktransferjournal.IBankNumber', $ibank_number);
        $query = $this->db->get();

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

    function get_all_journal_ibank($ibank_number, $CompRecpCode) {
        $this->db->select('*, am_accountgl.AccountName,am_branch.BranchName');
        $this->db->from('at_ibanktransferjournal');
        $this->db->join('am_accountgl', 'am_accountgl.AccountNumber=at_ibanktransferjournal.IBankAccountNumber', 'inner');
        $this->db->join('at_compaccountgl', 'at_compaccountgl.AccountNumber=am_accountgl.AccountNumber', 'inner');
        $this->db->join('am_branch', 'am_branch.BranchCode=at_ibanktransferjournal.BranchRecpCode', 'inner');
        //$this->db->where('at_bsweepjournal.BranchCode', $CompRecpCode);
        $this->db->where('at_compaccountgl.BranchCode', $CompRecpCode);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        $this->db->where('at_ibanktransferjournal.IBankNumber', $ibank_number);
        $query = $this->db->get();

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

    function get_all_journal_ibank_transfer($ibank_number, $Branch_trans) {
        $this->db->select('*, am_accountgl.AccountName,am_branch.BranchName');
        $this->db->from('at_ibanktransferjournal');
        $this->db->join('am_accountgl', 'am_accountgl.AccountNumber=at_ibanktransferjournal.IBankAccountNumber', 'inner');
        $this->db->join('at_compaccountgl', 'at_compaccountgl.AccountNumber=am_accountgl.AccountNumber', 'inner');
        $this->db->join('am_branch', 'am_branch.BranchCode=at_ibanktransferjournal.BranchRecpCode', 'inner');
        //$this->db->where('at_bsweepjournal.BranchCode', $CompRecpCode);
        // $this->db->where('at_compaccountgl.BranchCode', $CompRecpCode);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        $this->db->where('at_ibanktransferjournal.IBankNumber', $ibank_number);
        $query = $this->db->get();

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

    function get_coa_bank_to_ho_add($CompanyCode, $currency_idr, $currency_code) {
        $jobtitle = $this->session->userdata("jobtitle");
        $BranchCode = $this->session->userdata('branch');
        $BranchStatus = $this->session->userdata('branch_status');

        $this->db->select('ag.AccountNumber, ag.AccountName, ag.CurrencyCode, br.BranchName');
        $this->db->from($this->account . ' as ac');
        $this->db->join($this->acc_gl . ' as ag', 'ac.AccountNumber=ag.AccountNumber', 'inner');
        $this->db->join($this->branch . ' as br', 'ac.BranchCode=br.BranchCode', 'inner');
        $this->db->join($this->sub_type . ' as sub', 'ag.AccountSubTypeID=sub.AccountSubTypeID', 'inner');

        $this->db->where('ac.CompanyCode', $CompanyCode);
        //$this->db->where('at_compaccountgl.BranchCode', $BranchCode);
        $this->db->where('ac.GLSaldoYears', date('Y'));
        $this->db->where('ac.TransStatus', 2);

        if ($currency_code == "IDR") {
            //$arry_currency = array($currency_idr,$currency_code);
            $arry_currency = array($currency_idr);
            $this->db->where_in('ag.CurrencyCode', $arry_currency);
        } else {
            $arry_currency = array($currency_code);
            $this->db->where_in('ag.CurrencyCode', $arry_currency);
        }

        //$this->db->where('am_branch.BranchStatus', 1);
        $this->db->where('sub.AccountTypeName', 'Bank');
        $this->db->where('ag.AccountChild', 1);

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

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

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

    function get_detail($id) {
        $this->db->select('gr.*,em.DepartmentCode,br.BranchName as branch_to,bc.BranchName as branch_from');
        $this->db->from($this->table . ' as gr');
        $this->db->join($this->employee . ' as em', 'em.EmployeeCode=gr.CreatedBy', 'inner');
        $this->db->join($this->branch . ' as br', 'br.BranchCode=gr.CompTransCode', 'inner');
        $this->db->join($this->branch . ' as bc', 'bc.BranchCode=gr.CompRecpCode', 'inner');
        $this->db->where('gr.IBankNumber', $id);
        $query = $this->db->get();

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

    function m_insert_journal($journal_debet) {
        $this->db->insert($this->journal, $journal_debet);
    }

    function m_update_group($data_ibank_transfer, $txt_ibank_no) {
        $this->db->where('IBankNumber', $txt_ibank_no);
        $this->db->update($this->table, $data_ibank_transfer);
    }

    function update_approved_data($data, $txt_ibank_no) {
        $this->db->where('IBankNumber', $txt_ibank_no);
        $this->db->update($this->table, $data);
    }

    function status_finished($app_finished, $txt_ibank_no) {
        $this->db->where('IBankNumber', $txt_ibank_no);
        $this->db->update('at_interbanktransfer', $app_finished);
    }

    function suspend_data($data_ibank, $IBankNumber) {
        $this->db->update($this->table, $data_ibank, array('IBankNumber' => $IBankNumber));
    }

    function delete_journal($txt_ibank_no) {
        $this->db->where('IBankNumber', $txt_ibank_no);
        return $this->db->delete('at_ibanktransferjournal');
    }

    function update_to_reverse($txt_ibank_no, $data_reversed) {
        $this->db->where('IBankNumber', $txt_ibank_no);
        $this->db->update($this->table, $data_reversed);
    }

    /* ==============================funngsi get detail untuk print preview================================= */

    function get_detail_print($bt_Number) {

        $this->db->select('bs.*, bank_from.AccountName as nm_bank_from, bank_to.AccountName as nm_bank_to, am_br.BranchName, am_br.BranchAddress, prepared.EmployeeName as nm_prepared, mng.EmployeeName as nm_manager');
        $this->db->from($this->table . ' as bs');
        $this->db->join('am_accountgl as bank_from', 'bank_from.AccountNumber=bs.BankTransfer', 'INNER');
        $this->db->join('am_accountgl as bank_to', 'bank_to.AccountNumber=bs.BankReceipt', 'INNER');
        $this->db->join('am_branch as am_br', 'am_br.BranchCode=bs.BranchCode', 'INNER');
        $this->db->join('am_employee as prepared', 'prepared.EmployeeCode=bs.CreatedBy', 'LEFT');
        $this->db->join('am_employee as mng', 'mng.EmployeeCode=bs.AppLevel1Code', 'LEFT');
        $this->db->where('IBankNumber', $bt_Number);
        $query = $this->db->get();

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

    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 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_interbanktransfer.IBankNumber, at_interbanktransfer.IBankDate, cf.BranchName as CompTo, ct.BranchName as CompFrom,bf.AccountName as BankFrom, bt.AccountName as BankTo,
         at_interbanktransfer.IBankAmount, at_interbanktransfer.IBankDesc, at_interbanktransfer.TransStatus,am_employee.EmployeeName as Created');
        $this->db->from($this->table);
        $this->db->join('am_branch', 'at_interbanktransfer.BranchCode = am_branch.BranchCode', 'INNER');
        $this->db->join('am_accountgl as bf', 'bf.AccountNumber = at_interbanktransfer.BankTransfer', 'left');
        $this->db->join('am_accountgl as bt', 'bt.AccountNumber = at_interbanktransfer.BankReceipt', 'left');
        $this->db->join('am_branch as cf', 'cf.BranchCode = at_interbanktransfer.CompTransCode', 'left');
        $this->db->join('am_branch as ct', 'ct.BranchCode = at_interbanktransfer.CompRecpCode', 'left');
        $this->db->join('am_employee', $this->table . '.CreatedBy=am_employee.EmployeeCode', 'LEFT');

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

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

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

        /* =====================================================find by status================================================== */
        if (!empty($TransStatus)) {
            $this->db->where_in('at_interbanktransfer.TransStatus', $TransStatus);
        } else {
            /*
              trans status 0,1 adalah aktif
             */
            $this->db->where_in('at_interbanktransfer.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 . '.IBankNumber', 'ASC');
        /* ===============================end================================================================================= */
        $query = $this->db->get();
        return $query->result();
    }
    function get_all_to_print_LAMA($sess_branch) {
        $this->db->select('ib.IBankDate,ib.IBankNumber,ib.IBankDesc,ib.IBankAmount,bf.AccountName as BankFrom,bt.AccountName as BankTo,cf.CompanyName as CompFrom,ct.CompanyName as CompTo');
        $this->db->from($this->table . ' as ib');
        $this->db->join('am_accountgl as bf', 'bf.AccountNumber = ib.BankTransfer');
        $this->db->join('am_accountgl as bt', 'bt.AccountNumber = ib.BankReceipt');
        $this->db->join('am_company as cf', 'cf.CompanyCode = ib.CompTransCode', 'left');
        $this->db->join('am_company as ct', 'ct.CompanyCode = ib.CompRecpCode', 'left');
        $this->db->where('ib.BranchCode', $sess_branch);

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

    function insert_transsaction($trans_data) {

        $this->db->trans_start();

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

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

        $this->db->trans_complete();

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

    function update_transsaction($trans_data) {

        $this->db->trans_start();

        $this->m_update_group($trans_data['update_group']['data'], $trans_data['update_group']['TransNumber']);

        $this->delete_journal($trans_data['delete_journal']['TransNumber']);

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

        $this->db->trans_complete();

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

    function approve_transsaction($trans_data) {

        $this->db->trans_start();

        $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 suspend_transsaction($trans_data) {

        $this->db->trans_start();

        $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 reverse_transsaction($trans_data) {

        $this->db->trans_start();

        $this->update_to_reverse($trans_data['reverse_data']['TransNumber'], $trans_data['reverse_data']['data']);
        $this->delete_reconcile($trans_data['delete_reconcile']['TransNumber']);

        $this->db->trans_complete();

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

}
