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


class ViewExcelInventarisasi extends XlsResponse
{
   var $mWorksheets = array('Data');
   
   function GetFileName() {
      return 'Daftar_Inventarisasi'.date('His').'.xls';
   }
   
   function ProcessRequest()
   {
      $Obj = new AppInventarisasi;

		//$filter = str_replace('|', ',', $_GET['id']->Raw());
      $filter = explode('|', $_GET['id']->Raw());
		$data = $Obj->GetInventarisasiForExcel($filter);
		if (empty($data))
      {
         @$this->mWorksheets['Data']->write(0, 0, 'Data kosong, tidak ada item yang di pilih');
         return;
      }
      // ---------
     
      // inisialisasi variabel
      $row = 0;
      $keys = implode('|', array_keys($data));
      $biayaLain = $Obj->GetListBiayaLain();
      foreach ($biayaLain as $key=>$value) $biaya[$key] = $biayaLain[$key]['name'];
      $batas = array('Utara', 'Selatan', 'Timur', 'Barat');
      $bukti = array('No Mesin', 'No Rangka', 'No Polisi', 'No Lain', 'Warna');
      unset($this->mrWorkbook->worksheets[0]);
      
      // cell format
      $fTitle = $this->mrWorkbook->add_format();
      $fTitle->set_size(12);
      $fTitle->set_bold();
      $fTitle->set_align('center');
      
      $fTHeader = $this->mrWorkbook->add_format();
      $fTHeader->set_size(10);
      $fTHeader->set_bold();
      $fTHeader->set_align('center');
      $fTHeader->set_align('vcenter');
      $fTHeader->set_border(1);
      
      $fTBody = $this->mrWorkbook->add_format();
      $fTBody->set_size(10);
      $fTBody->set_align('left');
      $fTBody->set_border(1);
      // ---------

      // KIB A: Tanah
      preg_match('@[^|]*KIB\sA[^|]*@', $keys, $match);
      if (!empty($match))
      {
         $key = $match[0];
         $this->mWorksheets[$key] = $this->mrWorkbook->add_worksheet('KIB A');
         
         // create header
         $row = 1;
         @$this->mWorksheets[$key]->write($row, 0, $key, $fTitle);
         $row += 2; $i = 0;
         $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" => ''
         );
         $this->create_header($key, $header, $fTHeader, $row, $i);
         $row += 2; $i = 0;
         
         foreach ($data[$key] as $value)
         {
            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'],
               $value['invNilaiPerolehanSatuan'],
               $value['invNilaiFaktur']
            );
            foreach ($biayaLain as $BL) $rowResult[] = $value['biaya_lain'][$BL['id']];
            array_push($rowResult,
               $value['invNilaiPerolehanSatuan'],
               $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) @$this->mWorksheets[$key]->write($row, $i++, $value, $fTBody);
            $row++; $i = 0;
         }
      }
      unset($data[$key]);
      // ---------

      // KIB B: Peralatan dan Mesin
      preg_match('@[^|]*KIB\sB[^|]*@', $keys, $match);
      if (!empty($match))
      {
         $key = $match[0];

         $this->mWorksheets[$key] = $this->mrWorkbook->add_worksheet('KIB B');
			
         // create header
         $row = 1;
         @$this->mWorksheets[$key]->write($row, 0, $key, $fTitle);
			
         $row += 2; $i = 0;

         $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" => ''
         );
         $this->create_header($key, $header, $fTHeader, $row, $i);
         $row += 2; $i = 0;
         
         foreach ($data[$key] as $value)
         {
            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['invNilaiFaktur']
            );
            foreach ($biayaLain as $BL) $rowResult[] = $value['biaya_lain'][$BL['id']];
            array_push($rowResult,
               $value['invNilaiPerolehanSatuan'],
               $value['invNilaiResidu'],
               $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) @$this->mWorksheets[$key]->write($row, $i++, $value, $fTBody);
            $row++; $i = 0;
         }
      }
      unset($data[$key]);
      // ---------
      
      // KIB E: Aset Lainnya
      preg_match('@[^|]*KIB\sE[^|]*@', $keys, $match);
      if (!empty($match))
      {
         $key = $match[0];
         $this->mWorksheets[$key] = $this->mrWorkbook->add_worksheet('KIB E');
         
         // create header
         $row = 1;
         @$this->mWorksheets[$key]->write($row, 0, $key, $fTitle);
         $row += 2; $i = 0;
         $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" => ''
         );
         $this->create_header($key, $header, $fTHeader, $row, $i);
         $row += 2; $i = 0;
         
         foreach ($data[$key] as $value)
         {
            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'],
               $value['invNilaiPerolehanSatuan']
            );
            foreach ($biayaLain as $BL) $rowResult[] = $value['biaya_lain'][$BL['id']];
            array_push($rowResult,
               $value['invNilaiPerolehanSatuan'],
               $value['invNilaiResidu'],
               $value['invKepemilikan'],
               $value['invPenguasaanBarang'],
               $value['invKondisi'],
               $value['invKampus'],
               html_entity_decode($value['lokasiAset']),
               $value['statusAsetNama'],
					$value['invKeteranganLain'],
               $value['unitkerjaNama']
            );
            
            foreach ($rowResult as $value) @$this->mWorksheets[$key]->write($row, $i++, $value, $fTBody);
            $row++; $i = 0;
         }
      }
      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" => ''
      );
      // ---------
      
      foreach (array_keys($data) as $key)
      {
         $row = 1;
         $this->mWorksheets[$key] = $this->mrWorkbook->add_worksheet($key);
         @$this->mWorksheets[$key]->write($row, 0, $key, $fTitle); $row += 2; $i = 0;
         $this->create_header($key, $header, $fTHeader, $row, $i); $row += 2; $i = 0;
         
         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'],
               @html_entity_decode($value['lokasiAset']),
               @$value['statusAsetNama'],
			   @$value['invKeteranganLain'],
               @$value['unitkerjaNama']
            );
            
            foreach ($rowResult as $value) @$this->mWorksheets[$key]->write($row, $i++, $value, $fTBody);
            $row++; $i = 0;
         }
      }
      // ----------
   }
   
   function create_header($sheet, $header, $format, $start_row, $start_col)
   {
      $row = $start_row;
      $col = $start_col;
      foreach ($header as $key=>$value)
      {
         @$this->mWorksheets[$sheet]->write($row, $col, $key, $format);
         if (is_array($value))
         {
            $this->mWorksheets[$sheet]->merge_cells($row, $col, $row, $col + count($value) - 1);
            foreach ($value as $key=>$val)
            {
               if ($key > 0) @$this->mWorksheets[$sheet]->write_blank($row, $col, $format);
               @$this->mWorksheets[$sheet]->write($row + 1, $col, $val, $format);
               $col++;
            }
         }
         else
         {
            @$this->mWorksheets[$sheet]->write_blank($row + 1, $col, $format);
            @$this->mWorksheets[$sheet]->merge_cells($row, $col, $row + 1, $col);
            $col++;
         }
      }
   }
}
?>
