<?php
require_once GTFWConfiguration::GetValue('application', 'docroot').'module/'.Dispatcher::Instance()->mModule.'/business/Pengelolaan.class.php';
require_once GTFWConfiguration::GetValue('application', 'docroot') . 'main/function/date.php';

require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/setting/business/Setting.class.php';
require_once GTFWConfiguration::GetValue('application', 'docroot') . 'module/tanggal/business/Tanggal.class.php';

class ViewPengelolaan extends HtmlResponse
{
	function TemplateModule ()
	{
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/'.Dispatcher::Instance()->mModule.'/template');
		$this->SetTemplateFile('view_pengelolaan.html');
	}
	
	function ProcessRequest ()
   {
		$Obj = new Pengelolaan();
		$Obj2 = new Setting();
		$obj3 = new Tanggal();
		
		$arrSetting = $Obj2->GetSetting();
		// inisialisasi messaging
		
		
      // ---------
		$_POST= $_POST->AsArray();
		$monthArr = $obj3->GetArrayMonthCb();
		for($i=0; $i <= 20; $i++){
			$yearArr[] = array('id' => date('Y')-$i,'name' => date('Y')-$i);
		}
	
		if($_POST || isset($_GET['cari'])) 
		{
			if(isset($_POST['unit_id'])) 
			{
				$notrans = $_POST['notransaksi'];
				$nama = $_POST['unit_nama'];				
				$id = $_POST['unit_id'];
				$bulan = ($_POST['bulan'] == 'all') ? '' : $_POST['bulan'];
				$tahun = ($_POST['tahun'] == 'all') ? '' : $_POST['tahun'];				
			} 
				elseif(isset($_GET['notransaksi'])) 
			{
				$notrans = Dispatcher::Instance()->Decrypt($_GET['notransaksi']);
				$nama = Dispatcher::Instance()->Decrypt($_GET['unit_nama']);				
			} 
				else 
			{
				$notrans = '';
				$nama = '';				
				$id = '';
				$bulan = '';
				$tahun = '';				
			}
		}

		//$totalData = $Obj->GetCountDataPengelolaan($notrans, $id, $arrSetting['3']['settingValue']);
		$itemViewed = 100000;
		$currPage = 1;
		$startRec = 0 ;
		
		if(isset($_GET['page'])) 
		{
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
		
		$data=$Obj->GetDataPengelolaan($startRec, $itemViewed, $notrans, $id, $arrSetting['3']['settingValue'],$bulan,$tahun);
		
		/* $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&notrans=' . Dispatcher::Instance()->Encrypt($notrans). '&unit_nama=' . Dispatcher::Instance()->Encrypt($nama).'&cari=' . Dispatcher::Instance()->Encrypt(1));
		Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest); */

		Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'bulan',array('bulan', $monthArr, $bulan, 'true', 'id="bulan"'),Messenger::CurrentRequest);
		Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'tahun',array('tahun', $yearArr, $tahun, 'true', 'id="tahun"'),Messenger::CurrentRequest);
		
		$return['data'] = $data;
		$return['search']['notrans'] = $notrans;
		$return['search']['unit_nama'] = $nama;
		$return['search']['unit_id'] = $id;
		$return['search']['bulan'] = $bulan;
		$return['search']['tahun'] = $tahun;
		return $return;
	}
	
	
	function ParseTemplate ($data = NULL)
	{
		$search = $data['search'];
		$this->mrTemplate->AddVar('content', 'NOTRANS', $search['notrans']);
		$this->mrTemplate->AddVar('content', 'UNIT_NAMA', $search['unit_nama']);
		$this->mrTemplate->AddVar('content', 'UNIT_ID', $search['unit_id']);
		
		$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('laporan_pengelolaan_bhp', 'pengelolaan', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'CETAK_EXEL', Dispatcher::Instance()->GetUrl('laporan_pengelolaan_bhp', 'laporanPengelolaan', 'print', 'xlsx')."&id=".$search['unit_id']."&no=".$search['notrans']."&mon=".$search['bulan']."&year=".$search['tahun']);
		$this->mrTemplate->AddVar('content', 'CETAK_HTML', Dispatcher::Instance()->GetUrl('laporan_pengelolaan_bhp', 'laporanPengelolaan', 'print', 'html')."&id=".$search['unit_id']."&no=".$search['notrans']."&mon=".$search['bulan']."&year=".$search['tahun']);
		
		$this->mrTemplate->AddVar('content', 'URL_UNIT', Dispatcher::Instance()->GetUrl('inventarisasi', 'popUpUnitPj', 'view', 'html'));
			 
		if (empty($data['data']))
		{
			$this->mrTemplate->addVar('data_grid', 'IS_DATA_EMPTY', 'YES');
			return;
		}
		else 
		{	
			$this->mrTemplate->addVar('data_grid', 'IS_DATA_EMPTY', 'NO');
			   
			$data = $data['data'];
			for ($i=0; $i<sizeof($data); $i++) 
			{
				$no = $i+1;
				$data[$i]['number'] = $no;
				if ($no % 2 != 0) $data[$i]['class_name'] = 'table-common-even';
				else $data[$i]['class_name'] = '';
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($data)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				
				$idEnc = Dispatcher::Instance()->Encrypt($data[$i]['trans_id']);
				
				$data[$i]['trans_tanggal'] = IndonesianDate($data[$i]['trans_tanggal'], 'yyyy-mm-dd');
				$data[$i]['url_detail'] = Dispatcher::Instance()->GetUrl('laporan_pengelolaan_bhp','detilPengelolaan', 'popup', 'html') . '&id=' . $idEnc;				
				
				$this->mrTemplate->AddVars('data_list', $data[$i], 'DATA_');
				$this->mrTemplate->parseTemplate('data_list', 'a');	 
			}
		}	
	}
}
?>
