<?php

/*
 @ClassName : View Buku Inventaris
 @Copyright : PT Gamatechno Indosnesia
 @Analyzed By : Nanang Ruswianto <nanang@gamatechno.com>
 @Designed By : Rosyid <rosyid@gamatechno.com>
 @Author By : Dyan Galih <galih@gamatechno.com>
 @Version : 1.0
 @StartDate : Nov 11, 2008
 @LastUpdate : Nov 11, 2008
 @Description :
*/

require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/laporan_inventaris/business/AppLaporanInventaris.class.php';
//set Label for module
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .
'module/label/response/Label.proc.class.php';
class ViewLaporanInventaris extends HtmlResponse {

   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/laporan_inventaris/template');
      $this->SetTemplateFile('view_laporan_inventaris.html');
   }
   
   function TemplateLabeling(){
      $ObjLabel = new GetModuleLabel();
      $arrLabel = $ObjLabel->GetLabel();
      		
      //patTemplate name yang memiliki label2 dinamic
      $arrContent=array("content");
      
      for($i=0;$i<count($arrContent);$i++){
         for($j=0;$j<count($arrLabel);$j++){
            $this->mrTemplate->AddVar($arrContent[$i],$arrLabel[$j]['labelCode'],$arrLabel[$j]['labelName']);
         }
      }
   }
   
   function ProcessRequest() {
      $Obj = new AppLaporanInventaris();
      $User = $Obj->GetDataUserByUsername($_SESSION['username']);
      $_POST = $_POST->AsArray();

      for($i=0; $i <= 20; $i++){
      	$tanggal[] = array('id' => date('Y')-$i,'name' => date('Y')-$i);
      }
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'tahun', array('tahun', $tanggal, $_POST['tahun'], true, ''), Messenger::CurrentRequest);
      
      //set combo gedung
      $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'],$_POST['tahun']);
      #print_r($return['data']);
      $return['barang']['kode'] =  $_POST['kode_barang'];
      $return['barang']['id'] =  $_POST['barang_id'];
      $return['tahun'] = $_POST['tahun'];
      $return['kondisi']= $Obj->GetKondisiBarang();
      
      $return['unit']=$_POST['unit'];
      $return['unitnama']=$_POST['unitnama'];
		
   	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_inventaris', 'LaporanInventaris', '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']);
      $tahun = Dispatcher::Instance()->Encrypt($data['tahun']);
      
      $urlPrint = Dispatcher::Instance()->GetUrl('laporan_inventaris', 'LaporanInventaris', 'print', 'html').'&unit='.$unit.'&barangId='.$barangId.'&tahun='.$tahun;
      $this->mrTemplate->addVar('content','URL_CETAK',$urlPrint);
      $urlExcel = Dispatcher::Instance()->GetUrl('laporan_inventaris', 'ExcelLaporanInventaris', 'view', 'xls').'&unit='.$unit.'&barangId='.$barangId.'&tahun='.$tahun;
      $this->mrTemplate->addVar('content','URL_EXCEL',$urlExcel);
      
      $urlPopup = Dispatcher::Instance()->GetUrl('laporan_inventaris', 'PopupUnitKerja', 'view', 'html');
      $this->mrTemplate->addVar('content','URL_POPUP',$urlPopup);
      $this->mrTemplate->addVar('content','UNITNAMA',$data['unitnama']);
      $this->mrTemplate->addVar('content','UNIT',$data['unit']);
      
      //set Label
      $this->TemplateLabeling();
      
      if(empty($data['data'])){
      	$this->mrTemplate->AddVar('data','DATA_EMPTY','YES');
      	return;
		}else
			$this->mrTemplate->AddVar('data','DATA_EMPTY','NO');
			
			$dataGrid = $data['data'];
			$no = 1;
			
			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, ',', '.');
      	}
			
			for($i=0;$i<count($dataGrid);$i++){
				$dataGrid[$i]['no'] = $no+$i;
				
				$this->mrTemplate->AddVars('data_item',$dataGrid[$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',$dataGrid[$i]['kondisi_'.$data['kondisi'][$j]['kondisibrgId']]);
					$this->mrTemplate->parseTemplate('kondisi_barang_value','a');
				}

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