<?php
class CT_scrap extends CI_Controller{
	function __construct()
	{
	  parent::__construct();
	  if (!$this->dx_auth->is_logged_in()) {
			redirect('');  
			die(); 
		}
		
		$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');
		$this->load->model("inventory/mdl_inventory", 'mdl_inventory');
		
		$this->load->helper('afunction');

	}

	function index(){

		$this->load->view('inventory/view_scrap');
	}
	
	function loaddatascrap(){
		$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'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$typebahan = (isset($_POST['typebahanx']) && !empty($_POST['typebahanx']))?$_POST['typebahanx']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
			
		$where = " 1=1 and typebahan='$typebahan' ";
		
		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 part_name like  '%".$searchString."%'"; 
					} else if ( $k->field == "date_stock" ) {
						$searchString = $k->data;
						$where .= " and date_stock = '$searchString'"; 
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_inventory->scrap_All($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_inventory->scrap_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_scrap']=$r->id_scrap; 
			$responce->rows[$i]['date_scrap']=$r->date_scrap; 
			$responce->rows[$i]['part_no']=$r->part_no;
			$responce->rows[$i]['part_name_customs']=$r->part_name_customs;
			$responce->rows[$i]['part_name']=$r->part_name;
			$responce->rows[$i]['qty']=$r->qty;
			$responce->rows[$i]['unit']=$r->unit;
			$responce->rows[$i]['unit_konversi']=$r->unit_konversi;
			$responce->rows[$i]['pengali']=$r->pengali;
			$responce->rows[$i]['netto']=$r->netto;
			$responce->rows[$i]['brutto']=$r->brutto;
			$responce->rows[$i]['typebahan']=$r->typebahan;
			$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 loaddatascrap_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']:'id'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$typebahan = (isset($_POST['typebahanx']) && !empty($_POST['typebahanx']))?$_POST['typebahanx']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$where = " 1=1 and typebahan='$typebahan' and status='new' "; 
		
		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 part_name like  '%".$searchString."%'"; 
					} else if ( $k->field == "date_stock" ) {
						$searchString = $k->data;
						$where .= " and date_stock = '$searchString'"; 
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_inventory->scrap_All($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_inventory->scrap_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_scrap']=$r->id_scrap; 
			$responce->rows[$i]['date_scrap']=$r->date_scrap; 
			$responce->rows[$i]['part_no']=$r->part_no;
			$responce->rows[$i]['part_name_customs']=$r->part_name_customs;
			$responce->rows[$i]['part_name']=$r->part_name;
			$responce->rows[$i]['qty']=$r->qty;
			$responce->rows[$i]['unit']=$r->unit;
			$responce->rows[$i]['unit_konversi']=$r->unit_konversi;
			$responce->rows[$i]['pengali']=$r->pengali;
			$responce->rows[$i]['netto']=$r->netto;
			$responce->rows[$i]['brutto']=$r->brutto;
			$responce->rows[$i]['typebahan']=$r->typebahan;
			$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 loaddata_scrap_out_list(){
		$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'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		$xax = (isset($_POST['xax']) && !empty($_POST['xax']))?$_POST['xax']:die('');
		
		if ($xax == 'song'){
			$where = " 1=1 and part_no='xxxxxxxxx'";
		
		} else {
			
				$where = " 1=1 ";}
		
		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 part_name like  '%".$searchString."%'"; 
					} else if ( $k->field == "date_stock" ) {
						$searchString = $k->data;
						$where .= " and date_stock = '$searchString'"; 
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_inventory->scrap_out_All($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_inventory->scrap_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;
		$i=0;
		foreach($records as $r){
			
			$responce->rows[$i]['id_scrap_out']=$r->id_scrap_out; 
			$responce->rows[$i]['date_scrap']=$r->date_scrap; 
			$responce->rows[$i]['part_no']=$r->part_no;
			$responce->rows[$i]['part_name_customs']=$r->part_name_customs;
			$responce->rows[$i]['part_name']=$r->part_name;
			$responce->rows[$i]['qty']=$r->qty;
			$responce->rows[$i]['unit']=$r->unit;
			$responce->rows[$i]['unit_konversi']=$r->unit_konversi;
			$responce->rows[$i]['pengali']=$r->pengali;
			$responce->rows[$i]['netto']=$r->netto;
			$responce->rows[$i]['brutto']=$r->brutto;
			$responce->rows[$i]['typebahan']=$r->typebahan;
			$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]['no_dok_out']=$r->no_dok_out;
			$responce->rows[$i]['id_scrap']=$r->id_scrap;
			
			
			$i++;
		}
		echo json_encode($responce);
	
	
	}
	
	function loaddata_scrap_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']:'id'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		$where = " 1=1 ";
		
		if (isset($_POST['idxscx'])){
			$idscr = $_POST['idxscx'];
			$where = " id_scrap = $idscr   ";
		}
		
		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 part_name like  '%".$searchString."%'"; 
					} else if ( $k->field == "date_stock" ) {
						$searchString = $k->data;
						$where .= " and date_stock = '$searchString'"; 
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_inventory->scrap_out_All($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_inventory->scrap_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;
		$i=0;
		foreach($records as $r){
			
			$responce->rows[$i]['id_scrap_out']=$r->id_scrap_out; 
			$responce->rows[$i]['date_scrap']=$r->date_scrap; 
			$responce->rows[$i]['part_no']=$r->part_no;
			$responce->rows[$i]['part_name_customs']=$r->part_name_customs;
			$responce->rows[$i]['part_name']=$r->part_name;
			$responce->rows[$i]['qty']=$r->qty;
			$responce->rows[$i]['unit']=$r->unit;
			$responce->rows[$i]['unit_konversi']=$r->unit_konversi;
			$responce->rows[$i]['pengali']=$r->pengali;
			$responce->rows[$i]['netto']=$r->netto;
			$responce->rows[$i]['brutto']=$r->brutto;
			$responce->rows[$i]['typebahan']=$r->typebahan;
			$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]['no_dok_out']=$r->no_dok_out;
			$responce->rows[$i]['id_scrap']=$r->id_scrap;
			$responce->rows[$i]['tg_dok_out']=$r->tg_dok_out;
			$responce->rows[$i]['no_scrap']=$r->no_scrap;
			
			$i++;
		}
		echo json_encode($responce);
	
	
	}
	
	function prosesgroupscrap_out(){
		
		$no_scrap_out = (isset($_POST['no_scrap_out']) && !empty($_POST['no_scrap_out']))?$_POST['no_scrap_out']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$tgl_scrap_out = (isset($_POST['tgl_scrap_out']) && !empty($_POST['tgl_scrap_out']))?$_POST['tgl_scrap_out']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$uid = $this->session->userdata('DX_username');
		
		$datadiff = (isset($_POST['datadiff']) && !empty($_POST['datadiff']))?$_POST['datadiff']:die('{"sts":"ERROR","desc":" Data  Tidak Ditemukan"}'); 
		$datadiff=json_decode($datadiff);
		$datacsv = '';
		
		foreach ($datadiff as $data ){
		    $id_scrap_out = (isset($data->id_scrap_out) && !empty($data->id_scrap_out ))?$data->id_scrap_out :die('{"sts":"ERROR","desc":" Param id scrap Tidak Ditemukan"}');
			$part_no = (isset($data->part_no) && !empty($data->part_no ))?$data->part_no :die('{"sts":"ERROR","desc":" Param part no Tidak Ditemukan"}');
			$qty = (isset($data->qty) && !empty($data->qty ))?$data->qty :die('{"sts":"ERROR","desc":" Param qty Tidak Ditemukan"}');
			$unit = (isset($data->unit) && !empty($data->unit ))?$data->unit :die('{"sts":"ERROR","desc":" Param unit Tidak Ditemukan"}');
			$part_name = (isset($data->part_name) && !empty($data->part_name ))?$data->part_name :die('{"sts":"ERROR","desc":" Param unit Tidak Ditemukan"}');
			$datacsv .= $id_scrap_out . ',' .$part_no . ',' .$qty . ',' . $unit . ',' . $part_name; 
			$datacsv .= "|";
		
		}
		
		//echo $datacsv; die();
		
		$datacsv=substr($datacsv, 0, strlen($datacsv) - 1); 

		
		$qry = $this->db->query("
		
			 EXEC ('spi_scrap_out @datadiff=''$datacsv'', @cmd=''proses'', @no_scrap_out=''$no_scrap_out'', @tgl_scrap_out=''$tgl_scrap_out'',@username=''$uid ''    ') 
			
		 ");
		 $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);
			
	
	
	}
	
	function prosesgroupscrap_out_cancel(){
		$no_scrap_out = (isset($_POST['id_scrap']) && !empty($_POST['id_scrap']))?$_POST['id_scrap']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$tgl_out = (isset($_POST['tgl_out']) && !empty($_POST['tgl_out']))?$_POST['tgl_out']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$uid = $this->session->userdata('DX_username');
		
		
		$qry = $this->db->query("
		
			 EXEC ('spi_scrap_out @datadiff='''', @cmd=''cancel'', @no_scrap_out=''$no_scrap_out'', @tgl_scrap_out=''$tgl_out'',@username=''$uid ''    ') 
			
		 ");
		 $rs = $qry->result_array();
      
		foreach ($rs as $row){
			$responce = array("sts" => $row['status'], "desc" => $row['description']);
		}  
		echo json_encode($responce);	
	
	}
	
	
	//-- scrap proses
	function view_scrap_bahanbaku(){
	
		$this->load->view('inventory/view_scrap_bahanbaku');
	
	}
	
	function data_uploadscrap_bahanbaku(){
		$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_scrap_upload'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$where = " 1=1 and tipe='SCRAPBB' ";
		
		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],103)  =  '$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_inventory->upload_scrapAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_inventory->upload_scrapcountAll($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_scrap_upload']=$r->id_scrap_upload; 
			$responce->rows[$i]['date_upload']=$r->date_upload; 
			$responce->rows[$i]['filename']= $r->filename; 
			$responce->rows[$i]['filename2']=str_replace(".xls","", $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;
			$responce->rows[$i]['update_date']=$r->update_date;
			$responce->rows[$i]['update_by']=$r->update_by;
		
			
			$i++;
		}
		echo json_encode($responce);	
	
	}
	 
	function upload_file_scrapbahanbaku(){
	   $status = "";
	   $msg = "";
	   $file_element_name = 'userfile_scrap_bahanbaku';
	   //$username = $this->session->userdata('DX_user_id');
	   $username = $this->session->userdata('DX_username');
	   $tipe = 'SCRAPBB';
	   if ($status != "error")
	   {
		  $apppath = $this->config->item('APP_upload_path');
		  $config['upload_path'] = $apppath.'inventory/scrap/bahan_baku/';
		  $config['allowed_types'] = 'xls';
		  $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/scrap/bahan_baku/log/'.$data['file_name'];
				$filename=$data['file_name'];
				//echo $myFile; die();
				/* echo " exec('[spuload_stockawal_fg] @filepath=''$myFile'', @filename=''$filename'', @username=''$username'', @tipe=''$tipe'', @trx=''add'', @id=0 ')";
				die(); */
				 
				$qry = $this->db->query("
					BEGIN TRY
					
					 exec('[spuload_scrap] @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_stockawal_fg]') ";
				 // $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_scrap_bahanbaku(){
		$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_scrap_upload = (isset($_POST['id_scrap_upload']) && !empty($_POST['id_scrap_upload']))?$_POST['id_scrap_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/scrap/bahan_baku/'.$filename;
		$pathnew=$apppath.'inventory/scrap/bahan_baku/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_scrap] @filepath=''$path'', @filename=''$filename'', @username=''$username'', @tipe=''$tipe'', @trx=''delete'', @id=$id_scrap_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_assylist]') ";
		 // $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 cek_data_scrap_bahanbaku(){
		error_reporting(E_ALL ^ E_NOTICE);
		//$this->load->file("application/libraries/excel_reader2.php", true);
		
		//require_once 'excel_reader2.php';
		//$this->load->library('Spreadsheet_Excel_Reader');
		include_once ( APPPATH."libraries/excel_reader2.php");
		
		$filename = (isset($_POST['filename']) && !empty($_POST['filename']))?$_POST['filename']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$apppath = $this->config->item('APP_upload_path');
		$path=$apppath.'inventory/scrap/bahan_baku/'.$filename;
		$data = new Spreadsheet_Excel_Reader($path, false);
		
		echo $data->dump(true,true);
			
	
	}
	
	function proses_data_scrap_bahanbaku(){
	
		$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_scrap_upload = (isset($_POST['id_scrap_upload']) && !empty($_POST['id_scrap_upload']))?$_POST['id_scrap_upload']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$username = $this->session->userdata('DX_username');
		$typebahan = 'SCRAPBB';
		/* echo  " exec('spi_scrap @filename=''$filename'', @id_scrap_upload=$id_scrap_upload, @tipe=''$tipe'',  @username=''$username'', @typebahan=''$typebahan''  ') ";
		die(); */
		
		$qry = $this->db->query("
			BEGIN TRY
			  exec('spi_scrap @filename=''$filename'', @id_scrap_upload=$id_scrap_upload, @tipe=''$tipe'',  @username=''$username'', @typebahan=''$typebahan''  ')
			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);
	}
	//-- bahanbaku end
	
	function view_scrap_proses_out(){
	
		$this->load->view('inventory/view_scrap_proses_out');
	
	}
	
	function view_scrap_proses_out1(){
		$id_scrap = (isset($_POST['id_scrap']) && !empty($_POST['id_scrap']))?$_POST['id_scrap']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$q = $this->db->query("
			select [id_scrap]
			  ,convert(varchar(10), [date_scrap], 126) as date_scrap
			  ,[no_scrap]
			  ,[part_no]
			  ,[part_name_customs]
			  ,[part_name]
			  ,[qty]
			  ,[sisa]
			  ,[unit]
			  ,[unit_konversi]
			  ,[pengali]
			  ,[netto]
			  ,[brutto]
			  ,[typebahan]
			  ,[id_scrap_out]
			 from [tbl_scrap] where  id_scrap =  $id_scrap
		
		");
		
		$date_scrap = '';
		$no_scrap = '';
		$part_no = '';
		$qty = '';
		$part_name_customs = '';
		foreach ($q->result() as $r){
			$date_scrap = $r->date_scrap;
			$no_scrap = $r->no_scrap;
			$part_no = $r->part_no;
			$qty = $r->qty;
			$part_name_customs = $r->part_name_customs;
			$part_name = $r->part_name;
		}
		$data['date_scrap'] = $date_scrap;
		$data['no_scrap'] = $no_scrap;
		$data['part_no'] = $part_no;
		$data['qty'] = $qty;
		$data['part_name_customs'] = $part_name_customs;
		$data['part_name'] = $part_name;
		$data['id_scrap'] = $id_scrap;
		$this->load->view('inventory/view_scrap_proses_out1', $data);
	
	}
	//sta scrap
	
	function view_scrap_saldoawal(){
	
		$this->load->view('inventory/view_scrap_saldoawal');
	
	}
	function data_uploadscrap_bahanbaku_sta(){
		$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_scrap_upload'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$where = " 1=1 and tipe='SCRAPBBSTA' ";
		
		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],103)  =  '$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_inventory->upload_scrapAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_inventory->upload_scrapcountAll($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_scrap_upload']=$r->id_scrap_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;
			$responce->rows[$i]['update_date']=$r->update_date;
			$responce->rows[$i]['update_by']=$r->update_by;
		
			
			$i++;
		}
		echo json_encode($responce);	
	
	}
	 
	function upload_file_scrapbahanbaku_sta(){
	   $status = "";
	   $msg = "";
	   $file_element_name = 'userfile_scrap_bahanbaku_sta';
	   //$username = $this->session->userdata('DX_user_id');
	   $username = $this->session->userdata('DX_username');
	   $tipe = 'SCRAPBBSTA';
	   if ($status != "error")
	   {
		  $apppath = $this->config->item('APP_upload_path');
		  $config['upload_path'] = $apppath.'inventory/scrap/bahan_baku/';
		  $config['allowed_types'] = 'xls';
		  $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/scrap/bahan_baku/log/'.$data['file_name'];
				$filename=$data['file_name'];
				//echo $myFile; die();
				/* echo " exec('[spuload_stockawal_fg] @filepath=''$myFile'', @filename=''$filename'', @username=''$username'', @tipe=''$tipe'', @trx=''add'', @id=0 ')";
				die(); */
				 
				$qry = $this->db->query("
					BEGIN TRY
					
					 exec('[spuload_scrap] @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_stockawal_fg]') ";
				 // $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_scrap_bahanbaku_sta(){
		$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_scrap_upload = (isset($_POST['id_scrap_upload']) && !empty($_POST['id_scrap_upload']))?$_POST['id_scrap_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/scrap/bahan_baku/'.$filename;
		$pathnew=$apppath.'inventory/scrap/bahan_baku/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_scrap] @filepath=''$path'', @filename=''$filename'', @username=''$username'', @tipe=''$tipe'', @trx=''delete'', @id=$id_scrap_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_assylist]') ";
		 // $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 cek_data_scrap_bahanbaku_sta(){
		error_reporting(E_ALL ^ E_NOTICE);
		//$this->load->file("application/libraries/excel_reader2.php", true);
		
		//require_once 'excel_reader2.php';
		//$this->load->library('Spreadsheet_Excel_Reader');
		include_once ( APPPATH."libraries/excel_reader2.php");
		
		$filename = (isset($_POST['filename']) && !empty($_POST['filename']))?$_POST['filename']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$apppath = $this->config->item('APP_upload_path');
		$path=$apppath.'inventory/scrap/bahan_baku/'.$filename;
		$data = new Spreadsheet_Excel_Reader($path, false);
		
		echo $data->dump(true,true);
			
	
	}
	
	function proses_data_scrap_bahanbaku_sta(){
	
		$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_scrap_upload = (isset($_POST['id_scrap_upload']) && !empty($_POST['id_scrap_upload']))?$_POST['id_scrap_upload']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$username = $this->session->userdata('DX_username');
		$typebahan = 'SCRAPBBSTA';
		/* echo "exec('spi_stock_awal @filename=''$filename'', @id_stockawal_upload=$id_stockawal_upload, @tipe=''$tipe'',  @username=''$username''  ')";
		die(); */
		
		$qry = $this->db->query("
			BEGIN TRY
			  exec('spi_scrap @filename=''$filename'', @id_scrap_upload=$id_scrap_upload, @tipe=''$tipe'',  @username=''$username'', @typebahan=''$typebahan''  ')
			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);
	}
	
	
	//end scrap sta
	
	
	
	function update_masterscrap(){
		$part_no_scr = (isset($_POST['part_no_scr']) && !empty($_POST['part_no_scr']))?$_POST['part_no_scr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$part_name_scr = (isset($_POST['part_name_scr']) && !empty($_POST['part_name_scr']))?$_POST['part_name_scr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$part_no_rep = str_replace(' ', '', $part_no_scr);
		
		$in_inv = $this->db->query("
			
			if (select count(*) from tbl_scrap_data where part_no2 = '$part_no_rep') > 0 
			begin			
				BEGIN TRY
					update [tbl_scrap_data] set part_no = '$part_no_rep' , part_name='$part_name_scr' , part_name_customs='$part_name_scr'  where part_no2 = '$part_no_rep'
				
				END TRY
				BEGIN CATCH 
					SELECT 
					'06' AS status
					,ERROR_MESSAGE() AS description;
					
				END CATCH;
				select '00' as status, ' SUKSES UPDATED' as description
			end
			else
			begin
				BEGIN TRY
					insert into tbl_scrap_data ([part_no], [part_name], [part_name_customs], [part_no2] ) values ('$part_no_scr', '$part_name_scr', '$part_name_scr', '$part_no_rep' )
				
				END TRY
				BEGIN CATCH 
					SELECT 
					'06' AS status
					,ERROR_MESSAGE() AS description;
					
				END CATCH;
				select '00' as status, ' SUKSES Insert' as description
			end
		");
		$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);
  
  
  }
	
	
	 //scrap ceisa
	function view_scrap_bc25(){
	
		$this->load->view('inventory/view_scrap_bc25');
	
	}
	
	
	function loaddata_bc25_gettassy_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_sa_hdr'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		$param_not = (isset($_POST['param_not']) && !empty($_POST['param_not']))?$_POST['param_not']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$where = " 1=1 and satuan like '%$param_not%' ";
				
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "create_date" ) {
						$searchString = $k->data;
						$where .= "  and CONVERT(VARCHAR(10),[create_date],101)  =  '$searchString'"; 
					}  else if  ($k->field == "SA_NO" ) {
						$searchString = $k->data;
						$where .= " and [SA_NO] like  '%".$searchString."%'"; 
					} else if  ($k->field == "BC25No" ) {
						$searchString = $k->data;
						$where .= " and [BC25No] like  '%".$searchString."%'"; 
					} else if  ($k->field == "BC25Tg" ) {
						$searchString = $k->data;
						$where .= " and convert(varchar(10), [BC25Tg], 121) =  '".$searchString."'"; 
					}
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_inventory->scrap_bc25_All($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_inventory->scrap_bc25_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_sa']=$r->id_sa; 
			$responce->rows[$i]['id_sa_hdr']=$r->id_sa_hdr; 
			$responce->rows[$i]['SA_NO']=$r->SA_NO; 
			$responce->rows[$i]['BC25No']=$r->BC25No; 
			$responce->rows[$i]['BC25Tg']=$r->BC25Tg; 
			$responce->rows[$i]['BC25NAJU']=$r->BC25NAJU; 
			$responce->rows[$i]['NPWP_PEMBELI']=$r->NPWP_PEMBELI; 
			$responce->rows[$i]['NAMA_PEMBELI']=$r->NAMA_PEMBELI; 
			$responce->rows[$i]['INV_NO']=$r->INV_NO; 
			$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]['JUMLAH_COLI']=$r->JUMLAH_COLI; 
			$responce->rows[$i]['GROSSWEIGHT']=$r->GROSSWEIGHT; 
			$responce->rows[$i]['SATUANGROSS']=$r->SATUANGROSS; 
			$responce->rows[$i]['NETWEIGHT']=$r->NETWEIGHT; 
			$responce->rows[$i]['SATUANNET']=$r->SATUANNET; 
			$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; 
		
			
			$i++;
		}
		echo json_encode($responce);
	
	
	}
  
	function update_scrap_out(){
		$id_scrap_outxx = (isset($_POST['id_scrap_outxx']) && !empty($_POST['id_scrap_outxx']))?$_POST['id_scrap_outxx']:0;
		$no_dok_out = (isset($_POST['no_dok_out']) && !empty($_POST['no_dok_out']))?$_POST['no_dok_out']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$tg_dok_out = (isset($_POST['tg_dok_out']) && !empty($_POST['tg_dok_out']))?$_POST['tg_dok_out']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$no_scrap = (isset($_POST['no_scrap']) && !empty($_POST['no_scrap']))?$_POST['no_scrap']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$date_scrap = (isset($_POST['date_scrap']) && !empty($_POST['date_scrap']))?$_POST['date_scrap']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$qty = (isset($_POST['qty']) && !empty($_POST['qty']))?$_POST['qty']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$id_scrapxxx = (isset($_POST['id_scrapxxx']) && !empty($_POST['id_scrapxxx']))?$_POST['id_scrapxxx']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$part_no_in = (isset($_POST['part_no_in']) && !empty($_POST['part_no_in']))?$_POST['part_no_in']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$part_name_in = (isset($_POST['part_name_in']) && !empty($_POST['part_name_in']))?$_POST['part_name_in']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$command = (isset($_POST['command']) && !empty($_POST['command']))?$_POST['command']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$username = $this->session->userdata('DX_username');
		
		
		$qry = $this->db->query("
			declare @command varchar(19) = '$command'
			declare @id_scrap_out bigint
			BEGIN TRY
				
				if @command = 'ADD'
				begin
					insert into tbl_scrap_out (
						[date_scrap]
						,[no_scrap]
						,[part_no]
						,[part_name]
					  ,[qty]
					  ,[unit]
					  ,[no_dok_out]
					  ,[tg_dok_out]
					  ,[id_scrap]
					   ,[create_date]
					  ,[create_by]
					  ,[update_date]
					  ,[update_by]
					  
					
					) VALUES (
					'$date_scrap'
					,'$no_scrap'
					,'$part_no_in'
					,'$part_name_in'
					,$qty
					,'KGM'
					,'$no_dok_out'
					,'$tg_dok_out'
					,$id_scrapxxx
					,GETDATE()
					,'$username'
					,GETDATE()
					,'$username'
					
					
					)
				set @id_scrap_out = SCOPE_IDENTITY() 	
				end
				
				if @command = 'UPDATE'
				begin
					update tbl_scrap_out set  
					 [date_scrap] = '$date_scrap'
						,[no_scrap] = '$no_scrap'
						,[part_no] = '$part_no_in'
						,[part_name] = '$part_name_in'
					  ,[qty] = $qty
					  ,[no_dok_out] = '$no_dok_out'
					  ,[tg_dok_out] = '$tg_dok_out'
					  ,[id_scrap] = '$id_scrapxxx'
					  ,[update_date]  = GETDATE()
					  ,[update_by]  = '$username'	
					where id_scrap_out = $id_scrap_outxx
					
				
				end
				if @command = 'DELETE'
				begin
					delete from tbl_scrap_out  where  id_scrap_out = $id_scrap_outxx
				end
			END TRY
			BEGIN CATCH 
				SELECT 
				'error' AS status
				,ERROR_MESSAGE() AS description;
				
			END CATCH; 
			
			if(select count(*) from tbl_scrap_out  where  id_scrap = $id_scrapxxx) > 0
			begin
				update tbl_scrap set  [status]= 'DATA PROCESSED OUT' where id_scrap	= $id_scrapxxx		
			end else 
			begin
				update tbl_scrap set  [status]= 'new' where id_scrap	= $id_scrapxxx
			end
			SELECT 
				'00' AS status
				,' SUKSES' AS description
				
			 ");
		 $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);
		
		
	
	}
	
	function view_scrap_memo(){
		$data['param'] = '';
		$this->load->view('inventory/view_scrap_memo',$data);	
	}
	
	function view_scrap_memo_z0(){
		$id_memo_scrap_hdr = (isset($_POST['id_memo_scrap_hdr']) && !empty($_POST['id_memo_scrap_hdr']))?$_POST['id_memo_scrap_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$no_register = (isset($_POST['no_register']) && !empty($_POST['no_register']))?$_POST['no_register']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$tgl_register = (isset($_POST['tgl_register']) && !empty($_POST['tgl_register']))?$_POST['tgl_register']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$data['id_memo_scrap_hdr'] = $id_memo_scrap_hdr;
		$data['no_register'] = $no_register;
		$data['tgl_register'] = $tgl_register;
		$this->load->view('inventory/view_scrap_memo_z0',$data);	
	}
	
	function view_scrap_memo_ex_bc(){
		$id_scrap_memo_dtl = (isset($_POST['id_scrap_memo_dtl']) && !empty($_POST['id_scrap_memo_dtl']))?$_POST['id_scrap_memo_dtl']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$part_no = (isset($_POST['part_no']) && !empty($_POST['part_no']))?$_POST['part_no']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$data['id_scrap_memo_dtl'] = $id_scrap_memo_dtl;
		$data['part_no'] = $part_no;
		$this->load->view('inventory/view_scrap_memo_ex_bc',$data);	
	}
	
	
	function tbl_scrap_memo(){
		$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_memo_scrap_hdr'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		//$param_not = (isset($_POST['param_not']) && !empty($_POST['param_not']))?$_POST['param_not']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$where = " 1=1 ";
				
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "tgl_surat" ) {
						$searchString = $k->data;
						$where .= "  and CONVERT(VARCHAR(10),[tgl_surat],126)  =  '$searchString'"; 
					}  else if  ($k->field == "no_surat" ) {
						$searchString = $k->data;
						$where .= " and [no_surat] like  '%".$searchString."%'"; 
					} else if  ($k->field == "no_register" ) {
						$searchString = $k->data;
						$where .= " and [no_register] like  '%".$searchString."%'"; 
					} else if  ($k->field == "tgl_register" ) {
						$searchString = $k->data;
						$where .= " and convert(varchar(10), [tgl_register], 126) =  '".$searchString."'"; 
					}
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_inventory->tbl_scrap_memo_All($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_inventory->tbl_scrap_memo_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_memo_scrap_hdr']=$r->id_memo_scrap_hdr; 
			$responce->rows[$i]['no_surat']=$r->no_surat; 
			$responce->rows[$i]['tgl_surat']=$r->tgl_surat; 
			$responce->rows[$i]['no_register']=$r->no_register; 
			$responce->rows[$i]['tgl_register']=$r->tgl_register; 
			$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_data']=$r->status_data; 
		
			
			$i++;
		}
		echo json_encode($responce);
	
	
	}
	
	function v_data_in_dtl_bc_part(){
		$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_memo_scrap_hdr'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		
		$part_no = (isset($_POST['part_no']) && !empty($_POST['part_no']))?str_replace(' ','', $_POST['part_no']):'AAA';
		$tgl = (isset($_POST['tgl']) && !empty($_POST['tgl']))? $_POST['tgl']:'2018-12-31';
		$where = " partno2='$part_no' and  doc_in_date > '$tgl'";
		
				
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "doc_in_date" ) {
						$searchString = $k->data;
						$where .= "  and CONVERT(VARCHAR(10),[doc_in_date],126)  =  '$searchString'"; 
					}  else if  ($k->field == "part_no" ) {
						$searchString = $k->data;
						$where .= " and [part_no] like  '%".$searchString."%'"; 
					} else if  ($k->field == "no_register" ) {
						$searchString = $k->data;
						$where .= " and [no_register] like  '%".$searchString."%'"; 
					} else if  ($k->field == "BC23Tg" ) {
						$searchString = $k->data;
						$where .= " and convert(varchar(10), [BC23Tg], 126) =  '".$searchString."'"; 
					} else if  ($k->field == "part_name" ) {
						$searchString = $k->data;
						$where .= " and [part_name] like  '%".$searchString."%'"; 
					}  else if  ($k->field == "doc_in_no" ) {
						$searchString = $k->data;
						$where .= " and [doc_in_no] like  '%".$searchString."%'"; 
					}  else if  ($k->field == "CAR" ) {
						$searchString = $k->data;
						$where .= " and [CAR] like  '%".$searchString."%'"; 
					}  else if  ($k->field == "BC23No" ) {
						$searchString = $k->data;
						$where .= " and [BC23No] like  '%".$searchString."%'"; 
					}  else if  ($k->field == "typedok" ) {
						$searchString = $k->data;
						$where .= " and [typedok] like  '%".$searchString."%'"; 
					}  
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_inventory->v_data_in_dtl_bc_part_all($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_inventory->v_data_in_dtl_bc_part_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]['id_bc_in_hdr']=$r->id_bc_in_hdr; 
			$responce->rows[$i]['doc_in_from_id']=$r->doc_in_from_id; 
			$responce->rows[$i]['doc_in_no']=$r->doc_in_no; 
			$responce->rows[$i]['doc_in_date']=$r->doc_in_date; 
			$responce->rows[$i]['CAR']=$r->CAR; 
			$responce->rows[$i]['BC23No']=$r->BC23No; 
			$responce->rows[$i]['BC23Tg']=$r->BC23Tg; 
			$responce->rows[$i]['part_no']=$r->part_no; 
			$responce->rows[$i]['part_name']=$r->part_name; 
			$responce->rows[$i]['part_name_customs']=$r->part_name_customs; 
			$responce->rows[$i]['qty_in']=$r->qty_in; 
			$responce->rows[$i]['unit_in']=$r->unit_in; 
			$responce->rows[$i]['harga']=$r->harga; 
			$responce->rows[$i]['kdval']=$r->kdval; 
			
		
			
			
			$i++;
		}
		echo json_encode($responce);
	
		
		
	}
	
	
	function loaddata_bc23dtl(){
		$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'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$idhdr = (isset($_POST['idhdrbc23']) && !empty($_POST['idhdrbc23']))?$_POST['idhdrbc23']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$where = " id_bc_in_hdr=$idhdr ";
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "Tipe" ) {
						$searchString = $k->data;
						$where .= "  and Tipe like  '%".$searchString."%'"; 
					}  else if  ($k->field == "CAR" ) {
						$searchString = $k->data;
						$where .= " and [CAR] like  '%".$searchString."%'"; 
					} else if  ($k->field == "BrgUrai" ) {
						$searchString = $k->data;
						$where .= " and [BrgUrai] like  '%".$searchString."%'"; 
					} else if  ($k->field == "KdBrg" ) {
						$searchString = $k->data;
						$where .= " and KdBrg =  '".$searchString."'"; 
					} 
				}
			}
		}
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_inventory->bc23detildataAll($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_inventory->bc23detildataCountAll($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_bc_in_dtl']=$r->id_bc_in_dtl; 
			$responce->rows[$i]['id_bc_in_hdr']=$r->id_bc_in_hdr; 
			$responce->rows[$i]['CAR']=$r->CAR; 
			$responce->rows[$i]['Serial']=$r->Serial; 
			$responce->rows[$i]['JnsBarangDtl']=$r->JnsBarangDtl; 
			$responce->rows[$i]['TujuanKirimDtl']=$r->TujuanKirimDtl; 
			$responce->rows[$i]['NoHs']=$r->NoHs; 
			$responce->rows[$i]['SeriTrp']=$r->SeriTrp; 
			$responce->rows[$i]['BrgUrai']=$r->BrgUrai; 
			$responce->rows[$i]['Merk']=$r->Merk; 
			$responce->rows[$i]['Tipe']=$r->Tipe; 
			$responce->rows[$i]['SpfLain']=$r->SpfLain; 
			$responce->rows[$i]['KdBrg']=$r->KdBrg; 
			$responce->rows[$i]['Penggunaan']=$r->Penggunaan; 
			$responce->rows[$i]['KemasJn']=$r->KemasJn;
			$responce->rows[$i]['KemasJm']=$r->KemasJm;
			$responce->rows[$i]['KdFasDtl']=$r->KdFasDtl;
			$responce->rows[$i]['BrgAsal']=$r->BrgAsal;
			$responce->rows[$i]['DNilInv']=$r->DNilInv;
			$responce->rows[$i]['KdSat']=$r->KdSat;
			$responce->rows[$i]['JmlSat']=$r->JmlSat;
			$responce->rows[$i]['NettoDtl']=$r->NettoDtl;
			$responce->rows[$i]['SatBmJm']=$r->SatBmJm;
			$responce->rows[$i]['SatCukJm']=$r->SatCukJm;
			$responce->rows[$i]['DtlOk']=$r->DtlOk;
			$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;
			$i++;
		}
		echo json_encode($responce);
		
	}
	
	function tbl_scrap_memo_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_memo_scrap_hdr'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		
		$param_not = (isset($_POST['id_hdr']) && !empty($_POST['id_hdr']))?$_POST['id_hdr']:'AAA';
		
		if ($param_not != 'AAA'){
			
			$where = " id_memo_scrap_hdr=$param_not ";
		} else {
			
			$where = " 1=1 ";
		}
		
			
				
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "tgl_ex_bc" ) {
						$searchString = $k->data;
						$where .= "  and CONVERT(VARCHAR(10),[tgl_ex_bc],126)  =  '$searchString'"; 
					}  else if  ($k->field == "part_no" ) {
						$searchString = $k->data;
						$where .= " and [part_no] like  '%".$searchString."%'"; 
					} else if  ($k->field == "no_register" ) {
						$searchString = $k->data;
						$where .= " and [no_register] like  '%".$searchString."%'"; 
					} else if  ($k->field == "vgi_date" ) {
						$searchString = $k->data;
						$where .= " and convert(varchar(10), [vgi_date], 126) =  '".$searchString."'"; 
					} else if  ($k->field == "issue_no" ) {
						$searchString = $k->data;
						$where .= " and [issue_no] like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_inventory->tbl_scrap_memo_dtl_All($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_inventory->tbl_scrap_memo_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_scrap_memo_dtl']=$r->id_scrap_memo_dtl; 
			$responce->rows[$i]['id_memo_scrap_hdr']=$r->id_memo_scrap_hdr; 
			$responce->rows[$i]['part_no']=$r->part_no; 
			$responce->rows[$i]['qty']=$r->qty; 
			$responce->rows[$i]['unit']=$r->unit; 
			$responce->rows[$i]['weight']=$r->weight; 
			$responce->rows[$i]['no_ex_bc']=$r->no_ex_bc; 
			$responce->rows[$i]['tgl_ex_bc']=$r->tgl_ex_bc; 
			$responce->rows[$i]['no_urut']=$r->no_urut; 
			$responce->rows[$i]['issue_no']=$r->issue_no; 
			$responce->rows[$i]['vgi_date']=$r->vgi_date; 
			$responce->rows[$i]['vgi_qty']=$r->vgi_qty; 
			$responce->rows[$i]['part_name']=$r->part_name; 
			$responce->rows[$i]['part_name_customs']=$r->part_name_customs; 
		
			
			
			$i++;
		}
		echo json_encode($responce);
	
	
	}
	
	function proses_save_memo_hdr(){
		$id_memo_scrap_hdr = (isset($_POST['id_memo_scrap_hdr']) && !empty($_POST['id_memo_scrap_hdr']))?$_POST['id_memo_scrap_hdr']:'AAA';
		$txt_scrap_noreg_req = (isset($_POST['txt_scrap_noreg_req']) && !empty( $_POST['txt_scrap_noreg_req']))?$_POST['txt_scrap_noreg_req']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$txt_scrap_tglreg_req = (isset($_POST['txt_scrap_tglreg_req']) && !empty($_POST['txt_scrap_tglreg_req']))?$_POST['txt_scrap_tglreg_req']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$username = $this->session->userdata('DX_username');
		
		if(trim($txt_scrap_noreg_req) == ''){
			echo '{"sts":"ERROR","desc":" Param Tidak Ditemukan"}';
			die();
			
		}
		
		if($id_memo_scrap_hdr == 'AAA'){
			
			$qry = $this->db->query("
				BEGIN TRY
					insert into tbl_scrap_memo 
					(
						[no_register]
					  ,[tgl_register]
					  ,[create_date]
					  ,[create_by]
					  ,[update_date]
					  ,[update_by]
					  ,[status_data]
						
					
					
					)
					values 
					(
						'$txt_scrap_noreg_req'
						,'$txt_scrap_tglreg_req'
						,GETDATE()
						,'$username'
						,GETDATE()
						,'$username'
						,'DATABARU'
					)	

					
				END TRY
				BEGIN CATCH 
					SELECT 
					'error' AS status
					,ERROR_MESSAGE() AS description;
					
				END CATCH; 
				
				SELECT 
				'00' AS status
				,' SUKSES' AS description
					
			");
			$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);
			
		} else {
			
			$qry = $this->db->query("
				BEGIN TRY
					update tbl_scrap_memo
					set
					no_register = '$txt_scrap_noreg_req'
					,tgl_register = '$txt_scrap_tglreg_req'
					,[update_date] = GETDATE()
					,[update_by] ='$username'
					where id_memo_scrap_hdr = $id_memo_scrap_hdr				

						
				END TRY
				BEGIN CATCH 
					SELECT 
					'error' AS status
					,ERROR_MESSAGE() AS description;
					
				END CATCH; 
				
				SELECT 
				'00' AS status
				,' SUKSES' AS description
					
			");
			$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);
			
			
		}
		
		
	}
	
	function proses_delete_memo_hdr(){
		$id_memo_scrap_hdr = (isset($_POST['id_memo_scrap_hdr']) && !empty( $_POST['id_memo_scrap_hdr']))?$_POST['id_memo_scrap_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$qry = $this->db->query("
				BEGIN TRY
					delete from tbl_scrap_memo where id_memo_scrap_hdr = $id_memo_scrap_hdr
					delete from tbl_scrap_memo_dtl where id_memo_scrap_hdr = $id_memo_scrap_hdr
					

					
				END TRY
				BEGIN CATCH 
					SELECT 
					'error' AS status
					,ERROR_MESSAGE() AS description;
					
				END CATCH; 
				
				SELECT 
				'00' AS status
				,' SUKSES' AS description
					
			");
			$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);
		
		
	}
	
	function proses_delete_memo_dtl(){
		$id_scrap_memo_dtl = (isset($_POST['id_scrap_memo_dtl']) && !empty( $_POST['id_scrap_memo_dtl']))?$_POST['id_scrap_memo_dtl']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$qry = $this->db->query("
				BEGIN TRY
					delete from tbl_scrap_memo_dtl where id_scrap_memo_dtl = $id_scrap_memo_dtl
				END TRY
				BEGIN CATCH 
					SELECT 
					'error' AS status
					,ERROR_MESSAGE() AS description;
					
				END CATCH; 
				
				SELECT 
				'00' AS status
				,' SUKSES' AS description
					
			");
			$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);
		
		
	}
	
	function proses_save_memo_dtl(){
		$id_scrap_memo_dtl = (isset($_POST['id_scrap_memo_dtl']) && !empty( $_POST['id_scrap_memo_dtl']))?$_POST['id_scrap_memo_dtl']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$qty = (isset($_POST['qty']))?$_POST['qty']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$weight = (isset($_POST['weight']))?$_POST['weight']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		
		$qry = $this->db->query("
				BEGIN TRY
					update  tbl_scrap_memo_dtl 
					set
					qty = $qty
					,weight = $weight
					where id_scrap_memo_dtl = $id_scrap_memo_dtl
				END TRY
				BEGIN CATCH 
					SELECT 
					'error' AS status
					,ERROR_MESSAGE() AS description;
					
				END CATCH; 
				
				SELECT 
				'00' AS status
				,' SUKSES' AS description
					
			");
			$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);
		
		
	}
	
	
	function loaddatavgi(){
  
		$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_vgi_hdr'; // get index row - i.e. user click to sort
		$sord = isset($_POST['sord'])?$_POST['sord']:'asc'; // get the direction	
		
		$q = (isset($_POST['q']) && !empty($_POST['q']))?$_POST['q']:'AAA';
		if ($q=='AAA'){
			$where = " 1=1 ";
		} else {
			$where = "r2 = 'z0' ";			
		}	
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "REG_DATE" ) {
						$searchString = $k->data;
						$where .= " and REG_DATE = '$searchString'"; 
					} else if ( $k->field == "ISSUE_NO" ) {
						$searchString = $k->data;
						$where .= " and ISSUE_NO like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_inventory->data_vgi_all($limit,$start,$sidx,$sord, $where);
		$count = $this->mdl_inventory->data_vgi_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_vgi_hdr']=$r->id_vgi_hdr; 
			$responce->rows[$i]['issue_no']=$r->issue_no;
			$responce->rows[$i]['vgi_date']=$r->vgi_date;
			$responce->rows[$i]['r2']=$r->r2;
			$i++;
		}
		echo json_encode($responce);
	
  
  
  }
  
  function loaddata_vgi_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']:'tbl_vgi_dtl.id_vgi_dtl'; // get index row - i.e. user click to sort
	$sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
	
	$id_vgi_hdr = (isset($_POST['id_vgi_hdr_dtl']) && !empty($_POST['id_vgi_hdr_dtl']))?$_POST['id_vgi_hdr_dtl']:die('{"sts":"ERROR","desc":" Param ID Tidak Ditemukan"}');
		
	$where = " tbl_vgi_dtl.id_vgi_hdr=$id_vgi_hdr ";
	
	$nullin = (isset($_POST['nullin']) && !empty($_POST['nullin']))?$_POST['nullin']:'';
	if ($nullin == 'ya'){ $where=" tbl_vgi_dtl.id_vgi_hdr=$id_vgi_hdr and tbl_data_part_hs.part_no is null "; };
	
	if ($search == 'true'){
		if (!empty($filters)){
			$array = json_decode($filters);
			foreach($array->rules as $arr=>$k){
				
				if ( $k->field == "vgi_date" ) {
					$searchString = $k->data;
					$where .= " and tbl_vgi_dtl.vgi_date = '$searchString'"; 
				} else if ( $k->field == "note" ) {
					$searchString = $k->data;
					$where .= " and tbl_vgi_dtl.r1 like  '%".$searchString."%'"; 
				} else if ( $k->field == "assycode" ) {
					$searchString = $k->data;
					$where .= " and tbl_vgi_dtl.r2 like  '%".$searchString."%'"; 
				} else if ( $k->field == "part_number" ) {
					$searchString = $k->data;
					$where .= " and tbl_vgi_dtl.part_number like  '%".$searchString."%'"; 
				} else if ( $k->field == "qty" ) {
					$searchString = $k->data;
					$where .= " and tbl_vgi_dtl.qty=$searchString"; 
				} else if ( $k->field == "part_name" ) {
					$searchString = $k->data;
					$where .= " and tbl_data_part_hs.part_name like  '%".$searchString."%'"; 
				} else if ( $k->field == "part_name_customs" ) {
					$searchString = $k->data;
					$where .= " and tbl_data_part_hs.part_name_customs like  '%".$searchString."%'"; 
				} 
			}
		}
	}
	
	
		
	$start = $limit*$page - $limit;
	if ($start<0) $start=0;
	
	$records = $this->mdl_inventory->data_vgi_dtl_all($limit,$start,$sidx,$sord, $where);
	$count = $this->mdl_inventory->data_vgi_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_vgi_dtl']=$r->id_vgi_dtl; 
		$responce->rows[$i]['id_vgi_hdr']=$r->id_vgi_hdr;
		$responce->rows[$i]['vgi_date']=$r->vgi_date;
		$responce->rows[$i]['note']=$r->note;
		$responce->rows[$i]['assycode']=$r->assycode;
		$responce->rows[$i]['part_number']=$r->part_number;
		$responce->rows[$i]['qty']=$r->qty;
		$responce->rows[$i]['part_name']=$r->part_name;
		$responce->rows[$i]['part_name_customs']=$r->part_name_customs;
		$i++;
	}
	echo json_encode($responce);
	
  }
	function proses_save_memo_dtl_z0(){
		$txt_id_memo_scrap_z0_hdr = (isset($_POST['txt_id_memo_scrap_z0_hdr']) && !empty($_POST['txt_id_memo_scrap_z0_hdr']))?$_POST['txt_id_memo_scrap_z0_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan 1  "}');
		$txt_scrap_noreg_req_z0 = (isset($_POST['txt_scrap_noreg_req_z0']) && !empty( $_POST['txt_scrap_noreg_req_z0']))?$_POST['txt_scrap_noreg_req_z0']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan 2 "}');
		$txt_scrap_tglreg_req_z0 = (isset($_POST['txt_scrap_tglreg_req_z0']) && !empty($_POST['txt_scrap_tglreg_req_z0']))?$_POST['txt_scrap_tglreg_req_z0']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan 3"}');
		$id_vgi_hdr = (isset($_POST['id_vgi_hdr']) && !empty($_POST['id_vgi_hdr']))?$_POST['id_vgi_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan 4"}');
		
		$username = $this->session->userdata('DX_username');
		
		$qry = $this->db->query("
				BEGIN TRY
					
					insert into tbl_scrap_memo_dtl
					(
						[id_memo_scrap_hdr]
					  ,[part_no]
					  ,[qty]
					  --,[unit]
					  --,[weight]
					  --,[no_ex_bc]
					  --,[tgl_ex_bc]
					  --,[no_urut]
					  ,[issue_no]
					  ,[vgi_date]
					  ,[vgi_qty]
					
					
					) 
					select 
					$txt_id_memo_scrap_z0_hdr 
					,part_number
					,qty
				  ,[issue_no]
				  ,[vgi_date]
				  ,[qty]
					
					from 
					tbl_vgi_dtl
					where id_vgi_hdr = 	$id_vgi_hdr 
					
				END TRY
				BEGIN CATCH 
					SELECT 
					'error' AS status
					,ERROR_MESSAGE() AS description;
					
				END CATCH; 
				
				SELECT 
				'00' AS status
				,' SUKSES' AS description
					
			");
			$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);
		

	  
	}
	
	function proses_save_memo_dtl_z0_item(){
		$txt_id_memo_scrap_z0_hdr = (isset($_POST['txt_id_memo_scrap_z0_hdr']) && !empty($_POST['txt_id_memo_scrap_z0_hdr']))?$_POST['txt_id_memo_scrap_z0_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan 1  "}');
		$txt_scrap_noreg_req_z0 = (isset($_POST['txt_scrap_noreg_req_z0']) && !empty( $_POST['txt_scrap_noreg_req_z0']))?$_POST['txt_scrap_noreg_req_z0']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan 2 "}');
		$txt_scrap_tglreg_req_z0 = (isset($_POST['txt_scrap_tglreg_req_z0']) && !empty($_POST['txt_scrap_tglreg_req_z0']))?$_POST['txt_scrap_tglreg_req_z0']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan 3"}');
		$id_vgi_hdr = (isset($_POST['id_vgi_hdr']) && !empty($_POST['id_vgi_hdr']))?$_POST['id_vgi_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan 4"}');
		$id_vgi_dtl = (isset($_POST['id_vgi_dtl']) && !empty($_POST['id_vgi_dtl']))?$_POST['id_vgi_dtl']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan 4"}');
		
		
		
		$username = $this->session->userdata('DX_username');
		
		$qry = $this->db->query("
				BEGIN TRY
					
					insert into tbl_scrap_memo_dtl
					(
						[id_memo_scrap_hdr]
					  ,[part_no]
					  ,[qty]
					  --,[unit]
					  --,[weight]
					  --,[no_ex_bc]
					  --,[tgl_ex_bc]
					  --,[no_urut]
					  ,[issue_no]
					  ,[vgi_date]
					  ,[vgi_qty]
					
					
					) 
					select 
					$txt_id_memo_scrap_z0_hdr 
					,part_number
					,qty
				  ,[issue_no]
				  ,[vgi_date]
				  ,[qty]
					
					from 
					tbl_vgi_dtl
					where id_vgi_hdr = 	$id_vgi_hdr  and id_vgi_dtl=$id_vgi_dtl
					
				END TRY
				BEGIN CATCH 
					SELECT 
					'error' AS status
					,ERROR_MESSAGE() AS description;
					
				END CATCH; 
				
				SELECT 
				'00' AS status
				,' SUKSES' AS description
					
			");
			$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);
		

	  
	}
	
	function scrap_memo_req_dl_hdr(){
		$idnya = (isset($_GET['idnya']) && !empty($_GET['idnya']))?$_GET['idnya']:die('{"sts":"ERROR","desc":" Param  tgawal Tidak Ditemukan"}');
		
		include_once ( APPPATH."libraries/PHPExcel.php");	
		include_once ( APPPATH."libraries/PHPExcel/IOFactory.php");	
		$phpExcel = new PHPExcel();
		$phpExcel->setActiveSheetIndex(0);
		$phpExcel->getActiveSheet()->setTitle('MEMO');
		
		$phpExcel->getActiveSheet()->SetCellValue("B1", "SCRAP REQUISITION (MATERIAL)");
		
		//$phpExcel->getActiveSheet()->SetCellValue("E1", "currency");
		
		$q = $this->db->query("
			select 
				no_register,
				convert(varchar(50),[tgl_register], 107 ) as [tgl_register]
			 from tbl_scrap_memo
				where id_memo_scrap_hdr = $idnya
			
		");
		
		foreach ($q->result() as $r){
			$no_register = $r->no_register;
			$tgl_register = $r->tgl_register;
		}
		
		$phpExcel->getActiveSheet()->SetCellValue("B3", "Date");
		$phpExcel->getActiveSheet()->SetCellValue("C3", ": " . $tgl_register);
		$phpExcel->getActiveSheet()->SetCellValue("B4", "Register No.");
		$phpExcel->getActiveSheet()->setCellValueExplicit("C4", ": " . $r->no_register , PHPExcel_Cell_DataType::TYPE_STRING); 
		
		$phpExcel->getActiveSheet()->SetCellValue("B5", "Company");
		$phpExcel->getActiveSheet()->SetCellValue("C5", ": " . "PEMI");	
		$phpExcel->getActiveSheet()->SetCellValue("B6", "Dept. / Sect.");
		$phpExcel->getActiveSheet()->SetCellValue("C6", ": " . "PPC/MPC");	
		
		$phpExcel->getActiveSheet()->getColumnDimension('A')->setWidth(8.43);
		$phpExcel->getActiveSheet()->getColumnDimension('B')->setWidth(27.43);
		$phpExcel->getActiveSheet()->getColumnDimension('C')->setWidth(28.29);
		$phpExcel->getActiveSheet()->getColumnDimension('D')->setWidth(14.43);
		$phpExcel->getActiveSheet()->getColumnDimension('E')->setWidth(8.43);
		$phpExcel->getActiveSheet()->getColumnDimension('F')->setWidth(11.43);
		
		$phpExcel->getActiveSheet()->SetCellValue("A8", "No");
		$phpExcel->getActiveSheet()->SetCellValue("B8", "PART");
		$phpExcel->getActiveSheet()->SetCellValue("C8", "COMPONENT");
		$phpExcel->getActiveSheet()->SetCellValue("D8", "QTY");
		$phpExcel->getActiveSheet()->SetCellValue("E8", "UNIT");
		$phpExcel->getActiveSheet()->SetCellValue("F8", "WEIGHT");
		
		$phpExcel->getActiveSheet()->getStyle('A8')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('A8')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('A8')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('A8')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			
		$phpExcel->getActiveSheet()->getStyle('B8')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('B8')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('B8')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('B8')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$phpExcel->getActiveSheet()->getStyle('C8')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('C8')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('C8')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('C8')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$phpExcel->getActiveSheet()->getStyle('D8')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('D8')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('D8')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('D8')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$phpExcel->getActiveSheet()->getStyle('E8')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('E8')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('E8')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('E8')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$phpExcel->getActiveSheet()->getStyle('F8')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('F8')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('F8')->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle('F8')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$q = $this->db->query("
			select 
			id_scrap_memo_dtl
				,[part_no]
      ,[qty]
      ,[unit]
      ,[weight]
      ,[no_ex_bc]
       ,convert(varchar(10),[tgl_ex_bc], 126 ) as  [tgl_ex_bc]
      ,[no_urut]
      ,[issue_no]
       ,convert(varchar(10),[vgi_date], 126 ) as  [vgi_date]
      ,[vgi_qty]
      ,[part_name]
      ,[part_name_customs]
			 ,tbl_kode_barang.satuan 
			 from v_scrap_memo_dtl
			 left outer join tbl_kode_barang on v_scrap_memo_dtl.part_name_customs = tbl_kode_barang.nama_barang
				where id_memo_scrap_hdr = $idnya
			
		");
		
		
		
		
		$i = 9;
		$j=1;
		foreach($q->result() as $r){
			$phpExcel->getActiveSheet()->SetCellValue("A" . "$i", $j);		
			$phpExcel->getActiveSheet()->setCellValueExplicit("B" . "$i", $r->part_no , PHPExcel_Cell_DataType::TYPE_STRING); 
			$phpExcel->getActiveSheet()->setCellValueExplicit("C" . "$i", $r->part_name , PHPExcel_Cell_DataType::TYPE_STRING); 
			$phpExcel->getActiveSheet()->SetCellValue("D" . "$i", $r->qty);
			$phpExcel->getActiveSheet()->setCellValueExplicit("E" . "$i", $r->satuan , PHPExcel_Cell_DataType::TYPE_STRING); 
			$phpExcel->getActiveSheet()->SetCellValue("F" . "$i", $r->weight);
			
			
			$phpExcel->getActiveSheet()->getStyle("D"."$i")->getNumberFormat()->setFormatCode('#,##0.0000');	
			$phpExcel->getActiveSheet()->getStyle("F"."$i")->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$phpExcel->getActiveSheet()->getStyle("A"."$i")->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
			
			$i=$i+1;
			$j++;			
		}
		$k=$i-1;
		$phpExcel->getActiveSheet()->getStyle("A"."$k")->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle("B"."$k")->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle("C"."$k")->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle("D"."$k")->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle("E"."$k")->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$phpExcel->getActiveSheet()->getStyle("F"."$k")->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		
		
		$filename = 'format_memo_scrap.xls';
		header("Content-type:application/vnd.ms-excel");
		header("Content-disposition:attachment; filename=$filename"); 
		header("Pragma:no-cache");
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		header("Expires:0");

		$objWriter = PHPExcel_IOFactory::createWriter($phpExcel, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
		$objWriter->save('php://output');  //send it to user, of course you can save it to disk also!
		 
		exit; //done.. exiting!	
		
		
		
	}
	
	function proses_update_memo_dtl_ex_bc(){
		$id_scrap_memo_dtl = (isset($_POST['id_scrap_memo_dtl']) && !empty( $_POST['id_scrap_memo_dtl']))?$_POST['id_scrap_memo_dtl']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$id_bc23_dtl = (isset($_POST['id_bc23_dtl']))?$_POST['id_bc23_dtl']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$serial = (isset($_POST['serial']))?$_POST['serial']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$id_bc_in_hdr = (isset($_POST['id_bc_in_hdr']) && !empty( $_POST['id_bc_in_hdr']))?$_POST['id_bc_in_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$unit = (isset($_POST['unit']) && !empty( $_POST['unit']))?$_POST['unit']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		
		$qry = $this->db->query("
				
				declare @car varchar(50), @nobc varchar(50), @tglbc date
				select 
					@car = car,
					@nobc = [BC23No],
					@tglbc = [BC23Tg]
				from [tbl_bc_in_hdr]
				where id_bc_in_hdr = $id_bc_in_hdr
				
				BEGIN TRY
					update  tbl_scrap_memo_dtl 
					set
					[no_ex_bc]  = @nobc
					  ,[tgl_ex_bc] = @tglbc
					  ,[no_urut] = $serial
						 ,[unit] = '$unit'
					
					where id_scrap_memo_dtl = $id_scrap_memo_dtl
				END TRY
				BEGIN CATCH 
					SELECT 
					'error' AS status
					,ERROR_MESSAGE() AS description;
					
				END CATCH; 
				
				SELECT 
				'00' AS status
				,' SUKSES' AS description
					
			");
			$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);
		
		
		
	}
		
		
	
  
}

