<?php
/*      Author 	: Wahyu / Tanggal : 22-07-2014	
 *	Revisi 	: Ferry / Tanggal : 23-03-2015  
 */

class M_preference extends CI_Model{
    public $itemcategory                = "am_itemcategory";
    public $account_gl                  = "am_accountgl";
    public $company_gl                  = "at_compaccountgl";
    public $branch              		= 'am_branch';
	 
    public $account_type                = "am_accounttype";
    public $account_subtype             = "am_accountsubtype";
    
    public $sys_itemcategory            = "sys_itemcategory";
    public $sys_itemaccount             = "sys_itemaccount";
    
    public $sys_salesaccount            = "sys_salesaccount";
	public $sys_sales_kategori 			= "sys_salescategory";
	
	public $sys_purchase_kategori 		= "sys_purchasecategory";
    public $sys_purcahseaccount         = "sys_purchaseaccount";
    
    public $sys_service_category        = "sys_servicecategory";
    public $sys_service_detail          = "sys_servicedetail";
    public $sys_service_account         = "sys_serviceaccount";

    public $sys_advance_category        = "sys_advancecategory";
    public $sys_advance_detail          = "sys_advancedetail";
    public $sys_advance_account         = "sys_advanceaccount";
    
    public $sys_expense_cataegory       = "sys_expensecategory";
    public $sys_expense_detail          = "sys_expensedetail";
    public $sys_expense_account         = "sys_expenseaccount";
    
    //item category
    function get_all_item_category(){
        $this->db->select('*');
        $this->db->from($this->itemcategory);
		$this->db->where($this->itemcategory.'.TransStatus', 1);
        $this->db->order_by('ItemCategoryName', 'ASC');

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

    function get_all_categoies_item_expense(){
        $this->db->select('*');
        $this->db->from($this->itemcategory);
		$this->db->where($this->itemcategory.'.TransStatus', 1);
        $this->db->order_by('ItemCategoryID', 'ASC');

        return $this->db->get()->result();
    }
	
    function get_all_akun($company){
        $this->db->select('*');
        $this->db->from($this->company_gl . 'cg');
        $this->db->join($this->account_gl . ' ag', 'ag.AccountNumber = cg.AccountNumber','INNER');
        $this->db->where('cg.CompanyCode', $company);
        $this->db->order_by('ag.AccountName', 'ASC');

        return $this->db->get()->result();
    }
	
    function get_akun($company, $id){
        $this->db->distinct();
        $this->db->select('ag.AccountNumber, ag.AccountName');
        $this->db->from($this->company_gl . ' as cg');
        $this->db->join($this->account_gl . ' as ag', 'ag.AccountNumber = cg.AccountNumber', 'INNER');
        $this->db->where('cg.CompanyCode', $company);
        $this->db->where('ag.AccountSubTypeID', $id);
        $this->db->where('ag.CurrencyCode', 'IDR');
        $this->db->where('ag.AccountChild', 1);
        $this->db->where('cg.TransStatus', 2);
        $this->db->where('cg.GLSaldoYears', date('Y'));
        $this->db->order_by('ag.AccountName', 'ASC');

        return $this->db->get()->result();
    }
	
	//by fahri
	function get_akun_sub_type_name($company, $sub_type_name){
		$this->db->distinct();
        $this->db->select('ag.AccountNumber, ag.AccountName');
		$this->db->from($this->company_gl . ' as cg');
		$this->db->join($this->account_gl . ' as ag', 'ag.AccountNumber = cg.AccountNumber', 'INNER');
		$this->db->join($this->account_subtype . ' as sbt', 'ag.AccountSubTypeID = sbt.AccountSubTypeID', 'INNER');
		$this->db->where('cg.CompanyCode', $company);
		$this->db->where_in('sbt.AccountTypeName', $sub_type_name);
		$this->db->where('ag.CurrencyCode', 'IDR');
		$this->db->where('ag.AccountChild', 1);
		$this->db->where('cg.TransStatus', 2);
		$this->db->where('cg.GLSaldoYears', date('Y'));
		$this->db->order_by('ag.AccountName', 'ASC');

		return $this->db->get()->result();
    }
	//end
	
    function get_akun_by_type_name($account_type_name){
        $company_code = $this->session->userdata('company');
        
        $this->db->distinct();
        $this->db->select('ag.AccountNumber, ag.AccountName');
        $this->db->from($this->company_gl . ' as cg');
        $this->db->join($this->account_gl . ' as ag', 'ag.AccountNumber = cg.AccountNumber', 'INNER');
        $this->db->join($this->account_subtype . ' as st', 'st.AccountSubTypeID = ag.AccountSubTypeID', 'INNER');
        $this->db->join($this->account_type . ' as at', 'at.AccountTypeID = st.AccountTypeID', 'INNER');
        $this->db->where('cg.CompanyCode', $company_code);
        $this->db->where_in('at.AccountTypeName', $account_type_name);
        $this->db->where('ag.CurrencyCode', 'IDR');
        $this->db->where('ag.AccountChild', 1);
        $this->db->where('cg.TransStatus', 2);
        $this->db->where('cg.GLSaldoYears', date('Y'));
        $this->db->order_by('ag.AccountName', 'ASC');

        return $this->db->get()->result();
    }    
    
    function get_akun_advance($company, $type){
        $branch = $this->session->userdata('branch');		
		
        $this->db->select('*');
        $this->db->from($this->company_gl . ' as cg');
        $this->db->join($this->account_gl . ' as ag', 'ag.AccountNumber = cg.AccountNumber', 'INNER');
        $this->db->where('cg.CompanyCode', $company);
        $this->db->where('cg.BranchCode', $branch);
        $this->db->where_in('ag.AccountSubTypeID', $type);
        $this->db->where('ag.CurrencyCode', 'IDR');
        $this->db->where('ag.AccountChild', 1);
        $this->db->where('cg.TransStatus', 2);
        $this->db->where('cg.GLSaldoYears', date('Y'));
        $this->db->order_by('ag.AccountName', 'ASC');

        return $this->db->get()->result();
    }
	
	//by fahri
	 function get_akun_advance_by_name($company, $sub_type_name){
        $branch = $this->session->userdata('branch');		
		$this->db->distinct();
        $this->db->select('*');
        $this->db->from($this->company_gl . ' as cg');
        $this->db->join($this->account_gl . ' as ag', 'ag.AccountNumber = cg.AccountNumber', 'INNER');
		$this->db->join($this->account_subtype . ' as sbt', 'ag.AccountSubTypeID = sbt.AccountSubTypeID', 'INNER');
        $this->db->where('cg.CompanyCode', $company);
        $this->db->where('cg.BranchCode', $branch);
        $this->db->where_in('sbt.AccountTypeName', $sub_type_name);
        $this->db->where('ag.CurrencyCode', 'IDR');
        $this->db->where('ag.AccountChild', 1);
        $this->db->where('cg.TransStatus', 2);
        $this->db->where('cg.GLSaldoYears', date('Y'));
        $this->db->order_by('ag.AccountName', 'ASC');

        return $this->db->get()->result();
    }
	//end

//    function get_akun_advance_purchase($company, $type){
////        $type = array('25','26','27');
//        
//        $this->db->select('*');
//        $this->db->from($this->company_gl);
//        $this->db->join('am_accountgl','at_compaccountgl.AccountNumber=am_accountgl.AccountNumber','INNER');
//        $this->db->where($this->company_gl.'.CompanyCode', $company);
//        $this->db->where_in('am_accountgl.AccountSubTypeID', $type);
//        $this->db->where('am_accountgl.AccountChild', 1);
//        $this->db->where('am_accountgl.TransStatus', 2);
//        $this->db->where($this->company_gl.'.GLSaldoYears', date('Y'));
//        $this->db->order_by('am_accountgl.AccountName', 'ASC');
//
//        return $this->db->get()->result();
//    }
	
    function get_expense_akun($company_code){
        $branch_code = $this->session->userdata('branch');
        
        $this->db->select('cg.AccountNumber, ag.AccountName');
        $this->db->from($this->company_gl . ' cg');
        $this->db->join($this->account_gl . ' ag', 'ag.AccountNumber = cg.AccountNumber','INNER');
        $this->db->join($this->account_subtype . ' st', 'st.AccountSubTypeID = ag.AccountSubTypeID', 'INNER');
        $this->db->where('CompanyCode', $company_code);
        $this->db->where('BranchCode', $branch_code);
        $this->db->like('st.AccountTypeName', 'Kas Bon');
        $this->db->where('ag.AccountChild', 1);
        $this->db->where('cg.TransStatus', 2);
        $this->db->where('cg.GLSaldoYears', date('Y'));
        $this->db->order_by('ag.AccountName', 'ASC');

       return $this->db->get()->result();
    }
	
    function get_payable_expense_akun($company){
        $this->db->distinct('*');
        $this->db->from($this->company_gl);
        $this->db->join('am_accountgl','at_compaccountgl.AccountNumber=am_accountgl.AccountNumber','INNER');
        $this->db->where($this->company_gl.'.CompanyCode', $company);
        $this->db->where($this->company_gl.'.BranchCode', $this->session->userdata('branch'));
        $this->db->where_in('am_accountgl.AccountSubTypeID', array('27', '57', '58', '59', '64'));
        $this->db->where('am_accountgl.AccountChild', 1);
        $this->db->where($this->company_gl.'.TransStatus', 2);
        $this->db->order_by('am_accountgl.AccountName', 'ASC');
        
        return $this->db->get()->result();
    }

    function get_realized_account($company){
        $this->db->select('*');
        $this->db->from($this->company_gl);
        $this->db->join('am_accountgl','at_compaccountgl.AccountNumber=am_accountgl.AccountNumber','INNER');
        $this->db->where($this->company_gl.'.CompanyCode', $company);
        $this->db->where($this->company_gl.'.BranchCode', $this->session->userdata('branch'));
        $this->db->where('am_accountgl.AccountSubTypeID', 60);
        $this->db->where('am_accountgl.AccountChild', 1);
        $this->db->where($this->company_gl.'.TransStatus', 2);
        $this->db->order_by('am_accountgl.AccountName', 'ASC');
        
        return $this->db->get()->result();
    }
    
    function get_unrealized_account($company){
        $this->db->select('*');
        $this->db->from($this->company_gl);
        $this->db->join('am_accountgl','at_compaccountgl.AccountNumber=am_accountgl.AccountNumber','INNER');
        $this->db->where($this->company_gl.'.CompanyCode', $company);
        $this->db->where($this->company_gl.'.BranchCode', $this->session->userdata('branch'));
        $this->db->where('am_accountgl.AccountSubTypeID', 60);
        $this->db->where('am_accountgl.AccountChild', 1);
        $this->db->where($this->company_gl.'.TransStatus', 2);
        $this->db->order_by('am_accountgl.AccountName', 'ASC');
        
        return $this->db->get()->result();
    }

    
    /**************************************************************Function Model Untuk Preference Account Item ************************************/
    //sys item catgeory
    function insert_sys_itemcategory($data_sysitemcategory){
        $this->db->insert($this->sys_itemcategory, $data_sysitemcategory);
    }

    function select_max_SysItemCategoryID(){
        $this->db->select_max('SysItemCategoryID');
        $this->db->from($this->sys_itemcategory);
        $query = $this->db->get();

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

            return $SysItemCategoryID;
        }
        else {
            return FALSE;
        }
    }

