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

class CT_datapart_save 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('datapart/view_planprod');
    }






	    function save_rak(){  
	 	       $kode_rak        = isset($_POST['kode_rak'])?$_POST['kode_rak']:'-1'; // get the requested page 
	  	       $nama_rak        = isset($_POST['nama_rak'])?$_POST['nama_rak']:''; // get the requested page 
	  	       $lantai          = isset($_POST['lantai'])?$_POST['lantai']:''; // get the requested page 
	  	       $nomor_rak       = isset($_POST['nomor_rak'])?$_POST['nomor_rak']:''; // get the requested page 
	  	       $posisi          = isset($_POST['posisi'])?$_POST['posisi']:''; // get the requested page 
	  	       $note            = isset($_POST['note'])?$_POST['note']:''; // get the requested page 
               $max_stock       = isset($_POST['max_stock'])?$_POST['max_stock']:''; // get the requested page 
               $min_stock       = isset($_POST['min_stock'])?$_POST['min_stock']:''; // get the requested page 
 	   
 	   

				$xmldoc =  XMLbasic('json'); 
		 		AddField($xmldoc ,'kode_rak',$kode_rak ); 
		 		AddField($xmldoc ,'nama_rak',$nama_rak );
		 		AddField($xmldoc ,'lantai',$lantai );
		 		AddField($xmldoc ,'nomor_rak',$nomor_rak );
		 		AddField($xmldoc ,'posisi',$posisi );
                AddField($xmldoc ,'note',$note );
                AddField($xmldoc ,'max_stock',$max_stock );
                AddField($xmldoc ,'min_stock',$min_stock );
                AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));

				$str = $xmldoc->saveXML();
			 	// print_r($str);die();
				exec_sp_array($xmldoc, 'dbo.SPX_DATARAK_ADD',$sts,$des,$rows,$detail); 
				echo json_encode(array('valid'=>($sts=='00'?true:false)
				,'stsno' => $sts
				, 'stsmsg' => $des
				, 'detail' => $detail)
				); 
		}
 
        function save_sparepart_use(){  
            $kode_mesin        = isset($_POST['kode_mesin'])?$_POST['kode_mesin']:'-1'; // get the requested page 
            $nama_mesin        = isset($_POST['nama_mesin'])?$_POST['nama_mesin']:''; // get the requested page 
            $kode_sparepart    = isset($_POST['kode_sparepart'])?$_POST['kode_sparepart']:''; // get the requested page 
            $nama_sparepart    = isset($_POST['nama_sparepart'])?$_POST['nama_sparepart']:''; // get the requested page 
            $tanggal           = isset($_POST['tanggal'])?$_POST['tanggal']:''; // get the requested page 
            $jumlah            = isset($_POST['jumlah'])?$_POST['jumlah']:''; // get the requested page 
     
    

            $xmldoc =  XMLbasic('json'); 
             AddField($xmldoc ,'kode_mesin',$kode_mesin ); 
             AddField($xmldoc ,'nama_mesin',$nama_mesin );
             AddField($xmldoc ,'kode_sparepart',$kode_sparepart );
             AddField($xmldoc ,'nama_sparepart',$nama_sparepart );
             AddField($xmldoc ,'tanggal',$tanggal );
            AddField($xmldoc ,'jumlah',$jumlah );
            AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));

            $str = $xmldoc->saveXML();
             // print_r($str);die();
            exec_sp_array($xmldoc, 'dbo.SPX_SPAREPART_USE_ADD',$sts,$des,$rows,$detail); 
            echo json_encode(array('valid'=>($sts=='00'?true:false)
            ,'stsno' => $sts
            , 'stsmsg' => $des
            , 'detail' => $detail)
            ); 
    }

        function edit_rak(){  
            $id_rak        = isset($_POST['id_rak'])?$_POST['id_rak']:''; // get the requested page 
             $kode_rak        = isset($_POST['kode_rak'])?$_POST['kode_rak']:''; // get the requested page 
             $nama_rak        = isset($_POST['nama_rak'])?$_POST['nama_rak']:''; // get the requested page 
             $lantai          = isset($_POST['lantai'])?$_POST['lantai']:''; // get the requested page 
             $nomor_rak       = isset($_POST['nomor_rak'])?$_POST['nomor_rak']:''; // get the requested page 
             $posisi          = isset($_POST['posisi'])?$_POST['posisi']:''; // get the requested page 
             $note            = isset($_POST['note'])?$_POST['note']:''; // get the requested page 
             $max_stock       = isset($_POST['max_stock'])?$_POST['max_stock']:''; // get the requested page 
             $min_stock       = isset($_POST['min_stock'])?$_POST['min_stock']:''; // get the requested page 
  
    

            $xmldoc =  XMLbasic('json'); 
            AddField($xmldoc ,'id_rak',$id_rak ); 
             AddField($xmldoc ,'kode_rak',$kode_rak ); 
             AddField($xmldoc ,'nama_rak',$nama_rak );
             AddField($xmldoc ,'lantai',$lantai );
             AddField($xmldoc ,'nomor_rak',$nomor_rak );
             AddField($xmldoc ,'posisi',$posisi );
             AddField($xmldoc ,'note',$note );
             AddField($xmldoc ,'max_stock',$max_stock );
             AddField($xmldoc ,'min_stock',$min_stock );
            AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));

            $str = $xmldoc->saveXML();
             // print_r($str);die();
            exec_sp_array($xmldoc, 'dbo.SPX_DATARAK_EDIT',$sts,$des,$rows,$detail); 
            echo json_encode(array('valid'=>($sts=='00'?true:false)
            ,'stsno' => $sts
            , 'stsmsg' => $des
            , 'detail' => $detail)
            ); 
    }


    function delete_rak(){  
        $id_rak = isset($_POST['id_rak'])?$_POST['id_rak']:'-1'; // get the requested page 
   
        $xmldoc =  XMLbasic('json'); 
         AddField($xmldoc ,'id_rak',$id_rak ); 
             
        $str = $xmldoc->saveXML();
         // print_r($str);die();
         exec_sp_array($xmldoc, 'dbo.SPX_DATARAK_DELETE',$sts,$des,$rows,$detail); 
        echo json_encode(array('valid'=>($sts=='00'?true:false)
        ,'stsno' => $sts
        , 'stsmsg' => $des
        , 'detail' => $detail)
        ); 
}  

