<?php
class CT_incomingitem 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');
		$username=$this->session->userdata('DX_username');
		if(!permissionAuth(2) && $uid != RID){ 
			header('HTTP/1.1 401 Unauthorize');die('noaccess');
		}
		
		$this->load->model("pasi_inventory/mdl_inventory", 'pasi_mdl_inventory');
		$this->load->model("pasi_settings/mdl_settings", 'pasi_mdl_settings');
		$this->load->model("pasi_upload/mdl_upload", 'pasi_mdl_upload');
		$this->db_pasi =$this->load->database('pasi', TRUE);

	}
	
	function index(){
	
		$this->load->view('pasi_inventory/view_incomingitem');
	
	}
	
	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']:'id_bpb_filter'; // get index row - i.e. user click to sort
		$sord 	= isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
			
		//$where = " 1=1 ";
		$where = " Status='UPLOAD' ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){

					if ( $k->field == "period" ) {
						$searchString = $k->data;
						$where .= " and period like  '%".$searchString."%'"; 	
					} else if ( $k->field == "section_no" ) {
						$searchString = $k->data;
						$where .= " and section_no like  '%".$searchString."%'";	
					} else if ( $k->field == "section_name" ) {
						$searchString = $k->data;
						$where .= " and section_name like  '%".$searchString."%'"; 
					} else if ( $k->field == "bpb_date" ) {
						$searchString = $k->data;
						$where .= " and bpb_date like  '%".$searchString."%'"; 
					} else if ( $k->field == "bpb_no" ) {
						$searchString = $k->data;
						$where .= " and bpb_no like  '%".$searchString."%'"; 	
					} else if ( $k->field == "ri_no" ) {
						$searchString = $k->data;
						$where .= " and ri_no like  '%".$searchString."%'"; 						
					} else if ( $k->field == "ri_date" ) {
						$searchString = $k->data;
						$where .= " and ri_date like  '%".$searchString."%'"; 
					} else if ( $k->field == "supp_do_no" ) {
						$searchString = $k->data;
						$where .= " and supp_do_no like  '%".$searchString."%'"; 
					} else if ( $k->field == "supp_do_date" ) {
						$searchString = $k->data;
						$where .= " and supp_do_date like  '%".$searchString."%'"; 
					} else if ( $k->field == "po_no" ) {
						$searchString = $k->data;
						$where .= " and po_no like  '%".$searchString."%'"; 
					} else if ( $k->field == "po_date" ) {
						$searchString = $k->data;
						$where .= " and po_date like  '%".$searchString."%'";	
					} else if ( $k->field == "item_no" ) {
						$searchString = $k->data;
						$where .= " and item_no like  '%".$searchString."%'"; 
					} else if ( $k->field == "item_name" ) {
						$searchString = $k->data;
						$where .= " and item_name like  '%".$searchString."%'"; 
					} else if ( $k->field == "type" ) {
						$searchString = $k->data;
						$where .= " and type like  '%".$searchString."%'"; 	
					} else if ( $k->field == "description" ) {
						$searchString = $k->data;
						$where .= " and description like  '%".$searchString."%'"; 						
					} else if ( $k->field == "bpb_qty" ) {
						$searchString = $k->data;
						$where .= " and bpb_qty like  '%".$searchString."%'"; 
					} else if ( $k->field == "unit" ) {
						$searchString = $k->data;
						$where .= " and unit like  '%".$searchString."%'"; 
					} else if ( $k->field == "po_currency" ) {
						$searchString = $k->data;
						$where .= " and po_currency like  '%".$searchString."%'"; 
					} else if ( $k->field == "po_amount" ) {
						$searchString = $k->data;
						$where .= " and po_amount like  '%".$searchString."%'"; 
					} else if ( $k->field == "po_amount_usd" ) {
						$searchString = $k->data;
						$where .= " and po_amount_usd like  '%".$searchString."%'"; 
					} else if ( $k->field == "part_no" ) {
						$searchString = $k->data;
						$where .= " and part_no like  '%".$searchString."%'"; 
					} else if ( $k->field == "status_bc" ) {
						$searchString = $k->data;
						$where .= " and status_bc like  '%".$searchString."%'"; 
					}else if ( $k->field == "supp_no" ) {
						$searchString = $k->data;
						$where .= " and supp_no like  '%".$searchString."%'"; 
					}
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->pasi_mdl_inventory->databpbrawmaterial_All($limit,$start,$sidx,$sord, $where);
		$count = $this->pasi_mdl_inventory->databpbrawmaterial_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_bpb_filter']=$r->id_bpb_filter; 
			$responce->rows[$i]['period']=$r->period; 
			$responce->rows[$i]['section_no']=$r->section_no;
			$responce->rows[$i]['section_name']=$r->section_name;
			$responce->rows[$i]['bpb_date']=$r->bpb_date;
			$responce->rows[$i]['bpb_no']=$r->bpb_no; 
			$responce->rows[$i]['ri_no']=$r->ri_no;
			$responce->rows[$i]['ri_date']=$r->ri_date;
			$responce->rows[$i]['supp_do_no']=$r->supp_do_no;
			$responce->rows[$i]['supp_do_date']=$r->supp_do_date; 
			$responce->rows[$i]['po_no']=$r->po_no; 
			$responce->rows[$i]['po_date']=$r->po_date;
			$responce->rows[$i]['kdbrg']=$r->kdbrg;
			$responce->rows[$i]['item_no']=$r->item_no;
			$responce->rows[$i]['item_name']=$r->item_name;
			$responce->rows[$i]['type']=$r->type; 
			$responce->rows[$i]['description']=$r->description;
			$responce->rows[$i]['bpb_qty']=$r->bpb_qty;
			$responce->rows[$i]['unit']=$r->unit;
			$responce->rows[$i]['po_currency']=$r->po_currency;
			$responce->rows[$i]['po_amount']=$r->po_amount;
			$responce->rows[$i]['po_amount_usd']=$r->po_amount_usd;
			$responce->rows[$i]['status_bc']=$r->status_bc;
			$responce->rows[$i]['supp_no']=$r->supp_no;
			$responce->rows[$i]['create_date']=$r->create_date;
			$responce->rows[$i]['create_by']=$r->create_by;
			$responce->rows[$i]['update_date']=$r->update_date;
			$responce->rows[$i]['update_by']=$r->update_by;
			$responce->rows[$i]['status']=$r->status;

			$i++;
		}
		echo json_encode($responce);		
	}
	
	function getdataitemlist(){
		$where = "1=1";
		$count = $this->pasi_mdlsettings->datarawmaterial_CountAll($where);

		if ($count=='0'){
				$responce = array("sts" => '06 ', "desc" => 'Data Raw Material Refference kosong, silahkan di upload terlebih dahulu');
				echo json_encode($responce);die();		
		}

		$file_name 		= (isset($_POST['file_name']) && !empty($_POST['file_name']))?$_POST['file_name']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$id_upload_bpb 	= (isset($_POST['id_upload_bpb']) && !empty($_POST['id_upload_bpb']))?$_POST['id_upload_bpb']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$file_path 		= (isset($_POST['file_path']) && !empty($_POST['file_path']))?$_POST['file_path']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$username = $this->session->userdata('DX_username');
		
		$apppath = $this->config->item('APP_upload_path');
		$path=$apppath.'masterdata/';
		
		//echo "exec('sp_incoming_raw_material @id_upload=$id_upload_bpb , @filedir=''$path'',@username=''$username'',@filename=''$file_name''')";die();		
		
		
		$qry = $this->db_pasi->query("
			
			BEGIN TRY
				exec('sp_incoming_raw_material @id_upload=$id_upload_bpb , @filedir=''$path'',@username=''$username'',@filename=''$file_name''')
			END TRY
			BEGIN CATCH
				SELECT 
					'06' AS STATUS
					,ERROR_MESSAGE() AS DESCRIPTION;
				END CATCH
			
			 ");
			 
		 $rs = $qry->result_array();
         // $r = $qry->result();		
		 //var_dump($rs);die();
		 
		 foreach ($rs as $row){
			if ($row['STATUS']=='00'){
				$responce = array("sts" => $row['STATUS'], "desc" => $row['DESCRIPTION'].' Updated/Inserted');
			}
			
			if ($row['STATUS']=='06'){
				$r = $this->db_pasi->query("select * from tbl_bpb_raw_material where kdbrg is null or status_bc is null");
				 
				$aa = $r->result_array();			
					$statusx = '';
					$d=1;
					foreach ($aa as $dd){
						$aaa=$dd['kdbrg'];
						$bbb=$dd['status_bc'];
						
						if ($aaa=='' || $bbb==''){
							$statusx .= chr(10).$d.'.'.$dd['item_name'] ;
							$s = $this->db_pasi->query("delete from tbl_bpb_raw_material where id_bpb_header='".$dd['id_bpb_header']."'");
							$t = $this->db_pasi->query("delete from tbl_master_bpb where id_master_bpb='".$dd['id_bpb_header']."'");
						}	
					$d++;
					}
					$responce = array("sts" => '06', "desc" => $statusx);
			}
		 }  
		 echo json_encode($responce);
	}

	function view_incomingitem_itemlist(){
	
		$this->load->view('pasi_inventory/view_incomingitem_itemlist');
	
	}
	function data_incoming_itemlist(){
		$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_upload_bpb'; // 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 == "file_name" ) {
						$searchString = $k->data;
						$where .= " and file_name like  '%".$searchString."%'"; 
					} else if ( $k->field == "status" ) {
						$searchString = $k->data;
						$where .= " and status  like  '%".$searchString."%'";
					} else if ( $k->field == "upload_by" ) {
						$searchString = $k->data;
						$where .= " and upload_by like  '%".$searchString."%'"; 
					}  else if ( $k->field == "upload_date" ) {
						$searchString = $k->data;
						$where .= " and upload_date =  '$searchString'";
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->pasi_mdl_upload->uploadbpb_all($limit,$start,$sidx,$sord, $where);
        $count = $this->pasi_mdl_upload->uploadbpb_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_upload_bpb']=$r->id_upload_bpb; 
			$responce->rows[$i]['file_name']=$r->file_name; 
			$responce->rows[$i]['file_path']=$r->file_path; 
			$responce->rows[$i]['status']=$r->status;
			$responce->rows[$i]['upload_by']=$r->upload_by;
			$responce->rows[$i]['upload_date']=$r->upload_date;
			
			$i++;
		}
		echo json_encode($responce);	
	}
	
	function prosesdatabl(){
		$bc_status = (isset($_POST['bc_status']) && !empty($_POST['bc_status']))?$_POST['bc_status']:die('{"sts":"ERROR","desc":" Status BC Tidak Ditemukan"}');  
		$username = $this->session->userdata('DX_username');
		
		$datadiff = (isset($_POST['datadiff']) && !empty($_POST['datadiff']))?$_POST['datadiff']:'0'; 
		$datadiff=json_decode($datadiff);
		$statusx = '';
		$datacsv = '';
		foreach ($datadiff as $data ){
			
			$idbpb = (isset($data->id_bpb_filter) && !empty($data->id_bpb_filter))?$data->id_bpb_filter :die('{"sts":"ERROR","desc":" Param period Tidak Ditemukan"}');
			$period = (isset($data->period2) && !empty($data->period2))?$data->period2 :die('{"sts":"ERROR","desc":" Param period Tidak Ditemukan"}');
			$section_no = (isset($data->section_no2) && !empty($data->section_no2))?$data->section_no2 :die('{"sts":"ERROR","desc":" Param section_no Tidak Ditemukan"}');
			$section_name = (isset($data->section_name2) && !empty($data->section_name2))?$data->section_name2 :die('{"sts":"ERROR","desc":" Param section_name Tidak Ditemukan"}');
			$bpb_date = (isset($data->bpb_date2) && !empty($data->bpb_date2))?$data->bpb_date2 :die('{"sts":"ERROR","desc":" Param bpb_date Tidak Ditemukan"}');
			$bpb_no = (isset($data->bpb_no2) && !empty($data->bpb_no2))?$data->bpb_no2 :die('{"sts":"ERROR","desc":" Param bpb_no Tidak Ditemukan"}');
			$ri_no = (isset($data->ri_no2) && !empty($data->ri_no2))?$data->ri_no2 :die('{"sts":"ERROR","desc":" Param ri_no Tidak Ditemukan"}');
			$ri_date = (isset($data->ri_date2) && !empty($data->ri_date2))?$data->ri_date2 :die('{"sts":"ERROR","desc":" Param ri_date Tidak Ditemukan"}');
			$supp_do_no = (isset($data->supp_do_no2) && !empty($data->supp_do_no2))?$data->supp_do_no2 :'-'; //die('{"sts":"ERROR","desc":" Param supp_do_no Tidak Ditemukan"}');
			$supp_do_date = (isset($data->supp_do_date2) && !empty($data->supp_do_date2))?$data->supp_do_date2 :'-'; //die('{"sts":"ERROR","desc":" Param supp_do_date Tidak Ditemukan"}');
			$po_no = (isset($data->po_no2) && !empty($data->po_no2))?$data->po_no2 :die('{"sts":"ERROR","desc":" Param po_no Tidak Ditemukan"}');
			$po_date = (isset($data->po_date2) && !empty($data->po_date2))?$data->po_date2 :die('{"sts":"ERROR","desc":" Param po_date Tidak Ditemukan"}');
			$kdbrg = (isset($data->kdbrg2) && !empty($data->kdbrg2))?$data->kdbrg2 :die('{"sts":"ERROR","desc":" Param kdbrg Tidak Ditemukan"}');
			$item_no = (isset($data->item_no2) && !empty($data->item_no2))?$data->item_no2 :die('{"sts":"ERROR","desc":" Param item_no Tidak Ditemukan"}');
			$item_name = (isset($data->item_name2) && !empty($data->item_name2))?$data->item_name2 :die('{"sts":"ERROR","desc":" Param item_name Tidak Ditemukan"}');
			$description = (isset($data->description2) && !empty($data->description2))?$data->description2 :die('{"sts":"ERROR","desc":" Param description Tidak Ditemukan"}');
			$bpb_qty = (isset($data->bpb_qty2) && !empty($data->bpb_qty2))?$data->bpb_qty2 :die('{"sts":"ERROR","desc":" Param bpb_qty Tidak Ditemukan"}');
			$unit = (isset($data->unit2) && !empty($data->unit2))?$data->unit2 :die('{"sts":"ERROR","desc":" Param unit Tidak Ditemukan"}');
			$po_currency = (isset($data->po_currency2) && !empty($data->po_currency2))?$data->po_currency2 :die('{"sts":"ERROR","desc":" Param po_currency Tidak Ditemukan"}');
			$po_amount = (isset($data->po_amount2) && !empty($data->po_amount2))?$data->po_amount2 :die('{"sts":"ERROR","desc":" Param po_amount Tidak Ditemukan"}');
			$po_amount_usd = (isset($data->po_amount_usd2) && !empty($data->po_amount_usd2))?$data->po_amount_usd2 :die('{"sts":"ERROR","desc":" Param po_amount_usd Tidak Ditemukan"}');
			$status_bc = (isset($data->status_bc2) && !empty($data->status_bc2))?$data->status_bc2 :die('{"sts":"ERROR","desc":" Param status Tidak Ditemukan"}');
			$supplier  = (isset($data->supp_no2) && !empty($data->supp_no2))?$data->supp_no2 :die('{"sts":"ERROR","desc":" Param status Tidak Ditemukan"}');
			
			if ($status_bc!=$bc_status){
				$statusx .= chr(10).$bpb_no.'='.$status_bc ;
			}
	
			$datacsv .= $idbpb . ','.$period . ',' .$section_no.',' .$section_name.',' .$bpb_date.',' .$bpb_no.',' .$ri_no.',' .$ri_date.',' .$supp_do_no.',' .$supp_do_date.',' .$po_no.',' .$po_date.',' .$kdbrg.',' .$item_no.',' .$item_name.',' .$description.',' .$bpb_qty.',' .$unit.',' .$po_currency.',' .$po_amount.',' .$po_amount_usd.',' .$status_bc.',' .$supplier;
			$datacsv .= "|";
	
		}
			$datacsv = substr($datacsv,0,-1);

			if ($statusx!='') {
				
				$responce = array("sts" => '00', "desc" => 'Please check again (BC Status) '.$statusx);
		
				echo json_encode($responce);die();
			}
			//echo $datacsv;die();
			
/*   		echo "EXEC('sp_insert_bc_from_bpb_raw_material @username=''$username'', @datadiff=''$datacsv'', @cmd=''proses'', @bc=''$bc_status''  ')";
		die(); */  
		
		$qry = $this->db_pasi->query("
		
			BEGIN TRY
				exec('sp_insert_bc_from_bpb_raw_material @username=''$username'', @datadiff=''$datacsv'', @cmd=''proses'', @bc=''$bc_status''  ') 
			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);
		
	
	}
 }