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

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

class ViewDODetail extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/'.Dispatcher::Instance()->mModule.'/template');
      $this->SetTemplateFile('view_delivery_order_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 messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
      $this->Data = $msg[0][0];
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
      // ---------
      
      // inisialisasi value
      $data = $Obj->GetDODetail($_GET['id']->Integer()->Raw(),'');
      
      if ($data === false) // Unit break attempt
      {
         $urlRedirect = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'DOList', 'view', 'html');
         if (isset($_GET['ascomponent']) AND $_GET['ascomponent']->Raw()) $urlRedirect .= "&ascomponent=1";
         $this->RedirectTo($urlRedirect) ;
         return NULL;
      }
      
      $return['header'] = $data['header'];
      
      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'];
      }
      // ---------
      
      // inisialisasi url
      $return['url']['back'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'DOList', 'view', 'html');
      $return['url']['action'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'penerimaan', 'do', 'html')."&id=".$_GET['id'];
      // ---------
      $combo = $Obj->GetComboUnitDetail($_GET['id']);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'unitId',array('unitId', $combo, $comboSelect, 'false', 'onChange=refreshTable(this.value,\''.$_GET['id'].'\')'),Messenger::CurrentRequest);
      
      #$return['comboRuang'] = $Obj->GetComboRuang();
      $return['start'] = 1;
      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();
      
      // Render message box
      if($this->Pesan)
      {
         $msg = '';
         if (is_array($this->Pesan)) foreach ($this->Pesan as $value)
            $msg .= "\t$value<br/>\n";
         else $msg .= $this->Pesan;
         
			$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 Header
      if ($data['roleId'] == 1)
      {
         $data['header']['vartitle1'] = 'Unit Tujuan';
         $data['header']['varvalue1'] = $data['header']['unitkerjaNama'];
         $data['header']['vartitle2'] = 'Jumlah';
			$this->mrTemplate->SetAttribute('rolePusat1', 'visibility', 'visible');
         $this->mrTemplate->AddVar('rolePusat1', 'DOPICPENERIMA', $data['header']['doPICPenerima'] ? $data['header']['doPICPenerima'] : '<i>Belum di ACC</i>');
         $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('roleUnit1', 'visibility', 'visible');
         $this->mrTemplate->AddVars('roleUnit1', $data['header'], '');
         $this->mrTemplate->SetAttribute('roleUnit2', 'visibility', 'visible');
         $this->mrTemplate->SetAttribute('roleUnit3', 'visibility', 'visible');
      }
      $this->mrTemplate->AddVars('content', $data['header'], '');
      // ---------
      
      // Render URL
      $this->mrTemplate->AddVars('content', $data['url'], 'URL_');
      // ---------
      
      // 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'] = '<em>Belum diterima</em>';
         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++;
      }
      // ---------
   }
}
?>