    function get_sysitem_categori_id($ItemCategoryID){
        $CompanyCode = $this->session->userdata('company');

        $this->db->select('SysItemCategoryID');
        $this->db->from($this->sys_itemcategory);
        $this->db->where('ItemCategoryID', $ItemCategoryID);
        $this->db->where('CompanyCode', $CompanyCode);
        $query = $this->db->get();

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

            return $SysItemCategoryID;
        }
        else {
            return FALSE;
        }
    }

    function cek_kode_on_preference($code_akun, $SysItemCategoryID){
        $this->db->select('AccountCode');
        $this->db->from($this->sys_itemaccount);
        $this->db->where('AccountCode', $code_akun);
        $this->db->where('SysItemCategoryID', $SysItemCategoryID);
        $query = $this->db->get();

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

            return $AccountCode;
        }
        else {
            return FALSE;
        }
    }

    function cek_approval_on_item_account($ItemCategoryID){
        $this->db->select('AppLevel1Status');
        $this->db->from('sys_itemcategory');
        $this->db->where('ItemCategoryID', $ItemCategoryID);
        $query = $this->db->get();

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

            return $AppLevel1Status;
        }
        else {
            return FALSE;
        }
    }

    //sys item account 
    function insert_sysitemaccount($data_sysitemaccount){
        $this->db->insert($this->sys_itemaccount, $data_sysitemaccount);
    }

    function update_sysitemaccount($data_sysitemaccount, $SysItemCategoryID, $code){
        $this->db->where('SysItemCategoryID', $SysItemCategoryID);
        $this->db->where('AccountCode', $code);
        return $this->db->update($this->sys_itemaccount, $data_sysitemaccount);
    }

    function update_approval_item_account($data_item, $sysitemcategoryid){
        $this->db->where('SysItemCategoryID', $sysitemcategoryid);
        $this->db->where('AppLevel1Status', 0);
        return $this->db->update('sys_itemcategory', $data_item);
    }

    //akun sales value
    function get_akun_sales_value($item_id){
        $company_code = $this->session->userdata('company');

        $this->db->select('sys_itemaccount.AccountNumber');
        $this->db->from($this->sys_itemcategory);
        $this->db->join('sys_itemaccount','sys_itemcategory.SysItemCategoryID=sys_itemaccount .SysItemCategoryID','INNER');
        $this->db->where('sys_itemcategory.ItemCategoryID', $item_id);
        $this->db->where('sys_itemaccount.AccountCode', 'Sales');
        $this->db->where('sys_itemcategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun cogs value
    function get_akun_cogs_value($item_id){
        $company_code = $this->session->userdata('company');

        $this->db->select('sys_itemaccount.AccountNumber');
        $this->db->from($this->sys_itemcategory);
        $this->db->join('sys_itemaccount','sys_itemcategory.SysItemCategoryID=sys_itemaccount .SysItemCategoryID','INNER');
        $this->db->where('sys_itemcategory.ItemCategoryID', $item_id);
        $this->db->where('sys_itemaccount.AccountCode', 'COGS');
        $this->db->where('sys_itemcategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun inventory value
    function get_akun_inventory_value($item_id){
        $company_code = $this->session->userdata('company');

        $this->db->select('sys_itemaccount.AccountNumber');
        $this->db->from($this->sys_itemcategory);
        $this->db->join('sys_itemaccount','sys_itemcategory.SysItemCategoryID=sys_itemaccount .SysItemCategoryID','INNER');
        $this->db->where('sys_itemcategory.ItemCategoryID', $item_id);
        $this->db->where('sys_itemaccount.AccountCode', 'Inventory');
        $this->db->where('sys_itemcategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun txable21 value
    function get_akun_taxable21_value($item_id){
        $company_code = $this->session->userdata('company');

        $this->db->select('sys_itemaccount.AccountNumber');
        $this->db->from($this->sys_itemcategory);
        $this->db->join('sys_itemaccount','sys_itemcategory.SysItemCategoryID=sys_itemaccount .SysItemCategoryID','INNER');
        $this->db->where('sys_itemcategory.ItemCategoryID', $item_id);
        $this->db->where('sys_itemaccount.AccountCode', 'TaxablePPH21');
        $this->db->where('sys_itemcategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun txable22 value
    function get_akun_taxable22_value($item_id){
        $company_code = $this->session->userdata('company');

        $this->db->select('sys_itemaccount.AccountNumber');
        $this->db->from($this->sys_itemcategory);
        $this->db->join('sys_itemaccount','sys_itemcategory.SysItemCategoryID=sys_itemaccount .SysItemCategoryID','INNER');
        $this->db->where('sys_itemcategory.ItemCategoryID', $item_id);
        $this->db->where('sys_itemaccount.AccountCode', 'TaxablePPH22');
        $this->db->where('sys_itemcategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun txable23 value
    function get_akun_taxable23_value($item_id){
        $company_code = $this->session->userdata('company');

        $this->db->select('sys_itemaccount.AccountNumber');
        $this->db->from($this->sys_itemcategory);
        $this->db->join('sys_itemaccount','sys_itemcategory.SysItemCategoryID=sys_itemaccount .SysItemCategoryID','INNER');
        $this->db->where('sys_itemcategory.ItemCategoryID', $item_id);
        $this->db->where('sys_itemaccount.AccountCode', 'TaxablePPH23');
        $this->db->where('sys_itemcategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun txable25 value
//    function get_akun_taxable25_value($item_id){
//        $company_code = $this->session->userdata('company');
//        $this->db->select('sys_itemaccount.AccountNumber');
//        $this->db->from($this->sys_itemcategory);
//        $this->db->join('sys_itemaccount','sys_itemcategory.SysItemCategoryID=sys_itemaccount .SysItemCategoryID','INNER');
//        $this->db->where('sys_itemcategory.ItemCategoryID', $item_id);
//        $this->db->where('sys_itemaccount.AccountCode', 'TaxablePPH25');
//        $this->db->where('sys_itemcategory.CompanyCode', $company_code);
//        $query = $this->db->get();
//
//        if ($query->num_rows() > 0) {
//            foreach ($query->result() as $row) {
//                $AccountNumber = $row->AccountNumber;
//            }
//
//            return $AccountNumber;
//        }
//        else {
//            return FALSE;
//        }
//    }

    //akun txable pasal 4 ayat 2 value
//    function get_akun_taxable_pasal4_ayat2_value($item_id){
//        $company_code = $this->session->userdata('company');
//        $this->db->select('sys_itemaccount.AccountNumber');
//        $this->db->from($this->sys_itemcategory);
//        $this->db->join('sys_itemaccount','sys_itemcategory.SysItemCategoryID=sys_itemaccount .SysItemCategoryID','INNER');
//        $this->db->where('sys_itemcategory.ItemCategoryID', $item_id);
//        $this->db->where('sys_itemaccount.AccountCode', 'TaxablePasal4Ayat2');
//        $this->db->where('sys_itemcategory.CompanyCode', $company_code);
//        $query = $this->db->get();
//
//        if ($query->num_rows() > 0) {
//            foreach ($query->result() as $row) {
//                $AccountNumber = $row->AccountNumber;
//            }
//
//            return $AccountNumber;
//        }
//        else {
//            return FALSE;
//        }
//    }

    //akun txable pasal 29
//    function get_akun_taxable_pasal29_value($item_id){
//        $company_code = $this->session->userdata('company');
//        $this->db->select('sys_itemaccount.AccountNumber');
//        $this->db->from($this->sys_itemcategory);
//        $this->db->join('sys_itemaccount','sys_itemcategory.SysItemCategoryID=sys_itemaccount .SysItemCategoryID','INNER');
//        $this->db->where('sys_itemcategory.ItemCategoryID', $item_id);
//        $this->db->where('sys_itemaccount.AccountCode', 'TaxablePasal29');
//        $this->db->where('sys_itemcategory.CompanyCode', $company_code);
//        $query = $this->db->get();
//
//        if ($query->num_rows() > 0) {
//            foreach ($query->result() as $row) {
//                $AccountNumber = $row->AccountNumber;
//            }
//
//            return $AccountNumber;
//        }
//        else {
//            return FALSE;
//        }
//    }

    //akun sr value
    function get_akun_sr_value($item_id){
        $company_code = $this->session->userdata('company');
        $this->db->select('sys_itemaccount.AccountNumber');
        $this->db->from($this->sys_itemcategory);
        $this->db->join('sys_itemaccount','sys_itemcategory.SysItemCategoryID=sys_itemaccount .SysItemCategoryID','INNER');
        $this->db->where('sys_itemcategory.ItemCategoryID', $item_id);
        $this->db->where('sys_itemaccount.AccountCode', 'SalesReturn');
        $this->db->where('sys_itemcategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun pr value
    function get_akun_pr_value($item_id){
        $company_code = $this->session->userdata('company');
        $this->db->select('sys_itemaccount.AccountNumber');
        $this->db->from($this->sys_itemcategory);
        $this->db->join('sys_itemaccount','sys_itemcategory.SysItemCategoryID=sys_itemaccount .SysItemCategoryID','INNER');
        $this->db->where('sys_itemcategory.ItemCategoryID', $item_id);
        $this->db->where('sys_itemaccount.AccountCode', 'PurchaseReturn');
        $this->db->where('sys_itemcategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun good intransit value
    function get_akun_gi_value($item_id){
        $company_code = $this->session->userdata('company');
        $this->db->select('sys_itemaccount.AccountNumber');
        $this->db->from($this->sys_itemcategory);
        $this->db->join('sys_itemaccount','sys_itemcategory.SysItemCategoryID=sys_itemaccount .SysItemCategoryID','INNER');
        $this->db->where('sys_itemcategory.ItemCategoryID', $item_id);
        $this->db->where('sys_itemaccount.AccountCode', 'GoodIntransit');
        $this->db->where('sys_itemcategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun expense value
    function get_akun_expense_value($item_id){
         $company_code = $this->session->userdata('company');
        $this->db->select('sys_itemaccount.AccountNumber');
        $this->db->from($this->sys_itemcategory);
        $this->db->join('sys_itemaccount','sys_itemcategory.SysItemCategoryID=sys_itemaccount .SysItemCategoryID','INNER');
        $this->db->where('sys_itemcategory.ItemCategoryID', $item_id);
        $this->db->where('sys_itemaccount.AccountCode', 'Expense');
        $this->db->where('sys_itemcategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun unbilled value
    function get_akun_unbilled_value($item_id){
        $company_code = $this->session->userdata('company');

        $this->db->select('sys_itemaccount.AccountNumber');
        $this->db->from($this->sys_itemcategory);
        $this->db->join('sys_itemaccount','sys_itemcategory.SysItemCategoryID=sys_itemaccount .SysItemCategoryID','INNER');
        $this->db->where('sys_itemcategory.ItemCategoryID', $item_id);
        $this->db->where('sys_itemaccount.AccountCode', 'Unbilled');
        $this->db->where('sys_itemcategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

            return $AccountNumber;
        }
        else {
            return FALSE;
        }
    }
        
    //akun unbilled value
    function get_akun_expense_category($item_id){
        $company_code = $this->session->userdata('company');

        $this->db->select('sys_itemaccount.AccountNumber');
        $this->db->from($this->sys_itemcategory);
        $this->db->join('sys_itemaccount', 'sys_itemcategory.SysItemCategoryID = sys_itemaccount .SysItemCategoryID', 'INNER');
        $this->db->where('sys_itemcategory.ItemCategoryID', $item_id);
        $this->db->where('sys_itemaccount.AccountCode', 'ExpenseCategory');
        $this->db->where('sys_itemcategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

            return $AccountNumber;
        }
        else {
            return FALSE;
        }
    }
//***********************************************************END Function Model Untuk Preference Account Item ************************************/

//***********************************************************Funtion Model Untuk Preference Account Sales*****************************************/
    //sales item catgeory
    function insert_sys_salescategory($data_salescategoryid){
        $this->db->insert('sys_salescategory', $data_salescategoryid);
    }

    function get_sales_categori_id($ItemCategoryID){
        $CompanyCode = $this->session->userdata('company');

        $this->db->select('SysSalesCategoryID');
        $this->db->from('sys_salescategory');
        $this->db->where('ItemCategoryID', $ItemCategoryID);
        $this->db->where('CompanyCode', $CompanyCode);
        $query = $this->db->get();

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

            return $SysSalesCategoryID;
        }
        else {
            return FALSE;
        }
    }

    function cek_kode_on_sales_account($code_akun, $sys_salescategoryid){
        $this->db->select('AccountCode');
        $this->db->from($this->sys_salesaccount);
        $this->db->where('AccountCode', $code_akun);
        $this->db->where('SysSalesCategoryID', $sys_salescategoryid);
        $query = $this->db->get();

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

            return $AccountCode;
        }
        else {
            return FALSE;
        }
    }

    function cek_approval_on_sales_account($jobtitlecode, $syssalescategoryid){
        $this->db->select('AppLevel1Code');
        $this->db->from('sys_salescategory');
        $this->db->where('AppLevel1Code', $jobtitlecode);
        $this->db->where('SysSalesCategoryID', $syssalescategoryid);
        $query = $this->db->get();

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

            return $AppLevel1Code;
        }
        else {
            return FALSE;
        }
    }

    //Sales Account 
    function insert_sysaccount_sales($data_sysaccount_sales){
        $this->db->insert($this->sys_salesaccount, $data_sysaccount_sales);
    }

    function update_sysaccount_sales($data_sysaccount_sales, $sys_salescategoryid, $code){
        $this->db->where('SysSalesCategoryID', $sys_salescategoryid);
        $this->db->where('AccountCode', $code);

        return $this->db->update($this->sys_salesaccount, $data_sysaccount_sales);
    }

    function update_approval_sales($data_sysaccount_sales, $syssalescategoryid){
        $this->db->where('AppLevel1Status', 0);
        $this->db->where('SysSalesCategoryID', $syssalescategoryid);

        return $this->db->update('sys_salescategory', $data_sysaccount_sales);
    }

    //akun sales receivable
    function get_akun_sr_receiveble($item_id){
        $CompanyCode = $this->session->userdata('company');
        
        $this->db->select('sys_salesaccount.AccountNumber');
        $this->db->from('sys_salescategory');
        $this->db->join('sys_salesaccount','sys_salescategory.SysSalesCategoryID=sys_salesaccount.SysSalesCategoryID','INNER');
        $this->db->where('sys_salescategory.ItemCategoryID', $item_id);
        $this->db->where('sys_salesaccount.AccountCode', 'SRAccountReceivable');
        $this->db->where('sys_salescategory.CompanyCode', $CompanyCode);
        $query = $this->db->get();

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

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

    //akun sales advance
    function get_akun_sales_advance($item_id){
        $CompanyCode = $this->session->userdata('company');
        
        $this->db->select('sys_salesaccount.AccountNumber');
        $this->db->from('sys_salescategory');
        $this->db->join('sys_salesaccount','sys_salescategory.SysSalesCategoryID=sys_salesaccount.SysSalesCategoryID','INNER');
        $this->db->where('sys_salescategory.ItemCategoryID', $item_id);
        $this->db->where('sys_salesaccount.AccountCode', 'AdvanceSales');
        $this->db->where('sys_salescategory.CompanyCode', $CompanyCode);
        $query = $this->db->get();

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

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

    //akun sales diskon
    function get_akun_invoice_diskon($item_id){
        $CompanyCode = $this->session->userdata('company');	
        
        $this->db->select('sys_salesaccount.AccountNumber');
        $this->db->from('sys_salescategory');
        $this->db->join('sys_salesaccount','sys_salescategory.SysSalesCategoryID=sys_salesaccount.SysSalesCategoryID','INNER');
        $this->db->where('sys_salescategory.ItemCategoryID', $item_id);
        $this->db->where('sys_salesaccount.AccountCode', 'InvoiceDiscount');
        $this->db->where('sys_salescategory.CompanyCode', $CompanyCode);
        $query = $this->db->get();

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

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

    //akun receive diskon
    function get_akun_receive_diskon($item_id){
        $CompanyCode = $this->session->userdata('company');	

        $this->db->select('sys_salesaccount.AccountNumber');
        $this->db->from('sys_salescategory');
        $this->db->join('sys_salesaccount','sys_salescategory.SysSalesCategoryID=sys_salesaccount.SysSalesCategoryID','INNER');
        $this->db->where('sys_salescategory.ItemCategoryID', $item_id);
        $this->db->where('sys_salesaccount.AccountCode', 'ReceiveDiscount');
        $this->db->where('sys_salescategory.CompanyCode', $CompanyCode);
        $query = $this->db->get();

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

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

    //akun sales taxable
    function get_akun_sales_taxable($item_id){
        $CompanyCode = $this->session->userdata('company');
        
        $this->db->select('sys_salesaccount.AccountNumber');
        $this->db->from('sys_salescategory');
        $this->db->join('sys_salesaccount','sys_salescategory.SysSalesCategoryID=sys_salesaccount.SysSalesCategoryID','INNER');
        $this->db->where('sys_salescategory.ItemCategoryID', $item_id);
        $this->db->where('sys_salesaccount.AccountCode', 'SalesTaxable');
        $this->db->where('sys_salescategory.CompanyCode', $CompanyCode);
        $query = $this->db->get();

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

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

    //akun sales freight
    function get_akun_sales_freight($item_id){
        $CompanyCode = $this->session->userdata('company');	
        
        $this->db->select('sys_salesaccount.AccountNumber');
        $this->db->from('sys_salescategory');
        $this->db->join('sys_salesaccount','sys_salescategory.SysSalesCategoryID = sys_salesaccount.SysSalesCategoryID', 'INNER');
        $this->db->where('sys_salescategory.ItemCategoryID', $item_id);
        $this->db->where('sys_salesaccount.AccountCode', 'SalesFreight');
        $this->db->where('sys_salescategory.CompanyCode', $CompanyCode);
        $query = $this->db->get();

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

            return $AccountNumber;
        }
        else {
            return FALSE;
        }
    }
    
    //akun realized
    function get_akun_realized($item_id){
        $CompanyCode = $this->session->userdata('company');
        
        $this->db->select('sys_salesaccount.AccountNumber');
        $this->db->from('sys_salescategory');
        $this->db->join('sys_salesaccount','sys_salescategory.SysSalesCategoryID=sys_salesaccount.SysSalesCategoryID','INNER');
        $this->db->where('sys_salescategory.ItemCategoryID', $item_id);
        $this->db->where('sys_salesaccount.AccountCode', 'RealizedGainedLoss');
        $this->db->where('sys_salescategory.CompanyCode', $CompanyCode);
        $query = $this->db->get();

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

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

    //akun unrealized
    function get_akun_unrealized($item_id){
        $CompanyCode = $this->session->userdata('company');	

        $this->db->select('sys_salesaccount.AccountNumber');
        $this->db->from('sys_salescategory');
        $this->db->join('sys_salesaccount','sys_salescategory.SysSalesCategoryID=sys_salesaccount.SysSalesCategoryID','INNER');
        $this->db->where('sys_salescategory.ItemCategoryID', $item_id);
        $this->db->where('sys_salesaccount.AccountCode', 'UnRealizedGainedLoss');
        $this->db->where('sys_salescategory.CompanyCode', $CompanyCode);
        $query = $this->db->get();

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

            return $AccountNumber;
        }
        else {
            return FALSE;
        }
    }
/***********************************************************END Function Model Untuk Preference Account Sales************************************/

/***********************************************************Function Model Untuk Preference Account Purchase**************************************/
    function insert_sys_purchasecategory($data_purchasecategoryid){
       $this->db->insert('sys_purchasecategory', $data_purchasecategoryid);
    }

    function get_purchase_categori_id($ItemCategoryID){
        $CompanyCode = $this->session->userdata('company');	

        $this->db->select('SysPurchaseCategoryID');
        $this->db->from('sys_purchasecategory');
        $this->db->where('ItemCategoryID', $ItemCategoryID);
        $this->db->where('CompanyCode', $CompanyCode);
        $query = $this->db->get();

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

            return $SysPurchaseCategoryID;
        }
        else {
            return FALSE;
        }
    }

    function cek_kode_on_purchase_account($code_akun, $syspurchasecategoryid){
        $this->db->select('AccountCode');
        $this->db->from($this->sys_purcahseaccount);
        $this->db->where('AccountCode', $code_akun);
        $this->db->where('SysPurchaseCategoryID', $syspurchasecategoryid);
        $query = $this->db->get();

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

            return $AccountCode;
        }
        else {
            return FALSE;
        }
    }

//    function cek_approval_on_purchase_account($ItemCategoryID){
//        $this->db->select('AppLevel1Status');
//        $this->db->from('sys_purchasecategory');
//        $this->db->where('ItemCategoryID', $ItemCategoryID);
//        $query = $this->db->get();
//
//        if ($query->num_rows() > 0) {
//            foreach ($query->result() as $row) {
//                $AppLevel1Status = $row->AppLevel1Status;
//            }
//
//            return $AppLevel1Status;
//        }
//        else {
//            return FALSE;
//        }
//    }

    function insert_sysaccount_purchase($data_sysaccount_purchase){
        $this->db->insert($this->sys_purcahseaccount, $data_sysaccount_purchase);
    }

    function update_sysaccount_purchase($data_sysaccount_sales, $syspurchasecategoryid, $code){
        $this->db->where('SysPurchaseCategoryID', $syspurchasecategoryid);
        $this->db->where('AccountCode', $code);
        return $this->db->update($this->sys_purcahseaccount, $data_sysaccount_sales);
    }

    function update_approval_purchase($data_sysaccount_purchase, $syspurchasecategoryid){
        $this->db->where('SysPurchaseCategoryID', $syspurchasecategoryid);
        $this->db->where('AppLevel1Status', NULL);
        $this->db->or_where('AppLevel1Status', 0);
        return $this->db->update('sys_purchasecategory', $data_sysaccount_purchase);
    }

    //akun payable
    function get_akun_purchase_payable($item_id){
        $company_code = $this->session->userdata('company');	

        $this->db->select('sys_purchaseaccount.AccountNumber');
        $this->db->from('sys_purchasecategory');
        $this->db->join('sys_purchaseaccount','sys_purchasecategory.SysPurchaseCategoryID=sys_purchaseaccount.SysPurchaseCategoryID','INNER');
        $this->db->where('sys_purchasecategory.ItemCategoryID', $item_id);
        $this->db->where('sys_purchaseaccount.AccountCode', 'Payable');
        $this->db->where('sys_purchasecategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun advance
    function get_akun_purchase_advance($item_id){
        $company_code = $this->session->userdata('company');
        
        $this->db->select('sys_purchaseaccount.AccountNumber');
        $this->db->from('sys_purchasecategory');
        $this->db->join('sys_purchaseaccount','sys_purchasecategory.SysPurchaseCategoryID=sys_purchaseaccount.SysPurchaseCategoryID','INNER');
        $this->db->where('sys_purchasecategory.ItemCategoryID', $item_id);
        $this->db->where('sys_purchaseaccount.AccountCode', 'AdvancePurchase');
        $this->db->where('sys_purchasecategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun discount
    function get_invoice_discount($item_id){
        $company_code = $this->session->userdata('company');
        
        $this->db->select('sys_purchaseaccount.AccountNumber');
        $this->db->from('sys_purchasecategory');
        $this->db->join('sys_purchaseaccount','sys_purchasecategory.SysPurchaseCategoryID=sys_purchaseaccount.SysPurchaseCategoryID','INNER');
        $this->db->where('sys_purchasecategory.ItemCategoryID', $item_id);
        $this->db->where('sys_purchaseaccount.AccountCode', 'InvoiceDiscount');
        $this->db->where('sys_purchasecategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun discount
    function get_akun_payment_discount($item_id){
        $company_code = $this->session->userdata('company');
        
        $this->db->select('sys_purchaseaccount.AccountNumber');
        $this->db->from('sys_purchasecategory');
        $this->db->join('sys_purchaseaccount','sys_purchasecategory.SysPurchaseCategoryID=sys_purchaseaccount.SysPurchaseCategoryID','INNER');
        $this->db->where('sys_purchasecategory.ItemCategoryID', $item_id);
        $this->db->where('sys_purchaseaccount.AccountCode', 'PaymentDiscount');
        $this->db->where('sys_purchasecategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun taxable
    function get_akun_purchase_taxable($item_id){
        $company_code = $this->session->userdata('company');		
        $this->db->select('sys_purchaseaccount.AccountNumber');
        $this->db->from('sys_purchasecategory');
        $this->db->join('sys_purchaseaccount','sys_purchasecategory.SysPurchaseCategoryID=sys_purchaseaccount.SysPurchaseCategoryID','INNER');
        $this->db->where('sys_purchasecategory.ItemCategoryID', $item_id);
        $this->db->where('sys_purchaseaccount.AccountCode', 'PurchaseTaxable');
        $this->db->where('sys_purchasecategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun Freight
    function get_akun_purchase_freight($item_id){
        $company_code = $this->session->userdata('company');
        
        $this->db->select('sys_purchaseaccount.AccountNumber');
        $this->db->from('sys_purchasecategory');
        $this->db->join('sys_purchaseaccount','sys_purchasecategory.SysPurchaseCategoryID=sys_purchaseaccount.SysPurchaseCategoryID','INNER');
        $this->db->where('sys_purchasecategory.ItemCategoryID', $item_id);
        $this->db->where('sys_purchaseaccount.AccountCode', 'PurchaseFreight');
        $this->db->where('sys_purchasecategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

            return $AccountNumber;
        }
        else {
            return FALSE;
        }
    }    
    
    //akun purchase realized
    function get_akun_purchase_realized($item_id){
        $company_code = $this->session->userdata('company');		

        $this->db->select('sys_purchaseaccount.AccountNumber');
        $this->db->from('sys_purchasecategory');
        $this->db->join('sys_purchaseaccount','sys_purchasecategory.SysPurchaseCategoryID=sys_purchaseaccount.SysPurchaseCategoryID','INNER');
        $this->db->where('sys_purchasecategory.ItemCategoryID', $item_id);
        $this->db->where('sys_purchaseaccount.AccountCode', 'RealizedGainedLoss');
        $this->db->where('sys_purchasecategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    //akun purchase unrealized
    function get_akun_purchase_unrealized($item_id){
        $company_code = $this->session->userdata('company');
        
        $this->db->select('sys_purchaseaccount.AccountNumber');
        $this->db->from('sys_purchasecategory');
        $this->db->join('sys_purchaseaccount','sys_purchasecategory.SysPurchaseCategoryID=sys_purchaseaccount.SysPurchaseCategoryID','INNER');
        $this->db->where('sys_purchasecategory.ItemCategoryID', $item_id);
        $this->db->where('sys_purchaseaccount.AccountCode', 'UnRealizedGainedLoss');
        $this->db->where('sys_purchasecategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    /**************************************************************************************/
    function insert_category_expense_account($data_category_expense){
        $this->db->insert('sys_expensecategory', $data_category_expense);
		
		if ($this->db->trans_status() == FALSE) {
            return 0;
        }
        else {
            return 1;
        }  
    }

    function get_all_category_expense_account(){
        $this->db->select('*');
        $this->db->from('sys_expensecategory');
        $this->db->order_by('ExpCategoryID', 'ASC');
        return $this->db->get()->result();
    }

    function cek_kode_on_expense_account($code_akun, $ExpenseCategoryID){
        $this->db->select('AccountCode');
        $this->db->from('sys_expenseaccount');
        $this->db->where('AccountCode', $code_akun);
        $this->db->where('ExpCategoryID	', $ExpenseCategoryID);
        $query = $this->db->get();

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

            return $AccountCode;
        }
        else {
            return FALSE;
        }
    }

    function cek_kode_on_expense_account_detail($itemName, $ExpenseCategoryID){
        $this->db->select('ExpCategoryID');
        $this->db->from($this->sys_expense_detail);
        $this->db->where('ExpCategoryID', $ExpenseCategoryID);
        $this->db->where('ExpDetailName	', $itemName);
        $query = $this->db->get();

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

            return $ExpenseCategoryID;
        }
        else {
            return FALSE;
        }
    }

    function cek_approval_on_expense_account($jobtitlecode, $ExpenseCategoryID){
        $this->db->select('AppLevel1Code');
        $this->db->from('sys_expensecategory');
        $this->db->where('AppLevel1Code', $jobtitlecode);
        $this->db->where('ExpCategoryID', $ExpenseCategoryID);
        $query = $this->db->get();

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

            return $AppLevel1Code;
        }
        else {
            return FALSE;
        }
    }

    function insert_sysexpense_acc_expense($data_expense_acc){
        $this->db->insert('sys_expenseaccount', $data_expense_acc);
    }

    function update_sysexpense_acc_expense($data_expense_acc, $ExpenseCategoryID, $code){
        $this->db->where('ExpCategoryID', $ExpenseCategoryID);
        $this->db->where('AccountCode', $code);

        return $this->db->update('sys_expenseaccount', $data_expense_acc);
    }

    function insert_sysexpense_acc_payable($data_payable_acc){
        $this->db->insert('sys_expenseaccount', $data_payable_acc);
    }

    function update_sysexpense_acc_payable($data_payable_acc, $ExpenseCategoryID){
        $this->db->where('ExpAccountID', $ExpenseCategoryID);

        return $this->db->update('sys_expenseaccount', $data_payable_acc);
    }

    function insert_detail_sysexpense($data_detail_exepense){
        $this->db->insert('sys_expensedetail', $data_detail_exepense);
    }

    function update_detail_sysexpense($data_detail_exepense, $ExpenseCategoryID, $itemName){
        $this->db->where('ExpCategoryID', $ExpenseCategoryID);
        $this->db->where('ExpDetailName', $itemName);

        return $this->db->update('sys_expensedetail', $data_detail_exepense);
    }

    function update_detail_categories($data_detail_exepense, $id){
        $this->db->where('ExpDetailID', $id);

        return $this->db->update('sys_expensedetail', $data_detail_exepense);
    }

    function update_approval_expense_account($data_approval, $ExpenseCategoryID){
        $this->db->where('ExpCategoryID', $ExpenseCategoryID);
        $this->db->where('AppLevel1Status', 0);
        $this->db->or_where('AppLevel1Status', NULL);

        return $this->db->update('sys_expensecategory', $data_approval);
    }

    function delete_detail_categories($id){
        return $this->db->delete('sys_expensedetail', array('ExpDetailID' => $id)); 
    }

    function get_all_list_expense_detail($id){
        $this->db->select('*');
        $this->db->from('sys_expensedetail');
        $this->db->where('ExpCategoryID', $id);
        $result = $this->db->get()->result();

        return json_encode($result);
    }

    function get_akun_expense_acc($cat_id){
        $company_code = $this->session->userdata('company');

        $this->db->select('exp_acc.AccountNumber');
        $this->db->from($this->sys_expense_account . ' exp_acc');
        $this->db->join($this->sys_expense_cataegory . ' exp_cat','exp_cat.ExpCategoryID = exp_acc.ExpCategoryID', 'INNER');
        $this->db->where('exp_acc.ExpCategoryID', $cat_id);
        $this->db->where('exp_acc.AccountCode', 'Expense');
        $this->db->where('exp_cat.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    function get_akun_payable_acc($cat_id, $account_code){
        $company_code = $this->session->userdata('company');

        $this->db->select('exp_acc.AccountNumber');
        $this->db->from($this->sys_expense_account . ' exp_acc');
        $this->db->join($this->sys_expense_cataegory . ' exp_cat', 'exp_cat.ExpCategoryID = exp_acc.ExpCategoryID','INNER');
        $this->db->where('exp_acc.ExpCategoryID', $cat_id);
        $this->db->where('exp_acc.AccountCode', $account_code);
        $this->db->where('exp_cat.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

	function get_akun_payable_acc_new($cat_id, $account_code){
        $company_code = $this->session->userdata('company');

        $this->db->select('ag.AccountName');
        $this->db->from($this->sys_expense_account . ' exp_acc');
        $this->db->join($this->sys_expense_cataegory . ' exp_cat', 'exp_cat.ExpCategoryID = exp_acc.ExpCategoryID','INNER');
		$this->db->join($this->account_gl . ' ag','ag.AccountNumber = exp_acc.AccountNumber', 'INNER');
		
        $this->db->where('exp_acc.ExpCategoryID', $cat_id);
        $this->db->where('exp_acc.AccountCode', $account_code);
        $this->db->where('exp_cat.CompanyCode', $company_code);
        $query = $this->db->get();

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

            return $AccountName;
        }
        else {
            return FALSE;
        }
    }
	
    function get_all_categori_expense($value,$field){
        $company_code = $this->session->userdata('company');
		$this->db->_protect_identifiers = FALSE;
        $this->db->select('*, "0" as ExpCategoryType');
        $this->db->from($this->sys_expense_cataegory);
        $this->db->where('CompanyCode', $company_code);
		$this->db->like($field, $value);
        $this->db->order_by('ExpCategoryID', 'ASC');
        $query = $this->db->get();
		$this->db->_protect_identifiers = TRUE;
        return $query->result();    
	}
	
	function get_all_categori_expense_empty(){
        $company_code = $this->session->userdata('company');
		$this->db->_protect_identifiers = FALSE;
        $this->db->select('*, "0" as ExpCategoryType');
        $this->db->from($this->sys_expense_cataegory);
        $this->db->where('CompanyCode', $company_code);
        $this->db->order_by('ExpCategoryID', 'ASC');
        $query = $this->db->get();
		$this->db->_protect_identifiers = TRUE;
        return $query->result();
    }

    function get_all_categori_expense_payment(){
        $company_code = $this->session->userdata('company');
        
        $this->db->select('*');
        $this->db->from($this->sys_expense_cataegory);
        $this->db->where('CompanyCode', $company_code);
        $this->db->order_by('ExpCategoryID', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    }

    function cek_categori_expense($category_name){
        $company_code = $this->session->userdata('company');
        
        $this->db->select('ExpCategoryName');
        $this->db->from('sys_expensecategory');
        $this->db->where('CompanyCode', $company_code);
        $this->db->where('ExpCategoryName', $category_name);
        $query = $this->db->get();

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

            return $category_name;
        }
        else {
            return FALSE;
        }
    }       
    /**************************************************************************************/

    /*******************************SALES APPROVAL LIMIT**********************************/
    function get_all_list_approval_limit(){
        $this->db->select('*');
        $this->db->from('sys_approvallimit');
        $this->db->order_by('LimitBegin', 'ASC');
        $this->db->order_by('LimitEnd', 'ASC');
        $result = $this->db->get()->result();
        return json_encode($result);
    }
	
    function get_jobtitle_by_limit($over_limit){
//		$this->db->select('JobTitleCode');
//		$this->db->from('sys_approvallimit');
//		$this->db->where('LimitBegin <=',$over_limit);
//		$this->db->where('LimitEnd >=',$over_limit);
//		$this->db->query('
//			SELECT JobTitleCode FROM sys_approvallimit WHERE '. $over_limit .' > LimitBegin AND ('. $over_limit .' <= LimitEnd OR LimitEnd=0)
//		');
        
        $this->db->select('JobTitleCode');
        $this->db->from('sys_approvallimit');
        $this->db->where('LimitBegin <=',$over_limit);
		$this->db->where('(LimitEnd >= '. $over_limit .' OR LimitEnd = 0)');

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

    function insert_approval_limit($data_approval_limit){
            $this->db->insert('sys_approvallimit', $data_approval_limit);
    }

    function update_approval_limit($data_approval_limit, $jobtitle_edit){
            $this->db->where('JobTitleCode', $jobtitle_edit);
            return $this->db->update('sys_approvallimit', $data_approval_limit);
    }

    function delete_approval_limit($id){
            return $this->db->delete('sys_approvallimit', array('JobTitleCode' => $id)); 
    }

    /*********************************BOUNUS QTY LIMIT*****************************/
    function get_all_list_bonus_quantity_limit(){
            $this->db->select('*');
            $this->db->from('sys_bonuslimit');
            $result = $this->db->get()->result();
            return json_encode($result);
    }

    function insert_bonus_limit($data_bonus_limit){
            $this->db->insert('sys_bonuslimit', $data_bonus_limit);
    }

    function update_bonus_limit($data_bonus_limit, $id){
            $this->db->where('BonusLimitID', $id);
            return $this->db->update('sys_bonuslimit', $data_bonus_limit);
    }

    function delete_bonus_limit($id){
            return $this->db->delete('sys_bonuslimit', array('BonusLimitID' => $id)); 
    }

    /*************************************Preference Misclenaouse*****************************************/
    function get_invoice_aging_due_date(){
        $this->db->select('*');
            $this->db->from('sys_preference');
            $this->db->where('PreferenceCode', 'IADueDate');
            return $this->db->get()->result();
    }

    function get_invoice_aging_range(){
        $this->db->select('*');
            $this->db->from('sys_preference');
            $this->db->where('PreferenceName', 'IARange');
            $this->db->where('CompanyCode', $this->session->userdata("company")); 
            return $this->db->get()->result();
    }

    function get_calculate_aging(){
        $this->db->select('*');
            $this->db->from('sys_preference');
            $this->db->where('PreferenceCode', 'CalculateAging');
            return $this->db->get()->result();
    }

    function get_digit_desimal(){
        $this->db->select('*');
            $this->db->from('sys_preference');
            $this->db->where('PreferenceCode', 'DigitDesimal');
            return $this->db->get()->result();
    }

    function get_rounding(){
        $this->db->select('*');
            $this->db->from('sys_preference');
            $this->db->where('PreferenceCode', 'Rounding');
            return $this->db->get()->result();
    }

    function get_tolerance(){
        $this->db->select('*');
            $this->db->from('sys_preference');
            $this->db->where('PreferenceName', 'ToleranceID');
            $this->db->where('CompanyCode', $this->session->userdata("company")); 
            return $this->db->get()->result();
    }

    function get_cogs_method(){
            $this->db->select('*');
            $this->db->from('sys_preference');
            $this->db->where('PreferenceCode', 'COGSMethod');
            return $this->db->get()->result();
    }

    function cek_data_agingDueDate($agingDueDateID){
            $this->db->select('PreferenceCode');
            $this->db->from('sys_preference');
            $this->db->where('PreferenceCode', $agingDueDateID);
            $query = $this->db->get();

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

        return $PreferenceCode;
    }
    else {
        return FALSE;
    }
    }

    function cek_data_agingRange($agingRangeID){
        $this->db->select('PreferenceCode');
        $this->db->from('sys_preference');
        $this->db->where('PreferenceCode', $agingRangeID);
        $query = $this->db->get();

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

            return $PreferenceCode;
        }
        else {
            return FALSE;
        }
    }

    function cek_data_calculate_aging($CalculateAgingID){
            $this->db->select('PreferenceCode');
            $this->db->from('sys_preference');
            $this->db->where('PreferenceCode', $CalculateAgingID);
            $query = $this->db->get();

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

        return $PreferenceCode;
    }
    else {
        return FALSE;
    }
    }

    function cek_data_desimal_digit($DigitDesimalID){
            $this->db->select('PreferenceCode');
            $this->db->from('sys_preference');
            $this->db->where('PreferenceCode', $DigitDesimalID);
            $query = $this->db->get();

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

        return $PreferenceCode;
    }
    else {
        return FALSE;
    }
    }

    function cek_data_tolerance($ToleranceID){
        $this->db->select('PreferenceCode');
        $this->db->from('sys_preference');
        $this->db->where('PreferenceCode', $ToleranceID);
        $query = $this->db->get();

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

    function cek_data_cogs_method($CogsMethodID){
            $this->db->select('PreferenceCode');
            $this->db->from('sys_preference');
            $this->db->where('PreferenceCode', $CogsMethodID);
            $query = $this->db->get();

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

        return $PreferenceCode;
    }
    else {
        return FALSE;
    }
    }

    function insert_aging_due_date($agingDueDateID){	

            $ValueAgingDueDate = $this->input->post('ValueAgingDueDate');

            $arrData = array('PreferenceValue' => $ValueAgingDueDate,
                             'PreferenceCode' => $agingDueDateID,
                                             'LastUser' => $this->session->userdata('username')
                               );

            $this->db->insert('sys_preference', $arrData);		

    }

    function update_aging_due_date($agingDueDateID){	

            $ValueAgingDueDate = $this->input->post('ValueAgingDueDate');

            $arrData = array('PreferenceValue' => $ValueAgingDueDate,
                                             'LastUser' => $this->session->userdata('username')
                               );

            $this->db->where('PreferenceCode', $agingDueDateID);
            $this->db->update('sys_preference', $arrData);		

    }

    function insert_aging_range($agingRangeID){	

        $ValueAgingRange = $this->input->post('ValueAgingRange');

            $arrData = array('PreferenceValue' => $ValueAgingRange,
                             'PreferenceCode' => $agingRangeID,
                                             'LastUser' => $this->session->userdata('username')
                               );

            $this->db->insert('sys_preference', $arrData);		

    }

    function update_aging_range($agingRangeID){	

        $ValueAgingRange = $this->input->post('ValueAgingRange');

            $arrData = array('PreferenceValue' => $ValueAgingRange,
                                             'LastUser' => $this->session->userdata('username')
                               );

            $this->db->where('PreferenceCode', $agingRangeID);
            $this->db->update('sys_preference', $arrData);		

    }

    function insert_calculate_aging($CalculateAgingID){	

            $CalAging_rdo = $this->input->post('CalAging_rdo');

            $arrData = array('PreferenceValue' => $CalAging_rdo,
                             'PreferenceCode' => $CalculateAgingID,
                                             'LastUser' => $this->session->userdata('username')
                               );

            $this->db->insert('sys_preference', $arrData);		

    }

    function update_calculate_aging($CalculateAgingID){	

            $CalAging_rdo = $this->input->post('CalAging_rdo');

            $arrData = array('PreferenceValue' => $CalAging_rdo,
                                             'LastUser' => $this->session->userdata('username')
                               );

            $this->db->where('PreferenceCode', $CalculateAgingID);
            $this->db->update('sys_preference', $arrData);		

    }

    function insert_digit_desimal($DigitDesimalID){	

            $select_digit_desimal = $this->input->post('select_digit_desimal');

            $arrData = array('PreferenceValue' => $select_digit_desimal,
                             'PreferenceCode' => $DigitDesimalID,
                                             'LastUser' => $this->session->userdata('username')
                               );

            $this->db->insert('sys_preference', $arrData);		

    }

    function update_digit_desimal($DigitDesimalID){	

            $select_digit_desimal = $this->input->post('select_digit_desimal');

            $arrData = array('PreferenceValue' => $select_digit_desimal,
                                             'LastUser' => $this->session->userdata('username')
                               );

            $this->db->where('PreferenceCode', $DigitDesimalID);
            $this->db->update('sys_preference', $arrData);		

    }

    function insert_rounding($RoundingID){	

            $select_rounding = $this->input->post('select_rounding');

            $arrData = array('PreferenceValue' => $select_rounding,
                             'PreferenceCode' => $RoundingID,
                                             'LastUser' => $this->session->userdata('username')
                               );

            $this->db->insert('sys_preference', $arrData);		

    }

    function update_rounding($RoundingID){	

            $select_rounding = $this->input->post('select_rounding');

            $arrData = array('PreferenceValue' => $select_rounding,
                                             'LastUser' => $this->session->userdata('username')
                               );

            $this->db->where('PreferenceCode', $RoundingID);
            $this->db->update('sys_preference', $arrData);		

    }

    function insert_tolerance($ToleranceID){	

            $select_days = $this->input->post('select_days');

            $arrData = array('PreferenceValue' => $select_days,
                             'PreferenceCode' => $ToleranceID,
                                             'LastUser' => $this->session->userdata('username')
                               );

            $this->db->insert('sys_preference', $arrData);		

    }

    function update_tolerance($ToleranceID){	

            $select_days = $this->input->post('select_days');

            $arrData = array('PreferenceValue' => $select_days,
                                             'LastUser' => $this->session->userdata('username')
                               );

            $this->db->where('PreferenceCode', $ToleranceID);
            $this->db->update('sys_preference', $arrData);		

    }

    function insert_cogs_method($CogsMethodID){	

            $select_cogs_method = $this->input->post('select_cogs_method');

            $arrData = array('PreferenceValue' => $select_cogs_method,
                             'PreferenceCode' => $CogsMethodID,
                                             'LastUser' => $this->session->userdata('username')
                               );

            $this->db->insert('sys_preference', $arrData);		

    }

    function update_cogs_method($CogsMethodID){	

            $select_cogs_method = $this->input->post('select_cogs_method');

            $arrData = array('PreferenceValue' => $select_cogs_method,
                                             'LastUser' => $this->session->userdata('username')
                               );

            $this->db->where('PreferenceCode', $CogsMethodID);
            $this->db->update('sys_preference', $arrData);		

    }

    function get_value_by_code($code) {
        $this->db->select('PreferenceValue');
        $this->db->from('sys_preference');
        $this->db->where('PreferenceCode', $code); 
        $query = $this->db->get();    

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

            return $PreferenceValue;
        }
        else {
            return FALSE;
        }  	
    }

    /************************CEK KATEGORI DI EXPENSE AKUN***********************************/	
//    function get_item_category_for_expense($AccountNumber){
//        $this->db->select('ItemCategory');
//        $this->db->from('am_accountgl');
//        $this->db->where('AccountNumber',$AccountNumber);
//        $query = $this->db->get();
//
//        if($query->num_rows() > 0){
//            foreach($query->result() as $row){
//                $ItemCategory = $row->ItemCategory;
//            }
//
//            return $ItemCategory;
//        }
//        else{
//            return FALSE;
//        }
//    }
	
//    function update_expense_item_category($AccountNumber,$ItemCategory){
//        $this->db->where('AccountNumber',$AccountNumber);
//        $this->db->update('am_accountgl',array('ItemCategory'=>$ItemCategory));
//    }

    /************************CEK KATEGORI DI OTHER AKUN***********************************/
    function cek_categori_afiliasi($categoriID){
        $CompanyCode = $this->session->userdata('company');	

        $this->db->select('OtherCategoryID');
        $this->db->from('sys_othercategory');
        $this->db->where('OtherCategoryID', $categoriID);
        $this->db->where('CompanyCode', $CompanyCode);
        $query = $this->db->get();

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

            return $OtherCategoryID;
        }
        else {
            return FALSE;
        }
    }

    function cek_categori_asset($categoriID){
        $CompanyCode = $this->session->userdata('company');	

        $this->db->select('OtherCategoryID');
        $this->db->from('sys_othercategory');
        $this->db->where('OtherCategoryID', $categoriID);
        $this->db->where('CompanyCode', $CompanyCode);
        $query = $this->db->get();

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

            return $OtherCategoryID;
        }
        else {
            return FALSE;
        }
    }

    function get_sysothercategory_id($CompanyCode, $categoriID){
        $this->db->select('SysOtherCategoryID');
        $this->db->from('sys_othercategory');
        $this->db->where('OtherCategoryID', $categoriID);
        $this->db->where('CompanyCode', $CompanyCode);
        $query = $this->db->get();

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

            return $SysOtherCategoryID;
        }
        else {
            return FALSE;
        }
    }

    function get_sysothercategory_id2($CompanyCode, $categoriID2){
        $this->db->select('SysOtherCategoryID');
        $this->db->from('sys_othercategory');
        $this->db->where('OtherCategoryID', $categoriID2);
        $this->db->where('CompanyCode', $CompanyCode);
        $query = $this->db->get();

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

            return $SysOtherCategoryID;
        }
        else {
            return FALSE;
        }
    }

    function insert_category_afiliasi($data_category){
        $this->db->insert('sys_othercategory', $data_category);
    }

    function insert_category_asset($data_category){
        $this->db->insert('sys_othercategory', $data_category);
    }

    function cek_kode_on_payment_afiliasi($code_payment, $ExpenseCategoryID){
        $this->db->select('AccountCode');
        $this->db->from('sys_otheraccount');
        $this->db->where('AccountCode', $code_payment);
        $this->db->where('SysOtherCategoryID', $ExpenseCategoryID);
        $query = $this->db->get();

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

            return $AccountCode;
        }
        else {
            return FALSE;
        }
    }

    function cek_kode_on_fix_asset($code_asset1, $ExpenseCategoryID){
        $this->db->select('AccountCode');
        $this->db->from('sys_otheraccount');
        $this->db->where('AccountCode', $code_asset1);
        $this->db->where('SysOtherCategoryID', $ExpenseCategoryID);
        $query = $this->db->get();

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

            return $AccountCode;
        }
        else {
            return FALSE;
        }
    }

    function insert_sysother_payment_account($data_payment){
        $this->db->insert('sys_otheraccount', $data_payment);
    }

    function insert_sysother_asset1_account($data_asset){
        $this->db->insert('sys_otheraccount', $data_asset);
    }

    function update_sysother_payment_account($data_payment, $ExpenseCategoryID, $code_payment){
        $this->db->where('SysOtherCategoryID', $ExpenseCategoryID);
        $this->db->where('AccountCode', $code_payment);
        
//        return $this->db->update('sys_otheraccount', $data_payment);
        $this->db->update('sys_otheraccount', $data_payment);
    }

    function update_sysother_asset1_account($data_asset, $ExpenseCategoryID, $code_asset1){
        $this->db->where('SysOtherCategoryID', $ExpenseCategoryID);
        $this->db->where('AccountCode', $code_asset1);

        return $this->db->update('sys_otheraccount', $data_asset);
    }


    function cek_kode_on_receipt_afiliasi($code_receipt, $ExpenseCategoryID){
        $this->db->select('AccountCode');
        $this->db->from('sys_otheraccount');
        $this->db->where('AccountCode', $code_receipt);
        $this->db->where('SysOtherCategoryID', $ExpenseCategoryID);
        $query = $this->db->get();

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

            return $AccountCode;
        }
        else {
            return FALSE;
        }
    }

    function cek_kode_on_fix_asset2($code_asset2, $ExpenseCategoryID){
        $this->db->select('AccountCode');
        $this->db->from('sys_otheraccount');
        $this->db->where('AccountCode', $code_asset2);
        $this->db->where('SysOtherCategoryID', $ExpenseCategoryID);
        $query = $this->db->get();

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

            return $AccountCode;
        }
        else {
            return FALSE;
        }
    }

    function insert_sysother_receipt_account($data_receipt){
        $this->db->insert('sys_otheraccount', $data_receipt);
    }

    function insert_sysother_asset2_account($data_asset2){
        $this->db->insert('sys_otheraccount', $data_asset2);
    }

    function update_sysother_receipt_account($data_receipt, $ExpenseCategoryID, $code_receipt){
        $this->db->where('SysOtherCategoryID', $ExpenseCategoryID);
        $this->db->where('AccountCode', $code_receipt);

        return $this->db->update('sys_otheraccount', $data_receipt);
    }

    function update_sysother_asset2_account($data_asset2, $ExpenseCategoryID, $code_asset2){
        $this->db->where('SysOtherCategoryID', $ExpenseCategoryID);
        $this->db->where('AccountCode', $code_asset2);

        return $this->db->update('sys_otheraccount', $data_asset2);
    }

    function cek_approval_on_sysother_account($jobtitlecode, $ExpenseCategoryID){
        $this->db->select('AppLevel1Code');
        $this->db->from('sys_othercategory');
        $this->db->where('AppLevel1Code', $jobtitlecode);
        $this->db->where('OtherCategoryID', $ExpenseCategoryID);
        $query = $this->db->get();

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

            return $AppLevel1Code;
        }
        else {
            return FALSE;
        }
    }

    function update_approval_sysother_account($data_approval, $ExpenseCategoryID){
        $this->db->where('OtherCategoryID', $ExpenseCategoryID);
        $this->db->where('AppLevel1Status', 0);
        return $this->db->update('sys_othercategory', $data_approval);
    }

    function get_akun_payment_other($ExpenseCategoryID){
        $company_code = $this->session->userdata('company');
        $this->db->select('sys_otheraccount.AccountNumber');
        $this->db->from('sys_otheraccount');
        $this->db->join('sys_othercategory','sys_otheraccount.SysOtherCategoryID=sys_othercategory.SysOtherCategoryID','INNER');
        $this->db->where('sys_otheraccount.SysOtherCategoryID', $ExpenseCategoryID);
        $this->db->where('sys_otheraccount.AccountCode', 'PaymentAfiliasi');
        $this->db->where('sys_othercategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    function get_akun_asset1_other($ExpenseCategoryID){
        $company_code = $this->session->userdata('company');
        $this->db->select('sys_otheraccount.AccountNumber');
        $this->db->from('sys_otheraccount');
        $this->db->join('sys_othercategory','sys_otheraccount.SysOtherCategoryID=sys_othercategory.SysOtherCategoryID','INNER');
        $this->db->where('sys_otheraccount.SysOtherCategoryID', $ExpenseCategoryID);
        $this->db->where('sys_otheraccount.AccountCode', 'Asset1');
        $this->db->where('sys_othercategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    function get_akun_receipt_other($ExpenseCategoryID){
        $company_code = $this->session->userdata('company');
        
        $this->db->select('sys_otheraccount.AccountNumber');
        $this->db->from('sys_otheraccount');
        $this->db->join('sys_othercategory','sys_otheraccount.SysOtherCategoryID=sys_othercategory.SysOtherCategoryID','INNER');
        $this->db->where('sys_otheraccount.SysOtherCategoryID', $ExpenseCategoryID);
        $this->db->where('sys_otheraccount.AccountCode', 'ReceiptAfiliasi');
        $this->db->where('sys_othercategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    function get_akun_asset2_other($ExpenseCategoryID){
        $company_code = $this->session->userdata('company');
        $this->db->select('sys_otheraccount.AccountNumber');
        $this->db->from('sys_otheraccount');
        $this->db->join('sys_othercategory','sys_otheraccount.SysOtherCategoryID=sys_othercategory.SysOtherCategoryID','INNER');
        $this->db->where('sys_otheraccount.SysOtherCategoryID', $ExpenseCategoryID);
        $this->db->where('sys_otheraccount.AccountCode', 'Asset2');
        $this->db->where('sys_othercategory.CompanyCode', $company_code);
        $query = $this->db->get();

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

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

    function get_cek_app_item_default($item_id){
        $company_code = $this->session->userdata('company');
        
        $this->db->select('AppLevel1Status');
        $this->db->from('sys_itemcategory');
        $this->db->where('ItemCategoryID', $item_id);
        $this->db->where('CompanyCode', $company_code);
        $query = $this->db->get();

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

            return $AppLevel1Status;
        }
        else {
            return FALSE;
        }
    }

    function get_cek_app_sales($item_id){
        $company_code = $this->session->userdata('company');

        $this->db->select('AppLevel1Status');
        $this->db->from('sys_salescategory');
        $this->db->where('ItemCategoryID', $item_id);
        $this->db->where('CompanyCode', $company_code);
        $query = $this->db->get();

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

            return $AppLevel1Status;
        }
        else {
            return FALSE;
        }
    }

    function get_cek_app_purchase($item_id){
        $company_code = $this->session->userdata('company');

        $this->db->select('AppLevel1Status');
        $this->db->from('sys_purchasecategory');
        $this->db->where('ItemCategoryID', $item_id);
        $this->db->where('CompanyCode', $company_code);
        $query = $this->db->get();

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

            return $AppLevel1Status;
        }
        else {
            return FALSE;
        }
    }

    function get_cek_app_expense($item_id){
        $company_code = $this->session->userdata('company');
        
        $this->db->select('AppLevel1Status');
        $this->db->from($this->sys_expense_cataegory);
        $this->db->where('ExpCategoryID', $item_id);
        $this->db->where('CompanyCode', $company_code);
        $query = $this->db->get();

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

            return $AppLevel1Status;
        }
        else {
            return FALSE;
        }
    }
    
    
/* Created by : Ferry / 31-03-2015 */
//***********************************************Function Model Untuk COA Account*******************************************//
    function get_coa_account($account_sub_type, $in_array){
        $company_code   = $this->session->userdata('company');
        $branch_code    = $this->session->userdata('branch');
        
        $this->db->select('cg.AccountNumber, ag.AccountName');
        $this->db->from($this->company_gl . ' cg');
        $this->db->join($this->account_gl . ' ag', 'ag.AccountNumber = cg.AccountNumber','INNER');
		$this->db->join($this->account_subtype . ' st', 'st.AccountSubTypeID = ag.AccountSubTypeID','INNER');
		
        $this->db->where('cg.CompanyCode', $company_code);
        $this->db->where('cg.BranchCode', $branch_code);

        if($in_array == 0){
            $this->db->where('ag.AccountSubTypeID', $account_sub_type);            
        }
        elseif($in_array == 1) {
            $this->db->where_in('ag.AccountSubTypeID', $account_sub_type);
        }
        elseif($in_array == 2){
            $this->db->like('ag.AccountName', $account_sub_type);
        }
		elseif($in_array == 3) {
            $this->db->where_in('st.AccountTypeName', $account_sub_type);
        }
        else{
            $this->db->where_in('ag.AccountName', $account_sub_type);
        }
        
        $this->db->where('ag.CurrencyCode', 'IDR');
        $this->db->where('ag.AccountChild', 1);
        $this->db->where('cg.TransStatus', 2);
		$this->db->where('cg.GLSaldoYears', date('Y'));
		
        $this->db->order_by('ag.AccountName', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    }
//***************************END Function Model Untuk Preference Service/Expense Account************************************//
    
//*****************************Function Model Untuk Preference Service/Expense Account**************************************//
    function get_service_category($value, $field){
        $this->db->_protect_identifiers = FALSE;
		$company_code   = $this->session->userdata('company');
		 
        $this->db->select("*, '0' as SvcCategoryType");
        $this->db->from($this->sys_service_category);
        $this->db->order_by('SvcCategoryName', 'ASC');
		$this->db->where('CompanyCode', $company_code);
		$this->db->like($field, $value);
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
                
        return $query->result();
    }
	
	function get_service_category_empty(){
        $this->db->_protect_identifiers = FALSE;
		$company_code   = $this->session->userdata('company');
		 
        $this->db->select("*, '0' as SvcCategoryType");
        $this->db->from($this->sys_service_category);
        $this->db->order_by('SvcCategoryName', 'ASC');
		$this->db->where('CompanyCode', $company_code);
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
                
        return $query->result();
    }

    function get_service_account($category_id, $account_code){
        $company_code   = $this->session->userdata('company');

        $this->db->select('sa.AccountNumber, ag.AccountName');
        $this->db->from($this->sys_service_account . ' as sa');
        $this->db->join($this->sys_service_category . ' as sc', 'sc.SvcCategoryID = sa.SvcCategoryID', 'INNER');
        $this->db->join($this->account_gl . ' as ag', 'ag.AccountNumber = sa.AccountNumber', 'INNER');
        $this->db->where('sc.CompanyCode', $company_code);
        $this->db->where('sa.SvcCategoryID', $category_id);
        $this->db->where('sa.AccountCode', $account_code);
        $query = $this->db->get();

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

//            return $AccountNumber;
            return $query->row();
        }
        else {
            return FALSE;
        }
    }

    function get_service_detail($value,$field){
        $this->db->_protect_identifiers = FALSE;
        $this->db->select("sys_servicedetail.*, (case when SvcPPh23Status = 0 then 'No' else 'Yes' end) as SvcDetailTax, '0' as SvcDetailType, sc.SvcCategoryName");
        $this->db->from($this->sys_service_detail);
		$this->db->join($this->sys_service_category . ' as sc', 'sc.SvcCategoryID = sys_servicedetail.SvcCategoryID', 'INNER');
		 
        $this->db->like($field, $value);
        $this->db->order_by('SvcCategoryName', 'ASC');
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
                
        return $query->result();
    }
    
	function get_service_detail_empty(){
        $this->db->_protect_identifiers = FALSE;
        $this->db->select("sys_servicedetail.*, (case when SvcPPh23Status = 0 then 'No' else 'Yes' end) as SvcDetailTax, '0' as SvcDetailType,sc.SvcCategoryName");
        $this->db->from($this->sys_service_detail);
		$this->db->join($this->sys_service_category . ' as sc', 'sc.SvcCategoryID = sys_servicedetail.SvcCategoryID', 'INNER');
        $this->db->order_by('SvcCategoryName', 'ASC');
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
                
        return $query->result();
    }
	
    function cek_service_category($check_id, $check_type){
        $company_code   = $this->session->userdata('company');

        $this->db->select('SvcCategoryID');
        $this->db->from($this->sys_service_category);
        $this->db->where('CompanyCode', $company_code);

        if($check_type == 0){
            $this->db->where('SvcCategoryID', $check_id);
        }
        else{
            $this->db->where('SvcCategoryName', $check_id);            
        }
        
        $query = $this->db->get();

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

            return $svc_category_id;
        }
        else {
            return FALSE;
        }
    }
    
    function cek_service_detail_by_id($svc_category_id, $check_id){
        $this->db->select('SvcDetailID');
        $this->db->from($this->sys_service_detail);
        $this->db->where('SvcCategoryID', $svc_category_id);
        $this->db->where('SvcDetailID', $check_id);        
        $query = $this->db->get();

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

            return $svc_detail_id;
        }
        else {
            return FALSE;
        }
    }

    function cek_service_detail_by_name($svc_category_id, $check_name){
        $this->db->select('SvcDetailName');
        $this->db->from($this->sys_service_detail);
        $this->db->where('SvcCategoryID', $svc_category_id);
        $this->db->like('SvcDetailName', $check_name);            
        $query = $this->db->get();

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

            return $svc_detail_name;
        }
        else {
            return FALSE;
        }
    }
    
    function cek_service_account($account_code, $svc_category_id){
        $this->db->select('SvcAccountID');
        $this->db->from($this->sys_service_account);
        $this->db->where('SvcCategoryID', $svc_category_id);
        $this->db->where('AccountCode', $account_code);
        $query = $this->db->get();

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

            return $svc_account_id;
        }
        else {
            return FALSE;
        }
    }
    
    function insert_service_category($data_category){
        $this->db->insert($this->sys_service_category, $data_category);
    }
    
    function update_service_category($data_category, $svc_category_id){
        $this->db->where('SvcCategoryID', $svc_category_id);
        return $this->db->update($this->sys_service_category, $data_category);
    }

    function insert_service_detail($data_detail){
        $this->db->insert($this->sys_service_detail, $data_detail);
    }

    function insert_service_account($data_account){
        $this->db->insert($this->sys_service_account, $data_account);
    }
    
    function update_service_detail($data_detail, $svc_detail_id){
        $this->db->where('SvcDetailID', $svc_detail_id);
        return $this->db->update($this->sys_service_detail, $data_detail);
    }

    function update_service_account($data_account, $svc_account_id){
        $this->db->where('SvcAccountID', $svc_account_id);
        return $this->db->update($this->sys_service_account, $data_account);
    }

    function delete_service_category($id){
        return $this->db->delete($this->sys_service_category, array('SvcCategoryID' => $id));
    }
    
    function delete_service_detail($id){
        return $this->db->delete($this->sys_service_detail, array('SvcDetailID' => $id));
    }
//*****************************END Function Model Untuk Preference Service/Expense Account**********************************//

//*****************************Function Model Untuk Preference Advance Payment Account**************************************//
    function get_advance_category($value,$field){
        $this->db->_protect_identifiers = FALSE;
		$company_code   = $this->session->userdata('company');
        $this->db->select("*, '0' as AdvCategoryType");
        $this->db->from($this->sys_advance_category);
		$this->db->like($field, $value);
		$this->db->where('CompanyCode', $company_code);
		
        $this->db->order_by('AdvCategoryName', 'ASC');
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
                
        return $query->result();
    }
	
	function get_advance_category_empty(){
        $this->db->_protect_identifiers = FALSE;
		$company_code   = $this->session->userdata('company');
        $this->db->select("*, '0' as AdvCategoryType");
        $this->db->from($this->sys_advance_category);
		$this->db->where('CompanyCode', $company_code);
		
        $this->db->order_by('AdvCategoryName', 'ASC');
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
                
        return $query->result();
    }
	
    function get_advance_detail($value, $field){
        $this->db->_protect_identifiers = FALSE;
        $this->db->select("sys_advancedetail.*, concat((AdvNetDueIn div 12) , ' years ' , (AdvNetDueIn mod 12) , ' months') as AdvLenghtOfDue, '0' as AdvDetailType, sys_advancecategory.AdvCategoryName");
        $this->db->from($this->sys_advance_detail);
		$this->db->join('sys_advancecategory', 'sys_advancecategory.AdvCategoryID = sys_advancedetail.AdvCategoryID');
		 
        $this->db->like($field, $value);
        $this->db->order_by('AdvCategoryName', 'ASC');
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
                
        return $query->result();
    }
    
	function get_advance_detail_empty(){
        $this->db->_protect_identifiers = FALSE;
        $this->db->select("*, concat((AdvNetDueIn div 12) , ' years ' , (AdvNetDueIn mod 12) , ' months') as AdvLenghtOfDue, '0' as AdvDetailType,sys_advancecategory.AdvCategoryName");
        $this->db->from($this->sys_advance_detail);
		$this->db->join('sys_advancecategory', 'sys_advancecategory.AdvCategoryID = sys_advancedetail.AdvCategoryID');
        $this->db->order_by('AdvCategoryName', 'ASC');
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
                
        return $query->result();
    }
	
    function get_advance_account($category_id, $account_code){
        $company_code   = $this->session->userdata('company');

        $this->db->select('aa.AccountNumber, ag.AccountName');
        $this->db->from($this->sys_advance_account . ' as aa');
        $this->db->join($this->sys_advance_category . ' as ac', 'ac.AdvCategoryID = aa.AdvCategoryID', 'INNER');
        $this->db->join($this->account_gl . ' as ag', 'ag.AccountNumber = aa.AccountNumber', 'INNER');
        $this->db->where('ac.CompanyCode', $company_code);
        $this->db->where('aa.AdvCategoryID', $category_id);
        $this->db->where('aa.AccountCode', $account_code);
        $query = $this->db->get();

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

    function cek_advance_account($account_code, $adv_category_id){
        $this->db->select('AdvAccountID');
        $this->db->from($this->sys_advance_account);
        $this->db->where('AdvCategoryID', $adv_category_id);
        $this->db->where('AccountCode', $account_code);
        $query = $this->db->get();

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

            return $adv_account_id;
        }
        else {
            return FALSE;
        }
    }
    
    function insert_advance_category($data_category){
        $this->db->insert($this->sys_advance_category, $data_category);
    }
    
    function update_advance_category($data_category, $svc_category_id){
        $this->db->where('AdvCategoryID', $svc_category_id);
        $this->db->update($this->sys_advance_category, $data_category);
//        return $this->db->update($this->sys_advance_category, $data_category);
    }

    function delete_advance_category($id){
        return $this->db->delete($this->sys_advance_category, array('AdvCategoryID' => $id));
    }
    
    function insert_advance_detail($data_detail){
        $this->db->insert($this->sys_advance_detail, $data_detail);
    }

    function update_advance_detail($data_detail, $svc_detail_id){
        $this->db->where('AdvDetailID', $svc_detail_id);
        $this->db->update($this->sys_advance_detail, $data_detail);
//        return $this->db->update($this->sys_advance_detail, $data_detail);
    }

    function delete_advance_detail($id){
        return $this->db->delete($this->sys_advance_detail, array('AdvDetailID' => $id));
    }

    function insert_advance_account($data_account){
        $this->db->insert($this->sys_advance_account, $data_account);
    }
    
    function update_advance_account($data_account, $svc_account_id){
        $this->db->where('AdvAccountID', $svc_account_id);
        $this->db->update($this->sys_advance_account, $data_account);
//        return $this->db->update($this->sys_advance_account, $data_account);
    }
//*****************************END Function Model Untuk Preference Advance Payment Account**********************************//
	
	//crated by fachri 29/07/2015
	//function update prefrence expense group casbon
	function update_expense_category($data_category, $exp_category_id){
        $this->db->where('ExpCategoryID', $exp_category_id);
        return $this->db->update($this->sys_expense_cataegory, $data_category);
    }
	
	function delete_expen_category($id){
        return $this->db->delete($this->sys_expense_cataegory, array('ExpCategoryID' => $id));
    }
	
	function get_expense_detail($value,$field){
        $this->db->_protect_identifiers = FALSE;
        $this->db->select("sys_expensedetail.*,'0' as ExpDetailType, ac.ExpCategoryName");
        $this->db->from($this->sys_expense_detail);
		$this->db->join($this->sys_expense_cataegory . ' as ac', 'ac.ExpCategoryID = sys_expensedetail.ExpCategoryID', 'INNER');
		
        $this->db->like($field, $value);
        $this->db->order_by('ExpCategoryName', 'ASC');
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
                
        return $query->result();
    }
	
	function get_expense_detail_empty(){
        $this->db->_protect_identifiers = FALSE;
        $this->db->select("sys_expensedetail.*,'0' as ExpDetailType,ac.ExpCategoryName");
        $this->db->from($this->sys_expense_detail);
      	$this->db->join($this->sys_expense_cataegory . ' as ac', 'ac.ExpCategoryID = sys_expensedetail.ExpCategoryID', 'INNER');
		
        $this->db->order_by('ExpCategoryName', 'ASC');
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
                
        return $query->result();
    }
	
	function get_expense_account($category_id, $account_code){
        $company_code   = $this->session->userdata('company');

        $this->db->select('aa.AccountNumber, ag.AccountName');
        $this->db->from($this->sys_expense_account . ' as aa');
        $this->db->join($this->sys_expense_cataegory . ' as ac', 'ac.ExpCategoryID = aa.ExpCategoryID', 'INNER');
        $this->db->join($this->account_gl . ' as ag', 'ag.AccountNumber = aa.AccountNumber', 'INNER');
        $this->db->where('ac.CompanyCode', $company_code);
        $this->db->where('aa.ExpCategoryID', $category_id);
        $this->db->where('aa.AccountCode', $account_code);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->row();
        }
        else {
            return FALSE;
        }
    }
	
	function update_expense_detail_category($data_category, $detail_id){
        $this->db->where('ExpDetailID', $detail_id);
        return $this->db->update($this->sys_expense_detail, $data_category);
    }
	
	function delete_expense_detail($id){
        return $this->db->delete($this->sys_expense_detail, array('ExpDetailID' => $id));
    }
	
	function cek_expense_account($account_code, $exp_category_id){
        $this->db->select('ExpAccountID');
        $this->db->from($this->sys_expense_account);
        $this->db->where('ExpCategoryID', $exp_category_id);
        $this->db->where('AccountCode', $account_code);
        $query = $this->db->get();

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

            return $exp_account_id;
        }
        else {
            return FALSE;
        }
    }
	//end created
	
	/*=======================lemparan dari item categories pas di suspend=========================================*/
	function get_sys_item_categories_suspen($item_categori){
		$this->db->select('sys_itemcategory.ItemCategoryID,sys_itemcategory.SysItemCategoryID');
        $this->db->from('sys_itemcategory');
        $this->db->join('sys_itemaccount', 'sys_itemaccount.SysItemCategoryID = sys_itemcategory.SysItemCategoryID');
        $this->db->where('sys_itemcategory.ItemCategoryID', $item_categori);

        return $this->db->get()->result();
	}
	
	function get_sys_sales_categories_suspen($item_categori){
		$this->db->select('sys_salescategory.ItemCategoryID,sys_salescategory.SysSalesCategoryID');
        $this->db->from('sys_salescategory');
        $this->db->join('sys_salesaccount', 'sys_salesaccount.SysSalesCategoryID = sys_salescategory.SysSalesCategoryID');
        $this->db->where('sys_salescategory.ItemCategoryID', $item_categori);

        return $this->db->get()->result();
	}
	
	function get_sys_purchases_categories_suspen($item_categori){
		$this->db->select('sys_purchasecategory.ItemCategoryID,sys_purchasecategory.SysPurchaseCategoryID');
        $this->db->from('sys_purchasecategory');
        $this->db->join('sys_purchaseaccount', 'sys_purchaseaccount.SysPurchaseCategoryID = sys_purchasecategory.SysPurchaseCategoryID');
        $this->db->where('sys_purchasecategory.ItemCategoryID', $item_categori);

        return $this->db->get()->result();
	}
	/*=======================end===================================================================================*/
	
	/*====================delete permanen sys item dari suspen item kategori==============================================*/
	function delete_sys_item($id, $SysItemCategoryID){
         $this->db->delete($this->sys_itemaccount, array('SysItemCategoryID' => $SysItemCategoryID));
		$this->db->delete($this->sys_itemcategory, array('ItemCategoryID' => $id));
       
//		
//        if ($this->db->trans_status() == FALSE) {//dah ga pakai ini karena dah pakai array
//            return 0;
//        }
//        else {
//            return 1;
//        }
    }
	
	function delete_sys_sales($id, $SysSalesCategoryID){
         $this->db->delete($this->sys_salesaccount, array('SysSalesCategoryID' => $SysSalesCategoryID));
		$this->db->delete($this->sys_sales_kategori, array('ItemCategoryID' => $id));
    }
	
	function delete_sys_purchase($id, $SysPurchaseCategoryID){
         $this->db->delete($this->sys_purcahseaccount, array('SysPurchaseCategoryID' => $SysPurchaseCategoryID));
		$this->db->delete($this->sys_purchase_kategori, array('ItemCategoryID' => $id));
    }
	
	function get_account_cas_bon($account_name){
        $company_code   = $this->session->userdata('company');
        $branch_code    = $this->session->userdata('branch');
        
        $this->db->select('cg.AccountNumber, ag.AccountName');
        $this->db->from($this->company_gl . ' as cg');
        $this->db->join($this->account_gl . ' as ag', 'ag.AccountNumber = cg.AccountNumber', 'INNER');
        $this->db->join($this->branch . ' as br', 'br.BranchCode = cg.BranchCode', 'INNER');
        $this->db->where('cg.CompanyCode', $company_code);
        $this->db->where('cg.BranchCode', $branch_code);
        $this->db->where('(case when br.BranchStatus = 0 then cg.PLStatus in (0, 1) else cg.PLStatus = 1 end)');            
        $this->db->where('cg.GLSaldoYears', date('Y'));                
        $this->db->like('ag.AccountName', $account_name);
        
        $query_account = $this->db->get();
        
        if ($query_account->num_rows() > 0) {
            $data = $query_account->row();
            return $data;
        }
        else {
            return false;
        }			
    }
    
    //======= DHIDHIK, 16 MARET 2016, array in model system =======//
    function check_num_sys_preference($prefName){           
        $query = $this->db->get_where('sys_preference', array('sys_preference.PreferenceName' => $prefName,'sys_preference.CompanyCode' => $this->session->userdata("company")));		
        if ($query->num_rows() > 0) {
            return TRUE;
        }else {
            return FALSE;
        }	
    }
    function update_sys_preference($PreferenceName,$PreferenceValue){        
        $company = $this->session->userdata("company");
        $arrData = array(
            'PreferenceValue'   => $PreferenceValue,
            'LastUser'          => $this->session->userdata('username')
        );
        $this->db->where('PreferenceName', $PreferenceName);
        $this->db->where('CompanyCode', $company);
        $this->db->update('sys_preference', $arrData);
    }
    function insert_sys_preference($PreferenceName,$PreferenceValue){    
        $arrData = array(
            'CompanyCode'       => $this->session->userdata("company"),
            'PreferenceName'    => $PreferenceName,
            'PreferenceValue'   => $PreferenceValue,
            'LastUser'          => $this->session->userdata('username')
        );
        $this->db->insert('sys_preference', $arrData);
    }
    
    function data_send_preference_item($dataSend){                 
        $this->db->trans_start();
          
        foreach($dataSend['data_sysitemcategory'] as $key){
            $this->insert_sys_itemcategory($key);
        }
        $sysitemcategoryid = $this->m_preference->get_sysitem_categori_id($dataSend['ItemCategoryID'] );
        foreach($dataSend['data_sysitemaccount_insert'] as $key){
            $key['SysItemCategoryID']   = $sysitemcategoryid;
            $this->insert_sysitemaccount($key);
        }        
        foreach($dataSend['data_sales_akun_update'] as $key){
            $data_sales_akun_update = array(
                'AccountCode'       => $key['AccountCode'],
                'AccountNumber'     => $key['AccountNumber'], 
                'LastUser'          => $key['LastUser']
            );
            $this->update_sysitemaccount($data_sales_akun_update, $sysitemcategoryid, $key['code_sales_akun']);
        }
        foreach($dataSend['data_cogs_akun_update'] as $key){
            $data_cogs_akun_update = array(
                'AccountCode'       => $key['AccountCode'],
                'AccountNumber'     => $key['AccountNumber'],
                'LastUser'          => $key['LastUser']
            );
            $this->update_sysitemaccount($data_cogs_akun_update, $key['sysitemcategoryid'], $key['code_cogs_akun']);
        }  
        foreach($dataSend['data_inventory_update'] as $key){
            $data_inventory_update = array(
                'SysItemCategoryID' => $key['SysItemCategoryID'], 
                'AccountCode'       => $key['AccountCode'],
                'AccountNumber'     => $key['AccountNumber'],
                'LastUser'          => $key['LastUser']
            );
            $this->update_sysitemaccount($data_inventory_update, $sysitemcategoryid, $key['code_inventory_akun']);
        }   
        foreach($dataSend['data_coa_update'] as $key){
            $data_coa_update = array(
                'ItemCategory'      => $key['ItemCategory'],
                'LastUser'          => $key['LastUser']
            );
            $this->m_coa->update($data_coa_update, $key['cmb_akun_inventory']);
        }    
        foreach($dataSend['data_taxable22_update'] as $key){
            $data_taxable22_update = array(
                'AccountCode'       => $key['AccountCode'],
                'AccountNumber'     => $key['AccountNumber'],
                'LastUser'          => $key['LastUser']               
            );
            $this->update_sysitemaccount($data_taxable22_update, $sysitemcategoryid, $key['code_taxable22_akun']);
        }  
        foreach($dataSend['data_sr_update'] as $key){
            $data_sr_update = array(
                'AccountCode'       => $key['AccountCode'],
                'AccountNumber'     => $key['AccountNumber'],
                'LastUser'          => $key['LastUser']
            );
            $this->update_sysitemaccount($data_sr_update, $sysitemcategoryid, $key['code_sr_akun']);
        }  
        foreach($dataSend['data_pr_update'] as $key){
            $data_pr_update = array(
                'AccountCode'       => $key['AccountCode'],
                'AccountNumber'     => $key['AccountNumber'],
                'LastUser'          => $key['LastUser']
            );
            $this->update_sysitemaccount($data_pr_update, $sysitemcategoryid, $key['code_pr_akun']);
        }  
        foreach($dataSend['data_unbilled_update'] as $key){
            $data_unbilled_update = array(
                'AccountCode'       => $key['AccountCode'],
                'AccountNumber'     => $key['AccountNumber'],
                'LastUser'          => $key['LastUser']
            );
            $this->update_sysitemaccount($data_unbilled_update, $sysitemcategoryid, $key['code_unbilled_akun']);
        }  
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    function data_send_preference_sales($dataSend){                
        $this->db->trans_start();
        
        foreach($dataSend['data_salescategoryid'] as $key){
            $this->insert_sys_salescategory($key);
        }
        $syssalescategoryid = $this->get_sales_categori_id($dataSend['ItemCategoryID']); // masuk ke model JUGA, klo ga,ntar ga dapet syssalescategoryid nya
        
        foreach($dataSend['data_sysaccount_sales_insert'] as $key){
            $key['SysSalesCategoryID'] = $syssalescategoryid;
            $this->insert_sysaccount_sales($key);
        }
        foreach($dataSend['data_sales_account_receivable_update'] as $key){
            $data_sales_account_receivable_update = array(
                'AccountCode'           => $key['AccountCode'],
                'AccountNumber'         => $key['AccountNumber'],
                'LastUser'              => $key['LastUser']
            );
            $this->update_sysaccount_sales($data_sales_account_receivable_update, $syssalescategoryid, $key['code_sr_receiveble_akun']);
        }
        foreach($dataSend['data_advance_sales_update'] as $key){
            $data_advance_sales_update = array(
                'AccountCode'           => $key['AccountCode'],
                'AccountNumber'         => $key['AccountNumber'],
                'LastUser'              => $key['LastUser']
            );
            $this->update_sysaccount_sales($data_advance_sales_update, $syssalescategoryid, $key['code_advance_sales_akun']);
        }
        foreach($dataSend['data_sales_diskon_update'] as $key){
            $data_sales_diskon_update = array(
                'AccountCode' => $key['AccountCode'],
                'AccountNumber' => $key['AccountNumber'],
                'LastUser' => $key['LastUser'] 
            );
            $this->update_sysaccount_sales($data_sales_diskon_update, $syssalescategoryid, $key['code_invoice_discount_akun'] );
        }
        foreach($dataSend['data_receive_diskon_update'] as $key){
            $data_receive_diskon_update = array(
                'AccountCode' => $key['AccountCode'],
                'AccountNumber' => $key['AccountNumber'],
                'LastUser' => $key['LastUser']
            );
            $this->update_sysaccount_sales($data_receive_diskon_update, $syssalescategoryid, $key['code_receive_discount_akun']);
        }
        foreach($dataSend['data_sales_taxable_update'] as $key){
            $data_sales_taxable_update = array(
                'AccountCode'           => $key['AccountCode'],
                'AccountNumber'         => $key['AccountNumber'],
                'LastUser'              => $key['LastUser']
            );
            $this->update_sysaccount_sales($data_sales_taxable_update, $syssalescategoryid, $key['code_sales_taxable_akun']);
        }
        foreach($dataSend['data_sales_freight_update'] as $key){
            $data_sales_freight_update = array(
                'AccountCode'           => $key['AccountCode'],
                'AccountNumber'         => $key['AccountNumber'],
                'LastUser'              => $key['LastUser']
            );
            $this->update_sysaccount_sales($data_sales_freight_update, $syssalescategoryid, $key['code_sales_freight_akun']);
        }
        foreach($dataSend['data_realized_gained_update'] as $key){
            $data_realized_gained_update = array(
                'AccountCode'           => $key['AccountCode'],
                'AccountNumber'         => $key['AccountNumber'],
                'LastUser'              => $key['LastUser']
            );
            $this->update_sysaccount_sales($data_realized_gained_update, $syssalescategoryid, $key['code_realized_akun']);
        }
        foreach($dataSend['data_unrealized_gained_update'] as $key){
            $data_unrealized_gained_update = array(
                'AccountCode'           => $key['AccountCode'],
                'AccountNumber'         => $key['AccountNumber'],
                'LastUser'              => $key['LastUser']
            );
            $this->update_sysaccount_sales($data_unrealized_gained_update, $syssalescategoryid, $key['code_unrealized_akun']);//masuk ke model
        }
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    function data_send_preference_purchase($dataSend){              
        $this->db->trans_start();
        
        foreach($dataSend['data_purchasecategoryid'] as $key){
            $this->insert_sys_purchasecategory($key);
        }
        $syspurchasecategoryid = $this->get_purchase_categori_id($dataSend['ItemCategoryID']);
        
        foreach($dataSend['data_sysaccount_purchase_insert'] as $key){
            $key['SysPurchaseCategoryID'] = $syspurchasecategoryid;
            $this->insert_sysaccount_purchase($key);
        }
        foreach($dataSend['data_purchase_account_payable_update'] as $key){
            $data_purchase_account_payable_update = array(
                'AccountCode'               => $key['AccountCode'],
                'AccountNumber'             => $key['AccountNumber'],
                'LastUser'                  => $key['LastUser']
            );
            $this->update_sysaccount_purchase($data_purchase_account_payable_update, $syspurchasecategoryid, $key['code_payable_akun']);
        }
        foreach($dataSend['data_advance_purchase_account_update'] as $key){
            $data_advance_purchase_account_update = array(
                'AccountCode'               => $key['AccountCode'],
                'AccountNumber'             => $key['AccountNumber'],
                'LastUser'                  => $key['LastUser']
            );
            $this->update_sysaccount_purchase($data_advance_purchase_account_update, $syspurchasecategoryid, $key['code_advance_purchase_akun']);
        }
        foreach($dataSend['data_discount_invoice_account_update'] as $key){
            $data_discount_invoice_account_update = array(
                'AccountCode'               => $key['AccountCode'],
                'AccountNumber'             => $key['AccountNumber'],
                'LastUser'                  => $key['LastUser']
            );
            $this->update_sysaccount_purchase($data_discount_invoice_account_update, $syspurchasecategoryid, $key['code_discount_invoice_akun']);
        }
        foreach($dataSend['data_discount_payment_account_update'] as $key){
            $data_discount_payment_account_update = array(
                'AccountCode'               => $key['AccountCode'],
                'AccountNumber'             => $key['AccountNumber'],
                'LastUser'                  => $key['LastUser']
            );
            $this->update_sysaccount_purchase($data_discount_payment_account_update, $syspurchasecategoryid, $key['code_discount_payment_akun']);
        }
        foreach($dataSend['data_taxable_purchase_account_update'] as $key){
            $data_taxable_purchase_account_update = array(
                'AccountCode'               => $key['AccountCode'],
                'AccountNumber'             => $key['AccountNumber'],
                'LastUser'                  => $key['LastUser']
            );
            $this->update_sysaccount_purchase($data_taxable_purchase_account_update, $syspurchasecategoryid, $key['code_taxable_purchase_akun']);
        }
        foreach($dataSend['data_purchase_freight_update'] as $key){
            $data_purchase_freight_update = array(
                'AccountCode'               => $key['AccountCode'],
                'AccountNumber'             => $key['AccountNumber'],
                'LastUser'                  => $key['LastUser']
            );
            $this->update_sysaccount_purchase($data_purchase_freight_update, $syspurchasecategoryid, $key['code_purchase_freight_akun']); 
        }
        foreach($dataSend['data_realized_gained_account_update'] as $key){
            $data_realized_gained_account_update = array(
                'AccountCode'               => $key['AccountCode'],
                'AccountNumber'             => $key['AccountNumber'],
                'LastUser'                  => $key['LastUser']
            );
            $this->update_sysaccount_purchase($data_realized_gained_account_update, $syspurchasecategoryid, $key['code_realized_purchase_akun']); 
        }
        foreach($dataSend['data_unrealized_gained_account_update'] as $key){
            $data_unrealized_gained_account_update = array(
                'AccountCode'               => $key['AccountCode'],
                'AccountNumber'             => $key['AccountNumber'],
                'LastUser'                  => $key['LastUser']
            );
            $this->update_sysaccount_purchase($data_unrealized_gained_account_update, $syspurchasecategoryid, $key['code_unrealized_purchase_akun']);
        }
                
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    function data_send_preference_service_category($dataSend){        
        $this->db->trans_start();
        
        foreach($dataSend['data_category_insert'] as $key){
            $this->insert_service_category($key);
        }
        foreach($dataSend['data_category_update'] as $key){
            $data_category_update = array(
                'SvcCategoryName'   => $key['SvcCategoryName'], 
                'SvcCategoryDesc'   => $key['SvcCategoryDesc'], 
                'LastUser'          => $key['LastUser']
            ); 
           $this->update_service_category($data_category_update, $key['category_id']);
        }
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    function data_send_preference_service_detail($dataSend){        
        $this->db->trans_start();
        
        foreach($dataSend['data_detail_insert'] as $key){
            $this->insert_service_detail($key);
        }
        foreach($dataSend['data_detail_update'] as $key){
            $data_detail_update = array(
                'SvcCategoryID'     => $key['SvcCategoryID'], 
				'SvcDetailName'     => $key['SvcDetailName'], 
                'SvcDetailDesc'     => $key['SvcDetailDesc'],
                'SvcDetailUnit'     => $key['SvcDetailUnit'],
                'SvcPPh23Status'    => $key['SvcPPh23Status'],
                'LastUser'          => $key['LastUser']
            ); 
           $this->update_service_detail($data_detail_update, $key['detail_id']);
        }
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    function data_send_preference_service_account($dataSend){     
        $this->db->trans_start();
        
        foreach($dataSend['data_account_insert'] as $key){
            $this->insert_service_account($key);
        }
        foreach($dataSend['data_account_update'] as $key){
            $data_account_update = array(
                'SvcCategoryID'     => $key['SvcCategoryID'], 
                'AccountCode'       => $key['AccountCode'],
                'AccountNumber'     => $key['AccountNumber'],
                'LastUser'          => $key['LastUser']
            );
            $this->update_service_account($data_account_update, $key['idcode']);
        }
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    } 
    function data_send_preference_service_category_delete($dataSend){        
        $this->db->trans_start();
        
        $this->delete_service_category($dataSend['svc_category_id']);
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    function data_send_preference_service_detail_delete($dataSend){        
        $this->db->trans_start();
        
        $this->delete_service_detail($dataSend['svc_detail_id']);
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    function data_send_preference_advances_category($dataSend){        
        $this->db->trans_start();
        
        foreach($dataSend['data_category_insert'] as $key){
            $this->insert_advance_category($key);
        }
        foreach($dataSend['data_category_update'] as $key){
            $data_category_update = array(
                'AdvCategoryName'   => $key['AdvCategoryName'], 
                'AdvCategoryDesc'   => $key['AdvCategoryDesc'], 
                'LastUser'          => $key['LastUser']
            ); 
            $this->update_advance_category($data_category_update, $key['category_id']);
        }
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }    
    function data_send_preference_advances_detail($dataSend){       
        $this->db->trans_start();
        
        foreach($dataSend['data_account_insert'] as $key){
            $this->insert_advance_account($key);
        }
        foreach($dataSend['data_account_update'] as $key){
            $data_account_update = array( 
                'AdvCategoryID'     => $key['AdvCategoryID'], 
                'AccountCode'       => $key['AccountCode'],
                'AccountNumber'     => $key['AccountNumber'],
                'LastUser'          => $key['LastUser'],
            ); 
            $this->update_advance_account($data_account_update, $key['idcode']);
        }
        foreach($dataSend['data_detail_insert'] as $key){
            $this->insert_advance_detail($key);
        }
        foreach($dataSend['data_detail_update'] as $key){
            $data_detail_update = array( 
                'AdvDetailName'     => $key['AdvDetailName'], 
                'AdvDetailDesc'     => $key['AdvDetailDesc'],
                'AdvNetDueIn'       => $key['AdvNetDueIn'],
                'LastUser'          => $key['LastUser']
            ); 
            $this->update_advance_detail($data_detail_update, $key['idcode']);
        }
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
	function data_advances_detail($dataSend){       
        $this->db->trans_start();
        
        foreach($dataSend['data_detail_insert'] as $key){
            $this->insert_advance_detail($key);
        }
        foreach($dataSend['data_detail_update'] as $key){
            $data_detail_update = array( 
                'AdvDetailName'     => $key['AdvDetailName'], 
                'AdvDetailDesc'     => $key['AdvDetailDesc'],
				'AdvCategoryID'     => $key['AdvCategoryID'], 
                'AdvNetDueIn'       => $key['AdvNetDueIn'],
                'LastUser'          => $key['LastUser']
            ); 
            $this->update_advance_detail($data_detail_update, $key['idcode']);
        }
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    function data_send_preference_advances_category_delete($dataSend){        
        $this->db->trans_start();
        
        $this->delete_advance_category($dataSend['adv_category_id']);
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    function data_send_preference_advances_detail_delete($dataSend){        
        $this->db->trans_start();
        
        $this->delete_advance_detail($dataSend['adv_detail_id']);
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    function data_send_preference_expense_category($dataSend){   
        $this->db->trans_start();
        
        foreach($dataSend['data_category_expense_insert'] as $key){
            $this->insert_category_expense_account($key);
        }
        foreach($dataSend['data_category_expense_update'] as $key){
            $data_category_expense_update = array(
                'CompanyCode'       => $key['CompanyCode'], 
                'ExpCategoryName'   => $key['ExpCategoryName'],
                'ExpCategoryDesc'   => $key['ExpCategoryDesc'], 
                'LastUser'          => $key['LastUser']
            ); 
           $this->update_expense_category($data_category_expense_update, $key['idcode']);
        }
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    function data_preference_expense_detail($dataSend){
        $this->db->trans_start();
        
        foreach($dataSend['data_detail_insert'] as $key){
            $this->insert_detail_sysexpense($key);
        }
        foreach($dataSend['data_detail_update'] as $key){
            $data_detail_update = array(
                'ExpDetailName'     => $key['ExpDetailName'], 
                'ExpDetailDesc'     => $key['ExpDetailDesc'],
                'ExpCategoryID'     => $key['ExpCategoryID'],
                'LastUser'          => $key['LastUser']
            ); 
           $this->update_expense_detail_category($data_detail_update, $key['idcode']);
        }
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
	function data_send_preference_expense_detail($dataSend){
        $this->db->trans_start();
        
        foreach($dataSend['data_account_insert'] as $key){
            $this->insert_sysexpense_acc_payable($key);
        }
        foreach($dataSend['data_account_update'] as $key){
            $data_detail_update = array(
                'ExpCategoryID'     => $key['ExpCategoryID'], 
                'AccountCode'       => $key['AccountCode'],
                'AccountNumber'     => $key['AccountNumber'],
                'LastUser'          => $key['LastUser']
            ); 
           $this->update_sysexpense_acc_payable($data_detail_update, $key['idcode']);
        }
        foreach($dataSend['data_detail_insert'] as $key){
            $this->insert_detail_sysexpense($key);
        }
        foreach($dataSend['data_detail_update'] as $key){
            $data_detail_update = array(
                'ExpDetailName'     => $key['ExpDetailName'], 
                'ExpDetailDesc'     => $key['ExpDetailDesc'],
                'ExpCategoryID'     => $key['ExpCategoryID'],
                'LastUser'          => $key['LastUser']
            ); 
           $this->update_expense_detail_category($data_detail_update, $key['idcode']);
        }
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    function data_send_preference_expense_category_delete($dataSend){        
        $this->db->trans_start();
        
        $this->delete_expen_category($dataSend['exp_category_id']);
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    function data_send_preference_expense_detail_delete($dataSend){        
        $this->db->trans_start();
        
        $this->delete_expense_detail($dataSend['exp_detail_id']);
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    function data_send_preference_other_afiliasi($dataSend){      
        $this->db->trans_start();
                
        foreach($dataSend['data_category_insert'] as $key){
            $this->insert_category_afiliasi($key);
        }
        $ExpenseCategoryID = $this->get_sysothercategory_id($dataSend['CompanyCode'], $dataSend['categoriID']);
        
        foreach($dataSend['data_payment_insert'] as $key){
            $key['SysOtherCategoryID']  = $ExpenseCategoryID;
            $this->insert_sysother_payment_account($key);
        }
        foreach($dataSend['data_payment_update'] as $key){            
            $data_payment_update  = array(
                'SysOtherCategoryID' => $ExpenseCategoryID, 
                'AccountNumber' => $key['AccountNumber'],
                'AccountCode' => $key['AccountCode'],				 
                'LastUser' => $key['LastUser']
            );
            $this->update_sysother_payment_account($data_payment_update, $ExpenseCategoryID, $key['idcode']);
        }
        foreach($dataSend['data_receipt_insert'] as $key){
            $key['SysOtherCategoryID']  = $ExpenseCategoryID;
            $this->insert_sysother_receipt_account($key);
        }
        foreach($dataSend['data_receipt_update'] as $key){            
            $data_receipt_update  = array(
                'SysOtherCategoryID' => $ExpenseCategoryID, 
                'AccountNumber' => $key['AccountNumber'],
                'AccountCode' => $key['AccountCode'],				 
                'LastUser' => $key['LastUser']
            );
            $this->update_sysother_receipt_account($data_receipt_update, $ExpenseCategoryID, $key['idcode']);
        }
        
        //$cek_approval = $this->cek_approval_on_sysother_account($jobtitlecode, $ExpenseCategoryID);
        $date_approval = date('Y-m-d'); 

        if($dataSend['jobtitlecode'] == 'MGR'){
            $data_approval = array(
                'AppLevel1Status' => 1,
                'AppLevel1Code' => $dataSend['jobtitlecode'],
                'AppLevel1Date' => $date_approval
            );
            $this->update_approval_sysother_account($data_approval, $ExpenseCategoryID);
        }
                    
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result['status']  = false;
        }else{
            $result['akun_payment_other']   = $this->get_akun_payment_other($ExpenseCategoryID);
            $result['akun_receipt_other']   = $this->get_akun_receipt_other($ExpenseCategoryID);
            $result['akun_fix_asset1']      = $this->get_akun_asset1_other($ExpenseCategoryID);
            $result['akun_fix_asset2']      = $this->get_akun_asset2_other($ExpenseCategoryID);
            $result['status']  = true;
        }
        return $result;
    }
    function data_send_preference_approval_insert($dataSend){      
        $this->db->trans_start();
                
        foreach($dataSend['data_approval_limit'] as $key){
            $this->insert_approval_limit($key);
        }
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result = true;
        }
        return $result;
    }
    function data_send_preference_approval_update($dataSend){
        $this->db->trans_start();
        
        $data_approval_limit = array(
            'LimitBegin'    => $dataSend['data_approval_limit']['LimitBegin'],
            'LimitEnd'      => $dataSend['data_approval_limit']['LimitEnd']
        );
        $this->update_approval_limit($data_approval_limit, $dataSend['data_approval_limit']['jobtitle_edit']);
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result = true;
        }
        return $result;
    }
    function data_send_preference_approval_delete($dataSend){
        $this->db->trans_start();
        $this->delete_approval_limit($dataSend['idcode']);
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result = true;
        }
        return $result;
    }
    function data_send_preference_approval_insert_update($dataSend){            
        $this->db->trans_start();       
        
        if($dataSend['cek_agingDueDateExist']){
            $this->update_sys_preference("IARange",$dataSend['ValueAgingRange']);
        } else {
            $this->insert_sys_preference("IARange",$dataSend['ValueAgingRange']);
        }

        if($dataSend['cek_ToleranceExist']){
            $this->update_sys_preference("ToleranceID",$dataSend['ValueTolerance']);
        } else {
            $this->insert_sys_preference("ToleranceID",$dataSend['ValueTolerance']);                
        }
            
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    
	function get_item_categories_sum($trans_status){
		$CompanyCode = $this->session->userdata('company');
        
        $this->db->select('count(ItemCategoryID) as ItemCategoryID');
        $this->db->from('am_itemcategory');
        $this->db->where('TransStatus', $trans_status);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->row();
        }
        else {
            return FALSE;
        }
	}
	
    //======= END DHIDHIK, 16 MARET 2016, array in model system =======//
    
	/*========================================end=======================================================================*/
}