function delete_sparepart(){  
    $id_sparepart = isset($_POST['id_sparepart'])?$_POST['id_sparepart']:'-1'; // get the requested page 

    $xmldoc =  XMLbasic('json'); 
     AddField($xmldoc ,'id_sparepart',$id_sparepart ); 
         
    $str = $xmldoc->saveXML();
     // print_r($str);die();
     exec_sp_array($xmldoc, 'dbo.SPX_SPAREPART_DELETE',$sts,$des,$rows,$detail); 
    echo json_encode(array('valid'=>($sts=='00'?true:false)
    ,'stsno' => $sts
    , 'stsmsg' => $des
    , 'detail' => $detail)
    ); 
}  


function delete_sparepart_use(){  
    $id_header = isset($_POST['id_header'])?$_POST['id_header']:'-1'; // get the requested page 
    $username = isset($_POST['username'])?$_POST['username']:'';

 
    $xmldoc =  XMLbasic('json'); 
     AddField($xmldoc ,'id_header',$id_header ); 
    AddField($xmldoc ,'username',$username );        
    $str = $xmldoc->saveXML();
     // print_r($str);die();
     exec_sp_array($xmldoc, 'dbo.SPX_SPAREPART_USE_DELETE',$sts,$des,$rows,$detail); 
    echo json_encode(array('valid'=>($sts=='00'?true:false)
    ,'stsno' => $sts
    , 'stsmsg' => $des
    , 'detail' => $detail)
    ); 
}  


function save_sparepart(){  

    $kode_sparepart		=isset($_POST['kode_sparepart'])?$_POST['kode_sparepart']:'-1';     
    $nama_sparepart		=isset($_POST['nama_sparepart'])?$_POST['nama_sparepart']:'';     
    $item_no			=isset($_POST['item_no'])?$_POST['item_no']:''; 
    $satuan				=isset($_POST['satuan'])?$_POST['satuan']:'';    
    $model				=isset($_POST['model'])?$_POST['model']:'';    
    $lokasi				=isset($_POST['lokasi'])?$_POST['lokasi']:'';    
    $maker				=isset($_POST['maker'])?$_POST['maker']:'';   
    $suppno				=isset($_POST['suppno'])?$_POST['suppno']:'';   
    $suppname			=isset($_POST['suppname'])?$_POST['suppname']:'';  
    $note1				=isset($_POST['note1'])?$_POST['note1']:'';    
    $note2				=isset($_POST['note2'])?$_POST['note2']:'';       
    $note3				=isset($_POST['note3'])?$_POST['note3']:'';

  

    $xmldoc =  XMLbasic('json'); 
    AddField($xmldoc ,'kode_sparepart',$kode_sparepart ); 
    AddField($xmldoc ,'nama_sparepart',$nama_sparepart );
    AddField($xmldoc ,'item_no',$item_no );
    AddField($xmldoc ,'satuan',$satuan );
    AddField($xmldoc ,'model',$model );
    AddField($xmldoc ,'lokasi',$lokasi );
    AddField($xmldoc ,'maker',$maker );
    AddField($xmldoc ,'suppno',$suppno );
    AddField($xmldoc ,'suppname',$suppname );
    AddField($xmldoc ,'note1',$note1 );
    AddField($xmldoc ,'note2',$note2 );
    AddField($xmldoc ,'note3',$note3 );
    AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));

    $str = $xmldoc->saveXML();
     // print_r($str);die();
    exec_sp_array($xmldoc, 'dbo.SPX_SPAREPART_ADD',$sts,$des,$rows,$detail); 
    echo json_encode(array('valid'=>($sts=='00'?true:false)
    ,'stsno' => $sts
    , 'stsmsg' => $des
    , 'detail' => $detail)
    ); 
}


