<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/'.Dispatcher::Instance()->mModule.'/business/AppInventarisasi.class.php';
//set Label for module
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .
'module/label/response/Label.proc.class.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();
      $User = $Obj->GetDataUserByUsername($_SESSION['username']);

      // inisialisasi messaging
		@$msg = Messenger::Instance()->Receive(__FILE__);
		@$this->Pesan = $msg[0][1];
		@$this->css = $msg[0][2];
      // End
	  
      if (isset($_GET['page'])) $post = @$msg[0][0];
      else $post = $_POST->AsArray();

      if (empty($post)) $post = array
      (
         "jenis_kib" => "all",
         "tahun" => "all",
         "sbr_dana" => "all",
         "kepemilikan" => "all",
         "lokasi" => "all",
         "kondisi" => "all",
         "status" => "1",
         "unit_pj_id" => "",
         "key" => ""
      );
      else Messenger::Instance()->Send(Dispatcher::Instance()->mModule, 'inventarisasi', 'view', 'html', array($post), Messenger::UntilFetched);

      if ($User['role_id'] != 1)
      {
         $unit = $Obj->GetDataUnitByUnitname($post['unit_pj']);
         if ($User['unit_kerja_id'] != $unit['unitkerjaId'] AND $User['unit_kerja_id'] != $unit['unitkerjaParentId'])
            $post['unit_pj'] = $User['unit_kerja_nama'];
      }

      $return['filter_cetak'] = $post;
      $key = $post['key'];
      if(!empty($post['tahun']))
         $tahun = $post['tahun'];
      else
         $tahun = date("Y");

      //combo
      $cbxkib = $Obj->GetCbxJenisKibAll();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'jenis_kib', array('jenis_kib', $cbxkib, $post['jenis_kib'], true, ''), Messenger::CurrentRequest);

      $cbxsumberdana = $Obj->GetCbxSumberDana();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'sbr_dana', array('sbr_dana', $cbxsumberdana, $post['sbr_dana'], true, ''), Messenger::CurrentRequest);

      $cbxkepemilikan = $Obj->GetCbxKepemilikan();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'kepemilikan', array('kepemilikan', $cbxkepemilikan, $post['kepemilikan'], true, ''), Messenger::CurrentRequest);

      $cbxlokasi = $Obj->GetCbxLokasiKampus();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'lokasi', array('lokasi', $cbxlokasi, $post['lokasi'], true, ''), Messenger::CurrentRequest);

      $cbxkondisi = $Obj->GetCbxKondisi();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'kondisi', array('kondisi', $cbxkondisi, $post['kondisi'], true, ''), Messenger::CurrentRequest);
		
		$cbxstatus = $Obj->GetCbxStatusAset();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'status', array('status', $cbxstatus, $post['status'], true, ''), Messenger::CurrentRequest);

      $cbxkib = $Obj->ConvertCombo($cbxkib);
      $cbxsumberdana = $Obj->ConvertCombo($cbxsumberdana);
      $cbxkepemilikan = $Obj->ConvertCombo($cbxkepemilikan);
      $cbxlokasi = $Obj->ConvertCombo($cbxlokasi);
      $cbxkondisi = $Obj->ConvertCombo($cbxkondisi);
      $cbxstatus = $Obj->ConvertCombo($cbxstatus);

      @$return['filter'] = array
      (
         "JENIS_KIB" => ($cbxkib[$post["jenis_kib"]]) ? $cbxkib[$post["jenis_kib"]] : 'Semua',
         "THN" => ($post["tahun"] == 'all') ? 'Semua' : $post["tahun"],
         "SBR_DANA" => ($cbxsumberdana[$post["sbr_dana"]]) ? $cbxsumberdana[$post["sbr_dana"]] : 'Semua',
         "KEPEMILIKAN" => ($cbxkepemilikan[$post["kepemilikan"]]) ? $cbxkepemilikan[$post["kepemilikan"]] : 'Semua',
         "LOKASI" => ($cbxlokasi[$post["lokasi"]]) ? $cbxlokasi[$post["lokasi"]] : 'Semua',
         "KONDISI" => ($cbxkondisi[$post["kondisi"]]) ? $cbxkondisi[$post["kondisi"]] : 'Semua',
         "STATUS" => ($cbxstatus[$post["status"]]) ? $cbxstatus[$post["status"]] : 'Semua',
         "UNIT" => ($post["unit_pj"]) ? $post["unit_pj"] : 'Semua',
         "KODE" => ($post["key"]) ? $post["key"] : 'Semua'
      );
      
      $itemViewed = 20;
      $currPage = 1;
      $startRec = 0 ;
      if(isset($_GET['page'])) {
         $currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
         $startRec =($currPage-1) * $itemViewed;
      }


      $data_list = $Obj->GetInventarisasi($post, $startRec,$itemViewed);
      //$totalData = $Obj->GetCountInventarisasi($post);
      $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['unit_pj'] = $post['unit_pj'];
      $return['unit_pj_id'] = $post['unit_pj_id'];
      $return['key'] = $key;
      $return['inv'] = $data_list;
      $return['start'] = $startRec+1;
      $return['cbx_thn'] = $post['tahun'];
      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);
		}
      // ---------



      if(!empty($data['cbx_thn']))
         $cbx_tahun = $data['cbx_thn'];
      $this->GenerateTahun($cbx_tahun);

      $this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('inventarisasi_detail', 'Inventarisasi', 'view', 'html') );
      $this->mrTemplate->AddVar('content', 'URL_CETAK_RESIZE2', Dispatcher::Instance()->GetUrl('inventarisasi_detail', 'popupResizeQrcode', 'view', 'html') );
      $this->mrTemplate->AddVar('content', 'URL_CETAK_RESIZE', Dispatcher::Instance()->GetUrl('inventarisasi_detail', 'popupResize', 'view', 'html') );
      $this->mrTemplate->AddVar('content', 'URL_CETAK_KIB', Dispatcher::Instance()->GetUrl('inventarisasi_detail', 'cetakKib', 'view', 'html') );
      $this->mrTemplate->AddVar('content', 'URL_CETAK', Dispatcher::Instance()->GetUrl('inventarisasi_detail', 'cetakInventarisasi', 'view', 'html'));
      //$this->mrTemplate->AddVar('content', 'URL_CETAK', Dispatcher::Instance()->GetUrl('inventarisasi_detail', 'cetakInventarisasi', 'view', 'html')."&filter=".urlencode(base64_encode(gzcompress(serialize($data['filter_cetak'])))) );
      $this->mrTemplate->AddVar('content', 'URL_EXCEL', Dispatcher::Instance()->GetUrl('inventarisasi_detail', 'ExcelInventarisasi', 'view', 'xls')."&filter=".urlencode(base64_encode(gzcompress(serialize($data['filter_cetak'])))) );
      $this->mrTemplate->AddVar('content', 'URL_REINV', Dispatcher::Instance()->GetUrl('inventarisasi_detail', 'ReInventarisasi', 'view', 'html') );
      $this->mrTemplate->AddVar('content', 'URL_POP_UP_UNIT_PJ', Dispatcher::Instance()->GetUrl('inventarisasi_detail', 'popUpUnitPj', 'view', 'html') );
      $this->mrTemplate->AddVar('content', 'UNIT_PJ', $data['unit_pj']);
      $this->mrTemplate->AddVar('content', 'UNIT_PJ_ID', $data['unit_pj_id']);
      $this->mrTemplate->AddVar('content', 'KEY', $data['key']);
      
      // Filter 
      $this->mrTemplate->AddVar('content','FILTER_JENIS_KIB',$data['filter']['JENIS_KIB']);
      $this->mrTemplate->AddVar('content','FILTER_THN',$data['filter']['THN']);
      $this->mrTemplate->AddVar('content','FILTER_SBR_DANA',$data['filter']['SBR_DANA']);
      $this->mrTemplate->AddVar('content','FILTER_KEPEMILIKAN',$data['filter']['KEPEMILIKAN']);
      $this->mrTemplate->AddVar('content','FILTER_LOKASI',$data['filter']['LOKASI']);
      $this->mrTemplate->AddVar('content','FILTER_KONDISI',$data['filter']['KONDISI']);
      $this->mrTemplate->AddVar('content','FILTER_STATUS',$data['filter']['STATUS']);
      $this->mrTemplate->AddVar('content','FILTER_UNIT',$data['filter']['UNIT']);
      $this->mrTemplate->AddVar('content','FILTER_KODE',$data['filter']['KODE']);
      //$this->mrTemplate->AddVars('content', $data['filter'], 'FILTER_');
      
      if (empty($data['inv'])) {
         $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
      } else {
         $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
         $nomor = 1;
         for($i=0; $i<count($data['inv']); $i++) {
            $nomor = $i+$data['start'];
            $data['inv'][$i]['nomor'] = $nomor;
            $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]['url_detil'] = Dispatcher::Instance()->GetUrl('inventarisasi_detail', 'detailInventarisasi', 'view', 'html').'&id='.$data['inv'][$i]['id'];
            $data['inv'][$i]['url_edit'] = Dispatcher::Instance()->GetUrl('inventarisasi_detail', 'inventarisasiEdit', 'view', 'html').'&id='.$data['inv'][$i]['id'];
				if($data['inv'][$i]['golBarang']==4||$data['inv'][$i]['golBarang']==5||$data['inv'][$i]['golBarang']==8){
					$data['inv'][$i]['display'] = "none";
				} 
            $data['inv'][$i]['url_log_lokasi'] = Dispatcher::Instance()->GetUrl('inventarisasi_detail', 'logLokasi', 'view', 'html').'&id='.$data['inv'][$i]['id'];
            $data['inv'][$i]['url_log_kondisi'] = Dispatcher::Instance()->GetUrl('inventarisasi_detail', 'logKondisi', 'view', 'html').'&id='.$data['inv'][$i]['id'];
            $data['inv'][$i]['url_penyesuaian'] = Dispatcher::Instance()->GetUrl('inventarisasi_detail', 'inventarisasiPenyesuaian', 'view', 'html').'&id='.$data['inv'][$i]['id'];
				$data['inv'][$i]['url_delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').
					"&urlDelete=".Dispatcher::Instance()->Encrypt('inventarisasi|deleteInventarisasi|do|json').
					"&urlReturn=".Dispatcher::Instance()->Encrypt('inventarisasi|inventarisasi|view|html').
					"&label=".Dispatcher::Instance()->Encrypt('Inventarisasi').
					"&id=".Dispatcher::Instance()->Encrypt($data['inv'][$i]['id']).
					"&dataName=".Dispatcher::Instance()->Encrypt(htmlentities($data['inv'][$i]['KIB']));
			
            $this->mrTemplate->AddVars('data_item', $data['inv'][$i], 'INV_');
            $this->mrTemplate->parseTemplate('data_item', 'a');
         }
      }

   }

   function GenerateTahun($default = NULL) {
      $now = (int) date('Y')+4;
      $now++;

      for ($i=0; $i < 110; $i++) {
         $this->mrTemplate->addVar("cbxtahun", "ID_TAHUN", $now);
         $this->mrTemplate->addVar("cbxtahun", "TAHUN", $now);
         if ($default == $now) {
            $this->mrTemplate->addVar("cbxtahun", "TAHUN_SELECTED", "selected");
         }else{
            $this->mrTemplate->addVar("cbxtahun", "TAHUN_SELECTED", "");
         }
         $now--;
         $this->mrTemplate->parseTemplate("cbxtahun","a");
      }
   }

}
?>