<?php
class CT_inv_detil_draft extends CI_Controller{
	function __construct()
	{
	  parent::__construct();
		if (!$this->dx_auth->is_logged_in()) {
			header('HTTP/1.1 401 Unauthorize');die('expire'); 
		}
		
		$uid = $this->session->userdata('DX_user_id');
		
		if(!permissionAuth(2) && $uid != RID){ 
			header('HTTP/1.1 401 Unauthorize');die('noaccess');
		}
		
		$this->load->model("exim/mdl_invoice", 'mdl_invoice');
		
	}
	
	function index(){
		$id_inv_hdr = (isset($_POST['id_inv_hdr']) && !empty($_POST['id_inv_hdr']))?$_POST['id_inv_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$data["id_inv_hdr"] = $id_inv_hdr;
		
		$q = $this->db->query("
			SELECT     v_noinv_dtl.partnumber, tbl_data_part_hs.part_no
			FROM         v_noinv_dtl LEFT OUTER JOIN
								  tbl_data_part_hs ON replace(v_noinv_dtl.partnumber,' ','') = tbl_data_part_hs.part_no2
			where tbl_data_part_hs.part_no is null and v_noinv_dtl.inv_hdr_id=$id_inv_hdr
			group by v_noinv_dtl.partnumber, tbl_data_part_hs.part_no
		");
		$rs = $q->result_array();
		//var_dump($rs); die();
		$datanull='';
		foreach ($rs as $row){
			$datanull.=$row['partnumber'].'|';		
		}
		if ($datanull != ''){
			$data['test'] = 'Data Part Tidak Match dengan Part HS : '.$datanull;
		} else {
			$data['test']=$datanull;
		}
			
		$this->load->view('exim/view_inv_detil_draft', $data);
	
	}
	
	function loaddata(){
				
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'inv_hdr_id'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$id_inv_hdr = (isset($_POST['id_inv_hdrx']) && !empty($_POST['id_inv_hdrx']))?$_POST['id_inv_hdrx']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$where = " a.[inv_hdr_id] = '".$id_inv_hdr."'";
		//echo $where;  die();
		//search toolbar
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if($k->field == "part_name"){
						$searchString = $k->data;
						$where .= " and b.part_name like  '%".$searchString."%'";   
					} else if ( $k->field == "part_name_customs" ) {
						$searchString = $k->data;
						$where .= " and b.part_name_customs like  '%".$searchString."%'"; 
					} else if ( $k->field == "r15" ) {
						$searchString = $k->data;
						$where .= " and a.r15 like  '%".$searchString."%'"; 
					} else if ( $k->field == "qty" ) {
						$searchString = $k->data;
						$where .= " and a.qty like  '%".$searchString."%'"; 
					} else if ( $k->field == "r7" ) {
						$searchString = $k->data;
						$where .= " and a.r7 like  '%".$searchString."%'"; 
					} else if ( $k->field == "amount" ) {
						$searchString = $k->data;
						$where .= " and a.amount like  '%".$searchString."%'"; 
					} else if ( $k->field == "kdval" ) {
						$searchString = $k->data;
						$where .= " and a.kdval like  '%".$searchString."%'"; 
					} else if ( $k->field == "totalamount" ) {
						$searchString = $k->data;
						$where .= " and a.totalamount like  '%".$searchString."%'"; 
					} else if ( $k->field == "pack" ) {
						$searchString = $k->data;
						$where .= " and a.pack like  '%".$searchString."%'"; 
					} 
				
				}
			}
		}
		
