<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Revisi : Dhidhik(edit), 18 Feb 2016
 * Desc   : menambahkan function :  num_importtrans, num_importtransdetail, importItemMaster
 * 
 */
class M_import_transaction extends CI_Model {
    
	public $table_sq		 					= 'at_salesquotation';
	public $table_sq_detail 					= 'at_squodetail';
	
	public $table_so 							= 'at_salesorder';
	public $table_so_detail 					= 'at_sorddetail';
	public $table_so_dp 						= 'at_sorddetaildp';
	
	public $table_do 							= 'at_deliveryorder';
	public $table_do_detail 					= 'at_dorddetail';
	
	public $table_si 							= 'at_salesinvoice';
	public $table_si_detail 					= 'at_sinvdetail';
	
	public $table_sales_receipt 				= 'at_salesreceipt';
	public $table_receipt_detail 				= 'at_srecdetail';
	
	public $table_purchase_requsition 			= 'at_purchaserequisition';
	public $table_purchase_requsition_detail 	= 'at_preqdetail';
	
	public $table_purchase_order 				= 'at_purchaseorder';
	public $table_purchase_order_detail 		= 'at_porddetail';
	public $table_po_down_payment 				= 'at_porddetaildp';
	
	public $table_receive_item 					= 'at_receiveitem';
	public $table_receive_item_detail 			= 'at_precdetail';
	
	public $table_purchase_invoice 				= 'at_purchaseinvoice';
	public $table_purchase_invoice_detail 		= 'at_pinvdetail';
	
	public $table_purchase_payment 				= 'at_purchasepayment';
	public $table_purchase_payment_detail 		= 'at_ppaydetail';
	
	public $am_item 							= 'am_item';
	public $sys_servis 							= 'sys_servicedetail';
	public $am_item_detail 						= 'am_itemdetail';
	public $am_fixedassettype 					= 'at_inventdetail';
	public $at_inventories 						= 'at_inventories';
	public $am_customer 						= 'am_customer';
	public $am_vendor 							= 'am_vendor';
	
	
	function get_all_history(){
		/*$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 20;
		$offset = ($page-1) * $rows;
		
		$result = array();
		$result['total'] = $this->db->get('ah_importtrans')->num_rows();
		$row = array();*/
		
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'ImportTransID';
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';
		
		$this->db->select("ah_importtrans.*, am_employee.EmployeeName, am_department.DepartmentName");
		$this->db->from('ah_importtrans');
		$type = $this->input->post('type');
		$this->db->join('am_employee', 'am_employee.EmployeeCode = ah_importtrans.LastUser', 'INNER');
		$this->db->join('am_department', 'am_department.DepartmentCode = am_employee.DepartmentCode', 'INNER');
		
		if($type == 'so'){
		    $this->db->where("ah_importtrans.TransType", 'SO');
		}
		elseif($type == 'sq'){
		     $this->db->where("ah_importtrans.TransType", 'SQ');
		}
		elseif($type == 'preq'){
		    $this->db->where("ah_importtrans.TransType", 'PR');
		}
		elseif($type == 'po'){
		    $this->db->where("ah_importtrans.TransType", 'PO');
		}
		
		elseif($type == 'Item_no'){
		    $this->db->where("ah_importtrans.TransType", 'Item');
		}
		elseif($type == 'Item_noninventories'){
		    $this->db->where("ah_importtrans.TransType", 'Item_Expense');
		}
		elseif($type == 'Fix_asset'){
		    $this->db->where("ah_importtrans.TransType", 'Asset');
		}
		
		elseif($type == 'Cust'){
		    $this->db->where("ah_importtrans.TransType", 'Customer');
		}
		elseif($type == 'Vendor'){
		    $this->db->where("ah_importtrans.TransType", 'Vendors');
		}
		elseif($type == 'Item_inventories'){
		    $this->db->where("ah_importtrans.TransType", 'Inventories');
		}
		elseif($type == 'Ar_transaction'){
		    $this->db->where("ah_importtrans.TransType", 'Ar_Transaction');
		}
		elseif($type == 'Ap_transaction'){
		    $this->db->where("ah_importtrans.TransType", 'Ap_Transaction');
		}
		elseif($type == 'ri'){
		    $this->db->where("ah_importtrans.TransType", 'RI');
		}
		//$this->db->limit($rows,$offset);
		$this->db->order_by($sort,$order);
		return json_encode($this->db->get()->result());
	}
	
	function get_detail_history($id, $trans_type){
		/*$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 20;
		$offset = ($page-1) * $rows;
		
		$result = array();
		$result['total'] = $this->db->get('ah_importtrans')->num_rows();
		$row = array();*/
		
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'ImportTransID';
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';
		//$this->db->limit($rows,$offset);
		
		if($trans_type == "Item"){
			$this->db->select("ah_importtransdetail.*, ah_importtrans.TransType, ah_importtrans.ImportTransID, ah_importtrans.LastUser, am_item.ItemDesc");
			$this->db->from('ah_importtransdetail');
			$this->db->join('ah_importtrans', 'ah_importtrans.ImportTransID = ah_importtransdetail.ImportTransID', 'INNER');
			$this->db->join('am_item', 'am_item.ItemCode = ah_importtransdetail.SourceNumber', 'INNER');
			$this->db->where('ah_importtransdetail.TransType',$trans_type);
		}
		else if($trans_type == "Customer"){
			$this->db->select("ah_importtransdetail.*, ah_importtrans.TransType, ah_importtrans.ImportTransID, ah_importtrans.LastUser, am_customer.CustomerName");
			$this->db->from('ah_importtransdetail');
			$this->db->join('ah_importtrans', 'ah_importtrans.ImportTransID = ah_importtransdetail.ImportTransID', 'INNER');
			$this->db->join('am_customer', 'am_customer.OldCustomerCode = ah_importtransdetail.SourceNumber', 'INNER');
		}
		else if($trans_type == "Vendors"){
			$this->db->select("ah_importtransdetail.*, ah_importtrans.TransType, ah_importtrans.ImportTransID, ah_importtrans.LastUser, am_vendor.VendorName");
			$this->db->from('ah_importtransdetail');
			$this->db->join('ah_importtrans', 'ah_importtrans.ImportTransID = ah_importtransdetail.ImportTransID', 'INNER');
			$this->db->join('am_vendor', 'am_vendor.VendorCode = ah_importtransdetail.SourceNumber', 'INNER');
		}
		else if($trans_type == "PR"){
			$this->db->select("ah_importtransdetail.*, ah_importtrans.TransType, ah_importtrans.ImportTransID, ah_importtrans.LastUser");
			$this->db->from('ah_importtransdetail');
			$this->db->join('ah_importtrans', 'ah_importtrans.ImportTransID = ah_importtransdetail.ImportTransID', 'INNER');
			$this->db->join('at_purchaserequisition', 'at_purchaserequisition.PReqNumber = ah_importtransdetail.SourceNumber', 'INNER');
		}
		else if($trans_type == "Item_Expense"){
			$this->db->select("ah_importtransdetail.*, ah_importtrans.TransType, ah_importtrans.ImportTransID, ah_importtrans.LastUser, sys_servicedetail.SvcDetailUnit");
			$this->db->from('ah_importtransdetail');
			$this->db->join('ah_importtrans', 'ah_importtrans.ImportTransID = ah_importtransdetail.ImportTransID', 'INNER');
			$this->db->join('sys_servicedetail', 'sys_servicedetail.SvcDetailName = ah_importtransdetail.SourceNumber', 'INNER');
		}
		else if($trans_type == "Ar_Transaction"){
			$this->db->select("ah_importtransdetail.*, ah_importtrans.TransType, ah_importtrans.ImportTransID, ah_importtrans.LastUser");
			$this->db->from('ah_importtransdetail');
			$this->db->join('ah_importtrans', 'ah_importtrans.ImportTransID = ah_importtransdetail.ImportTransID', 'INNER');	
		}
		else if($trans_type == "Ap_Transaction"){
			$this->db->select("ah_importtransdetail.*, ah_importtrans.TransType, ah_importtrans.ImportTransID, ah_importtrans.LastUser");
			$this->db->from('ah_importtransdetail');
			$this->db->join('ah_importtrans', 'ah_importtrans.ImportTransID = ah_importtransdetail.ImportTransID', 'INNER');
			
		}
		else if($trans_type == "RI"){
			$this->db->select("ah_importtransdetail.*, ah_importtrans.TransType, ah_importtrans.ImportTransID, ah_importtrans.LastUser");
			$this->db->from('ah_importtransdetail');
			$this->db->join('ah_importtrans', 'ah_importtrans.ImportTransID = ah_importtransdetail.ImportTransID', 'INNER');
			
		}
		
		$this->db->where('ah_importtransdetail.ImportTransID',$id);
		$this->db->order_by('ah_importtransdetail.ImportNumber','ASC');
		return json_encode($this->db->get()->result());
	}
	
	function get_detail_history_inventori($id){
		/*$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 20;
		$offset = ($page-1) * $rows;
		
		$result = array();
		$result['total'] = $this->db->get('ah_importtrans')->num_rows();
		$row = array();*/
		
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'ImportTransID';
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';
		
		$this->db->select("ah_importtransdetail.*, ah_importtrans.TransType, ah_importtrans.ImportTransID, ah_importtrans.LastUser, am_item.ItemCategoryID, am_item.ItemDesc");
		$this->db->from('ah_importtransdetail');
		$this->db->join('ah_importtrans', 'ah_importtrans.ImportTransID = ah_importtransdetail.ImportTransID', 'INNER');
		$this->db->join('at_inventories', 'at_inventories.ItemCode = ah_importtransdetail.SourceNumber', 'INNER');
		$this->db->join('am_item', 'am_item.ItemCode = at_inventories.ItemCode', 'INNER');
		//$this->db->limit($rows,$offset);
		$this->db->where('ah_importtransdetail.ImportTransID',$id);
		$this->db->order_by('ah_importtransdetail.ImportNumber','ASC');
		return json_encode($this->db->get()->result());
	}
	
	function get_detail_history_asset($id){
		/*$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 20;
		$offset = ($page-1) * $rows;
		
		$result = array();
		$result['total'] = $this->db->get('ah_importtrans')->num_rows();
		$row = array();*/
		
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'ImportTransID';
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';
		
		$this->db->select("ah_importtransdetail.*, ah_importtrans.TransType, ah_importtrans.ImportTransID, ah_importtrans.LastUser, at_inventdetail.ItemCategoryID, at_inventdetail.ItemDesc");
		$this->db->from('ah_importtransdetail');
		$this->db->join('ah_importtrans', 'ah_importtrans.ImportTransID = ah_importtransdetail.ImportTransID', 'INNER');
		$this->db->join('at_inventdetail', 'at_inventdetail.AssetCode = ah_importtransdetail.SourceNumber', 'INNER');
		//$this->db->limit($rows,$offset);
		$this->db->where('ah_importtransdetail.ImportTransID',$id);
		$this->db->order_by('ah_importtransdetail.ImportNumber','ASC');
		return json_encode($this->db->get()->result());
	}
	
	
	function get_detail($id){
        $query = $this->db->get_where('ah_importtrans', array('ah_importtrans.ImportTransID' => $id));

        if($query->num_rows()>0){
            return $query->row();
        }
        else {
            return false;
        }      
    }
	
	function get_detail_journal($branch,$ImportTransID,$ItemCategoryID){
		$this->db->select("am_accountgl.AccountNumber, am_accountgl.AccountName, ah_itfassetjournal.ItfAssetSaldoDebet,ah_itfassetjournal.ItfAssetSaldoCredit, ah_itfassetjournal.AmountCurrency");
		$this->db->from('ah_itfassetjournal');
		$query = $this->db->join('am_accountgl',"am_accountgl.AccountNumber=ah_itfassetjournal.ItfAssetAccountNumber","INNER");
		//$query = $this->db->join('at_inventdetail',"at_inventdetail.ItemCategoryID=ah_itfassetjournal.ItemCategoryID","INNER");
		$query = $this->db->join('am_branch',"am_branch.BranchCode=ah_itfassetjournal.BranchCode","INNER");
		$query = $this->db->join('am_company',"am_company.CompanyCode=am_branch.CompanyCode","INNER");
		
		//$this->db->limit($rows,$offset);
		$this->db->where('ah_itfassetjournal.BranchCode',$branch);
		$this->db->where('ah_itfassetjournal.ImportTransID',$ImportTransID);
		$this->db->order_by('ah_itfassetjournal.ItfAssetNumber','ASC');
		
        return $this->db->get()->result();      
    }
	
