<?php
class CT_reportcustoms 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->library("fpdf"); 
		$this->load->model("reports/mdl_reports", 'mdl_reports');	
  }

	function index(){
 //   $data['query'] = $this->incomingitemAll->getAll();
    //$this->load->view('daily/input',$data);
	
		$this->load->view('reports/view_reportcustoms');
	}
  
	function view_laporanpemasukanbarangperdokumen(){
		$data['tanggal'] = date('d/m/Y');
		
		$this->load->view('reports/view_reportcustomspemasukanbarang', $data);

	}
	
	function data_laporanpemasukanbarangperdokumen(){
  
		$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']:'tgl_receive'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'0'; // get the direction	
		
		$caritanggal = (isset($_POST['caritanggal']) && !empty($_POST['caritanggal']))?$_POST['caritanggal']:'';
		$tglawallaporanpemasukanbarang = (isset($_POST['tglawallaporanpemasukanbarang']) && !empty($_POST['tglawallaporanpemasukanbarang']))?$_POST['tglawallaporanpemasukanbarang']:Date('d/m/Y');
		$tglakhirlaporanpemasukanbarang = (isset($_POST['tglakhirlaporanpemasukanbarang']) && !empty($_POST['tglakhirlaporanpemasukanbarang']))?$_POST['tglakhirlaporanpemasukanbarang']:Date('d/m/Y');
		$jns_dok = (isset($_POST['jns_dok']) && !empty($_POST['jns_dok']))?$_POST['jns_dok']:'-';
	    $where='';
	 
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_reports->datareportpemasukanbarangperdokumen_All($tglawallaporanpemasukanbarang,$tglakhirlaporanpemasukanbarang,$jns_dok,$limit,$start,$sidx,$sord, $page);
        $count = $this->mdl_reports->datareportpemasukanbarangperdokumen_CountAll($tglawallaporanpemasukanbarang,$tglakhirlaporanpemasukanbarang,$jns_dok);
		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){
			$kodebrg = $r['kode_brg'];
			if ($kodebrg == ''){
				$kodebrg = '-';
			}
			
			$responce->rows[$i]['nomorurut']=$r['row']; 
			$responce->rows[$i]['id_data_in_hdr']=$r['id_data_in_hdr']; 
			$responce->rows[$i]['doc_in_no']=$r['no_receive']; 
			$responce->rows[$i]['doc_in_date']=$r['tgl_receive']; 
			$responce->rows[$i]['BC23No']=$r['no_daftar']; 
			$responce->rows[$i]['BC23Tg']=$r['tgl_nodaf']; 
			$responce->rows[$i]['PasokNama']=$r['pemasok']; 
			$responce->rows[$i]['part_name_customs']=$r['nama_brg']; 
			$responce->rows[$i]['sumqty']=$r['jumlah']; 
			$responce->rows[$i]['unit_in']=$r['satuan']; 
			$responce->rows[$i]['doc_in_from_name']=$r['doc_in_from_name']; 
			$responce->rows[$i]['kode_barang']=$kodebrg; 
			$responce->rows[$i]['nilaibarang']=$r['nilai_barang']; 
			$i++;
			 
		}
		echo json_encode($responce);
		
	}
	
	function printlaporanicomingexcel(){
		include_once ( APPPATH."libraries/PHPExcel.php");	
		include_once ( APPPATH."libraries/PHPExcel/IOFactory.php");	
		$phpExcel = new PHPExcel();
		$objTpl = PHPExcel_IOFactory::load(APPPATH."libraries/PHPExcel/templates/pemi_report_pemasukan_barang.xls");	
		
		
		$caritanggal = (isset($_GET['caritanggal']) && !empty($_GET['caritanggal']))?$_GET['caritanggal']:'';
		$tglawallaporanpemasukanbarang = (isset($_GET['tglawallaporanpemasukanbarang']) && !empty($_GET['tglawallaporanpemasukanbarang']))?$_GET['tglawallaporanpemasukanbarang']:Date('d/m/Y');
		$tglakhirlaporanpemasukanbarang = (isset($_GET['tglakhirlaporanpemasukanbarang']) && !empty($_GET['tglakhirlaporanpemasukanbarang']))?$_GET['tglakhirlaporanpemasukanbarang']:Date('d/m/Y');
		$jns_dok = (isset($_GET['jns_dok']) && !empty($_GET['jns_dok']))?$_GET['jns_dok']:'-';
				
		$tglawalr =  $tglawallaporanpemasukanbarang;	
		$tglakhirr = $tglakhirlaporanpemasukanbarang;	
		
		
		$tglawalbb = explode("/", $tglawalr);	
		$tglakhirbb = explode("/", $tglakhirr);	
		
		
		$tgawal =  $tglawalbb[1].'/'.$tglawalbb[0].'/'.$tglawalbb[2];
		$tgakhir = $tglakhirbb[1].'/'.$tglakhirbb[0].'/'.$tglakhirbb[2];
		
		$this->load->helper('afunction_helper');
		
		$periodetgawal = $tglawalbb[0].' - '.fbulan($tglawalbb[1]).' - '.$tglawalbb[2];
		$periodetgakhir = $tglakhirbb[0].' - '.fbulan($tglakhirbb[1]).' - '.$tglakhirbb[2];
		$periode = $periodetgawal . ' S/D ' . $periodetgakhir ;
		
		
		//$where1 = " 1 = 1  and (dbo.tbl_bc_in_hdr.[BC23No] <> '' or dbo.tbl_bc_in_hdr.[BC23No] is not null)  ";
		$where = "";
		
		// if  ($caritanggal=='ya'){
			// if ($jns_dok == '-'){	
				// $where1 .= " and  tbl_data_in_hdr.doc_in_from_id IN (2,3,4,5) and (doc_in_date between convert(date,'$tglawallaporanpemasukanbarang',103) and convert(date,'$tglakhirlaporanpemasukanbarang', 103)) ";	
				// $where = " doc_in_date between convert(date,'$tglawallaporanpemasukanbarang',103) and convert(date,'$tglakhirlaporanpemasukanbarang', 103) ";
			// }  else {
				// $where1 .= " and  tbl_data_in_hdr.doc_in_from_id IN (2,3,4,5) and (doc_in_date between convert(date,'$tglawallaporanpemasukanbarang',103) and convert(date,'$tglakhirlaporanpemasukanbarang', 103)) and doc_in_from_name = '$jns_dok' ";	
				// $where = " doc_in_date between convert(date,'$tglawallaporanpemasukanbarang',103) and convert(date,'$tglakhirlaporanpemasukanbarang', 103) and doc_in_from_name = '$jns_dok' ";
			// }	
		// }
		
		$records = $this->mdl_reports->datareportpemasukanbarangperdokumen_excel($tglawallaporanpemasukanbarang,$tglakhirlaporanpemasukanbarang,$where);
			
		$objTpl->setActiveSheetIndex(0);  //set first sheet as active
		$objTpl->getActiveSheet()->setCellValue("C4", ":     " . $periode);
		
		//var_dump($records);
		$i = 1;
		$j = '';
		$k = 7;
		$l = 0;
		$m = '';
		$n = ''; 
		$o = 7;
		
		foreach ($records as $r ){
			$k = 7 + $i;
			$j = "$k";
			$kodebrg = $r['kode_brg'];
			if ($kodebrg == ''){
				$kodebrg = "-";
			}
			$objTpl->getActiveSheet()->setCellValue("A".$j, $i);
			$objTpl->getActiveSheet()->setCellValue("B".$j, $r['doc_in_from_name']);
			$objTpl->getActiveSheet()->setCellValueExplicit("C".$j, $r['no_daftar'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValue("D".$j, $r['tgl_nodaf']);
			$objTpl->getActiveSheet()->setCellValueExplicit("E".$j, $r['no_receive'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValue("F".$j, $r['tgl_receive']);
			$objTpl->getActiveSheet()->setCellValueExplicit("G".$j, $r['pemasok'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValueExplicit("H".$j, $kodebrg, PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValueExplicit("I".$j, $r['nama_brg'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValueExplicit("J".$j, $r['satuan'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValue("K".$j, $r['jumlah']);
			$objTpl->getActiveSheet()->setCellValue("L".$j, $r['nilai_barang']);
			$i++;
		} 
			 
		$objTpl->getActiveSheet()->getStyle("L7:"."L".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$datenow = date('Ymd');
		$filename =  "REPORT_". "4BULAN_PEMI_PEMASUKANDOK_"  . $datenow .".xls";
		header("Content-type:application/vnd.ms-excel");
		header("Content-disposition:attachment; filename=$filename"); 
		header("Pragma:no-cache");
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		header("Expires:0");

		$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
		$objWriter->save('php://output');  //send it to user, of course you can save it to disk also!
		 
		exit; //done.. exiting!
	
	}
	
	 
	
	function pdf_reportcustomspemasukanbarang_jasper(){
	
		$tglawallaporanpemasukanbarang = (isset($_GET['tglawallaporanpemasukanbarang']) && !empty($_GET['tglawallaporanpemasukanbarang']))?$_GET['tglawallaporanpemasukanbarang']:die('{"sts":"ERROR","desc":" Tanggal Start Param Tidak Ditemukan"}');
		$tglakhirlaporanpemasukanbarang = (isset($_GET['tglakhirlaporanpemasukanbarang']) && !empty($_GET['tglakhirlaporanpemasukanbarang']))?$_GET['tglakhirlaporanpemasukanbarang']:die('{"sts":"ERROR","desc":" Tanggal Start Param Tidak Ditemukan"}');
		$jns_dok = (isset($_GET['jns_dok']) && !empty($_GET['jns_dok']))?$_GET['jns_dok']:die('{"sts":"ERROR","desc":" Tanggal Start Param Tidak Ditemukan"}');
		
	
		$tglawallaporanpemasukanbarang = explode("/", $tglawallaporanpemasukanbarang);	
		$tglakhirlaporanpemasukanbarang = explode("/", $tglakhirlaporanpemasukanbarang);	
		
		
		$tgawal =  $tglawallaporanpemasukanbarang[1].'/'.$tglawallaporanpemasukanbarang[0].'/'.$tglawallaporanpemasukanbarang[2];
		$tgakhir = $tglakhirlaporanpemasukanbarang[1].'/'.$tglakhirlaporanpemasukanbarang[0].'/'.$tglakhirlaporanpemasukanbarang[2];
		
		$this->load->helper('afunction_helper');
		
		$periodetgawal = $tglawallaporanpemasukanbarang[0].' - '.fbulan($tglawallaporanpemasukanbarang[1]).' - '.$tglawallaporanpemasukanbarang[2];
		$periodetgakhir = $tglakhirlaporanpemasukanbarang[0].' - '.fbulan($tglakhirlaporanpemasukanbarang[1]).' - '.$tglakhirlaporanpemasukanbarang[2];
		
		
		
		
		$data['periodetgawal '] = $periodetgawal;
		$data['periodetgakhir '] = $periodetgakhir;
		
		$this->load->library('java_bridge');
		$cekjava=$this->java_bridge->checkJavaExtension(); 
		$bcreportdir = $this->config->item('APP_dirreport');
		
		if($cekjava){
			$irep = $bcreportdir.'\rpt_incoming.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("inputdata1", "This is a test string");
			$params->put("tglawal",  $tgawal);
			$params->put("tglakhir",  $tgakhir);
			$params->put("prmperiode", $periodetgawal . ' S/D' . $periodetgakhir );
			$params->put("nama_perusahaan", "PT. EDS MANUFACTURING INDONESIA");
			//if ($jns_dok == '-'){
				$params->put("jns_dok", " id_data_in_hdr <> 0 ");
			//}
			
			//$params->put("number", 3.00);
			
			
			
			$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($jdburl, $db['default']['username'], $db['default']['password']);
			
			$jasperPrint = $fillManager->fillReport($report, $params, $conn);
			$outputPath = realpath(".")."\ireport/"."pdfpenerimaanbrg.pdf";
			
				$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
				$exportManager->exportReportToPdfFile($jasperPrint, $outputPath);

			header("Content-type: application/pdf");
			
			readfile($outputPath);
			
			@unlink($outputPath);
		}
		
	}
	
	function pdf_reportcustomspemasukanbarang_jasper_xls(){
	
		$tglawallaporanpemasukanbarang = (isset($_GET['tglawallaporanpemasukanbarang']) && !empty($_GET['tglawallaporanpemasukanbarang']))?$_GET['tglawallaporanpemasukanbarang']:die('{"sts":"ERROR","desc":" Tanggal Start Param Tidak Ditemukan"}');
		$tglakhirlaporanpemasukanbarang = (isset($_GET['tglakhirlaporanpemasukanbarang']) && !empty($_GET['tglakhirlaporanpemasukanbarang']))?$_GET['tglakhirlaporanpemasukanbarang']:die('{"sts":"ERROR","desc":" Tanggal Start Param Tidak Ditemukan"}');
		
			
		$tglawallaporanpemasukanbarang = explode("/", $tglawallaporanpemasukanbarang);	
		$tglakhirlaporanpemasukanbarang = explode("/", $tglakhirlaporanpemasukanbarang);	
		
		
		$tgawal =  $tglawallaporanpemasukanbarang[1].'/'.$tglawallaporanpemasukanbarang[0].'/'.$tglawallaporanpemasukanbarang[2];
		$tgakhir = $tglakhirlaporanpemasukanbarang[1].'/'.$tglakhirlaporanpemasukanbarang[0].'/'.$tglakhirlaporanpemasukanbarang[2];
		
		$this->load->helper('afunction_helper');
		
		$periodetgawal = $tglawallaporanpemasukanbarang[0].' - '.fbulan($tglawallaporanpemasukanbarang[1]).' - '.$tglawallaporanpemasukanbarang[2];
		$periodetgakhir = $tglakhirlaporanpemasukanbarang[0].' - '.fbulan($tglakhirlaporanpemasukanbarang[1]).' - '.$tglakhirlaporanpemasukanbarang[2];
		
		
		
		
		$data['periodetgawal '] = $periodetgawal;
		$data['periodetgakhir '] = $periodetgakhir;
		
		$this->load->library('java_bridge');
		$cekjava=$this->java_bridge->checkJavaExtension(); 
		$bcreportdir = $this->config->item('APP_dirreport');
		
		if($cekjava){
			$irep = $bcreportdir.'\rpt_incoming.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("inputdata1", "This is a test string");
			$params->put("tglawal",  $tgawal);
			$params->put("tglakhir",  $tgakhir);
			$params->put("prmperiode", $periodetgawal . ' S/D' . $periodetgakhir );
			$params->put("nama_perusahaan", "PT. EDS MANUFACTURING INDONESIA");
			
			//$params->put("number", 3.00);
			
			
			
			$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($jdburl, $db['default']['username'], $db['default']['password']);
			
			$jasperPrint = $fillManager->fillReport($report, $params, $conn);
			$outputPath = realpath(".")."\ireport"."\xlspenerimaanbrg.xls";
			
			$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
			
			//$exportManager->exportReportToPdfFile($jasperPrint, $outputPath);
			try {
				$exporter = new java("net.sf.jasperreports.engine.export.JRXlsExporter");
				$exporter->setParameter(java("net.sf.jasperreports.engine.export.JRXlsExporterParameter")->IS_ONE_PAGE_PER_SHEET, java("java.lang.Boolean")->TRUE);
				$exporter->setParameter(java("net.sf.jasperreports.engine.export.JRXlsExporterParameter")->IS_WHITE_PAGE_BACKGROUND, java("java.lang.Boolean")->FALSE);
				$exporter->setParameter(java("net.sf.jasperreports.engine.export.JRXlsExporterParameter")->IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, java("java.lang.Boolean")->TRUE);
				$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
				$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
			} catch (JavaException $ex) {
				echo $ex;
			}
			
			header("Content-type: application/vnd.ms-excel");
			header("Content-Disposition: attachment; filename=xlspenerimaanbrg.xls");
			readfile($outputPath);
            //unlink($outputPath);		
			//@unlink($outputPath);
		}
		
	}
	function view_reportcustomspemasukanbarang_jasper(){
	
		$tglawallaporanpemasukanbarang = (isset($_POST['tglawallaporanpemasukanbarang']) && !empty($_POST['tglawallaporanpemasukanbarang']))?$_POST['tglawallaporanpemasukanbarang']:die('{"sts":"ERROR","desc":" Tanggal Start Param Tidak Ditemukan"}');
		$tglakhirlaporanpemasukanbarang = (isset($_POST['tglakhirlaporanpemasukanbarang']) && !empty($_POST['tglakhirlaporanpemasukanbarang']))?$_POST['tglakhirlaporanpemasukanbarang']:die('{"sts":"ERROR","desc":" Tanggal Start Param Tidak Ditemukan"}');
		$jns_dok = (isset($_POST['jns_dok']) && !empty($_POST['jns_dok']))?$_POST['jns_dok']:die('{"sts":"ERROR","desc":" Tanggal Start Param Tidak Ditemukan"}');
		
		$data['tglawallaporanpemasukanbarang'] = $tglawallaporanpemasukanbarang;
		$data['tglakhirlaporanpemasukanbarang'] = $tglakhirlaporanpemasukanbarang;
		$data['jns_dok'] = $jns_dok;
		$this->load->view('reports/view_reportcustomspemasukanbarang_jasper', $data);
	}
	
	
	function printpdfincoming(){
		$tglawallaporanpemasukanbarang = (isset($_POST['tglawallaporanpemasukanbarang']) && !empty($_POST['tglawallaporanpemasukanbarang']))?$_POST['tglawallaporanpemasukanbarang']:'';
		$tglakhirlaporanpemasukanbarang = (isset($_POST['tglakhirlaporanpemasukanbarang']) && !empty($_POST['tglakhirlaporanpemasukanbarang']))?$_POST['tglakhirlaporanpemasukanbarang']:'';
		
		$tglawallaporanpemasukanbarang = explode("/", $tglawallaporanpemasukanbarang);	
		$tglakhirlaporanpemasukanbarang = explode("/", $tglakhirlaporanpemasukanbarang);	
		
		
		$tgawal =  $tglawallaporanpemasukanbarang[1].'/'.$tglawallaporanpemasukanbarang[0].'/'.$tglawallaporanpemasukanbarang[2];
		$tgakhir = $tglakhirlaporanpemasukanbarang[1].'/'.$tglakhirlaporanpemasukanbarang[0].'/'.$tglakhirlaporanpemasukanbarang[2];
		
		$this->load->helper('afunction_helper');
		echo fbulan($tglawallaporanpemasukanbarang[1]);
		echo fbulan($tglakhirlaporanpemasukanbarang[1]);
		
		$periodetgawal = $tglawallaporanpemasukanbarang[0].' - '.fbulan($tglawallaporanpemasukanbarang[1]).' - '.$tglawallaporanpemasukanbarang[2];
		$periodetgakhir = $tglakhirlaporanpemasukanbarang[0].' - '.fbulan($tglakhirlaporanpemasukanbarang[1]).' - '.$tglakhirlaporanpemasukanbarang[2];	
	}
	
	function pdfpemasukanbarangperdokumen(){
		$tglawallaporanpemasukanbarang = (isset($_REQUEST['tglawallaporanpemasukanbarang']) && !empty($_REQUEST['tglawallaporanpemasukanbarang']))?$_REQUEST['tglawallaporanpemasukanbarang']:'-';
		$tglakhirlaporanpemasukanbarang = (isset($_REQUEST['tglakhirlaporanpemasukanbarang']) && !empty($_REQUEST['tglakhirlaporanpemasukanbarang']))?$_REQUEST['tglakhirlaporanpemasukanbarang']:'-';
		
		$tgawal = substr($tglawallaporanpemasukanbarang, 3, 2).'/'.substr($tglawallaporanpemasukanbarang, 0, 2).'/'.substr($tglawallaporanpemasukanbarang, 6, 4);
		$tgakhir = substr($tglakhirlaporanpemasukanbarang, 3, 2).'/'.substr($tglakhirlaporanpemasukanbarang, 0, 2).'/'.substr($tglakhirlaporanpemasukanbarang, 6, 4);
		
		$this->load->library('Pdf');
		$this->pdf->SetSubject('TCPDF Tutorial');
		$this->pdf->SetKeywords('PDF','tcpdf','html');
		
		
		$this->pdf->SetMargins(1.5, 1.5, 1.5);
		$this->pdf->SetHeaderMargin(1);
		$this->pdf->SetFooterMargin(1);
		
        $this->pdf->SetFont('times', '', 9);
		$this->pdf->addPage( 'L', 'LETTER' );
		
		$html = '<table width="2000" border="0">
  <tr>
    <td width="100">A.</td>
    <td width="1900">LAPORAN PEMASUKAN BARANG PER DOKUMEN PABEAN</td>
  </tr>
  <tr>
	<td width="100">&nbsp;</td>
    <td width="1900">KAWASAN BERIKAT PT. EDS MANUFACTURING INDONESIA</td>
  </tr>
  <tr>
	<td width="100">&nbsp;</td>
    <td width="1900">PERIODE: '. $tgawal .' S.D '. $tgakhir .'</td>
  </tr>
</table>
';
	
	$this->pdf->writeHTML($html, true, false, true, false, '');
	
	$this->pdf->SetFont('Times','B',12); //jenis font : Times New Romans, Bold, ukuran 14
	$this->pdf->Cell(0,7,"Laporan ",0,'C',1); //TBLR (untuk garis)=> B = Bottom,// L = Left, R = Right//untuk garis, C = center
	$this->fpdf->ln(0.3);
	$this->fpdf->write(0,'Tampilan Table MySql Dengan FPDF');
	$this->fpdf->Cell(1,0.5,'ID',1,0,'C');
	$this->fpdf->Cell(2,0.5,'NPM',1,0,'C');
	$this->fpdf->Cell(5,0.5,'NAMA',1,0,'C');
	$this->fpdf->Cell(6,0.5,'ALAMAT',1,0,'C');
	$this->fpdf->Ln();
	$this->pdf->output();
	
	
	$html2 = '
	<table width="3000" border="1">
  <tr>
    <td width="58" rowspan="2"><div align="center">No.</div>      <div align="center"></div></td>
    <td width="140" rowspan="2"><div align="center">Jenis Dokumen </div></td>
    <td colspan="2"><div align="center">Dokumen Pabean </div></td>
    <td colspan="2"><div align="center">Bukti Penerimaan Barang</div></td>
    <td width="176" rowspan="2"><div align="center">Pemasok/Pengirim</div></td>
    <td width="130" rowspan="2"><div align="center">Kode Barang</div></td>
    <td width="339" rowspan="2"><div align="center">Nama Barang</div></td>
    <td width="218" rowspan="2"><div align="center">Satuan</div></td>
    <td width="247" rowspan="2"><div align="center">Jumlah</div></td>
    <td width="297" rowspan="2"><div align="center">Nilai Barang</div></td>
  </tr>
  <tr>
    <td width="132"><div align="center">Nomor</div></td>
    <td width="123"><div align="center">Tanggal</div></td>
    <td width="119"><div align="center">Nomor</div></td>
    <td width="146"><div align="center">Tanggal</div></td>
  </tr>
</table>
	';
		
		//$this->pdf->writeHTML($html2, true, false, true, false, '');


// Print text using writeHTMLCell()
//$this->pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
		//$this->pdf->writeHTML($html, true, false, true, false, '');
		//$this->pdf->Output('example_001.pdf', 'I');
		$this->pdf->Output('example_001.pdf', 'FD');
	}
	
	function view_laporanpengeluaranbarangperdokumen(){
		
		$data['loading_indicator'] =  "<div class='loading_indicator'>loading...<br/><img src='./assets/images/loading.gif' alt='loading' /></div>";
		$this->load->view('reports/view_reportcustomspengeluaranbarang', $data);

	}
	
	
	
	function print_laporanpengeluaranperdok(){
		
		$tgawalpengeluaranbrgperdokumen= isset($_GET['tgawalpengeluaranbrgperdokumen'])?$_GET['tgawalpengeluaranbrgperdokumen']:'12/12/2012';
		$tgakhirpengeluaranbrgperdokumen= isset($_GET['tgakhirpengeluaranbrgperdokumen'])?$_GET['tgakhirpengeluaranbrgperdokumen']:'12/12/2012';
		$format= isset($_GET['format'])?$_GET['format']:'pdf';
		
		$tgawalpengeluaranbrgperdokumen = explode("/", $tgawalpengeluaranbrgperdokumen);	
		$tgakhirpengeluaranbrgperdokumen = explode("/", $tgakhirpengeluaranbrgperdokumen);	
		
		
		$tgawal =  $tgawalpengeluaranbrgperdokumen[1].'/'.$tgawalpengeluaranbrgperdokumen[0].'/'.$tgawalpengeluaranbrgperdokumen[2];
		$tgakhir = $tgakhirpengeluaranbrgperdokumen[1].'/'.$tgakhirpengeluaranbrgperdokumen[0].'/'.$tgakhirpengeluaranbrgperdokumen[2];
		
		$this->load->helper('afunction_helper');
		//echo fbulan($tgawalpengeluaranbrgperdokumen[1]);
		//echo fbulan($tgakhirpengeluaranbrgperdokumen[1]);
		
		$periodetgawal = $tgawalpengeluaranbrgperdokumen[0].' - '.fbulan($tgawalpengeluaranbrgperdokumen[1]).' - '.$tgawalpengeluaranbrgperdokumen[2];
		$periodetgakhir = $tgakhirpengeluaranbrgperdokumen[0].' - '.fbulan($tgakhirpengeluaranbrgperdokumen[1]).' - '.$tgakhirpengeluaranbrgperdokumen[2];
		$periode = $periodetgawal . ' S/D ' . $periodetgakhir ;
		//echo $tgawal.$tgakhir;  die();
		$this->load->library('java_bridge');
		$cekjava=$this->java_bridge->checkJavaExtension(); 
		$bcreportdir = $this->config->item('APP_dirreport');
		
		if($cekjava){
			$irep = $bcreportdir.'\rpt_outgoing.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("inputdata1", "This is a test string");
			$params->put("tglawal", $tgawal);
			$params->put("tglakhir", $tgakhir);
			$params->put("periode", $periodetgawal.' S/D '.$periodetgakhir );
			$params->put("nama_perusahaan", "PT. EDS MANUFACTURING INDONESIA");
		//	$params->put("number", 3.00);
			
			
			
			$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($jdburl, $db['default']['username'], $db['default']['password']);
			
			$jasperPrint = $fillManager->fillReport($report, $params, $conn);
			$outputPath = realpath(".")."\ireport/"."pdfpengeluaranbrg.pdf";
			
			switch ($format) {
			case 'pdf':
				
				$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
				$exportManager->exportReportToPdfFile($jasperPrint, $outputPath);
				header("Content-type: application/pdf");
				//header("Content-Disposition: attachment; filename=output.pdf"); //langsung dowload
				//$exporter->exportReport();
				readfile($outputPath);
				@unlink($outputPath);
				
				break;
			case 'rtf':
				try {
					$exporter = new java("net.sf.jasperreports.engine.export.JRRtfExporter");
					$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
					$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
				} catch (JavaException $ex) {
					echo $ex;
				}

				header("Content-type: application/rtf");
				header("Content-Disposition: attachment; filename=output.rtf");
				
				
				$exporter->exportReport();
				readfile($outputPath);
				
				@unlink($outputPath);
				break;
					
			}
			
			/* $outputPath = realpath(".")."\ireport\\"."pdfpengeluaranbrg.xls";
		    $exporter = new java("net.sf.jasperreports.engine.JRExporter");
			
			try {
				$exporter = new java("net.sf.jasperreports.engine.export.JRXlsExporter");
				$exporter->setParameter(java("net.sf.jasperreports.engine.export.JRXlsExporterParameter")->IS_ONE_PAGE_PER_SHEET, java("java.lang.Boolean")->TRUE);
				$exporter->setParameter(java("net.sf.jasperreports.engine.export.JRXlsExporterParameter")->IS_WHITE_PAGE_BACKGROUND, java("java.lang.Boolean")->FALSE);
				$exporter->setParameter(java("net.sf.jasperreports.engine.export.JRXlsExporterParameter")->IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, java("java.lang.Boolean")->TRUE);
				$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
				$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
			} catch (JavaException $ex) {
				echo $ex;
			}
			
			header("Content-type: application/vnd.ms-excel");
			header("Content-Disposition: attachment; filename=output.xls");
			 */
			
			
			//$outputPath = realpath(".") . "\\" . "output.rtf";

			
			
		}
	
	}
	
	function data_laporanpengeluaranbarangperdokumen(){
  
		$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']:'ctebc_out_lap.BCTGL'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'asc'; // get the direction	
		
		$tglawallaporanpengeluaranbarang = (isset($_POST['tglawallaporanpengeluaranbarang']) && !empty($_POST['tglawallaporanpengeluaranbarang']))?$_POST['tglawallaporanpengeluaranbarang']:date('d/m/Y');
		$tglakhirlaporanpengeluaranbarang = (isset($_POST['tglakhirlaporanpengeluaranbarang']) && !empty($_POST['tglakhirlaporanpengeluaranbarang']))?$_POST['tglakhirlaporanpengeluaranbarang']:date('d/m/Y');
		
		$jns_dok = (isset($_POST['jns_dok']) && !empty($_POST['jns_dok']))?$_POST['jns_dok']:'-';
		 
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		$records = $this->mdl_reports->datalaporanpengeluaranbarang_all($tglawallaporanpengeluaranbarang,$tglakhirlaporanpengeluaranbarang,$jns_dok,$limit,$start,$sidx,$sord, $page);
        $count = $this->mdl_reports->datalaporanpengeluaranbarang_CountAll($tglawallaporanpengeluaranbarang,$tglakhirlaporanpengeluaranbarang,$jns_dok);

	 	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]['row']=$r['row']; 
			$responce->rows[$i]['BCNO']=$r['BCNO']; 
			$responce->rows[$i]['BCTGL']=$r['BCTGL']; 
			$responce->rows[$i]['DOCOUTNO']=$r['DOCOUTNO'];
			$responce->rows[$i]['DOCOUTDATE']=$r['DOCOUTDATE']; 
			$responce->rows[$i]['NAMAPEMBELI']=$r['NAMAPEMBELI'];
			$responce->rows[$i]['ASSY_CODE']=$r['ASSY_CODE'];
			$responce->rows[$i]['ASSY_NO']=$r['ASSY_NO']; 
			$responce->rows[$i]['JNSATUAN']=$r['JNSATUAN'];
			$responce->rows[$i]['JMSATUAN']=$r['JMSATUAN'];
			$responce->rows[$i]['TYPEBC']=$r['TYPEBC']; 
			$responce->rows[$i]['NILAI']=$r['NILAI'];
			$i++;
			
		}
		echo json_encode($responce);
		
	}
	
	function printlaporanoutgoingexcel(){
		include_once ( APPPATH."libraries/PHPExcel.php");	
		include_once ( APPPATH."libraries/PHPExcel/IOFactory.php");	
		$phpExcel = new PHPExcel();
		$objTpl = PHPExcel_IOFactory::load(APPPATH."libraries/PHPExcel/templates/pemi_report_pengeluaran_barang.xls");	
		
		$caritanggal = (isset($_GET['caritanggal']) && !empty($_GET['caritanggal']))?$_GET['caritanggal']:'';
		$tglawallaporanpengeluaranbarang = (isset($_GET['tglawallaporanpengeluaranbarang']) && !empty($_GET['tglawallaporanpengeluaranbarang']))?$_GET['tglawallaporanpengeluaranbarang']:Date('d/m/Y');
		$tglakhirlaporanpengeluaranbarang = (isset($_GET['tglakhirlaporanpengeluaranbarang']) && !empty($_GET['tglakhirlaporanpengeluaranbarang']))?$_GET['tglakhirlaporanpengeluaranbarang']:Date('d/m/Y');
		$jns_dok = (isset($_GET['jns_dok']) && !empty($_GET['jns_dok']))?$_GET['jns_dok']:'-';
				
		$tglawalr =  $tglawallaporanpengeluaranbarang;	
		$tglakhirr = $tglakhirlaporanpengeluaranbarang;	
		
		
		$tglawalbb = explode("/", $tglawalr);	
		$tglakhirbb = explode("/", $tglakhirr);	
		
		
		$tgawal =  $tglawalbb[1].'/'.$tglawalbb[0].'/'.$tglawalbb[2];
		$tgakhir = $tglakhirbb[1].'/'.$tglakhirbb[0].'/'.$tglakhirbb[2];
		
		$this->load->helper('afunction_helper');
		
		$periodetgawal = $tglawalbb[0].' - '.fbulan($tglawalbb[1]).' - '.$tglawalbb[2];
		$periodetgakhir = $tglakhirbb[0].' - '.fbulan($tglakhirbb[1]).' - '.$tglakhirbb[2];
		$periode = $periodetgawal . ' S/D ' . $periodetgakhir ;
		
		if ($jns_dok == '-'){
			$where = " 1 = 1 and BCNO is not null and DOCOUTDATE between convert(date, '$tglawallaporanpengeluaranbarang',103) and convert(date, '$tglakhirlaporanpengeluaranbarang' ,103) ";
		} else {
			$where = " 1 = 1 and BCNO is not null and DOCOUTDATE between convert(date, '$tglawallaporanpengeluaranbarang',103) and convert(date, '$tglakhirlaporanpengeluaranbarang' ,103) ";
			$where .= " and TYPEBC='$jns_dok' ";
		}
		
		$records = $this->mdl_reports->datalaporanpengeluaranbarang_excel($tglawallaporanpengeluaranbarang,$tglakhirlaporanpengeluaranbarang,$jns_dok);
 		$objTpl->setActiveSheetIndex(0);  //set first sheet as active
		$objTpl->getActiveSheet()->setCellValue("C4", ":     " . $periode);
		
		$i = 1;
		$j = '';
		$k = 7;
		$l = 0;
		$m = '';
		$n = ''; 
		$o = 7;
		
		foreach ($records as $r ){
			$k = 7 + $i;
			$j = "$k";
			
			$objTpl->getActiveSheet()->setCellValue("A".$j, $i);
			$objTpl->getActiveSheet()->setCellValue("B".$j, $r['TYPEBC']);
			$objTpl->getActiveSheet()->setCellValueExplicit("C".$j, $r['BCNO'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValue("D".$j, $r['BCTGL']);
			$objTpl->getActiveSheet()->setCellValueExplicit("E".$j, $r['DOCOUTNO'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValue("F".$j, $r['DOCOUTDATE']);
			$objTpl->getActiveSheet()->setCellValueExplicit("G".$j, $r['NAMAPEMBELI'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValueExplicit("H".$j, $r['ASSY_CODE'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValueExplicit("I".$j, $r['ASSY_NO'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValueExplicit("J".$j, $r['JNSATUAN'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValue("K".$j, $r['JMSATUAN']);
			$objTpl->getActiveSheet()->setCellValue("L".$j, $r['NILAI']);
			$i++;
		} 
		$objTpl->getActiveSheet()->getStyle("L7:"."L".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		
		$datenow = date('Ymd');
		$filename =  "REPORT_". "4BULAN_PEMI_PENGELUARANDOK_"  . $datenow .".xls";
		
		header("Content-type:application/vnd.ms-excel");
		header("Content-disposition:attachment; filename=$filename"); 
		header("Pragma:no-cache");
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		header("Expires:0");

		$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
		$objWriter->save('php://output');  //send it to user, of course you can save it to disk also!
		 
		exit; //done.. exiting!
		
	}
	
	function printlaporanwipexcel(){
		include_once ( APPPATH."libraries/PHPExcel.php");	
		include_once ( APPPATH."libraries/PHPExcel/IOFactory.php");	
		$phpExcel = new PHPExcel();
		$objTpl = PHPExcel_IOFactory::load(APPPATH."libraries/PHPExcel/templates/pemi_report_wip.xls");	
		
		$caritanggal = (isset($_GET['caritanggal']) && !empty($_GET['caritanggal']))?$_GET['caritanggal']:'';
		$tglawallapposisibrgdlmproses = (isset($_GET['tglawallapposisibrgdlmproses']) && !empty($_GET['tglawallapposisibrgdlmproses']))?$_GET['tglawallapposisibrgdlmproses']:Date('d/m/Y');
		$tglakhirlapposisibrgdlmproses = (isset($_GET['tglakhirlapposisibrgdlmproses']) && !empty($_GET['tglakhirlapposisibrgdlmproses']))?$_GET['tglakhirlapposisibrgdlmproses']:Date('d/m/Y');
		$jns_dok = (isset($_GET['jns_dok']) && !empty($_GET['jns_dok']))?$_GET['jns_dok']:'-';
				
		$tglawalr =  $tglawallapposisibrgdlmproses;	
		$tglakhirr = $tglakhirlapposisibrgdlmproses;	
		
		
		$tglawalbb = explode("/", $tglawalr);	
		$tglakhirbb = explode("/", $tglakhirr);	
		
		$date1 = $tglawalbb[2] . '-' . $tglawalbb[1] . '-' . $tglawalbb[0];
		$date2 = $tglakhirbb[2] . '-' . $tglakhirbb[1] . '-' . $tglakhirbb[0];
		
		$tgawal =  $tglawalbb[1].'/'.$tglawalbb[0].'/'.$tglawalbb[2];
		$tgakhir = $tglakhirbb[1].'/'.$tglakhirbb[0].'/'.$tglakhirbb[2];
		
		$this->load->helper('afunction_helper');
		
		$periodetgawal = $tglawalbb[0].' - '.fbulan($tglawalbb[1]).' - '.$tglawalbb[2];
		$periodetgakhir = $tglakhirbb[0].' - '.fbulan($tglakhirbb[1]).' - '.$tglakhirbb[2];
		$periode = $periodetgawal . ' S/D ' . $periodetgakhir ;
		
		$records = $this->mdl_reports->datawip_excel($date1,$date2);
		$objTpl->setActiveSheetIndex(0);  //set first sheet as active
		$objTpl->getActiveSheet()->setCellValue("C4", ":     " . $periode);
		
		$i = 1;
		$j = '';
		$k = 7;
		$l = 0;
		$m = '';
		$n = ''; 
		$o = 7;
		
		foreach ($records as $r ){
			$k = 7 + $i;
			$j = "$k";
			
			$objTpl->getActiveSheet()->setCellValue("A".$j, $i);
 			$objTpl->getActiveSheet()->setCellValueExplicit("B".$j, $r['part_no'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValueExplicit("C".$j, $r['part_name'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValueExplicit("D".$j, $r['unit'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValue("F".$j, $r['qty']);
			$i++;
		} 
		
		$objTpl->getActiveSheet()->getStyle("F7:"."F".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		
		$datenow = date('Ymd');
		$filename =  "REPORT_". "4BULAN_PEMI_WIP_"  . $datenow .".xls";
		
		header("Content-type:application/vnd.ms-excel");
		header("Content-disposition:attachment; filename=$filename"); 
		header("Pragma:no-cache");
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		header("Expires:0");

		$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
		$objWriter->save('php://output');  //send it to user, of course you can save it to disk also!
		 
		exit; //done.. exiting!
	}
	
	function view_reportcustomsposisibarangdalamproses(){
		
		$data['loading_indicator'] =  "<div class='loading_indicator'>loading...<br/><img src='./assets/images/loading.gif' alt='loading' /></div>";
		$query = $this->db->query("
			select convert(varchar, MAX(tgl_closed), 103) as date_stock from tbl_closed_period
		");
		$result = $query->result_array();
		$xx='01/05/2013';
		foreach ($result as $r){
			$xx=$r['date_stock'];
		}
		$data['datestock'] =$xx; 
		echo $xx;
		
		$this->load->view('reports/view_reportcustomsposisibarangdalamproses', $data);

	}
	
	function loaddataposisibarangdalamproses(){
		$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']:'tbl_data_out_dtl.part_number'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'asc'; // get the direction	
		
		$tglawallapposisibrgdlmproses = (isset($_POST['tglawallapposisibrgdlmproses']) && !empty($_POST['tglawallapposisibrgdlmproses']))?$_POST['tglawallapposisibrgdlmproses']:date('d/m/Y');
		$tglakhirlapposisibrgdlmproses = (isset($_POST['tglakhirlapposisibrgdlmproses']) && !empty($_POST['tglakhirlapposisibrgdlmproses']))?$_POST['tglakhirlapposisibrgdlmproses']:date('d/m/Y');
		
		//$where = " tbl_data_out_hdr.doc_out_to_id=1 and out_date between convert(date, '$tglawallapposisibrgdlmproses', 103) and convert(date, '$tglakhirlapposisibrgdlmproses', 103)  ";
		
		
		$tglawalbb = explode("/", $tglawallapposisibrgdlmproses);	
		$tglakhirbb = explode("/", $tglakhirlapposisibrgdlmproses);	
		
		
		$tgawal =  $tglawalbb[1].'/'.$tglawalbb[0].'/'.$tglawalbb[2];
		$tgakhir = $tglakhirbb[1].'/'.$tglakhirbb[0].'/'.$tglakhirbb[2];
		
		$tgl = date('m/d/Y');
		
		$date1=$tgl;
		$date2=$tgl; 
		
		$where = " 1=1 ";
		$wherecount = " 1=1 ";
		if ($tgawal != '-' && $tgawal != '-'){
		  $wheredate = " '$tgawal' and '$tgakhir' ";
		  $date1=$tgawal;
		  $date2=$tgakhir; 
		} else {
			 $wheredate = " '$tgl' and '$tgl' ";
			 $date1=$tgl;
			 $date2=$tgl; 
		}
		
		
		
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_reports->datawip_All($limit,$start,$sidx,$sord, $where, $wheredate, $date1, $date2, $page);
        $count = $this->mdl_reports->datawip_CountAll($where,$wheredate, $date1, $date2);
		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]['nomorurut']= $r['row']; 
			$responce->rows[$i]['qty']= $r['qty']; 
			$responce->rows[$i]['part_name_customs']= $r['part_name']; 
			$responce->rows[$i]['kodebrg']= $r['part_no']; 
			$responce->rows[$i]['satuan']= $r['unit']; 
			$responce->rows[$i]['keterangan']= '-'; //$r['row']; 
			
			$i++;
			
		}
		echo json_encode($responce);
	
	}
	
	function data_data_wip(){
	
		$tglawallapposisibrgdlmproses= isset($_GET['tglawallapposisibrgdlmproses'])?$_GET['tglawallapposisibrgdlmproses']:'12/12/2012';
		$tglakhirlapposisibrgdlmproses= isset($_GET['tglakhirlapposisibrgdlmproses'])?$_GET['tglakhirlapposisibrgdlmproses']:'12/12/2012';
		$format= isset($_GET['format'])?$_GET['format']:'pdf';
		
		$tglawallapposisibrgdlmproses = explode("/", $tglawallapposisibrgdlmproses);	
		$tglakhirlapposisibrgdlmproses = explode("/", $tglakhirlapposisibrgdlmproses);	
		
		
		$tgawal =  $tglawallapposisibrgdlmproses[1].'/'.$tglawallapposisibrgdlmproses[0].'/'.$tglawallapposisibrgdlmproses[2];
		$tgakhir = $tglakhirlapposisibrgdlmproses[1].'/'.$tglakhirlapposisibrgdlmproses[0].'/'.$tglakhirlapposisibrgdlmproses[2];
		
		$this->load->helper('afunction_helper');
		//echo fbulan($tglawallapposisibrgdlmproses[1]);
		//echo fbulan($tglakhirlapposisibrgdlmproses[1]);
		
		$periodetgawal = $tglawallapposisibrgdlmproses[0].' - '.fbulan($tglawallapposisibrgdlmproses[1]).' - '.$tglawallapposisibrgdlmproses[2];
		$periodetgakhir = $tglakhirlapposisibrgdlmproses[0].' - '.fbulan($tglakhirlapposisibrgdlmproses[1]).' - '.$tglakhirlapposisibrgdlmproses[2];
		$periode = $periodetgawal . ' S/D ' . $periodetgakhir ;
		//echo $tgawal.$tgakhir;  die();
		$this->load->library('java_bridge');
		$cekjava=$this->java_bridge->checkJavaExtension(); 
		$bcreportdir = $this->config->item('APP_dirreport');
		
		if($cekjava){
			$irep = $bcreportdir.'\rpt_wip.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("inputdata1", "This is a test string");
			$params->put("tglawal", $tgawal);
			$params->put("tglakhir", $tgakhir);
			$params->put("periode", $periodetgawal.' S/D '.$periodetgakhir );
			$params->put("nama_perusahaan", "PT. EDS MANUFACTURING INDONESIA");
		//	$params->put("number", 3.00);
			
			
			
			$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($jdburl, $db['default']['username'], $db['default']['password']);
			
			$jasperPrint = $fillManager->fillReport($report, $params, $conn);
			$outputPath = realpath(".")."\ireport/"."rpt_wip.pdf";
			
			switch ($format) {
			case 'pdf':
				
				$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
				$exportManager->exportReportToPdfFile($jasperPrint, $outputPath);
				header("Content-type: application/pdf");
				//header("Content-Disposition: attachment; filename=output.pdf"); //langsung dowload
				//$exporter->exportReport();
				readfile($outputPath);
				@unlink($outputPath);
				
				break;
			case 'rtf':
				try {
					$exporter = new java("net.sf.jasperreports.engine.export.JRRtfExporter");
					$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
					$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
				} catch (JavaException $ex) {
					echo $ex;
				}

				header("Content-type: application/rtf");
				header("Content-Disposition: attachment; filename=output.rtf");
				
				
				$exporter->exportReport();
				readfile($outputPath);
				
				@unlink($outputPath);
				break;
					
			}
		}		
	
	}
	
	function view_reportcustomsmutasibahanbakudanbahanpenolong(){
		//select top 1 convert(varchar, date_stock, 103) as date_stock from dbo.tbl_stock_awal where typebahan='RM' group by date_stock order by date_stock desc
		$data['loading_indicator'] =  "<div class='loading_indicator'>loading...<br/><img src='./assets/images/loading.gif' alt='loading' /></div>";
		$query = $this->db->query("
			select convert(varchar, MAX(tgl_closed), 103) as date_stock from tbl_closed_period
		");
		$result = $query->result_array();
		$xx='01/05/2013';
		foreach ($result as $r){
			$xx=$r['date_stock'];
		}
		$data['datestock'] =$xx; 
		//echo $xx;
		
		/* $q = $this->db->query("
			select top 1 date_stock_opname from dbo.tbl_stock_opname 
			where date_stock_opname < '2013-09-01'
			order by date_stock_opname desc
		");
		 */
		 
		 
		$this->load->view('reports/view_reportcustomsmutasibahanbakudanbahanpenolong', $data);

	}
	
	function data_reportcustomsmutasibahanbakudanbahanpenolong(){
		$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']:'tbl_data_out_dtl.part_number'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'asc'; // get the direction	
		
		$tglawalbb = (isset($_POST['tglawalbb']) && !empty($_POST['tglawalbb']))?$_POST['tglawalbb']:date('m/d/Y');
		$tglakhirbb = (isset($_POST['tglakhirbb']) && !empty($_POST['tglakhirbb']))?$_POST['tglakhirbb']:date('m/d/Y');
		
		$tglawalbb = explode("/", $tglawalbb);	
		$tglakhirbb = explode("/", $tglakhirbb);	
		
		
		$tgawal =  $tglawalbb[1].'/'.$tglawalbb[0].'/'.$tglawalbb[2];
		$tgakhir = $tglakhirbb[1].'/'.$tglakhirbb[0].'/'.$tglakhirbb[2];
		
		$tgl = date('m/d/Y');
		
		$date1=$tgl;
		$date2=$tgl; 
		
		$where = " 1=1 ";
		$wherecount = " 1=1 ";
		if ($tgawal != '-' && $tgawal != '-'){
		  $wheredate = " '$tgawal' and '$tgakhir' ";
		  $date1=$tgawal;
		  $date2=$tgakhir; 
		} else {
			 $wheredate = " '$tgl' and '$tgl' ";
			 $date1=$tgl;
			 $date2=$tgl; 
		}
		
		
		
		
		//echo $wheredate; die();
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "kodebarang" ) {
						$searchString = $k->data;
						$where .= " and kodebarang like  '%".$searchString."%'"; 
						$wherecount .= " and x.ASSY_CODE like  '%".$searchString."%'"; 
					} else if ( $k->field == "part_name" ) {
						$searchString = $k->data;
						$where .= " and part_name like  '%".$searchString."%'"; 
						$wherecount .= " and x.ASSY_NO like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_reports->datamutasibb_All($limit,$start,$sidx,$sord, $where, $wheredate, $date1, $date2, $page);
		$count = $this->mdl_reports->datamutasibb_CountAll($where,$wheredate, $date1, $date2);
		
		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]['no']= $r['row']; 
			$responce->rows[$i]['kodebarang']=$r['kodebarang']; 
			$responce->rows[$i]['namabarang']=$r['namabarang'];
			$responce->rows[$i]['saldoawal']=$r['qty_saldoawal']; 
			$responce->rows[$i]['satuan']=$r['unit']; 
			$responce->rows[$i]['pemasukan']=$r['qty_in']; 
			$responce->rows[$i]['pengeluaran']=$r['qty_out'];  
			$responce->rows[$i]['saldoakhir']=$r['saldo_akhir'];  
			$responce->rows[$i]['stockopname']=$r['stock_opname'];  
			$responce->rows[$i]['penyesuaian']=$r['adj'];  
			$responce->rows[$i]['selisih']=$r['selisih']; ;  
			 
			 
			 
			$i++;
			
		}
		echo json_encode($responce);	
	
	}
	
	function data_reportcustomsmutasibahanbakudanbahanpenolong_jasper(){
	
		$tglawalbb= isset($_GET['tglawalbb'])?$_GET['tglawalbb']:'01/01/2013';
		$tglakhirbb= isset($_GET['tglakhirbb'])?$_GET['tglakhirbb']:'01/01/2013';
		$format= isset($_GET['format'])?$_GET['format']:'pdf';
		
		$tglawalbb = explode("/", $tglawalbb);	
		$tglakhirbb = explode("/", $tglakhirbb);	
		
		
		$tgawal =  $tglawalbb[1].'/'.$tglawalbb[0].'/'.$tglawalbb[2];
		$tgakhir = $tglakhirbb[1].'/'.$tglakhirbb[0].'/'.$tglakhirbb[2];
		
		$this->load->helper('afunction_helper');
		//echo fbulan($tglawalbb[1]);
		//echo fbulan($tglakhirbb[1]);
		
		$periodetgawal = $tglawalbb[0].' - '.fbulan($tglawalbb[1]).' - '.$tglawalbb[2];
		$periodetgakhir = $tglakhirbb[0].' - '.fbulan($tglakhirbb[1]).' - '.$tglakhirbb[2];
		$periode = $periodetgawal . ' S/D ' . $periodetgakhir ;
		//echo $tgawal.$tgakhir;  die();
		$this->load->library('java_bridge');
		$cekjava=$this->java_bridge->checkJavaExtension(); 
		$bcreportdir = $this->config->item('APP_dirreport');
		
		if($cekjava){
			$irep = $bcreportdir.'\rpt_mts_bb_bp.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("inputdata1", "This is a test string");
			$params->put("tglawal", $tgawal);
			$params->put("tglakhir", $tgakhir);
			$params->put("periode", $periodetgawal.' S/D '.$periodetgakhir );
			$params->put("nama_perusahaan", "PT. EDS MANUFACTURING INDONESIA");
		//	$params->put("number", 3.00);
			
			
			
			$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($jdburl, $db['default']['username'], $db['default']['password']);
			
			$jasperPrint = $fillManager->fillReport($report, $params, $conn);
			$outputPath = realpath(".")."\ireport/"."rpt_bb_out.pdf";
			
			switch ($format) {
			case 'pdf':
				
				$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
				$exportManager->exportReportToPdfFile($jasperPrint, $outputPath);
				header("Content-type: application/pdf");
				//header("Content-Disposition: attachment; filename=output.pdf"); //langsung dowload
				//$exporter->exportReport();
				readfile($outputPath);
				@unlink($outputPath);
				
				break;
			case 'rtf':
				try {
					$exporter = new java("net.sf.jasperreports.engine.export.JRRtfExporter");
					$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
					$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
				} catch (JavaException $ex) {
					echo $ex;
				}

				header("Content-type: application/rtf");
				header("Content-Disposition: attachment; filename=output.rtf");
				
				
				$exporter->exportReport();
				readfile($outputPath);
				
				@unlink($outputPath);
				break;
					
			}
		}		
	
	
	}
	function view_reportcustomsmutasibahanjadi(){
		
		$data['loading_indicator'] =  "<div class='loading_indicator'>loading...<br/><img src='./assets/images/loading.gif' alt='loading' /></div>";
		$query = $this->db->query("
			select convert(varchar, MAX(tgl_closed), 103) as date_stock from tbl_closed_period
		");
		$result = $query->result_array();
		$xx='01/05/2013';
		foreach ($result as $r){
			$xx=$r['date_stock'];
		}
		$data['datestock'] =$xx; 
		
		$this->load->view('reports/view_reportcustomsmutasibarangjadi', $data);

	}
	
	
	function data_reportcustomsmutasibahanjadi(){
		$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']:'tbl_data_out_dtl.part_number'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'asc'; // get the direction	
		
		$tglawalbb = (isset($_POST['tglawalbj']) && !empty($_POST['tglawalbj']))?$_POST['tglawalbj']:date('m/d/Y');
		$tglakhirbb = (isset($_POST['tglakhirbj']) && !empty($_POST['tglakhirbj']))?$_POST['tglakhirbj']:date('m/d/Y');
		
		$tglawalbb = explode("/", $tglawalbb);	
		$tglakhirbb = explode("/", $tglakhirbb);	
		
		
		$tgawal =  $tglawalbb[1].'/'.$tglawalbb[0].'/'.$tglawalbb[2];
		$tgakhir = $tglakhirbb[1].'/'.$tglakhirbb[0].'/'.$tglakhirbb[2];
		
		$tgl = date('m/d/Y');
		
				
		$where = " 1=1 ";
		$wherecount = " 1=1 ";
		if ($tgawal != '-' && $tgawal != '-'){
		  $wheredate = " '$tgawal' and '$tgakhir' ";
		  $date1=$tgawal;
		  $date2=$tgakhir; 
		} else {
			 $wheredate = " '$tgl' and '$tgl' ";
			 $date1=$tgl;
			 $date2=$tgl; 
		}
		
		//echo $wheredate; die();
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "part_no" ) {
						$searchString = $k->data;
						$where .= " and part_no like  '%".$searchString."%'"; 
						$wherecount .= " and x.ASSY_CODE like  '%".$searchString."%'"; 
					} else if ( $k->field == "part_name" ) {
						$searchString = $k->data;
						$where .= " and part_name like  '%".$searchString."%'"; 
						$wherecount .= " and x.ASSY_NO like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_reports->datamutasibj_All($limit,$start,$sidx,$sord, $where, $wheredate, $date1, $date2, $page);
		$count = $this->mdl_reports->datamutasibj_CountAll($where,$wheredate, $date1, $date2);
		
		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]['no']= $r['row']; 
			$responce->rows[$i]['kodebarang']=$r['part_no']; 
			$responce->rows[$i]['namabarang']=$r['part_name'];
			$responce->rows[$i]['saldoawal']=$r['stock_awal']; 
			$responce->rows[$i]['pemasukan_1']=$r['qty_in_by_date']; 
			$responce->rows[$i]['pengeluaran_1']= $r['qty_out_by_date']; 
			$responce->rows[$i]['saldoakhir_1']=$r['sisa']; 
			$responce->rows[$i]['satuan']=$r['unit']; 
			$responce->rows[$i]['pemasukan']= $r['qty_in'];
			$responce->rows[$i]['pengeluaran']=$r['qty_out']; 
			$responce->rows[$i]['saldoakhir']=$r['sisa_by_date'];  
			$responce->rows[$i]['stockopname']=$r['stock_opname'];  
			$responce->rows[$i]['penyesuaian']=$r['adj'];  
			$responce->rows[$i]['selisih']=$r['selisih']; 
			$i++;
			
		}
		echo json_encode($responce);	
	
	}
	
	function data_reportcustomsmutasibahanjadi_jasper(){
		
		$tglawalbj= isset($_GET['tglawalbj'])?$_GET['tglawalbj']:'12/12/2012';
		$tglakhirbj= isset($_GET['tglakhirbj'])?$_GET['tglakhirbj']:'12/12/2012';
		$format= isset($_GET['format'])?$_GET['format']:'pdf';
		
		$tglawalbj = explode("/", $tglawalbj);	
		$tglakhirbj = explode("/", $tglakhirbj);	
		
		
		$tgawal =  $tglawalbj[1].'/'.$tglawalbj[0].'/'.$tglawalbj[2];
		$tgakhir = $tglakhirbj[1].'/'.$tglakhirbj[0].'/'.$tglakhirbj[2];
		
		$this->load->helper('afunction_helper');
		//echo fbulan($tglawalbj[1]);
		//echo fbulan($tglakhirbj[1]);
		
		$periodetgawal = $tglawalbj[0].' - '.fbulan($tglawalbj[1]).' - '.$tglawalbj[2];
		$periodetgakhir = $tglakhirbj[0].' - '.fbulan($tglakhirbj[1]).' - '.$tglakhirbj[2];
		$periode = $periodetgawal . ' S/D ' . $periodetgakhir ;
		//echo $tgawal.$tgakhir;  die();
		$this->load->library('java_bridge');
		$cekjava=$this->java_bridge->checkJavaExtension(); 
		$bcreportdir = $this->config->item('APP_dirreport');
		
		if($cekjava){
			$irep = $bcreportdir.'\rpt_mts_fg.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("inputdata1", "This is a test string");
			$params->put("tglawal", $tgawal);
			$params->put("tglakhir", $tgakhir);
			$params->put("periode", $periodetgawal.' S/D '.$periodetgakhir );
			$params->put("nama_perusahaan", "PT. EDS MANUFACTURING INDONESIA");
		//	$params->put("number", 3.00);
			
			
			
			$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($jdburl, $db['default']['username'], $db['default']['password']);
			
			$jasperPrint = $fillManager->fillReport($report, $params, $conn);
			$outputPath = realpath(".")."\ireport/"."rpt_fg_out.pdf";
			
			switch ($format) {
			case 'pdf':
				
				$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
				$exportManager->exportReportToPdfFile($jasperPrint, $outputPath);
				header("Content-type: application/pdf");
				//header("Content-Disposition: attachment; filename=output.pdf"); //langsung dowload
				//$exporter->exportReport();
				readfile($outputPath);
				@unlink($outputPath);
				
				break;
			case 'rtf':
				try {
					$exporter = new java("net.sf.jasperreports.engine.export.JRRtfExporter");
					$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
					$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
				} catch (JavaException $ex) {
					echo $ex;
				}

				header("Content-type: application/rtf");
				header("Content-Disposition: attachment; filename=output.rtf");
				
				
				$exporter->exportReport();
				readfile($outputPath);
				
				@unlink($outputPath);
				break;
					
			}
		}		
	
	}
	
	function view_reportcustomsmutasibahanmesin(){
		$data['loading_indicator'] =  "<div class='loading_indicator'>loading...<br/><img src='./assets/images/loading.gif' alt='loading' /></div>";
		$query = $this->db->query("
			select convert(varchar, MAX(tgl_closed), 103) as date_stock from tbl_closed_period
		");
		$result = $query->result_array();
		$xx='01/05/2013';
		foreach ($result as $r){
			$xx=$r['date_stock'];
		}
		$data['date_stock'] =$xx; 
		
		$this->load->view('reports/view_reportcustomsmutasibahanmesin', $data);

	}
	
	function data_reportmutasimpk(){
		$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']:'tbl_data_out_dtl.part_number'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'asc'; // get the direction	
		
		$tglawalmpk = (isset($_POST['tglawalmpk']) && !empty($_POST['tglawalmpk']))?$_POST['tglawalmpk']:date('m/d/Y');
		$tglakhirmpk = (isset($_POST['tglakhirmpk']) && !empty($_POST['tglakhirmpk']))?$_POST['tglakhirmpk']:date('m/d/Y');
		
		$tglawalmpk = explode("/", $tglawalmpk);	
		$tglakhirmpk = explode("/", $tglakhirmpk);	
		
		
		$tgawal =  $tglawalmpk[1].'/'.$tglawalmpk[0].'/'.$tglawalmpk[2];
		$tgakhir = $tglakhirmpk[1].'/'.$tglakhirmpk[0].'/'.$tglakhirmpk[2];
		
		$tgl = date('m/d/Y');
		
		$date1=$tgl;
		$date2=$tgl; 
		
		$where = " 1=1 ";
		$wherecount = " 1=1 ";
		if ($tgawal != '-' && $tgawal != '-'){
		  $wheredate = " '$tgawal' and '$tgakhir' ";
		  $date1=$tgawal;
		  $date2=$tgakhir; 
		} else {
			 $wheredate = " '$tgl' and '$tgl' ";
			 $date1=$tgl;
			 $date2=$tgl; 
		}
		
		//echo $date1.'--'.$date2; die();
		//echo $wheredate; die();
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "kodebarang" ) {
						$searchString = $k->data;
						$where .= " and kodebarang like  '%".$searchString."%'"; 
						$wherecount .= " and x.ASSY_CODE like  '%".$searchString."%'"; 
					} else if ( $k->field == "part_name" ) {
						$searchString = $k->data;
						$where .= " and part_name like  '%".$searchString."%'"; 
						$wherecount .= " and x.ASSY_NO like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_reports->datamutasimpk_All($limit,$start,$sidx,$sord, $where, $wheredate, $date1, $date2, $page);
		$count = $this->mdl_reports->datamutasimpk_CountAll($where,$wheredate, $date1, $date2);
		
		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]['no']= $r['row']; 
			$responce->rows[$i]['kodebarang']=$r['kodebarang']; 
			$responce->rows[$i]['namabarang']=$r['namabarang'];
			$responce->rows[$i]['saldoawal']=$r['qty_saldoawal']; 
			$responce->rows[$i]['satuan']=$r['unit']; 
			$responce->rows[$i]['pemasukan']=$r['qty_in']; 
			$responce->rows[$i]['pengeluaran']=$r['qty_out'];  
			$responce->rows[$i]['saldoakhir']=$r['saldo_akhir'];  
			$responce->rows[$i]['stockopname']=$r['stock_opname'];  
			$responce->rows[$i]['penyesuaian']=$r['adj'];  
			$responce->rows[$i]['selisih']='0';  
			 
			 
			 
			$i++;
			
		}
		echo json_encode($responce);	
	
	}
	
	function data_reportmutasimpk_jasper(){
	
		$tglawalmpk= isset($_GET['tglawalmpk'])?$_GET['tglawalmpk']:'01/01/2013';
		$tglakhirmpk= isset($_GET['tglakhirmpk'])?$_GET['tglakhirmpk']:'01/01/2013';
		$format= isset($_GET['format'])?$_GET['format']:'pdf';
		
		$tglawalmpk = explode("/", $tglawalmpk);	
		$tglakhirmpk = explode("/", $tglakhirmpk);	
		
		
		$tgawal =  $tglawalmpk[1].'/'.$tglawalmpk[0].'/'.$tglawalmpk[2];
		$tgakhir = $tglakhirmpk[1].'/'.$tglakhirmpk[0].'/'.$tglakhirmpk[2];
		
		$this->load->helper('afunction_helper');
		//echo fbulan($tglawalmpk[1]);
		//echo fbulan($tglakhirmpk[1]);
		
		$periodetgawal = $tglawalmpk[0].' - '.fbulan($tglawalmpk[1]).' - '.$tglawalmpk[2];
		$periodetgakhir = $tglakhirmpk[0].' - '.fbulan($tglakhirmpk[1]).' - '.$tglakhirmpk[2];
		$periode = $periodetgawal . ' S/D ' . $periodetgakhir ;
		//echo $tgawal.$tgakhir;  die();
		$this->load->library('java_bridge');
		$cekjava=$this->java_bridge->checkJavaExtension(); 
		$bcreportdir = $this->config->item('APP_dirreport');
		
		if($cekjava){
			$irep = $bcreportdir.'\rpt_mts_ms_pk.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("inputdata1", "This is a test string");
			$params->put("tglawal", $tgawal);
			$params->put("tglakhir", $tgakhir);
			$params->put("periode", $periodetgawal.' S/D '.$periodetgakhir );
			$params->put("nama_perusahaan", "PT. EDS MANUFACTURING INDONESIA");
		//	$params->put("number", 3.00);
			
			
			
			$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($jdburl, $db['default']['username'], $db['default']['password']);
			
			$jasperPrint = $fillManager->fillReport($report, $params, $conn);
			$outputPath = realpath(".")."\ireport/"."rpt_mpk.pdf";
			
			switch ($format) {
			case 'pdf':
				
				$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
				$exportManager->exportReportToPdfFile($jasperPrint, $outputPath);
				header("Content-type: application/pdf");
				//header("Content-Disposition: attachment; filename=output.pdf"); //langsung dowload
				//$exporter->exportReport();
				readfile($outputPath);
				@unlink($outputPath);
				
				break;
			case 'rtf':
				try {
					$exporter = new java("net.sf.jasperreports.engine.export.JRRtfExporter");
					$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
					$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
				} catch (JavaException $ex) {
					echo $ex;
				}

				header("Content-type: application/rtf");
				header("Content-Disposition: attachment; filename=output.rtf");
				
				
				$exporter->exportReport();
				readfile($outputPath);
				
				@unlink($outputPath);
				break;
					
			}
		}		
	
	
	}
	
	//---scrap
	function view_reportcustomsmutasibahanscrap(){
		$data['loading_indicator'] =  "<div class='loading_indicator'>loading...<br/><img src='./assets/images/loading.gif' alt='loading' /></div>";
		$query = $this->db->query("
			select convert(varchar, MAX(tgl_closed), 103) as date_stock from tbl_closed_period
		");
		$result = $query->result_array();
		$xx='01/05/2013';
		foreach ($result as $r){
			$xx=$r['date_stock'];
		}
		$data['date_stock'] =$xx; 
		
		$this->load->view('reports/view_reportcustomsmutasibahanscrap', $data);

	}
	
	function data_reportmutasiscr(){
		$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']:'tbl_data_out_dtl.part_number'; // get index row - i.e. user click to sort
        $sord = isset($_POST['sord'])?$_POST['sord']:'asc'; // get the direction	
		
		$tglawalscr = (isset($_POST['tglawalscr']) && !empty($_POST['tglawalscr']))?$_POST['tglawalscr']:date('m/d/Y');
		$tglakhirscr = (isset($_POST['tglakhirscr']) && !empty($_POST['tglakhirscr']))?$_POST['tglakhirscr']:date('m/d/Y');
		
		$tglawalscr = explode("/", $tglawalscr);	
		$tglakhirscr = explode("/", $tglakhirscr);	
		
		
		$tgawal =  $tglawalscr[1].'/'.$tglawalscr[0].'/'.$tglawalscr[2];
		$tgakhir = $tglakhirscr[1].'/'.$tglakhirscr[0].'/'.$tglakhirscr[2];
		
		$tgl = date('m/d/Y');
		
		$date1=$tgl;
		$date2=$tgl; 
		
		$where = " 1=1 ";
		$wherecount = " 1=1 ";
		if ($tgawal != '-' && $tgawal != '-'){
		  $wheredate = " '$tgawal' and '$tgakhir' ";
		  $date1=$tgawal;
		  $date2=$tgakhir; 
		} else {
			 $wheredate = " '$tgl' and '$tgl' ";
			 $date1=$tgl;
			 $date2=$tgl; 
		}
		
		//echo $date1.'--'.$date2; die();
		//echo $wheredate; die();
		
		if ($search == 'true'){
			if (!empty($filters)){
				$array = json_decode($filters);
				foreach($array->rules as $arr=>$k){
					
					if ( $k->field == "kodebarang" ) {
						$searchString = $k->data;
						$where .= " and kodebarang like  '%".$searchString."%'"; 
						$wherecount .= " and x.ASSY_CODE like  '%".$searchString."%'"; 
					} else if ( $k->field == "part_name" ) {
						$searchString = $k->data;
						$where .= " and part_name like  '%".$searchString."%'"; 
						$wherecount .= " and x.ASSY_NO like  '%".$searchString."%'"; 
					} 
				}
			}
		}
		$start = $limit*$page - $limit;
		if ($start<0) $start=0;
		
		$records = $this->mdl_reports->datamutasiscr_All($limit,$start,$sidx,$sord, $where, $wheredate, $date1, $date2, $page);
		$count = $this->mdl_reports->datamutasiscr_CountAll($where,$wheredate, $date1, $date2);
		
		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]['no']= $r['row']; 
			$responce->rows[$i]['kodebarang']=$r['kodebarang']; 
			$responce->rows[$i]['namabarang']=$r['namabarang'];
			$responce->rows[$i]['saldoawal']=$r['qty_saldoawal']; 
			$responce->rows[$i]['satuan']=$r['unit']; 
			$responce->rows[$i]['pemasukan']=$r['qty_in']; 
			$responce->rows[$i]['pengeluaran']=$r['qty_out'];  
			$responce->rows[$i]['saldoakhir']=$r['saldo_akhir'];  
			$responce->rows[$i]['stockopname']=$r['stock_opname'];  
			$responce->rows[$i]['penyesuaian']=$r['adj'];   
			$responce->rows[$i]['selisih']='0';  
			 
			 
			 
			$i++;
			
		}
		echo json_encode($responce);	
	
	}
	
	function data_reportmutasiscr_jasper(){
	
		$tglawalscr= isset($_GET['tglawalscr'])?$_GET['tglawalscr']:'01/01/2013';
		$tglakhirscr= isset($_GET['tglakhirscr'])?$_GET['tglakhirscr']:'01/01/2013';
		$format= isset($_GET['format'])?$_GET['format']:'pdf';
		
		$tglawalscr = explode("/", $tglawalscr);	
		$tglakhirscr = explode("/", $tglakhirscr);	
		
		
		$tgawal =  $tglawalscr[1].'/'.$tglawalscr[0].'/'.$tglawalscr[2];
		$tgakhir = $tglakhirscr[1].'/'.$tglakhirscr[0].'/'.$tglakhirscr[2];
		
		$this->load->helper('afunction_helper');
		//echo fbulan($tglawalscr[1]);
		//echo fbulan($tglakhirscr[1]);
		
		$periodetgawal = $tglawalscr[0].' - '.fbulan($tglawalscr[1]).' - '.$tglawalscr[2];
		$periodetgakhir = $tglakhirscr[0].' - '.fbulan($tglakhirscr[1]).' - '.$tglakhirscr[2];
		$periode = $periodetgawal . ' S/D ' . $periodetgakhir ;
		//echo $tgawal.$tgakhir;  die();
		$this->load->library('java_bridge');
		$cekjava=$this->java_bridge->checkJavaExtension(); 
		$bcreportdir = $this->config->item('APP_dirreport');
		
		if($cekjava){
			$irep = $bcreportdir.'\rpt_mts_ss_sc.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("inputdata1", "This is a test string");
			$params->put("tglawal", $tgawal);
			$params->put("tglakhir", $tgakhir);
			$params->put("periode", $periodetgawal.' S/D '.$periodetgakhir );
			$params->put("nama_perusahaan", "PT. EDS MANUFACTURING INDONESIA");
		//	$params->put("number", 3.00);
			
			
			
			$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($jdburl, $db['default']['username'], $db['default']['password']);
			
			$jasperPrint = $fillManager->fillReport($report, $params, $conn);
			$outputPath = realpath(".")."\ireport/"."rpt_scr.pdf";
			
			switch ($format) {
			case 'pdf':
				
				$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
				$exportManager->exportReportToPdfFile($jasperPrint, $outputPath);
				header("Content-type: application/pdf");
				//header("Content-Disposition: attachment; filename=output.pdf"); //langsung dowload
				//$exporter->exportReport();
				readfile($outputPath);
				@unlink($outputPath);
				
				break;
			case 'rtf':
				try {
					$exporter = new java("net.sf.jasperreports.engine.export.JRRtfExporter");
					$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
					$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
				} catch (JavaException $ex) {
					echo $ex;
				}

				header("Content-type: application/rtf");
				header("Content-Disposition: attachment; filename=output.rtf");
				
				
				$exporter->exportReport();
				readfile($outputPath);
				
				@unlink($outputPath);
				break;
					
			}
		}		
	
	
	}
	
	function printxlsmutasibahanbakudanbahanpenolong(){
		$tglawalbb= isset($_GET['tglawalbb'])?$_GET['tglawalbb']:'01/01/2013';
		$tglakhirbb= isset($_GET['tglakhirbb'])?$_GET['tglakhirbb']:'01/01/2013';
		$format= isset($_GET['format'])?$_GET['format']:'pdf';
		
		$tglawalr =  $tglawalbb;	
		$tglakhirr = $tglakhirbb;	
		
		
		$tglawalbb = explode("/", $tglawalbb);	
		$tglakhirbb = explode("/", $tglakhirbb);	
		
		
		$tgawal =  $tglawalbb[1].'/'.$tglawalbb[0].'/'.$tglawalbb[2];
		$tgakhir = $tglakhirbb[1].'/'.$tglakhirbb[0].'/'.$tglakhirbb[2];
		
		$this->load->helper('afunction_helper');
		//echo fbulan($tglawalbb[1]);
		//echo fbulan($tglakhirbb[1]);
		
		$periodetgawal = $tglawalbb[0].' - '.fbulan($tglawalbb[1]).' - '.$tglawalbb[2];
		$periodetgakhir = $tglakhirbb[0].' - '.fbulan($tglakhirbb[1]).' - '.$tglakhirbb[2];
		$periode = $periodetgawal . ' S/D ' . $periodetgakhir ;
			$query = $this->db->query("
			select row
			, kodebarang
			, namabarang
			,qty_saldoawal
			,qty_in
			,qty_out
			,saldo_akhir
			,unit
			,stock_opname
			, adj from (
		 	  select row_number() OVER (ORDER BY kodebarang) AS row
			 , kodebarang
			 , namabarang
			 , qty_saldoawal
			 , qty_in
			 , qty_out
			 , qty_penyesuaian as adj
			 , saldo_akhir
			 , stock_opname
			 , selisih
			 , unit
				FROM            dbo.gab_rpt_mutasi_bahanbaku('$tgawal','$tgakhir', '', '')
 			) x 
			order by x.kodebarang asc 
		");
		 
		
		$row = $query->result_array();
		
		
		$qsto = $this->db->query("
			if
			 (select count(*) from (select top 1 convert(varchar, date_stock_opname, 103) as date_stock_opname from dbo.tbl_stock_opname 
						where date_stock_opname between '$tgawal' and '$tgakhir' and typebahan = 'RM'
						order by date_stock_opname desc
			) xx) > 0
			begin
			 select top 1 convert(varchar,  max(date_stock_opname), 103) as date_stock_opname from dbo.tbl_stock_opname 
						where date_stock_opname between '$tgawal' and '$tgakhir' and typebahan = 'RM'
						order by date_stock_opname desc
			end
			else
			begin
			 select top 1 convert(varchar,  max(date_stock_opname), 103) as date_stock_opname from dbo.tbl_stock_opname 
						where date_stock_opname <= '$tgawal' and typebahan = 'RM'
						order by date_stock_opname desc
			end
		");
		
		$rsto = $qsto->result_array();
		$dtsto=$tgawal;
		
		foreach ($rsto as $rsto){
			$dtsto=$rsto['date_stock_opname'];
		}
		
		include_once ( APPPATH."libraries/PHPExcel.php");	
		include_once ( APPPATH."libraries/PHPExcel/IOFactory.php");	
		$phpExcel = new PHPExcel();
		$objTpl = PHPExcel_IOFactory::load(APPPATH."libraries/PHPExcel/templates/pemi_report_4bulan_bb.xls");	
		$objTpl->setActiveSheetIndex(0);  //set first sheet as active
		
		
		$i = 1;
		$j = '';
		$k = 11;
		$l = 0;
		$m = '';
		$n = ''; $o = 12;
		foreach ($row as $r ){
			$k = 11 + $i;
			$j = "$k";
			$o = 12+$i;
			$n = "$o";
			$objTpl->getActiveSheet()->setCellValue("A".$j, $i);
 			$objTpl->getActiveSheet()->setCellValueExplicit("B".$j, $r['kodebarang'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValue("C".$j, $r['namabarang']);
			$objTpl->getActiveSheet()->setCellValue("D".$j, $r['unit']);
			$objTpl->getActiveSheet()->setCellValue("E".$j, $r['qty_saldoawal']);
			$objTpl->getActiveSheet()->setCellValue("F".$j, $r['qty_in']);
			$objTpl->getActiveSheet()->setCellValue("G".$j, $r['qty_out']);
			$objTpl->getActiveSheet()->setCellValue("H".$j, $r['adj']); //adjusment
			$objTpl->getActiveSheet()->setCellValue("I".$j, $r['saldo_akhir']);
			$objTpl->getActiveSheet()->setCellValue("J".$j, $r['stock_opname']); //stok opname
			$objTpl->getActiveSheet()->setCellValue("K".$j, $r['selisih']); //selisih
			$objTpl->getActiveSheet()->setCellValue("L".$j, '-');  //ket
			
			$i++;	
		}
		
		$objTpl->getActiveSheet()->getStyle("A12:"."A".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objTpl->getActiveSheet()->getStyle("B12:"."B".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objTpl->getActiveSheet()->getStyle("C12:"."C".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
		$objTpl->getActiveSheet()->getStyle("D12:"."D".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		$objTpl->getActiveSheet()->getStyle('A12:L12')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$objTpl->getActiveSheet()->getStyle("A12:"."A".$n)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("A12:"."A".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("C12:"."C".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("D12:"."D".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("E12:"."E".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("F12:"."F".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("G12:"."G".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("H12:"."H".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("I12:"."I".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("J12:"."J".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("K12:"."K".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$objTpl->getActiveSheet()->getStyle("B12:"."B".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("L12:"."L".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$objTpl->getActiveSheet()->getStyle("A".$j. ":" . "L" . $j)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
		
				
		$objTpl->getActiveSheet()->getStyle("E12:"."E".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("F12:"."F".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("G12:"."G".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("H12:"."H".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("I12:"."I".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("J12:"."J".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("K12:"."K".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		
		$objTpl->getActiveSheet()->getStyle("A".$n. ":" . "L" . $n)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_DOUBLE);
		$objTpl->getActiveSheet()->setCellValue("C".$n, 'TOTAL');  //ket
		$objTpl->getActiveSheet()->getStyle("C".$n)->getFont()->setName('Book Antiqua');	
		$objTpl->getActiveSheet()->getStyle("C".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		$t = "=SUM(E12:E$k)";
		$u = "=SUM(F12:F$k)";
		$u1 = "=SUM(G12:G$k)";
		$u2 = "=SUM(H12:H$k)";
		$u3 = "=SUM(I12:I$k)";
		$u4 = "=SUM(J12:J$k)";
		$u5 = "=SUM(K12:K$k)";
		$objTpl->getActiveSheet()->setCellValue("E".$n, $t);
		$objTpl->getActiveSheet()->getStyle("E".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("E".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		$objTpl->getActiveSheet()->setCellValue("F".$n, $u);
		$objTpl->getActiveSheet()->getStyle("F".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("F".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("G".$n, $u1);
		$objTpl->getActiveSheet()->getStyle("G".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("G".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("H".$n, $u2);
		$objTpl->getActiveSheet()->getStyle("H".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("H".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("I".$n, $u3);
		$objTpl->getActiveSheet()->getStyle("I".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("I".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("J".$n, $u4);
		$objTpl->getActiveSheet()->getStyle("J".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("J".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("K".$n, $u5);
		$objTpl->getActiveSheet()->getStyle("K".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("K".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$datenow = date('Ymd');
		$filename =  "REPORT_". "4BULAN_PEMI_BAHANBAKUDANPENOLONG_"  . $datenow .".xls";
		
		$objTpl->getActiveSheet()->setCellValue('C5', ':     '.$periode . " (2)");
		$objTpl->getActiveSheet()->setCellValue('E9', ' '.$tglawalr);
		$objTpl->getActiveSheet()->setCellValue('I9', ' '.$tglakhirr);
		$objTpl->getActiveSheet()->setCellValue('J9', ' '.$dtsto);
		
				
		header("Content-type:application/vnd.ms-excel");
		header("Content-disposition:attachment; filename=$filename"); 
		header("Pragma:no-cache");
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		header("Expires:0");

		$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
		$objWriter->save('php://output');  //send it to user, of course you can save it to disk also!
		 
		exit; //done.. exiting!
		
		//echo $periode;
	
	}
	
	function printxlsmutasibahanjadi(){
		
		$tglawalbb= isset($_GET['tglawalbj'])?$_GET['tglawalbj']:'01/01/2013';
		$tglakhirbb= isset($_GET['tglakhirbj'])?$_GET['tglakhirbj']:'01/01/2013';
		$format= isset($_GET['format'])?$_GET['format']:'pdf';
		
		$tglawalr =  $tglawalbb;	
		$tglakhirr = $tglakhirbb;	
		
		
		$tglawalbb = explode("/", $tglawalbb);	
		$tglakhirbb = explode("/", $tglakhirbb);	
		
		
		$tgawal =  $tglawalbb[1].'/'.$tglawalbb[0].'/'.$tglawalbb[2];
		$tgakhir = $tglakhirbb[1].'/'.$tglakhirbb[0].'/'.$tglakhirbb[2];
		
		$this->load->helper('afunction_helper');
		//echo fbulan($tglawalbb[1]);
		//echo fbulan($tglakhirbb[1]);
		
		$periodetgawal = $tglawalbb[0].' - '.fbulan($tglawalbb[1]).' - '.$tglawalbb[2];
		$periodetgakhir = $tglakhirbb[0].' - '.fbulan($tglakhirbb[1]).' - '.$tglakhirbb[2];
		$periode = $periodetgawal . ' S/D ' . $periodetgakhir ;
		

			$query = $this->db->query("
			
			select * from (
			select row_number() OVER (ORDER BY part_no) AS row, part_no,part_name,stock_awal,qty_in,qty_out,sisa,unit,qty_in_by_date,qty_out_by_date,sisa_by_date,stock_opname,adj ,selisih
			FROM            dbo.gab_rpt_mutasi_barangjadi('$tgawal','$tgakhir', '', '')
 		
			) x 
			order by x.part_no asc
		
			
			
		
		");
			// $query = $this->db->query("
			
			// select * from (
			// select row_number() OVER (ORDER BY part_no) AS row, 
			// part_no,
			// part_name,
			// stock_awal,
			// qty_in,
			// qty_out,
			// sisa,
			// unit,
			// qty_in_by_date,
			// qty_out_by_date,
			// sisa_by_date,
			// stock_opname,
			// adj		
			// from [dbo].[f_mutasi_fg]('$tgawal','$tgakhir','FG')  
			// --where (qty_in <> 0 and qty_out <> 0) or stock_awal <> 0
			// where (qty_in <> 0 or qty_out <> 0 or stock_awal <> 0) 
			// ) x 
			// order by x.part_no asc
		
			
			
		
		// ");
		
		$row = $query->result_array();
		
		
		$qsto = $this->db->query("
			if
			 (select count(*) from (select top 1 convert(varchar, date_stock_opname, 103) as date_stock_opname from dbo.tbl_stock_opname 
						where date_stock_opname between '$tgawal' and '$tgakhir' and typebahan = 'FG'
						order by date_stock_opname desc
			) xx) > 0
			begin
			 select top 1 convert(varchar,  max(date_stock_opname), 103) as date_stock_opname from dbo.tbl_stock_opname 
						where date_stock_opname between '$tgawal' and '$tgakhir' and typebahan = 'FG'
						order by date_stock_opname desc
			end
			else
			begin
			 select top 1 convert(varchar,  max(date_stock_opname), 103) as date_stock_opname from dbo.tbl_stock_opname 
						where date_stock_opname <= '$tgawal' and typebahan = 'FG'
						order by date_stock_opname desc
			end
		");
		
		$rsto = $qsto->result_array();
		$dtsto=$tgawal;
		
		foreach ($rsto as $rsto){
			$dtsto=$rsto['date_stock_opname'];
		}
		
		include_once ( APPPATH."libraries/PHPExcel.php");	
		include_once ( APPPATH."libraries/PHPExcel/IOFactory.php");	
		$phpExcel = new PHPExcel();
		$objTpl = PHPExcel_IOFactory::load(APPPATH."libraries/PHPExcel/templates/pemi_report_4bulan_bj.xls");	
		$objTpl->setActiveSheetIndex(0);  //set first sheet as active
		
		
		$i = 1;
		$j = '';
		$k = 11;
		$l = 0;
		$m = '';
		$n = ''; $o = 12;
		foreach ($row as $r ){
			$k = 11 + $i;
			$j = "$k";
			$o = 12+$i;
			$n = "$o";
			$objTpl->getActiveSheet()->setCellValue("A".$j, $i);
			$objTpl->getActiveSheet()->setCellValueExplicit("B".$j, $r['part_no'], PHPExcel_Cell_DataType::TYPE_STRING);
			$objTpl->getActiveSheet()->setCellValue("C".$j, $r['part_name']);
			$objTpl->getActiveSheet()->setCellValue("D".$j, $r['unit']);
			$objTpl->getActiveSheet()->setCellValue("E".$j, $r['stock_awal']);
			$objTpl->getActiveSheet()->setCellValue("F".$j, $r['qty_in']);
			$objTpl->getActiveSheet()->setCellValue("G".$j, $r['qty_out']);
			$objTpl->getActiveSheet()->setCellValue("H".$j, $r['adj']); //adjusment
			$objTpl->getActiveSheet()->setCellValue("I".$j, $r['sisa_by_date']);
			$objTpl->getActiveSheet()->setCellValue("J".$j, $r['stock_opname']); //stok opname
			$objTpl->getActiveSheet()->setCellValue("K".$j, $r['selisih']); //selisih
			$objTpl->getActiveSheet()->setCellValue("L".$j, '-');  //ket
			
			$i++;	
		}
	 	$objTpl->getActiveSheet()->getStyle("A12:"."A".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objTpl->getActiveSheet()->getStyle("B12:"."B".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objTpl->getActiveSheet()->getStyle("C12:"."C".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
		$objTpl->getActiveSheet()->getStyle("D12:"."D".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		$objTpl->getActiveSheet()->getStyle('A12:L12')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$objTpl->getActiveSheet()->getStyle("A12:"."A".$n)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("A12:"."A".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("C12:"."C".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("D12:"."D".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("E12:"."E".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("F12:"."F".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("G12:"."G".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("H12:"."H".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("I12:"."I".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("J12:"."J".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("K12:"."K".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$objTpl->getActiveSheet()->getStyle("B12:"."B".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("L12:"."L".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$objTpl->getActiveSheet()->getStyle("A".$j. ":" . "L" . $j)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
		
				
		$objTpl->getActiveSheet()->getStyle("E12:"."E".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("F12:"."F".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("G12:"."G".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("H12:"."H".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("I12:"."I".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("J12:"."J".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("K12:"."K".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		
		$objTpl->getActiveSheet()->getStyle("A".$n. ":" . "L" . $n)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_DOUBLE);
		$objTpl->getActiveSheet()->setCellValue("C".$n, 'TOTAL');  //ket
		$objTpl->getActiveSheet()->getStyle("C".$n)->getFont()->setName('Book Antiqua');	
		$objTpl->getActiveSheet()->getStyle("C".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		$t = "=SUM(E12:E$k)";
		$u = "=SUM(F12:F$k)";
		$u1 = "=SUM(G12:G$k)";
		$u2 = "=SUM(H12:H$k)";
		$u3 = "=SUM(I12:I$k)";
		$u4 = "=SUM(J12:J$k)";
		$u5 = "=SUM(K12:K$k)";
		
		  
	
		$objTpl->getActiveSheet()->setCellValue("E".$n, $t);
		$objTpl->getActiveSheet()->getStyle("E".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("E".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		$objTpl->getActiveSheet()->setCellValue("F".$n, $u);
		$objTpl->getActiveSheet()->getStyle("F".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("F".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("G".$n, $u1);
		$objTpl->getActiveSheet()->getStyle("G".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("G".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("H".$n, $u2);
		$objTpl->getActiveSheet()->getStyle("H".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("H".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("I".$n, $u3);
		$objTpl->getActiveSheet()->getStyle("I".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("I".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("J".$n, $u4);
		$objTpl->getActiveSheet()->getStyle("J".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("J".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("K".$n, $u5);
		$objTpl->getActiveSheet()->getStyle("K".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("K".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
	
		$datenow = date('Ymd');
		$filename =  "REPORT_". "4BULAN_PEMI_BAHANJADI_"  . $datenow .".xls";
		
		$objTpl->getActiveSheet()->setCellValue('C5', ':     '.$periode . " (2)");
		$objTpl->getActiveSheet()->setCellValue('E9', ' '.$tglawalr);
		$objTpl->getActiveSheet()->setCellValue('I9', ' '.$tglakhirr);
		$objTpl->getActiveSheet()->setCellValue('J9', ' '.$dtsto);
		
				
		header("Content-type:application/vnd.ms-excel");
		header("Content-disposition:attachment; filename=$filename"); 
		header("Pragma:no-cache");
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		header("Expires:0");

		$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
		$objWriter->save('php://output');  //send it to user, of course you can save it to disk also!
		 
		exit; //done.. exiting!
		
	
	
	
	}
	
	function printxlsmutasiscr(){
		
		$tglawalbb= isset($_GET['tglawalscr'])?$_GET['tglawalscr']:'01/01/2013';
		$tglakhirbb= isset($_GET['tglakhirscr'])?$_GET['tglakhirscr']:'01/01/2013';
		$format= isset($_GET['format'])?$_GET['format']:'pdf';
		
		$tglawalr =  $tglawalbb;	
		$tglakhirr = $tglakhirbb;	
		
		
		$tglawalbb = explode("/", $tglawalbb);	
		$tglakhirbb = explode("/", $tglakhirbb);	
		
		
		$tgawal =  $tglawalbb[1].'/'.$tglawalbb[0].'/'.$tglawalbb[2];
		$tgakhir = $tglakhirbb[1].'/'.$tglakhirbb[0].'/'.$tglakhirbb[2];
		
		$this->load->helper('afunction_helper');
		//echo fbulan($tglawalbb[1]);
		//echo fbulan($tglakhirbb[1]);
		
		$periodetgawal = $tglawalbb[0].' - '.fbulan($tglawalbb[1]).' - '.$tglawalbb[2];
		$periodetgakhir = $tglakhirbb[0].' - '.fbulan($tglakhirbb[1]).' - '.$tglakhirbb[2];
		$periode = $periodetgawal . ' S/D ' . $periodetgakhir ;
		
		$query = $this->db->query("
			
				select 
			row
			,kodebarang
			,namabarang
			,qty_saldoawal
			,qty_in
			,qty_out
			,saldo_akhir
			,unit
			,stock_opname
			, adj 
			,selisih 
			  from (
		 	  select row_number() OVER (ORDER BY kodebarang) AS row
			 , kodebarang
			 , namabarang
			 , qty_saldoawal
			 , qty_in
			 , qty_out
			 , qty_penyesuaian as adj
			 , saldo_akhir
			 , stock_opname
			 , selisih
			 , unit
				FROM            dbo.gab_rpt_mutasi_scrap('$tgawal','$tgakhir', '', '')
 			) x
			order by x.kodebarang asc 
		");
		
		$row = $query->result_array();
		
		
		$qsto = $this->db->query("
			if
			 (select count(*) from (select top 1 convert(varchar, date_stock_opname, 103) as date_stock_opname from dbo.tbl_stock_opname 
						where date_stock_opname between '$tgawal' and '$tgakhir' and typebahan = 'SS'
						order by date_stock_opname desc
			) xx) > 0
			begin
			 select top 1 convert(varchar,  max(date_stock_opname), 103) as date_stock_opname from dbo.tbl_stock_opname 
						where date_stock_opname between '$tgawal' and '$tgakhir' and typebahan = 'SS'
						order by date_stock_opname desc
			end
			else
			begin
			 select top 1 convert(varchar,  max(date_stock_opname), 103) as date_stock_opname from dbo.tbl_stock_opname 
						where date_stock_opname <= '$tgawal' and typebahan = 'SS'
						order by date_stock_opname desc
			end
		");
		
		$rsto = $qsto->result_array();
		$dtsto=$tgawal;
		
		foreach ($rsto as $rsto){
			$dtsto=$rsto['date_stock_opname'];
		}
		
		include_once ( APPPATH."libraries/PHPExcel.php");	
		include_once ( APPPATH."libraries/PHPExcel/IOFactory.php");	
		$phpExcel = new PHPExcel();
		$objTpl = PHPExcel_IOFactory::load(APPPATH."libraries/PHPExcel/templates/pemi_report_4bulan_scr.xls");	
		$objTpl->setActiveSheetIndex(0);  //set first sheet as active
		
		
		$i = 1;
		$j = '';
		$k = 11;
		$l = 0;
		$m = '';
		$n = ''; $o = 12;
		foreach ($row as $r ){
			$k = 11 + $i;
			$j = "$k";
			$o = 12+$i;
			$n = "$o";
			$objTpl->getActiveSheet()->setCellValue("A".$j, $i);
			$objTpl->getActiveSheet()->setCellValueExplicit("B".$j, $r['kodebarang'], PHPExcel_Cell_DataType::TYPE_STRING);
 			$objTpl->getActiveSheet()->setCellValue("C".$j, $r['namabarang']);
			$objTpl->getActiveSheet()->setCellValue("D".$j, $r['unit']);
			$objTpl->getActiveSheet()->setCellValue("E".$j, $r['qty_saldoawal']);
			$objTpl->getActiveSheet()->setCellValue("F".$j, $r['qty_in']);
			$objTpl->getActiveSheet()->setCellValue("G".$j, $r['qty_out']);
			$objTpl->getActiveSheet()->setCellValue("H".$j, $r['adj']); //adjusment
			$objTpl->getActiveSheet()->setCellValue("I".$j, $r['saldo_akhir']);
			$objTpl->getActiveSheet()->setCellValue("J".$j, $r['stock_opname']); //stok opname
			$objTpl->getActiveSheet()->setCellValue("K".$j, $r['selisih']); //selisih
			$objTpl->getActiveSheet()->setCellValue("L".$j, '-');  //ket
			
			$i++;	
		}
		
		$objTpl->getActiveSheet()->getStyle("A12:"."A".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objTpl->getActiveSheet()->getStyle("B12:"."B".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objTpl->getActiveSheet()->getStyle("C12:"."C".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
		$objTpl->getActiveSheet()->getStyle("D12:"."D".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		$objTpl->getActiveSheet()->getStyle('A12:L12')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$objTpl->getActiveSheet()->getStyle("A12:"."A".$n)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("A12:"."A".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("C12:"."C".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("D12:"."D".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("E12:"."E".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("F12:"."F".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("G12:"."G".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("H12:"."H".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("I12:"."I".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("J12:"."J".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("K12:"."K".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$objTpl->getActiveSheet()->getStyle("B12:"."B".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("L12:"."L".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$objTpl->getActiveSheet()->getStyle("A".$j. ":" . "L" . $j)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
		
				
		$objTpl->getActiveSheet()->getStyle("E12:"."E".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("F12:"."F".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("G12:"."G".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("H12:"."H".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("I12:"."I".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("J12:"."J".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("K12:"."K".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		
		$objTpl->getActiveSheet()->getStyle("A".$n. ":" . "L" . $n)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_DOUBLE);
		$objTpl->getActiveSheet()->setCellValue("C".$n, 'TOTAL');  //ket
		$objTpl->getActiveSheet()->getStyle("C".$n)->getFont()->setName('Book Antiqua');	
		$objTpl->getActiveSheet()->getStyle("C".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		$t = "=SUM(E12:E$k)";
		$u = "=SUM(F12:F$k)";
		$u1 = "=SUM(G12:G$k)";
		$u2 = "=SUM(H12:H$k)";
		$u3 = "=SUM(I12:I$k)";
		$u4 = "=SUM(J12:J$k)";
		$u5 = "=SUM(K12:K$k)";
		$objTpl->getActiveSheet()->setCellValue("E".$n, $t);
		$objTpl->getActiveSheet()->getStyle("E".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("E".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		$objTpl->getActiveSheet()->setCellValue("F".$n, $u);
		$objTpl->getActiveSheet()->getStyle("F".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("F".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("G".$n, $u1);
		$objTpl->getActiveSheet()->getStyle("G".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("G".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("H".$n, $u2);
		$objTpl->getActiveSheet()->getStyle("H".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("H".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("I".$n, $u3);
		$objTpl->getActiveSheet()->getStyle("I".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("I".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("J".$n, $u4);
		$objTpl->getActiveSheet()->getStyle("J".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("J".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("K".$n, $u5);
		$objTpl->getActiveSheet()->getStyle("K".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("K".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$datenow = date('Ymd');
		$filename =  "REPORT_". "4BULAN_PEMI_SCRAPSISA_"  . $datenow .".xls";
		
		$objTpl->getActiveSheet()->setCellValue('C5', ':     '.$periode . " (2)");
		$objTpl->getActiveSheet()->setCellValue('E9', ' '.$tglawalr);
		$objTpl->getActiveSheet()->setCellValue('I9', ' '.$tglakhirr);
		$objTpl->getActiveSheet()->setCellValue('J9', ' '.$dtsto);
		
				
		header("Content-type:application/vnd.ms-excel");
		header("Content-disposition:attachment; filename=$filename"); 
		header("Pragma:no-cache");
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		header("Expires:0");

		$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
		$objWriter->save('php://output');  //send it to user, of course you can save it to disk also!
		 
		exit; //done.. exiting!
		
	
	
	
	}
	
	function printxlsmutasimpk(){
		$tglawalbb= isset($_GET['tglawalmpk'])?$_GET['tglawalmpk']:'01/01/2013';
		$tglakhirbb= isset($_GET['tglakhirmpk'])?$_GET['tglakhirmpk']:'01/01/2013';
		$format= isset($_GET['format'])?$_GET['format']:'pdf';
		
		$tglawalr =  $tglawalbb;	
		$tglakhirr = $tglakhirbb;	
		
		
		$tglawalbb = explode("/", $tglawalbb);	
		$tglakhirbb = explode("/", $tglakhirbb);	
		
		
		$tgawal =  $tglawalbb[1].'/'.$tglawalbb[0].'/'.$tglawalbb[2];
		$tgakhir = $tglakhirbb[1].'/'.$tglakhirbb[0].'/'.$tglakhirbb[2];
		
		$this->load->helper('afunction_helper');
		//echo fbulan($tglawalbb[1]);
		//echo fbulan($tglakhirbb[1]);
		
		$periodetgawal = $tglawalbb[0].' - '.fbulan($tglawalbb[1]).' - '.$tglawalbb[2];
		$periodetgakhir = $tglakhirbb[0].' - '.fbulan($tglakhirbb[1]).' - '.$tglakhirbb[2];
		$periode = $periodetgawal . ' S/D ' . $periodetgakhir ;
		
		$query = $this->db->query("
			
			select 
			row
			,kodebarang
			,namabarang
			,qty_saldoawal
			,qty_in
			,qty_out
			,saldo_akhir
			,unit
			,stock_opname
			, adj 
			,selisih 
			  from (
		 	  select row_number() OVER (ORDER BY kodebarang) AS row
			 , kodebarang
			 , namabarang
			 , qty_saldoawal
			 , qty_in
			 , qty_out
			 , qty_penyesuaian as adj
			 , saldo_akhir
			 , stock_opname
			 , selisih
			 , unit
				FROM            dbo.gab_rpt_mutasi_mesin('$tgawal','$tgakhir', '', '')
 			) x
			order by x.kodebarang asc
		
		
			
					
			
		
		");
		
		$row = $query->result_array();
		
		
		$qsto = $this->db->query("
			if
			 (select count(*) from (select top 1 convert(varchar, date_stock_opname, 103) as date_stock_opname from dbo.tbl_stock_opname 
						where date_stock_opname between '$tgawal' and '$tgakhir' and typebahan = 'MPK'
						order by date_stock_opname desc
			) xx) > 0
			begin
			 select top 1 convert(varchar, max(date_stock_opname), 103) as date_stock_opname from dbo.tbl_stock_opname 
						where date_stock_opname between '$tgawal' and '$tgakhir' and typebahan = 'MPK'
						order by date_stock_opname desc
			end
			else
			begin
			 select top 1 convert(varchar, max(date_stock_opname), 103) as date_stock_opname from dbo.tbl_stock_opname 
						where date_stock_opname <= '$tgawal' and typebahan = 'MPK'
						order by date_stock_opname desc
			end
		");
		
		$rsto = $qsto->result_array();
		$dtsto=$tgawal;
		
		foreach ($rsto as $rsto){
			$dtsto=$rsto['date_stock_opname'];
		}
		
		include_once ( APPPATH."libraries/PHPExcel.php");	
		include_once ( APPPATH."libraries/PHPExcel/IOFactory.php");	
		$phpExcel = new PHPExcel();
		$objTpl = PHPExcel_IOFactory::load(APPPATH."libraries/PHPExcel/templates/pemi_report_4bulan_mpk.xls");	
		$objTpl->setActiveSheetIndex(0);  //set first sheet as active
		
		
		$i = 1;
		$j = '';
		$k = 11;
		$l = 0;
		$m = '';
		$n = ''; $o = 12;
		foreach ($row as $r ){
			$k = 11 + $i;
			$j = "$k";
			$o = 12+$i;
			$n = "$o";
			$objTpl->getActiveSheet()->setCellValue("A".$j, $i);
			$objTpl->getActiveSheet()->setCellValueExplicit("B".$j, $r['kodebarang'], PHPExcel_Cell_DataType::TYPE_STRING);
 			$objTpl->getActiveSheet()->setCellValue("C".$j, $r['namabarang']);
			$objTpl->getActiveSheet()->setCellValue("D".$j, $r['unit']);
			$objTpl->getActiveSheet()->setCellValue("E".$j, $r['qty_saldoawal']);
			$objTpl->getActiveSheet()->setCellValue("F".$j, $r['qty_in']);
			$objTpl->getActiveSheet()->setCellValue("G".$j, $r['qty_out']);
			$objTpl->getActiveSheet()->setCellValue("H".$j, $r['adj']); //adjusment
			$objTpl->getActiveSheet()->setCellValue("I".$j, $r['saldo_akhir']);
			$objTpl->getActiveSheet()->setCellValue("J".$j, $r['stock_opname']); //stok opname
			$objTpl->getActiveSheet()->setCellValue("K".$j, $r['selisih']); //selisih
			$objTpl->getActiveSheet()->setCellValue("L".$j, '-');  //ket
			
			$i++;	
		}
		
		$objTpl->getActiveSheet()->getStyle("A12:"."A".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objTpl->getActiveSheet()->getStyle("B12:"."B".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objTpl->getActiveSheet()->getStyle("C12:"."C".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
		$objTpl->getActiveSheet()->getStyle("D12:"."D".$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		$objTpl->getActiveSheet()->getStyle('A12:L12')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$objTpl->getActiveSheet()->getStyle("A12:"."A".$n)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("A12:"."A".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("C12:"."C".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("D12:"."D".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("E12:"."E".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("F12:"."F".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("G12:"."G".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("H12:"."H".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("I12:"."I".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("J12:"."J".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("K12:"."K".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$objTpl->getActiveSheet()->getStyle("B12:"."B".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		$objTpl->getActiveSheet()->getStyle("L12:"."L".$n)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
		
		$objTpl->getActiveSheet()->getStyle("A".$j. ":" . "L" . $j)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
		
				
		$objTpl->getActiveSheet()->getStyle("E12:"."E".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("F12:"."F".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("G12:"."G".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("H12:"."H".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("I12:"."I".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("J12:"."J".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("K12:"."K".$j)->getNumberFormat()->setFormatCode('#,##0.00');
		
		$objTpl->getActiveSheet()->getStyle("A".$n. ":" . "L" . $n)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_DOUBLE);
		$objTpl->getActiveSheet()->setCellValue("C".$n, 'TOTAL');  //ket
		$objTpl->getActiveSheet()->getStyle("C".$n)->getFont()->setName('Book Antiqua');	
		$objTpl->getActiveSheet()->getStyle("C".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		$t = "=SUM(E12:E$k)";
		$u = "=SUM(F12:F$k)";
		$u1 = "=SUM(G12:G$k)";
		$u2 = "=SUM(H12:H$k)";
		$u3 = "=SUM(I12:I$k)";
		$u4 = "=SUM(J12:J$k)";
		$u5 = "=SUM(K12:K$k)";
		$objTpl->getActiveSheet()->setCellValue("E".$n, $t);
		$objTpl->getActiveSheet()->getStyle("E".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("E".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		$objTpl->getActiveSheet()->setCellValue("F".$n, $u);
		$objTpl->getActiveSheet()->getStyle("F".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("F".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("G".$n, $u1);
		$objTpl->getActiveSheet()->getStyle("G".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("G".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("H".$n, $u2);
		$objTpl->getActiveSheet()->getStyle("H".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("H".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("I".$n, $u3);
		$objTpl->getActiveSheet()->getStyle("I".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("I".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("J".$n, $u4);
		$objTpl->getActiveSheet()->getStyle("J".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("J".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$objTpl->getActiveSheet()->setCellValue("K".$n, $u5);
		$objTpl->getActiveSheet()->getStyle("K".$n)->getNumberFormat()->setFormatCode('#,##0.00');
		$objTpl->getActiveSheet()->getStyle("K".$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
		
		
		$datenow = date('Ymd');
		$filename =  "REPORT_". "4BULAN_PEMI_MESINPK_"  . $datenow .".xls";
		
		$objTpl->getActiveSheet()->setCellValue('C5', ':     '.$periode . " (2)");
		$objTpl->getActiveSheet()->setCellValue('E9', ' '.$tglawalr);
		$objTpl->getActiveSheet()->setCellValue('I9', ' '.$tglakhirr);
		$objTpl->getActiveSheet()->setCellValue('J9', ' '.$dtsto);
		
				
		header("Content-type:application/vnd.ms-excel");
		header("Content-disposition:attachment; filename=$filename"); 
		header("Pragma:no-cache");
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		header("Expires:0");

		$objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
		$objWriter->save('php://output');  //send it to user, of course you can save it to disk also!
		 
		exit; //done.. exiting!
		
	}
	
	function tglstockopname(){
		$tglawalbb = (isset($_POST['tglawalbb']) && !empty($_POST['tglawalbb']))?$_POST['tglawalbb']:date('m/d/Y');
		$tglakhirbb = (isset($_POST['tglakhirbb']) && !empty($_POST['tglakhirbb']))?$_POST['tglakhirbb']:date('m/d/Y');
		$typebahan = (isset($_POST['typebahan']) && !empty($_POST['typebahan']))?$_POST['typebahan']:'RM';
		
		
		$tglawalbb = explode("/", $tglawalbb);	
		$tglakhirbb = explode("/", $tglakhirbb);	
		
		
		$tgawal =  $tglawalbb[1].'/'.$tglawalbb[0].'/'.$tglawalbb[2];
		$tgakhir = $tglakhirbb[1].'/'.$tglakhirbb[0].'/'.$tglakhirbb[2];
		
		$q = $this->db->query("
			if
			 (select count(*) from (select top 1 convert(varchar, date_stock_opname, 103) as date_stock_opname from dbo.tbl_stock_opname 
						where date_stock_opname between '$tgawal' and '$tgakhir' and typebahan = '$typebahan'
						order by date_stock_opname desc
			) xx) > 0
			begin
			 select top 1 convert(varchar, max(date_stock_opname), 103) as date_stock_opname from dbo.tbl_stock_opname 
						where date_stock_opname between '$tgawal' and '$tgakhir' and typebahan = '$typebahan'
						order by date_stock_opname desc
			end
			else
			begin
			 select top 1 convert(varchar, max(date_stock_opname), 103) as date_stock_opname from dbo.tbl_stock_opname 
						where date_stock_opname <= '$tgawal' and typebahan = '$typebahan'
						order by date_stock_opname desc
			end
		");
		
		/* echo "	select top 1 convert(varchar, date_stock_opname, 103) as date_stock_opname from dbo.tbl_stock_opname 
			where date_stock_opname <= '$tgawal'
			order by date_stock_opname desc "; die();
		 */
		
		$rs = $q->result_array();
		$xx=$tgawal;
		$responce = array("sts" => "00", "desc" => $tglawalbb); 
		
		foreach ($rs as $r){
			$xx=$r['date_stock_opname'];
		}
		
		
	    $responce = array("sts" => "00", "desc" => $xx);
		echo json_encode($responce);

	
	}
	
	
	

}