function edit_sparepart(){  
    $id_sparepart		=isset($_POST['id_sparepart'])?$_POST['id_sparepart']:'';
    $kode_sparepart		=isset($_POST['kode_sparepart'])?$_POST['kode_sparepart']:'';     
    $nama_sparepart		=isset($_POST['nama_sparepart'])?$_POST['nama_sparepart']:'';     
    $item_no			=isset($_POST['item_no'])?$_POST['item_no']:''; 
    $satuan				=isset($_POST['satuan'])?$_POST['satuan']:'';    
    $model				=isset($_POST['model'])?$_POST['model']:'';    
    $lokasi				=isset($_POST['lokasi'])?$_POST['lokasi']:'';    
    $maker				=isset($_POST['maker'])?$_POST['maker']:'';   
    $suppno				=isset($_POST['suppno'])?$_POST['suppno']:'';     
    $suppname			=isset($_POST['suppname'])?$_POST['suppname']:'';     
    $note1				=isset($_POST['note1'])?$_POST['note1']:'';    
    $note2				=isset($_POST['note2'])?$_POST['note2']:'';      
    $note3				=isset($_POST['note3'])?$_POST['note3']:'';

  

    $xmldoc =  XMLbasic('json'); 
    AddField($xmldoc ,'id_sparepart',$id_sparepart ); 
    AddField($xmldoc ,'kode_sparepart',$kode_sparepart ); 
    AddField($xmldoc ,'nama_sparepart',$nama_sparepart );
    AddField($xmldoc ,'item_no',$item_no );
    AddField($xmldoc ,'satuan',$satuan );
    AddField($xmldoc ,'model',$model );
    AddField($xmldoc ,'lokasi',$lokasi );
    AddField($xmldoc ,'maker',$maker );
    AddField($xmldoc ,'suppno',$suppno );
    AddField($xmldoc ,'suppname',$suppname );
    AddField($xmldoc ,'note1',$note1 );
    AddField($xmldoc ,'note2',$note2 );
    AddField($xmldoc ,'note3',$note3 );
    AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));

    $str = $xmldoc->saveXML();
     // print_r($str);die();
    exec_sp_array($xmldoc, 'dbo.SPX_SPAREPART_EDIT',$sts,$des,$rows,$detail); 
    echo json_encode(array('valid'=>($sts=='00'?true:false)
    ,'stsno' => $sts
    , 'stsmsg' => $des
    , 'detail' => $detail)
    ); 
}



function save_header_sparepart_use(){   
    $header_id          = isset($_POST['header_id'])?$_POST['header_id']:'-1'; // get the requested page 
    $no_dokumen         = isset($_POST['no_dokumen'])?$_POST['no_dokumen']:''; // get the requested page 
    $tanggal            = isset($_POST['tanggal'])?$_POST['tanggal']:''; // get the requested page 
    $no_shift           = isset($_POST['no_shift'])?$_POST['no_shift']:''; // get the requested page 
    $kode_mesin         = isset($_POST['kode_mesin'])?$_POST['kode_mesin']:''; // get the requested page 
    $nama_mesin         = isset($_POST['nama_mesin'])?$_POST['nama_mesin']:''; // get the requested page 
                                   
     $SPT=$header_id =='-1'?'SPX_SPAREPART_USE_HEADER_ADD':'SPX_SPAREPART_USE_HEADER_EDIT';

     $xmldoc =  XMLbasic('json');  
     AddField($xmldoc ,'id_header',$header_id );
     AddField($xmldoc ,'no_dokumen',$no_dokumen );
     AddField($xmldoc ,'tanggal',$tanggal );
     AddField($xmldoc ,'no_shift',$no_shift );
     AddField($xmldoc ,'kode_mesin',$kode_mesin ); 
     AddField($xmldoc ,'nama_mesin',$nama_mesin ); 
     AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));
     $str = $xmldoc->saveXML();
     // print_r($str);die();
      exec_sp_array($xmldoc, $SPT,$sts,$des,$rows,$detail); 
     echo json_encode(array('valid'=>($sts=='00'?true:false)
     ,'stsno' => $sts
     , 'stsmsg' => $des
     , 'detail' => $detail)
     ); 
}


