<?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 ViewDOInput extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/'.Dispatcher::Instance()->mModule.'/template');
      $this->SetTemplateFile('view_delivery_order_input.html');
   }
   
   function array_unique_key($array) {
		 if(!is_array($array))
		     return false;

		 $tmpId = 'id';
		 $tmpVal = array();
		 foreach($array as $key => $val) {
		     if(!in_array($val[$tmpId], $tmpVal)) 
		     {
		         $tmpVal[] = $val[$tmpId];
		         $arrayRes[] = $val;
		     } else {
		     		unset($array[$key]);
		     }
		 }
		 return $arrayRes;
	}
   
   function ProcessRequest()
   {
      $Obj = new DeliveryOrder;
      
      // inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
      $this->Data = $msg[0][0];
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
      // ---------
      
      // inisialisasi default value
      $data = array();
      if (is_array($this->Data))
         $data = $this->Data;
      if (isset($_GET['id']))
      {
         $result = $Obj->GetDOById($_GET['id']->Integer()->Raw());
         if ($result === false) unset($_GET['id']);
         else
         {
            if (!is_array($this->Data)) $data = $result;
            if ($data['pengadaanId'] == $result['pengadaanId']) $isChangeJmlAset = true;
            $diffJmlAset = $result['jumlahDO'];
         }
      }
      
      $return['data'] = $data;
      unset ($return['data']['jumlahDO']);
      // ---------
	  
      // Generate List Item Pengadaan
      $return['listItemPengadaan'] = $Obj->GetListItemPengadaanById((int)$data['pengadaanId'],$_GET['id']->Integer()->Raw());
      foreach (array_keys($return['listItemPengadaan']) as $key)
      {
         #$return['listItemPengadaan'][$key]['jumlahDO'] = (int) $data['jumlahDO'][$return['listItemPengadaan'][$key]['idMst']];
        if (isset($_GET['id'])){
      		$idBarang = $return['listItemPengadaan'][$key]['invBarangId'];
      		$idUnit = $return['listItemPengadaan'][$key]['id'];
      		$return['listItemPengadaan'][$key]['jumlahDO'] = $data['jumlahDO'][$idUnit][$idBarang];
      	}else $return['listItemPengadaan'][$key]['jumlahDO'] = (int) $return['listItemPengadaan'][$key]['pengadaanDetJmlAset'];
        if (isset($isChangeJmlAset) AND $isChangeJmlAset)
            $return['listItemPengadaan'][$key]['pengadaanDetJmlAset'] += (int) $data['jumlahDO'][$idUnit][$idBarang];
      }
      // ---------
		
      // inisialisasi link address
      $return['url']['action'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'DOInput', 'do', 'html');
      if (isset($_GET['id'])) $return['url']['action'] .= '&id='.$_GET['id']->Integer()->Raw();
      $return['url']['pop_up_pengadaan_list'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'pengadaanList', 'popup', 'html');
      // ---------
      
      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();
      $this->mrTemplate->AddVar('content', 'JUDUL', (isset($_GET['id']))?'Ubah':'Tambah');
      // ---------
      
      // render message box
      if($this->Pesan)
      {
         $msg = '';
         if (count($this->Pesan) > 1) foreach ($this->Pesan as $value)
            $msg .= "\t$value<br/>\n";
         else $msg .= $this->Pesan[0];
         
			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $msg);
			$this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $this->css);
		}
      // ---------
      
		if(!isset($data['data']['doTglDO'])){
			$datenow = date('Y-m-d+1');
			$data['data']['doTglDO'] = strtotime($datenow);
		}
		
      // render link
      $this->mrTemplate->AddVars('content', $data['url'], 'URL_');
      // ---------
      
      // render form default value
      if (isset($_GET['id'])){
      	$data['data']['mode'] = "Ubah";
      	$data['data']['show_edit'] = "none";
      }
      else $data['data']['mode'] = "Tambah";
      $this->mrTemplate->AddVars('content', $data['data'], '');
      // ---------
      
      // render list item pengadaan
		
		if (empty($data['listItemPengadaan']))
		{
			$this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
			return;
		}
		else $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
      
      $i = 1;
      foreach ($data['listItemPengadaan'] as $value)
      {
         $value['nomor'] = $i;
         $value['class_name'] = ($i%2) ? 'table-common-even' : '';
  
         $this->mrTemplate->AddVars('data_item', $value, '');
         $this->mrTemplate->parseTemplate('data_item', 'a');
         $i++;
      }
      // ---------
   }
}
?>
