<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/laporan_pengolahan_atk/business/AppPengolahanAtk.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/setting/business/Setting.class.php';

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

class ViewLapPengolahanAtk extends HtmlResponse {

   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/laporan_pengolahan_atk/template');
      $this->SetTemplateFile('view_laporan_pengolahan_atk.html');
   }
	
   function ProcessRequest() {
      $Obj = new AppPengolahanAtk();
      $Obj2 = new Setting();
      
		//set combo kib
      $arrSetting = $Obj2->GetSetting();
		$arrComboGudang = $Obj->GetComboGudang();
      $arrStockBarang = $Obj->GetStockBarang();

      if (count($_POST) == 0)
      {
         $_POST = array();
         $_POST['gudang'] = $arrSetting['1']['settingValue'];
         $_POST['nama_barang_atk'] = '';
         $_POST['stock_barang'] = 'all';
      }
		
		$return['search'] = $_POST;
      $return['data'] = $Obj->GetAtkList($_POST['gudang'],$_POST['nama_barang_atk'],$_POST['stock_barang']);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'stock_barang', array('stock_barang', $arrStockBarang, $_POST['stock_barang'], true, ''), Messenger::CurrentRequest);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'gudang', array('gudang', $arrComboGudang, $_POST['gudang'], true, ''), Messenger::CurrentRequest);

      return $return;
   }

   function ParseTemplate($data = NULL) {

		$gudang = $data['search']['gudang'];
		$barang = $data['search']['nama_barang_atk'];
		$stok = $data['search']['stock_barang'];

		$urlAction = Dispatcher::Instance()->GetUrl('laporan_pengolahan_atk', 'lapPengolahanAtk', 'view', 'html');
		$this->mrTemplate->addVar('content','URL_ACTION',$urlAction);

		$this->mrTemplate->addVar('content','NAMA_BARANG_ATK',$barang);

		$urlCetak = Dispatcher::Instance()->GetUrl('laporan_pengolahan_atk', 'lapCetakAtk', 'view', 'html').'&gudang='.$gudang.'&barang='.$barang.'&stok='.$stok;
		$urlCetakXls = Dispatcher::Instance()->GetUrl('laporan_pengolahan_atk', 'lapCetakAtk', 'view', 'xlsx').'&gudang='.$gudang.'&barang='.$barang.'&stok='.$stok;
		$this->mrTemplate->addVar('content','URL_CETAK',$urlCetak);
		$this->mrTemplate->addVar('content','URL_CETAK_XLS',$urlCetakXls);

		if(empty($data['data'])){
			$this->mrTemplate->AddVar('data','DATA_EMPTY','YES');
			return;
		} else
			$this->mrTemplate->AddVar('data','DATA_EMPTY','NO');

			$dataGrid = $data['data'];
			$no = 1;

			for($i=0;$i<count($dataGrid);$i++){
				$dataGrid[$i]['no'] = $no+$i;
				$dataGrid[$i]['class_name'] = ($i % 2) ? '' : 'table-common-even';
				
            if($dataGrid[$i]['jumlah_stok']<=0){
					$dataGrid[$i]['display'] = 'none';				
				}
			
            $stok_minimal = round($dataGrid[$i]['stok_minimal']);
            if ($stok_minimal == 0) $stok_minimal = 1;
            if (round($dataGrid[$i]['jumlah_stok']) <= $stok_minimal*1.2) $dataGrid[$i]['class_name'] = 'table-common-warning';
            if (round($dataGrid[$i]['jumlah_stok']) <= $stok_minimal) $dataGrid[$i]['class_name'] = 'table-common-warning';
          
				if(is_numeric($dataGrid[$i]['stok_minimal'])){
					$dataGrid[$i]['stok_minimal'] = number_format($dataGrid[$i]['stok_minimal']);
            }
				
				if( is_numeric($dataGrid[$i]['jumlah_stok'])){
					$dataGrid[$i]['jumlah_stok'] = number_format($dataGrid[$i]['jumlah_stok']);
				}
				
				$this->mrTemplate->AddVars('data_item',$dataGrid[$i],'PB_');
            $this->mrTemplate->parseTemplate('data_item','a');
         }
   }
}
?>