	function get_detail_journal_inventori($branch,$ImportTransID,$ItemCategoryID){
		$this->db->select("am_accountgl.AccountNumber, am_accountgl.AccountName, ah_ititemjournal.ITItemSaldoDebet, ah_ititemjournal.ITItemSaldoCredit");
		$this->db->from('ah_ititemjournal');

		$query = $this->db->join('am_accountgl',"am_accountgl.AccountNumber=ah_ititemjournal.ITItemAccountNumber","INNER");
		$query = $this->db->join('am_branch',"am_branch.BranchCode=ah_ititemjournal.BranchCode","INNER");
		$query = $this->db->join('am_company',"am_company.CompanyCode=am_branch.CompanyCode","INNER");
		
		//$this->db->limit($rows,$offset);
		$this->db->where('ah_ititemjournal.BranchCode',$branch);
		$this->db->where('ah_ititemjournal.ImportTransID',$ImportTransID);
		//$this->db->where('ah_ititemjournal.ItemCategoryID',$ItemCategoryID);
		$this->db->order_by('ah_ititemjournal.ITItemNumber','ASC');
		
        return $this->db->get()->result();
    }	
	
	function insert_sq($data){
		return $this->db->insert($this->table_sq, $data);
	}
	
	function insert_sq_detail($data){
		$this->db->insert($this->table_sq_detail, $data);
		$insert_id = $this->db->insert_id();
   		return  $insert_id;
	}
	
	function insert_so($data){
		return $this->db->insert($this->table_so, $data);
	}
	
	function insert_so_detail($data){
		return $this->db->insert($this->table_so_detail, $data);
	}
	
	function insert_so_down_payment($data){
		return $this->db->insert($this->table_so_dp, $data);
	}
	
	function insert_do($data){
		return $this->db->insert($this->table_do, $data);
	}
	
	function insert_do_detail($data){
		return $this->db->insert($this->table_do_detail, $data);
	}
	
	function insert_si($data){
		return $this->db->insert($this->table_si, $data);
	}
	
	function insert_si_detail($data){
		return $this->db->insert($this->table_si_detail, $data);
	}
	
	function insert_history_transaction($data_hisimport){
		$this->db->insert('ah_importtrans', $data_hisimport);
	}
	
	function insert_sales_receipt($data){
		return $this->db->insert($this->table_sales_receipt, $data);
	}
	
	function insert_receipt_detail($data){
		return $this->db->insert($this->table_receipt_detail, $data);
	}
	
	function insert_purchase_requisition($data){
		$this->db->insert($this->table_purchase_requsition, $data);
	}
	
	function insert_purchase_requisition_detail($data){
		$this->db->insert($this->table_purchase_requsition_detail, $data);
		$insert_id = $this->db->insert_id();
   		return  $insert_id;
	}
	
	function insert_purchase_order($data){
		$this->db->insert($this->table_purchase_order, $data);
	}
	
	function insert_po_detail($data){
		return $this->db->insert($this->table_purchase_order_detail, $data);
	}
	
	function insert_po_down_payment($data){
		return $this->db->insert($this->table_po_down_payment, $data);
	}
	
	function insert_receive_item($data){
		return $this->db->insert($this->table_receive_item, $data);
	}
	
	function insert_receive_item_detail($data){
		return $this->db->insert($this->table_receive_item_detail, $data);
	}
	
	function insert_purchase_invoice($data){
		return $this->db->insert($this->table_purchase_invoice, $data);
	}
	
	function insert_purchase_invoice_detail($data){
		return $this->db->insert($this->table_purchase_invoice_detail, $data);
	}
	
	function insert_purchase_payment($data){
		return $this->db->insert($this->table_purchase_payment, $data);
	}
	
	function insert_purchase_payment_detail($data){
		return $this->db->insert($this->table_purchase_payment_detail, $data);
	}
	function insert_purchase_payment_detail_new($data_detail){
		return $this->db->insert($this->table_purchase_payment_detail, $data_detail);
	}
	
	//fachri
	function insert_purchase_invoice_detail_new($data_detail){
		return $this->db->insert($this->table_purchase_invoice_detail, $data_detail);
	}
	
	function insert_sys_servis($data){
		$this->db->insert($this->sys_servis, $data);
	}
	
	function insert_am_item($data){
		$this->db->insert($this->am_item, $data);
	}
	
	function insert_am_item_detail($data){
		return $this->db->insert($this->am_item_detail, $data);
	}
	
	function am_fixedassettype($data){
		$this->db->insert($this->am_fixedassettype, $data);
	}
	
	function insert_am_customer($data){
		return $this->db->insert($this->am_customer, $data);
	}
	
	function insert_at_inventories($data){
		return $this->db->insert($this->at_inventories, $data);
	}
	
	function insert_ah_importtrans_detail($data){
		return $this->db->insert('ah_importtransdetail', $data);
	}
	
	function insert_journal_at_itfassetjournal($data){
		$this->db->insert('ah_itfassetjournal', $data);
	}
	
	function insert_jv_group($data){
		$this->db->insert('at_journalvoucher', $data);
	}
	
	function insert_jv_detail($data){
		$this->db->insert('at_jvchdetail', $data);
	}
	
	function insert_jv_journal($data){
		$this->db->insert('at_jvchjournal', $data);
	}
	
	function insert_journal_at_ititemjournal($data){
		$this->db->insert('ah_ititemjournal', $data);
	}
	
	function insert_am_vendor($data){
		return $this->db->insert($this->am_vendor, $data);
	}
	//fachri
	
	function update_detail_item_code($data_detail, $PRecNumber, $ItemCode){
		 $this->db->update('at_precdetail', $data_detail, array('PRecNumber' => $PRecNumber, 'ItemCode' => $ItemCode));
	}
	
	function taxes($Ppn){
		$this->db->select('TaxesCode, TaxesRate');
		$this->db->from('am_taxes');
		$this->db->where('am_taxes.TaxesName', $Ppn);
		return $this->db->get()->row();	
	}
	
	function taxes_code($Ppn_kode){
		$this->db->select('TaxesRate');
		$this->db->from('am_taxes');
		$this->db->where('am_taxes.TaxesCode', $Ppn_kode);
		return $this->db->get()->row();	
	}
	
	function get_am_state($state){
		$this->db->select('StateCode, StateName, CountryCode');
		$this->db->from('am_state');
		$this->db->where('am_state.StateCode', $state);
		return $this->db->get()->row();	
	}
	
	function get_am_city($Wilayah){
		$this->db->select('CityName, CityCode, StateCode');
		$this->db->from('am_city');
		$this->db->where('am_city.CityName', $Wilayah);
		return $this->db->get()->row();	
	}
	
	function get_am_country($Country){
		$this->db->select('CountryName, CountryCode');
		$this->db->from('am_country');
		$this->db->where('am_country.CountryName', $Country);
		return $this->db->get()->row();	
	}
	
