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

class PrintLaporanPengelolaan extends XlsxResponse
{
   public $Excel;
   
   public function ProcessRequest()
   {
      // set n get data
      	$Obj 			= new Pengelolaan();
		$Obj2 			= new Setting();
		$Obj3			= new Tanggal();
		
		$arrSetting 	= $Obj2->GetSetting();
		$notrans 		= Dispatcher::Instance()->Decrypt($_GET['no']);
		$id 			= Dispatcher::Instance()->Decrypt($_GET['id']);
		$bulan			= Dispatcher::Instance()->Decrypt($_GET['mon']);
		$tahun			= Dispatcher::Instance()->Decrypt($_GET['year']);
		$alamat 		= GTFWConfiguration::GetValue('organization', 'city');
		$instansi		= GTFWConfiguration::GetValue('organization', 'company_name');
		$namacetak	 	= $_SESSION['username'];
		$tglcetak		= IndonesianDate(date('Y-m-d'), 'YYYY-MM-DD');
		$judul			= "Laporan Penggunaan BHP";
		$monArr			= $Obj3->GetArrayMonth();
		
		#$totalData 		= $Obj->GetCountDataPengelolaan($notrans, $id, $arrSetting['3']['settingValue']);
		$itemViewed 	= 1000;
		$startRec 		= 0 ;
		
		$data 			= $Obj->GetDataPengelolaan($startRec, $itemViewed, $notrans, $id,$bulan,$tahun);
		
      // set file name
      $filename   = 'laporan_penggunaan_bhp_'.date('dmY').'.xls';
      $this->SetFileName($filename);
      
      // set sheet
      $sheet      = $this->Excel->getActiveSheet(0);
      
      // set metadata properties data excel
      $metadata   = $this->Excel->getProperties();
      
      // set default font setting for document
      $sheet->getDefaultStyle()->getFont()->setName('Arial')->setSize('10');
      
      // setting paging
      $sheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
      $sheet->getPageSetup()->setFitToWidth(1);
      $sheet->getPageSetup()->setFitToHeight(0);
      $sheet->getPageSetup()->setHorizontalCentered(true);
      $sheet->getPageSetup()->setVerticalCentered(false);
      
      $metadata->setCreator("anam");
      $metadata->setLastModifiedBy("anam");
      $metadata->setTitle("Laporan Penggunaan BHP");
      $metadata->setSubject("BHP");
      $metadata->setDescription("Laporan Penggunaan BHP printed on ".date('Y-m-d H:i:s', time()));
      $metadata->setKeywords("BHP");
      $metadata->setCategory("gtAset");
      
      // set worksheet name
      $sheet->setTitle('penggunaan_bhp');
      $sheet->setShowGridlines(false);

      
      // generate value
      if(empty($data)){
         $sheet->setCellValue('A1','Data Tidak Ditemukan');
      }else{
      
         // set height n width
         $sheet->getRowDimension('1')->setRowHeight('40');
         $sheet->getRowDimension('6')->setRowHeight('25');
         # $sheet->getRowDimension('13')->setRowHeight('40');
         
         // set coloum
         $sheet->getColumnDimension('A')->setWidth('5');
         $sheet->getColumnDimension('B')->setWidth('10');
         $sheet->getColumnDimension('C')->setWidth('20');
         $sheet->getColumnDimension('D')->setWidth('30');
         $sheet->getColumnDimension('E')->setWidth('30');
         $sheet->getColumnDimension('F')->setWidth('30');
         
         // set table header
         $headerStyledArray = array(
         	'font' => array(
               'bold' => true, 
               'size' => '14'
            ), 
            'alignment' => array(
               'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, 
               'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            )
         );
         
         $subHeaderStyledArray   = array(
            'font' => array(
               'bold' => true, 
               'size' => 12
            ), 
            'alignment' => array(
               'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT, 
               'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            )
         );
         
         $headerDescStyledArray  = array(
            'font' => array(
               'size' => '10',
               'bold' => false
            ), 
            'alignment' => array(
               'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT
            )
         );
         
         $desStyledArray   = array(
            'alignment' => array(
               'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT, 
               'vertical' => PHPExcel_Style_Alignment::VERTICAL_TOP,
               'wrap' => true
            )
         );
         
         $styledTableHeaderArray = array(
            'borders' => array(
               'allborders' => array(
                  'style' => PHPExcel_Style_Border::BORDER_THICK, 
                  '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
            )
         );
         
         $tableCellStyledArray   = array(
            'borders' => array(
               'allborders' => array(
                  'style' => PHPExcel_Style_Border::BORDER_THIN, 
                  'color' => array('argb' => 'ff000000')
               )
            )
         );
         
         $tableCellAlignCenterStyledArr   = array(
            'alignment' => array(
               'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
            )
         );
         
         $sheet->getStyle('A6:G6')->applyFromArray($styledTableHeaderArray);
         # $sheet->getStyle('A8:I14')->applyFromArray($desStyledArray);
         
         // header excel print
         $sheet->SetCellValue('A1', $instansi);
         $sheet->mergeCells('A1:F1');
         $sheet->getStyle('A1:F1')->applyFromArray($headerStyledArray);
         
         // sub header
         $sheet->SetCellValue('A3', $judul);
         $sheet->mergeCells('A3:C3');
         $sheet->getStyle('A3:C3')->applyFromArray($subHeaderStyledArray);
         
         if($id == ''){
				$unit = "Semua Unit kerja";
			} else {
				$unit = $data[0]['unit_nama'];
			}
			
			$sheet->SetCellValue('A4', 'Unit Kerja : '.$unit);
         $sheet->mergeCells('A4:F4');
         $sheet->getStyle('A4:F4')->applyFromArray($headerDescStyledArray);
         
         // set Header Data Table
         $sheet->SetCellValue('A6','No');
         $sheet->SetCellValue('B6','Bulan');
         $sheet->SetCellValue('C6','Tahun');
         $sheet->SetCellValue('D6','Kode Barang');
         $sheet->SetCellValue('E6','Nama Barang');
         $sheet->SetCellValue('F6','Unit Kerja');
         $sheet->SetCellValue('G6','Total');
         
         // get select data
         $setFirstColumn   = '7';
         for($i=0;$i<sizeof($data);$i++){
         	$data[$i]['no']		= ($i+1);
         	$sheet->setCellValueByColumnAndRow(0, $setFirstColumn+$i, $data[$i]['no']);
            $sheet->setCellValueByColumnAndRow(1, $setFirstColumn+$i, $monArr[$data[$i]['bulan']]);
            $sheet->setCellValueByColumnAndRow(2, $setFirstColumn+$i, $data[$i]['tahun']);
			$sheet->setCellValueByColumnAndRow(3, $setFirstColumn+$i, $data[$i]['kode']);
            $sheet->setCellValueByColumnAndRow(4, $setFirstColumn+$i, $data[$i]['nama_brg']);
            $sheet->setCellValueByColumnAndRow(5, $setFirstColumn+$i, $data[$i]['unit']);
            $sheet->setCellValueByColumnAndRow(6, $setFirstColumn+$i, $data[$i]['total']);
		}
			
         // bottom text
         $last_row   = $setFirstColumn+(count($data))-1;
         $sheet->getStyle('A6:G'.$last_row)->applyFromArray($tableCellStyledArray);
         #$sheet->getStyle('A7:B'.($last_row-1))->applyFromArray($tableCellAlignCenterStyledArr);
         #$sheet->getStyle('E17:F'.($last_row-1))->applyFromArray($tableCellAlignCenterStyledArr);
         
         $sheet->SetCellValue('E'.($last_row+4), $alamat.', '.$tglcetak);
         $sheet->mergeCells('E'.($last_row+4).':F'.($last_row+4));
         
         $sheet->SetCellValue('E'.($last_row+5), 'Mengetahui,');
         $sheet->mergeCells('E'.($last_row+5).':F'.($last_row+5));         
         
         $sheet->SetCellValue('E'.($last_row+12), '...............................');
         $sheet->mergeCells('E'. ($last_row+12).':F'.($last_row+12));
      }
  
      // save data
      $this->Save();
   }
   
   

}
?>
