<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot').
   'module/pengadaan/business/Komponen.class.php';

//set Label for module
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .
'module/label/response/Label.proc.class.php';

class ViewKomponenDetail extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/pengadaan/template');
      $this->SetTemplateFile('view_komponen_detail.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new Komponen;
      
      // inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
      $this->Data = $msg[0][0];
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
      // ---------
      
      // inisialisasi default value
      $data = array();
      if (is_array($this->Data))
         $data = $this->Data;
      elseif (isset($_GET['id']))
      {
         $result = $Obj->GetPengadaanDetById($_GET['id']->Integer()->Raw());
         if ($result === false) unset($_GET['id']);
         else $data = $result;
      }
      
      $KIB = $Obj->GetCbxJenisKib();
      $tmp = $Obj->GetPengadaanDetail();
      if ($tmp['jenis_pengadaan'] == 'BHP') $KIB = array(end($KIB));
      //else array_pop($KIB);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'jenis_kib', array('golbrgId', $KIB, $data['golbrgId'], false, 'id="jenisKib" style="display: none"'), Messenger::CurrentRequest);
      $tmp = $Obj->ConvertCombo($KIB);
      $data['jenis_kib'] = $tmp[$data['golbrgId']];
      
      $satuanBarang = $Obj->ConvertCombo($Obj->GetCbxSatuan());
      $data['satuan_barang'] = $satuanBarang[$data['pengadaanDetSatuan']];
      
      $pagar = $Obj->ConvertCombo($Obj->GetCbxPagar());
      $data['pagar'] = $pagar[$data['pengadaanDetPagar']];
      
      $sifatBangunan = $Obj->ConvertCombo($Obj->GetCbxSifatBangunan());
      $data['sifat_bangunan'] = $sifatBangunan[$data['pengadaanDetSifatBangunan']];
      
      $IMB = $Obj->ConvertCombo($Obj->GetCbxIMB());
      $data['IMB'] = $IMB[$data['pengadaanDetIMB']];
      
      $kepemilikan = $Obj->ConvertCombo($Obj->GetCbxKepemilikan());
      $data['kepemilikan'] = $kepemilikan[$data['pengadaanDetPmlkBrg']];
      
      $kepenguasaan = $Obj->ConvertCombo($Obj->GetCbxPenguasaanBarang());
      $data['kepenguasaan'] = $kepenguasaan[$data['pengadaanDetPenguasaan']];
      
      $kondisiBarang = $Obj->ConvertCombo($Obj->GetCbxKondisi());
      $data['kondisi_barang'] = $kondisiBarang[$data['pengadaanDetKondisi']];
      
      if ($data['pengadaanDetDenah'] != '' && file_exists(GTFW_APP_DIR.'/'.$Obj->mImagePath.$data['pengadaanDetDenah']))
         $data['pengadaanDetDenah'] = '<img src="'.$Obj->mImagePath.$data['pengadaanDetDenah'].'">';
      else $data['pengadaanDetDenah'] = 'Tidak ada';
      if ($data['pengadaanDetFoto'] != '' && file_exists(GTFW_APP_DIR.'/'.$Obj->mImagePath.$data['pengadaanDetFoto']))
         $data['pengadaanDetFoto'] = '<img src="'.$Obj->mImagePath.$data['pengadaanDetFoto'].'">';
      else $data['pengadaanDetFoto'] = 'Tidak ada';
      
      foreach (array_keys($data) as $tmp)
         if (is_numeric($data[$tmp]) && strpos($data[$tmp], '.') !== false)
            $data[$tmp] = number_format($data[$tmp], 2, ',', '.');
      foreach (array_keys($data['biaya_lain']) as $tmp)
         $data['biaya_lain'][$tmp] = number_format($data['biaya_lain'][$tmp], 2, ',', '.');
      // ---------
      
      // inisialisasi link address
      $return['link']['back'] = Dispatcher::Instance()->GetUrl('pengadaan', 'komponenList', 'view', 'html');
      $return['link']['action'] = Dispatcher::Instance()->GetUrl('pengadaan', 'komponenInput', 'do', 'html');
      if (isset($_GET['id'])) $return['link']['action'] .= '&id='.$_GET['id']->Integer()->Raw();
      $return['link']['pop_up_kode_aset'] = Dispatcher::Instance()->GetUrl('inventarisasi', 'popUpKodefikasi', 'view', 'html');
      // ---------
      
      $return['list_biaya_lain'] = $Obj->GetListBiayaLain();
      $return['data'] = $data;
      return $return;
   }
   
   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 ParseTemplate($data = NULL)
   {
      if ($data == null) return null;
      
      // set Label
      $this->TemplateLabeling();
      $this->mrTemplate->AddVar('content', 'JUDUL', (isset($_GET['id']))?'Ubah':'Tambah');
      // ---------
      
      // render message box
      if($this->Pesan)
      {
         $msg = '';
         if (count($this->Pesan) > 1) foreach ($this->Pesan as $value)
            $msg .= "\t$value<br/>\n";
         else $msg .= $this->Pesan[0];
         
			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $msg);
			$this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $this->css);
		}
      // ---------
      
      // render link
      $this->mrTemplate->AddVars('content', $data['link'], 'URL_');
      // ---------
      
      // render data detail
      $biayaLain = $data['data']['biaya_lain'];
      unset($data['data']['biaya_lain']);
      $this->mrTemplate->AddVars('content', $data['data'], '');
      // ---------
      
      // render biaya lain
      if (empty($data['list_biaya_lain']))
         $this->mrTemplate->AddVar('biaya_lain', 'BIAYA_LAIN_EMPTY', 'YES');
      else
      {
         $this->mrTemplate->AddVar('biaya_lain', 'BIAYA_LAIN_EMPTY', 'NO');
         foreach ($data['list_biaya_lain'] as $value)
         {
            $this->mrTemplate->AddVar('biaya_item', 'BIAYAID', $value['id']);
            $this->mrTemplate->AddVar('biaya_item', 'BIAYANAME', $value['name']);
            $this->mrTemplate->AddVar('biaya_item', 'BIAYANILAI', $biayaLain[$value['id']]);
            $this->mrTemplate->parseTemplate('biaya_item', 'a');
         }
      }
      // ---------
   }
}
?>