<?php
class CT_fg extends CI_Controller{
    function __construct()
    {
        parent::__construct();
		if (!$this->dx_auth->is_logged_in()) {
            redirect('');  
            die(); 
        }
        
        $uid = $this->session->userdata('DX_user_id');
        $username = $this->session->userdata('DX_username');
        
        if(!permissionAuth(2) && $uid != RID){ 
            header('HTTP/1.1 401 Unauthorize');die('noaccess');
        } 
		
		$this->load->model("inventory/mdl_incoming", 'mdl_incoming');
		$this->load->model("inventory/mdl_outgoing", 'mdl_outgoing');
    }

    function index(){
         $this->load->view('inventory/view_fg');
    }    
    
    function loadDataGrid(){
		$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']:'tbl_data_in_hdr.id_data_in_hdr'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$where = " tbl_data_in_hdr.doc_in_from_id=1 ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "doc_in_no" ) {
						$searchString = $k->data;
						$where .= " and tbl_data_in_hdr.doc_in_no like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		//$start = $page;
		$records = $this->mdl_incoming->dataincoming_finishgood_all($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_incoming->dataincoming_finishgood_CountAll($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]['id_data_in_hdr']=$r->id_data_in_hdr; 
			$responce->rows[$i]['doc_in_no']=$r->doc_in_no; 
			$responce->rows[$i]['doc_in_from_id']=$r->doc_in_from_id; 
			$responce->rows[$i]['doc_in_date']=$r->doc_in_date;
			$responce->rows[$i]['id_data_in_hdr_h_status']=$r->id_data_in_hdr_h_status;
			$responce->rows[$i]['id_terkait']=$r->id_terkait;
			$responce->rows[$i]['status']=$r->status;
			$responce->rows[$i]['doc_in_from_name']=$r->doc_in_from_name;
					
			
			$i++;
		}
		echo json_encode($responce);
		
		
	}
	
	function view_fg_load_data(){
	
		 $this->load->view('inventory/view_fg_load_data');
	
	}
	
