<?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 ViewInventarisasiEdit extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/inventarisasi/template');
      $this->SetTemplateFile('view_inventarisasi_edit.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new AppInventarisasi;
      
      // 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;
         $data['invDetBarcode'] = $data['invBarcode'];
         $data['invDetMstLabel'] = $data['invMstLabel'];
         $data['invDetMerek'] = $data['invMerek'];
         $data['invDetTipe'] = $data['invTipe'];
         $data['invDetSpesifikasi'] = $data['invSpesifikasi'];
         $data['invDetNamaFile']=$data['invNamaFileOld'];
         $data['invDetKepemilikan']=$data['invKepemilikan'];
      }elseif (isset($_GET['id'])){
         $result = $Obj->GetInventarisasiById($_GET['id']->Integer()->Raw());
         if ($result) $data = $result;
         else unset($_GET['id']);
      }
      // ---------
      
      // render combo box
      $KIB = $Obj->GetCbxJenisKib();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'jenis_kib', array('golbrgId', $KIB, $data['golbrgId'], false, 'onChange="show_form_for(this.value)" id="invJenisKib"'), Messenger::CurrentRequest);
      foreach ($KIB as $value) {
			$kib[$value['id']] = $value['kode'];
			$kib2[$value['id']] = $value['name'];
		}
      $data['KIB'] = $kib2[$data['golbrgId']];
      $return['kib'] = json_encode($kib);
      
		$statusASet = $Obj->GetCbxStatusAset();
      @Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'statusaset', array('statusaset', $statusASet, $data['invDetAsetStatusId'], false, ''), Messenger::CurrentRequest);
		
      $sumberDana = $Obj->GetCbxSumberDana();
      @Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'sumber_dana', array('invSumberDana', $sumberDana, $data['invSumberDana'], false, ''), Messenger::CurrentRequest);
      $sumberDana = $Obj->ConvertCombo($sumberDana);
      $data['sumberDana'] = $sumberDana[$data['invSumberDana']];
      
      $satuanBarang = $Obj->GetCbxSatuan();
      @Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'satuan_barang', array('invSatuanBarang', $satuanBarang, $data['invSatuanBarang'], false, 'id="pop_up_satuan_barang_name"'), Messenger::CurrentRequest);
      
      $pagar = $Obj->GetCbxPagar();
      @Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'pagar', array('invPagar', $pagar, $data['invDetPagar'], false, ''), Messenger::CurrentRequest);
		
		$identitas_barang = $Obj->GetCbxIdentitasBarang();
      @Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'identitas_barang', array('invDetIdentitasBarang', $identitas_barang, $data['invDetIdentitasBarang'], false, ''), Messenger::CurrentRequest);
      
      $kepemilikan = $Obj->GetCbxKepemilikan();
      @Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'kepemilikan', array('invKepemilikan', $kepemilikan, $data['invDetKepemilikan'], false, ''), Messenger::CurrentRequest);
      
      $kepenguasaan = $Obj->GetCbxPenguasaanBarang();
      @Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'kepenguasaan', array('invPenguasaanBarang', $kepenguasaan, $data['invDetPenguasaanBarang'], false, ''), Messenger::CurrentRequest);
      
      $kondisiBarang = $Obj->GetCbxKondisi();
      @Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'kondisi_barang', array('invKondisiBrgKondId', $kondisiBarang, $data['invKondisiBrgKondId'], false, ''), Messenger::CurrentRequest);
      
      $lokasiKampus = $Obj->GetCbxLokasiKampus();
      @Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'lokasi_kampus', array('invKampus', $lokasiKampus, $data['invDetKampus'], false, ''), Messenger::CurrentRequest);
      // ---------
      
      // inisialisasi link address
      $return['link']['url_action'] = Dispatcher::Instance()->GetUrl('inventarisasi', 'reInventarisasi', 'do', 'html');
      if (isset($_GET['id'])) $return['link']['url_action'] .= '&id='.$_GET['id']->Integer()->Raw();
      $return['link']['url_pop_up_unit_pj'] = Dispatcher::Instance()->GetUrl('inventarisasi', 'popUpUnitPj', 'view', 'html');
      $return['link']['url_pop_up_lokasi_aset'] = Dispatcher::Instance()->GetUrl('inventarisasi', 'popUpLokasiAset', 'view', 'html');
      $return['link']['url_pop_up_kode_aset'] = Dispatcher::Instance()->GetUrl('inventarisasi', 'popUpKodefikasi', 'view', 'html');
      // ---------
      
      $return['list_biaya_lain'] = $Obj->GetListBiayaLain();
      @$data['invNamaFileOld'] = $data['invDetNamaFile'];
      @$data['link_foto'] = Dispatcher::Instance()->GetUrl('download', 'ReadFile', 'view', 'html').'&pid=3&file='.$data['invDetNamaFile'];
		$data['invNilaiPerolehanSatuan2'] = number_format($data['invNilaiPerolehanSatuan'], 0, ',', '.');
		$data['invNilaiFaktur2'] = number_format($data['invNilaiFaktur'], 0, ',', '.');
		$data['invTotalPerolehan2'] = number_format($data['invTotalPerolehan'], 0, ',', '.');
		@$data['invNilaiResidu2'] = number_format($data['invNilaiResidu'], 0, ',', '.');
		$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'], '');
      // ---------
      
      // render KIB list
      $this->mrTemplate->AddVar('content', 'JSON_KIB_LIST', $data['kib']);
      // ---------
      
      // render form default value
      $biayaLain = $data['data']['biaya_lain'];
      unset($data['data']['biaya_lain']);
      $data['data']['max_size']=GTFWConfiguration::GetValue('application','max_upload_size');
      if (!isset($data['data']['oldKondisi'])) $data['data']['oldKondisi'] = $data['data']['invKondisiBrgKondId'];
      if (!isset($data['data']['oldLokasi'])) $data['data']['oldLokasi'] = @$data['data']['invKampus'].'.'.@$data['data']['invGedungId'].'.'.@$data['data']['invRuanganId'];
      $this->mrTemplate->AddVars('content', $data['data'], '');
      // ---------
      
      // penyesuaian form terkait penyusutan
      if ($data['data']['is_penyusutan'])
         $this->mrTemplate->AddVar('content', 'IS_PENYUSUTAN_READONLY', 'readonly');
      // ---------
      
      // 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');
         }
      }
      // ---------
   }
}
?>