	function get_branch_code($BranchCode){
        $this->db->select('BranchCode');
        $this->db->from('am_branch');
        $this->db->where('am_branch.CompanyCode', $this->session->userdata("company"));
        $this->db->where('am_branch.BranchCode', $BranchCode);
        $query = $this->db->get();

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

            return $BranchCode;
        }
        else {
            return FALSE;
        }
    }
	
	function get_item_master($ItemDesc){
        $this->db->select('am_item.ItemCode,am_item.ItemDesc, am_item.ItemCategoryID, am_item.ItemUnit, am_itemcategory.ItemCategoryName as ItemCategoryCode');
        $this->db->from('am_item');
		$this->db->join('am_itemcategory','am_itemcategory.ItemCategoryID=am_item.ItemCategoryID','INNER');
		$this->db->where('am_item.TransStatus', 1);
        $this->db->where('am_item.ItemDesc', $ItemDesc);
        $query = $this->db->get();

       if ($query->num_rows() > 0) {
            return $query->row();
        }
        else {
            return FALSE;
        }
    }
	
	function get_item_expense($ItemDesc){
        $this->db->select('SvcDetailName as ItemCode,SvcDetailDesc as ItemDesc,SvcCategoryID as ItemCategoryID, SvcDetailUnit as ItemUnit, SvcCategoryID as ItemCategoryCode');
        $this->db->from('sys_servicedetail');
        $this->db->where('sys_servicedetail.SvcDetailName', $ItemDesc);
        $query = $this->db->get();

       if ($query->num_rows() > 0) {
            return $query->row();
        }
        else {
            return FALSE;
        }
    }
	
	function get_pordetail_id($pord_number, $itemCode){
		$this->db->select('at_porddetail.POrdDetailID,at_porddetail.POrdNumber');
        $this->db->from('at_porddetail');
		//$this->db->join('at_receiveitem','at_receiveitem.POrdNumber=at_porddetail.POrdNumber','INNER');
        $this->db->where('at_receiveitem.POrdNumber', $pord_number);
		$this->db->where('at_porddetail.ItemCode', $itemCode);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
	}
	
	function get_preq_num($branch_code, $PReqNumber){
		$this->db->select('PReqNumber, PReqDate');
		$this->db->from('at_purchaserequisition');
		$this->db->where('at_purchaserequisition.BranchCode', $branch_code);
		$this->db->where('at_purchaserequisition.OldPReqNumber', $PReqNumber);
		return $this->db->get()->row();
	}
	
	function get_po_num($BranchCode, $OldPurchaseNo){
		$this->db->select('POrdNumber, POrdSubTotal, POrdPPn10, POrdTotalAmount');
		$this->db->from('at_purchaseorder');
		$this->db->where('at_purchaseorder.BranchCode', $BranchCode);
		$this->db->where('at_purchaseorder.OldPOrdNumber', $OldPurchaseNo);
		$this->db->where('at_purchaseorder.TransStatus', 2);
		return $this->db->get()->row();
	}
	
	function get_ri_num($BranchCode, $OldReceiveNo){
		$this->db->select('PRecNumber, PRecSubTotal, PRecPPn10, PRecTotalAmount');
		$this->db->from('at_receiveitem');
		$this->db->where('at_receiveitem.BranchCode', $BranchCode);
		$this->db->where('at_receiveitem.OldReceiveNumber', $OldReceiveNo);
		$this->db->where('at_receiveitem.TransStatus', 1);
		return $this->db->get()->row();
	}
	
	function get_sreq_num($BranchCode, $So_number){
		$this->db->select('*');
		$this->db->from('at_salesquotation');
		$this->db->where('at_salesquotation.BranchCode', $BranchCode);
		$this->db->where('at_salesquotation.OldSQuoNumber', $So_number);
		$this->db->where('at_salesquotation.TransStatus', 1);
		return $this->db->get()->row();
	}
	
	function get_sord_num($BranchCode, $So_number){
		$this->db->select('*');
		$this->db->from('at_salesorder');
		$this->db->where('at_salesorder.BranchCode', $BranchCode);
		$this->db->where('at_salesorder.OldSOrdNumber', $So_number);
		$this->db->where('at_salesorder.TransStatus', 1);
		return $this->db->get()->row();
	}
	
	function get_preq_detail($branch_code, $OldPReqNumber){
		$this->db->select('at_preqdetail.ItemQtyRI, at_preqdetail.ItemQtyPO, at_preqdetail.PReqDetailID, at_purchaserequisition.PReqNumber, at_purchaserequisition.PReqDate');
		$this->db->from('at_purchaserequisition');
		$this->db->join('at_preqdetail','at_purchaserequisition.PReqNumber=at_preqdetail.PReqNumber','INNER');
		
		$this->db->where('at_purchaserequisition.BranchCode', $branch_code);
		$this->db->where('at_purchaserequisition.OldPReqNumber', $OldPReqNumber);
		$this->db->where('at_purchaserequisition.TransStatus', 1);
		return $this->db->get()->row();
	}
	
	function get_customer_code($BranchCode, $OldCustomerCode){
		$this->db->select('CustomerCode, CustomerName, OldCustomerCode, CustomerAddress, CurrencyCode, CurrencyRate,TermsPaymentID');
		$this->db->from('am_customer');
		$this->db->where('am_customer.BranchCode', $BranchCode);
		$this->db->where('am_customer.OldCustomerCode', $OldCustomerCode);
		return $this->db->get()->row();
	}
	
	function get_currency_code($CurrencyName){
		$this->db->select('CurrencyCode');
		$this->db->from('am_currency');
		$this->db->where('am_currency.CurrencyCode', $CurrencyName);
		$this->db->where('am_currency.TransStatus', 1);
		return $this->db->get()->row();
	}
	
	function get_departemen_code_name($Department_Touper){
		$this->db->select('DepartmentCode, DepartmentName');
		$this->db->from('am_department');
		$this->db->where('am_department.DepartmentName', $Department_Touper);
		$this->db->where('am_department.TransStatus', 1);
		$this->db->where('am_department.CompanyCode', $this->session->userdata("company"));
		return $this->db->get()->row();
	}
	
	function get_item_code($ItemCode){
		$this->db->select('am_item.ItemCode, am_item.ItemUnit, am_item.ItemDesc, am_item.ItemCategoryID, am_itemcategory.ItemCategoryName');
		$this->db->from('am_item');
		$this->db->join('am_itemcategory','am_itemcategory.ItemCategoryID=am_item.ItemCategoryID','INNER');
		$this->db->where('am_item.ItemCode', $ItemCode);
		$this->db->where('am_item.TransStatus', 1);
		$query = $this->db->get();

        if ($query->num_rows() > 0) {
           return $query->row();
        }
        else {
           return FALSE;
        }
	}
	
	function get_item_by_desc($ItemDesc){
		$this->db->select('am_item.ItemCode, am_item.ItemUnit, am_item.ItemDesc, am_item.ItemCategoryID, am_itemcategory.ItemCategoryName');
		$this->db->from('am_item');
		$this->db->join('am_itemcategory','am_itemcategory.ItemCategoryID=am_item.ItemCategoryID','INNER');
		$this->db->where('am_item.ItemDesc', $ItemDesc);
		$this->db->where('am_item.TransStatus', 1);
		$query = $this->db->get();
		
		if ($query->num_rows() > 0) {
           return $query->row();
        }
        else {
           return FALSE;
        }
	}
	
	function get_item_sys_servis($ItemDesc){
		$this->db->select('sys_servicedetail.SvcDetailName,sys_servicedetail.SvcDetailDesc, sys_servicedetail.SvcDetailUnit, sys_servicecategory.SvcCategoryID');
		$this->db->from('sys_servicedetail');
		$this->db->join('sys_servicecategory','sys_servicecategory.SvcCategoryID=sys_servicedetail.SvcCategoryID','INNER');
		$this->db->where('sys_servicedetail.SvcDetailName', $ItemDesc);
		return $this->db->get()->row();
	}
	
	function get_item_by_name($asset_name){
		$this->db->select('am_item.ItemCode, am_item.ItemUnit, am_item.ItemDesc, am_item.ItemCategoryID, am_itemcategory.ItemCategoryName');
		$this->db->from('am_item');
		$this->db->join('am_itemcategory','am_itemcategory.ItemCategoryID=am_item.ItemCategoryID','INNER');
		$this->db->where('am_item.ItemDesc', $asset_name);
		$this->db->where('am_item.TransStatus', 1);
		return $this->db->get()->row();
	}
	
	function get_account_number($BankPayment){
		$CompanyCode = $this->session->userdata('company');
        $this->db->select('AccountNumber');
        $this->db->from('at_compaccountgl');
		$this->db->where('at_compaccountgl.CompanyCode', $CompanyCode);
		 $this->db->where('at_compaccountgl.BranchCode', $this->session->userdata("branch"));
        $this->db->where('at_compaccountgl.AccountNumber', $BankPayment);
        $query = $this->db->get();

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

            return $AccountNumber;
        }
        else {
            return FALSE;
        }
    }
	
	function get_cust_billing($CustomerCode){
		$this->db->select('CustomerAddress,CustomerCode');
		$this->db->from('am_customer');
		$this->db->where('CustomerCode', $CustomerCode);
		return $this->db->get()->result();	
	}
	
	function get_dept_code($DepartmentCode){
		$this->db->select('DepartmentCode, DepartmentName');
		$this->db->from('am_department');
		$this->db->where('DepartmentName', $DepartmentCode);
		return $this->db->get()->result();	
	}
	
	function get_am_depreciatemethod($DepMethodID){
		$this->db->select('DepMethodeName, DepMethodID');
		$this->db->from('am_depreciatemethod');
		$this->db->where('am_depreciatemethod.DepMethodeName', $DepMethodID);
		return $this->db->get()->result();	
	}
	
	function get_cek_ri($PRecNumber){
        $this->db->select('PRecNumber');
        $this->db->from('at_receiveitem');
        $this->db->where('at_receiveitem.PRecNumber', $PRecNumber);
        $query = $this->db->get();

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

            return $PRecNumber;
        }
        else {
            return FALSE;
        }
    }
	
	function get_cek_po($POrdNumber){
        $this->db->select('POrdNumber');
        $this->db->from('at_purchaseorder');
        $this->db->where('at_purchaseorder.POrdNumber', $POrdNumber);
        $query = $this->db->get();

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

            return $POrdNumber;
        }
        else {
            return FALSE;
        }
    }
	
	function get_cek_precdetail($PRecNumber){
        $this->db->select('PRecDetailID');
        $this->db->from('at_precdetail');
        $this->db->where('at_precdetail.PRecNumber', $PRecNumber);
        $query = $this->db->get();

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

            return $PRecDetailID;
        }
        else {
            return FALSE;
        }
    }
	
	function get_cek_podetail($POrdNumber){
        $this->db->select('POrdDetailID');
        $this->db->from('at_porddetail');
        $this->db->where('at_porddetail.POrdNumber', $POrdNumber);
        $query = $this->db->get();

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

            return $POrdDetailID;
        }
        else {
            return FALSE;
        }
    }
	
	function get_item_unit($ItemCode){
        $this->db->select('ItemUnit');
        $this->db->from('am_item');
        $this->db->where('am_item.ItemCode', $ItemCode);
        $query = $this->db->get();

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

            return $ItemUnit;
        }
        else {
            return FALSE;
        }
    }
	
	
	
	function get_item_desc($ItemCode){
		$this->db->select('ItemDesc,ItemUnit');
		$this->db->from('am_item');
		$this->db->where('am_item.ItemCode', $ItemCode);
		return $this->db->get()->result();
	}
	
	function get_cek_preq_by_po($PreqNumber){
		$this->db->select('ItemQty, PreqNumber, PreqDetailID');
		$this->db->from('at_preqdetail');
		$this->db->where('PreqNumber', $PreqNumber);
		return $this->db->get()->result();	
	}
	
	function get_cek_sq_by_so($SQuoNumber){
		$this->db->select('ItemQty, SQuoDetailID');
		$this->db->from('at_squodetail');
		$this->db->where('SQuoNumber', $SQuoNumber);
		return $this->db->get()->result();	
	}
	
	function get_cek_po_by_inv($POrdNumber){
		$this->db->select('ItemCode, ItemQty, ItemUnit, ItemPrice, ItemAmount, POrdNumber, POrdDetailID');
		$this->db->from('at_porddetail');
		$this->db->where('POrdNumber', $POrdNumber);
		return $this->db->get()->result();	
	}
	
	function get_vendor_code($BranchCode, $OldVendorCode){
		$this->db->select('VendorCode, OldVendorCode, VendorAddress,TermsPaymentID');
		$this->db->from('am_vendor');
		$this->db->where('am_vendor.OldVendorCode', $OldVendorCode);
		$this->db->where('am_vendor.BranchCode', $BranchCode);
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
            return $query->row();
        }
        else {
            return FALSE;
        }
	}
	
	function get_purchase_req($preq_no, $itemCode){
		$this->db->select('PReqDetailID,PReqNumber');
        $this->db->from('at_preqdetail');
        $this->db->where('PReqNumber', $preq_no);
		$this->db->where('ItemCode', $itemCode);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
	}
	
	function get_employee_code($BranchCode, $SalesName){
		$this->db->select('am_employee.EmployeeCode');
		$this->db->from('am_employee');
		$this->db->where('am_employee.EmployeeName', $SalesName);
		$this->db->where('am_employee.BranchCode', $BranchCode);
		return $this->db->get()->row();
	}
	
	function get_tax_type_vendor($Tax_Type){
		$this->db->select('TaxTypeID, TaxTypeName');
		$this->db->from('am_taxtype');
		$this->db->where('am_taxtype.TaxTypeName', $Tax_Type);
		return $this->db->get()->row();
	}
	
	function get_inv_by_pp($PInvNumber){
		$this->db->select('PInvTotalAmount,PInvDiscount, VendSINumber');
		$this->db->from('at_purchaseinvoice');
		$this->db->where('VendSINumber', $PInvNumber);
		return $this->db->get()->result();	
	}
	
	function get_purchase_inv_number($PInvNumber){
		$this->db->select('PInvNumber');
        $this->db->from('at_purchaseinvoice');
        $this->db->where('at_purchaseinvoice.PInvNumber', $PInvNumber);
        $query = $this->db->get();

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

            return $PInvNumber;
        }
        else {
            return FALSE;
        }
	}
	
	function get_term_code($Term){
        $this->db->select('TermspaymentID');
        $this->db->from('am_termspayment');
        $this->db->where('am_termspayment.NetDueIn', $Term);
        $query = $this->db->get();

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

            return $TermspaymentID;
        }
        else {
            return FALSE;
        }
    }
	
	function get_vendor_addres($VendorCode){
        $this->db->select('VendorAddress');
        $this->db->from('am_vendor');
        $this->db->where('am_vendor.VendorCode', $VendorCode);
        $query = $this->db->get();

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

            return $VendorAddress;
        }
        else {
            return FALSE;
        }
    }
	
	/****************************Journal Deliveru Order*******************/
	function get_account_item_cogs($sys_item_categori){
		$this->db->select('sys_itemaccount.AccountNumber as account_cogs');
		$this->db->from('sys_itemaccount');
		$this->db->join('at_compaccountgl','sys_itemaccount.AccountNumber=at_compaccountgl.AccountNumber','INNER');
		$this->db->where('sys_itemaccount.AccountCode', 'COGS');
		$this->db->where('sys_itemaccount.SysItemCategoryID', $sys_item_categori);
		$query = $this->db->get();

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

            return $account_cogs;
        }
        else {
            return FALSE;
        }
	}
	
	function get_account_item_inventory($sys_item_categori){
		$this->db->select('sys_itemaccount.AccountNumber as account_inventory');
		$this->db->from('sys_itemaccount');
		$this->db->join('at_compaccountgl','sys_itemaccount.AccountNumber=at_compaccountgl.AccountNumber','INNER');
		$this->db->where('sys_itemaccount.AccountCode', 'Inventory');
		$this->db->where('sys_itemaccount.SysItemCategoryID', $sys_item_categori);
		$query = $this->db->get();

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

            return $account_inventory;
        }
        else {
            return FALSE;
        }
	}
	
	function get_do_date_by_number($SOrdNumber){
		$this->db->select('DOrdDate');
		$this->db->from('at_deliveryorder');
		$this->db->where('DOrdNumber', $SOrdNumber);
		return $this->db->get()->result();
	}
	
	function get_detail_sr($SrecNumber){
		$this->db->select('SInvOwing');
		$this->db->from('at_srecdetail');
		$this->db->where('SrecNumber', $SrecNumber);
		return $this->db->get()->result();
	}
	
	function insert_journal_debit_delivery_order($debit){
	    return $this->db->insert('at_dordjournal', $debit);
	}
	
	function insert_journal_credit_delivery_order($credit){
		return $this->db->insert('at_dordjournal', $credit);
	}
	
	/**********************************Categori Item Di Journal*************************************************/	
	function get_category_item($ItemCode){
		$this->db->select('ItemCategoryID');
		$this->db->from('am_item');
		$this->db->where('ItemCode', $ItemCode);
		$query = $this->db->get();

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

            return $ItemCategoryID;
        }
        else {
            return FALSE;
        }
	}
	
	function get_sys_item_category_id($item_categori){
		$CompanyCode = $this->session->userdata('company');
		$this->db->select('SysItemCategoryID');
		$this->db->from('sys_itemcategory');
		$this->db->where('ItemCategoryID', $item_categori);
		$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;
        }
	}
	
	/*************************************Journal Sales Invoice********************************************************/
	function get_sys_sales_category_id($item_categori){
		$this->db->select('SysSalesCategoryID');
		$this->db->from('sys_salescategory');
		$this->db->where('ItemCategoryID', $item_categori);
		$query = $this->db->get();

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

            return $SysSalesCategoryID;
        }
        else {
            return FALSE;
        }
	}
	
	function get_account_advance_sales($sys_sales_categori){
		$this->db->select('sys_salesaccount.AccountNumber as advance_sales');
		$this->db->from('sys_salesaccount');
		$this->db->join('at_compaccountgl','sys_salesaccount.AccountNumber=at_compaccountgl.AccountNumber','INNER');
		$this->db->where('sys_salesaccount.AccountCode', 'AdvanceSales');
		$this->db->where('sys_salesaccount.SysSalesCategoryID', $sys_sales_categori);
		$query = $this->db->get();

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

            return $advance_sales;
        }
        else {
            return FALSE;
        }
	}
	
	function get_account_item_receivable($sys_sales_categori){
		$this->db->select('sys_salesaccount.AccountNumber as account_receivable');
		$this->db->from('sys_salesaccount');
		$this->db->join('at_compaccountgl','sys_salesaccount.AccountNumber=at_compaccountgl.AccountNumber','INNER');
		$this->db->where('sys_salesaccount.AccountCode', 'SRAccountReceivable');
		$this->db->where('sys_salesaccount.SysSalesCategoryID', $sys_sales_categori);
		$query = $this->db->get();

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

            return $account_receivable;
        }
        else {
            return FALSE;
        }
	}
	
	function get_account_sales($sys_item_categori){
		$this->db->select('sys_itemaccount.AccountNumber as account_sales');
		$this->db->from('sys_itemaccount');
		$this->db->join('at_compaccountgl','sys_itemaccount.AccountNumber=at_compaccountgl.AccountNumber','INNER');
		$this->db->where('sys_itemaccount.AccountCode', 'Sales');
		$this->db->where('sys_itemaccount.SysItemCategoryID', $sys_item_categori);
		$query = $this->db->get();

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

            return $account_sales;
        }
        else {
            return FALSE;
        }
	}
	
	function get_account_item_taxable($sys_sales_categori){
		$this->db->select('sys_salesaccount.AccountNumber as account_taxable');
		$this->db->from('sys_salesaccount');
		$this->db->join('at_compaccountgl','sys_salesaccount.AccountNumber=at_compaccountgl.AccountNumber','INNER');
		$this->db->where('sys_salesaccount.AccountCode', 'SalesTaxable');
		$this->db->where('sys_salesaccount.SysSalesCategoryID', $sys_sales_categori);
		$query = $this->db->get();

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

            return $account_taxable;
        }
        else {
            return FALSE;
        }
	}
	
	function get_account_sales_disc($sys_sales_categori){
		$this->db->select('sys_salesaccount.AccountNumber as account_disc');
		$this->db->from('sys_salesaccount');
		$this->db->join('at_compaccountgl','sys_salesaccount.AccountNumber=at_compaccountgl.AccountNumber','INNER');
		$this->db->where('sys_salesaccount.AccountCode', 'InvoiceDiscount');
		$this->db->where('sys_salesaccount.SysSalesCategoryID', $sys_sales_categori);
		$query = $this->db->get();

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

            return $account_disc;
        }
        else {
            return FALSE;
        }
	}
	
	/*----akhir modul fachri-------------------------------------------------*/
	function get_purchase_account($sys_item_categori){
		$CompanyCode = $this->session->userdata('company');
		$this->db->select('sys_purchaseaccount.AccountNumber as account_taxable');
		$this->db->from('sys_purchaseaccount');
		$this->db->join('sys_purchasecategory','sys_purchaseaccount.SysPurchaseCategoryID=sys_purchasecategory.SysPurchaseCategoryID','INNER');
		$this->db->where('sys_purchaseaccount.AccountCode', 'PurchaseTaxable');
		$this->db->where('sys_purchasecategory.CompanyCode', $CompanyCode);
		$this->db->where('sys_purchasecategory.ItemCategoryID', $sys_item_categori);
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $account_taxable = $row->account_taxable;
            }

            return $account_taxable;
        }
        else {
            return FALSE;
        }
		
	}
	/*------------------akhir modul fachri------------------------*/
	
	function get_si_date_by_number($SInvNumber){
		$this->db->select('SInvDate, SInvTotalAmount, SInvSubTotal');
		$this->db->from('at_salesinvoice');
		$this->db->where('SInvNumber', $SInvNumber);
		return $this->db->get()->result();
	}
	
	function get_jurnal_si_by_no($SInvNumber){
		$this->db->select('SInvNumber, SInvAccountNumber');
		$this->db->from('at_sinvjournal');
		$this->db->where('SInvNumber', $SInvNumber);
		return $this->db->get()->result();
	}
	
	function insert_journal_debit_sales_invoice_receivable($debit_receivable){
	    return $this->db->insert('at_sinvjournal', $debit_receivable);
	}
	
	function insert_journal_credit_sales_invoice($credit_sales){
	    return $this->db->insert('at_sinvjournal', $credit_sales);
	}
	
	function insert_journal_credit_sales_invoice_taxable($credit_taxable){
	    return $this->db->insert('at_sinvjournal', $credit_taxable);
	}
	
	function insert_journal_debit_sales_invoice_hpp($debit_hpp){
	    return $this->db->insert('at_sinvjournal', $debit_hpp);
	}
	
	/**********************************************JOURNAL SALES RECEIPT************************************/
	function get_receipt_by_srnumber($SRecNumber){
		$this->db->select('*');
		$this->db->from('at_salesreceipt');
		$this->db->where('SRecNumber', $SRecNumber);
		return $this->db->get()->result();
	}
	
	function get_receipt_item_by_invoice($SInvNumber){
		$this->db->select('*');
		$this->db->from('at_sinvdetail');
		$this->db->where('SInvNumber', $SInvNumber);
		return $this->db->get()->result();	
	}
	
	function insert_journal_debit_sreceipt($debit_bank){
		return $this->db->insert('at_srecjournal', $debit_bank);
	}
	
	function insert_journal_credit_sreceipt($credit){
		return $this->db->insert('at_srecjournal', $credit);
	}
	
	function insert_journal_credit_sub_total_sreceipt($credit_sales){
		return $this->db->insert('at_srecjournal', $credit_sales);
	}
	
	/*******************************************JOURNAL RECEIVE ITEM********************************************/
	function get_sys_purchase_category_id($item_categori){
		$CompanyCode = $this->session->userdata('company');
		$this->db->select('SysPurchaseCategoryID');
		$this->db->from('sys_purchasecategory');
		$this->db->where('ItemCategoryID', $item_categori);
		$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 get_pinv_jurnal($PInvNumber){
		$this->db->select('PInvNumber');
		$this->db->from('at_pinvjournal');
		$this->db->where('PInvNumber', $PInvNumber);
		$query = $this->db->get();

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

            return $PInvNumber;
        }
        else {
            return FALSE;
        }
	}
	
	function get_customer_saldo_balance($CustomerCode){
		$this->db->select('SaldoBalance');
		$this->db->from('am_customer');
		$this->db->where('CustomerCode', $CustomerCode);
		$query = $this->db->get();

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

            return $SaldoBalance;
        }
        else {
            return FALSE;
        }
	}
	
	function get_account_item_unbilled($sys_item_categori){
		$this->db->select('sys_itemaccount.AccountNumber as account_ubilled');
		$this->db->from('sys_itemaccount');
		$this->db->join('at_compaccountgl','sys_itemaccount.AccountNumber=at_compaccountgl.AccountNumber','INNER');
		$this->db->where('sys_itemaccount.AccountCode', 'Unbilled');
		$this->db->where('sys_itemaccount.SysItemCategoryID', $sys_item_categori);
		$query = $this->db->get();

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

            return $account_ubilled;
        }
        else {
            return FALSE;
        }
	}
	/*----function fachri-------------------------------------------------*/
	function get_account_item_unbilled_new($sys_item_categori){
		$CompanyCode = $this->session->userdata('company');
		$this->db->select('sys_itemaccount.AccountNumber as account_ubilled');
		$this->db->from('sys_itemaccount');
		$this->db->join('sys_itemcategory','sys_itemaccount.SysItemCategoryID=sys_itemcategory.SysItemCategoryID','INNER');
		$this->db->where('sys_itemaccount.AccountCode', 'Unbilled');
		$this->db->where('sys_itemcategory.CompanyCode', $CompanyCode);
		$this->db->where('sys_itemcategory.SysItemCategoryID', $sys_item_categori);
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $account_ubilled = $row->account_ubilled;
            }

            return $account_ubilled;
        }
        else {
            return FALSE;
        }
		
	}
	/*------------------akhir modul fachri------------------------*/
	
	function get_sales_resip_detail($PInvNumber){
		$this->db->select('SInvNumber,SrecNumber');
		$this->db->from('at_srecdetail');
		$this->db->where('SInvNumber', $PInvNumber);
		return $this->db->get()->result();
	}
	
	function get_receive_bynumber($PRecNumber){
		$this->db->select('PRecDate, POrdNumber, PRecNumber');
		$this->db->from('at_receiveitem');
		$this->db->where('PRecNumber', $PRecNumber);
		return $this->db->get()->result();
	}
	
	function get_poamount_byponumber($POrdNumber){
		$this->db->select('POrdTotalAmount');
		$this->db->from('at_purchaseorder');
		$this->db->where('POrdNumber', $POrdNumber);
		return $this->db->get()->result();
	}

	function insert_journal_debit_receive_item($debit_item){
	    return $this->db->insert('at_precjournal', $debit_item);
	}
	
	function insert_journal_credit_receive_unbilled($credit_unbilled){
	    return $this->db->insert('at_precjournal', $credit_unbilled);
	}
	
	function insert_journal_credit_receive_taxable($credit_taxable){
	    return $this->db->insert('at_precjournal', $credit_taxable);
	}
	
	function update_inventoris($data_inventori, $itemcode){
	    return $this->db->update('at_inventories', $data_inventori, array('ItemCode' => $itemcode));
	}
	
	function update_app_finished($data, $id){
	    return $this->db->update('ah_itfassetjournal', $data, array('ItFAssetNumber' => $id));
	}
	
	function update_app_finished_inventori($data, $id){
	    return $this->db->update('ah_ititemjournal', $data, array('ITItemNumber' => $id));
	}
	
	function cek_item_oninventories($ItemCode){
		$this->db->select('at_inventories.ItemCode, at_inventories.ItemAverage, 
							at_inventories.ItemQty, am_item.ItemUnit');
		$this->db->from('at_inventories');
		$this->db->join('am_item','am_item.ItemCode=at_inventories.ItemCode','INNER');
		$this->db->where('at_inventories.ItemCode', $ItemCode);
		return $this->db->get()->row();
	}
	
	/*function cek_item_oninventories($ItemCode){
		$this->db->select('ItemCode, ItemHPP, ItemQty');
		$this->db->from('at_inventories');
		$this->db->where('ItemCode', $ItemCode);
		return $this->db->get()->num_rows();
	}
	*/
	function cek_item_oninventories_1($ItemCode_si){
		$this->db->select('ItemCode, ItemHPP, ItemQty');
		$this->db->from('at_inventories');
		$this->db->where('ItemCode', $ItemCode_si);
		return $this->db->get()->result();
	}
	
	function get_item_invent($ItemCode){
		$this->db->select('ItemCode');
		$this->db->from('at_inventories');
		$this->db->where('ItemCode', $ItemCode);
		$query = $this->db->get();

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

            return $ItemCode;
        }
        else {
            return FALSE;
        }
	}
	
	function create_initial_code_by_name($ItemCategoryID){
		//Get Last Purchase Requisition Number
        $this->db->where('am_item.ItemCategoryID',$ItemCategoryID);
        $num            = $this->db->get('am_item')->num_rows()+1;
        $long           = strlen($num);
		
        //Create PReq Number
        if($long == 1){
            $number='0000'.$num;
        }
        elseif($long == 2){
            $number='000'.$num;
        }
		elseif($long == 3){
            $number='00'.$num;
        }
		elseif($long == 4){
            $number='0'.$num;
        }
        else{
            $number=$num;
        }
		
        //Return PReq Serial + PReq Number
        return  $number;
	}
	
	function get_item_kategori_expense($ItemCategoryID_Trouper){
		$this->db->select('SvcCategoryID, SvcCategoryName, SvcCategoryDesc');
		$this->db->from('sys_servicecategory');
		$this->db->where('sys_servicecategory.SvcCategoryName',$ItemCategoryID_Trouper);
		return $this->db->get()->row();
	}
	
	function get_item_kategori($ItemCategoryID_Trouper){
		$this->db->select('ItemCategoryID, ItemCategoryName, InitialCategory');
		$this->db->from('am_itemcategory');
		$this->db->where('am_itemcategory.ItemCategoryName',$ItemCategoryID_Trouper);
		$this->db->where_in('am_itemcategory.TransStatus', array(0,1));
		return $this->db->get()->row();
	}
	
	function get_item_type($ItemTypeID){
		$this->db->select('ItemTypeName, ItemTypeID');
		$this->db->from('am_itemtype');
		$this->db->where('am_itemtype.ItemTypeName',$ItemTypeID);
		return $this->db->get()->row();
	}
	
	function get_warehouse_id($ItemCode){
		$this->db->select('ItemWarehouseID');
		$this->db->from('am_item');
		$this->db->where('ItemCode', $ItemCode);
		$query = $this->db->get();

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

            return $ItemWarehouseID;
        }
        else {
            return FALSE;
        }
	}
	
	function get_qty_inventori($ItemCode){
		$this->db->select('ItemQty');
		$this->db->from('at_inventories');
		$this->db->where('ItemCode', $ItemCode);
		$query = $this->db->get();

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

            return $ItemQty;
        }
        else {
            return FALSE;
        }
	}
	
	function create_preq_number_awal(){
	    $branch_code=$this->session->userdata('branch');
	   	$month=date('m');
		$year=date('y');
	    $preq_number=$branch_code."/".$month.$year."SR/BIM-1234/";
		$this->db->like('SRecNumber',$preq_number);
		$num=$this->db->get($this->table_sales_receipt)->num_rows()+1;
		$long=strlen($num);
		if($long==1){
		   $number='00'.$num;
		}
		elseif($long==2){
		   $number='0'.$num;
		}
		else{
			$number=$num;
		}
		
		return  $preq_number;
	}
	
	function create_preq_number_akhir(){
	    $branch_code=$this->session->userdata('branch');
	   	$month=date('m');
		$year=date('y');
	    $preq_number=$branch_code."/".$month.$year."SR/";
		$this->db->like('SRecNumber',$preq_number);
		$num=$this->db->get($this->table_sales_receipt)->num_rows()+1;
		$long=strlen($num);
		if($long==1){
		   $number='00'.$num;
		}
		elseif($long==2){
		   $number='0'.$num;
		}
		else{
			$number=$num;
		}
		return  $number;
	}
	
	function get_item_price($POrdNumber, $ItemCode){
		$this->db->select('ItemPrice');
		$this->db->from('at_porddetail');
		$this->db->where('POrdNumber', $POrdNumber);
		$this->db->where('ItemCode', $ItemCode);
		$query = $this->db->get();

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

            return $ItemPrice;
        }
        else {
            return FALSE;
        }
	}
	
	function join_akun_bank($BankReceipt){
		$this->db->select('am_currency.CurrencyCode,am_currency.CurrencyRate');
		$this->db->from('am_accountgl');
		$this->db->join('am_currency','am_accountgl.CurrencyCode=am_currency.CurrencyCode','INNER');
		$this->db->where('am_accountgl.AccountSubTypeID',6);
		$this->db->where('am_accountgl.AccountChild',1);
		$this->db->where('am_accountgl.AccountNumber', $BankReceipt);
		return $this->db->get()->result();
	}
	
	function join_sr_si($SInvNumber){
		$this->db->select('SInvTotalAmount, SInvNumber, BranchCode, SInvDate, SInvDiscount');
		$this->db->from('at_salesinvoice');
		$this->db->where('at_salesinvoice.SInvNumber',$SInvNumber);
		return $this->db->get()->result();
	}
	
	function join_si_po($CustPONumber){
		$this->db->select('SOrdNumber, CustPONumber');
		$this->db->from('at_salesorder');
		$this->db->where('at_salesorder.CustPONumber',$CustPONumber);
		return $this->db->get()->result();
	}
	
	function join_si_po_2($SOrdNumber){
		$this->db->select('ItemCode, ItemQty, 
							ItemUnit, ItemPrice,
							ItemAmount');
		$this->db->from('at_sorddetail');
		$this->db->where('at_sorddetail.SOrdNumber',$SOrdNumber);
		return $this->db->get()->result();
	}
	
	function count_serial_number(){
		$this->db->from('at_inventdetail');
		return $this->db->count_all_results();
	}
	
	function get_last_serial_number(){
		$this->db->select_max('ReceiveSN');
		$this->db->from('at_inventdetail');
		return $this->db->get()->result();
	}
	
	function get_sq($SOrdNumber){
		$this->db->select('SQuoDetailID');
		$this->db->from('at_squodetail');
		$this->db->where('SQuoNumber', $SOrdNumber);
		$query = $this->db->get();

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

            return $SQuoDetailID;
        }
        else {
            return FALSE;
        }
	}
	
	function get_sod($SOrdNumber){
		$this->db->select('SOrdDetailID');
		$this->db->from('at_sorddetail');
		$this->db->where('SOrdNumber', $SOrdNumber);
		$query = $this->db->get();

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

            return $SOrdDetailID;
        }
        else {
            return FALSE;
        }
	}
	
	function get_sales_order($SOrdNumber){
		$this->db->select('SOrdNumber,TotalAmount');
		$this->db->from('at_salesorder');
		$this->db->where('SOrdNumber', $SOrdNumber);
		return $this->db->get()->result();
	}
	
	function get_purchase_order($POrdNumber){
		$this->db->select('POrdNumber, SUM(ItemAmount) as hasil');
		$this->db->from('at_porddetail');
		$this->db->where('POrdNumber', $POrdNumber);
		return $this->db->get()->result();
	}
	
	
	function get_si($SInvNumber){
		$this->db->select('SRecNumber,SInvSubTotal');
		$this->db->from('at_salesinvoice');
		$this->db->where('SInvNumber', $SInvNumber);
		return $this->db->get()->result();
	}
	
	function get_sr($SRecNumber){
		$this->db->select('SrecNumber,ChequeAmount');
		$this->db->from('at_salesreceipt');
		$this->db->where('SrecNumber', $SRecNumber);
		return $this->db->get()->result();
	}
	
	function get_po($PInvNumber){
		$this->db->select('PInvNumber,PInvSubTotal');
		$this->db->from('at_purchaseinvoice');
		$this->db->where('PInvNumber', $PInvNumber);
		return $this->db->get()->result();
	}
	
	function update_si_paymen_amont($update_si_paymen_amont, $PInvNumber){
		return $this->db->update('at_purchaseinvoice', $update_si_paymen_amont, array('PInvNumber' => $PInvNumber));
	}
	
	function update_si($upate_si, $SInvNumber){
		return $this->db->update('at_salesinvoice', $upate_si, array('SInvNumber' => $SInvNumber));
	}
	
	function update_sr($upate_sr, $SRecNumber){
		return $this->db->update('at_salesreceipt', $upate_sr, array('SRecNumber' => $SRecNumber));
	}
	
	function update_pord($upate_pord, $POrdNumber){
		return $this->db->update('at_purchaseorder', $upate_pord, array('OldPOrdNumber' => $POrdNumber));
	}
	
	function update_sord($upate_so, $SOrdNumber){
		return $this->db->update('at_salesorder', $upate_so, array('OldSOrdNumber' => $SOrdNumber));
	}
	
	function update_pinv($upate_pinv, $PInvNumber){
		return $this->db->update('at_purchaseinvoice', $upate_pinv, array('PInvNumber' => $PInvNumber));
	}
	
	function update_sub_total($upate_sub_total, $PInvNumber){
		return $this->db->update('at_purchaseinvoice', $upate_sub_total, array('PInvNumber' => $PInvNumber));
	}
	
	function update_sub_total_so($upate_sub_total_so, $SOrdNumber){
		return $this->db->update('at_salesorder', $upate_sub_total_so, array('SOrdNumber' => $SOrdNumber));
	}
	
	function update_cus_sq($upate_cust_po, $SQuoNumber){
		return $this->db->update('at_salesquotation', $upate_cust_po, array('SQuoNumber' => $SQuoNumber));
	}
	
	/*---jurnal pinv--*/
	function update_sub_jurnal_inv_1($upate_sub_jurnal_1, $PInvNumber){
		return $this->db->update('at_pinvjournal', $upate_sub_jurnal_1, array('PInvNumber' => $PInvNumber, 'PInvAccountNumber' => '2-1-09-000-00'));
	}
	
	function update_sub_jurnal_inv_2($upate_sub_jurnal_2, $PInvNumber){
		return $this->db->update('at_pinvjournal', $upate_sub_jurnal_2, array('PInvNumber' => $PInvNumber, 'PInvAccountNumber' => '2-1-01-001-00'));
	}
	
	function update_sub_jurnal_inv_3($upate_sub_jurnal_3, $PInvNumber){
		return $this->db->update('at_pinvjournal', $upate_sub_jurnal_3, array('PInvNumber' => $PInvNumber, 'PInvAccountNumber' => '1-4-07-005-00'));
	}
	/*-------------end jurnal-*/
	
	/*-----------untuk jurnal sales invoice*/
	function update_sub_jurnal_sinv_1($upate_sub_jurnal_si_1, $SInvNumber, $account_receivable){
		return $this->db->update('at_sinvjournal', $upate_sub_jurnal_si_1, array('SInvNumber' => $SInvNumber, 'SInvAccountNumber' => $account_receivable));
	}
	
	function update_sub_jurnal_sinv_2($upate_sub_jurnal_si_2, $SInvNumber, $account_sales){
		return $this->db->update('at_sinvjournal', $upate_sub_jurnal_si_2, array('SInvNumber' => $SInvNumber, 'SInvAccountNumber' => $account_sales));
	}
	
	function update_sub_jurnal_sinv_3($upate_sub_jurnal_si_3, $SInvNumber, $account_item_taxable){
		return $this->db->update('at_sinvjournal', $upate_sub_jurnal_si_3, array('SInvNumber' => $SInvNumber, 'SInvAccountNumber' => $account_item_taxable));
	}
	/*-------------end jurnal------------*/
	
	
	function update_po($upate_po, $PInvNumber){
		return $this->db->update('at_purchaseinvoice', $upate_po, array('PInvNumber' => $PInvNumber));
	}
	
	function update_po_jurnal($debit_unbilled, $PInvNumber){
		return $this->db->update('at_pinvjournal', $debit_unbilled, array('PInvNumber' => $PInvNumber));
	}
	
	function update_sr_jurnal($upate_sr_jurnal, $SRecNumber){
		return $this->db->update('at_srecjournal', $upate_sr_jurnal, array('SRecNumber' => $SRecNumber));
	}
	
	function update_dp($upate_dp, $SInvNumber){
		return $this->db->update('at_salesinvoice', $upate_dp, array('SInvNumber' => $SInvNumber));
	}
	function update_jurnal($upate_jurnal, $SInvNumber){
		return $this->db->update('at_sinvjurnal', $upate_dp, array('SInvNumber' => $SInvNumber));
	}
	
	function update_sales_order($upate_sales, $SOrdNumber){
		return $this->db->update('at_salesorder', $upate_sales, array('SOrdNumber' => $SOrdNumber));
	}
	
	function update_inventories($data_inventories, $ItemCode){
		return $this->db->update('at_inventories', $data_inventories, array('ItemCode' => $ItemCode));
	}
	
	function update_detail_inventories($detail_inventories, $ItemCode){
		return $this->db->update('at_inventdetail', $detail_inventories, array('ItemCode' => $ItemCode));
	}
	
	function insert_inventories($data_inventories){
	    return $this->db->insert('at_inventories', $data_inventories);
	}
	
	function insert_asset_journal($data){
	    $this->db->insert('ah_itfassetjournal', $data);
	}
	
	function insert_detail_inventories($detail_inventories){
	    return $this->db->insert('at_inventdetail', $detail_inventories);
	}
	
	/***************************************************JOURNAL PURCHASE INVOICE*************************************/
	function get_account_purchase_payable($sys_purchase_categori){
		$this->db->select('sys_purchaseaccount.AccountNumber as account_purchase_payable');
		$this->db->from('sys_purchaseaccount');
		$this->db->join('sys_purchasecategory','sys_purchaseaccount.AccountNumber=at_compaccountgl.AccountNumber','INNER');
		$this->db->where('sys_purchaseaccount.AccountCode', 'Payable');
		$this->db->where('sys_purchaseaccount.SysPurchaseCategoryID	', $sys_purchase_categori);
		$query = $this->db->get();

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

            return $account_purchase_payable;
        }
        else {
            return FALSE;
        }
	}
	/*----akhir modul fachri-------------------------------------------------*/
	function get_account_purchase_payable_new($sys_purchase_categori){
		$CompanyCode = $this->session->userdata('company');
		$this->db->select('sys_purchaseaccount.AccountNumber as account_purchase_payable');
		$this->db->from('sys_purchaseaccount');
		$this->db->join('sys_purchasecategory','sys_purchaseaccount.SysPurchaseCategoryID=sys_purchasecategory.SysPurchaseCategoryID','INNER');
		$this->db->where('sys_purchaseaccount.AccountCode', 'Payable');
		$this->db->where('sys_purchasecategory.CompanyCode', $CompanyCode);
		$this->db->where('sys_purchasecategory.SysPurchaseCategoryID', $sys_purchase_categori);
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $account_purchase_payable = $row->account_purchase_payable;
            }

            return $account_purchase_payable;
        }
        else {
            return FALSE;
        }
		
	}
	/*------------------akhir modul fachri------------------------*/
	
	function get_purchase_invoice_date_by_number($PInvNumber){
		$this->db->select('PInvDate, PInvNumber');
		$this->db->from('at_purchaseinvoice');
		$this->db->where('PInvNumber', $PInvNumber);
		return $this->db->get()->result();
	}
	
	function get_piamount_bypinumber($PInvNumber){
		$this->db->select('*');
		$this->db->from('at_purchaseinvoice');
		$this->db->where('PInvNumber', $PInvNumber);
		return $this->db->get()->result();
	}
	
	/*masuk jurnal invoice fachri*/
	function insert_journal_debit_purchase_invoice_unbilled($debit_unbilled){
	    return $this->db->insert('at_pinvjournal', $debit_unbilled);
	}
	
	function insert_journal_credit_purchase_payable($credit_purchase_payable){
	    return $this->db->insert('at_pinvjournal', $credit_purchase_payable);
	}
	function insert_journal_credit_purchase_taxable($credit_taxable){
	    return $this->db->insert('at_pinvjournal', $credit_taxable);
	}
	/*-----------akhir jurnla invoice----------------------------------------------*/
	
	/***************************************************JOURNAL PURCHASE PAYMENT***********************************/
	function get_item_on_purchase_invoice($PInvNumber){
		$this->db->select('ItemCode');
		$this->db->from('at_pinvdetail');
		$this->db->where('PInvNumber', $PInvNumber);
		return $this->db->get()->result();
	}
	
	function get_item_on_purchase_invoice_jurnal($PInvNumber){
		$this->db->select('PInvNumber');
		$this->db->from('at_purchaseinvoice');
		$this->db->where('VendSINumber', $PInvNumber);
		return $this->db->get()->result();
	}
	
	function get_item_on_purchase_invoice_jurnal_detail($PInvNumber_row){
		$this->db->select('ItemCode');
		$this->db->from('at_pinvdetail');
		$this->db->where('PInvNumber', $PInvNumber_row);
		return $this->db->get()->result();
	}
	
	function get_purchase_payment_date_by_number($PPayNumber){
		$this->db->select('at_purchasepayment.PPayDate, at_purchasepayment.PPayDesc, at_purchasepayment.PPayNumber');
		$this->db->from('at_purchasepayment');
		$this->db->join('at_ppaydetail','at_purchasepayment.PPayNumber=at_ppaydetail.PPayNumber');
		$this->db->where('at_purchasepayment.PPayNumber', $PPayNumber);
		return $this->db->get()->result();
	}
	
	function get_payamount_bypaynumber($PPayNumber){
		$this->db->select('ChequeAmount');
		$this->db->from('at_purchasepayment');
		$this->db->where('PPayNumber', $PPayNumber);
		return $this->db->get()->result();
	}
	
	function get_account_bank_on_payment($PPayNumber){
		$this->db->select('BankPayment');
		$this->db->from('at_purchasepayment');
		$this->db->where('PPayNumber', $PPayNumber);
		$query = $this->db->get();

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

            return $BankPayment;
        }
        else {
            return FALSE;
        }
	}
	
	function insert_journal_debit_purchase_payment($debit_payable){
	    return $this->db->insert('at_ppayjournal', $debit_payable);
	}
	
	function insert_journal_credit_purchase_payment($credit_bank){
	    return $this->db->insert('at_ppayjournal', $credit_bank);
	}
	
	function get_fixaset_type($asset_type){
            $this->db->select('FixedAssetType,EstimatedLife, DepriciateRate');
            $this->db->from('am_fixedassettype');
            $this->db->where('am_fixedassettype.FixedAssetType',$asset_type);
            $this->db->where('am_fixedassettype.TransStatus',1);//tambahi dhidhik, 3maret 2016 
            return $this->db->get()->row();
	}
	
	function get_journal_by_number($Number){
        $this->db->_protect_identifiers = FALSE;
        $this->db->select("ah_itfassetjournal.*,am_accountgl.AccountNumber,am_accountgl.AccountName, am_branch.BranchName, am_company.CompanyName");
        $this->db->join('am_accountgl',"am_accountgl.AccountNumber=ah_itfassetjournal.ItfAssetAccountNumber","INNER");
		$this->db->join('am_branch',"am_branch.BranchCode=ah_itfassetjournal.BranchCode","INNER");
		$this->db->join('am_company',"am_company.CompanyCode=am_branch.CompanyCode","INNER");
        
		$query = $this->db->get_where('ah_itfassetjournal', array('ah_itfassetjournal.BranchCode' => $Number));
        $this->db->_protect_identifiers = TRUE;
        
        if($query->num_rows()>0){
            return $query->row();
        }
        else {
            return false;
        }
    }
	
	function get_journal_by_number_inventori($Number){
        $this->db->_protect_identifiers = FALSE;
        $this->db->select("ah_ititemjournal.*,am_accountgl.AccountNumber,am_accountgl.AccountName, am_branch.BranchName, am_company.CompanyName");
        $this->db->join('am_accountgl',"am_accountgl.AccountNumber=ah_ititemjournal.ITItemAccountNumber","INNER");
		$this->db->join('am_branch',"am_branch.BranchCode=ah_ititemjournal.BranchCode","INNER");
		$this->db->join('am_company',"am_company.CompanyCode=am_branch.CompanyCode","INNER");
        
		$query = $this->db->get_where('ah_ititemjournal', array('ah_ititemjournal.BranchCode' => $Number));
        $this->db->_protect_identifiers = TRUE;
        
        if($query->num_rows()>0){
            return $query->row();
        }
        else {
            return false;
        }
    }
	
	function get_branch_by_company($branch){
		$this->db->select('CompanyCode, BranchCode');
		$this->db->from('am_branch');
		$this->db->where('am_branch.BranchCode',$branch);
		return $this->db->get()->row();
	}
	
	function get_item_account($account_code, $item_category_id, $branch, $company){	   
        $this->db->select('SysItemCategoryID');
        $this->db->from('sys_itemcategory');
        $this->db->where('ItemCategoryID', $item_category_id);
        $this->db->where('CompanyCode', $company);
        $query = $this->db->get();
		
        if ($query->num_rows() > 0) {
            $data = $query->row();
            
            $this->db->select('sys_ia.AccountNumber, ag.AccountName');
            $this->db->from('sys_itemaccount as sys_ia');
            $this->db->join('at_compaccountgl as cg', 'cg.AccountNumber = sys_ia.AccountNumber', 'INNER');
            $this->db->join('am_accountgl as ag', 'ag.AccountNumber = cg.AccountNumber', 'INNER');
            $this->db->join('am_branch as br', 'br.BranchCode = cg.BranchCode', 'INNER');
            $this->db->where('cg.CompanyCode', $company);
            $this->db->where('cg.BranchCode', $branch);            
            $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->where('ag.CurrencyCode', 'IDR');
            $this->db->where('sys_ia.AccountCode', $account_code);
            $this->db->where('sys_ia.SysItemCategoryID', $data->SysItemCategoryID);
            $query_account = $this->db->get();
            
//            $this->db->select('AccountNumber');
//            $this->db->from('sys_itemaccount');
//            $this->db->where('AccountCode', $AccountCode);
//            $this->db->where('SysItemCategoryID', $data->SysItemCategoryID);
//            $query_account = $this->db->get();

            if ($query_account->num_rows() > 0) {
                $data = $query_account->row();

                if($data->AccountNumber != NULL){
                    return $data;
                }
                else{
                    return false;	
                }
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }			
    }
	
	function check_code_exist($new_so_number) {
        $query = $this->db->get_where('ah_itfassetjournal', array('ItFAssetNumber' => $new_so_number), 1, 0);
		
        if ($query->num_rows() > 0) {
            return TRUE;
        }
        else {
            return FALSE;
        }	
    }
	
	function create_asset_number($branch){
		
        $month          = date('m');
        $year           = date('y');
		
        $so_number	= $branch ."/". $month . $year ."FA/";
		
        $this->db->_protect_identifiers = false;
        $this->db->select('(CASE 
                              WHEN MAX(CONVERT(SUBSTR(ItFAssetNumber, -3),signed)) IS NULL then 1
                              ELSE MAX(CONVERT(SUBSTR(ItFAssetNumber, -3),signed)) + 1 END) as New_Number');
        $this->db->like('ItFAssetNumber', $so_number);
        $query = $this->db->get('ah_itfassetjournal');
        $this->db->_protect_identifiers = true;
                
        $num    = $query->row()->New_Number;
        $long   = strlen($num);
        
        if($long == 1){
            $number = '00' . $num;
        }
        elseif($long == 2){
            $number = '0' . $num;
        }
        else{
            $number = $num;
        }

        $new_so_number = $so_number . $number;

        while($this->check_code_exist($new_so_number)){
            $num++;
            $long = strlen($num);
    
            if($long == 1){
                $number = '00' . $num;
            }
            elseif($long == 2){
                $number = '0' . $num;
            }
            else{
                $number = $num;
            }
            
            $new_so_number = $so_number . $number;		
        }
        
        return  $new_so_number;
    }
	
	
	
	function create_inventori_number($branch){        
        
        $month          = date('m');
        $year           = date('y');
	
        $inventori_number    = $branch."/".$month.$year."IV/";

        $this->db->like('ITItemNumber',$inventori_number);
	
        $num    = $this->db->get('ah_ititemjournal')->num_rows()+1;
        $long   = strlen($num);
		
        if($long == 1){
            $number='00'.$num;
        }
        elseif($long==2){
            $number='0'.$num;
        }
        else{
            $number=$num;
        }
		
        return  $inventori_number.$number;
    }
    
    /********* TAMBAHAN DHIDHIK 18 FEB 2016 **********************/    
    function insert_at_inventdetail($data){        
        $this->db->insert("at_inventdetail", $data);
    }
    function update_app_finished_itfassetjournal($data, $id){
        return $this->db->update('ah_itfassetjournal', $data, array('ItfAssetNumber' => $id));
    }
	function update_app_finished_jv($data, $id){
        return $this->db->update('at_journalvoucher', $data, array('JVchNumber' => $id));
    }
	function update_app_finished_jv_journal($data, $id){
        return $this->db->update('at_jvchjournal', $data, array('JVchNumber' => $id));
    }
    function check_num_customer_code($BranchCode, $customerCode){//dhidhik, 22feb2016 , untuk mencari banyak customer code
        $query = $this->db->get_where('am_customer', array('am_customer.BranchCode' => $BranchCode,'am_customer.CustomerCode' => $customerCode));		
        if ($query->num_rows() > 0) {
            return TRUE;
        }else {
            return FALSE;
        }	
    }
    function check_num_oldcustomer_code($BranchCode, $OldCustomerCode){//dhidhik, 22feb2016 , untuk mencari banyak customer code
        $query = $this->db->get_where('am_customer', array('am_customer.BranchCode' => $BranchCode,'am_customer.OldCustomerCode' => $OldCustomerCode));		
        if ($query->num_rows() > 0) {
            return TRUE;
        }else {
            return FALSE;
        }	
    }    
    function check_num_vendor_code($BranchCode, $VendorCode){
        $query = $this->db->get_where('am_vendor', array('am_vendor.BranchCode' => $BranchCode,'am_vendor.VendorCode' => $VendorCode));		
        if ($query->num_rows() > 0) {
            return TRUE;
        }else {
            return FALSE;
        }
    }  
    function check_num_oldvendor_code($BranchCode, $OldVendorCode){
        $query = $this->db->get_where('am_vendor', array('am_vendor.BranchCode' => $BranchCode,'am_vendor.OldVendorCode' => $OldVendorCode));		
        if ($query->num_rows() > 0) {
            return TRUE;
        }else {
            return FALSE;
        }
    }    
    function check_num_item_in_inventories($BranchCode, $ItemCode){
        $query = $this->db->get_where('at_inventories', array('at_inventories.BranchCode' => $BranchCode,'at_inventories.ItemCode' => $ItemCode));		
        if ($query->num_rows() > 0) {
            return TRUE;
        }else {
            return FALSE;
        }
    }  
    function check_num_assetcode_in_invendetail($AssetCode){
        $query = $this->db->get_where('at_inventdetail', array('at_inventdetail.AssetCode' => $AssetCode));		
        if ($query->num_rows() > 0) {
            return TRUE;
        }else {
            return FALSE;
        }
    }
    function num_importtrans(){
        $num = $this->db->get('ah_importtrans')->num_rows() + 1;
        return $num;
    }
    function num_importtransdetail($type){        
        $this->db->like('TransType', $type);
        $num    = $this->db->get('ah_importtransdetail')->num_rows() + 1;
        return $num;
    }
    function get_accountnumber_by_accountname($company,$branch,$accountName){
        $this->db->select('am_accountgl.AccountNumber');
        $this->db->from('at_compaccountgl');
        $this->db->join('am_accountgl','am_accountgl.AccountNumber=at_compaccountgl.AccountNumber');
        $this->db->where('am_accountgl.AccountName', $accountName);
        $this->db->where('at_compaccountgl.CompanyCode', $company);
        $this->db->where('at_compaccountgl.BranchCode', $branch);
        $this->db->where('at_compaccountgl.GLSaldoYears', date('Y'));
        $this->db->where_in('at_compaccountgl.TransStatus', array(1,2));
        return $this->db->get()->row();
    }    
    function get_inventories($branch,$itemCode){
        $this->db->select('ItemQty,ItemValue,ItemAverage');
        $this->db->from('at_inventories');
        $this->db->where('at_inventories.ItemCode', $itemCode);
        $this->db->where('at_inventories.BranchCode', $branch);
        return $this->db->get()->row();
    }
    function loopHisimport($dataImport){
        foreach($dataImport['data_hisimport'] as $key){
            $data_hisimport = array(
                'ImportTransID' => $key['ImportTransID'],
                'TransType' 	=> $key['TransType'], 
                'DataType'      => $key['DataType'], 
                'ImportStatus' 	=> $key['ImportStatus'],
                'LastEdited' 	=> $key['LastEdited'],
                'LastUser'      => $key['LastUser']
            );
            $this->insert_history_transaction($data_hisimport);
        }  
    }
    function loopHisimportDetail($dataImport){
        foreach($dataImport['data_hisimport_detail'] as $key){
            $data_hisimport_detail = array(
                'BranchCode'        => $key['BranchCode'],
                'ImportTransID'     => $key['ImportTransID'],
                'SourceNumber'      => $key['SourceNumber'],
                'TransType'         => $key['TransType'], 
                'SourceDate'        => $key['SourceDate'],
                'ImportNumber'      => $key['ImportNumber'],
                'LastUser'          => $key['LastUser']
            );	
            $this->insert_ah_importtrans_detail($data_hisimport_detail);
        }
    }
    
    function importItemMaster($dataImport){
        $this->db->trans_start();
        $this->loopHisimport($dataImport);
        
        foreach($dataImport['data_item'] as $key){
            $data_item = array(
                'ItemCode'          => $key['ItemCode'],
                'ItemTypeID'        => $key['ItemTypeID'],
                'ItemUnit'          => $key['ItemUnit'],
                'ItemDesc'          => $key['ItemDesc'],
                'ItemCategoryID'    => $key['ItemCategoryID'],
                'AppLevel1Status'   => $key['AppLevel1Status'],
                'AppLevel1Code'     => $key['AppLevel1Code'],
                'AppLevel1Date'     => $key['AppLevel1Date'],
                'AppLevel2Status'   => $key['AppLevel2Status'],
                'AppLevel2Code'     => $key['AppLevel2Code'],
                'AppLevel2Date'     => $key['AppLevel2Date'],
                'TransStatus'       => $key['TransStatus'],
                'ItemPPhType'       => $key['ItemPPhType'],
                'LastUser'          => $key['LastUser']
            );
            $this->insert_am_item($data_item);
        }
        foreach($dataImport['data_detail'] as $key){
            $data_detail = array(
                'ItemCode'      => $key['ItemCode'],
                'ItemMerk'      => $key['ItemMerk'],
                'ItemDesign' 	=> $key['ItemDesign'],
                'ItemColour' 	=> $key['ItemColour'],
                'ItemSize'      => $key['ItemSize'],
                'ItemQuality' 	=> $key['ItemQuality'],
                'ItemType'      => $key['ItemType'],
                'ItemShading' 	=> $key['ItemShading'],
                'ItemGroup' 	=> $key['ItemGroup'],
                'LastUser'      => $key['LastUser']
            );
            $this->insert_am_item_detail($data_detail);
        }     
        foreach($dataImport['data_hisimport_detail'] as $key){
            $data_hisimport_detail = array(
                'ImportTransID'     => $key['ImportTransID'],
                'SourceNumber'      => $key['SourceNumber'],
                'TransType'         => $key['TransType'], 
                'SourceDate'        => $key['SourceDate'],
                'ImportNumber'      => $key['ImportNumber'],
                'LastUser'          => $key['LastUser']
            );	
            $this->insert_ah_importtrans_detail($data_hisimport_detail);
        }
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    
	function importItemExpense($dataImport){
        $this->db->trans_start();
        $this->loopHisimport($dataImport);
        
        foreach($dataImport['data_item'] as $key){
            $data_item = array(
                'SvcCategoryID' 	=> $key['SvcCategoryID'],
				'SvcDetailName' 	=> $key['SvcDetailName'],
				'SvcDetailDesc' 	=> $key['SvcDetailDesc'],
				'SvcDetailUnit' 	=> $key['SvcDetailUnit'],
				'SvcPPh23Status' 	=> $key['SvcPPh23Status'],
				'LastUser' 			=> $key['LastUser']
            );
            $this->insert_sys_servis($data_item);
        }     
        foreach($dataImport['data_hisimport_detail'] as $key){
            $data_hisimport_detail = array(
                'ImportTransID'     => $key['ImportTransID'],
                'SourceNumber'      => $key['SourceNumber'],
                'TransType'         => $key['TransType'], 
                'SourceDate'        => $key['SourceDate'],
                'ImportNumber'      => $key['ImportNumber'],
                'LastUser'          => $key['LastUser']
            );	
            $this->insert_ah_importtrans_detail($data_hisimport_detail);
        }
        
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
	
	function import_ar_transaction($trans_data){
        $this->db->trans_start();
		$this->insert_history_transaction($trans_data['data_hisimport']['data']);
		
		/* ===================== insert journal ================================ */
        for ($x = 0; $x < count($trans_data['data_sales_invoice']['data']); $x++) {
            $this->m_sales_invoice->insert($trans_data['data_sales_invoice']['data'][$x]);
        }
		
		for ($x = 0; $x < count($trans_data['data_detail_si']['data']); $x++) {
            $this->m_sales_invoice->insert_detail($trans_data['data_detail_si']['data'][$x]);
        }
		
		for ($x = 0; $x < count($trans_data['data_invoice_receipt']['data']); $x++) {
            $this->m_sales_invoice->insert_sinv_receipt($trans_data['data_invoice_receipt']['data'][$x]);
        }
		
		for ($x = 0; $x < count($trans_data['data_invoice_receipt']['data']); $x++) {
            $this->m_sales_invoice->insert_sinv_receipt($trans_data['data_invoice_receipt']['data'][$x]);
        }
		
		for ($x = 0; $x < count($trans_data['data_Inventori_journal']['data']); $x++) {
            $this->m_sales_invoice->insert_journal($trans_data['data_Inventori_journal']['data'][$x]);
        }
		
		for ($x = 0; $x < count($trans_data['data_receivable_journal']['data']); $x++) {
            $this->m_sales_invoice->insert_journal($trans_data['data_receivable_journal']['data'][$x]);
        }
		
		for ($x = 0; $x < count($trans_data['data_hisimport_detail']['data']); $x++) {
            $this->insert_ah_importtrans_detail($trans_data['data_hisimport_detail']['data'][$x]);
        }
		
		for ($x = 0; $x < count($trans_data['data_update_invoice_group']['data']); $x++) {
           $this->m_sales_invoice->update($trans_data['data_update_invoice_group']['data'][$x], $trans_data['update_group']['InvoiceNumber'][$x]);
        }
		
		
        /* ===================== end insert journal =============================== */

        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            return false;
        } else {
            return true;
        }
    }
	
    function importCustomerMaster($dataImport){
        //return ['result'=>true,'data'=>$dataImport];
        $this->db->trans_start();
        $this->loopHisimport($dataImport);   
        
        foreach($dataImport['data_cust'] as $key){
            $data_cust = array(
                    'BranchCode'            => $key['BranchCode'],
                    'CustomerCode'          => $key['CustomerCode'],
                    'OldCustomerCode'       => $key['OldCustomerCode'],
                    'CustomerName'          => $key['CustomerName'],
                    'CustomerAddress'       => $key['CustomerAddress'],
                    'CustTaxAddress'        => $key['CustTaxAddress'],
                    'CustomerCity'          => $key['CustomerCity'],
                    'CustomerState'         => $key['CustomerState'],
                    'CustomerCountry'       => $key['CustomerCountry'],
                    'CustomerPostalCode'    => $key['CustomerPostalCode'],
                    'PhoneNumber'           => $key['PhoneNumber'],
                    'FacsimileNumber'       => $key['FacsimileNumber'],
                    'ContactPerson'         => $key['ContactPerson'],
                    'EmailAddress'          => $key['EmailAddress'],
                    'WebAddress'            => $key['WebAddress'],
                    'TermsPaymentID'        => $key['TermsPaymentID'],
                    'CurrencyCode'          => $key['CurrencyCode'],
                    'CurrencyMemo'          => $key['CurrencyMemo'],
                    'Taxes1Code'            => $key['Taxes1Code'],
                    'Taxes2Code'            => $key['Taxes2Code'],
                    'TaxesNumber'           => $key['TaxesNumber'],
                    'PPKPNumber'            => $key['PPKPNumber'],
                    //'TaxesTypeID'         => ,
                    'TransStatus'           => $key['TransStatus'],
                    'LastUser'              => $key['LastUser']
            );	
            $this->insert_am_customer($data_cust);
        } 
        
        $this->loopHisimportDetail($dataImport);
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    
    function importVendorMaster($dataImport){
        $this->db->trans_start();
        $this->loopHisimport($dataImport); 
        
        foreach($dataImport['data_vendor'] as $key){
            $data_vendor = array(
                'BranchCode' 		=> $key['BranchCode'],
                'VendorCode' 		=> $key['VendorCode'],
                'OldVendorCode' 	=> $key['OldVendorCode'],
                'VendorName' 		=> $key['VendorName'],
                'VendorAddress' 	=> $key['VendorAddress'],
                'VendorTaxAddress' 	=> $key['VendorTaxAddress'],
                'VendorCity' 		=> $key['VendorCity'],
                'VendorState' 		=> $key['VendorState'],
                'VendorCountry' 	=> $key['VendorCountry'],
                'VendorPostalCode' 	=> $key['VendorPostalCode'],
                'PhoneNumber' 		=> $key['PhoneNumber'],
                'FacsimileNumber' 	=> $key['FacsimileNumber'],
                'ContactPerson' 	=> $key['ContactPerson'],
                'EmailAddress' 		=> $key['EmailAddress'],
                'WebAddress' 		=> $key['WebAddress'],
                'VendorCategory' 	=> $key['VendorCategory'],
                'VendorNotes' 		=> $key['VendorNotes'],
                'TermsPaymentID' 	=> $key['TermsPaymentID'],
                'CurrencyCode' 		=> $key['CurrencyCode'],
                'Tax1Code' 		=> $key['Tax1Code'],
                'VendorTaxNumber' 	=> $key['VendorTaxNumber'],
                'TaxTypeID'		=> $key['TaxTypeID'] ,
                'PKPNumber' 		=> $key['PKPNumber'],
                'VendorStatus' 		=> $key['VendorStatus'],
                'TransStatus' 		=> $key['TransStatus'],
                'LastUser' 		=> $key['LastUser']
        );	
            $this->insert_am_vendor($data_vendor);
        } 
        
        $this->loopHisimportDetail($dataImport);
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    
    function importInventories($dataImport){
        $this->db->trans_start();
        $this->loopHisimport($dataImport); 
        
        foreach($dataImport['data_inventories'] as $key){
            $data_inventories = array(
                'BranchCode'    => $key['BranchCode'],
                'ItemCode'      => $key['ItemCode'],
                'ItemQty'       => $key['ItemQty'],
                'ItemAverage'   => $key['ItemAverage'],
                'ItemValue'     => $key['ItemValue'],
                'LastUser'      => $key['LastUser']
            );
            $this->insert_at_inventories($data_inventories);
        } 
        $array_number_inventori = [];
        $array_opening          = [];
        foreach($dataImport['journal_debet'] as $key){
            $create_number_inventori  = $this->create_inventori_number($key['BranchCode']);
            $journal_debet = array(
                'BranchCode'            => $key['BranchCode'],
                'ITItemNumber'          => $create_number_inventori,
                'ItemCategoryID'        => $key['ItemCategoryID'],                
                'ImportTransID'         => $key['ImportTransID'],
                'ITItemDate'            => $key['ITItemDate'],
                'ITItemAccountNumber' 	=> $key['ITItemAccountNumber'],
                'ITItemSaldoDebet'      => $key['ITItemSaldoDebet'],
                'ITItemSaldoCredit' 	=> $key['ITItemSaldoCredit'],
                'AmountCurrency'        => $key['AmountCurrency'],
                'ITItemSignStatus'      => $key['ITItemSignStatus'],
                'LastUser'              => $key['LastUser']
            );
            $this->insert_journal_at_ititemjournal($journal_debet);
            if(!in_array($create_number_inventori, $array_number_inventori)){
                $array_number_inventori[] = $create_number_inventori;
            }
            if(!in_array($key['ItemCategoryID'], $array_opening)){
                $array_opening[$key['ItemCategoryID']] = $create_number_inventori;
            }
        } 
        
        foreach($dataImport['journal_opening_balance'] as $key){
            $journal_opening_balance = array(
                'BranchCode'            => $key['BranchCode'],
                'ITItemNumber'          => $array_opening[$key['ItemCategoryID']],//$create_number_inventori
                'ItemCategoryID'        => $key['ItemCategoryID'],                
                'ImportTransID'         => $key['ImportTransID'],
                'ITItemDate'            => $key['ITItemDate'],
                'ITItemAccountNumber'   => $key['ITItemAccountNumber'],
                'ITItemSaldoDebet'      => $key['ITItemSaldoDebet'],
                'ITItemSaldoCredit'     => $key['ITItemSaldoCredit'], //value saldo credit adalah $value
                'AmountCurrency'        => $key['AmountCurrency'],
                'ITItemSignStatus'      => $key['ITItemSignStatus'],
                'LastUser'              => $key['LastUser']
            );
            $this->insert_journal_at_ititemjournal($journal_opening_balance);
        }
        
        foreach($array_number_inventori as $key){            
            $update_app_finished = array('AppFinished' => 1);
            $this->update_app_finished_inventori($update_app_finished, $key);
        }
        $this->loopHisimportDetail($dataImport);
        $this->db->trans_complete();
        if($this->db->trans_status() == FALSE){
            $result  = false;
        }else{
            $result  = true;
        }
        return $result;
    }
    
    function importAsset($dataImport){
        //try{
            $this->db->trans_start();
            //$this->db->trans_begin();//test pakai trans_begin dan try-catch karena foreach ada yg error tp insert yg lain ke commit juga, yg harusnya ga dieksekusi/rollback
            $this->loopHisimport($dataImport); 

            foreach($dataImport['data_invendetail'] as $key){
                $data_invendetail = array(
                    'BranchCode'        => $key['BranchCode'],
                    'ReceiptNumber'     => $key['ReceiptNumber'],//belum ketemu /belum pasti
                    'ReceiptDate'       => $key['ReceiptDate'],
                    'UsageDate'         => $key['UsageDate'],
                    'ItemCode'          => $key['ItemCode'],
                    'ItemDesc'          => $key['ItemDesc'],
                    'ItemQty'           => $key['ItemQty'],
                    'ItemAverage'       => $key['ItemAverage'],
                    //'PaymentAmount'     => $key['PaymentAmount'],
                    'PaymentPeriod'     => $key['PaymentPeriod'],
                    'AmountBookValues'  => $key['AmountBookValues'],
                    'ItemCategoryID'    => $key['ItemCategoryID'],
                    'DepartmentCode'    => $key['DepartmentCode'],
                    'Description'       => $key['Description'],
                    'AssetCode'         => $key['AssetCode'],
                    'SoldStatus'        => $key['SoldStatus'],
                    'LastUser'          => $key['LastUser']
                );
                $this->insert_at_inventdetail($data_invendetail);
            } 
            $this->loopHisimportDetail($dataImport);
            foreach($dataImport['data_inventori_insert'] as $key){
                $data_inventori_insert = array(
                    'BranchCode' 	=> $key['BranchCode'],
                    'ItemCode' 	=> $key['ItemCode'],
                    'ItemQty' 	=> $key['ItemQty'],
                    'ItemValue' 	=> $key['ItemValue'],
                    'ItemAverage' 	=> $key['ItemAverage'],
                    'LastUser' 	=> $key['LastUser']
                );
                $this->insert_at_inventories($data_inventori_insert);
            } 
            foreach($dataImport['data_inventori_update'] as $key){
                $data_inventori_update = array(
                    'BranchCode' 	=> $key['BranchCode'],
                    'ItemCode' 	=> $key['ItemCode'],
                    'ItemQty' 	=> $key['ItemQty'],
                    'ItemValue' 	=> $key['ItemValue'],
                    'ItemAverage' 	=> $key['ItemAverage'],
                    'LastUser' 	=> $key['LastUser']
                );
                $this->update_inventoris($data_inventori_update, $key['ItemCode']);
            } 
            $array_number_asset         = [];
            $arrayDataCatogryIdToAsset  = [];
            foreach($dataImport['journal_debet'] as $key){//untuk penginsertan jurnal debet
                $create_number_asset    = $this->m_import_transaction->create_asset_number($Branch);
                $journal_debet = array(
                    'BranchCode'            => $key['BranchCode'],
                    'ItfAssetNumber'        => $key['ItfAssetNumber'],
                    'ItemCategoryID'        => $key['ItemCategoryID'],
                    'ImportTransID'         => $key['ImportTransID'],
                    'ItfAssetDate'          => $key['ItfAssetDate'],
                    'ItfAssetAccountNumber' => $key['ItfAssetAccountNumber'],
                    'ItfAssetSaldoDebet'    => $key['ItfAssetSaldoDebet'],
                    'ItfAssetSaldoCredit'   => $key['ItfAssetSaldoCredit'],
                    'AmountCurrency'        => $key['AmountCurrency'],
                    'ItfAssetSignStatus'    => $key['ItfAssetSignStatus'],
                    'LastUser'              => $key['LastUser']
                );
                $this->insert_journal_at_itfassetjournal($journal_debet);
                if(!in_array($create_number_asset, $array_number_asset)){
                    $array_number_asset[] = $key['ItfAssetNumber'];
                }
                if(!in_array($key['ItemCategoryID'], $arrayDataCatogryIdToAsset)){
                    $array_number_asset[$key['ItemCategoryID']] = $key['ItfAssetNumber'];
                }
            }   

            foreach($dataImport['journal_credit'] as $key){//untuk peng-insert-an data jurnal kredit
                $journal_credit = array(
                    'BranchCode'            => $key['BranchCode'],
                    'ItfAssetNumber'        => $key['ItfAssetNumber'],//$create_number_asset
                    'ItemCategoryID'        => $key['ItemCategoryID'],
                    'ImportTransID'         => $key['ImportTransID'],
                    'ItfAssetDate'          => $key['ItfAssetDate'],
                    'ItfAssetAccountNumber' => $key['ItfAssetAccountNumber'],
                    'ItfAssetSaldoDebet'    => $key['ItfAssetSaldoDebet'],
                    'ItfAssetSaldoCredit'   => $key['ItfAssetSaldoCredit'],
                    'AmountCurrency'        => $key['AmountCurrency'],
                    'ItfAssetSignStatus'    => $key['ItfAssetSignStatus'],
                    'LastUser'              => $key['LastUser']
                );
                $this->insert_journal_at_itfassetjournal($journal_credit);
            }       
			
			/*
				Jv insert
			*/
			if(!empty($dataImport['jv_group'])){
				$array_number_jv         = [];
				foreach($dataImport['jv_group'] as $key){//untuk peng-insert-an data jurnal open balance
					$jv_group = array(
						'BranchCode' 		=> $key['BranchCode'],
						'JVchDate' 			=> $key['JVchDate'],
						'JVchNumber' 		=> $key['JVchNumber'],
						'JVchDesc' 			=> $key['JVchDesc'],
						'JVchTotalDebet' 	=> $key['JVchTotalDebet'],
						'JVchTotalCredit' 	=> $key['JVchTotalCredit'],
						'JVchType' 			=> $key['JVchType'],
						'AppLevel1Status' 	=> $key['AppLevel1Status'],
						'AppLevel1Code' 	=> $key['AppLevel1Code'],
						'AppLevel1Date' 	=> $key['AppLevel1Date'],
						'AppLevel2Status' 	=> $key['AppLevel2Status'],
						'AppLevel2Code' 	=> $key['AppLevel2Code'],
						'AppLevel2Date' 	=> $key['AppLevel2Date'],
						'TransStatus' 		=> $key['TransStatus'],
						'AppFinished' 		=> $key['AppFinished'],
						'CreatedBy' 		=> $key['CreatedBy'],
						'CreatedAt' 		=> $key['CreatedAt'],
						'LastUser' 			=> $key['LastUser']
					);
					$this->insert_jv_group($jv_group);
				}   
				
				foreach($dataImport['jv_detail'] as $key){
					$jv_detail = array(
						'JVchNumber' 		=> $key['JVchNumber'],
						'JVchAccount' 		=> $key['JVchAccount'],
						'JVchDebetSaldo' 	=> $key['JVchDebetSaldo'],
						'JVchCreditSaldo' 	=> $key['JVchCreditSaldo'],
						'JVchDetailMemo' 	=> $key['JVchDetailMemo'],
						'JVchDetailDept' 	=> $key['JVchDetailDept'],
						'JVchRate' 			=> $key['JVchRate'],
						'CurrencyCode' 		=> $key['CurrencyCode'],
						'AssetCode' 		=> $key['AssetCode'],
						'LastUser' 			=> $key['LastUser']
					);
					$this->insert_jv_detail($jv_detail);
				}
				
				foreach($dataImport['journal_debet_jv'] as $key){
					$journal_debet_jv = array(
						'JVchDate' 			=> $key['JVchDate'],
						'JVchNumber' 		=> $key['JVchNumber'],
						'JVchDesc' 			=> $key['JVchDesc'],
						'JVchAccountNumber' => $key['JVchAccountNumber'],
						'JVchSaldoDebet' 	=> $key['JVchSaldoDebet'],
						'JVchSaldoCredit' 	=> $key['JVchSaldoCredit'],
						'JVchSignStatus' 	=> $key['JVchSignStatus'],
						'AmountCurrency' 	=> $key['AmountCurrency'],
						'LastUser'          => $key['LastUser']
					);
					$this->insert_jv_journal($journal_debet_jv);
					$array_number_jv[] = $key['JVchNumber'];
				}
				
				foreach($dataImport['journal_credit_jv'] as $key){
					$journal_credit_jv = array(
						'JVchDate' 			=> $key['JVchDate'],
						'JVchNumber' 		=> $key['JVchNumber'],
						'JVchDesc' 			=> $key['JVchDesc'],
						'JVchAccountNumber' => $key['JVchAccountNumber'],
						'JVchSaldoDebet' 	=> $key['JVchSaldoDebet'],
						'JVchSaldoCredit' 	=> $key['JVchSaldoCredit'],
						'JVchSignStatus' 	=> $key['JVchSignStatus'],
						'AmountCurrency' 	=> $key['AmountCurrency'],
						'LastUser'          => $key['LastUser']
					);
					$this->insert_jv_journal($journal_credit_jv);
					$array_number_jv[] = $key['JVchNumber'];
				}
				
				foreach($dataImport['update_app_finish'] as $key){
					$data_app_finish = array(
						'AppFinished' => $key['AppFinished']
					);
					$this->update_app_finished_jv($data_app_finish, $key['JVchNumber']);
					$this->update_app_finished_jv_journal($data_app_finish, $key['JVchNumber']);
				}
				
				foreach($dataImport['update_jv_group'] as $key){
					$data_group_jv = array(
						//'JVchTotalDebet' 	=> $key['JVchTotalDebet'],
						//'JVchTotalCredit' 	=> $key['JVchTotalCredit'],
										
						'BranchCode' => $key['BranchCode']
					);
					$this->update_app_finished_jv($data_group_jv, $key['JVchNumber']);
				}
			}
			
            foreach($array_number_asset as $key){  //update status ke finished          
                $update_app_finished = array('AppFinished' => 1);
                $this->update_app_finished_itfassetjournal($update_app_finished, $key);
                //$this->update_app_finished_inventori($update_app_finished, $key);
            }

            $this->db->trans_complete();
            if($this->db->trans_status() == FALSE){
                $result  = false;
            }else{
                $result  = true;
            }
            
//            if ($this->db->trans_status() == FALSE){
//                $this->db->trans_rollback();                
//                $result  = false;
//            }else{
//                $this->db->trans_commit();                
//                $result  = TRUE;
//            }
//            return $result;
//        }catch (Exception $e){    
//            $this->db->trans_rollback();
//            $result  = false;
//            return $result;
//        }
    }
    /**********END TAMBAHAN DHIDHIK 18 FEB 2016 *******************/
}

/* End of file M_import_transaction.php */
/* Location: ./application/models/M_import_transaction.php */