<?php
require_once GTFWConfiguration::GetValue('application', 'docroot').'module/'.Dispatcher::Instance()->mModule.'/business/AppInventarisasi.class.php';
require_once GTFWConfiguration::GetValue('application', 'docroot').'main/function/date.php';

class ViewExcelLapIndukInventaris extends XlsxResponse
{
   public $Excel;
   var $sheet;
   var $maxExcelRows = 20000;
   
   function createNewSheet($sheetName,$sheetNum=0){
	   	// set sheet
	   	if($sheetNum <> 0) $this->Excel->createSheet(NULL,$sheetNum);
	   	$this->sheet = $this->Excel->setActiveSheetIndex($sheetNum);
	   	
	   	// set default font setting for document
	   	$this->sheet->getDefaultStyle()->getFont()->setName('Arial')->setSize('10');
	   	
	   	// setting paging
	   	$this->sheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
	   	$this->sheet->getPageSetup()->setFitToWidth(1);
	   	$this->sheet->getPageSetup()->setFitToHeight(0);
	   	$this->sheet->getPageSetup()->setHorizontalCentered(true);
	   	$this->sheet->getPageSetup()->setVerticalCentered(false);
	   	
	   	// set worksheet name
	   	$this->sheet->setTitle($sheetName);
	   	$this->sheet->setShowGridlines(false);
   }
   
   function initNewSheet($var){
	   	// set coloum
	   	$this->sheet->getColumnDimension('A')->setWidth('10');
	   	$this->sheet->getColumnDimension('B')->setWidth('20');
	   	$this->sheet->getColumnDimension('C')->setWidth('20');
	   	$this->sheet->getColumnDimension('D')->setWidth('20');
	   	$this->sheet->getColumnDimension('E')->setWidth('20');
	   	$this->sheet->getColumnDimension('F')->setWidth('20');
	   	$this->sheet->getColumnDimension('G')->setWidth('35');
	   	$this->sheet->getColumnDimension('H')->setWidth('50');
	   	$this->sheet->getColumnDimension('I')->setWidth('20');
	   	$this->sheet->getColumnDimension('J')->setWidth('20');
	   	$this->sheet->getColumnDimension('K')->setWidth('20');
	   	$this->sheet->getColumnDimension('L')->setWidth('20');
	   	$this->sheet->getColumnDimension('M')->setWidth('20');
	   	$this->sheet->getColumnDimension('N')->setWidth('20');
	   	
	   	# ====== HEADER STYLE ======= #
	   	$headerStyledArray = array(
	   			'font' => array(
	   					'bold' => true,
	   					'size' => '12'
	   			),
	   			'alignment' => array(
	   					'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
	   					'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
	   			)
	   	);
	   	
	   	$styledTableHeaderArray = array(
	   			'borders' => array(
	   					'allborders' => array(
	   							'style' => PHPExcel_Style_Border::BORDER_THIN,
	   							'color' => array('argb' => 'ff000000')
	   					)
	   			),
	   			'fill' => array(
	   					'type' => PHPExcel_Style_Fill::FILL_SOLID,
	   					'startcolor' => array(
	   							'argb' => 'ffcccccc'
	   					)
	   			),
	   			'font' => array(
	   					'bold' => true,
	   					'color' => array(
	   							'rgb' => '000000'
	   					)
	   			),
	   			'alignment' => array(
	   					'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
	   					'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
	   					'wrap' => true
	   			)
	   	);
	   	
	   	// header 1
	   	$rowOneTitle 		= "KEMENTERIAN PENDIDIKAN DAN KEBUDAYAAN";
	   	$rowTwoTitle 		= strtoupper(GTFWConfiguration::GetValue('application','company_name'));
	   	 
	   	$this->sheet->SetCellValue('A1', $rowOneTitle);
	   	$this->sheet->mergeCells('A1:G1');
	   	$this->sheet->SetCellValue('A2', $rowTwoTitle);
	   	$this->sheet->mergeCells('A2:G2');
	   	$this->sheet->SetCellValue('A4',GTFWConfiguration::GetValue('language','unit_pemakai_barang'));
	   	$this->sheet->mergeCells('A4:B4');
	   	$this->sheet->SetCellValue('C4',': '.$var['unitname']);
	   	$this->sheet->SetCellValue('A5',GTFWConfiguration::GetValue('language','tanggal_perolehan'));
	   	$this->sheet->mergeCells('A5:B5');
	   	$this->sheet->SetCellValue('C5',': '.IndonesianDate($var['tglAwal'], 'YYYY-MM-DD').' s.d '.IndonesianDate($var['tglAkhir'], 'YYYY-MM-DD'));
	   		
	   	// sub header 1
	   	$row1 		= GTFWConfiguration::GetValue('language','laporan_induk_inventaris');
	   	$this->sheet->SetCellValue('A7', $row1);
	   	$this->sheet->mergeCells('A7:M7');
	   	$this->sheet->getStyle('A7:M7')->applyFromArray($headerStyledArray);
	   	 
	   	// set Header Data Table
	   	$this->sheet->SetCellValue('A10',strtoupper(GTFWConfiguration::GetValue('language','no')));
	   	$this->sheet->mergeCells('A10:A11');
	   	$this->sheet->SetCellValue('B10',strtoupper(GTFWConfiguration::GetValue('language','kode_barang')));
	   	$this->sheet->mergeCells('B10:B11');
	   	$this->sheet->SetCellValue('C10',strtoupper(GTFWConfiguration::GetValue('language','nama_barang')));
	   	$this->sheet->mergeCells('C10:C11');
	   	$this->sheet->SetCellValue('D10',strtoupper(GTFWConfiguration::GetValue('language','keterangan_barang')));
	   	$this->sheet->mergeCells('D10:E10');
	   	$this->sheet->SetCellValue('D11',strtoupper(GTFWConfiguration::GetValue('language','merk')));
	   	$this->sheet->SetCellValue('E11',strtoupper(GTFWConfiguration::GetValue('language','tipe')));
	   	$this->sheet->SetCellValue('F10',strtoupper(GTFWConfiguration::GetValue('language','kode_aset')));
	   	$this->sheet->mergeCells('F10:F11');
	   	$this->sheet->SetCellValue('G10',strtoupper(GTFWConfiguration::GetValue('language','kode_lokasi')));
	   	$this->sheet->mergeCells('G10:G11');
	   	$this->sheet->SetCellValue('H10',strtoupper(GTFWConfiguration::GetValue('language','lokasi_aset')));
	   	$this->sheet->mergeCells('H10:H11');
	   	$this->sheet->SetCellValue('I10',strtoupper(GTFWConfiguration::GetValue('language','unit_pj')));
	   	$this->sheet->mergeCells('I10:I11');
	   	$this->sheet->SetCellValue('J10',strtoupper(GTFWConfiguration::GetValue('language','tanggal_perolehan')));
	   	$this->sheet->mergeCells('J10:J11');
	   	$this->sheet->SetCellValue('K10',strtoupper(GTFWConfiguration::GetValue('language','tanggal_pencatatan')));
	   	$this->sheet->mergeCells('K10:K11');
	   	$this->sheet->SetCellValue('L10',strtoupper(GTFWConfiguration::GetValue('language','nilai_perolehan_rp')));
	   	$this->sheet->mergeCells('L10:L11');
	   	$this->sheet->SetCellValue('M10',strtoupper(GTFWConfiguration::GetValue('language','asal').' '.GTFWConfiguration::GetValue('language','perolehan')));
	   	$this->sheet->mergeCells('M10:M11');
	   	$this->sheet->SetCellValue('N10',strtoupper(GTFWConfiguration::GetValue('language','kondisi')));
	   	$this->sheet->mergeCells('N10:N11');
	   	$this->sheet->getStyle('A10:N11')->applyFromArray($styledTableHeaderArray);
   }
   
