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

class ViewExcelLapNeracaAset extends XlsxResponse
{
   public $Excel;
   
   public function ProcessRequest()
   {
      $Obj = new lapNeracaAset();
	  $_GET = $_GET->AsArray();
      //set data
      $unitEselon = GTFWConfiguration::GetValue('organization', 'company_name');
      //$unitName = $_GET['unitname'];
      $alamat = GTFWConfiguration::GetValue('organization', 'city');
      $user = $Obj->getUserData();
      $tanggalCetak = IndonesianDate(date('Y-m-d'), 'YYYY-MM-DD');
      $tanggal = IndonesianDate($_GET['tanggal'],'YYYY-MM-DD');
      $dateTemp = explode('-', $_GET['tanggal']);
      $tahun = $dateTemp[0];
      $kodeSimak = 	GTFWConfiguration::GetValue('organization', 'kode_kemendiknas').".".
      							GTFWConfiguration::GetValue('organization', 'kode_eselon1').".".
      							GTFWConfiguration::GetValue('organization', 'kode_wilayah').".".
      							GTFWConfiguration::GetValue('organization', 'kode_universitas');
      $data = $Obj->getDataLaporan($_GET['tanggal'],$_GET['unit']);
      $unit = $Obj->getDataUnitKerjaById($_GET['unit']);
      $unitName = $unit['unitkerjaNama'];
      $kodeSimak .= ".".$unit['unitkerjaKode'];
		
      // set file name
      $filename   = 'laporan_neraca_aset_'.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($user);
      $metadata->setLastModifiedBy($user);
      $metadata->setTitle("Laporan Neraca Aset");
      $metadata->setSubject("neraca");
      $metadata->setDescription("Laporan Neraca Aset printed on ".date('Y-m-d H:i:s', time()));
      $metadata->setKeywords("neraca");
      $metadata->setCategory("gtAset");
      
      // set worksheet name
      $sheet->setTitle('laporan_neraca_aset');
      $sheet->setShowGridlines(false);
      
      // generate value
      if(empty($data)){
         $sheet->setCellValue('A1','Data Tidak Ditemukan');
      }else{
         
         // set coloum
         $sheet->getColumnDimension('A')->setWidth('18');
         $sheet->getColumnDimension('B')->setWidth('50');
         $sheet->getColumnDimension('C')->setWidth('15');
         $sheet->getColumnDimension('D')->setWidth('18');
         
         // set table header
         $headerStyledArray = array(
         	'font' => array(
               'bold' => true, 
               'size' => '12'
            ), 
            'alignment' => array(
               'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, 
               'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            )
         );
         
         $subHeaderStyledArray   = array(
            'font' => array(
               'bold' => true, 
               'size' => '11'
            ), 
            'alignment' => array(
               'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT, 
               'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            )
         );
			
			$subHeaderStyled2Array   = array(
            'font' => array(
               'bold' => true, 
               'size' => '11'
            ), 
            '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
            )
         );
         
         $tableCellStyledArray   = array(
            'borders' => array(
               'allborders' => array(
                  'style' => PHPExcel_Style_Border::BORDER_THIN, 
                  'color' => array('argb' => 'ff000000')
               )
            ),
				'alignment' => array(
               'wrap' => true
            )
         );
         
         $tableCellAlignCenterStyledArr   = array(
            'alignment' => array(
               'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
            )
         );
			
			// header 1
			$rowOneTitle 		= "KEMENTERIAN PENDIDIKAN DAN KEBUDAYAAN"; 
			$rowTwoTitle 		= strtoupper($unitEselon); 
			$rowTrdTitle 		= strtoupper($unitName);
         
			$sheet->SetCellValue('A1', $rowOneTitle);
			$sheet->mergeCells('A1:D1');
         	$sheet->SetCellValue('A2', $rowTwoTitle);
			$sheet->mergeCells('A2:D2');
         	$sheet->SetCellValue('A3', $rowTrdTitle);
			$sheet->mergeCells('A3:D3');
			
         	//$sheet->getStyle('A1:D3')->applyFromArray($headerStyledArray);
			
			// sub header 1
			$row1 		= "LAPORAN POSISI BARANG MILIK NEGARA DI NERACA";
			$row2 		= "PERSEDIAAN PER TANGGAL ".strtoupper($tanggal); 
			$row3 		= "TAHUN ANGGARAN ".$tahun; 
         
			$sheet->SetCellValue('A5', $row1);
			$sheet->mergeCells('A5:D5');
         	$sheet->SetCellValue('A6', $row2);
			$sheet->mergeCells('A6:D6');
         	$sheet->SetCellValue('A7', $row3);
			$sheet->mergeCells('A7:D7');
			
         	$sheet->getStyle('A5:D7')->applyFromArray($headerStyledArray);
         	
         	$sheet->SetCellValue('A9', "NAMA UAKPB: ".$kodeSimak." ".$unitName);
         	$sheet->mergeCells('A9:B11');
         	$sheet->SetCellValue('C9', "Tanggal");$sheet->SetCellValue('D9', ": ".$tanggal);
         	$sheet->SetCellValue('C10', "Halaman");$sheet->SetCellValue('D10', ": 1");
         	$sheet->SetCellValue('C11', "Kode Lap.");$sheet->SetCellValue('D11', ": LBNSG");
         
         // set Header Data Table
			$sheet->SetCellValue('A12',strtoupper(GTFWConfiguration::GetValue('language','perkiraan_neraca')));
         	$sheet->mergeCells('A12:B12');
			$sheet->SetCellValue('C12',strtoupper(GTFWConfiguration::GetValue('language','jumlah')));
			$sheet->mergeCells('C12:D13');
			$sheet->SetCellValue('A13',strtoupper(GTFWConfiguration::GetValue('language','kode')));
			$sheet->SetCellValue('B13',strtoupper(GTFWConfiguration::GetValue('language','uraian')));
			$sheet->getStyle('A12:D13')->applyFromArray($styledTableHeaderArray);
			
			$rows = 14;
			$total = 0;
			for ($i=0; $i<count($data); $i++){
				$total=$total+$data[$i]['jumlah'];
				$sheet->SetCellValue('A'.$rows, $data[$i]['kode']);
				$sheet->SetCellValue('B'.$rows, $data[$i]['golongan']);
				$sheet->SetCellValue('C'.$rows, number_format($data[$i]['jumlah'], 2, ',', '.'));
				$sheet->mergeCells('C'.$rows.':D'.$rows);
				$rows++;
			}
			
			$sheet->SetCellValue('A'.$rows, strtoupper(GTFWConfiguration::GetValue('language','jumlah')));
			$sheet->mergeCells('A'.$rows.':B'.$rows);
			$sheet->SetCellValue('C'.$rows, number_format($total, 2, ',', '.'));
			$sheet->mergeCells('C'.$rows.':D'.$rows);
			$sheet->getStyle('A14:D'.$rows)->applyFromArray($tableCellStyledArray);
			$sheet->getStyle('A14:A'.($rows-1))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$sheet->getStyle('C14:D'.$rows)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
			$sheet->getStyle('A'.$rows.':D'.$rows)->getFont()->setBold(true);
			$sheet->getStyle('A'.$rows.':B'.$rows)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			
			$rows+=2;
			$sheet->SetCellValue('C'.$rows, $alamat.', '.$tanggalCetak);
			$sheet->mergeCells('C'.$rows.':D'.$rows);
         	$sheet->SetCellValue('C'.($rows+1), 'Penanggung Jawab UAKPB,');
         	$sheet->mergeCells('C'.($rows+1).':D'.($rows+1));         
            $sheet->SetCellValue('C'.($rows+7), '('.$user['RealName'].')');
     	    $sheet->mergeCells('C'.($rows+7).':D'.($rows+7));
     	    $sheet->getStyle('C'.$rows.':D'.($rows+7))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		}

      // save data
      $this->Save();
   }
}
?>
