<?php
/*
 @ClassName : Namaclass or nama file
 @Copyright : PT Gamatechno Indosnesia
 @Analyzed By : Nanang Ruswianto <nanang@gamatechno.com>
 @Designed By : Rosyid <rosyid@gamatechno.com>
 @Author By : Dyan Galih <galih@gamatechno.com>
 @Version : 1.0
 @StartDate : Okt 16, 2008
 @LastUpdate : Okt 17, 2008
 @Description :
*/
require_once GTFWConfiguration::GetValue( 'application', 'docroot').
   'module/pengendalian_barang/business/PegendalianBarang.class.php';

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

class ViewPengendalianBarang extends HtmlResponse
{
	
	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 TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/pengendalian_barang/template');
      $this->SetTemplateFile('view_pengendalian_barang.html');
   }
   
   function ProcessRequest()
   {
   	$obj = new PengendalianBarang;
   	
      // 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();
      
      if (isset($_POST['btncari'])) $filter = $_POST->AsArray();
      elseif (isset($_GET['page']) && is_array($this->Data)) $filter = $this->Data;
      
      if (!empty($filter))
      {
         $tmp = strtotime($filter['tanggal_awal_year'].'-'.$filter['tanggal_awal_mon'].'-'.$filter['tanggal_awal_day']);
         $filter['tanggal_awal_year'] = date('Y', $tmp);
         $filter['tanggal_awal_mon'] = date('m', $tmp);
         $filter['tanggal_awal_day'] = date('d', $tmp);
         
         $tmp = strtotime($filter['tanggal_akhir_year'].'-'.$filter['tanggal_akhir_mon'].'-'.$filter['tanggal_akhir_day']);
         if ($tmp == 0) $tmp = time();
         $filter['tanggal_akhir_year'] = date('Y', $tmp);
         $filter['tanggal_akhir_mon'] = date('m', $tmp);
         $filter['tanggal_akhir_day'] = date('d', $tmp);
      } else {
			$filter['status_barang'] = 'all';
			$filter['unit_kerja'] = 'all';
			
			$filter['tanggal_awal_year'] = date('Y')-10;
         $filter['tanggal_awal_mon'] = date('m');
         $filter['tanggal_awal_day'] = date('d');
		
			$filter['tanggal_akhir_year'] = date('Y');
         $filter['tanggal_akhir_mon'] = date('m');
         $filter['tanggal_akhir_day'] = date('d');
		}
      
      Messenger::Instance()->Send(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType, array($filter), Messenger::UntilFetched);
      $return['filter'] = $filter;
      // ---------
      
   	//set combo status barang
   	$statusBarang = $obj->getStatusBarang();
   	Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'status_barang', array('status_barang', $statusBarang, $filter['status_barang'], true, ''), Messenger::CurrentRequest);
   	
   	//set combo unit kerja
   	$unitKerja = $obj->getUnitKerja();
   	Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'unit_kerja', array('unit_kerja', $unitKerja, $filter['unit_kerja'], true, ''), Messenger::CurrentRequest);
   	
      // Inisialisasi komponen tanggal
      Messenger::Instance()->SendToComponent('tanggal', 'tanggal', 'view', 'html', 'tanggal_awal', array($filter['tanggal_awal_year'].'-'.$filter['tanggal_awal_mon'].'-'.$filter['tanggal_awal_day'], 1970), Messenger::CurrentRequest);
      Messenger::Instance()->SendToComponent('tanggal', 'tanggal', 'view', 'html', 'tanggal_akhir', array($filter['tanggal_akhir_year'].'-'.$filter['tanggal_akhir_mon'].'-'.$filter['tanggal_akhir_day'], 1970), Messenger::CurrentRequest);
      // ---------
      
      // Inisialisasi komponen paging
      //$totalData = (int) $obj->GetDataGridCount($filter);
      $itemViewed = 10;
      if (isset($_GET['page'])) $currPage = $_GET['page']->Integer()->Raw();
      if (!isset($currPage) OR $currPage < 1) $currPage = 1;
      $startRec = ($currPage - 1) * $itemViewed;
      
      //get list pengendalian barang
      $return['dataGrid'] = $obj->GetDataGrid($filter, $startRec, $itemViewed);
      $totalData = $obj->GetCount();
      $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;
      // ---------
		
      // inisialisasi url
      $return['url']['search'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType);
      $return['url']['add'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'inputPengendalianBarang', 'view', 'html');
      $return['url']['detail'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'detailPengendalianBarang', 'view', 'html');
      $return['url']['log'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'logPengendalianBarang', 'view', 'html');
      $return['url']['cetak'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'cetak', 'view', 'html');
      $return['url']['excel'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'excel', 'view', 'html');
      $return['url']['delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html');
      // ---------
      
      // Inisialisasi komponen confirmDelete
      $ObjLabel = new GetModuleLabel();
      $arrLabel = $ObjLabel->GetLabel();
      
      foreach ($arrLabel as $value)
         if (strtoupper($value['labelCode']) == 'LABEL_TITLE')
            {$label = $value['labelName']; break;}
         else $label = "Menajemen Pegendalian Barang";
		
      $urlDelete = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'deletePengendalianBarang', 'do', 'html');
		$urlReturn = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType);
		Messenger::Instance()->Send('confirm', 'confirmDelete', 'do', 'html', array($label, $urlDelete, $urlReturn),Messenger::UntilFetched);
      // ---------
      
      return $return;
   }
   
   
	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);
		}
      // ---------
      
      // Render URL
      $this->mrTemplate->AddVars('content', $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['pengBrgTgl'] = date('d F Y', strtotime($value['pengBrgTgl']));
         $value['url_detail'] = $data['url']['detail'] . "&id=". $value['id'];
         $value['url_edit'] = $data['url']['add'] . "&id=". $value['id'];
         $value['url_log'] = $data['url']['log'] . "&id=". $value['id'];
         $value['pengBrgHrgJual'] = number_format($value['pengBrgHrgJual'], 2, ',', '.');
         //$value['pengBrgLokasi'] = str_replace('->', ' &raquo;&nbsp;', $value['pengBrgLokasi']);
         
         $this->mrTemplate->AddVars('data_item', $value, '');
         $this->mrTemplate->parseTemplate('data_item', 'a');
         
         $i++;
      }
      // ---------
   }
}
?>
