<?php
class CT_stock_out_rm 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_inventory/mdl_inventory", 'pasi_mdl_inventory');
		$this->load->model("pasi_upload/mdl_upload", 'pasi_mdl_upload');

	}
	
	function index(){
	
		$this->load->view('pasi_inventory/view_stock_out_rm');
	
	}

	function view_stockout_print(){
	
		$this->load->view('pasi_inventory/view_stock_out_print');
		
	}

	function view_stockout_jasper(){
	
		$begin_date = (isset($_POST['begin_date']) && !empty($_POST['begin_date']))?$_POST['begin_date']:die('{"sts":"ERROR","desc":" Parameter nip Tidak Ditemukan"}');
		$end_date = (isset($_POST['end_date']) && !empty($_POST['end_date']))?$_POST['end_date']:die('{"sts":"ERROR","desc":" Parameter nip Tidak Ditemukan"}');
		
		$data["begin_date"] = $begin_date;
		$data["end_date"] = $end_date;
		
		$this->load->view('pasi_inventory/view_stock_out_jasper',$data);
		
	}
	
	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_adjusment'; // 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 == "kode_barang" ) {
						$searchString = $k->data;
						$where .= " and kode_barang like  '%".$searchString."%'"; 	
					} else if ( $k->field == "nama_barang" ) {
						$searchString = $k->data;
						$where .= " and nama_barang like  '%".$searchString."%'";	
					}else if ( $k->field == "tanggal_pengeluaran" ) {
						$searchString = $k->data;
						$where .= " and tanggal_pengeluaran like  '%".$searchString."%'";	
					}
				}
			}
		}
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->pasi_mdl_inventory->stockoutbb_All($limit,$start,$sidx,$sord, $where);
		$count = $this->pasi_mdl_inventory->stockoutbb_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_stock_out_bb']=$r->id_stock_out_bb; 
			$responce->rows[$i]['kode_barang']=$r->kode_barang; 
			$responce->rows[$i]['nama_barang']=$r->nama_barang;
			$responce->rows[$i]['satuan_barang']=$r->satuan_barang;
			$responce->rows[$i]['pengeluaran']=$r->pengeluaran;
			$responce->rows[$i]['tanggal_pengeluaran']=$r->tanggal_pengeluaran;
			$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 pdf_reportstockout_jasper(){

		
		$begin_date = (isset($_GET['begin_date']) && !empty($_GET['begin_date']))?$_GET['begin_date']:die('{"sts":"ERROR","desc":"nip Tidak Ditemukan"}');
		$end_date = (isset($_GET['end_date']) && !empty($_GET['end_date']))?$_GET['end_date']:die('{"sts":"ERROR","desc":" end_date Param Tidak Ditemukan"}');
		
		$this->load->library('java_bridge');
		$cekjava=$this->java_bridge->checkJavaExtension(); 
		$bcreportdir = $this->config->item('APP_dirreport');
		
		
		if($cekjava){
			$irep = $bcreportdir.'\rptOutgoingRawMaterial.jrxml';
			$compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
			
			$report = $compileManager->compileReport($irep);
			$fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
			
			$params = new Java("java.util.HashMap");
			$params->put("begin_date",  $begin_date);
			$params->put("end_date",  $end_date);
			//$params->put("prmperiode", $periodetgawal . ' S/D' . $periodetgakhir );
			//$params->put("nama_perusahaan", "PT. AUTOCOMP SYSTEMS INDONESIA");
			
			$class = new JavaClass("java.lang.Class");
			$class->forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			$driverManager = new JavaClass("java.sql.DriverManager");
			
			//db username and password
			$conn = $driverManager->getConnection("jdbc:sqlserver://teana:1433;databaseName=db_pasi_new", "sa", "P4ssword");
			$jasperPrint = $fillManager->fillReport($report, $params, $conn);
			$outputPath = realpath(".")."\ireport/"."pdfbarangjadi.pdf";
			
				$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
				$exportManager->exportReportToPdfFile($jasperPrint, $outputPath);

			header("Content-type: application/pdf");
			
			readfile($outputPath);
			
			@unlink($outputPath);
		}	
	} 
 }