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

class ViewPrintLaporanSp2d extends HtmlResponse {

   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/laporan_sp2d/template');
      $this->SetTemplateFile('view_print_laporan_sp2d.html');
   }
   
   function TemplateBase() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application', 'docroot') . 'main/template/');
      $this->SetTemplateFile('document-print.html');
      $this->SetTemplateFile('layout-common-print.html');
   }
   
   function ProcessRequest() {
		$Obj = new LaporanSp2d();
      $User = $Obj->GetDataUserByUsername($_SESSION['username']);
	
      $arrComboUnit = $Obj->GetComboUnit();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'unit', array('unit', $arrComboUnit, $_POST['unit'], true, ''), Messenger::CurrentRequest);
      if(!empty($_GET['unit']) || !empty($_GET['unit']))
         $return['data']= $Obj->GetDataInventaris($_GET['unit'],$_GET['barangId']);

      $return['barang']['kode'] =  $_POST['kode_barang'];
      $return['barang']['id'] =  $_POST['barang_id'];
      $return['kondisi'] = $Obj->GetKondisiBarang();
      
      $return['unit'] = $_POST['unit'];		

		$alamat 			= GTFWConfiguration::GetValue('organization', 'city');
		$company_name	= GTFWConfiguration::GetValue('organization', 'company_name');
		$namacetak	 	= $_SESSION['username'];
		$tglcetak		= IndonesianDate(date('Y-m-d'), 'YYYY-MM-DD');
		
		$return['alamat'] = $alamat;
		$return['company_name'] = $company_name;
		$return['nama'] = $namacetak;
		$return['tanggal'] = $tglcetak;	
      
   	return $return;  
   }
   
   function ParseTemplate($data = NULL) {
      $this->mrTemplate->addVar('content','ALAMAT', $data['alamat']);
      $this->mrTemplate->addVar('content','COMPANY_NAME', $data['company_name']);
      $this->mrTemplate->addVar('content','NAMA', $data['nama']);
      $this->mrTemplate->addVar('content','TANGGAL', $data['tanggal']);
      
      $this->mrTemplate->addVar('content','COUNT_KONDISI',count($data['kondisi']));
      
      //set title additional column header
      for($i=0;$i<count($data['kondisi']);$i++){
      	$this->mrTemplate->addVar('kondisi_barang','KONDISI_BARANG',$data['kondisi'][$i]['kondisibrgNama']);
      	$this->mrTemplate->parseTemplate('kondisi_barang','a');
		}
		$this->mrTemplate->addVar('data','EMPTY_WIDTH',count($data['penguasaan'])+count($data['kondisi'])+17);
		
      
      $unit = Dispatcher::Instance()->Encrypt($data['unit']);
      $barangId = Dispatcher::Instance()->Encrypt($data['barang']['id']);
      
      $urlPrint = Dispatcher::Instance()->GetUrl('laporan_sp2d', 'printLaporanSp2d', 'view', 'html').'&unit='.$unit.'&barangId='.$barangId;
      $this->mrTemplate->addVar('content','URL_CETAK',$urlPrint);
      $urlExcel = Dispatcher::Instance()->GetUrl('laporan_sp2d', 'excellLaporanSp2d', 'view', 'xls').'&unit='.$unit.'&barangId='.$barangId;
      $this->mrTemplate->addVar('content','URL_EXCEL',$urlExcel);
      
      if(empty($data['data'])){
      	$this->mrTemplate->AddVar('data','DATA_EMPTY','YES');
      	return;
		}else
			$this->mrTemplate->AddVar('data','DATA_EMPTY','NO');
			
			$dataGrid = $data['data'];
			
			foreach (array_keys($dataGrid) as $key)
     	 	{
				$dataGrid[$key]['satuan_perolehan'] = number_format($dataGrid[$key]['satuan_perolehan'], 2, ',', '.');
				$dataGrid[$key]['jumlah_perolehan'] = number_format($dataGrid[$key]['jumlah_perolehan'], 2, ',', '.');
      	}
      	
      	$dataList = $data['data'];
			$referensi = $data['data'][0]['invSPMNumber'];
			
			$flag = 0;
         for ($i=0; $i<sizeof($dataList); $i++) {
				
				$dataList[$i]['spm_view'] = '';
				
				if($referensi !== $dataList[$i]['invSPMNumber']){
					$referensi = $dataList[$i]['invSPMNumber'];
					$dataList[$flag]['spm_view'] = '<td style="vertical-align:middle" align="left" rowspan="'.$rowspan.'">'.$dataList[$i-1]['invSPMNumber'].'</td>';
					$flag = $i;
					$rowspan =1;
				} elseif(empty($dataList[$i]['invSPMNumber'])) {
					$referensi = $dataList[$i]['invSPMNumber'];
					$dataList[$flag]['spm_view'] = '<td style="vertical-align:middle" align="left"></td>';
					$flag = $i;
				} else {
					$rowspan++;
				}
			}

			$dataList[$flag]['spm_view'] = '<td style="vertical-align:middle" align="left" rowspan="'.$rowspan.'">'.trim($dataList[$flag]['invSPMNumber']).'</td>';
			$no = 1;
			
			for($i=0;$i<count($dataList);$i++){
				$dataList[$i]['no'] = $no+$i;
					
				$this->mrTemplate->AddVars('data_item',$dataList[$i],'DIR_');
				
				$this->mrTemplate->clearTemplate('kondisi_barang_value');
				for($j=0;$j<count($data['kondisi']);$j++){
               $this->mrTemplate->AddVar('kondisi_barang_value','KONDISI_BARANG_VALUE',$dataList[$i]['kondisi_'.$data['kondisi'][$j]['kondisibrgId']]);
					$this->mrTemplate->parseTemplate('kondisi_barang_value','a');
				}

				$this->mrTemplate->parseTemplate('data_item','a');
			}
   }
}
?>
