<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CT_planprod 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("production/mdl_production", 'mdl_production');
		
    }
    
    
    function index()
    {
        $this->load->view('production/view_planprod');
    }
	
	function loaddatabom(){
		$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_bom_hdr'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$where = " 1=1 ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "ASSY_CODE" ) {
						$searchString = $k->data;
						$where .= " and ASSY_CODE like  '%".$searchString."%'"; 
					} else if ( $k->field == "supplier_name" ) {
						$searchString = $k->data;
						$where .= " and ASSY_NO like  '%".$searchString."%'";
					} else if ( $k->field == "filename" ) {
						$searchString = $k->data;
						$where .= " and filename like  '%".$searchString."%'"; 
					} else if ( $k->field == "dateexec" ) {
						$searchString = $k->data;
						$where .= " and dateexec =  '$searchString'"; 
					} 
				}
			}
		}
		
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_production->databom_All($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_production->databom_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_bom_hdr']=$r->id_bom_hdr; 
			$responce->rows[$i]['ASSY_CODE']=$r->ASSY_CODE; 
			$responce->rows[$i]['ASSY_NO']=$r->ASSY_NO; 
			$responce->rows[$i]['filename']=$r->filename;
			$responce->rows[$i]['itemcount']=$r->itemcount;
			$responce->rows[$i]['dateexec']=$r->dateexec;
			
			$i++;
		}
		echo json_encode($responce);
		
	
	}
	
	function getadataloaderbom(){

		$qry = $this->db->query("
			  BEGIN TRY 	
				exec('spi_bom')
			  END TRY
			 BEGIN CATCH 
				SELECT 
				'error' 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'].' Data Loaded');
		 }  
		 echo json_encode($responce);
		
	}
	
	function view_planprod_bom(){
		
		$id_bom_hdr = (isset($_POST['id_bom_hdr']) && !empty($_POST['id_bom_hdr']))?$_POST['id_bom_hdr']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		$data["id_bom_hdr"]=$id_bom_hdr;
		$this->load->view('production/view_planprod_bom',$data);
	
	}
	
	function data_planprod_bom(){
		$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_bom_hdr'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		$hdnassycode = (isset($_POST['hdnassycode']) && !empty($_POST['hdnassycode']))?$_POST['hdnassycode']:die('{"sts":"ERROR","desc":" Param Tidak Ditemukan"}');
		
		$where = " tbl_bom_dtl.ASSY_CODE='$hdnassycode' ";
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "ASSY_CODE" ) {
						$searchString = $k->data;
						$where .= " and ASSY_CODE like  '%".$searchString."%'"; 
					} else if ( $k->field == "supplier_name" ) {
						$searchString = $k->data;
						$where .= " and ASSY_NO like  '%".$searchString."%'";
					} else if ( $k->field == "filename" ) {
						$searchString = $k->data;
						$where .= " and filename like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_production->databomdtl_All($limit,$start,$sidx,$sord, $where);
        $count = $this->mdl_production->databomdtl_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_bom']=$r->id_bom; 
			$responce->rows[$i]['ASSY_CODE']=$r->ASSY_CODE; 
			$responce->rows[$i]['ASSY_NO']=$r->ASSY_NO; 
			$responce->rows[$i]['PART_NO']=$r->PART_NO;
			$responce->rows[$i]['PARTNO2']=$r->PARTNO2;
			$responce->rows[$i]['QTY']=$r->QTY;
			$responce->rows[$i]['FLAG']=$r->FLAG;
			$responce->rows[$i]['STATUS']=$r->STATUS;
			$responce->rows[$i]['status_bom']=$r->status_bom;
			$responce->rows[$i]['id_part_number']=$r->id_part_number;
			$responce->rows[$i]['part_name']=$r->part_name;
			
			
			$i++;
		}
		echo json_encode($responce);
		
	
	
	}
    
        
}
    



?>