function request_sparepart_to_gudang(){ 
	$id_detail = isset($_POST['id_detail'])?$_POST['id_detail']:die('pilih dokumen dahulu');; // get the requested page 
	$qty_request = isset($_POST['qty_request'])?$_POST['qty_request']:''; // get the requested page 
	$satuan = isset($_POST['satuan'])?$_POST['satuan']:''; // get the requested page 
	$xmldoc =  XMLbasic('json'); 
	AddField($xmldoc ,'id_detail',$id_detail);
	AddField($xmldoc ,'qty_request',$qty_request );
	AddField($xmldoc ,'satuan',$satuan );
	AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));
	AddField($xmldoc,'modetrx','sparepart');
	$str = $xmldoc->saveXML();
	 // print_r($str);die();
	 exec_sp_array($xmldoc, 'dbo.SPX_REQUEST_SPAREPART_TO_GUDANG',$sts,$des,$rows,$detail); 
	echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
}


function save_sparepart_use_approve(){ 
	$id_header = isset($_POST['id_header'])?$_POST['id_header']:die('pilih dokumen dahulu');; // get the requested page 
	$petugas = isset($_POST['petugas'])?$_POST['petugas']:''; // get the requested page 
 
	$xmldoc =  XMLbasic('json'); 
	AddField($xmldoc ,'id_header',$id_header);
	AddField($xmldoc ,'petugas',$petugas );
	AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));
	AddField($xmldoc,'modetrx','sparepart');
	$str = $xmldoc->saveXML();
	 // print_r($str);die();
	 exec_sp_array($xmldoc, 'dbo.SPX_SPAREPART_USE_APPROVE',$sts,$des,$rows,$detail); 
	echo json_encode(array('valid'=>($sts=='00'?true:false),'stsno' => $sts, 'stsmsg' => $des)); 
}


function save_detail_sparepart_use(){   
 $header_id      = isset($_POST['header_id'])?$_POST['header_id']:'-1'; // get the requested page 
 $detail_id      = isset($_POST['detail_id'])?$_POST['detail_id']:'-1'; // get the requested page 
 $kode_sparepart = isset($_POST['kode_sparepart'])?$_POST['kode_sparepart']:'-1'; // get the requested page 
 $nama_sparepart = isset($_POST['nama_sparepart'])?$_POST['nama_sparepart']:''; // get the requested page 
 $jumlah         = isset($_POST['qty'])?$_POST['qty']:''; // get the requested page 
 $satuan         = isset($_POST['satuan'])?$_POST['satuan']:''; // get the requested page 
 $no_dokumen     = isset($_POST['no_dokumen'])?$_POST['no_dokumen']:''; // get the requested page 
 $item_no        = isset($_POST['item_no'])?$_POST['item_no']:''; // get the requested page 
 
 $SPT=$detail_id =='-1'?'dbo.SPX_SPAREPART_USE_DETAIL_ADD':'dbo.SPX_SPAREPART_USE_DETAIL_EDIT';
 
 $xmldoc =  XMLbasic('json');  
 AddField($xmldoc ,'header_id',$header_id );
 AddField($xmldoc ,'detail_id',$detail_id );
 AddField($xmldoc ,'kode_sparepart',$kode_sparepart );
 AddField($xmldoc ,'nama_sparepart',$nama_sparepart );
 AddField($xmldoc ,'jumlah',$jumlah );
 AddField($xmldoc ,'satuan',$satuan ); 
 AddField($xmldoc ,'no_dokumen',$no_dokumen ); 
 AddField($xmldoc ,'item_no',$item_no ); 
 AddField($xmldoc,'data_by',$this->session->userdata('DX_user_id'));    
 
 $str = $xmldoc->saveXML();
 // print_r($str);die();
 exec_sp_array($xmldoc, $SPT ,$sts,$des,$rows,$detail); 
 echo json_encode(array('valid'=>($sts=='00'?true:false)
 ,'stsno' => $sts
 , 'stsmsg' => $des
 , 'detail' => $detail)
 ); 
}


    }  