<?php
class M_custom extends CI_Model {
    public $department 			= 'am_department';
    public $jobtitle 			= 'am_jobtitle';
    public $city 				= 'am_city';
    public $state 				= 'am_state';
    public $country 			= 'am_country';
    public $termspayment 		= 'am_termspayment';
    public $currency 			= 'am_currency';
    public $taxes 				= 'am_taxes';
    public $taxtype 			= 'am_taxtype';
    public $region 				= 'am_region';
    public $item 				= 'am_item';
    public $inventory 			= 'at_inventories';
    public $itemtype 			= 'am_itemtype';
    public $itemcategory 		= 'am_itemcategory';
    public $itemprice 			= 'am_itemprice';
    public $itempricelist 		= 'am_itemprice';
    public $vendor 				= 'am_vendor';
    public $shipment 			= 'am_shipment';
    public $branch 				= 'am_branch';
    public $warehouse 			= 'am_warehouse';	
    public $bank 				= 'am_bank';	
    public $accountgl 			= 'am_accountgl';	
    public $compaccountgl       = 'at_compaccountgl';   
    public $company 			= 'am_company';	
    public $customer 			= 'am_customer';
    public $employee 			= 'am_employee';	
    public $warehousetype 		= 'am_warehousetype';	
    public $fixedassettype 		= 'am_fixedassettype';	
    public $depreciatemethod 	= 'am_depreciatemethod';	
    public $accountsubtype      = 'am_accountsubtype';
    public $accounttype         = 'am_accounttype';
    public $purchaserequisition = 'at_purchaserequisition';
    public $userlog 			= 'tbl_userlog';
    public $servicedetail 		= 'sys_servicedetail';	
    public $advancedetail 		= 'sys_advancedetail';
	public $journalvoucher 		= 'at_journalvoucher';
	public $jv_detail 			= 'at_jvchdetail';
	public $jv_journal 			= 'at_jvchjournal';
	public $bank_book 			= 'at_bankbook';
    public $av_transaction      = 'av_all_transaction';	
    public $av_transstatus      = 'av_all_transstatus';
	public $av_item_already     = 'av_item_already';
    public $av_approval_listing = 'av_approval_listing';
    public $av_check_list_app   = 'av_check_list_app';  
	public $period   			= 'at_periodend';
    public $hlogin              = 'ah_loginhistory';    
    
	public function __construct() {
        parent::__construct();
        $this->load->model('activities/m_journalvoucher');
		$this->load->model('list/m_currency');
		$this->load->model('list/m_coa');	      		
    }
	
	//        PHP-CI NOT SUPPORT CLAUSE WHERE NOT EXISTS - DO NOT BE DELETE        
	//        $this->db->join('am_city', 'am_city.CityCode = ' . $this->bank . '.BankCity', 'INNER' );
	//
	//        if ($this->session->userdata('jobtitle') != 'DIR') {  
	//            $this->db->where('CompanyCode', $this->session->userdata('company'));
	//    
	//            if ($this->session->userdata('branch_status') == 1) {
	//                $this->db->where('BranchCode', $this->session->userdata('branch'));
	//            }
	//        }
	//        
	//        $this->db->where(' NOT EXISTS (Select Null From am_accountgl Where am_accountgl.BankID = am_bank.BankID)');
	//        $this->db->order_by('BankName', 'ASC');
	//        $query = $this->db->get($this->bank);
	//        return $query->result();
	
