<?php
require_once GTFWConfiguration::GetValue('application', 'docroot').'module/laporan_intrakomptabel/business/AppLapIntra.class.php';
require_once GTFWConfiguration::GetValue('application', 'docroot').'main/function/date.php';

class ViewExcelLaporanIntrakomptabel extends XlsxResponse
{
   public $Excel;
   
   public function ProcessRequest()
   {
      $Obj = new AppLapIntra();
      $user = $Obj->GetDataUserByUsername($_SESSION['username']);
		
      //set combo kib			
		$alamat 			= GTFWConfiguration::GetValue('organization', 'city');
		$instansi		= GTFWConfiguration::GetValue('organization', 'company_name');
		$namacetak	 	= $_SESSION['username'];
		$tglcetak		= IndonesianDate(date('Y-m-d'), 'YYYY-MM-DD');
		$get 				= $_GET->AsArray();
		$data				= $Obj->GetListIntra($get);
		
      // set file name
      $filename   = 'laporan_intrakomptabel_'.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['real_name']);
      $metadata->setLastModifiedBy($user['real_name']);
      $metadata->setTitle("Laporan Intrakomptabel");
      $metadata->setSubject("intra");
      $metadata->setDescription("Laporan Intrakomptabel printed on ".date('Y-m-d H:i:s', time()));
      $metadata->setKeywords("intra");
      $metadata->setCategory("gtAset");
      
      // set worksheet name
      $sheet->setTitle('laporan_intrakomptabel');
      $sheet->setShowGridlines(false);
      
