<?php
class CT_wip 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("production/mdl_production", 'mdl_production');
		$this->load->model("inventory/mdl_outgoing", 'mdl_outgoing');
		
  }

  function index(){
 	$this->load->view('production/view_wip');
  }


  function data_wip(){
	$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_dtl'; // get index row - i.e. user click to sort
	$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		
	$where = " 1=1  and type_items='RM' ";
	$paramwip = (isset($_POST['paramwip']) or !empty($_POST['paramwip']))?$_POST['paramwip']:'-';
	$tglawalwip = (isset($_POST['tglawalwip']) or !empty($_POST['tglawalwip']))?$_POST['tglawalwip']:date('Y-m-d');
	$tglakhirwip = (isset($_POST['tglakhirwip']) or !empty($_POST['tglakhirwip']))?$_POST['tglakhirwip']:date('Y-m-d');
	
	if ($paramwip != '-'){
		$where .= " and tbl_data_out_dtl.out_date between '$tglawalwip' and '$tglakhirwip' ";	
	}	
		
	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;
		$responce->rows[$i]['doc_out_no']=$r->doc_out_no;
		
		
		
		$i++;
	}
	echo json_encode($responce);
		
  }
}

