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

class ViewInventarisasi extends HtmlResponse {

   #var $Pesan;

   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/'.Dispatcher::Instance()->mModule.'/template');
      $this->SetTemplateFile('view_inventarisasi.html');
   }

   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 ProcessRequest() {    	  	
      	$Obj = new AppInventarisasi();
      	
      	$msg = Messenger::Instance()->Receive(__FILE__);
      
      	$filter = array();
		if (isset($_POST['cari'])) $filter = $_POST->AsArray();
		if (isset($_GET['page'])) $filter = @$msg[0][0];
		
		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);
         
         Messenger::Instance()->Send(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType, array($filter), Messenger::UntilFetched);
         
         $itemViewed = 500;
         $currPage = 1;
         $startRec = 0 ;
         if(isset($_GET['page'])) {
         	$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
         	$startRec =($currPage-1) * $itemViewed;
         }
         
         $data_list = $Obj->GetInventarisasi($filter,$startRec,$itemViewed);
         $totalData = $Obj->GetCount();
         $totalPerolehan = $Obj->GetTotalPerolehan($filter);
         
         $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);
      	} else {	
		 $filter['tanggal_awal_year'] = date('Y')-5;
         $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');
			
		 $filter['jenis_kib'] = 'all';
		}
		
      	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);
		
		$cbxkib = $Obj->GetCbxJenisKib();
      	Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'jenis_kib', array('jenis_kib', $cbxkib, $filter['jenis_kib'], true, ''), Messenger::CurrentRequest);

      	$return['start'] = $startRec+1;
      	@$return['kode_brg'] = $filter['kode_brg'];
      	@$return['unit'] = $filter['unit'];
      	@$return['unitnama'] = $filter['unitnama'];
      	$return['totalData'] = ($totalData<>0)?$totalData:0;
      	$return['totalPerolehan'] = ($totalPerolehan <> 0) ? $totalPerolehan : 0;
      	$return['inv'] = $data_list;
      	$return['tglAwal'] = $filter['tanggal_awal_year'].'-'.$filter['tanggal_awal_mon'].'-'.$filter['tanggal_awal_day'];
      	$return['tglAkhir'] = $filter['tanggal_akhir_year'].'-'.$filter['tanggal_akhir_mon'].'-'.$filter['tanggal_akhir_day'];

      	return $return;
   }

   function ParseTemplate($data = NULL) {
      //set Label
      $this->TemplateLabeling();

      $this->mrTemplate->AddVar('content', 'URL_ACTION', Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'Inventarisasi', 'view', 'html') );
      $this->mrTemplate->AddVar('content', 'URL_CETAK', Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'LaporanIndukInventaris', 'print', 'html').'&tglAwal='.$data['tglAwal'].'&tglAkhir='.$data['tglAkhir'].'&unit='.$data['unit'].'&kode_brg='.$data['kode_brg']);
      $this->mrTemplate->AddVar('content', 'URL_EXCEL', Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'ExcelLapIndukInventaris', 'view', 'xlsx').'&tglAwal='.$data['tglAwal'].'&tglAkhir='.$data['tglAkhir'].'&unit='.$data['unit'].'&kode_brg='.$data['kode_brg']);
      $this->mrTemplate->AddVar('content', 'URL_POPUP', Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'popUpUnitPj', 'view', 'html') );
      $this->mrTemplate->AddVar('content', 'URL_POPUP_KODE_BRG',Dispatcher::Instance()->GetUrl('popup', 'kodeBarang', 'popup', 'html'));
      $this->mrTemplate->AddVar('content', 'KODE_BRG', $data['kode_brg']);
      $this->mrTemplate->AddVar('content', 'UNIT', $data['unit']);
      $this->mrTemplate->AddVar('content', 'UNITNAMA', $data['unitnama']);
      $this->mrTemplate->AddVar('content', 'TOTAL_DATA', $data['totalData']);
      $this->mrTemplate->AddVar('content', 'TOTAL_PEROLEHAN', number_format($data['totalPerolehan'],2,',','.'));
      
      if (empty($data['inv'])) {
         $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
      } else {
         $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
         $company_type = GTFWConfiguration::GetValue('application','company_type');
         if($company_type == '' || empty($company_type)) $company_type = '1';
         for($i=0; $i<count($data['inv']); $i++) {
            $nomor = $i+$data['start'];
            $data['inv'][$i]['nomor'] = $nomor;
            $data['inv'][$i]['KIB'] = ($company_type == '1') ? '<span style="border-bottom: 1px solid black">'.str_replace('|','</span><br/>',$data['inv'][$i]['KIB']) : str_replace('|','<br/><span style="border-top: 1px solid black">',$data['inv'][$i]['KIB'])."</span>";
            if($nomor % 2 != 0)
               $data['inv'][$i]['class_name'] = 'table-common-even';
            else
               $data['inv'][$i]['class_name'] = '';

            $data['inv'][$i]['lokasi_aset'] = str_replace(array(' ','@@'),array('&nbsp;', ' &raquo;&nbsp;'),$data['inv'][$i]['lokasi_aset']);

            $data['inv'][$i]['nilai_perolehan'] = number_format($data['inv'][$i]['nilai_perolehan'],2,',','.');
            //$data['inv'][$i]['nilai_buku'] = number_format($data['inv'][$i]['nilai_buku'],2,',','.');
            $data['inv'][$i]['tgl_pengadaan'] = IndonesianDate($data['inv'][$i]['tgl_pengadaan'], 'YYYY-MM-DD');
            $data['inv'][$i]['tgl_pencatatan'] = IndonesianDate($data['inv'][$i]['tgl_pencatatan'], 'YYYY-MM-DD');

          	if($data['inv'][$i]['golBarang']==4||$data['inv'][$i]['golBarang']==5||$data['inv'][$i]['golBarang']==8){
				$data['inv'][$i]['display'] = "none";
			} 
			
            $this->mrTemplate->AddVars('data_item', $data['inv'][$i], 'INV_');
            $this->mrTemplate->parseTemplate('data_item', 'a');
         }
      }

   }

}
?>