      // generate value
      if(empty($data)){
         $sheet->setCellValue('A1','Data Tidak Ditemukan');
      }else{
      
         // set height n width
         $sheet->getRowDimension('13')->setRowHeight('20');
         $sheet->getRowDimension('14')->setRowHeight('20');
         $sheet->getRowDimension('15')->setRowHeight('20');
         
         // set coloum
         $sheet->getColumnDimension('A')->setWidth('18');
         $sheet->getColumnDimension('B')->setWidth('40');
         $sheet->getColumnDimension('C')->setWidth('15');
         $sheet->getColumnDimension('D')->setWidth('18');
         $sheet->getColumnDimension('E')->setWidth('18');
         $sheet->getColumnDimension('F')->setWidth('18');
         $sheet->getColumnDimension('G')->setWidth('18');
         $sheet->getColumnDimension('H')->setWidth('18');
         $sheet->getColumnDimension('I')->setWidth('18');
         $sheet->getColumnDimension('J')->setWidth('18');
         $sheet->getColumnDimension('K')->setWidth('18');
         $sheet->getColumnDimension('L')->setWidth('18');
         
         // 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' => '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 excel print
         $sheet->SetCellValue('A1', $instansi);
         $sheet->mergeCells('A1:K1');
         $sheet->getStyle('A1:K1')->applyFromArray($headerStyledArray);
			
			// sub header 1
			$rowOneTitle 		= "KEMENTRIAN PERHUBUNGAN"; 
			$rowTwoTitle 		= "BADAN PENGEMBANGAN SUMBER DAYA MANUSIA PERHUBUNGAN"; 
			$rowTrdTitle 		= "PROPINSI ".(strtoupper($alamat)); 
         
			$sheet->SetCellValue('A3', $rowOneTitle);
			$sheet->mergeCells('A3:D3');
         $sheet->SetCellValue('A4', $rowTwoTitle);
			$sheet->mergeCells('A4:D4');
         $sheet->SetCellValue('A5', $rowTrdTitle);
			$sheet->mergeCells('A5:D5');
			
         $sheet->getStyle('A3:D5')->applyFromArray($subHeaderStyledArray);
			
			// sub header 2
			$row1 		= "LAPORAN BARANG KUASA PENGGUNA";
			$row2 		= "INTRAKOMPTABEL"; 
			$row3 		= "RINCIAN PER SUB SUB KELOMPOK BARANG"; 
			$row4 		= "POSISI 1 JANUARI ".$get['tahunAwal']." S/D 31 DESEMBER ".($get['tahun']); 
			$row5 		= "TAHUN ANGGARAN ".($get['tahun']);
         
			$sheet->SetCellValue('A7', $row1);
			$sheet->mergeCells('A7:K7');
         $sheet->SetCellValue('A8', $row2);
			$sheet->mergeCells('A8:K8');
         $sheet->SetCellValue('A9', $row3);
			$sheet->mergeCells('A9:K9');
			$sheet->SetCellValue('A10', $row4);
			$sheet->mergeCells('A10:K10');
			$sheet->SetCellValue('A11', $row5);
			$sheet->mergeCells('A11:K11');
			
         $sheet->getStyle('A7:K11')->applyFromArray($subHeaderStyled2Array);
         
         // set Header Data Table
			$sheet->SetCellValue('A13','AKUN NERACA/SUB-SUB KELOMPOK BARANG');
         $sheet->mergeCells('A13:C14');
			$sheet->SetCellValue('A15','KODE');
         $sheet->SetCellValue('B15','URAIAN');
         $sheet->SetCellValue('C15','MERK');
         $sheet->SetCellValue('D13','SATUAN');
         $sheet->mergeCells('D13:D15');
			$sheet->SetCellValue('E13','SALDO PER 1 JANUARI '.($get['tahunAwal']));
			$sheet->mergeCells('E13:E14');
			$sheet->SetCellValue('E15','KUANTITAS');	
         $sheet->SetCellValue('F15','NILAI (RP)');
			$sheet->SetCellValue('G13','MUTASI');
			$sheet->mergeCells('G13:J13');
			$sheet->SetCellValue('G14','BERTAMBAH');
			$sheet->mergeCells('G14:H14');
         $sheet->SetCellValue('G15','KUANTITAS');
         $sheet->SetCellValue('H15','NILAI (RP)');
         $sheet->SetCellValue('I14','BERKURANG');
			$sheet->mergeCells('I14:J14');
			$sheet->SetCellValue('I15','KUANTITAS');
         $sheet->SetCellValue('J15','NILAI (RP)');
			$sheet->SetCellValue('K13','SALDO PER 31 DESEMBER '.($get['tahun']));
			$sheet->mergeCells('K13:K14');
         $sheet->SetCellValue('K15','KUANTITAS');
         $sheet->SetCellValue('L15','NILAI (RP)');
			
			$sheet->getStyle('A13:L15')->applyFromArray($styledTableHeaderArray);
			
			$setFstColumn   = '16';
			$dataGrid = $data;
			$dataGrid2 = $dataGrid;
			$kode_akun ="";
			for($i=0;$i<count($dataGrid);$i++){
				
				if($kode_akun != $dataGrid[$i]['kode_akun']){
					$kode_akun = $dataGrid[$i]['kode_akun'];
					$send = array();
					$send['kode_barang'] = $dataGrid[$i]['kode_akun'];
					$send['nama_barang'] = $dataGrid[$i]['nama_golongan'];
					$send['merk'] = $dataGrid[$i]['invMerek'];
					
					for($a=0;$a<count($dataGrid2);$a++){
						if($kode_akun == $dataGrid2[$a]['kode_akun']){
							$nilai_awal[$kode_akun] += $dataGrid2[$a]['nilai_awal'];
							$nilai_tambah[$kode_akun] += $dataGrid2[$a]['nilai_tambah'];
							$nilai_kurang[$kode_akun] += $dataGrid2[$a]['nilai_kurang'];
							$nilai_akhir[$kode_akun] += $dataGrid2[$a]['nilai_akhir'];
							$kuantitas_awal[$kode_akun] += $dataGrid2[$a]['kuantitas_awal'];
							$kuantitas_tambah[$kode_akun] += $dataGrid2[$a]['kuantitas_tambah'];
							$kuantitas_kurang[$kode_akun] += $dataGrid2[$a]['kuantitas_kurang'];
							$kuantitas_akhir[$kode_akun] += $dataGrid2[$a]['kuantitas_akhir'];
						}
					}
										
					$send['nilai_awal'] = $nilai_awal[$kode_akun];
					$send['nilai_tambah'] = $nilai_tambah[$kode_akun];
					$send['nilai_kurang'] = $nilai_kurang[$kode_akun];
					$send['nilai_akhir'] = $nilai_akhir[$kode_akun];
					$send['kuantitas_awal'] = $kuantitas_awal[$kode_akun];
					$send['kuantitas_tambah'] = $kuantitas_tambah[$kode_akun];
					$send['kuantitas_kurang'] = $kuantitas_kurang[$kode_akun];
					$send['kuantitas_akhir'] = $kuantitas_akhir[$kode_akun];
					
					$sheet->setCellValueExplicit('A'.($setFstColumn+$i), $dataGrid[$i]['kode_akun'], PHPExcel_Cell_DataType::TYPE_STRING);
					$sheet->setCellValueExplicit('B'.($setFstColumn+$i), trim($dataGrid[$i]['nama_golongan']), PHPExcel_Cell_DataType::TYPE_STRING);
					$sheet->setCellValueExplicit('C'.($setFstColumn+$i), trim($dataGrid[$i]['invMerek']), PHPExcel_Cell_DataType::TYPE_STRING);
					$sheet->setCellValueByColumnAndRow(3, $setFstColumn+$i, '');
					$sheet->setCellValueByColumnAndRow(4, $setFstColumn+$i, $send['kuantitas_awal']);
					$sheet->setCellValueByColumnAndRow(5, $setFstColumn+$i, $send['nilai_awal']);
					$sheet->setCellValueByColumnAndRow(6, $setFstColumn+$i, $send['kuantitas_tambah']);
					$sheet->setCellValueByColumnAndRow(7, $setFstColumn+$i, $send['nilai_tambah']);
					$sheet->setCellValueByColumnAndRow(8, $setFstColumn+$i, $send['kuantitas_kurang']);
					$sheet->setCellValueByColumnAndRow(9, $setFstColumn+$i, $send['nilai_kurang']);
					$sheet->setCellValueByColumnAndRow(10, $setFstColumn+$i, $send['kuantitas_akhir']);
					$sheet->setCellValueByColumnAndRow(11, $setFstColumn+$i, $send['nilai_akhir']);
				}
				
				if(!empty($dataGrid[$i]['kode_barang'])){
					$send = array();
					$data1['nilai_awal_total'] += $dataGrid[$i]['nilai_awal'];
					$data1['nilai_tambah_total'] += $dataGrid[$i]['nilai_tambah'];
					$data1['nilai_kurang_total'] += $dataGrid[$i]['nilai_kurang'];
					$data1['nilai_akhir_total'] += $dataGrid[$i]['nilai_akhir'];
					
					$send['kode_barang'] = $dataGrid[$i]['kode_barang'];
					$send['nama_barang'] = $dataGrid[$i]['nama_barang'];
					$send['satuan'] = $dataGrid[$i]['satuan'];
					$send['nilai_awal'] = $dataGrid[$i]['nilai_awal'];
					if(empty($send['nilai_awal'])){
						$send['nilai_awal'] = 0;
					}					
					$send['nilai_tambah'] = $dataGrid[$i]['nilai_tambah'];
					$send['nilai_kurang'] = $dataGrid[$i]['nilai_kurang'];
					$send['nilai_akhir'] = $dataGrid[$i]['nilai_akhir'];
					$send['kuantitas_awal'] = $dataGrid[$i]['kuantitas_awal'];
					$send['kuantitas_tambah'] = $dataGrid[$i]['kuantitas_tambah'];
					$send['kuantitas_kurang'] = $dataGrid[$i]['kuantitas_kurang'];
					$send['kuantitas_akhir'] = $dataGrid[$i]['kuantitas_akhir'];
					
					
					$sheet->setCellValueExplicit('A'.($setFstColumn+$i+1), $send['kode_barang'], PHPExcel_Cell_DataType::TYPE_STRING);
					$sheet->setCellValueExplicit('B'.($setFstColumn+$i+1), trim($send['nama_barang']), PHPExcel_Cell_DataType::TYPE_STRING);
					$sheet->setCellValueExplicit('C'.($setFstColumn+$i+1), trim($dataGrid[$i]['invMerek']), PHPExcel_Cell_DataType::TYPE_STRING);
					$sheet->setCellValueByColumnAndRow(3, $setFstColumn+$i+1, $send['satuan']);
					$sheet->setCellValueByColumnAndRow(4, $setFstColumn+$i+1, $send['kuantitas_awal']);
					$sheet->setCellValueByColumnAndRow(5, $setFstColumn+$i+1, $send['nilai_awal']);
					$sheet->setCellValueByColumnAndRow(6, $setFstColumn+$i+1, $send['kuantitas_tambah']);
					$sheet->setCellValueByColumnAndRow(7, $setFstColumn+$i+1, $send['nilai_tambah']);
					$sheet->setCellValueByColumnAndRow(8, $setFstColumn+$i+1, $send['kuantitas_kurang']);
					$sheet->setCellValueByColumnAndRow(9, $setFstColumn+$i+1, $send['nilai_kurang']);
					$sheet->setCellValueByColumnAndRow(10, $setFstColumn+$i+1, $send['kuantitas_akhir']);
					$sheet->setCellValueByColumnAndRow(11, $setFstColumn+$i+1, $send['nilai_akhir']);
				}
			}
			
			$last_row   = $setFstColumn+(count($data));
         // total here
			$sheet->getRowDimension(($last_row+1))->setRowHeight('20');
			$sheet->SetCellValue('A'.($last_row+1), 'TOTAL');
			$sheet->SetCellValue('F'.($last_row+1), $data1['nilai_awal_total']);
			$sheet->SetCellValue('H'.($last_row+1), $data1['nilai_tambah_total']);
			$sheet->SetCellValue('J'.($last_row+1), $data1['nilai_kurang_total']);
			$sheet->SetCellValue('L'.($last_row+1), $data1['nilai_akhir_total']);	
					
			$sheet->getStyle('A'.($last_row+1).':L'.($last_row+1))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$sheet->getStyle('A'.($last_row+1).':L'.($last_row+1))->getFont()->setBold(true);
			$sheet->mergeCells('A'.($last_row+1).':C'.($last_row+1));
			$sheet->getStyle('A'.($last_row+1).':C'.($last_row+1))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			# $sheet->getStyle('D16:K'.($last_row+1))->getNumberFormat()->setFormatCode('#,##');
			$sheet->getStyle('A16:L'.($last_row+1))->applyFromArray($tableCellStyledArray);
			
         $sheet->SetCellValue('H'.($last_row+4), $alamat.', '.$tglcetak);
         $sheet->mergeCells('H'.($last_row+4).':I'.($last_row+4));
         
         $sheet->SetCellValue('H'.($last_row+5), 'Mengetahui,');
         $sheet->mergeCells('H'.($last_row+5).':I'.($last_row+5));         
         
         $sheet->SetCellValue('H'.($last_row+12), '...............................');
         $sheet->mergeCells('H'. ($last_row+12).':I'.($last_row+12));
		}

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