<?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 PrintDODetail extends HtmlResponse
{
   function TemplateBase() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application', 'docroot') . 'main/template/');
      $this->SetTemplateFile('document-print.html');
      $this->SetTemplateFile('layout-common-print.html');
   }
   
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/'.Dispatcher::Instance()->mModule.'/template');
      $this->SetTemplateFile('print_delivery_order_detail.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new DeliveryOrder;
      //$temp = $Obj->GetDataUserByUsername($_SESSION['username']);
      $return['roleId'] = 1;
      $Obj->roleId = 1;
      
      // inisialisasi value
      $data = $Obj->GetDODetailForPrint($_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'];
      // ---------
      
      $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 Header
      $data['header']['doTglDO'] = date('d F Y', strtotime($data['header']['doTglDO']));
      
      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'] : 'Belum di ACC');
      }
      else
      {
         $data['header']['vartitle1'] = 'Unit Penerima';
         $data['header']['varvalue1'] = $data['header']['unitkerjaNama'];
         $data['header']['vartitle2'] = 'Lokasi';
      	$this->mrTemplate->SetAttribute('roleUnit1', 'visibility', 'visible');
         $this->mrTemplate->AddVars('roleUnit1', $data['header'], '');
      }
      $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');
      
		$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,',','.');
         $value['kodeBarang'] = $value['kodeAset'];
         
         if (($temp = strtotime($value['doUnitTglTerima'])) == 0)
            $value['doUnitTglTerima'] = 'Belum diterima';
         else $value['doUnitTglTerima'] = date('d F Y', $temp);
         
         if ($data['roleId'] != 1) $value['varvalue2'] = $value['ruangNama'];
         else  $value['varvalue2'] = $value['jumlah'];
         
         $this->mrTemplate->AddVars('data_item', $value, '');
			$this->mrTemplate->parseTemplate('data_item', 'a');
         
         $i++;
      }
      // ---------
   }
}
?>