<?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 PrintDOList 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_list.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;
      }
      
      #TODO : print list berdasarkan role
      
      // Generate data structure
      $id = explode('|', $_GET['id']->Raw());
      if (empty($id)) $return['dataGrid'] = array();
      else $return['dataGrid'] = $Obj->GetDOListForPrint($id);
      // ---------
      
      return $return;
   }
   
   function TemplateLabeling(){
      $ObjLabel = new GetModuleLabel();
      $arrLabel = $ObjLabel->GetLabel();
      
      //patTemplate name yang memiliki label2 dinamic
      $arrContent=array("content","subtitle_role_pusat","subtitle_role_unit");
      
      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)
   {
      //set Label
      $this->TemplateLabeling();
      
		$kota = GTFWConfiguration::GetValue('organization', 'city');
      $tanggal = date('Y-m-d');
		$user = $_SESSION['username'];
      
		$this->mrTemplate->AddVar('content', 'KOTA', $kota);
		$this->mrTemplate->AddVar('content', 'TANGGAL', $tanggal);
		$this->mrTemplate->AddVar('content', 'USER', $user);
		
      // Render title for Role Pusat or Role Uni
      if ($data['roleId'] == 1)
      {
         $var['vartitle1'] = 'Unit Tujuan';
         $var['vartitle2'] = 'Penanggung Jawab';
         $this->mrTemplate->SetAttribute('subtitle_role_unit', 'visibility', 'hidden');
      }
      else
      {
         $var['vartitle1'] = 'Tanggal Terima';
         $var['vartitle2'] = 'PIC Penerima Barang';
         $this->mrTemplate->SetAttribute('subtitle_role_pusat', 'visibility', 'hidden');
      }
      // ---------
      
      $this->mrTemplate->AddVars('content', $var, '');
      
      // Render data grid
		if (empty($data['dataGrid']))
		{
			$this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
			return;
		}
		else $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
      
		$i = 1;
      foreach ($data['dataGrid'] as $value)
      {
			$value['nomor'] = $i;
         $value['doTglDo'] = date('d F Y', strtotime($value['doTglDo']));
         
         if ($data['roleId'] == 1)
         {
            $value['variable1'] = $value['unitkerjaNama'];
            $value['variable2'] = $value['penanggungJawab'];
         }
         else
         {
            $value['variable1'] = ($value['doTglTerima']) ? $value['doTglTerima'] : 'Belum di ACC';
            $value['variable2'] = ($value['doPICPenerima']) ? $value['doPICPenerima'] : 'Belum di ACC';
         }
         
         $this->mrTemplate->AddVars('data_item', $value, '');
         $this->mrTemplate->parseTemplate('data_item', 'a');
         
         $i++;
      }
      // ---------
   }
}
?>