    function get_city(){        
        $this->db->select('am_city.StateCode, CityCode, CityName, CityDesc, StateName');
        $this->db->from($this->city);
        $this->db->join('am_state', 'am_state.StateCode = am_city.StateCode', 'INNER');
        $this->db->order_by('CityName', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    }

    function get_state(){
        $this->db->select('am_state.CountryCode, StateCode, StateName, StateDesc, CountryName');
        $this->db->from($this->state);
        $this->db->join('am_country', 'am_country.CountryCode = am_state.CountryCode', 'INNER');
        $this->db->order_by('StateCode', 'ASC');
        $query = $this->db->get();

        return $query->result();
    }
    
    function get_country(){
        $query = $this->db->order_by('CountryCode', 'ASC')->get($this->country);
        return $query->result();
    }
    
    function get_department(){
        $this->db->select('*');
        $this->db->from($this->department);
        $this->db->where('CompanyCode', $this->session->userdata('company'));
        $this->db->where_in('TransStatus', array(0, 1));
        $query = $this->db->get();

        return $query->result();
    }
	
    function get_department_by_name($DepartmentName){
        $this->db->select('*');
        $this->db->from($this->department);
        $this->db->where('CompanyCode', $this->session->userdata('company'));
        $this->db->where_in('TransStatus', array(0, 1));
        $this->db->where('DepartmentName',$DepartmentName);
        $query = $this->db->get();
		
        return $query->result();
    }

    function get_jobtitle(){
        $this->db->select("JobTitleCode, JobTitleName, JobTitleHead, JobTitleLevel, Description, TransStatus, 
                          (case when JobTitleStatus = 0 then 'Director'
                                when JobTitleStatus = 1 then 'Managerial'
                                when JobTitleStatus = 2 then 'Supervisor'
                                when JobTitleStatus = 3 then 'Staff'
                                when JobTitleStatus = 4 then 'administrator' end) as JobTitleStatus");
        $this->db->from($this->jobtitle);
        $this->db->where('TransStatus', 1);
        $this->db->order_by('JobTitleLevel', 'ASC');
        $this->db->order_by('JobTitleStatus', 'ASC');
        $query = $this->db->get();

        return $query->result();
    }
	
    function get_jobtitlestatus(){
        $this->db->select("JobTitleStatus, 
                          (case when JobTitleStatus = 0 then 'Director'
                                when JobTitleStatus = 1 then 'Managerial'
                                when JobTitleStatus = 2 then 'Supervisor'
                                when JobTitleStatus = 3 then 'Staff'
                                when JobTitleStatus = 4 then 'administrator' end) as JobTitleStatusName");
        $this->db->from($this->jobtitle);
        $this->db->where('TransStatus', 1);
        $this->db->order_by('JobTitleStatusName', 'ASC');
        $query = $this->db->get();

        return $query->result();
    }
	
    function get_termspayment(){
        $query = $this->db->order_by('NetDueIn', 'ASC')->get($this->termspayment);
        return $query->result();
    }
    
    function get_currency(){
        $this->db->select('*');
        $this->db->from($this->currency . ' as cy');
        $this->db->join($this->country . ' as ct', 'ct.CountryCode = cy.CurrencyCountry', 'INNER');
        $this->db->where('cy.TransStatus', 1);
		$this->db->where('cy.CompanyCode', $this->session->userdata('company'));
        $this->db->order_by('CurrencyCode', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    }

    function get_taxes(){
        $query = $this->db->where('TransStatus', 1)->order_by('TaxesName', 'ASC')->get($this->taxes);
        return $query->result();
    }

    function get_taxtype(){
        $query = $this->db->order_by('TaxTypeName', 'ASC')->get($this->taxtype);
        return $query->result();
    }

    function get_region(){
        $query = $this->db->where('TransStatus', 1)->order_by('RegionName', 'ASC')->get($this->region);
        return $query->result();
    }
	
    function get_item(){
        $query = $this->db->where('TransStatus', 0)->order_by('ItemCode', 'ASC')->get($this->item);
        return $query->result();
    }
	
    function get_inventory(){
        $this->db->select('at_inventories.ItemCode, at_inventories.ItemQty, am_item.ItemDesc, am_item.ItemUnit, am_itemprice.ItemPrice, am_item.ItemCategoryID');
        $this->db->from($this->inventory);
        $this->db->join('am_item', 'am_item.ItemCode = at_inventories.ItemCode', 'inner');
        $this->db->join('am_itemprice', 'am_itemprice.ItemCode = at_inventories.ItemCode', 'inner');
        $this->db->where('at_inventories.BranchCode',$this->session->userdata('branch'));
        $this->db->order_by('at_inventories.ItemCode', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    }
	
	/*============================get item kategori===============================================================*/
    function get_itemcategory(){
        $this->db->select('*');
        $this->db->from($this->itemcategory);
		
		$this->db->where_in('TransStatus', array(0,1));
		
        $this->db->order_by('ItemCategoryName', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    }
	
	function get_itemcategory_for_add(){
        $this->db->select('*');
        $this->db->from($this->itemcategory);
		
		$this->db->where_in('TransStatus', array(0,1));
		
        $this->db->order_by('ItemCategoryName', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    }
	/*=============================end================================================================================================*/
	
	/*============================get item kategori no fix asset status===============================================================*/
	function get_itemcategory_no_asset(){
        $this->db->select('*');
        $this->db->from($this->itemcategory);
		
		$this->db->where_in('TransStatus', array(0,1));
		$this->db->where('AssetStatus', 0);
		
        $this->db->order_by('ItemCategoryName', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    }
	/*=============================end================================================================================================*/
	
	/*============================get item kategori adjust===============================================================*/
	function get_itemcategory_adjust($replace_number){
        $this->db->select('*');
        $this->db->from($this->itemcategory);
		$this->db->join('sys_itemcategory', 'sys_itemcategory.ItemCategoryID = am_itemcategory.ItemCategoryID', 'inner');
		$this->db->join('sys_itemaccount', 'sys_itemaccount.SysItemCategoryID = sys_itemcategory.SysItemCategoryID', 'inner');
		
		$this->db->where_in('TransStatus', array(0,1));
		$this->db->where('AssetStatus', 0);
		
		$this->db->where_in('sys_itemaccount.AccountNumber', $replace_number);
		$this->db->where('sys_itemcategory.CompanyCode', $this->session->userdata('company'));
		
        $this->db->order_by('ItemCategoryName', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    }
	/*=============================end================================================================================================*/
	
    function get_itemtype(){
        $query = $this->db->order_by('ItemTypeID', 'ASC')->get($this->itemtype);
        return $query->result();
    }
	
    function get_itemtype_aset_edit($nilai_item){
        $this->db->select('ItemTypeName,ItemTypeID');
        $this->db->from($this->itemtype);
        $this->db->order_by('ItemTypeID', 'ASC');
        $this->db->where('ItemTypeID =', $nilai_item);
        $query = $this->db->get();
        
        return $query->result();
    }
	
    function get_itemtype_inventori(){
        $this->db->select('ItemTypeName,ItemTypeID');
        $this->db->from($this->itemtype);
        $this->db->order_by('ItemTypeID', 'ASC');
        $this->db->where('ItemTypeID !=', 1);
        $query = $this->db->get();
        
        return $query->result();
    }
	
    function get_itemtype_fix_aset($nilai){
        $this->db->select('ItemTypeName,ItemTypeID');
        $this->db->from($this->itemtype);
        $this->db->order_by('ItemTypeID', 'ASC');
        $this->db->where('ItemTypeID =', $nilai);		
        $query = $this->db->get();
        
        return $query->result();
    }
	
    /* function get_itemprice(){
        $query = $this->db->order_by('ItemCode', 'ASC')->get($this->itemprice);
        return $query->result();
    }
	
    function get_itempricelist(){
        $query = $this->db->order_by('ItemCode', 'ASC')->get($this->itempricelist);
        return $query->result();
    } */
	
    function get_vendor($branch_code){
        $this->db->select('*, cy.CurrencyRate as ExchangeRate, vd.CurrencyCode as Vnd_CurrencyCode');
        $this->db->from($this->vendor . ' as vd');
        $this->db->join('am_currency cy', 'cy.CurrencyCode = vd.CurrencyCode', 'INNER');
 
        /*if($this->session->userdata('jobtitle') != 'DIR'){
            if($branch_code != ''){
                $this->db->where('vd.BranchCode', $branch_code);            
            }
            else{
                $this->db->where('vd.BranchCode', $this->session->userdata('branch'));
            }
        }*/
		$this->db->where('vd.BranchCode', $this->session->userdata('branch'));
        $this->db->where('cy.CompanyCode', $this->session->userdata('company'));
        $this->db->where('vd.TransStatus', 1);
        $this->db->order_by('vd.VendorCode', 'ASC');                
        $query = $this->db->get();
        
        return $query->result();
    }
	
	function get_vendor_new_filter($branch_code, $company_code){
        $this->db->select('vd.BranchCode, vd.VendorCode,vd.VendorName, vd.VendorAddress, vd.TermsPaymentID, cy.CurrencyRate as ExchangeRate, vd.CurrencyCode as Vnd_CurrencyCode');
        $this->db->from($this->vendor . ' as vd');
        $this->db->join('am_currency cy', 'cy.CurrencyCode = vd.CurrencyCode', 'INNER');
 		
		if($branch_code != ''){
			$this->db->where('vd.BranchCode', $branch_code); 
			$this->db->where('cy.CompanyCode', $company_code);            
		}
		else{
			$this->db->where('vd.BranchCode', $this->session->userdata('branch'));
			$this->db->where('cy.CompanyCode', $this->session->userdata('company'));
		}
		
        $this->db->where('vd.TransStatus', 1);
        $this->db->order_by('vd.VendorCode', 'ASC');                
        $query = $this->db->get();
        
        return $query->result();
    }
	
	function get_vendor_new($branch_code, $company_code){
        $this->db->select('*, cy.CurrencyRate as ExchangeRate, vd.CurrencyCode as Vnd_CurrencyCode');
        $this->db->from($this->vendor . ' as vd');
        $this->db->join('am_currency cy', 'cy.CurrencyCode = vd.CurrencyCode', 'INNER');
 		
		if($branch_code != ''){
			$this->db->where('vd.BranchCode', $branch_code); 
			$this->db->where('cy.CompanyCode', $company_code);            
		}
		else{
			$this->db->where('vd.BranchCode', $this->session->userdata('branch'));
			$this->db->where('cy.CompanyCode', $this->session->userdata('company'));
		}
		
        $this->db->where('vd.TransStatus', 1);
        $this->db->order_by('vd.VendorCode', 'ASC');                
        $query = $this->db->get();
        
        return $query->result();
    }
	
	function get_vendor_code($branch_code){
        $this->db->select('vd.*');
        $this->db->from($this->vendor . ' as vd');
 
        $this->db->where('vd.BranchCode', $branch_code);
		
        $this->db->where('vd.TransStatus', 1);
        $this->db->order_by('vd.VendorCode', 'ASC');                
        $query = $this->db->get();
        
        return $query->result();
    }
	
    function get_shipment(){
        $query = $this->db->where('TransStatus', 1)->order_by('ShipmentName', 'ASC')->get($this->shipment);
        return $query->result();
    }
	
    function get_branch(){
        if($this->session->userdata("jobtitle") != 'DIR') {
            $this->db->where('am_branch.CompanyCode', $this->session->userdata("company"));
        }
        $this->db->order_by('BranchName', 'ASC');
        $query = $this->db->get($this->branch);
        return $query->result();
    }
	
	function get_branch_no_filter(){
		if($this->session->userdata('branch_status') == 1){
			$this->db->where('am_branch.BranchCode', $this->session->userdata('branch'));
		}
		else{
			$this->db->where('am_branch.CompanyCode', $this->session->userdata("company"));
		}
        $this->db->order_by('BranchName', 'ASC');
        $query = $this->db->get($this->branch);
        return $query->result();
    }
		
    function get_warehouse(){
        $query = $this->db->where('TransStatus', 0)->order_by('WarehouseName', 'ASC')->get($this->warehouse);
        return $query->result();
    }
	
    function get_bank(){
        if ($this->session->userdata('jobtitle_status')==0) {  
            $this->db->join('am_city', 'am_city.CityCode = ' . $this->bank . '.BankCity', 'INNER' );
            $this->db->order_by('BankName', 'ASC');
            $query = $this->db->get($this->bank);
        }
        else {
            if ($this->session->userdata('branch_status') == 1) {
                $this->db->where('BranchCode', $this->session->userdata('branch'));
            }

            $this->db->where('CompanyCode', $this->session->userdata('company'));
            $this->db->join('am_city', 'am_city.CityCode = ' . $this->bank . '.BankCity', 'INNER' );
            $this->db->order_by('BankName', 'ASC');

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

    function get_bank_coa($branch_code, $bank_id){
        $select_bank =  "SELECT *
                         FROM `am_bank`
                         INNER JOIN `am_city` ON `am_city`.`CityCode` = `am_bank`.`BankCity`
                         INNER JOIN `am_branch` ON `am_branch`.`BranchCode` = `am_bank`.`BranchCode`
                         WHERE ((NOT EXISTS (Select Null From am_accountgl Where am_accountgl.BankID = am_bank.BankID))"; 

        if ($bank_id != Null || $bank_id != ''){
            $select_bank = $select_bank . "OR (`am_bank`.`BankID` LIKE '%" . $bank_id . "%')) ";
        }
        else {
            $select_bank = $select_bank . ")";
        }
        
        $select_bank = $select_bank . "AND (`am_bank`.`CompanyCode` = '" . $this->session->userdata('company') . "') ";
        $select_bank = $select_bank . "AND (`am_bank`.`BranchCode` = '" . $branch_code . "') ";
		$select_bank = $select_bank . "AND (`am_bank`.`TransStatus` = 1) ";
        $select_bank = $select_bank . "ORDER BY `am_branch`.`BranchStatus` ASC, `am_bank`.`BankName` ASC";               

        $query = $this->db->query($select_bank);
        return $query->result();
    }
    
    function get_bank_by_branch($branch_code){
        $this->db->join('am_city', 'am_city.CityCode = ' . $this->bank . '.BankCity', 'INNER' );
        $this->db->where('CompanyCode', $this->session->userdata('company'));
        $this->db->where('BranchCode', $branch_code);
        $this->db->order_by('BankName', 'ASC');
        $query = $this->db->get($this->bank);

        return $query->result();
    }
	
    function get_accountsubtype(){
        $query = $this->db->order_by('AccountTypeName', 'ASC')->get($this->accountsubtype);
        return $query->result();
    }
	
	function get_accounttype(){
        $query = $this->db->order_by('AccountTypeName', 'ASC')->get('am_accounttype');
        return $query->result();
    }
	
    function get_accountgl(){
        $query = $this->db->order_by('AccountName', 'ASC')->get($this->accountgl);
        return $query->result();
    }
	
    function get_company(){
        $query = $this->db->order_by('CompanyName', 'ASC')->get($this->company);
        return $query->result();
    }

    function get_company_branch(){
        $this->db->select('am_company.CompanyCode as CompanyCode, CompanyName, CompBranchStatus, BranchCode, BranchName, BranchStatus');
        $this->db->from($this->company);
        $this->db->join('am_branch', 'am_branch.CompanyCode = am_company.CompanyCode', 'INNER');
        $this->db->where('am_branch.BranchStatus', 0);
		if(($this->session->userdata('username') == 20000) || ($this->session->userdata('jobtitle_status') == 0)){
			
		}
		else{
			$this->db->where('am_company.CompanyCode', $this->session->userdata('company'));
		}
		
        $this->db->order_by('am_company.CompanyCode', 'ASC');
        $this->db->order_by('am_branch.BranchCode', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    }
    
    function get_employee(){
        $query = $this->db->where('TransStatus', 0)->order_by('EmployeeName', 'ASC')->get($this->employee);
        return $query->result();
    }
	
	
	function get_customer($branch_code){
        $this->db->select('*, cy.CurrencyRate as ExchangeRate');
        $this->db->from($this->customer . ' as cs');
        $this->db->join('am_currency cy', 'cy.CurrencyCode = cs.CurrencyCode', 'INNER');
 
        if($this->session->userdata('jobtitle_status') != 0){
            if($branch_code != ''){
                $this->db->where('cs.BranchCode', $branch_code);            
            }
            else{
                $this->db->where('cs.BranchCode', $this->session->userdata('branch'));
            }
        }
        
        $this->db->where('cs.TransStatus', 1);
        $this->db->order_by('cs.CustomerCode', 'ASC');                
        $query = $this->db->get();
        
        return $query->result();
    }
	
    function get_customer_with_rate($BranchCode){
		$this->db->select('am_customer.*,
		am_city.CityName as CustomerCityName,am_country.CountryName as CustomerCountryName,am_state.StateName as CustomerStateName, am_currency.CurrencyRate');
		$this->db->from($this->customer);
		$this->db->join($this->city,'am_customer.CustomerCity=am_city.CityCode','INNER');
		$this->db->join($this->state,'am_customer.CustomerState=am_state.StateCode','INNER');
		$this->db->join($this->country,'am_customer.CustomerCountry=am_country.CountryCode','INNER');
		$this->db->join($this->currency,'am_customer.CurrencyCode=am_currency.CurrencyCode','INNER');
		$this->db->where('BranchCode', $BranchCode);
		$this->db->where($this->currency.'.CompanyCode', $this->session->userdata('company'));
		
		$this->db->where('am_customer.TransStatus', 1);
		
        $this->db->order_by('CustomerCode', 'ASC');                
        $query = $this->db->get();
        
        return $query->result();
    }
	
	function get_vendor_with_rate($BranchCode){
		$this->db->select('am_vendor.*,
		am_city.CityName as VendorCityName,am_country.CountryName as VendorCountryName,am_state.StateName as VendorStateName, am_currency.CurrencyRate');
		$this->db->from($this->vendor);
		
		$this->db->join($this->city,'am_vendor.VendorCity=am_city.CityCode','INNER');
		$this->db->join($this->state,'am_vendor.VendorState=am_state.StateCode','INNER');
		$this->db->join($this->country,'am_vendor.VendorCountry=am_country.CountryCode','INNER');
		$this->db->join($this->currency,'am_vendor.CurrencyCode=am_currency.CurrencyCode','INNER');
		
		if($this->session->userdata('jobtitle_status') != 0){
			$this->db->where('BranchCode', $BranchCode);
		}
		
		$this->db->where($this->currency.'.CompanyCode', $this->session->userdata('company'));
		
		$this->db->where('am_vendor.TransStatus', 1);
		
        $this->db->order_by('VendorCode', 'ASC');                
        $query = $this->db->get();
        
        return $query->result();
    }
	
    function get_customer_address(){
        $this->db->select('am_customer.BranchCode, am_customer.SaldoBalance, am_customer.SaldoDP, am_customer.TaxIncludedStatus, am_customer.CustomerCode, am_customer.CustomerName, am_customer.CustomerAddress, am_city.CityName as CustCity, am_state.StateName as CustState, am_country.CountryName as CustCountry');
        $this->db->from($this->customer);
        $this->db->join('am_city', 'am_city.CityCode = am_customer.CustomerCity', 'inner');
        $this->db->join('am_state', 'am_state.StateCode = am_customer.CustomerState', 'inner');
        $this->db->join('am_country', 'am_country.CountryCode = am_customer.CustomerCountry', 'inner');
        $this->db->order_by('am_customer.CustomerName', 'ASC');
        $this->db->where('am_customer.BranchCode',$this->session->userdata('branch'));
        $this->db->where('am_customer.TransStatus', 0);
        $query = $this->db->get();
        return $query->result();
    }
	
    function get_branch_address(){
        $this->db->select('am_branch.BranchCode, am_branch.BranchName, am_branch.BranchAddress, am_city.CityName as CustCity, am_state.StateName as CustState, am_country.CountryName as CustCountry');
        $this->db->from($this->branch);
        $this->db->join('am_city', 'am_city.CityCode = am_branch.BranchCity', 'inner');
        $this->db->join('am_state', 'am_state.StateCode = am_branch.BranchState', 'inner');
        $this->db->join('am_country', 'am_country.CountryCode = am_branch.BranchCountry', 'inner');
        $this->db->order_by('am_branch.BranchName', 'ASC');
        $this->db->where('am_branch.CustomerStatus', 1);
        $this->db->where('am_branch.TransStatus', 0);
        $query = $this->db->get();
        return $query->result();
    }
	
    function get_warehouse_type(){
        $query = $this->db->where('TransStatus', 0)->order_by('WHTypeName', 'ASC')->get($this->warehousetype);
        return $query->result();
    }
	
    function get_sales(){
        $this->db->select('*');
        $this->db->join('am_department', 'am_department.DepartmentCode = am_employee.DepartmentCode');
        $this->db->where_in('am_department.DepartmentName', array('MARKETING', 'SALES'));
		//$this->db->where('am_employee.JobTitleCode','STAFF');
		$this->db->where_in('am_employee.TransStatus',array(0,1));
        $this->db->where('am_employee.BranchCode', $this->session->userdata('branch'));
        $this->db->order_by('am_employee.EmployeeCode', 'ASC');
        $query = $this->db->get($this->employee);
        
        return $query->result();
    }
	
    function get_fixedassettype(){
        $query = $this->db->where('TransStatus', 0)->order_by('FixedAssetType', 'ASC')->get($this->fixedassettype);
        return $query->result();
    }
	
    function get_depreciatemethod(){
        $query = $this->db->order_by('DepMethodeName', 'ASC')->get($this->depreciatemethod);
        return $query->result();
    }
	
	// ini mysql_real_escape_string harus diganti ke CI, dan isset($_POST[])
    function search_item(){
        $this->db->_protect_identifiers = true;
		
		/*======================================scrol view 1-150=======================================*/
		$page		= ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
		$rows		= ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;
		
		$beginning 	= ($page - 1) * $rows;
		$end 		= $page * $rows;
		/*==============================================================================================*/
		
		/*=================================post dari find di atas pop up====================================================*/
		$param 				= $this->input->post('param');
		$ItemCategoryID		= isset($param['cmb_ItemCategoryID']) ? $param['cmb_ItemCategoryID'] : '';
		$ItemCode			= isset($param['txt_ItemCode']) ? $param['txt_ItemCode'] : '';
		$ItemDesc			= isset($param['txt_ItemDesc']) ? $param['txt_ItemDesc'] : '';
		/*============================================end============================================================*/
		
        $this->db->select("it.ItemCode, it.ItemCategoryID, it.ItemDesc, it.ItemTypeID, it.ItemUnit, it.ItemPPhType,
                           ItemPPhType as ItemPPh22Status, '0' ItemPPh23Status, '0' as ItemAdvNetDueIn, 
                           ic.ItemCategoryName as ItemCategoryCode, ic.AssetStatus as ItemStatus");
        $this->db->from($this->item . ' it');
        $this->db->join($this->itemcategory . ' ic', 'ic.ItemCategoryID = it.ItemCategoryID', 'INNER');
		
		$this->db->where_in('it.TransStatus', array(0,1));
		
		/*===============================================pencarian pas pop up jika kondisi tidak sama dengan kosong ==================*/
		if($ItemCategoryID != ""){
			if($ItemCategoryID == "All"){
				
			}else{
				$this->db->where('it.ItemCategoryID', $ItemCategoryID);
			}
		}
		if($ItemCode != ''){$this->db->like('it.ItemCode', $ItemCode);}
		if($ItemDesc != ''){$this->db->like('it.ItemDesc', $ItemDesc);}
		/*====================================end======================================================================================*/
		
		$this->db->order_by('it.ItemCode', 'ASC');
		$this->db->limit($end, $beginning);
        $this->db->_protect_identifiers = false;

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

	// ini mysql_real_escape_string harus diganti ke CI
	function search_service(){
		$this->db->_protect_identifiers = true;
		/*======================================scrol view 1-150=======================================*/
		$page		= ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
		$rows		= ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;
		
		$beginning 	= ($page - 1) * $rows;
		$end 		= $page * $rows;
		/*==============================================================================================*/
		
		/*=================================post dari find di atas pop up====================================================*/
		$param 				= $this->input->post('param');
		$ItemCategoryID		= isset($param['cmb_ItemCategoryID']) ? $param['cmb_ItemCategoryID'] : '';
		$SvcDetailName		= isset($param['txt_ItemCode_Servis']) ? $param['txt_ItemCode_Servis'] : '';
		$SvcDetailDesc		= isset($param['txt_ItemDesc_Servis']) ? $param['txt_ItemDesc_Servis'] : '';
		/*============================================end============================================================*/
		
        /*=========================ny session====================================================================*/
		$sess_branch    	= $this->session->userdata('branch');
		$sess_company    	= $this->session->userdata('company');
		/*=============================end======================================================================*/
		
		$this->db->select("SvcDetailName as ItemCode, SvcDetailDesc as ItemDesc, SvcDetailUnit as ItemUnit, sc.SvcCategoryID as ItemCategoryID,sc.SvcCategoryName as ItemCategoryName, '0' as ItemPPh22Status, 
                           SvcPPh23Status as ItemPPh23Status, '0' as ItemAdvNetDueIn, SvcCategoryName as ItemCategoryCode, '2' as ItemStatus");
        $this->db->from($this->servicedetail . ' as sd');
        $this->db->join('sys_servicecategory as sc', 'sc.SvcCategoryID = sd.SvcCategoryID', 'INNER');
		
		$this->db->where('sc.companycode', $sess_company);
		
		/*===============================================pencarian pas pop up jika kondisi tidak sama dengan kosong ==================*/
		if($SvcDetailName != ''){$this->db->like('sd.SvcDetailName', $SvcDetailName);}
        if($SvcDetailDesc != ''){$this->db->like('sd.SvcDetailDesc', $SvcDetailDesc);}
		/*====================================end======================================================================================*/
		
		$this->db->order_by('sd.SvcDetailName', 'DESC');
		$this->db->limit($end, $beginning);
		
		$this->db->_protect_identifiers = false;
		return $this->db->get()->result();
    }
    
	function search_advance(){
		$this->db->_protect_identifiers = true;
		/*======================================scrol view 1-150=======================================*/
		$page		= ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
		$rows		= ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;
		
		$beginning 	= ($page - 1) * $rows;
		$end 		= $page * $rows;
		/*==============================================================================================*/
		
		/*=================================post dari find di atas pop up====================================================*/
		$param 				= $this->input->post('param');
		$AdvDetailName		= isset($param['txt_ItemCode_Adv']) ? $param['txt_ItemCode_Adv'] : '';
		$AdvDetailDesc		= isset($param['txt_ItemDesc_Adv']) ? $param['txt_ItemDesc_Adv'] : '';
		/*============================================end============================================================*/
		
        /*=========================ny session====================================================================*/
		$sess_branch    	= $this->session->userdata('branch');
		$sess_company    	= $this->session->userdata('company');
		/*=============================end======================================================================*/
		
		$this->db->select("AdvDetailName as ItemCode, AdvDetailDesc as ItemDesc, 'Times' as ItemUnit, ac.AdvCategoryID as ItemCategoryID, '0' as ItemPPh22Status, 
                           '0' as ItemPPh23Status, AdvNetDueIn as ItemAdvNetDueIn, AdvCategoryName as ItemCategoryCode, '3' as ItemStatus");
        $this->db->from($this->advancedetail . ' as ad');
        $this->db->join('sys_advancecategory as ac', 'ac.AdvCategoryID = ad.AdvCategoryID', 'INNER');
		
		$this->db->where('ac.CompanyCode', $sess_company);
		
		/*===============================================pencarian pas pop up jika kondisi tidak sama dengan kosong ==================*/
		if($AdvDetailName != ''){$this->db->like('ad.AdvDetailName', $AdvDetailName);}
        if($AdvDetailDesc != ''){ $this->db->like('ad.AdvDetailDesc', $AdvDetailDesc);}
		/*====================================end======================================================================================*/
		
		$this->db->order_by('ad.AdvDetailName', 'DESC');
		$this->db->limit($end, $beginning);
		
		$this->db->_protect_identifiers = false;
		return $this->db->get()->result();
    }
    
	// ini mysql_real_escape_string harus diganti ke CI
    function search_item_with_category_item(){
        $item_catcode       = isset($_POST['ItemCategoryID'])? mysql_real_escape_string($_POST['ItemCategoryID']) : '';
        $item_code          = isset($_POST['item_code'])? mysql_real_escape_string($_POST['item_code']) : '';
        $item_desc          = isset($_POST['item_desc'])? mysql_real_escape_string($_POST['item_desc']) : '';
		
        if($item_catcode!=''){
            $this->db->select('*');
            $this->db->from($this->item);
            $this->db->join($this->itemcategory, "am_item.ItemCategoryID=am_itemcategory.ItemCategoryID", "left");
				
            if($item_catcode != ''){
                $this->db->where('ItemCategoryID', $item_catcode);
            }

            if($item_code != ''){
                $this->db->like('ItemCode', $item_code);
            }

            if($item_desc != ''){
                $this->db->like('ItemDesc', $item_desc);
            }

            $this->db->where('TransStatus', 0);
            $this->db->order_by('ItemCode', 'ASC');
            return $this->db->get()->result();
        }
        else{
            return '[]';
        }
    }
	
	// ini mysql_real_escape_string harus diganti ke CI
    function search_inventory(){
		$this->db->_protect_identifiers = true;
        $item_catcode       = isset($_POST['item_categori'])? mysql_real_escape_string($_POST['item_categori']) : '';
        $item_code          = isset($_POST['item_code'])? mysql_real_escape_string($_POST['item_code']) : '';
        $item_desc          = isset($_POST['item_desc'])? mysql_real_escape_string($_POST['item_desc']) : '';

        $this->db->select('at_inventories.ItemCode, at_inventories.ItemQty, at_inventories.ItemBooks, am_item.ItemDesc, 
                           am_item.ItemUnit, am_itemprice.ItemPrice as ItemPrice, am_item.ItemCategoryID, am_item.ItemCategoryID as ItemCategoryCode, "0" as ItemStatus');
        $this->db->from($this->inventory);
        $this->db->join('am_item', 'am_item.ItemCode = at_inventories.ItemCode', 'INNER');
        $this->db->join('am_itemprice', 'am_itemprice.ItemCode = at_inventories.ItemCode AND am_itemprice.BranchCode = at_inventories.BranchCode', 'LEFT');
        $this->db->where('am_item.TransStatus',1);
		$this->db->where('at_inventories.BranchCode', $this->session->userdata('branch'));
		$this->db->where_in('am_item.ItemTypeID',array(1,2)); // 1 = Fixed Asset, 2 = Inventory Part
//		$this->db->where('at_inventories.LockStatus <>',1);
		
        if($item_catcode != ''){
            $this->db->where('am_item.ItemCategoryID', $item_catcode);
        }

        if($item_code != ''){
            $this->db->like('am_item.ItemCode', $item_code);
        }

        if($item_desc != ''){
            $this->db->like('am_item.ItemDesc', $item_desc);
        }

        $this->db->order_by('at_inventories.ItemCode', 'ASC');
		$this->db->_protect_identifiers = false;
        $query = $this->db->get();
		
        if($query->num_rows() > 0){
            return $query->result();
        }
        else{
            return array();
        }
    }
	
	//------------------by fachri-----------------------------------------
	// ini mysql_real_escape_string harus diganti ke CI
	//fachri tgl 15/04/2015--------------------------------------------------------
	function search_not_in_production(){
		$branch = $this->input->post('branch');
		$item_catcode	= isset($_POST['item_categori'])? mysql_real_escape_string($_POST['item_categori']) : '';
        $item_code	= isset($_POST['item_code'])? mysql_real_escape_string($_POST['item_code']) : '';
        $item_desc	= isset($_POST['item_desc'])? mysql_real_escape_string($_POST['item_desc']) : '';
        //$sess_branch    = $this->session->userdata('branch');
		
		$not_in = "SELECT ItemCode, ItemDesc, ItemUnit FROM am_item 
					WHERE ItemCode NOT IN (SELECT ItemCode FROM at_inventories WHERE BranchCode = '$branch') 
					AND TransStatus = 1";
		return $this->db->query($not_in)->result();
	}
	
	// ini mysql_real_escape_string harus diganti ke CI
    function search_job_costing(){
		
        $item_catcode	= isset($_POST['item_categori'])? mysql_real_escape_string($_POST['item_categori']) : '';
        $item_code      = isset($_POST['item_code'])? mysql_real_escape_string($_POST['item_code']) : '';
        $item_desc		= isset($_POST['item_desc'])? mysql_real_escape_string($_POST['item_desc']) : '';
        $sess_branch    = $this->session->userdata('branch');
        
        if($item_catcode!=''){
            $this->db->select('*, at_inventories.ItemQty, at_inventories.ItemAverage, am_itemcategory.ItemCategoryID, at_inventories.LockStatus as IsLock');
            $this->db->from($this->itemcategory);
            $this->db->join('am_item', 'am_item.ItemCategoryID = am_itemcategory.ItemCategoryID', 'inner');
            $this->db->join('at_inventories', 'at_inventories.ItemCode = am_item.ItemCode', 'inner');
			
			$explode_arr 	= array();
			$explode_arr 	= explode(",", $item_catcode);
			$var_array 		= implode(",", $explode_arr);

			$this->db->where('at_inventories.BranchCode', $sess_branch);
            $this->db->where_in('am_item.ItemCategoryID',$explode_arr);
			
            if($item_code != ''){
                $this->db->like('am_item.ItemCode', $item_code);
            }

            if($item_desc != ''){
                $this->db->like('am_item.ItemDesc', $item_desc);
            }
            
            $this->db->order_by('am_item.ItemCode', 'ASC');
            
            return $this->db->get()->result();
        }
        else{
            return '[]';
        }
    }
	//end fahri-------------------------------------------------------------------------------------
	
//    function search_expense_account(){
//        $AccountNumber	= isset($_POST['item_code'])? mysql_real_escape_string($_POST['item_code']) : '';
//        $AccountName	= isset($_POST['item_desc'])? mysql_real_escape_string($_POST['item_desc']) : '';
//		
//        $this->db->select('am_accountgl.AccountNumber as ItemCode,am_accountgl.AccountName as ItemDesc');
//        $this->db->from('at_compaccountgl');
//        $this->db->join('am_accountgl','am_accountgl.AccountNumber = at_compaccountgl.AccountNumber','LEFT');
//        $this->db->join('am_accountsubtype','am_accountsubtype.AccountSubTypeID=am_accountgl.AccountSubTypeID','LEFT');
//        $this->db->like('am_accountsubtype.AccountTypeName','Expense');
//        $this->db->where('at_compaccountgl.GLSaldoyears',date('Y'));
//        $this->db->where('at_compaccountgl.BranchCode',$this->session->userdata('branch'));
//		
//        if($AccountNumber != ''){
//            $this->db->like('am_accountgl.AccountNumber', $AccountNumber);
//        }
//
//        if($AccountName != ''){
//            $this->db->like('am_accountgl.AccountName', $AccountName);
//        }
//	
//        $this->db->order_by('am_accountgl.AccountNumber', 'ASC');
//        return $this->db->get()->result();
//    }
	
    function get_brg_dlm_proses(){
        $this->db->select('am_accountgl.AccountNumber, am_accountgl.AccountName, am_accountgl.ItemCategory');
        $this->db->from($this->accountgl);
        $this->db->join('at_compaccountgl', 'at_compaccountgl.AccountNumber = am_accountgl.AccountNumber', 'inner');
        $this->db->where('at_compaccountgl.BranchCode',$this->session->userdata('branch'));
        $this->db->where('at_compaccountgl.CompanyCode',$this->session->userdata('company'));
        $this->db->where('am_accountgl.AccountSubTypeID',16);
		$this->db->where('am_accountgl.TransStatus',2);
        $this->db->where('at_compaccountgl.GLSaldoYears',date('Y'));
        $query = $this->db->get();

        return $query->result();
    }
	
    function get_branch_by_company(){
        $this->db->where('CompanyCode',$this->session->userdata('company'));
        
        if ($this->session->userdata('branch_status') == 1){
            $this->db->where('BranchCode', $this->session->userdata('branch'));            
        }
        
        $this->db->order_by('BranchStatus', 'ASC');
        $this->db->order_by('BranchName', 'ASC');
        $query = $this->db->get($this->branch);
        return $query->result();
    }
	
	function get_branch_by_company_new($company){
		$branch_status   	= $this->session->userdata('branch_status');
		$branch_code   		= $this->session->userdata('branch');
		
		$this->db->select('BranchCode, BranchName');
        $this->db->from($this->branch);
		
		if($branch_status == 0){
			$this->db->where('CompanyCode', $company);
		}
		else{
			$this->db->where('BranchCode', $branch_code);
		}
        $query = $this->db->get();
        return $query->result();
    }

    function get_branch_active_by_company(){
        $this->db->where('CompanyCode',$this->session->userdata('company'));
        $this->db->where('BranchStatus =', 1);
        $this->db->where('TransStatus !=', 3);
        $query = $this->db->order_by('BranchName', 'ASC')->get($this->branch);
        return $query->result();
    }

    function get_branch_detail(){
        $this->db->where('BranchCode',$this->session->userdata('branch'));
        $query = $this->db->get($this->branch);
        return $query->row();
    }
	
    // Function Join
    function bank_join_currency(){
        $this->db->select('am_bank.BankID, am_bank.BankCode, am_bank.BankName, am_bank.BankNumber, am_bank.BankInCode, 
                           am_bank.BankOutCode, am_currency.CurrencyCode, am_currency.CurrencyName, am_currency.CurrencyRate');
        $this->db->from($this->bank);
        $this->db->join('am_currency', 'am_currency.CurrencyCode = am_bank.CurrencyCode', 'inner');
        $this->db->order_by('am_bank.BankName', 'ASC');
	
        $query = $this->db->get();
        return $query->result();
    }
	
    function join_bank_account(){
        $this->db->select('at_compaccountgl.AccountNumber,am_accountgl.AccountName,am_bank.BankOutCode,am_bank.BankInCode,am_currency.CurrencyCode,am_currency.CurrencyName,am_currency.CurrencyRate');
        $this->db->from('at_compaccountgl');
        $this->db->join('am_accountgl','at_compaccountgl.AccountNumber=am_accountgl.AccountNumber','INNER');
        $this->db->join('am_bank','am_bank.BankID=am_accountgl.BankID','INNER');
        $this->db->join('am_currency','am_accountgl.CurrencyCode=am_currency.CurrencyCode','INNER');
        $this->db->where('am_accountgl.AccountSubTypeID', 7);
        $this->db->where('am_accountgl.AccountChild', 1);
        $this->db->where('am_accountgl.TransStatus', 2);
        $this->db->where('at_compaccountgl.CompanyCode', $this->session->userdata('company'));
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        
        return $this->db->get()->result();
    }
	
    function get_employee_name($EmployeeCode){
        $this->db->select('EmployeeName');
        $this->db->from('am_employee');
        $this->db->where('EmployeeCode', $EmployeeCode);
        $query = $this->db->get();

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

    /* Added By	: Hasan / Added Date	: 10/12/2014 */
    function get_purchase_requisition_for_check($PReqNumber){
        return $this->db->get_where($this->purchaserequisition, array("PReqNumber"=> $PReqNumber))->num_rows();
    }
	
    function insert_userlog($data){
        $this->db->insert($this->userlog, $data);
    }
	
	//get currency by company (fahri)
	function get_currency_new($AccountAutoStatus){
        $this->db->select('*');
        $this->db->from($this->currency . ' as cy');
        $this->db->join($this->country . ' as ct', 'ct.CountryCode = cy.CurrencyCountry', 'INNER');
        $this->db->where('cy.TransStatus', 1);
		$this->db->where('cy.CompanyCode', $this->session->userdata('company'));
		
		if($AccountAutoStatus != ""){
			if($AccountAutoStatus != 0){
				$this->db->where('cy.CurrencyCode', "IDR");
			}
		}
		
        $this->db->order_by('CurrencyCode', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    }
	//end
	
	//function journal voucher opening balance
	function insert_journal_voucher($account_number, $branch_code, $opening_balance, $account_subtype, $account_name, $cbg_currency){
		
		$employee_code 			= $this->session->userdata("username");
		$company_code 			= $this->session->userdata("company");
		$JVchDate 				= date('Y-m-d');
		
		$jv_number    			= $this->m_journalvoucher->create_jv_number(date('Y-m-d'));
		$currency_code 			= $this->m_currency->get_detail_by_company($cbg_currency, $company_code);
		$chek_sub_type_name 	= $this->m_coa->check_sub_type_master($account_subtype);
		
		$account_subtype 		= $chek_sub_type_name->AccountTypeName;
		$opening_balance_abs 	= abs($opening_balance);
		
		$cek_subtype_name 		= $this->debet_kredit($account_subtype);
		
		//kondisi opening balance jika dia lebih kecil atau lebih besar
		$amount_currency 	= $opening_balance * $currency_code->CurrencyRate;
		
		$data_group = array(
			'BranchCode' 		=> $branch_code,
			'JVchDate' 			=> date('Y-m-d'),
			'JVchNumber' 		=> $jv_number,
			'JVchDesc' 			=> "Opening Balance",
			'JVchTotalDebet' 	=> 0,
			'JVchTotalCredit' 	=> $opening_balance_abs,
			'JVchType' 			=> 2,
			'AppLevel1Status' 	=> 1,
			'AppLevel1Code' 	=> $employee_code,
			'AppLevel1Date' 	=> date('Y-m-d'),
			'AppLevel2Status' 	=> 1,
			'AppLevel2Code' 	=> $employee_code,
			'AppLevel2Date' 	=> date('Y-m-d'),
			'TransStatus' 		=> 2,
			'LastUser' 			=> $employee_code
		);
		
		$result_insert = $this->db->insert($this->journalvoucher, $data_group);
		
		$update_data = array(
			'AppFinished' => 1
		);
		$result_insert = $this->db->update($this->journalvoucher, $update_data, array('JVchNumber' => $jv_number));
				
		if($opening_balance < 0 && $cek_subtype_name == "Kredit"){
			$data_detail_debet = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> $account_number,
				'JVchDebetSaldo' 	=> $opening_balance_abs,
				'JVchCreditSaldo' 	=> 0,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $this->db->insert($this->jv_detail, $data_detail_debet);
			
			$data_detail_credit = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> "3-7-00-000-00",
				'JVchDebetSaldo' 	=> 0,
				'JVchCreditSaldo' 	=> $opening_balance_abs,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $this->db->insert($this->jv_detail, $data_detail_credit);
			
			$journal_debet = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $jv_number,
				'JVchAccountNumber' 	=> $account_number,
				'JVchSaldoDebet' 		=> $opening_balance_abs,
				'JVchSaldoCredit'  		=> 0,
				'JVchSignStatus' 		=> 0,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $this->db->insert($this->jv_journal, $journal_debet);
			
			$journal_kredit = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $jv_number,
				'JVchAccountNumber' 	=> "3-7-00-000-00",
				'JVchSaldoDebet' 		=> 0,
				'JVchSaldoCredit'  		=> $opening_balance_abs,
				'JVchSignStatus' 		=> 1,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $this->db->insert($this->jv_journal, $journal_kredit);
		}
		else if($opening_balance < 0 && $cek_subtype_name == "Debet"){
			
			$data_detail_debet = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> "3-7-00-000-00",
				'JVchDebetSaldo' 	=> $opening_balance_abs,
				'JVchCreditSaldo' 	=> 0,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $this->db->insert($this->jv_detail, $data_detail_debet);
			
			$data_detail_credit = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> $account_number,
				'JVchDebetSaldo' 	=> 0,
				'JVchCreditSaldo' 	=> $opening_balance_abs,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $this->db->insert($this->jv_detail, $data_detail_credit);
			
			$journal_debet = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $jv_number,
				'JVchAccountNumber' 	=> "3-7-00-000-00",
				'JVchSaldoDebet' 		=> $opening_balance_abs,
				'JVchSaldoCredit'  		=> 0,
				'JVchSignStatus' 		=> 1,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $this->db->insert($this->jv_journal, $journal_debet);
			
			$journal_credit = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $jv_number,
				'JVchAccountNumber' 	=> $account_number,
				'JVchSaldoDebet' 		=> 0,
				'JVchSaldoCredit'  		=> $opening_balance_abs,
				'JVchSignStatus' 		=> 0,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $this->db->insert($this->jv_journal, $journal_credit);
		}
		else if($opening_balance > 0 && $cek_subtype_name == "Kredit"){
			$data_detail_debet = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> $account_number,
				'JVchDebetSaldo' 	=> $opening_balance_abs,
				'JVchCreditSaldo' 	=> 0,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $this->db->insert($this->jv_detail, $data_detail_debet);
			
			$data_detail_credit = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> "3-7-00-000-00",
				'JVchDebetSaldo' 	=> 0,
				'JVchCreditSaldo' 	=> $opening_balance_abs,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $this->db->insert($this->jv_detail, $data_detail_credit);
			
			$journal_debet = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $jv_number,
				'JVchAccountNumber' 	=> $account_number,
				'JVchSaldoDebet' 		=> $opening_balance_abs,
				'JVchSaldoCredit'  		=> 0,
				'JVchSignStatus' 		=> 0,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $this->db->insert($this->jv_journal, $journal_debet);
			
			$journal_kredit = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $jv_number,
				'JVchAccountNumber' 	=> "3-7-00-000-00",
				'JVchSaldoDebet' 		=> 0,
				'JVchSaldoCredit'  		=> $opening_balance_abs,
				'JVchSignStatus' 		=> 1,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $this->db->insert($this->jv_journal, $journal_kredit);
		}
		else{
			$data_detail_debet = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> "3-7-00-000-00",
				'JVchDebetSaldo' 	=> $opening_balance_abs,
				'JVchCreditSaldo' 	=> 0,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $this->db->insert($this->jv_detail, $data_detail_debet);
			
			$data_detail_credit = array(
				'JVchNumber' 		=> $jv_number,
				'JVchAccount' 		=> $account_number,
				'JVchDebetSaldo' 	=> 0,
				'JVchCreditSaldo' 	=> $opening_balance_abs,
				'JVchDetailMemo' 	=> "Opening Balancing",
				'JVchRate' 			=> $currency_code->CurrencyRate,
				'CurrencyCode' 		=> $cbg_currency,
			);
			
			$result_insert = $this->db->insert($this->jv_detail, $data_detail_credit);
			
			$journal_debet = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $jv_number,
				'JVchAccountNumber' 	=> "3-7-00-000-00",
				'JVchSaldoDebet' 		=> $opening_balance_abs,
				'JVchSaldoCredit'  		=> 0,
				'JVchSignStatus' 		=> 1,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $this->db->insert($this->jv_journal, $journal_debet);
			
			$journal_kredit = array(
				'JVchDate' 				=> $JVchDate,
				'JVchNumber' 			=> $jv_number,
				'JVchAccountNumber' 	=> $account_number,
				'JVchSaldoDebet' 		=> 0,
				'JVchSaldoCredit'  		=> $opening_balance_abs,
				'JVchSignStatus' 		=> 0,
				'AmountCurrency' 		=> $amount_currency,
			    'JVchDesc' 				=> 'Opening Balance',
				'LastUser' 				=> $employee_code
			);
			
			$result_insert = $this->db->insert($this->jv_journal, $journal_kredit);
		}
		//end
	}
	//end function journal voucher
	
	//function bank book terpakai saat transaksi yang berhubungan dengan bank
	function bank_book_data($txt_amount, $akun_number, $txt_branch_code, $txt_opening_balance_period){
		
		$check_bank_book_by_branch	= $this->m_coa->check_bank_book($akun_number, $txt_branch_code);
		if($check_bank_book_by_branch == TRUE){
			$SaldoBalance = $check_bank_book_by_branch->SaldoBalance;
		}
		else{
			$SaldoBalance = 0;
		}
		
		if($txt_amount >= 0){
			$saldo_debit 	= $SaldoBalance + $txt_amount - 0;
			$saldo_credit 	= 0;
		}
		else{
			$saldo_debit 	= 0;
			$saldo_credit 	= $SaldoBalance + 0 - $txt_amount;
		}
		  
		$bank_book = array (
			'BranchCode'    => $txt_branch_code,
			'TransDate'     => date("Y-m-d"),
			'TransNumber'	=> "Opening Balance",	
			'BankAccount' 	=> $akun_number ,
			'SaldoDebet' 	=> $saldo_debit,
			'SaldoCredit' 	=> $saldo_credit,
			'SaldoBalance' 	=> $txt_amount
		);
		$result_insert = $this->db->insert($this->bank_book, $bank_book);
	}
	//end bank book
	
	//get lates balance (untuk ambil data terakhir di bank book)
	function get_lates_balance($bank_akun, $branch_code){
		$this->db->select('SaldoDebet, SaldoCredit, SaldoBalance, BankAccount, TransNumber');
        $this->db->from($this->bank_book);
        $this->db->where('at_bankbook.BankAccount', $bank_akun);
		$this->db->where('at_bankbook.BranchCode', $branch_code);
        $query = $this->db->get();
		if($query->num_rows() > 0){
            return $query->row();
        }
        else{
            return 0;
        }
	}
	//end get lates balance
	
	//get company for find master
	function get_company_find(){
		$company_code   	= $this->session->userdata('company');
		$branch_status   	= $this->session->userdata('branch_status');
		$branch_code   		= $this->session->userdata('branch');
		$job_title_status   = $this->session->userdata('jobtitle_status');
		$job_title_code   	= $this->session->userdata('jobtitle');
		
        $this->db->select('CompanyCode, CompanyName');
        $this->db->from($this->company);
        
		if(($job_title_status == 0) && ($job_title_code == "DIR")){
			//status for owner
		}
		else{
			$this->db->where('CompanyCode', $company_code);
		}
		$this->db->order_by('am_company.CompanyCode', 'ASC');
        $query = $this->db->get();
        
        return $query->result();
    }
	//end
	
	function search_all_item_by_kategori(){
		$this->db->_protect_identifiers = true;
		/*======================================scrol view 1-150=======================================*/
		$page		= ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
		$rows		= ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;
		
		$beginning 	= ($page - 1) * $rows;
		$end 		= $page * $rows;
		/*==============================================================================================*/
		
		/*=================================post dari find di atas pop up====================================================*/
		$param 				= $this->input->post('param');
		$ItemCategoryID		= isset($param['cmb_ItemCategoryID']) ? $param['cmb_ItemCategoryID'] : '';
		$ItemCode			= isset($param['txt_ItemCode']) ? $param['txt_ItemCode'] : '';
		$ItemDesc			= isset($param['txt_ItemDesc']) ? $param['txt_ItemDesc'] : '';
		/*============================================end============================================================*/
		
        /*=========================ny session====================================================================*/
		$sess_branch    = $this->session->userdata('branch');
		/*=============================end======================================================================*/
		
		$this->db->select('am_item.ItemCode,am_item.ItemDesc, am_item.ItemUnit, 
							at_inventories.ItemQty, at_inventories.ItemAverage, at_inventories.ItemValue, "0" as ItemStatus, am_itemcategory.ItemCategoryID as ItemCategoryCode, 
							am_itemcategory.ItemCategoryID, at_inventories.LockStatus as IsLock, am_itemcategory.ItemCategoryName as ItemCategoryName,');
		$this->db->from('am_itemcategory');
		$this->db->join('am_item', 'am_item.ItemCategoryID = am_itemcategory.ItemCategoryID', 'inner');
		$this->db->join('at_inventories', 'at_inventories.ItemCode = am_item.ItemCode', 'inner');
		
		/*$explode_arr 	= array();
		$explode_arr 	= explode(",", $item_categori);
		$var_array 		= implode(",", $explode_arr);*/

		$this->db->where('at_inventories.BranchCode', $sess_branch);
		$this->db->where_in('am_item.TransStatus', array(0,1));
		
		/*===============================================pencarian pas pop up jika kondisi tidak sama dengan kosong ==================*/
		if($ItemCategoryID != ""){
			if($ItemCategoryID == "All"){
				
			}else{
				$this->db->like('am_item.ItemCategoryID', $ItemCategoryID);
			}
		}
		if($ItemCode != ''){$this->db->like('am_item.ItemCode', $ItemCode);}
		if($ItemDesc != ''){$this->db->like('am_item.ItemDesc', $ItemDesc);}
		/*====================================end======================================================================================*/
		
		$this->db->order_by('am_item.ItemCode', 'ASC');
		$this->db->limit($end, $beginning);
		$this->db->_protect_identifiers = false;
		
		return $this->db->get()->result();
    }
	
	function search_all_item_sq(){
		$this->db->_protect_identifiers = true;
		/*======================================scrol view 1-150=======================================*/
		$page		= ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
		$rows		= ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;
		
		$beginning 	= ($page - 1) * $rows;
		$end 		= $page * $rows;
		/*==============================================================================================*/
		
		/*=================================post dari find di atas pop up====================================================*/
		$param 				= $this->input->post('param');
		$ItemCategoryID		= isset($param['cmb_ItemCategoryID']) ? $param['cmb_ItemCategoryID'] : '';
		$ItemCode			= isset($param['txt_ItemCode']) ? $param['txt_ItemCode'] : '';
		$ItemDesc			= isset($param['txt_ItemDesc']) ? $param['txt_ItemDesc'] : '';
		/*============================================end============================================================*/
		
        /*=========================ny session====================================================================*/
		$sess_branch    = $this->session->userdata('branch');
		/*=============================end======================================================================*/
		
		$this->db->select('am_item.ItemCode,am_item.ItemDesc, am_item.ItemUnit, am_item.ItemTypeID,
							at_inventories.ItemQty, at_inventories.ItemAverage, at_inventories.ItemValue, "0" as ItemStatus, am_itemcategory.ItemCategoryID as ItemCategoryCode, 
							am_itemcategory.ItemCategoryID, at_inventories.LockStatus as IsLock, am_itemcategory.ItemCategoryName as ItemCategoryName,');
		$this->db->from('am_itemcategory');
		$this->db->join('am_item', 'am_item.ItemCategoryID = am_itemcategory.ItemCategoryID', 'inner');
		$this->db->join('at_inventories', 'at_inventories.ItemCode = am_item.ItemCode', 'inner');
		
		/*$explode_arr 	= array();
		$explode_arr 	= explode(",", $item_categori);
		$var_array 		= implode(",", $explode_arr);*/

		$this->db->where('at_inventories.BranchCode', $sess_branch);
		$this->db->where_in('am_item.TransStatus', array(0,1));
		$this->db->where('am_itemcategory.AssetStatus', 0);
				
		/*===============================================pencarian pas pop up jika kondisi tidak sama dengan kosong ==================*/
		if($ItemCategoryID != ""){
			if($ItemCategoryID == "All"){
				
			}else{
				$this->db->like('am_item.ItemCategoryID', $ItemCategoryID);
			}
		}
		if($ItemCode != ''){$this->db->like('am_item.ItemCode', $ItemCode);}
		if($ItemDesc != ''){$this->db->like('am_item.ItemDesc', $ItemDesc);}
		/*====================================end======================================================================================*/
		
		$this->db->order_by('am_item.ItemCode', 'ASC');
		$this->db->limit($end, $beginning);
		$this->db->_protect_identifiers = false;
		
		return $this->db->get()->result();
    }
	
	function search_all_item_by_kategori_no_asset(){
		/*======================================scrol view 1-150=======================================*/
		$page		= ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
		$rows		= ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;
		
		$beginning 	= ($page - 1) * $rows;
		$end 		= $page * $rows;
		/*==============================================================================================*/
		
		/*=================================post dari find di atas pop up====================================================*/
		$param 				= $this->input->post('param');
		$ItemCategoryID		= isset($param['cmb_ItemCategoryID']) ? $param['cmb_ItemCategoryID'] : '';
		$ItemCode			= isset($param['txt_ItemCode']) ? $param['txt_ItemCode'] : '';
		$ItemDesc			= isset($param['txt_ItemDesc']) ? $param['txt_ItemDesc'] : '';
		$AccounNumber 		= $this->input->post('AccounNumber');
		$Akun_search		= isset($param['akun_number']) ? $param['akun_number'] : '';
		/*============================================end============================================================*/
		
        /*=========================ny session====================================================================*/
		$sess_branch    = $this->session->userdata('branch');
		/*=============================end======================================================================*/
		
		$this->db->select('am_item.ItemCode,am_item.ItemDesc, am_item.ItemUnit, 
							at_inventories.ItemQty, at_inventories.ItemAverage, at_inventories.ItemValue, am_itemcategory.ItemCategoryName,
							am_itemcategory.ItemCategoryID, at_inventories.LockStatus as IsLock');
		$this->db->from('am_itemcategory');
		$this->db->join('am_item', 'am_item.ItemCategoryID = am_itemcategory.ItemCategoryID', 'inner');
		$this->db->join('at_inventories', 'at_inventories.ItemCode = am_item.ItemCode', 'inner');
		$this->db->join('sys_itemcategory', 'sys_itemcategory.ItemCategoryID = am_itemcategory.ItemCategoryID', 'inner');
		$this->db->join('sys_itemaccount', 'sys_itemaccount.SysItemCategoryID = sys_itemcategory.SysItemCategoryID', 'inner');
		
		/*$explode_arr 	= array();
		$explode_arr 	= explode(",", $item_categori);
		$var_array 		= implode(",", $explode_arr);*/

		$this->db->where('at_inventories.BranchCode', $sess_branch);
		$this->db->where_in('am_item.TransStatus', array(0,1));
		$this->db->where('am_itemcategory.AssetStatus', 0);
		
		if($Akun_search != ""){
			$this->db->where_in('sys_itemaccount.AccountNumber', $Akun_search);
		}
		else{
			$this->db->where_in('sys_itemaccount.AccountNumber', $AccounNumber);
		}
		
		$this->db->where('sys_itemcategory.CompanyCode', $this->session->userdata('company'));
		
		/*===============================================pencarian pas pop up jika kondisi tidak sama dengan kosong ==================*/
		if($ItemCategoryID != ""){
			if($ItemCategoryID == "All"){
				
			}else{
				$this->db->like('am_item.ItemCategoryID', $ItemCategoryID);
			}
		}
		if($ItemCode != ''){$this->db->like('am_item.ItemCode', $ItemCode);}
		if($ItemDesc != ''){$this->db->like('am_item.ItemDesc', $ItemDesc);}
		/*====================================end======================================================================================*/
		
		$this->db->order_by('am_item.ItemCode', 'ASC');
		$this->db->limit($end, $beginning);
		
		return $this->db->get()->result();
    }
	
	
	function search_all_item_master_by_kategori($BranchCode_Cmb){
		/*======================================scrol view 1-150=======================================*/
		
		
		$page		= ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
		$rows		= ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;
		
		$beginning 	= ($page - 1) * $rows;
		$end 		= $page * $rows;
		/*==============================================================================================*/
		
		/*=================================post dari find di atas pop up====================================================*/
		$param 				= $this->input->post('param');
		$ItemCategoryID		= isset($param['cmb_ItemCategoryID']) ? $param['cmb_ItemCategoryID'] : '';
		$ItemCode			= isset($param['txt_ItemCode']) ? $param['txt_ItemCode'] : '';
		$ItemDesc			= isset($param['txt_ItemDesc']) ? $param['txt_ItemDesc'] : '';
		/*============================================end============================================================*/
		
        /*=========================ny session====================================================================*/
		$sess_branch    = $this->session->userdata('branch');
		/*=============================end======================================================================*/
		
		if($ItemCode != ''){
			$ItemCodeAnd = "AND am_item.ItemCode LIKE '%$ItemCode%'";
			$branch_code	= isset($param['txt_Branch']) ? $param['txt_Branch'] : '';
		}
		else{
			$ItemCodeAnd = "";
			$branch_code = $BranchCode_Cmb;
		}
		
		if($ItemDesc != ''){
			$ItemDescAnd = "AND am_item.ItemDesc LIKE '%$ItemDesc%'";
			$branch_code	= isset($param['txt_Branch']) ? $param['txt_Branch'] : '';
		}
		else{
			$ItemDescAnd = "";
			$branch_code = $BranchCode_Cmb;
		}
		
		if($ItemCategoryID != ''){
			if($ItemCategoryID == "All"){
				$ItemCategoryIDAnd = "";
			}else{
				$ItemCategoryIDAnd = "AND am_item.ItemCategoryID = '$ItemCategoryID'";
			}
			$branch_code	= isset($param['txt_Branch']) ? $param['txt_Branch'] : '';
		}
		else{
			$ItemCategoryIDAnd = "";
			$branch_code = $BranchCode_Cmb;
		}
		
		$not_in = "SELECT am_item.ItemCode, am_item.ItemDesc, am_item.ItemUnit, am_itemcategory.ItemCategoryID,am_itemcategory.ItemCategoryName ,am_item.ItemTypeID
					FROM am_itemcategory 
					INNER JOIN am_item
						ON am_item.ItemCategoryID=am_itemcategory.ItemCategoryID
					WHERE am_item.ItemCode NOT IN (SELECT at_inventories.ItemCode FROM at_inventories WHERE at_inventories.BranchCode = '$branch_code') 
					AND am_item.TransStatus IN (0,1) $ItemCodeAnd $ItemDescAnd $ItemCategoryIDAnd
					ORDER BY am_item.ItemCode ASC LIMIT $beginning, $end";
		return $this->db->query($not_in)->result();
    }

    function m_checking_payment_save($tbl,$tbl_detail,$where,$join){

        $this->db->select($tbl_detail.'.*');
        $this->db->from($tbl);
        $this->db->join($tbl_detail,$join,'inner');
        $this->db->where($where);
        $query = $this->db->get()->result_array();
        return $query;
    }

    function m_checking_payment($tbl,$where){

        $this->db->where($where);
        $query = $this->db->get($tbl)->result_array();
        return $query;
    }

    function m_checking_approval($no_transaksi){
        $this->db->select('TransStatus');
        $this->db->where('TransNo',$no_transaksi);
        $query = $this->db->get($this->av_check_list_app)->result_array();
        return $query;
    }

    function m_checking_reverse($no_transaksi){
        $this->db->select('TransStatus');
        $this->db->where('TransNo',$no_transaksi);
        $query = $this->db->get($this->av_check_list_app)->result_array();
        return $query;
    }

    function m_checking_suspend($no_transaksi){
        $this->db->select('TransStatus');
        $this->db->where('TransNo',$no_transaksi);
        $this->db->where_in('TransStatus',array(0,4,5,6,7));
        $query = $this->db->get($this->av_approval_listing)->result_array();
        return $query;
    }

    function m_checking_update($no_transaksi,$status){
        $this->db->select('TransStatus');
        $this->db->where('TransNo',$no_transaksi);
        $this->db->where('TransStatus',$status);
        $query = $this->db->get($this->av_approval_listing)->result_array();
        return $query;
    }

    function m_get_bank_balance($branch,$AccountNumber){

        $month      = abs(date('m'));

        $this->db->select("(CASE WHEN 
                (acc.AccountTypeName='Revenue') or (acc.AccountTypeName='COGS / COGM (Cost Of Goods Made)') 
                or (acc.AccountTypeName='Expense') or (acc.AccountTypeName='Other Revenue')
                or (acc.AccountTypeName='Other Expense') THEN GLSaldoBalance$month ELSE GLSaldoBSheet END) as SaldoBSheet");
        $this->db->from($this->compaccountgl.' as ac');
        $this->db->join($this->accountgl.' as ag','ag.AccountNumber=ac.AccountNumber','inner');
        $this->db->join($this->accountsubtype.' as sub','sub.AccountSubTypeID=ag.AccountSubTypeID','inner');
        $this->db->join($this->accounttype.' as acc','acc.AccountTypeID=sub.AccountTypeID','inner');
        $this->db->where('ac.AccountNumber',$AccountNumber);
        $this->db->where('ac.BranchCode',$branch);
        $this->db->where('ac.GLSaldoYears',date('Y'));
        $this->db->where('ac.TransStatus',2);

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

        if(count($query) > 0){
            return $query[0]['SaldoBSheet'];
        }else{
            return false;
        }
    }

    function m_get_master_rate($company,$CurrencyCode){
        $this->db->select('CurrencyRate');
        $this->db->from($this->currency);
        $this->db->where('CurrencyCode',$CurrencyCode);
        $this->db->where('CompanyCode',$company);

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

        if(count($query) > 0){
            return $query[0]['CurrencyRate'];
        }else{
            return false;
        }
    }

    function get_total_paging($nm_table,$branch,$from_date,$end_date){
        $this->db->where('BranchCode',$branch);
        $this->db->where('CreatedAt >=',$from_date);
        $this->db->where('CreatedAt <=',$end_date);
        $this->db->order_by('CreatedAt','ASC');
        $count_data = $this->db->count_all_results($nm_table);
        return $count_data;
    }

    function get_all_data($nm_table,$nm_field,$branch,$from_date,$end_date){
        $this->db->select($nm_field.' as TransNumber');
        $this->db->where('BranchCode',$branch);
        $this->db->where('CreatedAt >=',$from_date);
        $this->db->where('CreatedAt <=',$end_date);
        $this->db->order_by('CreatedAt','ASC');

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

    function get_nextprev_transnumber($nm_table,$nm_field,$branch,$CreatedAt,$order){
        $this->db->select($nm_field.' as TransNumber');
        $this->db->where('BranchCode',$branch);
        if($order == 'ASC'){
            $this->db->where('CreatedAt >',$CreatedAt);
        }else{
            $this->db->where('CreatedAt <',$CreatedAt);
        }

        $this->db->order_by('CreatedAt',$order);
        $this->db->limit(1,0);

        $query  = $this->db->get($nm_table)->result_array();
        if(count($query)>0){
            return $query[0]['TransNumber'];
        }else{
            return 'none';
        }
    }

    function checking_transstatus($no_transaksi){
        $this->db->select('TransStatus');
        $this->db->where('TransNo',$no_transaksi);
        $this->db->where('TransStatus',1);
        $query = $this->db->get($this->av_check_list_app)->result_array();
        return $query;
    }
	
	/*
		Check Reverse 
		Apakah Reverse sudah di proses di transaksi sebelumnya
		Cretad By Fachri
	*/
	function check_item_already_used($itemcode, $branch, $table){
		$this->db->select('ItemCode, TransNumber, TransName,Branch');
		$this->db->from($this->av_item_already);
		$this->db->where('ItemCode',$itemcode);
		$this->db->where('Branch',$branch);
		$this->db->where('TransName !=',$table);
		$query = $this->db->get();

		if ($query->num_rows() > 0){
            return $query->row();
        }
        else{
            return false;
        }
	}
	
	function check_company_date($periodend){
		$this->db->select('com.CompanyDate');
		$this->db->from($this->company.' as com');
		$this->db->where('com.CompanyCode',$this->session->userdata('company'));
		
		$query = $this->db->get();

		if ($query->num_rows() > 0){
            return $query->row();
        }
        else{
            return false;
        }
	}
	
	function check_period_end_custom_date($periodend){
		$this->db->select('per.PeriodEnd, com.CompanyDate');
		$this->db->from($this->period.' as per');
		$this->db->join($this->company.' as com','com.CompanyCode=per.CompanyCode','inner');
		 
		$this->db->where('per.PeriodEnd',$periodend);
		$this->db->where('per.CompanyCode',$this->session->userdata('company'));
		
		$query = $this->db->get();

		if ($query->num_rows() > 0){
            return $query->row();
        }
        else{
            return false;
        }
	}
	
	function check_period_end_custom($periodend){
		$this->db->select('PeriodEnd');
		$this->db->from($this->period);
		$this->db->where('PeriodEnd',$periodend);
		$this->db->where('CompanyCode',$this->session->userdata('company'));

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

		if ($query->num_rows() > 0){
            return true;
        }
        else{
            return false;
        }
	}
	
	function check_item_reverse_si($id,$table_group,$table_detail,$field_number,$field_number_new){
		if(($table_group == 'at_purchasepayment')){
			$Field_data = 'PPayTotalAmnt';
		}
		else if(($table_group == 'at_salesreceipt')){
			$Field_data = 'SRecTotalAmnt';
		}
		else{
			$Field_data = 'ItemQty';
		}
		
        $this->db->select($table_group.'.TransStatus,'.$table_detail.'.'.$Field_data.','.$table_detail.'.'.$field_number);
        $this->db->from($table_detail);
		$this->db->join($table_group, $table_detail.'.'.$field_number.'='.$table_group.'.'.$field_number, 'INNER');
	    
	    $this->db->where($table_detail.'.'.$field_number_new,$id);
        $this->db->where_in($table_group.'.TransStatus', array(1,2));
		$this->db->group_by($table_detail.'.'.$field_number);
		
        $query = $this->db->get();
        
        if ($query->num_rows() > 0){
            return true;
        }
        else{
            return false;
        }
    }
	
	function check_item_reverse($id,$table_group,$table_detail,$field_number,$field_number_new){
		if(($table_group == 'at_purchasepayment')){
			$Field_data = 'PPayTotalAmnt';
		}
		else if(($table_group == 'at_salesreceipt')){
			$Field_data = 'SRecTotalAmnt';
		}
		else{
			$Field_data = 'ItemQty';
		}
		
        $this->db->select($table_group.'.TransStatus,'.$table_detail.'.'.$Field_data.','.$table_detail.'.'.$field_number);
        $this->db->from($table_detail);
		$this->db->join($table_group, $table_detail.'.'.$field_number.'='.$table_group.'.'.$field_number, 'INNER');
	    
	    $this->db->where($table_detail.'.'.$field_number_new,$id);
        $this->db->where_in($table_group.'.TransStatus', array(1,2,4,5,6,8,0));
		$this->db->group_by($table_detail.'.'.$field_number);
		
        $query = $this->db->get();
        
        if ($query->num_rows() > 0){
            return true;
        }
        else{
            return false;
        }
    }
	
	function check_item_reverse_do($id,$table_group,$table_detail,$field_number,$field_number_new){
		$Field_data = 'ItemQty';
		
        $this->db->select($table_group.'.TransStatus,'.$table_detail.'.'.$Field_data.','.$table_detail.'.'.$field_number);
        $this->db->from($table_detail);
		$this->db->join($table_group, $table_detail.'.'.$field_number.'='.$table_group.'.'.$field_number, 'INNER');
	    
	    $this->db->where($table_group.'.'.$field_number_new,$id);
        $this->db->where_in($table_group.'.TransStatus', array(1,2,4,5,6,8,0));
		$this->db->group_by($table_detail.'.'.$field_number);
		
        $query = $this->db->get();
        
        if ($query->num_rows() > 0){
            return true;
        }
        else{
            return false;
        }
    }
	
	function check_dp_for_reverse($id,$table_detail, $invoice_number, $table_group, $paymen_number){
        $this->db->select($table_detail.'.'.$invoice_number);
        $this->db->from($table_detail);
	    
	    $this->db->where($table_detail.'.'.$invoice_number,$id);
		$this->db->join($table_group, $table_group.'.'.$paymen_number.'='.$table_detail.'.'.$paymen_number, 'INNER');
		
		$this->db->where_in($table_group.'.TransStatus', array(1,2,4,5,6,8,0));
		
        $query = $this->db->get();
        
        if ($query->num_rows() > 0){
            return true;
        }
        else{
            return false;
        }
    }
	/*=====================END================================*/
}