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

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

class ViewUsulanApproval extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/approval_usulan_barang/template');
      $this->SetTemplateFile('view_usulan_approval.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new Usulan;
      
      // 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();
      $listBarang = array();
      if ($this->Data)
      {
         $listBarang = $Obj->GetListBarangToApprove($this->Data['barangId']);
         $data = $this->Data;
         
         if (empty($listBarang))
         {
            $this->Pesan = array("Tidak ada usulan permintaan untuk barang [{$data['namaAset']}].");
            $this->css = 'notebox-alert';
         }
         elseif (isset($data['listBarang']))
         {
            foreach (array_keys($listBarang) as $key)
               if (isset($data['listBarang'][$key]))
                  $listBarang[$key] = $data['listBarang'][$key] + $listBarang[$key];
			}
         
			unset($data['listBarang']);
      }
		
		if (!isset($data['usulanRkpNoRkp']))
      {
         $date = date('Ymd.His');
         $data['usulanRkpNoRkp'] = 'rkpusul.'.$date;
      }
		
		if (!isset($data['usulanRkpPicApprove']))
      {
         $data['usulanRkpPicApprove'] = $_SESSION['username'];
      }
		
		if (!isset($data['tglUsulan1Display']) || !isset($data['usulanRkpTglApprove']))
      {
			$datenow = date('Y-m-d+1');
			$data['usulanRkpTglApprove'] = strtotime($datenow);
		}
		
      $return['data'] = $data;
      $return['listBarang'] = $listBarang;
      // ---------
      
      // inisialisasi link address
      $return['url']['action'] = Dispatcher::Instance()->GetUrl('approval_usulan_barang', 'usulanApproval', 'do', 'html');
      if (isset($_GET['id'])) $return['url']['action'] .= '&id='.$_GET['id']->Integer()->Raw();
      $return['url']['pop_up_kode_aset'] = Dispatcher::Instance()->GetUrl('approval_usulan_barang', 'popUpBarangApproval', 'view', 'html');
      $return['url']['back'] = Dispatcher::Instance()->GetUrl('approval_usulan_barang', 'usulanList', 'view', 'html');
      // ---------
      
      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['url'], 'URL_');
      if (!empty($data['listBarang']))
      {
         $this->mrTemplate->SetAttribute('simpan', 'visibility', 'visible');
         $this->mrTemplate->AddVars('simpan', $data['url'], 'URL_');
      }
      // ---------
      
      // render form default value
      $this->mrTemplate->AddVars('content', $data['data'], '');
      // ---------
      
      // render biaya lain
      if (empty($data['listBarang']))
         $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
      else
      {
         $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
         $i = 1;
         foreach ($data['listBarang'] as $value)
         {
            $value['nomor'] = $i;
            $value['class_name'] = ($i%2) ? 'table-common-even' : '';
				$value['hargaLama1'] = number_format($value['hargaLama'],2,',','.');
				$value['hargaBaru2'] = number_format($value['hargaBaru'],2,',','.');
				$value['usulanBrgTotal11'] = number_format($value['usulanBrgTotal1'],2,',','.');
				$value['usulanBrgTotal22'] = number_format($value['usulanBrgTotal2'],2,',','.');
            $this->mrTemplate->AddVars('data_item', $value, '');
            $this->mrTemplate->parseTemplate('data_item', 'a');
            $i++;
         }
      }
      // ---------
   }
}
?>