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

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

class ViewKomponenList extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/pengadaan/template');
      $this->SetTemplateFile('view_komponen_list.html');
   }
   
   function ProcessRequest()
   {
      if (isset($_GET['id'])) $_SESSION['mod=pengadaan']['id'] = $_GET['id']->Integer()->Raw();
      $Obj = new Komponen;
      
      // Retrieving data about pengadaan detail
      $detail = $Obj->GetPengadaanDetail();
      $return['nominalPengadaan'] = $detail['nominal'];
      $detail['tglSPK'] = date ("d F Y", strtotime($detail['tglSPK']));
      $detail['nominal'] = number_format($detail['nominal'], 2, ',', '.');
      $detail['file_spk'] = $detail['pengadaanFileSPK'];
      $return['detail'] = $detail;
      // ---------
      
      // inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
      $this->Data = $msg[0][0];
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
      // ---------
      
      // inisialisasi filter
      $filter = array('nama' => '');
      
      if (isset($_POST['btncari']))
         $filter = array_intersect_key($_POST->AsArray(), $filter);
      elseif (isset($_GET['page']) && is_array($this->Data))
         $filter = array_intersect_key($this->Data, $filter);
      
      Messenger::Instance()->Send('pengadaan', 'komponenList', 'view', 'html', array($filter), Messenger::NextRequest);
      $return['filter'] = $filter;
      // ---------
      
      // Inisialisasi komponen paging
      $totalData = (int) $Obj->GetKomponenListCount($filter);
      $itemViewed = 10;
      if (isset($_GET['page'])) $currPage = $_GET['page']->Integer()->Raw();
      if (!isset($currPage) OR $currPage < 1) $currPage = 1;
      $startRec = ($currPage - 1) * $itemViewed;
      
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType);

      Messenger::Instance()->SendToComponent('paging', 'paging', 'view', 'html', 'paging_top', array($itemViewed, $totalData, $url, $currPage), Messenger::CurrentRequest);
      $return['start'] = $startRec+1;
      // ---------
      
      // Generate data structure
      $sumPerolehan = 0;
      if ($totalData > 0)
         $return['dataGrid'] = $Obj->GetKomponenList($filter, $startRec, $itemViewed);
      else $return['dataGrid'] = array();
      foreach (array_keys($return['dataGrid']) as $key)
      {
         $sumPerolehan += $return['dataGrid'][$key]['pengadaanDetTotalPerolehan'];
         $return['dataGrid'][$key]['pengadaanDetHargaFaktur'] = number_format($return['dataGrid'][$key]['pengadaanDetHargaFaktur'], 2, ',', '.');
         $return['dataGrid'][$key]['biayaLain'] = number_format($return['dataGrid'][$key]['biayaLain'], 2, ',', '.');
         $return['dataGrid'][$key]['pengadaanDetTotalPerolehan'] = number_format($return['dataGrid'][$key]['pengadaanDetTotalPerolehan'], 2, ',', '.');
      }
      $return['sumPerolehan'] = $sumPerolehan;
      // ---------
      
      // inisialisasi url
      $return['url']['search'] = Dispatcher::Instance()->GetUrl('pengadaan', 'komponenList', 'view', 'html');
      $return['url']['back'] = Dispatcher::Instance()->GetUrl('pengadaan', 'listPengadaan', 'view', 'html');
      $return['url']['add'] = Dispatcher::Instance()->GetUrl('pengadaan', 'komponenInput', 'view', 'html');
      $return['url']['detail'] = Dispatcher::Instance()->GetUrl('pengadaan', 'komponenDetail', 'view', 'html');
      $return['url']['delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html');
      $return['url']['preview'] = Dispatcher::Instance()->GetUrl('pengadaan', 'PopupSPK', 'view', 'html');
      // ---------
      
      // Inisialisasi komponen confirmDelete
		$label = "Detail Pengadaan";
		$urlDelete = Dispatcher::Instance()->GetUrl('pengadaan', 'komponenDelete', 'do', 'html');
		$urlReturn = Dispatcher::Instance()->GetUrl('pengadaan', 'komponenList', 'view', 'html');
		Messenger::Instance()->Send('confirm', 'confirmDelete', 'do', 'html', array($label, $urlDelete, $urlReturn),Messenger::NextRequest);
      // ---------
      
      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();
      
      // Render message box
      if($this->Pesan)
      {
			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $this->Pesan);
			$this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $this->css);
		}
      elseif ($data['sumPerolehan'] < $data['nominalPengadaan'])
      {
			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', 'Jumlah Total Perolehan (Rp. '.number_format($data['sumPerolehan'],2,',','.').') belum sama dengan Nilai Pengadaan!');
			$this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', 'notebox-alert');
		}
      // ---------
      
      // Render Pengadaan Detail
      if($data['detail']['file_spk'] <> '') $data['detail']['url_prev'] = $data['url']['preview'];
      else $data['detail']['prev_display'] = 'none';
      $this->mrTemplate->AddVars('content', $data['detail'], '');
      if ($data['detail']['pengadaanTglVerifikasi'] == '')
      {
			//$this->mrTemplate->SetAttribute('hidden1', 'visibility', 'visible');
			$this->mrTemplate->SetAttribute('hidden2', 'visibility', 'visible');
      }
      // ---------
      
      // Render URL
      $this->mrTemplate->AddVars('content', $data['url'], 'URL_');
      //$this->mrTemplate->AddVars('hidden1', $data['url'], 'URL_');
      // ---------
      
      // Render Filter Box
      $this->mrTemplate->AddVars('content', $data['filter'], '');
      // ---------
      
      // 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['url_edit'] = $data['url']['add'] . "&id=". $value['pengadaanDetId'];
         $value['url_detail'] = $data['url']['detail'] . "&id=". $value['pengadaanDetId'];
         
         $this->mrTemplate->AddVar('hidden2', 'URL_EDIT', $value['url_edit']);
         $this->mrTemplate->AddVars('data_item', $value, '');
         $this->mrTemplate->parseTemplate('data_item', 'a');
         
         $i++;
      }
      // ---------
      
      if ($data['sumPerolehan'] < $data['nominalPengadaan'])
         $this->mrTemplate->SetAttribute('warning', 'visibility', 'visible');
   }
}
?>