<?php

require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/laporan_sp2d/business/LaporanSp2d.class.php';

class ViewlaporanSp2d extends HtmlResponse {

   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/laporan_sp2d/template');
      $this->SetTemplateFile('view_laporan_sp2d.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(isset($_POST['cari']))
         $return['data']= $Obj->GetDataInventaris($_POST['unit'],$_POST['barang_id']);

      $return['barang']['kode'] =  $_POST['kode_barang'];
      $return['barang']['id'] =  $_POST['barang_id'];
      $return['kondisi']= $Obj->GetKondisiBarang();
      
      $return['unit']=$_POST['unit'];		
   	return $return;  
   }
   
   function ParseTemplate($data = NULL) {
   	
   	$urlPopUp = Dispatcher::Instance()->GetUrl('popup', 'kodeBarang', 'popup', 'html').'&cari=5';
      $this->mrTemplate->addVar('content','URL_POP_UP_KD_BARANG',$urlPopUp);
      
      $this->mrTemplate->addVar('content','KODE_BARANG',$data['barang']['kode']);
      $this->mrTemplate->addVar('content','BARANG_ID',$data['barang']['id']);
   	
      $urlAction = Dispatcher::Instance()->GetUrl('laporan_sp2d', 'laporanSp2d', 'view', 'html');
      $this->mrTemplate->addVar('content','URL_ACTION',$urlAction);
      $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');
			}
   }
}
?>