   public function ProcessRequest()
   {
   	# increase execution time & memory
   	set_time_limit(0);
   	ini_set('memory_limit', '512M');
   	ini_set('max_execution_time', 600);
   	
      $Obj = new AppInventarisasi();
    
    if (!empty($_GET))
      {
      	$filter = $_GET->AsArray();
      	$tmp = explode('-',$filter['tglAwal']);
      	$filter['tanggal_awal_year'] = $tmp[0];
      	$filter['tanggal_awal_mon'] = $tmp[1];
      	$filter['tanggal_awal_day'] = $tmp[2];
      	 
      	$tmp = explode('-',$filter['tglAkhir']);
      	$filter['tanggal_akhir_year'] = $tmp[0];
      	$filter['tanggal_akhir_mon'] = $tmp[1];
      	$filter['tanggal_akhir_day'] = $tmp[2];
      	$data = $Obj->GetInventarisasi($filter,0,0,true);
      	$total_perolehan = $Obj->GetTotalPerolehan($filter);
      	$unit = $Obj->GetDataUnitById($filter['unit']);
      	$alamat = GTFWConfiguration::GetValue('organization','city');
      	$tglAwal = $filter['tglAwal'];
      	$tglAkhir = $filter['tglAkhir'];
      }
		
      // set file name
      $filename   = 'laporan_induk_inventaris_'.date('dmY').'.xls';
      $this->SetFileName($filename);
      
      // set metadata properties data excel
      $metadata   = $this->Excel->getProperties();
      $metadata->setCreator($_SESSION['username']);
      $metadata->setLastModifiedBy($_SESSION['username']);
      $metadata->setTitle("Laporan Induk Inventaris");
      $metadata->setSubject("induk_inventaris");
      $metadata->setDescription("Laporan Induk Inventaris printed on ".date('Y-m-d H:i:s', time()));
      $metadata->setKeywords("induk_inventaris");
      $metadata->setCategory("gtAset");
      
      $this->createNewSheet('Sheet 1');
      
      // generate value
      if(empty($data)){
         $this->sheet->setCellValue('A1','Data Tidak Ditemukan');
      }else{
         
      	# ==== DATA STYLE ==== #
         $tableCellStyledArray   = array(
            'borders' => array(
               'allborders' => array(
                  'style' => PHPExcel_Style_Border::BORDER_THIN, 
                  'color' => array('argb' => 'ff000000')
               )
            ),
				'alignment' => array(
               'wrap' => true
            )
         );

        $headerVar = array(
        				'unitname' => $unit['unitkerjaNama'],
        				'tglAwal' => $tglAwal,
        				'tglAkhir' => $tglAkhir
        				);
        
        $this->initNewSheet($headerVar);
        
		$rows = 12;$no = 1;
		$rowCount = 1;
		for ($i=0; $i<count($data); $i++){
			if($rowCount <= $this->maxExcelRows){
				$this->sheet->SetCellValue('A'.$rows, $no);
				$tmp = explode('|', $data[$i]['KIB']);
				$this->sheet->SetCellValue('B'.$rows, $tmp[1]);
				$this->sheet->SetCellValue('C'.$rows, $data[$i]['nama_aset']);
				$this->sheet->SetCellValue('D'.$rows, $data[$i]['merk']);
				$this->sheet->SetCellValue('E'.$rows, $data[$i]['tipe']);
				$this->sheet->SetCellValue('F'.$rows, $data[$i]['kode_asli']);
				$this->sheet->SetCellValue('G'.$rows, $tmp[0]);
				$this->sheet->SetCellValue('H'.$rows, str_replace('@@',' > ', $data[$i]['lokasi_aset']));
				$this->sheet->SetCellValue('I'.$rows, $data[$i]['unit_pj']);
				$this->sheet->SetCellValue('J'.$rows, $data[$i]['tgl_pengadaan']);
				$this->sheet->SetCellValue('K'.$rows, $data[$i]['tgl_pencatatan']);
				$this->sheet->SetCellValue('L'.$rows, number_format($data[$i]['nilai_perolehan'], 2, ',', '.'));
				$this->sheet->SetCellValue('M'.$rows, $data[$i]['sumber_dana']);
				$this->sheet->SetCellValue('N'.$rows, $data[$i]['kondisi']);
				$rows++;$no++;
			}else{
				$this->sheet->SetCellValue('A'.$rows, ucwords(GTFWConfiguration::GetValue('language','total_perolehan_rp')));
				$this->sheet->mergeCells('A'.$rows.':K'.$rows);
				$this->sheet->SetCellValue('L'.$rows, number_format($total_perolehan, 2, ',', '.'));
				$this->sheet->getStyle('A12:N'.$rows)->applyFromArray($tableCellStyledArray);
				
				$rows+=2;
				$this->sheet->SetCellValue('K'.$rows, $alamat.', '.IndonesianDate(date('Y-m-d'), 'YYYY-MM-DD'));
				$this->sheet->mergeCells('K'.$rows.':N'.$rows);
				$this->sheet->SetCellValue('K'.($rows+6), '('.$_SESSION['username'].')');
				$this->sheet->mergeCells('K'.($rows+6).':N'.($rows+6));
				$this->sheet->getStyle('K'.$rows.':N'.($rows+7))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
				
				$active_sheet++;
				$this->createNewSheet('Sheet '.($active_sheet+1),$active_sheet);
				$this->initNewSheet($headerVar);
				$rowCount = 1;
				$rows = 12;
			}
			$rowCount++;
		}
		
		$this->sheet->SetCellValue('A'.$rows, ucwords(GTFWConfiguration::GetValue('language','total_perolehan_rp')));
		$this->sheet->mergeCells('A'.$rows.':K'.$rows);
		$this->sheet->SetCellValue('L'.$rows, number_format($total_perolehan, 2, ',', '.'));
		$this->sheet->getStyle('A12:N'.$rows)->applyFromArray($tableCellStyledArray);
		
		$rows+=2;
		$this->sheet->SetCellValue('K'.$rows, $alamat.', '.IndonesianDate(date('Y-m-d'), 'YYYY-MM-DD'));
		$this->sheet->mergeCells('K'.$rows.':N'.$rows);
		$this->sheet->SetCellValue('K'.($rows+6), '('.$_SESSION['username'].')');
		$this->sheet->mergeCells('K'.($rows+6).':N'.($rows+6));
		$this->sheet->getStyle('K'.$rows.':N'.($rows+7))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	}

      $this->Save();
   }
}
?>