		//---
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_invoice->inv_all_dtl($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_invoice->inv_CountAll_dtl($where);
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
			$responce->rows[$i]['inv_dtl']=$r->inv_dtl; 
			$responce->rows[$i]['inv_hdr_id']=$r->inv_hdr_id; 
			$responce->rows[$i]['r15']=$r->r15; 
			$responce->rows[$i]['part_name']=$r->part_name; 
			$responce->rows[$i]['qty']=$r->qty; 
			$responce->rows[$i]['r7']=$r->r7; 
			$responce->rows[$i]['amount']=$r->amount; 
			$responce->rows[$i]['kdval']=$r->kdval; 
			$responce->rows[$i]['box']=$r->box; 
			$responce->rows[$i]['pack']=$r->pack; 
			$responce->rows[$i]['totalamount']=$r->totalamount; 
			$responce->rows[$i]['part_name_customs']=$r->part_name_customs; 
			
			$i++;
		}
		echo json_encode($responce);
		
			
		
	}
	
	function view_inv_summary(){
	
		$nobl = (isset($_POST['nobl']) or !empty($_POST['nobl']))?$_POST['nobl']:die('{"sts":"ERROR","desc":" Param bl Tidak Ditemukan"}');
		$tgbl = (isset($_POST['tgbl']) or !empty($_POST['tgbl']))?$_POST['tgbl']:die('{"sts":"ERROR","desc":" Param tgbl Tidak Ditemukan"}');
		$data["nobl"] = $nobl;
		$data["tgbl"] = $tgbl;
		$data["q"] = '1';
		$this->load->view('exim/view_inv_detil_draft_summary', $data);
	
	}
	
	function view_inv_summary1(){
	
		$nobl = (isset($_POST['id_inv_hdr']) or !empty($_POST['id_inv_hdr']))?$_POST['id_inv_hdr']:die('{"sts":"ERROR","desc":" Param bl Tidak Ditemukan"}');
		$tgbl = (isset($_POST['tgbl']) or !empty($_POST['tgbl']))?$_POST['tgbl']:'';
		
		$data["nobl"] = $nobl;
		$data["tgbl"] = date("Y-m-d");
		$data["q"] = '99';
		$this->load->view('exim/view_inv_detil_draft_summary', $data);
	
	}
	
	function loaddatasummary(){
		$search = isset($_POST['_search'])?$_POST['_search']:false;
        $searchField = isset($_POST['searchField'])?$_POST['searchField']:'';
        $searchString = isset($_POST['searchString'])?$_POST['searchString']:'';
        $filters =  isset($_POST['filters'])?$_POST['filters']:'';
		
		$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:20; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'inv_hdr_id'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$nobl = (isset($_POST['nobl']) && !empty($_POST['nobl']))?$_POST['nobl']:die('{"sts":"ERROR","desc":" NOBL Param Tidak Ditemukan"}');
		$tgbl = (isset($_POST['tgbl']) && !empty($_POST['tgbl']))?$_POST['tgbl']:die('{"sts":"ERROR","desc":" TGBL Param Tidak Ditemukan"}');
		
		$q = (isset($_POST['q']) && !empty($_POST['q']))?$_POST['q']:'';
		
		
		$where = " dbo.tbl_inv_hdr.nobl = '$nobl' and dbo.tbl_inv_hdr.tgbl = '$tgbl'   ";
		
		if ($q == '99') { $where = " dbo.tbl_inv_hdr.inv_hdr_id = '$nobl' ";  };
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_invoice->inv_all_dtl_summary($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_invoice->inv_CountAll_dtl_summary($where);
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
        foreach($records as $r){
			
			$responce->rows[$i]['part_name']=$r->part_name; 
			$responce->rows[$i]['sumqty']=$r->sumqty; 
			$responce->rows[$i]['sumamount']=$r->sumamount; 
			$responce->rows[$i]['r7']=$r->r7; 
			$responce->rows[$i]['sumprice']=$r->sumamountqty;//sumamount * $r->sumqty; 
			$responce->rows[$i]['sumbox']=$r->sumbox; 
			$responce->rows[$i]['kode_barang']=$r->kode_barang; 
			
			
			
			
		$i++;
		}
		echo json_encode($responce);
	
	}
	
	function update_part_inv(){
		$inv_hdr_idxxx = (isset($_POST['inv_hdr_idxxx']) && !empty($_POST['inv_hdr_idxxx']))?$_POST['inv_hdr_idxxx']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$part_number_invxxx = (isset($_POST['part_number_invxxx']) && !empty($_POST['part_number_invxxx']))?$_POST['part_number_invxxx']:die('{"sts":"ERROR","desc":" Param car Tidak Ditemukan"}');
		$inv_dtlxxx = (isset($_POST['inv_dtlxxx']) && !empty($_POST['inv_dtlxxx']))?$_POST['inv_dtlxxx']:die('{"sts":"ERROR","desc":" Param car Tidak Ditemukan"}');
		
		$in_inv = $this->db->query("
			
			BEGIN TRY
				update [dbo].[tbl_inv_dtl] set [r4] = '$part_number_invxxx ' where [inv_dtl]=$inv_dtlxxx and [inv_hdr_id]=$inv_hdr_idxxx	
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			select '00' as status, ' SUKSES UPDATED' as description
			
		");
		$rs = $in_inv->result_array();
        //$r = $in_inv->result();		
		//var_dump($r);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	
	}
	
	
	function update_part_inv_coo(){
		$inv_hdr_idxxx = (isset($_POST['inv_hdr_idxxx']) && !empty($_POST['inv_hdr_idxxx']))?$_POST['inv_hdr_idxxx']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		$part_number_invxxx = (isset($_POST['part_number_invxxx']) && !empty($_POST['part_number_invxxx']))?$_POST['part_number_invxxx']:die('{"sts":"ERROR","desc":" Param car Tidak Ditemukan"}');
		$inv_dtlxxx = (isset($_POST['inv_dtlxxx']) && !empty($_POST['inv_dtlxxx']))?$_POST['inv_dtlxxx']:die('{"sts":"ERROR","desc":" Param car Tidak Ditemukan"}');
		
		$in_inv = $this->db->query("
			
			BEGIN TRY
				update [dbo].[tbl_coo_data_dtl] set [ITEM_NUMBER] = '$part_number_invxxx ' where [id_coo]=$inv_dtlxxx and [id_coo_data]=$inv_hdr_idxxx	
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			select '00' as status, ' SUKSES UPDATED' as description
			
		");
		$rs = $in_inv->result_array();
        //$r = $in_inv->result();		
		//var_dump($r);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	
	}

}

?>