<?php
class CT_finishgoodcode 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_finishgoodcode');
	
	}
	
	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_master_finishgood'; // 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 == "kdbrg" ) {
						$searchString = $k->data;
						$where .= " and kdbrg like  '%".$searchString."%'"; 
					} else if ( $k->field == "item_no" ) {
						$searchString = $k->data;
						$where .= " and item_no like  '%".$searchString."%'"; 
					} else if ( $k->field == "nama_barang" ) {
						$searchString = $k->data;
						$where .= " and nama_barang like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->pasi_mdl_settings->datamasterfinishgoodcode_All($limit,$start,$sidx,$sord, $where);
		$count = $this->pasi_mdl_settings->datamasterfinishgoodcode_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_master_finishgood']=$r->id_master_finishgood; 
			$responce->rows[$i]['kdbrg']=$r->kdbrg;
			$responce->rows[$i]['nama_barang']=$r->nama_barang;
			$responce->rows[$i]['satuan']=$r->satuan;
			$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 view_finishgoodcode_update(){
		$idx  = (isset($_POST['idx']) && !empty($_POST['idx']))?$_POST['idx']:'';
		$kd_brg  = (isset($_POST['kd_brg']) && !empty($_POST['kd_brg']))?$_POST['kd_brg']:'';
		$nama_barang  = (isset($_POST['nama_barang']) && !empty($_POST['nama_barang']))?$_POST['nama_barang']:'';
		$satuan  = (isset($_POST['satuan']) && !empty($_POST['satuan']))?$_POST['satuan']:'';
		
		$data["kd_brg"] = $kd_brg; 
		$data["nama_barang"] = $nama_barang; 
		$data["idx"] = $idx; 
		$data["satuan"] = $satuan;  
		
		$this->load->view('pasi_settings/view_finishgoodcode_update', $data);
	
	}
	function view_finishgoodcode_insert(){ 
	
		$this->load->view('pasi_settings/view_finishgoodcode_insert');
	
	}
	
	function insertkodebarang(){
		$username = $this->session->userdata('DX_username');
		$kodebarang  = (isset($_POST['kodebarang']) && !empty($_POST['kodebarang']))?$_POST['kodebarang']:die('{"sts":"ERROR","desc":" Param kodebarang Tidak Ditemukan"}');
		$namabarang  = (isset($_POST['namabarang']) && !empty($_POST['namabarang']))?$_POST['namabarang']:die('{"sts":"ERROR","desc":" Param namabarang Tidak Ditemukan"}');
		$satuan  = (isset($_POST['satuan']) && !empty($_POST['satuan']))?$_POST['satuan']:die('{"sts":"ERROR","desc":" Param satuan Tidak Ditemukan"}');		

		$qry = $this->db_pasi->query("
		
			BEGIN TRY
				exec('sp_insert_finishgood_code @username=''$username'', @kodebarang=''$kodebarang'',@namabarang=''$namabarang'',@satuan=''$satuan'', @cmd=''insert'' ') 
			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);
	}

	function updatekodebarang(){
		$username = $this->session->userdata('DX_username');
		$id = (isset($_POST['id']) && !empty($_POST['id']))?$_POST['id']:die('{"sts":"ERROR","desc":" Param id Tidak Ditemukan"}');
		$kodebarang  = (isset($_POST['kodebarang']) && !empty($_POST['kodebarang']))?$_POST['kodebarang']:die('{"sts":"ERROR","desc":" Param kodebarang Tidak Ditemukan"}');
		$namabarang  = (isset($_POST['namabarang']) && !empty($_POST['namabarang']))?$_POST['namabarang']:die('{"sts":"ERROR","desc":" Param namabarang Tidak Ditemukan"}');
		$satuan  = (isset($_POST['satuan']) && !empty($_POST['satuan']))?$_POST['satuan']:die('{"sts":"ERROR","desc":" Param satuan Tidak Ditemukan"}');		

		//echo "exec('sp_update_material_code @id=$id,@username=''$username'', @kodebarang=''$kodebarang'',@itemno=''$itemno'',@namabarang=''$namabarang'',@satuan=''$satuan'', @cmd=''update'' ')";die(); 
		$qry = $this->db_pasi->query("
		
			BEGIN TRY
				exec('sp_update_finishgood_code @id=$id,@username=''$username'', @kodebarang=''$kodebarang'',@namabarang=''$namabarang'',@satuan=''$satuan'', @cmd=''update'' ') 
			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);
	}

	function deletekodebarang(){
		$username = $this->session->userdata('DX_username');
		$id = (isset($_POST['idx']) && !empty($_POST['idx']))?$_POST['idx']:die('{"sts":"ERROR","desc":" Param id Tidak Ditemukan"}');
		
		$qry = $this->db_pasi->query("
		
			BEGIN TRY
				exec('sp_delete_finishgood_code @username=''$username'', @id=$id, @cmd=''delete'' ') 
			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);
	}	
	function getMasterFinishGoodCode(){
		$file_name 		= (isset($_POST['file_name']) && !empty($_POST['file_name']))?$_POST['file_name']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$id_upload_brg 	= (isset($_POST['id_upload_brg']) && !empty($_POST['id_upload_brg']))?$_POST['id_upload_brg']: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_master_finishgood_code @id_upload=$id_upload_brg , @filedir=''$path'',@username=$username,@filename=''$file_name''')";die();
		
		$qry = $this->db_pasi->query("
			
			BEGIN TRY
				exec('sp_master_finishgood_code @id_upload=$id_upload_brg , @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_finishgoodcode_itemlist(){
	
		$this->load->view('pasi_settings/view_finishgoodcode_itemlist');
	
	}
	
	function data_masterbarang_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_brg'; // 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->uploadbrg_all($limit,$start,$sidx,$sord, $where);
        $count = $this->pasi_mdl_upload->uploadbrg_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_brg']=$r->id_upload_brg; 
			$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);	
	}
 }