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

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

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

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

class ViewInputInventarisasi extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/gedung/template');
      $this->SetTemplateFile('view_input_inventarisasi.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new AppInventarisasi;
	  $objGedung = new AppGedung();
      $objRuang = new Ruang;
      // 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['golbrgId'] = '3';
			if($this->Data['invTglPembelian']==''){
				$data['invTglPembelian'] = date('Y');
			}
      $return['data'] = $data;
      if (isset($_GET['id'])){ 
	  
	  //new---untuk nambah inventaris id ruang uda ada
	  $this->idRuang=$_GET['id'];
	  $this->idGedung=$_GET['gedungId'];
	  
	  //info ruang dan gedung
	  $return['infoRuangGedung']= $objRuang->GetInventarisByRuangId($this->idRuang);
	  
	  //---------
	  unset($_GET['id']);}
      // ---------
      
	  //----NEW----
	  $get = $_GET->AsArray();
      
      // inisialisasi messaging
      $post = $msg[0][0];
      // ---------
      
      //get data by id
      if (is_array($post))
      {
         $gedung['gd_id'] = $post['gedung_id'];
         $gedung['gd_brg_id'] = $post['aset_id'];
         $gedung['aset_kode'] = $post['kode_aset'];
         $gedung['aset_nama'] = $post['kode'];
         $gedung['gd_kode'] = $post['gedung_kode'];
         $gedung['gd_nama'] = $post['gedung_nama'];
         $gedung['gd_thn_perolehan'] = $post['thn_pemerolehan'];
         $gedung['gd_sbr_dana'] = $post['sbr_dana'];
         $gedung['biaya_pembangunan'] = $post['biaya_pembangunan'];
         $gedung['gd_luas_tanah'] = $post['luas_tanah'];
         $gedung['gd_luas_bangunan'] = $post['luas_bangunan'];
         $gedung['gd_sifat_bangunan'] = $post['sifat_bangunan'];
         $gedung['gd_imb'] = $post['imb'];
         $gedung['gd_peruntukan'] = $post['peruntukan_bangunan'];
         $gedung['gd_fas_listrik'] = $post['listrik'];
         $gedung['gd_fas_air'] = $post['air'];
         $gedung['gd_fas_telp'] = $post['telepon'];
         $gedung['gd_milik_brg_id'] = $post['kepemilikan'];
         $gedung['gd_peng_brg_id'] = $post['penguasaan'];
         $gedung['gd_kondisi_id'] = $post['kondisi'];
         $gedung['umur_ekonomis'] = $post['umur_ekonomis'];
         $gedung['umur_teknis'] = $post['umur_teknis'];
         $gedung['nilai_residu'] = $post['nilai_residu'];
         $gedung['nilai_perolehan'] = $post['nilai_perolehan'];
         $gedung['gd_kmps_id'] = $post['lokasi_kampus'];
         $gedung['gd_keterangan'] = $post['keterangan'];
         $gedung['penanggungjawab_nama'] = $post['penanggungjawab_nama'];
         $gedung['gd_denah'] = '';
         $gedung['gd_foto'] = '';
      }
      else $gedung = $objGedung->GetGedungById(Dispatcher::Instance()->Decrypt($get['id_gedung']));
      if(!empty($gedung)){
         $sbr_dana = $gedung['gd_sbr_dana'];
         $kepemilikan = $gedung['gd_milik_brg_id'];
         $lokasi_kampus = $gedung['gd_kmps_id'];
         $kondisi = $gedung['gd_kondisi_id'];
         $peruntukan = $gedung['gd_peruntukan'];
         $penguasaan = $gedung['gd_peng_brg_id'];
      }
	  //---end NEW--

      // 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'];
      $return['kib'] = json_encode($kib);
      
      $sumberDana = $Obj->GetCbxSumberDana();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'sumber_dana', array('invSumberDana', $sumberDana, $data['invSumberDana'], false, ''), Messenger::CurrentRequest);
      
      $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['invPagar'], false, ''), Messenger::CurrentRequest);
      
      $kepemilikan = $Obj->GetCbxKepemilikan();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'kepemilikan', array('invKepemilikan', $kepemilikan, $data['invKepemilikan'], false, ''), Messenger::CurrentRequest);
      
      $kepenguasaan = $Obj->GetCbxPenguasaanBarang();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'kepenguasaan', array('invPenguasaanBarang', $kepenguasaan, $data['invPenguasaanBarang'], 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['invKampus'], false, ''), Messenger::CurrentRequest);
      // ---------
      
      // inisialisasi link address
      $return['link']['url_action'] = Dispatcher::Instance()->GetUrl('gedung', 'addInventarisasi', 'do', 'html').'&id_gedung=' .Dispatcher::Instance()->Decrypt($get['gedungId']).'&lantaiId=' .Dispatcher::Instance()->Decrypt($get['lantaiId'].'&idruang='.$this->idRuang);
      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['detil_gedung'] = $gedung;
      $return['list_biaya_lain'] = $Obj->GetListBiayaLain();
      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');
      // ---------
	  //----new-> hasil dari list ruang--
      $this->mrTemplate->AddVar('content', 'INVRUANGANID', $this->idRuang);
	  $this->mrTemplate->AddVar('content', 'INVGEDUNGID', $this->idGedung);
	  
	    //info gedung dan ruang
	  $this->mrTemplate->AddVar('content', 'KODE_GEDUNG',$data['infoRuangGedung']['gedung_kode']);
	  $this->mrTemplate->AddVar('content', 'NAMA_GEDUNG',$data['infoRuangGedung']['gedung_nama']);
	  
	   $this->mrTemplate->AddVar('content', 'KODE_RUANG',$data['infoRuangGedung']['ruang_kode']);
	   $this->mrTemplate->AddVar('content', 'NAMA_RUANG',$data['infoRuangGedung']['ruang_nama']);
	  
      // 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']);
      $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');
         }
      }
      // ---------
	  
	  if(!empty($data['detil_gedung'])) {
         $this->mrTemplate->AddVar('content', 'ASET_KODE', $data['detil_gedung']['aset_kode']);
         $this->mrTemplate->AddVar('content', 'ASET_NAMA', $data['detil_gedung']['aset_nama']);
         $this->mrTemplate->AddVar('content', 'GEDUNG_KODE', $data['detil_gedung']['gd_kode']);
         $this->mrTemplate->AddVar('content', 'ASET_ID', $data['detil_gedung']['gd_brg_id']);
         $this->mrTemplate->AddVar('content', 'GEDUNG_ID', $data['detil_gedung']['gd_id']);
         $this->mrTemplate->AddVar('content', 'GEDUNG_NAMA', $data['detil_gedung']['gd_nama']);
         $this->mrTemplate->AddVar('content', 'THN_PEMEROLEHAN', $data['detil_gedung']['gd_thn_perolehan']);
         $this->mrTemplate->AddVar('content', 'LUAS_TANAH', $data['detil_gedung']['gd_luas_tanah']);
         $this->mrTemplate->AddVar('content', 'LUAS_BANGUNAN', $data['detil_gedung']['gd_luas_bangunan']);
         $this->mrTemplate->AddVar('content', 'BIAYA_PEMBANGUNAN', $data['detil_gedung']['biaya_pembangunan']);
         $this->mrTemplate->AddVar('content', 'UMUR_EKONOMIS', $data['detil_gedung']['umur_ekonomis']);
         $this->mrTemplate->AddVar('content', 'UMUR_TEKNIS', $data['detil_gedung']['umur_teknis']);
         $this->mrTemplate->AddVar('content', 'NILAI_RESIDU', $data['detil_gedung']['nilai_residu']);
         $this->mrTemplate->AddVar('content', 'NILAI_PEROLEHAN', number_format($data['detil_gedung']['nilai_perolehan'], 2, ',', '.'));
         if($data['detil_gedung']['gd_sifat_bangunan'] == 'Semi permanen')
            $this->mrTemplate->AddVar('content', 'SP_SELECT', 'selected="selected"');
         elseif($data['detil_gedung']['gd_sifat_bangunan'] == 'Permanen')
            $this->mrTemplate->AddVar('content', 'P_SELECT', 'selected="selected"');
         elseif($data['detil_gedung']['gd_sifat_bangunan'] == 'Sederhana')
            $this->mrTemplate->AddVar('content', 'S_SELECT', 'selected="selected"');
         if($data['detil_gedung']['gd_imb'] == 'Ya')
            $this->mrTemplate->AddVar('content', 'IMB_CHECKED', 'checked');
         $this->mrTemplate->AddVar('content', 'KETERANGAN', $data['detil_gedung']['gd_keterangan']);
         $this->mrTemplate->AddVar('content', 'GEDUNG_DENAH', $data['detil_gedung']['gd_denah']);
         $this->mrTemplate->AddVar('content', 'GEDUNG_FOTO', $data['detil_gedung']['gd_foto']);
         $this->mrTemplate->AddVar('content', 'PENANGGUNGJAWAB_NAMA', $data['detil_gedung']['penanggungjawab_nama']);
      }
   }
}
?>