<?php
class CT_scrap 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_production/mdl_production", 'pasi_mdl_production');
		$this->load->model("pasi_upload/mdl_upload", 'pasi_mdl_upload');
		$this->db_pasi =$this->load->database('pasi', TRUE);

	}
	
	function index(){
		$page = '1'; // get the requested page
		$limit = '100'; // get how many rows we want to have into the grid
		$sidx = 'id_jenis_scrap'; // get index row - i.e. user click to sort
		$sord = '0'; // get the direction
		$where = "1=1";
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->pasi_mdl_production->jenisscrap_all($limit,$start,$sidx,$sord, $where);
       	$jenisscrap = $records;
		//echo $jenisscrap;die();
		$data['jenisscrap'] = $jenisscrap;
		
		$this->load->view('pasi_production/view_scrap',$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']:'id_real_scrap_hdr'; // 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 == "period" ) {
						$searchString = $k->data;
						$where .= " and period like  '%".$searchString."%'"; 	
					} else if ( $k->field == "section_no" ) {
						$searchString = $k->data;
						$where .= " and section_no like  '%".$searchString."%'";	
					}
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		
		$records = $this->pasi_mdl_production->datascrap_All($limit,$start,$sidx,$sord, $where);
		$count = $this->pasi_mdl_production->datascrap_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_real_scrap_hdr']=$r->id_real_scrap_hdr; 
			$responce->rows[$i]['date_start']=$r->date_start; 
			$responce->rows[$i]['date_end']=$r->date_end;
			$responce->rows[$i]['total_pvc']=$r->total_pvc;
			$responce->rows[$i]['total_copper']=$r->total_copper;
			$responce->rows[$i]['total_wire']=$r->total_wire; 
			$responce->rows[$i]['total_scrapkemasan']=$r->total_scrapkemasan; 
			$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 Insert_Data_Scrap(){

		$tglawal = (isset($_POST['tglawal']) && !empty($_POST['tglawal']))?$_POST['tglawal']:die('{"sts":"ERROR","desc":" Param Tgl Awal Tidak Ditemukan"}');
		$tglakhir = (isset($_POST['tglakhir']) && !empty($_POST['tglakhir']))?$_POST['tglakhir']:die('{"sts":"ERROR","desc":" Param Tgl Akhir Tidak Ditemukan"}');	
		$jnsscrap = (isset($_POST['jnsscrap']) && !empty($_POST['jnsscrap']))?$_POST['jnsscrap']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$jmlscrap = (isset($_POST['jmlscrap']) && !empty($_POST['jmlscrap']))?$_POST['jmlscrap']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');

		$username = $this->session->userdata('DX_username');
		$jmlscrap = (float) $jmlscrap;
		
		//echo $tglawal.' '.$tglakhir.' '.$jnsscrap.' '.$jmlscrap;die();
		
		$qry = $this->db_pasi->query("
			
			BEGIN TRY
				exec('sp_insert_data_scrap @jnsscrap=$jnsscrap , @jmlscrap=''$jmlscrap'' , @tglawal=''$tglawal'', @tglakhir=''$tglakhir'',@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($rs);die();
		 
		 foreach ($rs as $row){
			 $responce = array("sts" => $row['STATUS'], "desc" => $row['DESCRIPTION']);
		 }  
		 echo json_encode($responce);			

	
	}
	function getdataparths(){
		$qry = $this->db_pasi->query("
			
			  exec('sp_part_hs')
			 
			
			 ");
		 $rs = $qry->result_array();
         //// $r = $qry->result();		
		//// var_dump($r);die();
		 foreach ($rs as $row){
			 $responce = array("sts" => $row['status'], "desc" => $row['description'].' File/s Loaded');
		 }  
		 echo json_encode($responce);
		 
		 // $stmt = " exec('sp_part_hs') ";
		 // $qry = mssql_query($stmt); 
		 // $row = mssql_fetch_array($qry); 
		 // var_dump($row);die();
	
	
	}

	function data_buktipenerimaanbrg_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":" Param  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 ){
			
			$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"}');
			$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"}');
			$type = (isset($data->type2) && !empty($data->type2))?$data->type2 :die('{"sts":"ERROR","desc":" Param type 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"}');
			$part_no = (isset($data->part_no2) && !empty($data->part_no2))?$data->part_no2 :die('{"sts":"ERROR","desc":" Param part_no Tidak Ditemukan"}');
			$status = (isset($data->status2) && !empty($data->status2))?$data->status2 :die('{"sts":"ERROR","desc":" Param status Tidak Ditemukan"}');
			
	
			if ($status!=$bc_status){
				$statusx .= chr(10).$bpb_no.'='.$status ;
			}
	
			$datacsv .= $period . ',' .$section_no.',' .$section_name.',' .$bpb_date.',' .$bpb_no.',' .$ri_no.',' .$ri_date.',' .$supp_do_no.',' .$supp_do_date.',' .$po_no.',' .$po_date.',' .$item_no.',' .$item_name.',' .$type.',' .$description.',' .$bpb_qty.',' .$unit.',' .$po_currency.',' .$po_amount.',' .$po_amount_usd.',' .$part_no.',' .$part_no; 
			$datacsv .= "|";
	
		}
			$datacsv = substr($datacsv,0,-1);

			if ($statusx!='') {
				
				$responce = array("sts" => '06', "desc" => 'Cek kembali status BC nya'.$statusx);
		
				echo json_encode($responce);die();
			}
			
/* 		echo "EXEC('sp_insertbc_frombpb @username=''$username'', @datadiff=''$datacsv'', @cmd=''proses'', @bc=''$bc_status''  ')";
		die();
		 */
		$qry = $this->db_pasi->query("
		
			BEGIN TRY
			
				EXEC ('sp_insertbc_frombpb @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);
		
	
	}
 
 }