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

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

class PrintUsulanList 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/usulan_barang/template');
      $this->SetTemplateFile('print_usulan_list.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new Usulan;
      
      // Generate data structure
      $id = explode('|', $_GET['id']);
      if (count($id) > 0) $return = $Obj->GetDataGridForPrint($id);
      else $return = array('dataGrid'=>array(), 'barangUsulan'=>array());
      // ---------
      
      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)
   {
      //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 data grid
		if (count($data['dataGrid']) == 0)
		{
			$this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
			return;
		}
		else $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
      
		$i = 1;
      foreach ($data['dataGrid'] as $value)
      {
         $this->mrTemplate->SetAttribute('barang_usulan1', 'visibility', 'hidden');
         $this->mrTemplate->SetAttribute('barang_usulan2', 'visibility', 'hidden');
         $this->mrTemplate->SetAttribute('barang_usulan3', 'visibility', 'hidden');         
			
			if (isset($data['barangUsulan'][$value['usulanBrgMstId']]))
         {
            $this->mrTemplate->SetAttribute('barang_usulan2', 'visibility', 'visible');
            $value['jumlah_item'] = count($data['barangUsulan'][$value['usulanBrgMstId']]);
            reset($data['barangUsulan'][$value['usulanBrgMstId']]);
            
            $barang = current($data['barangUsulan'][$value['usulanBrgMstId']]);
            if ($barang['usulanBrgDetJmlApprove'] == 0) $barang['usulanBrgDetJmlApprove'] = "belum diapprove";
            else $barang['usulanBrgDetJmlApprove'] = 'disetujui '.$barang['usulanBrgDetJmlApprove'];
				
				$barang['usulanBrgDetJml'] = number_format($barang['usulanBrgDetJml'],0,',','.');
				
				$this->mrTemplate->AddVars('barang_usulan2', $barang, '');
            
            while ($barang = next($data['barangUsulan'][$value['usulanBrgMstId']]))
            {
               if ($barang['usulanBrgDetJmlApprove'] == 0) $barang['usulanBrgDetJmlApprove'] = "belum diapprove";
               else $barang['usulanBrgDetJmlApprove'] = 'disetujui '.$barang['usulanBrgDetJmlApprove'];
					
					$barang['usulanBrgDetJml'] = number_format($barang['usulanBrgDetJml'],0,',','.');
					
               $this->mrTemplate->SetAttribute('barang_usulan3', 'visibility', 'visible');
               $this->mrTemplate->AddVars('barang_usulan3', $barang, '');
               $this->mrTemplate->parseTemplate('barang_usulan3', 'a');
            }
            
         }
         else $this->mrTemplate->SetAttribute('barang_usulan1', 'visibility', 'visible');
         
			$value['nomor'] = $i;
         $value['usulanBrgMstTglUsulan'] = date('d F Y', strtotime($value['usulanBrgMstTglUsulan']));
         
         $this->mrTemplate->AddVars('data_item', $value, '');
         $this->mrTemplate->parseTemplate('data_item', 'a');
         $this->mrTemplate->clearTemplate('barang_usulan3');
         
         $i++;
      }
      // ---------
   }
}
?>