<?php
class CT_outgoing_scrap_mesin extends CI_Controller{
	function __construct()
	{
	   parent::__construct();
		if (!$this->dx_auth->is_logged_in()) {
			header('HTTP/1.1 401 Unauthorize');die('expire'); 
		}
		
		$uid = $this->session->userdata('DX_user_id');
		$username=$this->session->userdata('DX_username');
		if(!permissionAuth(2) && $uid != RID){ 
			header('HTTP/1.1 401 Unauthorize');die('noaccess');
		}
		
		$this->load->model("pasi_inventory/mdl_inventory", 'pasi_mdl_inventory');
		$this->load->model("pasi_upload/mdl_upload", 'pasi_mdl_upload');
		$this->db_pasi =$this->load->database('pasi', TRUE);

	}
	
	function index(){
	
		$this->load->view('pasi_inventory/view_outgoing_scrap_mesin');
	
	}
	
	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_bc25_invoice'; // 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 == "No_Invoice" ) {
						$searchString = $k->data;
						$where .= " and No_Invoice like  '%".$searchString."%'"; 	
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->pasi_mdl_inventory->bc25exportscrapmesin_All($limit,$start,$sidx,$sord, $where);
		$count = $this->pasi_mdl_inventory->bc25exportscrapmesin_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_invoice_scrap_mesin']=$r->id_invoice_scrap_mesin;
			$responce->rows[$i]['nodaf']=$r->nodaf;
			$responce->rows[$i]['tgl_daf']=$r->tgl_daf;
			$responce->rows[$i]['car']=$r->car;
			$responce->rows[$i]['out_no']=$r->out_no;
			$responce->rows[$i]['out_tgl']=$r->out_tgl;
			$responce->rows[$i]['kdbrg']=$r->kdbrg;
			$responce->rows[$i]['nama_barang']=$r->nama_barang;
			$responce->rows[$i]['jenis_barang']=$r->jenis_barang;
			$responce->rows[$i]['jumlah']=$r->jumlah;
			$responce->rows[$i]['satuan']=$r->satuan;
			$responce->rows[$i]['total_harga']=$r->total_harga;
			$responce->rows[$i]['no_invoice']=$r->no_invoice;
			$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 getbc25_scrapdataitemlist(){
		$file_name 		= (isset($_POST['file_name']) && !empty($_POST['file_name']))?$_POST['file_name']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$id_upload_bc25_scrap 	= (isset($_POST['id_upload_bc25_scrapmesin']) && !empty($_POST['id_upload_bc25_scrapmesin']))?$_POST['id_upload_bc25_scrapmesin']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$file_path 		= (isset($_POST['file_path']) && !empty($_POST['file_path']))?$_POST['file_path']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$username = $this->session->userdata('DX_username');
		
		$apppath = $this->config->item('APP_upload_path');
		$path=$apppath.'masterdata/';
		
		//echo "exec('sp_get_from_invoice_bc25_scrap @id_upload=$id_upload_bc25_scrap , @filedir=''$path'',@username=$username,@filename=''$file_name''')";die();
		
		$qry = $this->db_pasi->query("
			
			BEGIN TRY
				exec('sp_get_from_invoice_bc25_scrap_mesin @id_upload=$id_upload_bc25_scrap , @filedir=''$path'',@username=$username,@filename=''$file_name''')
			END TRY
			BEGIN CATCH
				SELECT 
					'06' AS STATUS
					,ERROR_MESSAGE() AS DESCRIPTION;
				END CATCH
				
			 ");
			 
		 $rs = $qry->result_array();
         // $r = $qry->result();		
		 //var_dump($rs);die();
		 
		 foreach ($rs as $row){
			 $responce = array("sts" => $row['STATUS'], "desc" => $row['DESCRIPTION'].' Updated/Inserted to Exp. BC 25');
		 }  
		 echo json_encode($responce);
	}

	function view_outgoing_scrap_mesin_itemlist(){
	
		$this->load->view('pasi_inventory/view_outgoing_scrap_mesinitemlist');
	
	}

	function data_bc25_scrap_mesinlist(){
	
		$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_bc25_scrap'; // 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->uploadbc25_scrapmachineall($limit,$start,$sidx,$sord, $where);
        $count = $this->pasi_mdl_upload->uploadbc25_scrapmesincountAll($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_bc25_scrapmesin']=$r->id_upload_bc25_scrapmesin; 
			$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 prosesdatabc25(){
        $bc_status = 'BC25';
		$username=$this->session->userdata('DX_username');
		
		//echo "exec('sp_insert_bc25_scrap_from_invoice @username=''$username'', @bc=''$bc_status''  ')";
		//die();

        $qry = $this->db_pasi->query("

			BEGIN TRY
				exec('sp_insert_bc25_scrap_mesin_from_invoice @username=''$username'', @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);

    }
 }