<?php
class CT_reportcustomspemasukanbarang 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');
        } 
  }

  function index(){
 //   $data['query'] = $this->incomingitemAll->getAll();
    //$this->load->view('daily/input',$data);
	
	$this->load->view('reports/view_reportcustomspemasukanbarang');
  }


  function loadDataGrid(){
      
        $page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
        $limit = isset($_POST['rows'])?$_POST['rows']:10; // get how many rows we want to have into the grid
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'rID'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction
        if ($sord == 'asc') { $sord = '0'; } else if ($sord == 'desc') { $sord = '1'; }
       
        $xmldata = XMLbasic('dbo.MG_PERIODE_FOR_CMB');    
        
    
        paramView($xmldata, $limit, $page,  $sidx, $sord);
        
        //$data = connectwebservice($xmldata->saveXML());
        connectwebservice($xmldata->saveXML(), '',$sts,$des,$rows,$detail);
       
        
        $sordby = $detail['sort']; 
        $sordtype = $detail['desc'];
        $record = $detail['rowspage']; 
        $totalrow = $detail['rows']; 
        $totalpages = $detail['pages']; 
          

        
        $responce->page = $page;
        $responce->total = $totalpages;
        $responce->records = $totalrow ; 
                
        
        $i=0;
        foreach ($rows as $per_row) {
        
            $responce->rows[$i]['id']=$per_row->getElementsByTagName('rID')->item(0)->nodeValue;
            $responce->rows[$i]['cell']=array($per_row->getElementsByTagName('rID')->item(0)->nodeValue,
                                              $per_row->getElementsByTagName('rn')->item(0)->nodeValue,
                                              $per_row->getElementsByTagName('r1')->item(0)->nodeValue, 
                                              $per_row->getElementsByTagName('r2')->item(0)->nodeValue);
            $i++;
            
            
        }
        
        echo json_encode($responce);
      
      /*
		$response='{"page":"1","total":2,"records":"7","rows":[
        {"id":"1","cell":["1","BC23","328032","6/10/2011","500894","6/10/2011","KUBOTA CORPORATION","K00011","3023-61922-00- SFVD CONTROL STICKER","BOX","100","204402.15"]},
		{"id":"2","cell":["2","BC23","328033","7/10/2011","500895","7/10/2011","SANYO DENKI PHILIPPINES,INC","K00012","3023-61357-01-00 SOLID COLOR CARD SEAL","BOX","120","87416.69"]},
		{"id":"3","cell":["3","BC23","328034","8/10/2011","500896","8/10/2011","S.I. ELECTRONICS CO. LTD","K00013","3023-65760-00- LABEL (INLET GATE CAUTION PET)","PCS","220","3065.76"]},
		{"id":"4","cell":["4","BC23","328035","9/10/2011","500897","9/10/2011","SJHI (XIAMEN)CO. LTD","K00014","LABEL(REMOTE CONTROL BKT PLACE)","BOX","100","6665.76"]},
		{"id":"5","cell":["5","BC23","328036","10/10/2011","500898","10/10/2011","SUNSTAR TECHNOLOGY TRADING LIMITED","K00124","SCREW SPRING (SELL OUT K)","CTN","100","8665.76"]},
		{"id":"6","cell":["6","BC23","328037","11/10/2011","500899","11/10/2011","WHIRLPOOL S.A.","K00016","3033-62076-03- SHAFT (LINK KS)","PCS","100","610555.76"]},
		{"id":"7","cell":["7","BC23","328038","12/10/2011","500900","12/10/2011","SMART EXCEL LIMITED","K00017","TRIM MARERIAL(AB INNER BOX1000)","CTN","100","566785.76"]}]}';
        
		echo $response;
        //echo json_encode($responce);
        */
  }
  
   function loadDataGridView(){
        $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']:10; // get how many rows we want to have into the grid
        $sidx           = isset($_POST['sidx'])?$_POST['sidx']:'rID'; // get index row - i.e. user click to sort
        $sord           = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction
        $periode_id     = isset($_POST['periode_id'])?$_POST['periode_id']:die('error');
        if ($sord == 'asc') { $sord = '0'; } else if ($sord == 'desc') { $sord = '1'; }
       
        $xmldata = XMLbasic('dbo.RPT_BARANG_PER_DOKUMENT_IN');    
        AddField($xmldata,'periode_id',$periode_id);
    
        paramView($xmldata, $limit, $page,  $sidx, $sord); 
        $xsearch = (isset($_POST['xsearch']) || !empty($_POST['xsearch']))?$_POST['xsearch']:'';
        $jenisdokumenrcpb   = (isset($_POST['jenisdokumenrcpb']) or !empty($_POST['jenisdokumenrcpb']))? $_POST['jenisdokumenrcpb'] :'';
        $nomorpabeanrcpb    = (isset($_POST['nomorpabeanrcpb']) or !empty($_POST['nomorpabeanrcpb']))? $_POST['nomorpabeanrcpb']:'';
        $nobuktircpb        = (isset($_POST['nobuktircpb']) or !empty($_POST['nobuktircpb']))?$_POST['nobuktircpb']:'';
        $pembelircpb        = (isset($_POST['pembelircpb']) or !empty($_POST['pembelircpb']))?$_POST['pembelircpb']:'';
        $kodebarangrcpb     = (isset($_POST['kodebarangrcpb']) or !empty($_POST['kodebarangrcpb']))?$_POST['kodebarangrcpb']:'';
        $namabarangrcpb     = (isset($_POST['namabarangrcpb']) or !empty($_POST['namabarangrcpb']))?$_POST['namabarangrcpb']:'';
        $satuanrcpb         = (isset($_POST['satuanrcpb']) or !empty($_POST['satuanrcpb']))?$_POST['satuanrcpb']:'';
        $tglpabeanrcpb_1    = (isset($_POST['tglpabeanrcpb_1']) or !empty($_POST['tglpabeanrcpb_1']))?$_POST['tglpabeanrcpb_1']:'';
        $tglpabeanrcpb_2    = (isset($_POST['tglpabeanrcpb_2']) or !empty($_POST['tglpabeanrcpb_2']))?$_POST['tglpabeanrcpb_2']:'';
        $tglbuktircpb_1     = (isset($_POST['tglbuktircpb_1']) or !empty($_POST['tglbuktircpb_1']))?$_POST['tglbuktircpb_1']:'';
        $tglbuktircpb_2     = (isset($_POST['tglbuktircpb_2']) or !empty($_POST['tglbuktircpb_2']))?$_POST['tglbuktircpb_2']:'';
        
         
        if ($xsearch) {
            if($jenisdokumenrcpb != '') addSearchText($xmldata,'r1','1','1',$jenisdokumenrcpb);
            if($nomorpabeanrcpb != '') addSearchText($xmldata,'r2','1','1',$nomorpabeanrcpb);
            
            if($nobuktircpb != '') addSearchText($xmldata,'r4','1','1',$nobuktircpb);
            if($pembelircpb != '') addSearchText($xmldata,'r6','1','1',$pembelircpb);
            if($kodebarangrcpb != '') addSearchText($xmldata,'r7','1','1',$kodebarangrcpb); 
            if($namabarangrcpb != '') addSearchText($xmldata,'r8','1','1',$namabarangrcpb);
            if($satuanrcpb != '') addSearchText($xmldata,'r9','1','1',$satuanrcpb);
            if($tglpabeanrcpb_1 != '' && $tglpabeanrcpb_2 == '') { 
                $tglpabeanrcpb_1 = explode( '/', $tglpabeanrcpb_1);
                $tglpabeanrcpb_1 = $tglpabeanrcpb_1[2].$tglpabeanrcpb_1[0].$tglpabeanrcpb_1[1];
                addSearchDate($xmldata,'r3',$tglpabeanrcpb_1,'','1');
            } else if($tglpabeanrcpb_2 != '' && $tglpabeanrcpb_1 == '') {
                $tglpabeanrcpb_2 = explode( '/', $tglpabeanrcpb_2);
                $tglpabeanrcpb_2 = $tglpabeanrcpb_2[2].$tglpabeanrcpb_2[0].$tglpabeanrcpb_2[1];
                addSearchDate($xmldata,'r3',$tglpabeanrcpb_2,'','1');
            } else if ($tglpabeanrcpb_2 != '' && $tglpabeanrcpb_1 != ''){
                $tglpabeanrcpb_1 = explode( '/', $tglpabeanrcpb_1);
                $tglpabeanrcpb_1 = $tglpabeanrcpb_1[2].$tglpabeanrcpb_1[0].$tglpabeanrcpb_1[1];
                $tglpabeanrcpb_2 = explode( '/', $tglpabeanrcpb_2);
                $tglpabeanrcpb_2 = $tglpabeanrcpb_2[2].$tglpabeanrcpb_2[0].$tglpabeanrcpb_2[1];
                addSearchDate($xmldata,'r3',$tglpabeanrcpb_1,$tglpabeanrcpb_2,'4');
            }
             
            
            if($tglbuktircpb_1 != '') addSearchText($xmldata,'r1','1','1',$tglbuktircpb_1);
            if($tglbuktircpb_2 != '') addSearchText($xmldata,'r1','1','1',$tglbuktircpb_2);
        }
        
        //$data = connectwebservice($xmldata->saveXML());
        connectwebservice($xmldata->saveXML(), '',$sts,$des,$rows,$detail);
       
        
        $sordby = $detail['sort']; 
        $sordtype = $detail['desc'];
        $record = $detail['rowspage']; 
        $totalrow = $detail['rows']; 
        $totalpages = $detail['pages']; 
          

        
        $responce->page = $page;
        $responce->total = $totalpages;
        $responce->records = $totalrow ; 
                
        
        $i=0;
        foreach ($rows as $per_row) {
            
            $responce->rows[$i]['rownumber']=$per_row->getElementsByTagName('rn')->item(0)->nodeValue;
            $responce->rows[$i]['rID']=$per_row->getElementsByTagName('rID')->item(0)->nodeValue;
            $responce->rows[$i]['jenisdokumenrcpb']=$per_row->getElementsByTagName('r1')->item(0)->nodeValue;
            $responce->rows[$i]['nomorpabeanrcpb']=$per_row->getElementsByTagName('r2')->item(0)->nodeValue;
            $responce->rows[$i]['tglpabeanrcpb']=$per_row->getElementsByTagName('r3')->item(0)->nodeValue;
            $responce->rows[$i]['nobuktircpb']=$per_row->getElementsByTagName('r4')->item(0)->nodeValue;
            $responce->rows[$i]['tglbuktircpb']=$per_row->getElementsByTagName('r5')->item(0)->nodeValue;
            $responce->rows[$i]['pembelircpb']=$per_row->getElementsByTagName('r6')->item(0)->nodeValue;
            $responce->rows[$i]['kodebarangrcpb']=$per_row->getElementsByTagName('r7')->item(0)->nodeValue;
            $responce->rows[$i]['namabarangrcpb']=$per_row->getElementsByTagName('r8')->item(0)->nodeValue;
            $responce->rows[$i]['satuanrcpb']=$per_row->getElementsByTagName('r9')->item(0)->nodeValue;
            
            
            $i++;
            
            
        }
        
        echo json_encode($responce);
      
      /*
        $response='{"page":"1","total":2,"records":"7","rows":[
        {"id":"1","cell":["1","BC23","328032","6/10/2011","500894","6/10/2011","KUBOTA CORPORATION","K00011","3023-61922-00- SFVD CONTROL STICKER","BOX","100","204402.15"]},
        {"id":"2","cell":["2","BC23","328033","7/10/2011","500895","7/10/2011","SANYO DENKI PHILIPPINES,INC","K00012","3023-61357-01-00 SOLID COLOR CARD SEAL","BOX","120","87416.69"]},
        {"id":"3","cell":["3","BC23","328034","8/10/2011","500896","8/10/2011","S.I. ELECTRONICS CO. LTD","K00013","3023-65760-00- LABEL (INLET GATE CAUTION PET)","PCS","220","3065.76"]},
        {"id":"4","cell":["4","BC23","328035","9/10/2011","500897","9/10/2011","SJHI (XIAMEN)CO. LTD","K00014","LABEL(REMOTE CONTROL BKT PLACE)","BOX","100","6665.76"]},
        {"id":"5","cell":["5","BC23","328036","10/10/2011","500898","10/10/2011","SUNSTAR TECHNOLOGY TRADING LIMITED","K00124","SCREW SPRING (SELL OUT K)","CTN","100","8665.76"]},
        {"id":"6","cell":["6","BC23","328037","11/10/2011","500899","11/10/2011","WHIRLPOOL S.A.","K00016","3033-62076-03- SHAFT (LINK KS)","PCS","100","610555.76"]},
        {"id":"7","cell":["7","BC23","328038","12/10/2011","500900","12/10/2011","SMART EXCEL LIMITED","K00017","TRIM MARERIAL(AB INNER BOX1000)","CTN","100","566785.76"]}]}';
        
        echo $response;
        //echo json_encode($responce);
        */
  }
  
        function print_rpt (){
        
        $periode_id     = isset($_GET['periode_id'])?$_GET['periode_id']:'';
        RPT_BARANG_PER_DOKUMENT_IN_EXPORT_TO($periode_id, 'pdf', $namafile, $isifile);
 
        $this->load->helper('download'); 
        force_download($namafile, $isifile);
                             
                
                     
         
                        
                
                        
                     
            
        }

  

  

}

