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

	}
	
	function index(){
	
		$this->load->view('pasi_settings/view_rawmaterial_reff');
	
	}
	
	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_raw_material'; // 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 == "itemno" ) {
						$searchString = $k->data;
						$where .= " and itemno like  '%".$searchString."%'"; 
					} else if ( $k->field == "partno" ) {
						$searchString = $k->data;
						$where .= " and partno like  '%".$searchString."%'"; 
					} else if ( $k->field == "unit" ) {
						$searchString = $k->data;
						$where .= " and unit like  '%".$searchString."%'"; 
					} else if ( $k->field == "status" ) {
						$searchString = $k->data;
						$where .= " and status like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->pasi_mdl_settings->datarawmaterial_All($limit,$start,$sidx,$sord, $where);
		$count = $this->pasi_mdl_settings->datarawmaterial_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_raw_material']=$r->id_raw_material; 
			$responce->rows[$i]['itemno']=$r->itemno; 
			$responce->rows[$i]['partno']=$r->partno;
			$responce->rows[$i]['unit']=$r->unit;
			$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 view_bahanbaku_update(){
		
		$part_no  = (isset($_POST['part_no']) && !empty($_POST['part_no']))?$_POST['part_no']:'';
		$part_name_customs  = (isset($_POST['part_name_customs']) && !empty($_POST['part_name_customs']))?$_POST['part_name_customs']:'';
		$part_name  = (isset($_POST['part_name']) && !empty($_POST['part_name']))?$_POST['part_name']:'';
		$hs_code  = (isset($_POST['hs_code']) && !empty($_POST['hs_code']))?$_POST['hs_code']:'';
		$bm  = (isset($_POST['bm']) && !empty($_POST['bm']))?$_POST['bm']:'';
		$cek_price  = (isset($_POST['cek_price']) && !empty($_POST['cek_price']))?$_POST['cek_price']:'';
		$idx = (isset($_POST['idx']) && !empty($_POST['idx']))?$_POST['idx']:'';
		$part_no2 = (isset($_POST['part_no2']) && !empty($_POST['part_no2']))?$_POST['part_no2']:'';
	
		$part_number2 = str_replace(" ", "",$part_no);
		
		$data["part_no"] = $part_no;
		$data["part_no2"] = $part_no2; 
		$data["part_name_customs"] = $part_name_customs; 
		$data["idx"] = $idx; 
		$data["part_name"] = $part_name; 
		$data["hs_code"] = $hs_code; 
		$data["bm"] = $bm; 
		$data["cek_price"] = $cek_price; 
		
		$this->load->view('pasi_settings/view_bahanbaku_update', $data);
	
	}
	function getdatarawmaterial(){
		$file_name 		= (isset($_POST['file_name']) && !empty($_POST['file_name']))?$_POST['file_name']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$id_upload_rm 	= (isset($_POST['id_upload_rm']) && !empty($_POST['id_upload_rm']))?$_POST['id_upload_rm']: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_raw_material @filedir=''$path'',@username=$username,@filename=''$file_name''')";
		//die();
		
		$qry = $this->db_pasi->query("
			
			BEGIN TRY
				exec('sp_raw_material_reff @id_upload=$id_upload_rm , @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');
		 }  
		 echo json_encode($responce);
	}
	
	function view_rawmaterial_reff_itemlist(){
	
		$this->load->view('pasi_settings/view_rawmaterial_reff_itemlist');
	
	}
	
	function data_rawmaterial_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_rm'; // 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->uploadrm_all($limit,$start,$sidx,$sord, $where);
        $count = $this->pasi_mdl_upload->uploadrm_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_rm']=$r->id_upload_rm; 
			$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);	
	}
 }