<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_delivery_order extends CI_Model {

	public $table 		= "at_deliveryorder";
	public $detail 		= "at_dorddetail";
	public $journal 	= "at_dordjournal";
	public $variant 	= "at_variant";
	
	function get_all(){
		/*=========================lemparan parah dari custom.js untuk searching_data_transaksi=====================*/
		$param = $this->input->post('param');
		/*====================================end===================================================================*/
		
		/*=================================post dari find di kiri====================================================*/
		$Number			= isset($param['txt_IDNumber']) ? $param['txt_IDNumber'] : '';
		$Desc			= isset($param['txt_Description']) ? $param['txt_Description'] : '';
		
		$DateFrom		= isset($param['txt_DateFrom']) ? $param['txt_DateFrom'] : '';
		$DateTo			= isset($param['txt_DateTo']) ? $param['txt_DateTo'] : '';
		$TransStatus	= isset($param['chk_TransStatus']) ? $param['chk_TransStatus'] : '';
		$BranchCode		= isset($param['cmb_branch']) ? $param['cmb_branch'] : '';
		$cmb_CustomerCode	= isset($param['cmb_CustomerCode']) ? $param['cmb_CustomerCode'] : '';
		$DODirect			= isset($param['chk_DO_Direct']) ? $param['chk_DO_Direct'] : '';
		/*============================================end============================================================*/
		
		/*======================================scrol view 1-150=======================================*/
		$page		= ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
		$rows		= ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;
		
		$beginning 	= ($page - 1) * $rows;
		$end 		= $page * $rows;
		/*==============================================================================================*/
		
		/*==========================================by session=======================================================*/
		$sess_company		= $this->session->userdata('company');
        $sess_branch		= $this->session->userdata('branch');
        $sess_branch_status	= $this->session->userdata('branch_status');
		/*===========================================================================================================*/
		
		$this->db->select("at_deliveryorder.TransStatus, at_deliveryorder.DOrdNumber, at_deliveryorder.DOrdDate,at_deliveryorder.DOrdDesc, 
		at_deliveryorder.SOrdNumber, at_deliveryorder.SOrdDate, at_deliveryorder.CustPONumber,at_deliveryorder.CustPODate, at_deliveryorder.DriverName, 
		at_deliveryorder.LicencePlate, at_deliveryorder.CustomerCode,at_deliveryorder.BranchCode, am_customer.CustomerName, am_employee.EmployeeName,
		(case 
                                when at_deliveryorder.TransStatus = 0 then 'Waiting' 
                                when at_deliveryorder.TransStatus = 1 then 'On Progress' 
                                when at_deliveryorder.TransStatus = 2 then 'Active' 
                                when at_deliveryorder.TransStatus = 3 then 'Suspended' 
                                when at_deliveryorder.TransStatus = 4 then 'Need Mgr. App.' 
                                when at_deliveryorder.TransStatus = 5 then 'Need Dir. App.' 
                                when at_deliveryorder.TransStatus = 6 then 'Need Dir & Mgr App.' end) as Status,
								
		am_branch.BranchName, at_deliveryorder.PrintStatus");
		$this->db->from('at_deliveryorder');
		$this->db->join('am_customer', 'am_customer.CustomerCode = at_deliveryorder.CustomerCode', 'inner');
		$this->db->join('am_branch', 'am_branch.BranchCode = at_deliveryorder.BranchCode', 'inner');
		
		$this->db->where('at_deliveryorder.BranchCode',$this->session->userdata('branch'));
		$this->db->join('am_employee', 'am_employee.EmployeeCode = at_deliveryorder.CreatedBy', 'inner');
		
		/*===================================find by number dan desc=========================================================*/
		if($Number != ""){$this->db->like('at_deliveryorder.DOrdNumber', $Number);}
		if($Desc != ""){$this->db->like('at_deliveryorder.DOrdDesc', $Desc);}
		if($cmb_CustomerCode != ""){$this->db->where('am_customer.CustomerCode', $cmb_CustomerCode);}
		/*=============================================end===================================================================*/
		
		/*===================================find by branch code=========================================================*/
		/*
			get data dari  combobox find di view
		*/
        if($BranchCode != ""){
			$this->db->where('at_deliveryorder.BranchCode', $BranchCode);
		}
		else{
			/*
				inisialisasi untuk loading pertama page branch info 
			*/
			//----------------kondisi by company-------------------------------------//
			/*
				branch status 0 = HO
				branch status 1 = depo/branch
				jangan lupa sesion data di atas untuk setiap kondisi
				jangan lupa tambahin case when trans status
			*/
			$this->db->where('at_deliveryorder.BranchCode', $sess_branch);
			//-----------------------end-------------------------------------------------//
		}
		/*=======================================================end========================================================*/
		
		/*=====================================================find by date==================================================*/
		if((($DateFrom != '') && ($DateTo != ''))){
            if($DateFrom <= $DateTo){
				$this->db->where('at_deliveryorder.DOrdDate >=', date("Y-m-d", strtotime($DateFrom)));
            	$this->db->where('at_deliveryorder.DOrdDate <=', date("Y-m-d", strtotime($DateTo)));
			}
        }
		/*======================================================end===========================================================*/
		
		/*=====================================================find by status==================================================*/
		if($TransStatus!=''){ 
			$this->db->where_in('at_deliveryorder.TransStatus',$TransStatus); 
		}
		else{
			/*
				trans status 0,1 adalah aktif
			*/
			$this->db->where_in('at_deliveryorder.TransStatus',array(0,1,4,5,6,7)); 
		}
		/*=======================================================end========================================================*/
		
		/*==============================================sort tabel asc==========================================================*/
		$this->db->order_by($this->table.'.TransStatus', 'ASC');
		$this->db->order_by($this->table.'.DOrdNumber', 'ASC');
		/*===============================end=================================================================================*/
		$query  = $this->db->limit($end, $beginning);
		$query = $this->db->get();
		return $query->result();
    }
	
	function get_all_print($param){
		/*=========================lemparan parah dari custom.js untuk searching_data_transaksi=====================*/
		//$param = $this->input->post('param');
		/*====================================end===================================================================*/
		
		/*=================================post dari find di kiri====================================================*/
		$Number			= isset($param['txt_IDNumber']) ? $param['txt_IDNumber'] : '';
		$Desc			= isset($param['txt_Description']) ? $param['txt_Description'] : '';
		
		$DateFrom		= isset($param['txt_DateFrom']) ? $param['txt_DateFrom'] : '';
		$DateTo			= isset($param['txt_DateTo']) ? $param['txt_DateTo'] : '';
		$TransStatus	= isset($param['chk_TransStatus']) ? $param['chk_TransStatus'] : '';
		$BranchCode		= isset($param['cmb_branch']) ? $param['cmb_branch'] : '';
		$cmb_CustomerCode	= isset($param['cmb_CustomerCode']) ? $param['cmb_CustomerCode'] : '';
		$DODirect			= isset($param['chk_DO_Direct']) ? $param['chk_DO_Direct'] : '';
		/*============================================end============================================================*/
		
		/*======================================scrol view 1-150=======================================*/
		$page		= ($this->input->post('page')) ? intval($this->input->post('page')) : 1;
		$rows		= ($this->input->post('rows')) ? intval($this->input->post('rows')) : 150;
		
		$beginning 	= ($page - 1) * $rows;
		$end 		= $page * $rows;
		/*==============================================================================================*/
		
		/*==========================================by session=======================================================*/
		$sess_company		= $this->session->userdata('company');
        $sess_branch		= $this->session->userdata('branch');
        $sess_branch_status	= $this->session->userdata('branch_status');
		/*===========================================================================================================*/
		
		$this->db->select("at_deliveryorder.TransStatus, at_deliveryorder.DOrdNumber, at_deliveryorder.DOrdDate,at_deliveryorder.DOrdDesc, 
		at_deliveryorder.SOrdNumber, at_deliveryorder.SOrdDate, at_deliveryorder.CustPONumber,at_deliveryorder.CustPODate, at_deliveryorder.DriverName, 
		at_deliveryorder.LicencePlate, at_deliveryorder.CustomerCode,at_deliveryorder.BranchCode, am_customer.CustomerName, am_employee.EmployeeName,
		(case 
                                when at_deliveryorder.TransStatus = 0 then 'Waiting' 
                                when at_deliveryorder.TransStatus = 1 then 'On Progress' 
                                when at_deliveryorder.TransStatus = 2 then 'Active' 
                                when at_deliveryorder.TransStatus = 3 then 'Suspended' 
                                when at_deliveryorder.TransStatus = 4 then 'Need Mgr. App.' 
                                when at_deliveryorder.TransStatus = 5 then 'Need Dir. App.' 
                                when at_deliveryorder.TransStatus = 6 then 'Need Dir & Mgr App.' end) as Status,
								
		am_branch.BranchName, at_deliveryorder.PrintStatus");
		$this->db->from('at_deliveryorder');
		$this->db->join('am_customer', 'am_customer.CustomerCode = at_deliveryorder.CustomerCode', 'inner');
		$this->db->join('am_branch', 'am_branch.BranchCode = at_deliveryorder.BranchCode', 'inner');
		
		$this->db->where('at_deliveryorder.BranchCode',$this->session->userdata('branch'));
		$this->db->join('am_employee', 'am_employee.EmployeeCode = at_deliveryorder.CreatedBy', 'inner');
		
		/*===================================find by number dan desc=========================================================*/
		if($Number != ""){$this->db->like('at_deliveryorder.DOrdNumber', $Number);}
		if($Desc != ""){$this->db->like('at_deliveryorder.DOrdDesc', $Desc);}
		if($cmb_CustomerCode != ""){$this->db->where('am_customer.CustomerCode', $cmb_CustomerCode);}
		/*=============================================end===================================================================*/
		
		/*===================================find by branch code=========================================================*/
		/*
			get data dari  combobox find di view
		*/
        if($BranchCode != ""){
			$this->db->where('at_deliveryorder.BranchCode', $BranchCode);
		}
		else{
			/*
				inisialisasi untuk loading pertama page branch info 
			*/
			//----------------kondisi by company-------------------------------------//
			/*
				branch status 0 = HO
				branch status 1 = depo/branch
				jangan lupa sesion data di atas untuk setiap kondisi
				jangan lupa tambahin case when trans status
			*/
			$this->db->where('at_deliveryorder.BranchCode', $sess_branch);
			//-----------------------end-------------------------------------------------//
		}
		/*=======================================================end========================================================*/
		
		/*=====================================================find by date==================================================*/
		if((($DateFrom != '') && ($DateTo != ''))){
            if($DateFrom <= $DateTo){
				$this->db->where('at_deliveryorder.DOrdDate >=', date("Y-m-d", strtotime($DateFrom)));
            	$this->db->where('at_deliveryorder.DOrdDate <=', date("Y-m-d", strtotime($DateTo)));
			}
        }
		/*======================================================end===========================================================*/
		
		/*=====================================================find by status==================================================*/
		if($TransStatus!=''){ 
			$this->db->where_in('at_deliveryorder.TransStatus',$TransStatus); 
		}
		else{
			/*
				trans status 0,1 adalah aktif
			*/
			$this->db->where_in('at_deliveryorder.TransStatus',array(0,1,4,5,6,7)); 
		}
		/*=======================================================end========================================================*/
		
		/*==============================================sort tabel asc==========================================================*/
		$this->db->order_by($this->table.'.TransStatus', 'ASC');
		$this->db->order_by($this->table.'.DOrdNumber', 'ASC');
		/*===============================end=================================================================================*/
		$query  = $this->db->limit($end, $beginning);
		$query = $this->db->get();
		return $query->result();
    }
	
	function get_detail($id){    
		$this->db->_protect_identifiers = FALSE;
	    $this->db->select('
			at_deliveryorder.DOrdDate, at_deliveryorder.DOrdNumber, at_deliveryorder.DOrdDesc, at_deliveryorder.BillingAddress, at_deliveryorder.NumberUnik,at_deliveryorder.DOrdTotalAmount,  
			at_deliveryorder.ShipAddress, at_deliveryorder.ShipDate, at_deliveryorder.ShipmentID, at_deliveryorder.BranchCode, 
			at_deliveryorder.CustomerCode, at_deliveryorder.DriverName, at_deliveryorder.LicencePlate, at_deliveryorder.AppLevel1Status, 
			at_deliveryorder.AppLevel1Code, at_deliveryorder.AppLevel1Date, at_deliveryorder.TransStatus, at_deliveryorder.BookingStatus, 
			at_deliveryorder.PrintStatus, at_salesorder.SOrdTaxesStatus, at_salesorder.SOrdInTaxStatus, at_deliveryorder.SOrdDate, at_deliveryorder.CreatedBy,
			at_deliveryorder.SOrdNumber, at_deliveryorder.CustPODate, at_deliveryorder.CustPONumber, am_branch.BranchName, am_company.CompanyName, 
			am_customer.CustomerName, am_shipment.ShipmentName, at_deliveryorder.CurrencyCode, at_deliveryorder.CurrencyRate, at_deliveryorder.DirectStatus,at_deliveryorder.CompPONumber,
			at_salesorder.SOrdTaxPcnt,at_salesorder.SOrdPPh23
		');
        $this->db->join('at_salesorder', 'at_salesorder.SOrdNumber = at_deliveryorder.SOrdNumber', 'inner');
		$this->db->join('am_branch', 'am_branch.BranchCode = at_deliveryorder.BranchCode', 'INNER');
		$this->db->join('am_customer', 'am_customer.CustomerCode = at_deliveryorder.CustomerCode', 'INNER');
		$this->db->join('am_shipment', 'am_shipment.ShipmentID = at_deliveryorder.ShipmentID', 'INNER');
		$this->db->join('am_company', 'am_company.CompanyCode = am_branch.CompanyCode', 'INNER');
		$this->db->where('at_deliveryorder.DOrdNumber', $id);
		$query = $this->db->get($this->table);
		$this->db->_protect_identifiers = TRUE;
        return $query->row();
    }

    //Get detail for print preview
    function get_detail_print($id){    
		$this->db->_protect_identifiers = FALSE;
	    $this->db->select('
			at_deliveryorder.DOrdDate, at_deliveryorder.DOrdNumber, at_deliveryorder.DOrdDesc, at_deliveryorder.BillingAddress,
			at_deliveryorder.ShipAddress, at_deliveryorder.ShipDate, at_deliveryorder.ShipmentID, at_deliveryorder.BranchCode, 
			at_deliveryorder.CustomerCode, at_deliveryorder.DriverName, at_deliveryorder.LicencePlate, at_deliveryorder.AppLevel1Status, 
			at_deliveryorder.AppLevel1Code, at_deliveryorder.AppLevel1Date, at_deliveryorder.TransStatus, at_deliveryorder.BookingStatus, 
			at_deliveryorder.PrintStatus, at_salesorder.SOrdTaxesStatus, at_salesorder.SOrdInTaxStatus, at_deliveryorder.SOrdDate, 
			at_deliveryorder.SOrdNumber, at_deliveryorder.CustPODate, at_deliveryorder.CustPONumber, am_branch.BranchName, am_company.CompanyName, 
			am_customer.CustomerName, am_customer.CustomerAddress, am_customer.PhoneNumber, am_shipment.ShipmentName, at_deliveryorder.CurrencyCode, at_deliveryorder.CurrencyRate, 
			at_salesorder.SOrdTaxPcnt, app.EmployeeName as app_do, iss.EmployeeName as iss_do
		');
        $this->db->join('at_salesorder', 'at_salesorder.SOrdNumber = at_deliveryorder.SOrdNumber', 'inner');
		$this->db->join('am_branch', 'am_branch.BranchCode = at_deliveryorder.BranchCode', 'INNER');
		$this->db->join('am_customer', 'am_customer.CustomerCode = at_deliveryorder.CustomerCode', 'INNER');
		$this->db->join('am_shipment', 'am_shipment.ShipmentID = at_deliveryorder.ShipmentID', 'INNER');
		$this->db->join('am_company', 'am_company.CompanyCode = am_branch.CompanyCode', 'INNER');
		$this->db->join('am_employee as app','at_deliveryorder.AppLevel1Code = app.EmployeeCode','LEFT');
		$this->db->join('am_employee as iss','at_deliveryorder.CreatedBy = iss.EmployeeCode','LEFT');
		$this->db->where('at_deliveryorder.DOrdNumber', $id);
		$query = $this->db->get($this->table);
		$this->db->_protect_identifiers = TRUE;
        return $query->row();
    }
	
    function get_detail_item($DOrdNumber, $BranchCode){
        $this->db->_protect_identifiers = FALSE;
        $this->db->select('at_dorddetail.ItemDesc, at_dorddetail.ItemCategoryID, at_dorddetail.ItemCategoryCode, at_dorddetail.ItemPriceCogs,at_inventories.ItemAverage,
			at_sorddetail.ItemPrice, at_inventories.ItemAverage, at_dorddetail.DOrdDetailID, 
			at_dorddetail.DOrdNumber, at_dorddetail.ItemCode, at_dorddetail.ItemQty, at_sorddetail.ItemQty as ItemQtySO, at_sorddetail.ItemQtySpd, at_sorddetail.SQuoDetailID, at_sorddetail.ItemDiscPcnt,
			at_dorddetail.ItemUnit, at_dorddetail.SOrdDetailID, 
			at_dorddetail.SOrdDetailID, at_salesorder.SOrdTaxes, at_sorddetail.ItemQtyDO,at_dorddetail.ItemPPH23, at_salesorder.SOrdNumber,
			(at_sorddetail.ItemQty - at_sorddetail.ItemQtyDO + at_dorddetail.ItemQty) as ItemMax, at_sorddetail.ItemQtyDOApp, 
			at_sorddetail.SalesmanCode, at_sorddetail.ItemTaxPPNOut, at_sorddetail.ItemTotalAmount, 
			at_dorddetail.ItemTax, at_dorddetail.ItemAmount, at_dorddetail.ItemType as ItemStatus');
        $this->db->join('at_dorddetail', 'at_deliveryorder.DOrdNumber=at_dorddetail.DOrdNumber', 'INNER');
        $this->db->join('at_salesorder', 'at_deliveryorder.SOrdNumber=at_salesorder.SOrdNumber', 'INNER');
        $this->db->join('at_sorddetail', 'at_sorddetail.SOrdDetailID = at_dorddetail.SOrdDetailID', 'INNER');
        $this->db->join('am_item', 'am_item.ItemCode = at_dorddetail.ItemCode', 'LEFT');
		$this->db->join('at_inventories', 'am_item.ItemCode=at_inventories.ItemCode AND at_inventories.BranchCode="'. $BranchCode .'"', 'LEFT');
        $this->db->where('at_dorddetail.DOrdNumber', $DOrdNumber);
        $query = $this->db->get('at_deliveryorder');	
        $this->db->_protect_identifiers = TRUE;
        
        return $query->result();
    }

    //Get detail item for print preview
    function get_detail_item_print($DOrdNumber, $BranchCode){
        $this->db->_protect_identifiers = FALSE;
        $this->db->select('at_dorddetail.ItemDesc, at_dorddetail.ItemCategoryID, at_dorddetail.ItemCategoryCode, at_dorddetail.ItemPriceCogs,
			at_sorddetail.ItemPrice, at_inventories.ItemAverage, at_dorddetail.DOrdDetailID, 
			at_dorddetail.DOrdNumber, at_dorddetail.ItemCode, at_dorddetail.ItemQty, at_sorddetail.ItemQty as ItemQtySO, at_sorddetail.ItemQtySpd, at_sorddetail.SQuoDetailID,
			at_dorddetail.ItemUnit, at_dorddetail.SOrdDetailID, 
			at_dorddetail.SOrdDetailID, at_salesorder.SOrdTaxes, at_sorddetail.ItemQtyDO, at_salesorder.SOrdNumber,
			(at_sorddetail.ItemQty - at_sorddetail.ItemQtyDO + at_dorddetail.ItemQty) as ItemMax, at_sorddetail.ItemQtyDOApp, 
			at_sorddetail.SalesmanCode, at_sorddetail.ItemTaxPPNOut, at_sorddetail.ItemAmount, at_sorddetail.ItemTotalAmount, 
			at_dorddetail.ItemTax, at_dorddetail.ItemAmount, at_dorddetail.ItemType as ItemStatus');
        $this->db->join('at_dorddetail', 'at_deliveryorder.DOrdNumber=at_dorddetail.DOrdNumber', 'INNER');
        $this->db->join('at_salesorder', 'at_deliveryorder.SOrdNumber=at_salesorder.SOrdNumber', 'INNER');
        $this->db->join('at_sorddetail', 'at_sorddetail.SOrdDetailID = at_dorddetail.SOrdDetailID', 'INNER');
        $this->db->join('am_item', 'am_item.ItemCode = at_dorddetail.ItemCode', 'LEFT');
		$this->db->join('at_inventories', 'am_item.ItemCode=at_inventories.ItemCode AND at_inventories.BranchCode="'. $BranchCode .'"', 'LEFT');
        $this->db->where('at_dorddetail.DOrdNumber', $DOrdNumber);
        $query = $this->db->get('at_deliveryorder');	
        $this->db->_protect_identifiers = TRUE;
        
        return $query->result_array();
    }
	
	function insert_transaction($trans_data){
		$this->db->trans_start();
		$this->insert($trans_data['insert_group']['data']);
		
		for($x=0;$x<count($trans_data['insert_detail']['data']);$x++){
			$this->insert_detail($trans_data['insert_detail']['data'][$x]);
		}
		
		for($x=0;$x<count($trans_data['update_so']['data']);$x++){
			$this->m_sales_order->update_detail($trans_data['update_so']['data'][$x],$trans_data['update_so']['SOrdDetailID'][$x]);
		}
		
		if(!empty($trans_data['insert_journal_cogs']['data'])){
			for($x=0;$x<count($trans_data['insert_journal_cogs']['data']);$x++){
				$this->insert_journal($trans_data['insert_journal_cogs']['data'][$x]);
			}
		}
		
		if(!empty($trans_data['insert_journal_inventori']['data'])){
			for($x=0;$x<count($trans_data['insert_journal_inventori']['data']);$x++){
				$this->insert_journal($trans_data['insert_journal_inventori']['data'][$x]);
			}
		}
		
		$this->db->trans_complete();
		if ($this->db->trans_status() === FALSE){
			return 0;
		}
		else{
			return 1;
		}
	}
	
	function update_transaction($trans_data){
		$this->db->trans_start();
		$this->delete_journal($trans_data['delete_journal']['DOrdNumber']);
		
		$this->update($trans_data['update_group']['data'],$trans_data['update_group']['DOrdNumber']);
		
		for($x=0;$x<count($trans_data['update_detail']['data']);$x++){
			$this->update_detail($trans_data['update_detail']['data'][$x],$trans_data['update_detail']['DOrdDetailID'][$x]);
		}
		
		for($x=0;$x<count($trans_data['update_so']['data']);$x++){
			$this->m_sales_order->update_detail($trans_data['update_so']['data'][$x],$trans_data['update_so']['SOrdDetailID'][$x]);
		}
		
		/*
			Delete grid
		*/
		if($trans_data['delete_detail']['total']>0){
			for($x=0;$x<count($trans_data['delete_detail']['DOrdDetailID']);$x++){
				$this->delete_detail_by_id($trans_data['delete_detail']['DOrdDetailID'][$x]);
			}
			
			for($x=0;$x<count($trans_data['update_so']['data']);$x++){
				$this->m_sales_order->update_detail($trans_data['update_so']['data'][$x],$trans_data['update_so']['SOrdDetailID'][$x]);
			}
		}
		/*===========================END================================*/
		
		/*
			Journal Insert
		*/
		if(!empty($trans_data['insert_journal_cogs']['data'])){
			for($x=0;$x<count($trans_data['insert_journal_cogs']['data']);$x++){
				$this->insert_journal($trans_data['insert_journal_cogs']['data'][$x]);
			}
		}
		
		if(!empty($trans_data['insert_journal_inventori']['data'])){
			for($x=0;$x<count($trans_data['insert_journal_inventori']['data']);$x++){
				$this->insert_journal($trans_data['insert_journal_inventori']['data'][$x]);
			}
		}
		/*========================End==================================*/
		
		$this->db->trans_complete();
		if ($this->db->trans_status() === FALSE){
			return 0;
		}
		else{
			return 1;
		}
	}
	
	function suspend_transaction($trans_data){
		$this->db->trans_start();
		$this->update($trans_data['update_group']['data'],$trans_data['update_group']['DOrdNumber']);
		
		for($x=0;$x<count($trans_data['update_so']['data']);$x++){
			$this->m_sales_order->update_detail($trans_data['update_so']['data'][$x],$trans_data['update_so']['SOrdDetailID'][$x]);
		}
		
		$this->db->trans_complete();
		if ($this->db->trans_status() === FALSE){
			return 0;
		}
		else{
			return 1;
		}
	}
	
	function approval_transaction($trans_data){
		$this->db->trans_start();
		$this->delete_journal($trans_data['delete_journal']['DOrdNumber']);
		
		/*
			DO Direct
		*/
		if(!empty($trans_data['insert_ri']['data'])){
			$this->m_receive_item->insert($trans_data['insert_ri']['data']);
		}
		
		if(!empty($trans_data['insert_ri_detail']['data'])){
			for($x=0;$x<count($trans_data['insert_ri_detail']['data']);$x++){
				$this->m_receive_item->insert_detail($trans_data['insert_ri_detail']['data'][$x]);
			}
		}
		
		if(!empty($trans_data['update_qty_po']['data'])){
			for($x=0;$x<count($trans_data['update_qty_po']['data']);$x++){
				$this->m_purchase_order->update_detail($trans_data['update_qty_po']['data'][$x],$trans_data['update_qty_po']['POrdDetailID'][$x]);
			}
		}
		
		if(!empty($trans_data['update_po']['data'])){
			for($x=0;$x<count($trans_data['update_po']['data']);$x++){
				$this->m_purchase_order->update($trans_data['update_po']['data'][$x],$trans_data['update_po']['POrdNumber'][$x]);
			}
		}
		
		if(!empty($trans_data['update_inventori']['data'])){
			for($x=0;$x<count($trans_data['update_inventori']['data']);$x++){
				$this->m_inventories->update($trans_data['update_inventori']['data'][$x],$trans_data['update_inventori']['ItemCode'][$x]);
			}
		}
		
		if(!empty($trans_data['insert_journal_ri']['data'])){
			for($x=0;$x<count($trans_data['insert_journal_ri']['data']);$x++){
				$this->m_receive_item->insert_journal($trans_data['insert_journal_ri']['data'][$x]);
			}
		}
		
		if(!empty($trans_data['insert_journal_ri_unbilled']['data'])){
			$this->m_receive_item->insert_journal($trans_data['insert_journal_ri_unbilled']['data']);
		}
		/*=========================END==================================*/
		
		for($x=0;$x<count($trans_data['update_so']['data']);$x++){
			$this->m_sales_order->update_detail($trans_data['update_so']['data'][$x],$trans_data['update_so']['SOrdDetailID'][$x]);
		}
		
		for($x=0;$x<count($trans_data['update_sq']['data']);$x++){
			$this->m_sales_quotation->update_detail($trans_data['update_sq']['data'][$x],$trans_data['update_sq']['SQuoDetailID'][$x]);
		}
		
		/*
			Journal Insert
		*/
		if(!empty($trans_data['insert_journal_cogs']['data'])){
			for($x=0;$x<count($trans_data['insert_journal_cogs']['data']);$x++){
				$this->insert_journal($trans_data['insert_journal_cogs']['data'][$x]);
			}
		}
		
		if(!empty($trans_data['insert_journal_inventori']['data'])){
			for($x=0;$x<count($trans_data['insert_journal_inventori']['data']);$x++){
				$this->insert_journal($trans_data['insert_journal_inventori']['data'][$x]);
			}
		}
		/*========================End==================================*/
		$this->update($trans_data['update_group']['data'],$trans_data['update_group']['DOrdNumber']);
		
		if(!empty($trans_data['update_proses_so']['data'])){
			for($x=0;$x<count($trans_data['update_proses_so']['data']);$x++){
				$this->m_sales_order->update($trans_data['update_proses_so']['data'][$x],$trans_data['update_proses_so']['SOrdNumber'][$x]);
			}
		}
		
		/*
			Insert Sales Invoice
		*/
		
		for($x=0;$x<count($trans_data['insert_group_si']['data']);$x++){
			$this->m_sales_invoice->insert($trans_data['insert_group_si']['data'][$x]);
		}
		
		for($x=0;$x<count($trans_data['insert_detail_si']['data']);$x++){
			$this->m_sales_invoice->insert_detail($trans_data['insert_detail_si']['data'][$x]);
		}
		
		/*
			Journal sales invoice
		*/
		if(!empty($trans_data['insert_journal']['data'])){
			for($x=0;$x<count($trans_data['insert_journal']['data']);$x++){
				$this->m_sales_invoice->insert_journal($trans_data['insert_journal']['data'][$x]);
			}
		}
		
		if(!empty($trans_data['update_journal']['data'])){
			for($x=0;$x<count($trans_data['update_journal']['data']);$x++){
				$this->m_sales_invoice->update_journal($trans_data['update_journal']['data'][$x],$trans_data['update_journal']['SInvNumber'][$x], $trans_data['update_journal']['SInvAccountNumber'][$x]);
			}
		}
			
		$this->db->trans_complete();
		if ($this->db->trans_status() === FALSE){
			return 0;
		}
		else{
			return 1;
		}
	}
	
	function reverse_trasaction($trans_data){
		$this->db->trans_start();
		
		if(!empty($trans_data['update_inventory']['data'])){
			for($x=0;$x<count($trans_data['update_inventory']['data']);$x++){
				$this->m_inventories->update($trans_data['update_inventory']['data'][$x],$trans_data['update_inventory']['ItemCode'][$x]);
			}
		}
		
		$this->m_sales_order->update($trans_data['update_proses_so']['data'],$trans_data['update_proses_so']['SOrdNumber']);
		
		$this->update($trans_data['update_do']['data'],$trans_data['update_do']['DOrdNumber']);
		
		for($x=0;$x<count($trans_data['delete_invoice']['SInvNumber']);$x++){
			$this->m_sales_invoice->delete_journal($trans_data['delete_invoice']['SInvNumber'][$x]);
			$this->m_sales_invoice->delete_detail($trans_data['delete_invoice']['SInvNumber'][$x]);
			$this->m_sales_invoice->delete_group($trans_data['delete_invoice']['SInvNumber'][$x]);
		}
		
		$this->db->trans_complete();
		if ($this->db->trans_status() === FALSE){
			return 0;
		}
		else{
			return 1;
		}
	}
	
	function update_print_transaction($trans_data){
		$this->db->trans_start();
		$this->update($trans_data['update_group']['data'],$trans_data['update_group']['DOrdNumber']);
		
		$this->db->trans_complete();
		if ($this->db->trans_status() === FALSE){
			return 0;
		}
		else{
			return 1;
		}
	}
	
    function insert($data) {
        $this->db->insert($this->table, $data);			        
    }
	
    function insert_detail($data_detail) {		
        $this->db->insert($this->detail, $data_detail);				        
    }

    function update($data, $id) {
        $this->db->update($this->table, $data, array('DOrdNumber' => $id));	
    }
        
    function update_detail($data, $id) {
        $this->db->update($this->detail, $data, array('DOrdDetailID' => $id));				
    }
	
	function update_inventori($data,$item_code, $branch_code) {
        $this->db->update('at_inventories', $data, array('ItemCode'=>$item_code, 'BranchCode'=>$branch_code));
    }
	
	function delete_detail_by_id($DOrdDetailID){
        $this->db->delete($this->detail,array('DOrdDetailID' => $DOrdDetailID));
    }
	
	/*======================penomeran transaksi new=======================================================*/
	function check_code_exist($new_do_number) {
        $query = $this->db->get_where($this->table, array('DOrdNumber' => $new_do_number), 1, 0);
		
        if ($query->num_rows() > 0) {
            return TRUE;
        }
        else {
            return FALSE;
        }	
    }
	
	function create_do_number($date = ''){
        $branch = $this->session->userdata('branch');
		
        if($date != ''){
            $month  = date('m', strtotime($date));
            $year   = date('y', strtotime($date));
        }
        else{
            $month  = date('m');
            $year   = date('y');
        }
		
        $do_number	= $branch ."/". $month . $year ."DO/";
		
        $this->db->_protect_identifiers = false;
        $this->db->select('(CASE 
                              WHEN MAX(CONVERT(SUBSTR(DOrdNumber, -3),signed)) IS NULL then 1
                              ELSE MAX(CONVERT(SUBSTR(DOrdNumber, -3),signed)) + 1 END) as New_Number');
        $this->db->like('DOrdNumber', $do_number);
        $query = $this->db->get($this->table);
        $this->db->_protect_identifiers = true;
                
        $num    = $query->row()->New_Number;
        $long   = strlen($num);
        
        if($long == 1){
            $number = '00' . $num;
        }
        elseif($long == 2){
            $number = '0' . $num;
        }
        else{
            $number = $num;
        }

        $new_do_number = $do_number . $number;

        while($this->check_code_exist($new_do_number)){
            $num++;
            $long = strlen($num);
    
            if($long == 1){
                $number = '00' . $num;
            }
            elseif($long == 2){
                $number = '0' . $num;
            }
            else{
                $number = $num;
            }
            
            $new_do_number = $do_number . $number;		
        }
        
        return  $new_do_number;
    }
	/*==============================================================end=================================*/
	
	function set_trans_status($id, $status) {
		$this->db->set('TransStatus', $status);
		$this->db->where('DOrdNumber', $id);
		$this->db->update($this->table);
	}
	
	function get_transstatus($id) {
        $this->db->select('TransStatus');
		$this->db->from($this->table);
		$this->db->where('DOrdNumber', $id); 
		$query = $this->db->get();    
        
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $row) {
				$TransStatus = $row->TransStatus;
			}    

			return $TransStatus;
		}
		else {
			return FALSE;
		}	
    }
	
	function get_date_by_id($id) {
        $this->db->select('DOrdDate');
		$this->db->from($this->table);
		$this->db->where('DOrdNumber', $id); 
		$query = $this->db->get();    
        
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $row) {
				$DOrdDate = $row->DOrdDate;
			}    

			return $DOrdDate;
		}
		else {
			return FALSE;
		}	
    }
	
    function join_so($id){    
        $this->db->_protect_identifiers = FALSE;
        $this->db->select('do.BranchCode, do.CustomerCode, do.DOrdNumber, do.DOrdDate, do.SOrdNumber, do.CustPONumber, do.ShipDate, 
                           do.BillingAddress, do.ShipAddress, do.ShipDate, so.ShipmentID, so.TermsPaymentID, so.FOBID, so.SOrdSubTotal, 
                           so.SOrdTotalAmount, so.SOrdTotalDP, so.SRecTotalDP, so.SOrdTaxPcnt, so.SOrdTaxes, so.SOrdTaxesStatus,so.SOrdPPh23Pcnt, 
                           so.SOrdDesc, so.SOrdInTaxStatus, sd.ItemPrice, sd.ItemAmount, sd.ItemTaxPPNOut, sd.SalesmanCode, 
						   so.CurrencyCode, so.CurrencyRate, so.SOrdDiscount, so.SOrdEstFreight, 
						   tp.NetDueIn');
        $this->db->join('at_salesorder as so', 'so.SOrdNumber = do.SOrdNumber', 'INNER');
        $this->db->join('at_sorddetail as sd', 'sd.SOrdNumber = so.SOrdNumber', 'INNER');
		$this->db->join('am_termspayment as tp', 'so.TermsPaymentID=tp.TermsPaymentID', 'INNER');
        $this->db->where('do.DOrdNumber', $id);
        $query = $this->db->get($this->table . ' as do');
        $this->db->_protect_identifiers = TRUE;

        return $query->result();
    }
	
	function join_so_new($id){
        $this->db->_protect_identifiers = FALSE;
        $this->db->select('do.BranchCode, do.CustomerCode, do.DOrdNumber, do.DOrdDate, do.SOrdNumber, do.CustPONumber, do.ShipDate,do.DOrdDesc, 
                           do.BillingAddress, do.ShipAddress, do.ShipDate, so.ShipmentID, so.TermsPaymentID, so.FOBID, so.SOrdSubTotal, so.SOrdPPh23,
                           so.SOrdTotalAmount, so.SOrdTotalDP, so.SRecTotalDP, so.SOrdTaxPcnt, so.SOrdTaxes, so.SOrdTaxesStatus, so.SOrdDPStatus,so.SOrdPPh23Pcnt,  
                           so.SOrdDesc, so.SOrdInTaxStatus, so.CurrencyCode, so.CurrencyRate, so.SOrdDiscount, 
						   so.SOrdEstFreight, tp.NetDueIn');
        $this->db->join('at_salesorder as so', 'so.SOrdNumber = do.SOrdNumber', 'INNER');
		$this->db->join('am_termspayment as tp', 'so.TermsPaymentID=tp.TermsPaymentID', 'INNER');
        $this->db->where('do.DOrdNumber', $id);
        $query = $this->db->get($this->table . ' as do');
        $this->db->_protect_identifiers = TRUE;

        return $query->result();
    }
	
//    function insert_journal_debit($data_journal_debit){
//		$this->db->trans_begin();
//		$this->db->insert($this->journal, $data_journal_debit);
//		
//		if ($this->db->trans_status() === FALSE) {
//			$this->db->trans_rollback();
//			return 0;
//		}
//		else {
//			$this->db->trans_commit();
//			return 1;
//		}
//	}
	
//	function insert_journal_credit($data_journal_credit){
//		$this->db->trans_begin();
//		$this->db->insert($this->journal, $data_journal_credit);
//		
//		if ($this->db->trans_status() === FALSE) {
//			$this->db->trans_rollback();
//			return 0;
//		}
//		else {
//			$this->db->trans_commit();
//			return 1;
//		}
//	}
	
    function insert_journal($data) {		
        $this->db->insert($this->journal, $data);
    }
	
    function delete_journal($txt_no) {
        $this->db->delete($this->journal, array('DOrdNumber' => $txt_no));
    }
	
    function get_item_account($AccountCode,$ItemCategoryID){
        $this->db->select('SysItemCategoryID');
		$this->db->from('sys_itemcategory');
		$this->db->where('ItemCategoryID',$ItemCategoryID);
		$this->db->where('CompanyCode',$this->session->userdata('company'));
		
		//get SysItemCategoryID
		$query = $this->db->get();
		
		if ($query->num_rows() > 0) {
			$data=$query->row();
			$this->db->select('AccountNumber');
			$this->db->from('sys_itemaccount');
			$this->db->where('AccountCode',$AccountCode);
			$this->db->where('SysItemCategoryID',$data->SysItemCategoryID);
			
			//get AccountNumber
			$query_account = $this->db->get();
			
			if ($query_account->num_rows() > 0) {
				return $query_account->row();
			}
			else {
				return false;
			}
		}
		else {
			return false;
		}
	}
	
	function get_sales_account($AccountCode,$ItemCategoryID){
        $this->db->select('SysSalesCategoryID');
		$this->db->from('sys_salescategory');
		$this->db->where('ItemCategoryID',$ItemCategoryID);
		$this->db->where('CompanyCode',$this->session->userdata('company'));
		
		//get SysSalesCategoryID
		$query = $this->db->get();
		
		if ($query->num_rows() > 0) {
			$data=$query->row();
			$this->db->select('AccountNumber');
			$this->db->from('sys_salesaccount');
			$this->db->where('AccountCode',$AccountCode);
			$this->db->where('SysSalesCategoryID',$data->SysSalesCategoryID);
			
			//get AccountNumber	
			$query_account = $this->db->get();
			
			if ($query_account->num_rows() > 0) {
				return $query_account->row();
			}
			else {
				return false;
			}
		}
		else {
			return false;
		}			
	}
	
	function get_account_with_currency($AccountNumber,$CurrencyCode){
		if($CurrencyCode!='IDR'){
			$this->db->select('AccountName');
			$this->db->from('am_accountgl');
			$this->db->join('at_compaccountgl','am_accountgl.AccountNumber=at_compaccountgl.AccountNumber','INNER');
			$this->db->where('am_accountgl.AccountNumber',$AccountNumber);
			$this->db->where('BranchCode',$this->session->userdata('branch'));
			
			$query = $this->db->get();
			if($query->num_rows() > 0){
				$result 		= $query->row();
				$AccountName 	= $result->AccountName;
				$AccountName 	= implode(' ', explode(' ', $AccountName, -1)) .' '. $CurrencyCode;
				
				$this->db->select('AccountNumber');
				$this->db->from('am_accountgl');
				$this->db->where('AccountName', $AccountName);
				
				$query = $this->db->get();
				if($query->num_rows() > 0){
					$AccountNumber = $query->row()->AccountNumber;
					return $AccountNumber;
				}
				else{
					return false;
				}
			}
			else{
				return false;
			}
		}
		else{
			if($AccountNumber!=''){
				return $AccountNumber;
			}
			else{
				return false;
			}
		}
	}
	
	function get_journal_by_number($Number){
	    $this->db->_protect_identifiers = FALSE;
	    $this->db->select("at_dordjournal.*,am_accountgl.AccountNumber,am_accountgl.AccountName");
		$this->db->join("am_accountgl","am_accountgl.AccountNumber=at_dordjournal.DOrdAccountNumber","INNER");
        $this->db->order_by('at_dordjournal.DOrdNumber','ASC');
		$query = $this->db->get_where($this->journal, array('DOrdNumber' => $Number));
		$this->db->_protect_identifiers = TRUE;
        return $query->result();
    }
	
	function get_qty_by_so($so) {
        $this->db->select("at_dorddetail.ItemQty");
		$this->db->join('at_dorddetail','at_dorddetail.DOrdNumber = at_deliveryorder.DOrdNumber', 'inner');
		$this->db->where('at_deliveryorder.SOrdNumber', $so);
		$this->db->where('at_deliveryorder.AppLevel1Status !=', 'NULL');
        $data = $this->db->get($this->table);
        return $data->result();
    }
	
	function get_app_level_code($so) {
        $this->db->select("at_deliveryorder.AppLevel1Code");
		$this->db->where('at_deliveryorder.SOrdNumber', $so);
		$this->db->where('at_deliveryorder.AppLevel1Status !=', 'NULL');
        $data = $this->db->get($this->table);
        return $data->row();
    }
	
	function set_print_status(){
		$id = $this->input->post('DOrdNumber');
		
		$this->db->set('PrintStatus', 1);
		$this->db->where('DOrdNumber', $id);
		$this->db->update($this->table);
	}
	
	function get_app_qty($SOrdNumber){
		$this->db->select('SUM(ItemQty) as ItemQty');
		$this->db->join('at_dorddetail','at_deliveryorder.DOrdNumber=at_dorddetail.DOrdNumber','INNER');
		$this->db->where('at_dorddetail.SOrdNumber',$SOrdNumber);
		$this->db->where('at_deliveryorder.TransStatus',1);
		
		$query = $this->db->get($this->table);
		
		if($query->num_rows()>0){
			foreach($query->result() as $row){
				$ItemQty = $row->ItemQty;
			}
			return $ItemQty;
		}else{
			return false;
		}
	}
	
	function get_detail_sales_order_for_do($SOrdDetailID, $ItemCode){
        $this->db->_protect_identifiers = FALSE;
        $this->db->select('(sod.ItemQty) as ItemQtySO, (sod.ItemQtyDO + sod.ItemQtySpd) as ItemQtyDO, sod.ItemQtySpd');
        $this->db->from('at_salesorder as so');
        $this->db->join('at_sorddetail as sod', 'sod.SOrdNumber = so.SOrdNumber', 'INNER');
        
		$this->db->where('sod.SOrdDetailID', $SOrdDetailID);
		$this->db->where('sod.ItemCode', $ItemCode);
		
        $query = $this->db->get();
        $this->db->_protect_identifiers = TRUE;
        
		if ($query->num_rows() > 0) {
            return $query->row();
        }
        else {
            return FALSE;
        }
    } 
	
	function check_item_do_approved_by_sordnumber($So_number){
        $this->db->select('TransStatus');
        $this->db->from($this->table);
        $this->db->where('SOrdNumber', $So_number);
        $this->db->where_in('TransStatus', array(4,5,6));
        $query = $this->db->get();
        
        if ($query->num_rows() > 0){
            return true;
        }
        else{
            return false;
        }
    }
	
	function get_delivery_order_header($cmb_customer){
		$this->db->select('sum(DOrdTotalAmount) as DOrdTotalAmount');
		$this->db->from($this->table);
		$this->db->where('CustomerCode', $cmb_customer);
		$this->db->where_in('TransStatus', array(2));
		$this->db->group_by('CustomerCode');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->row();
		}
		else {
			return FALSE;
		}	
	}
	
	function get_sordetail($SOrdNumber) {
        $this->db->select('(ItemQty - (ItemQtyDO + ItemQtySpd)) as Quantity');
        $this->db->from('at_sorddetail');
        $this->db->where('SOrdNumber', $SOrdNumber);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
			return $query->result();
		}
		else {
			return FALSE;
		}
    }
	
	function get_po_number($cmb_comp_po) {
        $this->db->select('ri.PRecNumber');
        $this->db->from('at_receiveitem as ri');
        $this->db->where('ri.POrdNumber', $cmb_comp_po);
		$this->db->where('ri.TransStatus !=', 3);
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
			return $query->row();
		}
		else {
			return FALSE;
		}
    }
	
	public function check_do_app_status($sord_number){
		$finish_preq = 0;
		$this->db->select('at_deliveryorder.TransStatus');
		$this->db->from($this->table);
		$this->db->where('at_deliveryorder.SOrdNumber',$sord_number);
		
		$query = $this->db->get();
		if($query->num_rows()>0){
			$result = $query->result();
			foreach($result as $row){
				if(in_array($row->TransStatus,array(0,1,2))){
					$finish_preq = 1;
				}
			}
			
			if($finish_preq == 0){
				return 0;
			}
			else{
				return 1;
			}
		}
		else{
			return 0;
		}
	}
	
	function get_total_qty_po($id){
		
        $this->db->select('sum(at_porddetail.ItemQtySpd) as TotalQtySpd, sum(at_porddetail.ItemQtyRI) as TotalQtyRI');
        $this->db->from('at_porddetail');
		$this->db->join('at_purchaseorder', 'at_purchaseorder.POrdNumber = at_porddetail.POrdNumber', 'INNER');
        $this->db->where('at_porddetail.POrdNumber', $id);
		$this->db->where_in('at_purchaseorder.TransStatus', array(0,1,2));
        $this->db->group_by('at_porddetail.POrdNumber');
		
        $query_po=$this->db->get();
		
		if($query_po->num_rows() > 0){
			return $query_po->row();
        }
        else{
            return false;
        }
    }
	
	function get_qty_dordetail($PordNumber, $ItemCode, $DOrdNumber){
		$this->db->select('dod.ItemQty, dod.ItemCode');
        $this->db->from('at_deliveryorder as do');
		$this->db->join('at_dorddetail as dod', 'dod.DOrdNumber = do.DOrdNumber', 'INNER');
        $this->db->where('dod.POrdNumber', $PordNumber);
		$this->db->where('dod.ItemCode', $ItemCode);
		$this->db->where('dod.DOrdNumber', $DOrdNumber);
		
        $query_po=$this->db->get();
		
		if($query_po->num_rows() > 0){
			return $query_po->row();
        }
        else{
            return false;
        }
	}
	
	function get_si_for_delete($DOrdNumber){
		$this->db->select('si.SInvNumber');
        $this->db->from('at_salesinvoice as si');
        $this->db->where('si.DOrdNumber', $DOrdNumber);
        $query_po=$this->db->get();
		
		if($query_po->num_rows() > 0){
			return $query_po->result();
        }
        else{
            return false;
        }
	}
	
	function get_sales_order($SOrdNumber){
		$this->db->select('so.SOrdNumber');
        $this->db->from('at_salesorder as so');
        $this->db->where('so.SOrdNumber', $SOrdNumber);
		$this->db->where_in('so.TransStatus', array(0));
        $query_po=$this->db->get();
		
		if($query_po->num_rows() > 0){
			return true;
        }
        else{
            return false;
        }
	}
	
	function get_delivery_order($SOrdNumber){
		$this->db->select('do.DOrdNumber');
        $this->db->from('at_deliveryorder as do');
        $this->db->where('do.SOrdNumber', $SOrdNumber);
		$this->db->where_in('do.TransStatus', array(1,2));
        $query_po=$this->db->get();
		
		if($query_po->num_rows() > 0){
			return true;
        }
        else{
            return false;
        }
	}

	function get_deliveryorder_data($DOrdNumber){
		$this->db->select('gr.DOrdNumber,gr.DOrdDate');
		$this->db->from($this->table.' as gr');
		$this->db->where('gr.DOrdNumber',$DOrdNumber);

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

		if($query->num_rows() > 0){
			return $query->row();
		}else{
			$result = [];
			return $result;
		}
	}

	function m_get_detail_deliveryordervariant($DOrdNumber){
		$this->db->select('dt.DOrdDetailID,dt.ItemCode,dt.ItemDesc,dt.ItemQty,dt.ItemUnit');
		$this->db->from($this->detail.' as dt');
		$this->db->where('dt.DOrdNumber',$DOrdNumber);

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

		return $query;
	}

	function get_BranchCodeDO($DOrdNumber){
		$this->db->select("BranchCode");
		$this->db->from($this->table.' as gr');
		$this->db->where("gr.DOrdNumber",$DOrdNumber);

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

		if($query->num_rows() > 0){
			$row = $query->row();
			return $row->BranchCode;
		}else{
			$BranchCode = "";
			return $BranchCode;
		}
	}

	function insert_variant($trans_data){

		$this->db->trans_start();

		for($x=0;$x<count($trans_data['insert_variant']['data']);$x++){
			$this->db->insert($this->variant,$trans_data['insert_variant']['data'][$x]);
		}

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

	function get_detail_variant($DOrdNumber){
		$this->db->select("gr.VrNumber,gr.ItemCode,gr.VrDesc,gr.ItemQty");
		$this->db->from($this->variant.' as gr');
		$this->db->where("gr.VrNumber",$DOrdNumber);
		$this->db->order_by("gr.VrDetailID","asc");

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

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