	function data_fg_load_data(){
		$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']:'id_bom_hdr'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$where = " status='new' ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "exec_date" ) {
						$searchString = $k->data;
						$where .= " and exec_date =  '$searchString'"; 
					} else if ( $k->field == "filename" ) {
						$searchString = $k->data;
						$where .= " and filename like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_incoming->datatbl_incoming_fg_all($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_incoming->datatbl_incoming_fg_CountAll($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]['id_incoming_fg_hdr']=$r->id_incoming_fg_hdr; 
			$responce->rows[$i]['from_file_name']=$r->from_file_name; 
			$responce->rows[$i]['exec_date']=$r->exec_date; 
			$responce->rows[$i]['status']=$r->status;
			$responce->rows[$i]['itemcount']=$r->itemcount;
			$responce->rows[$i]['incoming_date']=$r->incoming_date;
			$responce->rows[$i]['countrow']=$r->countrow;
				
				
			$i++;
		}
		echo json_encode($responce);
	
	
	}
	
	function get_fg_load_data(){
	
	
		$qry = $this->db->query("
			BEGIN TRY
			 exec('sp_incoming_finisgood')
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			 ");
		 $rs = $qry->result_array();
         // $r = $qry->result();		
		 // var_dump($r);die();
		 foreach ($rs as $row){
			 $responce = array("sts" => $row['status'], "desc" => $row['description'].' ');
		 }  
		  echo json_encode($responce);
		 // $stmt = " exec('sp_incoming_finisgood') ";
		 // $qry = mssql_query($stmt); 
		 // $row = mssql_fetch_array($qry); 
		 // var_dump($row);die();
		
	}
	
	function view_fg_view_load_dtl(){
		
		$id_incoming_fg_hdr = (isset($_POST['id_incoming_fg_hdr']) && !empty($_POST['id_incoming_fg_hdr']))?$_POST['id_incoming_fg_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$data["id_incoming_fg_hdr"] = $id_incoming_fg_hdr;
		$this->load->view('inventory/view_fg_view_load_dtl', $data);
	
	}
	
	function data_fg_view_load_dtl(){
	
	
		$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']:'id_incoming_fg_dtl'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$id_incoming_fg_hdr = (isset($_POST['id_incoming_fg_hdr']) && !empty($_POST['id_incoming_fg_hdr']))?$_POST['id_incoming_fg_hdr']:die('{"sts":"ERROR","desc":" id_incoming_fg_hdr Param Tidak Ditemukan!"}');
		
		$where = " id_incoming_fg=$id_incoming_fg_hdr ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "ASSY_NO" ) {
						$searchString = $k->data;
						$where .= " and ASSY_NO like  '%".$searchString."%'"; 
					} else if ( $k->field == "PROD_DATE" ) {
						$searchString = $k->data;
						$where .= " and PROD_DATE like  '%".$searchString."%'"; 
					} else if ( $k->field == "PERIODE" ) {
						$searchString = $k->data;
						$where .= " and PERIODE like  '%".$searchString."%'"; 
					} else if ( $k->field == "LINE_NO" ) {
						$searchString = $k->data;
						$where .= " and LINE_NO = '$searchString'"; 
					} else if ( $k->field == "LOADING_LINE" ) {
						$searchString = $k->data;
						$where .= " and LOADING_LINE like  '%".$searchString."%'"; 
					} else if ( $k->field == "ASSY_NO" ) {
						$searchString = $k->data;
						$where .= " and ASSY_NO = '$searchString'"; 
					} else if ( $k->field == "ASSY_CODE" ) {
						$searchString = $k->data;
						$where .= " and ASSY_CODE like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_incoming->data_fg_view_load_dtl_all($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_incoming->data_fg_view_load_dtl_CountAll($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]['id_incoming_fg_dtl']=$r->id_incoming_fg_dtl; 
			$responce->rows[$i]['id_incoming_fg']=$r->id_incoming_fg; 
			$responce->rows[$i]['PROD_DATE']=$r->PROD_DATE; 
			$responce->rows[$i]['PERIODE']=$r->PERIODE;
			$responce->rows[$i]['LINE_NO']=$r->LINE_NO;
			$responce->rows[$i]['LOADING_LINE']=$r->LOADING_LINE;
			$responce->rows[$i]['ASSY_NO']=$r->ASSY_NO;
			$responce->rows[$i]['ASSY_CODE']=$r->ASSY_CODE;
			$responce->rows[$i]['QTY_BOX_SF']=$r->QTY_BOX_SF;
			$responce->rows[$i]['QTY_BOX_AF']=$r->QTY_BOX_AF;
			$responce->rows[$i]['ATOT_BOX_SF']=$r->ATOT_BOX_SF;
			$responce->rows[$i]['ATOT_PCS_SF']=$r->ATOT_PCS_SF;
			$responce->rows[$i]['ATOT_BOX_AF']=$r->ATOT_BOX_AF;
			$responce->rows[$i]['ATOT_PCS_AF']=$r->ATOT_PCS_AF;
			$responce->rows[$i]['BTOT_BOX_SF']=$r->BTOT_BOX_SF;
			$responce->rows[$i]['BTOT_PCS_SF']=$r->BTOT_PCS_SF;
			$responce->rows[$i]['BTOT_BOX_AF']=$r->BTOT_BOX_AF;
			$responce->rows[$i]['BTOT_PCS_AF']=$r->BTOT_PCS_AF;
			$responce->rows[$i]['CTOT_BOX_SF']=$r->CTOT_BOX_SF;
			$responce->rows[$i]['CTOT_PCS_SF']=$r->CTOT_PCS_SF;
			$responce->rows[$i]['CTOT_BOX_AF']=$r->CTOT_BOX_AF;
			$responce->rows[$i]['CTOT_PCS_AF']=$r->CTOT_PCS_AF;
			$responce->rows[$i]['GROUP_NO']=$r->GROUP_NO;
			$responce->rows[$i]['LBJ_NO']=$r->LBJ_NO;
				
			$i++;
		}
		echo json_encode($responce);
	
	
	
	}
	
	function view_fg_dtl(){
	    
		$id_incominghdr = (isset($_POST['id_incominghdr']) && !empty($_POST['id_incominghdr']))?$_POST['id_incominghdr']:die('{"sts":"ERROR","desc":" Param id_incominghdr Tidak Ditemukan"}');
		$data["id_incominghdr"] = $id_incominghdr;
		$this->load->view('inventory/view_fg_dtl', $data);
	
	}
	
	function data_fg_dtl(){
	
		$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']:'id_data_in_dtl'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction
		
		$id_incominghdr = (isset($_POST['id_incominghdr']) && !empty($_POST['id_incominghdr']))?$_POST['id_incominghdr']:die('{"sts":"ERROR","desc":" Param id_incominghdr Tidak Ditemukan"}');
		
		$where = " id_data_in_hdr=$id_incominghdr ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "part_name_customs" ) {
						$searchString = $k->data;
						$where .= " and part_name_customs like  '%".$searchString."%'"; 
					} else if ( $k->field == "part_number" ) {
						$searchString = $k->data;
						$where .= " and part_number like  '%".$searchString."%'"; 
					} else if ( $k->field == "qty_in" ) {
						$searchString = $k->data;
						$where .= " and qty_in =  $searchString"; 
					} else if ( $k->field == "unit_in" ) {
						$searchString = $k->data;
						$where .= " and unit_in like  '%".$searchString."%'"; 
					}  else if ( $k->field == "box" ) {
						$searchString = $k->data;
						$where .= " and box  =  $searchString"; 
					}  else if ( $k->field == "pack" ) {
						$searchString = $k->data;
						$where .= " and pack =  $searchString";
					} 
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_incoming->data_incoming_dtl_All($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_incoming->data_incoming_dtl_CountAll($where);
	    //var_dump($records); die();
		
		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]['id_data_in_dtl']=$r->id_data_in_dtl; 
			$responce->rows[$i]['id_data_in_hdr']=$r->id_data_in_hdr; 
			$responce->rows[$i]['doc_in_from_id']=$r->doc_in_from_id; 
			$responce->rows[$i]['id_part_number']=$r->id_part_number;
			$responce->rows[$i]['part_name_customs']=$r->part_name_customs;
			$responce->rows[$i]['part_number']=$r->part_number;
			$responce->rows[$i]['qty_in']=$r->qty_in;
			$responce->rows[$i]['qty_out']=$r->qty_out;
			$responce->rows[$i]['unit_in']=$r->unit_in;
			$responce->rows[$i]['unit_konversi']=$r->unit_konversi;
			$responce->rows[$i]['pengali']=$r->pengali;
			$responce->rows[$i]['box']=$r->box;
			$responce->rows[$i]['pack']=$r->pack;
			$responce->rows[$i]['harga']=$r->harga;
			$responce->rows[$i]['kdval']=$r->kdval;
			$responce->rows[$i]['type_brg']=$r->type_brg;
			$responce->rows[$i]['part_no']=$r->part_no;
			
			$i++;
		}
		echo json_encode($responce);
		
	
	}
	
	
	
	
	function proses_incoming_fg(){
		$id_incoming_fg_hdr = (isset($_POST['id_incoming_fg_hdr']) && !empty($_POST['id_incoming_fg_hdr']))?$_POST['id_incoming_fg_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$doc_in_from_id = (isset($_POST['doc_in_from_id']) && !empty($_POST['doc_in_from_id']))?$_POST['doc_in_from_id']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$incoming_date = (isset($_POST['incoming_date']) && !empty($_POST['incoming_date']))?$_POST['incoming_date']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		//$this->session->userdata('DX_user_id');
		$stmt = " exec('spi_incoming_fg 
				@id_hdr = $id_incoming_fg_hdr,
				@doc_in_from_id = $doc_in_from_id,
				@date_incoming=N''".$incoming_date."''') ";
		//echo $stmt;
		$qry = $this->db->query($stmt);
		 $rs = $qry->result_array();
         // $r = $qry->result();		
		 // var_dump($r);die();
		 foreach ($rs as $row){
			 $responce = array("sts" => $row['status'], "desc" => $row['description']);
		 }  
		  echo json_encode($responce);
		 // $stmt = "  exec('spi_incoming_fg 
				// @id_hdr = $id_incoming_fg_hdr,
				// @doc_in_from_id = $doc_in_from_id')
		 
						// ') ";
		 // $qry = mssql_query($stmt); 
		 // $row = mssql_fetch_array($qry); 
		 // var_dump($row);die();
	
	
	}
	
	function view_fg_approved(){
		$idx = (isset($_POST['idx']) && !empty($_POST['idx']))?$_POST['idx']:die('{"sts":"ERROR","desc":" Param idx Tidak Ditemukan"}');
		$doc_in_from_id = (isset($_POST['doc_in_from_id']) && !empty($_POST['doc_in_from_id']))?$_POST['doc_in_from_id']:die('{"sts":"ERROR","desc":" Param doc_in_from_id Tidak Ditemukan"}');
		$doc_in_no = (isset($_POST['doc_in_no']) && !empty($_POST['doc_in_no']))?$_POST['doc_in_no']:die('{"sts":"ERROR","desc":" Param doc_in_no Tidak Ditemukan"}');
		$idterkait = (isset($_POST['idterkait']) && !empty($_POST['idterkait']))?$_POST['idterkait']:die('{"sts":"ERROR","desc":" Param idterkait Tidak Ditemukan"}');
		$typedok = (isset($_POST['typedok']) && !empty($_POST['typedok']))?$_POST['typedok']:die('{"sts":"ERROR","desc":" Param typedok Tidak Ditemukan"}');
		$doc_in_date = (isset($_POST['doc_in_date']) && !empty($_POST['doc_in_date']))?$_POST['doc_in_date']:'';
		$noinv = (isset($_POST['noinv']) && !empty($_POST['noinv']))?$_POST['noinv']:'';
		$tginv = (isset($_POST['tginv']) && !empty($_POST['tginv']))?$_POST['tginv']:'';
		
		
		$data["idx"] = $idx;
		$data["doc_in_from_id"] = $doc_in_from_id;
		$data["doc_in_no"] = $doc_in_no;
		$data["typedok"] = $typedok;
		$data["doc_in_date"] = $doc_in_date;
		$data["noinv"] = $noinv;
		$data["tginv"] = $tginv;
		$data["idterkait"] = $idterkait;
		$this->load->view('inventory/view_fg_approved', $data);
		
	}
	function approve_incoming_fg(){
	
		$doc_in_from_id = (isset($_POST['doc_in_from_id']) && !empty($_POST['doc_in_from_id']))?$_POST['doc_in_from_id']:die('{"sts":"ERROR","desc":" Param  Tidak Ditemukan"}');
		$doc_in_no = (isset($_POST['doc_in_no']) && !empty($_POST['doc_in_no']))?$_POST['doc_in_no']:die('{"sts":"ERROR","desc":" Param  Tidak Ditemukan"}');
		$incoming_recieve_date = (isset($_POST['incoming_recieve_date']) && !empty($_POST['incoming_recieve_date']))?$_POST['incoming_recieve_date']:die('{"sts":"ERROR","desc":" Param  DATE Tidak Ditemukan"}');
		$id_data_in_hdr = (isset($_POST['id_data_in_hdr']) && !empty($_POST['id_data_in_hdr']))?$_POST['id_data_in_hdr']:die('{"sts":"ERROR","desc":" Param  id_data_in_hdr Tidak Ditemukan"}');
		$id_terkait = (isset($_POST['id_terkait']) && !empty($_POST['id_terkait']))?$_POST['id_terkait']:die('{"sts":"ERROR","desc":" Param  id_terkait Tidak Ditemukan"}');
		
		
		if (strlen($incoming_recieve_date) != 10){
			echo '{"sts":"ERROR","desc":" Tanggal Salah"}';
			die();
		} 
		
		$pos = strpos($incoming_recieve_date, '/');
		
		if ($pos != 2) {
				echo '{"sts":"ERROR","desc":" Tanggal Salah"}';
				die();
			}
		
		//$user = $this->session->userdata('DX_user_id');
		$user = $this->session->userdata('DX_username');
		$in_inv = $this->db->query("
			
			EXEC [spi_incoming_approved] 
			@doc_in_from_id = $doc_in_from_id,
			@doc_in_no = '$doc_in_no',
			@doc_in_date = '$incoming_recieve_date',
			@id_data_in_hdr = $id_data_in_hdr,
			@id_terkait = $id_terkait,
			@user = $user
			");
		$rs = $in_inv->result_array();
        //$r = $in_inv->result();		
		//var_dump($rs);die();
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);
	
	
	
	}
	
	function view_fg_uploadfg(){
	
		$this->load->view('inventory/view_fg_uploadfg');
	
	}
	
	function data_uploadfgin(){
	
		$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']:'id_invoice_upload'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$where = " 1=1 ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "filename" ) {
						$searchString = $k->data;
						$where .= " and filename like  '%".$searchString."%'"; 
					} else if ( $k->field == "date_upload" ) {
						$searchString = $k->data;
						$where .= " and CONVERT(VARCHAR(10),[date_upload],101)  =  '$searchString'"; 
					} else if ( $k->field == "status_upload" ) {
						$searchString = $k->data;
						$where .= " and status_upload like  '%".$searchString."%'"; 
					}  else if ( $k->field == "create_by" ) {
						$searchString = $k->data;
						$where .= " and create_by like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_incoming->upload_fginAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_incoming->upload_fgincountAll($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]['id_fgin_upload']=$r->id_fgin_upload; 
			$responce->rows[$i]['date_upload']=$r->date_upload; 
			$responce->rows[$i]['filename']=$r->filename; 
			$responce->rows[$i]['status_upload']=$r->status_upload;
			$responce->rows[$i]['create_date']=$r->create_date;
			$responce->rows[$i]['create_by']=$r->create_by;
			$responce->rows[$i]['tipe']=$r->tipe;
			$responce->rows[$i]['filepath']=$r->filepath;
			
			$i++;
		}
		echo json_encode($responce);	
	
	
	}
	
	function upload_file_fgin(){
	
	   $status = "";
	   $msg = "";
	   $file_element_name = 'userfilefgin';
	   //$username = $this->session->userdata('DX_user_id');
	   $username = $this->session->userdata('DX_username');
	   $tipe = 'fgin';
	   if ($status != "error")
	   {
		  $apppath = $this->config->item('APP_upload_path');
		  $config['upload_path'] = $apppath.'inventory/finishgood/';
		  $config['allowed_types'] = 'csv';
		  $config['max_size']  = 1024 * 100;
		  $config['encrypt_name'] = false;
		  $config['overwrite']=true;
		  $config['remove_spaces']=true;
		  $this->load->library('upload', $config);
		  if (!$this->upload->do_upload($file_element_name))
		  {
			 $status = 'error';
			 $msg = $this->upload->display_errors('', '');
		  }
		  else
		  {
			 $data = $this->upload->data();
			 
			 // print_r($data);
			 // die();
			 if($data)
			 {
				$myFile = $data['full_path']; 
				$newfile=$apppath.'inventory/finishgood/log/'.$data['file_name'];
				$filename=$data['file_name'];
				
				
			//echo " exec('[spuload_fgin] @filepath=''$myFile'', @filename=''$filename'', @username=''$username'', @tipe=''$tipe'', @trx=''add'', @id=0 ')"; die();
				
				$qry = $this->db->query("
					BEGIN TRY
					
					 exec('[spuload_fgin] @filepath=''$myFile'', @filename=''$filename'', @username=''$username'', @tipe=''$tipe'', @trx=''add'', @id=0 ')
					
					END TRY
					BEGIN CATCH 
						SELECT 
						'error' AS status
						,ERROR_MESSAGE() AS description;
						
					END CATCH;
					
					");
				 $rs = $qry->result_array();
				 // $r = $qry->result();		
				 // var_dump($r);die();
				 foreach ($rs as $row){
					 $status = $row['status'];
					 $msg = $row['description'];
				 }  
				//  echo json_encode($responce);
				 // $stmt = " exec('[spuload_fgin]') ";
				 // $qry = mssql_query($stmt); 
				 // $row = mssql_fetch_array($qry); 
				 // var_dump($row);die();
						
				
				
				
				$status = $status;
				$msg = $msg;
			 }
			 else
			 {
				unlink($data['full_path']);
				$status = "error";
				$msg = "Something went wrong when saving the file, please try again.";
			 }
		  }
		  @unlink($_FILES[$file_element_name]);
	   }
	   echo json_encode(array('status' => $status, 'msg' => $msg));
	
	
	}
	
	
	function deletefile_fgin(){
		$filename = (isset($_POST['filename']) && !empty($_POST['filename']))?$_POST['filename']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$tipe = (isset($_POST['tipe']) && !empty($_POST['tipe']))?$_POST['tipe']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$id_fgin_upload = (isset($_POST['id_fgin_upload']) && !empty($_POST['id_fgin_upload']))?$_POST['id_fgin_upload']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		//$username = $this->session->userdata('DX_user_id');
	    $username = $this->session->userdata('DX_username');
		$apppath = $this->config->item('APP_upload_path');
		
		$path=$apppath.'inventory/finishgood/'.$filename;
		$pathnew=$apppath.'inventory/finishgood/log/'.$filename;
		
		if (!file_exists($path)) {
		   echo json_encode(array('sts' => '00', 'desc' => 'file does not exist'));
			die();
		}
		
		
		
		$qry = $this->db->query("
			BEGIN TRY
			
			 exec('[spuload_fgin] @filepath=''$path'', @filename=''$filename'', @username=''$username'', @tipe=''$tipe'', @trx=''delete'', @id=$id_fgin_upload ')
			
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			");
		 $rs = $qry->result_array();
		 // $r = $qry->result();		
		 // var_dump($r);die();
		 foreach ($rs as $row){
			 $status = $row['status'];
			 $msg = $row['description'];
		 }  
		//  echo json_encode($responce);
		 // $stmt = " exec('[spuload_fgin]') ";
		 // $qry = mssql_query($stmt); 
		 // $row = mssql_fetch_array($qry); 
		 // var_dump($row);die();
		
		$status = $status;
		$msg = $msg;
		
		if ($status=='00'){
			copy($path, $pathnew) or die(array('sts' => '00','desc' => "Unable to copy $path to $pathnew."));
			if (file_exists($path)) {
				unlink($path);
			} else {
				echo json_encode(array('sts' => '00', 'desc' => 'file does not exist'));
				die();
			}
		}
		echo json_encode(array('sts' => $status, 'desc' => $msg));
	
	
	}
	
	function view_fg_data_uploaded(){
	
		$this->load->view('inventory/view_fg_data_uploaded');
	
	}
	
	function view_fg_out(){
	
		$this->load->view('inventory/view_fg_out');
	
	}
	
	function loaddata_fg_out(){
		$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']:'tbl_data_out_hdr.id_data_out_hdr'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$where = " 1=1 and  (tbl_data_out_hdr.doc_out_to_id = 2 or tbl_data_out_hdr.doc_out_to_id = 3 or tbl_data_out_hdr.doc_out_to_id = 4 or tbl_data_out_hdr.doc_out_to_id =5 ) ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "doc_out_to_id" ) {
						$searchString = $k->data;
						$where .= " and tbl_data_out_hdr.doc_out_to_id =$searchString "; 
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_outgoing->data_out_hdr_All($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_outgoing->data_out_hdr_CountAll($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){
			if ($r->doc_out_to_id == 2){
				$tipe='BC30';
			} else if ($r->doc_out_to_id == 3){
				$tipe='BC25';
			}  else if ($r->doc_out_to_id == 4){
				$tipe='BC41';
			} 	else if ($r->doc_out_to_id == 5){
				$tipe='BC27';
			} 	
			
			$responce->rows[$i]['id_data_out_hdr']=$r->id_data_out_hdr; 
			$responce->rows[$i]['doc_out_no']=$r->doc_out_no; 
			$responce->rows[$i]['doc_out_date']=$r->doc_out_date; 
			$responce->rows[$i]['doc_out_to_id']=$tipe;//$r->doc_out_to_id;
			$responce->rows[$i]['id_data_out_hdr_h_status']= $r->id_data_out_hdr_h_status;
			$responce->rows[$i]['id_terkait']=$r->id_terkait;
			$responce->rows[$i]['status']=$r->status;
			$responce->rows[$i]['approve_date']=$r->approve_date;
			
			$i++;
		}
		echo json_encode($responce);
	}
	
	function view_fg_out_proses(){
	
		$this->load->view('inventory/view_fg_out_proses');
	
	
	}
	
	function loaddata_fg_out_proses(){
		
		$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']:'ctedata.id_bc_out'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$where = " 1=1  and ctedata.statusdok='DATA PROCESSED' ";
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_outgoing->datainv_proses_out_all($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_outgoing->datainv_proses_out_CountAll($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;
		
		//var_dump($records);
		
		$i=0;
		foreach($records as $r){
						
			$responce->rows[$i]['id_bc_out']= $r['id_bc_out']; 
			$responce->rows[$i]['namapembeli']=$r['namapembeli']; 
			$responce->rows[$i]['statusdok']=$r['statusdok'].' IN EXIM'; 
			$responce->rows[$i]['typedok']=$r['typedok']; 
			$i++;
		}
		echo json_encode($responce);
	  	
	}
	
	function view_fg_out_proses_dtl(){
		$id_bc_out = (isset($_POST['id_bc_out']) && !empty($_POST['id_bc_out']))?$_POST['id_bc_out']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$statusdok = (isset($_POST['statusdok']) && !empty($_POST['statusdok']))?$_POST['statusdok']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$typedok = (isset($_POST['typedok']) && !empty($_POST['typedok']))?$_POST['typedok']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$data['id_bc_out'] = $id_bc_out;
		$data['statusdok'] = $statusdok;
		$data['typedok'] = $typedok;
		
		$this->load->view('inventory/view_fg_out_proses_dtl', $data);
	
	}
	
	function loaddata_fg_out_proses_dtl(){
	
		$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']:'ctedata.id_bc_out'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'asc'; // get the direction	
		
		$id_bc_out = (isset($_POST['id_bc_out']) && !empty($_POST['id_bc_out']))?$_POST['id_bc_out']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$typedok = (isset($_POST['typedok']) && !empty($_POST['typedok']))?$_POST['typedok']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		//$typedok='BC30';
		//echo $id_bc_out; die();
		$where = " 1=1 and id_bc_out=$id_bc_out and typebc='$typedok'  ";
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_outgoing->detilitem_proses_out_all($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_outgoing->detilitem_proses_out_CountAll($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;
		
		//var_dump($records);
		
		$i=0;
		foreach($records as $r){
						
			$responce->rows[$i]['id_bc_out']= $r['id_bc_out']; 
			$responce->rows[$i]['typebc']=$r['typebc']; 
			$responce->rows[$i]['ASSY_CODE']=$r['ASSY_CODE'];
			$responce->rows[$i]['ASSY_NO']=$r['ASSY_NO']; 
			$responce->rows[$i]['TOTAL_SET']=$r['TOTAL_SET']; 
			$responce->rows[$i]['SATUAN']=$r['SATUAN']; 
			$responce->rows[$i]['NETDET']=$r['NETDET']; 
			$responce->rows[$i]['KGM']=$r['KGM']; 
			$i++;
		}
		echo json_encode($responce);	
	
	}
		
	function prosesdatatofg(){
		$id_bc_out = (isset($_POST['id_bc_out']) && !empty($_POST['id_bc_out']))?$_POST['id_bc_out']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$typedok = (isset($_POST['typedok']) && !empty($_POST['typedok']))?$_POST['typedok']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$username = $this->session->userdata('DX_username');
      
		$qry = $this->db->query("
			BEGIN TRY
			
			 exec('[spi_prosesdatatofg] @id_bc_out=$id_bc_out, @typedok=''$typedok'', @username=''$username'' ')
			
			END TRY
			BEGIN CATCH 
				SELECT 
				'06' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			");
		 $rs = $qry->result_array();
		 // $r = $qry->result();		
		 // var_dump($r);die();
		 foreach ($rs as $row){
			 $status = $row['status'];
			 $msg = $row['description'];
		 }  
		//  echo json_encode($responce);
		 // $stmt = " exec('[spi_prosesdatatofg]') ";
		 // $qry = mssql_query($stmt); 
		 // $row = mssql_fetch_array($qry); 
		 // var_dump($row);die();
		
		$status = $status;
		$msg = $msg;
		
		echo json_encode(array('sts' => $status, 'desc' => $msg));
	
	}	
	
	function view_fg_out_dtl(){
		
		$id_data_out_hdr = (isset($_POST['id_data_out_hdr']) && !empty($_POST['id_data_out_hdr']))?$_POST['id_data_out_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$data['id_data_out_hdr'] = $id_data_out_hdr;
		$this->load->view('inventory/view_fg_out_dtl', $data);
		
	
	}
	
	function loaddata_fg_out_dtl(){
	
		$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']:'id_data_out_hdr'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'asc'; // get the direction	
		
		$id_data_out_hdr = (isset($_POST['id_data_out_hdr']) && !empty($_POST['id_data_out_hdr']))?$_POST['id_data_out_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');

		
		$where = " 1=1 and id_data_out_hdr=$id_data_out_hdr   ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "out_date" ) {
						$searchString = $k->data;
						$where .= " and out_date = '$searchString'"; 
					} else if ( $k->field == "part_name_customs" ) {
						$searchString = $k->data;
						$where .= " and part_name_customs like  '%".$searchString."%'"; 
					} else if ( $k->field == "assy_code" ) {
						$searchString = $k->data;
						$where .= " and assy_code like  '%".$searchString."%'"; 
					} else if ( $k->field == "part_number" ) {
						$searchString = $k->data;
						$where .= " and part_number like  '%".$searchString."%'"; 
					} else if ( $k->field == "qty" ) {
						$searchString = $k->data;
						$where .= " and qty=$searchString"; 
					}
				}
			}
		}
		
		
			
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_outgoing->data_stocktransfer_dtl_all($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_outgoing->data_stocktransfer_dtl_CountAll($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]['id_data_out_dtl']=$r->id_data_out_dtl; 
			$responce->rows[$i]['id_data_out_hdr']=$r->id_data_out_hdr;
			$responce->rows[$i]['id_part_number']=$r->id_part_number;
			$responce->rows[$i]['out_date']=$r->out_date;
			$responce->rows[$i]['part_name_customs']=$r->part_name_customs;
			$responce->rows[$i]['part_number']=$r->part_number;
			$responce->rows[$i]['qty']=$r->qty;
			$responce->rows[$i]['qty_supply']=$r->qty_supply;
			$responce->rows[$i]['unit_out']=$r->unit_out;
			$responce->rows[$i]['unit_konversi']=$r->unit_konversi;
			$responce->rows[$i]['pengali']=$r->pengali;
			$responce->rows[$i]['box']=$r->box;
			$responce->rows[$i]['pack']=$r->pack;
			$responce->rows[$i]['assy_code']=$r->assy_code;
			
			$i++;
		}
		
		echo json_encode($responce);
	
	
	}
    
	function view_fg_out_approved(){
		
		$id_data_out_hdr = (isset($_POST['id_data_out_hdr']) && !empty($_POST['id_data_out_hdr']))?$_POST['id_data_out_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$doc_out_no = (isset($_POST['doc_out_no']) && !empty($_POST['doc_out_no']))?$_POST['doc_out_no']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$doc_out_to_id = (isset($_POST['doc_out_to_id']) && !empty($_POST['doc_out_to_id']))?$_POST['doc_out_to_id']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$car = '';
		$tgdaft = '';
		$nodaft = '';
		$qry = $this->db->query("
			declare @id_terkait bigint
			SELECT 
				  @id_terkait=[id_terkait]
			  
			  FROM [dbo].[tbl_data_out_hdr]
			  where id_data_out_hdr = $id_data_out_hdr
			select CAR, NODAFT, convert(date, TGDAFT, 113) as TGDAFT from  [dbo].[tbl_bc_out_hdr] where id_bc_out_hdr = @id_terkait
		");
		
		 $rs = $qry->result_array();
		 foreach ($rs as $row){
		    $car = $row["CAR"]; 
			$tgdaft = $row["NODAFT"]; 
			$nodaft = $row["TGDAFT"]; 
			 
		 }  
		 
		$data['car'] = $car;
		$data['tgdaft'] = $tgdaft;
		$data['nodaft'] = $nodaft;
		$data['id_data_out_hdr'] = $id_data_out_hdr;
		$data['doc_out_no'] = $doc_out_no;
		$data['doc_out_to_id'] = $doc_out_to_id;
		$this->load->view('inventory/view_fg_out_approved', $data);
		
		
	
	}
	
	function prosesdatatofg_approved(){
		$id_bc_out = (isset($_POST['id_bc_out']) && !empty($_POST['id_bc_out']))?$_POST['id_bc_out']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$typedok = (isset($_POST['typedok']) && !empty($_POST['typedok']))?$_POST['typedok']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$tglfg_out = (isset($_POST['tglfg_out']) && !empty($_POST['tglfg_out']))?$_POST['tglfg_out']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$tglfg_out_apporved = (isset($_POST['tglfg_out_apporved']) && !empty($_POST['tglfg_out_apporved']))?$_POST['tglfg_out_apporved']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$tglfg_out_a = explode('/' , $tglfg_out );
		$tglfg_out_apporved_a = explode('/', $tglfg_out_apporved); 
		
		$tglfg_out=$tglfg_out_a['2'].'-'.$tglfg_out_a['1'].'-'.$tglfg_out_a['0'];
		$tglfg_out_apporved = $tglfg_out_apporved_a['2'].'-'.$tglfg_out_apporved_a['1'].'-'.$tglfg_out_apporved_a['0'];
		
		/* $tglfg_out=$tglfg_out_a['1'].'/'.$tglfg_out_a['0'].'/'.$tglfg_out_a['2'];
		$tglfg_out_apporved=$tglfg_out_apporved_a['1'].'/'.$tglfg_out_apporved_a['0'].'/'.$tglfg_out_apporved_a['2']; */
				
		$username = $this->session->userdata('DX_username');
      
		$qry = $this->db->query("
			BEGIN TRY
			 exec('spfg_out_approved @id_bc_out=$id_bc_out, @typedok=''$typedok'', @tglfg_out=''$tglfg_out'', @tglfg_out_apporved=''$tglfg_out_apporved'', @username=''$username'' ')
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH;
			
			");
		 $rs = $qry->result_array();
         // $r = $qry->result();		
		 // var_dump($r);die();
		 foreach ($rs as $row){
			 $responce = array("sts" => $row['status'], "desc" => $row['description'].' ');
		 }  
		  echo json_encode($responce);
		 // $stmt = " exec('sp_incoming_finisgood') ";
		 // $qry = mssql_query($stmt); 
		 // $row = mssql_fetch_array($qry); 
		 // var_dump($row);die();
		
	}
	
	function view_fg_load_stock(){
	
		$this->load->view('inventory/view_fg_load_stock');
	
	}
	
	function loaddata_fg_load_stock(){
		$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']:' '; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'asc'; // get the direction	
		
		$tglawal_fg_stock_load = isset($_POST['tglawal_fg_stock_load'])?$_POST['tglawal_fg_stock_load']:'-';
		$tglakhir_fg_stock_load = isset($_POST['tglakhir_fg_stock_load'])?$_POST['tglakhir_fg_stock_load']:'-';
		
		$tgl = date('m/d/Y');
		 $date1=$tgl;
		 $date2=$tgl; 
		
		$where = " 1=1 ";
		$wherecount = " 1=1 ";
		if ($tglawal_fg_stock_load != '-' && $tglakhir_fg_stock_load != '-'){
		  $wheredate = " '$tglawal_fg_stock_load' and '$tglakhir_fg_stock_load' ";
		  $date1=$tglawal_fg_stock_load;
		  $date2=$tglakhir_fg_stock_load; 
		} else {
			 $wheredate = " '$tgl' and '$tgl' ";
		}
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "part_no" ) {
						$searchString = $k->data;
						$where .= " and part_no like  '%".$searchString."%'"; 
						$wherecount .= " and x.ASSY_CODE like  '%".$searchString."%'"; 
					} else if ( $k->field == "part_name" ) {
						$searchString = $k->data;
						$where .= " and part_name like  '%".$searchString."%'"; 
						$wherecount .= " and x.ASSY_NO like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		 
			
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_outgoing->data_fg_load_stock_all($limit,$start,$sidx,$sord, $where, $wheredate, $date1, $date2);
		$count = $this->mdl_outgoing->data_fg_load_stock_CountAll($where,$wheredate);
		if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
		if ($page > $total_pages) $page=$total_pages;
		
	//	var_dump($records); die();
		
		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records =  $count;
		$i=0;
		foreach($records as $r){
			
			//$responce->rows[$i]['row']= $r['row']; 
			
			$responce->rows[$i]['row']= $r['row']; 
			$responce->rows[$i]['part_no']=$r['part_no']; 
			$responce->rows[$i]['part_name']=$r['part_name'];
			$responce->rows[$i]['stock_awal']=$r['stock_awal']; 
			$responce->rows[$i]['qty_in']=$r['qty_in']; 
			$responce->rows[$i]['qty_out']=$r['qty_out']; 
			$responce->rows[$i]['sisa']=$r['sisa']; 
			$responce->rows[$i]['unit']=$r['unit']; 
			$responce->rows[$i]['qty_in_by_date']=$r['qty_in_by_date']; 
			$responce->rows[$i]['qty_out_by_date']=$r['qty_out_by_date'];  
			$responce->rows[$i]['sisa_by_date']=$r['sisa_by_date'];  
			
			$i++;
		}
		
		echo json_encode($responce);
	
	
	
	}
	
   
}