<?php

/*
 @ClassName : View Laporan DIRs
 @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_induk_inventaris/business/AppLaporanIndukInventaris.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/inventarisasi/business/AppInventarisasi.class.php';
//set Label for module
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .
'module/label/response/Label.proc.class.php';
class ViewLaporanIndukInventaris extends HtmlResponse {

   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/laporan_induk_inventaris/template');
      $this->SetTemplateFile('view_laporan_induk_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 AppLaporanIndukInventaris();
		$Obj2 = new AppInventarisasi();
      $User = $Obj->GetDataUserByUsername($_SESSION['username']);
		
		$filter = array();
		if (isset($_POST['cari'])) $filter = $_POST->AsArray();
		
		if (!empty($filter))
      {
         $tmp = strtotime($filter['tanggal_awal_year'].'-'.$filter['tanggal_awal_mon'].'-'.$filter['tanggal_awal_day']);
         $filter['tanggal_awal_year'] = date('Y', $tmp);
         $filter['tanggal_awal_mon'] = date('m', $tmp);
         $filter['tanggal_awal_day'] = date('d', $tmp);
         
         $tmp = strtotime($filter['tanggal_akhir_year'].'-'.$filter['tanggal_akhir_mon'].'-'.$filter['tanggal_akhir_day']);
         if ($tmp == 0) $tmp = time();
         $filter['tanggal_akhir_year'] = date('Y', $tmp);
         $filter['tanggal_akhir_mon'] = date('m', $tmp);
         $filter['tanggal_akhir_day'] = date('d', $tmp);
      } else {	
			$filter['tanggal_awal_year'] = date('Y')-5;
         $filter['tanggal_awal_mon'] = date('m');
         $filter['tanggal_awal_day'] = date('d');
		
			$filter['tanggal_akhir_year'] = date('Y');
         $filter['tanggal_akhir_mon'] = date('m');
         $filter['tanggal_akhir_day'] = date('d');
			
			$filter['jenis_kib'] = 'all';
		}
		
		// Inisialisasi komponen tanggal
      Messenger::Instance()->SendToComponent('tanggal', 'tanggal', 'view', 'html', 'tanggal_awal', array($filter['tanggal_awal_year'].'-'.$filter['tanggal_awal_mon'].'-'.$filter['tanggal_awal_day'], 1970), Messenger::CurrentRequest);
      Messenger::Instance()->SendToComponent('tanggal', 'tanggal', 'view', 'html', 'tanggal_akhir', array($filter['tanggal_akhir_year'].'-'.$filter['tanggal_akhir_mon'].'-'.$filter['tanggal_akhir_day'], 1970), Messenger::CurrentRequest);
      // ---------
		
     //set combo gedung
      $arrComboUnit = $Obj->GetComboUnit();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'unit', array('unit', $arrComboUnit, $filter['unit'], true, ''), Messenger::CurrentRequest);
		
		$cbxkib = $Obj2->GetCbxJenisKib();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'jenis_kib', array('jenis_kib', $cbxkib, $filter['jenis_kib'], true, ''), Messenger::CurrentRequest);
		
      if(isset($_POST['cari']))
         $return['data']= $Obj->GetDataIndukInventaris($filter);
      #print_r($return['data']);
      $return['penguasaan']= $Obj->GetPenguasaanBarang();
      $return['kondisi']= $Obj->GetKondisiBarang();
      
      $return['unit']=$filter;
		
   	return $return;  
   }
   
   function ParseTemplate($data = NULL) {
   	
      $urlAction = Dispatcher::Instance()->GetUrl('laporan_induk_inventaris', 'LaporanIndukInventaris', 'view', 'html');
      $this->mrTemplate->addVar('content','URL_ACTION',$urlAction);
      $this->mrTemplate->addVar('content','COUNT_KONDISI',count($data['kondisi']));
      $this->mrTemplate->addVar('content','COUNT_PENGUASAAN',count($data['penguasaan']));
      
      //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');
		}
		
		for($i=0;$i<count($data['penguasaan']);$i++){
      	$this->mrTemplate->addVar('penguasaan_barang','PENGUASAAN_BARANG',$data['penguasaan'][$i]['statuspengbrgNama']);
      	$this->mrTemplate->parseTemplate('penguasaan_barang','a');
		}
		
		$this->mrTemplate->addVar('data','EMPTY_WIDTH',count($data['penguasaan'])+count($data['kondisi'])+17);
		
      
      $unit = Dispatcher::Instance()->Encrypt($data['unit']['unit']);
      $jenis_kib = Dispatcher::Instance()->Encrypt($data['unit']['jenis_kib']);
      $tglA = Dispatcher::Instance()->Encrypt($data['unit']['tanggal_awal_year']);
      $blnA = Dispatcher::Instance()->Encrypt($data['unit']['tanggal_awal_mon']);
      $hrA = Dispatcher::Instance()->Encrypt($data['unit']['tanggal_awal_day']);
      $tglK = Dispatcher::Instance()->Encrypt($data['unit']['tanggal_akhir_year']);
      $blnK = Dispatcher::Instance()->Encrypt($data['unit']['tanggal_akhir_mon']);
      $hrK = Dispatcher::Instance()->Encrypt($data['unit']['tanggal_akhir_day']);
      
      $urlPrint = Dispatcher::Instance()->GetUrl('laporan_induk_inventaris', 'LaporanIndukInventaris', 'print', 'html')
			.'&unit='.$unit
                        .'&jenis_kib='.$jenis_kib
			.'&tanggal_awal_year='.$tglA
			.'&tanggal_awal_mon='.$blnA
			.'&tanggal_awal_day='.$hrA
			.'&tanggal_akhir_year='.$tglK
			.'&tanggal_akhir_mon='.$blnK
			.'&tanggal_akhir_day='.$hrK
			;
      $this->mrTemplate->addVar('content','URL_CETAK',$urlPrint);
      $urlExcel = Dispatcher::Instance()->GetUrl('laporan_induk_inventaris', 'ExcelLaporanIndukInventaris', 'view', 'xls')
			.'&unit='.$unit
                        .'&jenis_kib='.$jenis_kib
			.'&tanggal_awal_year='.$tglA
			.'&tanggal_awal_mon='.$blnA
			.'&tanggal_awal_day='.$hrA
			.'&tanggal_akhir_year='.$tglK
			.'&tanggal_akhir_mon='.$blnK
			.'&tanggal_akhir_day='.$hrK
			;
      $this->mrTemplate->addVar('content','URL_EXCEL',$urlExcel);
      
      $urlPopup = Dispatcher::Instance()->GetUrl('laporan_induk_inventaris', 'PopupUnitKerja', 'view', 'html');
      $this->mrTemplate->addVar('content','URL_POPUP',$urlPopup);
      $this->mrTemplate->addVar('content','UNITNAMA',$data['unit']['unitnama']);
      $this->mrTemplate->addVar('content','UNIT',$data['unit']['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]['jumlah_perolehan_sum'] = $dataGrid[$key]['jumlah_perolehan'];
				$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->clearTemplate('penguasaan_barang_value');
				for($j=0;$j<count($data['penguasaan']);$j++){
               $this->mrTemplate->AddVar('penguasaan_barang_value','PENGUASAAN_BARANG_VALUE',$dataGrid[$i]['penguasaan_'.$data['penguasaan'][$j]['statuspengbrgId']]);
					$this->mrTemplate->parseTemplate('penguasaan_barang_value','a');
				}
					
				$jumlah_perolehan_sum += $dataGrid[$i]['jumlah_perolehan_sum'];
				
				$this->mrTemplate->parseTemplate('data_item','a');
			}
			
			$cols = count($data['penguasaan']) + count($data['kondisi']) + 14;
			$this->mrTemplate->addVar('content','COLS', $cols);
			$this->mrTemplate->addVar('content','TOTAL_PEROLEHAN', number_format($jumlah_perolehan_sum, 2, ',', '.'));	
   }
}
?>
