<?php
require_once GTFWConfiguration::GetValue('application', 'docroot') .
   'module/inventarisasi/business/AppInventarisasi.class.php';

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

class ViewCetakInventarisasi extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/inventarisasi/template');
      $this->SetTemplateFile('view_cetak_inventarisasi.html');
   }
   
    function TemplateBase()
    {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application', 'docroot') . 'main/template/');
      $this->SetTemplateFile('document-print.html');
      $this->SetTemplateFile('layout-common-print.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new AppInventarisasi();
      
      // inisialisasi filter
      $filter = explode('|', $_GET['id']->Raw());
      // ---------
      
      // inisialisasi dataGrid
      $return['data'] = $Obj->GetInventarisasiForExcel($filter);
      $return['biayaLain'] = $Obj->GetListBiayaLain();
      $return['biaya'] = $Obj->ConvertCombo($return['biayaLain']);
      $return['batas'] = array('Utara', 'Selatan', 'Timur', 'Barat');
      $return['bukti'] = array('No Mesin', 'No Rangka', 'No Polisi', 'No Lain', 'Warna');
      // ---------
      
      return $return;
   }
   
   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 ParseTemplate($data = NULL)
   {
		if(empty($data['data'])){
			echo "Belum ada data yang dipilih";
			exit;
		}
      extract ($data);
      $keys = implode('|', array_keys($data));
      
      //set Label
      $this->TemplateLabeling();
      
      // KIB A: Tanah
      preg_match('@[^|]*KIB\sA[^|]*@', $keys, $match);
      if (!empty($match))
      {
         $key = $match[0];
         $content['jenis_kib'] = $key;
         
         // create header
         $header = array
         (
            "Kode Aset" => '',
            "Nama Aset" => '',
            "Label Aset" => '',
            "Tahun Pembelian" => '',
            "Sumber Dana" => '',
            "Umur Ekonomis" => '',
            "Luas Tanah" => '',
            "Nilai Perolehan per Meter" => '',
            "Nilai Faktur" => '',
            "Biaya Lain" => $biaya,
            "Nilai Perolehan" => '',
            "NJOP" => '',
            "Lokasi" => '',
            "Kegunaan" => '',
            "Landasan Legal" => array('Sertifikat', 'Akta'),
            "Pagar" => '',
            "Batas-batas" => $batas,
            "Kepemilikan" => '',
            "Penguasaan" => '',
            "Lokasi Kampus" => '',
            "Status Aset" => '',
            "Keterangan Lain" => '',
            "Unit Penanggung Jawab Aset" => ''
         );
         $content['table_header'] = $this->create_header($header);
         
         $content['table_body'] = '';
         foreach ($data[$key] as $value)
         {
            $content['table_body'] .= "   <tr>";
            
            if (preg_match('/tidak/',$value['invPagar']) > 0)
               $value['invPagar'] = "Tidak Ada";
            $rowResult = array
            (
               $value['invKodeAset'],
               $value['namaAset'],
               $value['invMstLabel'],
               $value['invTglPembelian'],
               $value['invSumberDana'],
               $value['invUmurEkonomis'],
               $value['invLuasTanah'],
               number_format($value['invNilaiPerolehanSatuan'],2,',','.'),
               number_format($value['invNilaiFaktur'],2,',','.')
            );
            foreach ($biayaLain as $BL) $rowResult[] = number_format($value['biaya_lain'][$BL['id']],2,',','.');
            array_push($rowResult,
               number_format($value['invNilaiPerolehanSatuan'],2,',','.'),
               $value['invNJOP'],
               $value['invLokasi'],
               $value['invKegunaan'],
               $value['invSertifikat'],
               $value['invAkta'],
               $value['invPagar'],
               $value['invBatasUtara'],
               $value['invBatasSelatan'],
               $value['invBatasTimur'],
               $value['invBatasBarat'],
               $value['invKepemilikan'],
               $value['invPenguasaanBarang'],
               $value['invKampus'],
               $value['statusAsetNama'],
               $value['invKeteranganLain'],
               $value['unitkerjaNama']
            );
            
            foreach ($rowResult as $value) $content['table_body'] .= "<td bgcolor=white>$value</td>";
            $content['table_body'] .= "</tr>\r\n";
         }
         
         $this->mrTemplate->AddVars('content', $content, '');
         $this->mrTemplate->parseTemplate('content', 'a');
      }
      unset($data[@$key]);
      // ---------
      
      // KIB B: Peralatan dan Mesin
      preg_match('@[^|]*KIB\sB[^|]*@', $keys, $match);
      if (!empty($match))
      {
         $key = $match[0];
         $content['jenis_kib'] = $key;
         
         // create header
         $header = array
         (
            "Nomor Barcode" => '',
            "Kode Aset" => '',
            "Nama Aset" => '',
            "Label Aset" => '',
            "Merek" => '',
            "Spesifikasi" => '',
            "Tahun Pembelian" => '',
            "Sumber Dana" => '',
            "Umur Ekonomis" => '',
            "Nilai Faktur" => '',
            "Biaya Lain" => $biaya,
            "Nilai Perolehan" => '',
            "Nilai Residu" => '',
            "Landasan Legal" => array('BPKB', 'STNK'),
            "Bukti Kendaraan" => $bukti,
            "Kepemilikan" => '',
            "Penguasaan" => '',
            "Kondisi Aset" => '',
            "Status Aset" => '',
            "Keterangan Lain" => '',
            "Unit Penanggung Jawab Aset" => ''
         );
         $content['table_header'] = $this->create_header($header);
         
         $content['table_body'] = '';
         foreach ($data[$key] as $value)
         {
            $content['table_body'] .= "   <tr>";
            
            if ($value['invBPKB'] == '') $value['invBPKB'] = "Tidak Ada";
            if ($value['invSTNK'] == '') $value['invSTNK'] = "Tidak Ada";
            $rowResult = array
            (
               $value['invBarcode'],
               $value['invKodeAset'],
               $value['namaAset'],
               $value['invMstLabel'],
               $value['invMerek'],
               $value['invSpesifikasi'],
               $value['invTglPembelian'],
               $value['invSumberDana'],
               $value['invUmurEkonomis'],
               number_format($value['invNilaiFaktur'],2,',','.')
            );
            foreach ($biayaLain as $BL) $rowResult[] = number_format($value['biaya_lain'][$BL['id']],2,',','.');
            array_push($rowResult,
               number_format($value['invNilaiPerolehanSatuan'],2,',','.'),
               number_format($value['invNilaiResidu'],2,',','.'),
               $value['invBPKB'],
               $value['invSTNK'],
               $value['invNoMesin'],
               $value['invNoRangka'],
               $value['invNoPolisi'],
               $value['invNoLain'],
               $value['invWarna'],
               $value['invKepemilikan'],
               $value['invPenguasaanBarang'],
               $value['invKondisi'],
               $value['statusAsetNama'],
               $value['invKeteranganLain'],
               $value['unitkerjaNama']
            );
            
            foreach ($rowResult as $value) $content['table_body'] .= "<td bgcolor=white>$value</td>";
            $content['table_body'] .= "</tr>\r\n";
         }
         
         $this->mrTemplate->AddVars('content', $content, '');
         $this->mrTemplate->parseTemplate('content', 'a');
      }
      unset($data[@$key]);
      // ---------
      
      // KIB E: Aset Lainnya
      preg_match('@[^|]*KIB\sE[^|]*@', $keys, $match);
      if (!empty($match))
      {
         $key = $match[0];
         $content['jenis_kib'] = $key;
         
         // create header
         $header = array
         (
            "Nomor Barcode" => '',
            "Kode Aset" => '',
            "Nama Aset" => '',
            "Label Aset" => '',
            "Merek" => '',
            "Spesifikasi" => '',
            "Tahun Pembelian" => '',
            "Sumber Dana" => '',
            "Umur Ekonomis" => '',
            "Jumlah Aset" => '',
            "Nilai Perolehan per Aset" => '',
            "Biaya Lain" => $biaya,
            "Nilai Perolehan" => '',
            "Nilai Residu" => '',
            "Kepemilikan" => '',
            "Penguasaan" => '',
            "Kondisi Aset" => '',
            "Lokasi Kampus" => '',
            "Lokasi Aset" => '',
            "Status Aset" => '',
            "Keterangan Lain" => '',
            "Unit Penanggung Jawab Aset" => ''
         );
         $content['table_header'] = $this->create_header($header);
         
         $content['table_body'] = '';
         foreach ($data[$key] as $value)
         {
            $content['table_body'] .= "   <tr>";
            
            if ($value['invBPKB'] == '') $value['invBPKB'] = "Tidak Ada";
            if ($value['invSTNK'] == '') $value['invSTNK'] = "Tidak Ada";
            $rowResult = array
            (
               $value['invBarcode'],
               $value['invKodeAset'],
               $value['namaAset'],
               $value['invMstLabel'],
               $value['invMerek'],
               $value['invSpesifikasi'],
               $value['invTglPembelian'],
               $value['invSumberDana'],
               $value['invUmurEkonomis'],
               $value['invJumlahAset']." ".$value['invSatuanBarang'],
               number_format($value['invNilaiPerolehanSatuan'],2,',','.')
            );
            foreach ($biayaLain as $BL) $rowResult[] = number_format($value['biaya_lain'][$BL['id']],2,',','.');
            array_push($rowResult,
               number_format($value['invNilaiPerolehanSatuan'],2,',','.'),
               number_format($value['invNilaiResidu'],2,',','.'),
               $value['invKepemilikan'],
               $value['invPenguasaanBarang'],
               $value['invKondisi'],
               $value['invKampus'],
               $value['lokasiAset'],
               $value['statusAsetNama'],
               $value['invKeteranganLain'],
               $value['unitkerjaNama']
            );
            
            foreach ($rowResult as $value) $content['table_body'] .= "<td bgcolor=white>$value</td>";
            $content['table_body'] .= "</tr>\r\n";
         }
         
         $this->mrTemplate->AddVars('content', $content, '');
         $this->mrTemplate->parseTemplate('content', 'a');
      }
      unset($data[@$key]);
      // ---------
      
      // Other....
      // create header
      $header = array
      (
         "Nomor Barcode" => '',
         "Kode Aset" => '',
         "Nama Aset" => '',
         "Label Aset" => '',
         "Merek" => '',
         "Spesifikasi" => '',
         "Tahun Pembelian" => '',
         "Sumber Dana" => '',
         "Umur Ekonomis" => '',
         "Luas Tanah" => '',
         "Nilai Faktur" => '',
         "Biaya Lain" => $biaya,
         "Nilai Perolehan" => '',
         "Nilai Residu" => '',
         "NJOP" => '',
         "Lokasi" => '',
         "Kegunaan" => '',
         "Landasan Legal" => array('Sertifikat', 'Akta', 'BPKB', 'STNK'),
         "Bukti Kendaraan" => $bukti,
         "Pagar" => '',
         "Batas-batas" => $batas,
         "Kepemilikan" => '',
         "Penguasaan" => '',
         "Kondisi Aset" => '',
         "Lokasi Kampus" => '',
         "Lokasi Aset" => '',
         "Status Aset" => '',
         "Keterangan Lain" => '',
         "Unit Penanggung Jawab Aset" => ''
      );
      // ---------
      $content['table_header'] = $this->create_header($header);
      
      foreach (array_keys($data) as $key)
      {
         $content['jenis_kib'] = $key;
         
         $content['table_body'] = '';
         foreach ($data[$key] as $value)
         {
            $content['table_body'] .= "   <tr>";
            
            if (preg_match('/tidak/',@$value['invPagar']) > 0)
               $value['invPagar'] = "Tidak Ada";
            $rowResult = array
            (
               $value['invBarcode'],
               $value['invKodeAset'],
               $value['namaAset'],
               $value['invMstLabel'],
               $value['invMerek'],
               $value['invSpesifikasi'],
               $value['invTglPembelian'],
               $value['invSumberDana'],
               @$value['invUmurEkonomis'],
               $value['invLuasTanah'],
               number_format($value['invNilaiFaktur'],2,',','.')
            );
            foreach ($biayaLain as $BL) @$rowResult[] = number_format($value['biaya_lain'][@$BL['id']],2,',','.');
            array_push($rowResult,
               number_format($value['invNilaiPerolehanSatuan'],2,',','.'),
               @number_format($value['invNilaiResidu'],2,',','.'),
               @$value['invNJOP'],
               @$value['invLokasi'],
               @$value['invKegunaan'],
               @$value['invSertifikat'],
               @$value['invAkta'],
               @$value['invBPKB'],
               @$value['invSTNK'],
               @$value['invNoMesin'],
               @$value['invNoRangka'],
               @$value['invNoPolisi'],
               @$value['invNoLain'],
               @$value['invWarna'],
               @$value['invPagar'],
               @$value['invBatasUtara'],
               @$value['invBatasSelatan'],
               @$value['invBatasTimur'],
               @$value['invBatasBarat'],
               $value['invKepemilikan'],
               $value['invPenguasaanBarang'],
               $value['invKondisi'],
               $value['invKampus'],
               $value['lokasiAset'],
               $value['statusAsetNama'],
               @$value['invKeteranganLain'],
               $value['unitkerjaNama']
            );
            
            foreach ($rowResult as $value) $content['table_body'] .= "<td bgcolor=white>$value</td>";
            $content['table_body'] .= "</tr>\r\n";
         }
         
         $this->mrTemplate->AddVars('content', $content, '');
         $this->mrTemplate->parseTemplate('content', 'a');
      }
      // ----------
   }
   
   function create_header($header)
   {
      $row1 = '';
      $row2 = '';
      
      foreach ($header as $key=>$value)
      {
         if (is_array($value))
         {
            $row1 .= "<th bgcolor='#CCCCCC' colspan=".count($value).">$key</th>";
            foreach ($value as $key=>$val) $row2 .= "<th bgcolor='#CCCCCC'>$val</th>";
         }
         else
         {
            $row1 .= "<th bgcolor='#CCCCCC' rowspan=2>$key</th>";
         }
      }
      
      $return = '';
      $return .= "   <tr>$row1</tr>\r\n";
      $return .= "   <tr>$row2</tr>\r\n";
      return $return;
   }
   
}
?>