<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot').
   'module/'.Dispatcher::Instance()->mModule.'/business/DeliveryOrder.class.php';

class ViewTableItemDetail extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/'.Dispatcher::Instance()->mModule.'/template');
      $this->SetTemplateFile('table_item_detail.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new DeliveryOrder;
   	  #jika modul pusat terdaftar maka viewnya pusat
      $aksesPusat = $Obj->GetModuleAccess('delivery_order','DOListPusat');
      if((int)$aksesPusat > 0){
      	$return['roleId'] = 1;
      	$Obj->roleId = 1;
      }
      
      // inisialisasi value
      $data = $Obj->GetDODetail($_GET['id']->Integer()->Raw(),$_GET['idUnit']->Integer()->Raw());
      $return['header']['oldPIC'] = $data['header']['doPICPenerima'];
      if ($return['roleId'] == 1) $return['dataGrid'] = $data['rolePusat'];
      else $return['dataGrid'] = $data['roleUnit'];
      if (!empty($this->Data))
      {
         foreach (array_keys($return['dataGrid']) as $key)
         {
            $temp = $this->Data['ruang'][$return['dataGrid'][$key]['id']];
            if ($temp > 0) $return['dataGrid'][$key]['doDetailRuang'] = $temp;
            $return['dataGrid'][$key]['ruangOld'] = $this->Data['ruangOld'][$return['dataGrid'][$key]['id']];
         }
         
         $return['header']['tglTerimaDisplay'] = $this->Data['tglTerimaDisplay'];
         $return['header']['tglTerima'] = $this->Data['tglTerima'];
         $return['header']['doPICPenerima'] = $this->Data['doPICPenerima'];
      }
      // ---------
      $return['comboRuang'] = $Obj->GetComboRuang($_GET['idUnit']->Integer()->Raw());
      $return['start'] = 1;
      return $return;
   }
   
   
   function ParseTemplate($data = NULL)
   {
      if ($data === NULL) return NULL;
      
      // Render Header
      if ($data['roleId'] == 1)
      {
         $data['header']['vartitle1'] = 'Unit Tujuan';
         $data['header']['varvalue1'] = $data['header']['unitkerjaNama'];
         $data['header']['vartitle2'] = 'Jumlah';
		 $this->mrTemplate->SetAttribute('rolePusat2', 'visibility', 'visible');
      }
      else
      {
         $data['header']['vartitle1'] = 'Unit Penerima';
         $data['header']['varvalue1'] = $data['header']['unitkerjaNama'];
         $data['header']['vartitle2'] = 'Lokasi';
         $data['header']['display'] = 'none';
         $this->mrTemplate->SetAttribute('roleUnit2', 'visibility', 'visible');
      }
      $this->mrTemplate->AddVars('content', $data['header'], '');
      // ---------
      
      // Render data grid
		if (empty($data['dataGrid']))
		{
			$this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
			return;
		}
		else $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
      
      if (empty($data['comboRuang'])) $data['comboRuang'] = array(array('id' => 0, 'name' => 'Belum ada ruang'));
      
		$i = $data['start'];
      foreach ($data['dataGrid'] as $value)
      {
			$value['nomor'] = $i;
			$value['class_name'] = ($i % 2) ? 'table-common-even' : '';
         $value['hargaSatuan'] = number_format($value['hargaSatuan'],2,',','.');
         
         if (($temp = strtotime($value['doUnitTglTerima'])) == 0 AND $data['roleId'] == 1)
            $value['doUnitTglTerima'] = 'Belum diterima';
         elseif ($temp > 0 OR $data['roleId'] == 1) $value['doUnitTglTerima'] = date('d F Y', $temp);
         
         // Render Combo Box Lokasi
         if ($data['roleId'] != 1)
         {
            $this->mrTemplate->clearTemplate('roleUnit2');
            $this->mrTemplate->clearTemplate('cbruang');
            foreach ($data['comboRuang'] as $option)
            {
               $option['selected'] = ("{$value['doDetRuangId']}" == "{$option['id']}") ? 'selected' : '';
               $option['disabled'] = ("{$value['doDetRuangId']}" == "") ? '' : 'disabled';
               
               $this->mrTemplate->AddVars('cbruang', $option, '');
               $this->mrTemplate->parseTemplate('cbruang', 'a');
            }
            
            $this->mrTemplate->AddVar('roleUnit2', 'ID', $value['id']);
            if (!isset($value['ruangOld'])) $value['ruangOld'] = $value['doDetRuangId'];
            $this->mrTemplate->AddVar('roleUnit2', 'ruangOld', $value['ruangOld']);
            $value['showlistpusat'] = 'none';
            if ($value['ruangOld'] != "") $this->mrTemplate->AddVar('roleUnit2', 'disabled', 'disabled');
            $this->mrTemplate->parseTemplate('roleUnit2', 'a');
         }
         // ---------
         else
         {
            $this->mrTemplate->AddVar('rolePusat2', 'JUMLAH', $value['jumlah']);
            $value['kodeBarang'] = $value['kodeAset'];
         }
         
         $this->mrTemplate->AddVars('data_item', $value, '');
			$this->mrTemplate->parseTemplate('data_item', 'a');
         
         $i++;
      }
      // ---------
   }
}
?>