<?php
class Komponen extends Database
{

	protected $mSqlFile= 'module/pengadaan/business/Komponen.sql.php';
   public $mPID;
   public $mImagePath; // Note: variabel ini dipakai oleh verifikasi_pengadaan/business/Verifikasi.class.php :: mImagePengadaanPath

	function __construct($connectionNumber=0)
   {
      $this->mPID = (int) $_SESSION['mod=pengadaan']['id'];
      $this->mImagePath = 'images/pengadaan/';
		parent::__construct($connectionNumber);
	}

   function ConvertCombo($combo)
   {
      foreach ($combo as $value) $return[$value['id']] = $value['name'];
      return $return;
   }

   // combo box
   function GetCbxJenisKib() {
      $result = $this->Open($this->mSqlQueries['get_combo_jenis_kib'], array());
      foreach (array_keys($result) as $key)
      {
         if (strpos($result[$key]['name'], 'KIB D:') !== 0)
            $return[] = $result[$key];
         if (strpos($result[$key]['name'], 'KIB E:') !== false)
            $return[] = array('id'=>$result[$key]['id'], 'name'=>$result[$key]['name'].' (BHP)');
      }

      return $return;
   }

   function GetCbxUnitPj() {
      $result = $this->Open($this->mSqlQueries['get_unit_pj'], array());
      return $result;
   }

   function GetCbxKepemilikan() {
      $result = $this->Open($this->mSqlQueries['get_kepemilikan'], array());
      return $result;
   }

   function GetCbxKondisi() {
      $result = $this->Open($this->mSqlQueries['get_kondisi'], array());
      return $result;
   }

   function GetCbxSatuan() {
      $result = $this->Open($this->mSqlQueries['get_cbx_satuan'], array());
      return $result;
   }

   function GetCbxPagar() {
      $result = $this->Open($this->mSqlQueries['get_cbx_pagar'], array());
      $array = eval('return '.str_replace('enum', 'array', $result[0]['Type']).';');
      foreach ($array as $key=>$val) $pagar[$key]['id'] = $pagar[$key]['name'] = $val;
      return $pagar;
   }

   function GetCbxSifatBangunan() {
      $result = $this->Open($this->mSqlQueries['get_cbx_sifat_bangunan'], array());
      $array = eval('return '.str_replace('enum', 'array', $result[0]['Type']).';');
      foreach ($array as $key=>$val) $pagar[$key]['id'] = $pagar[$key]['name'] = $val;
      return $pagar;
   }

   function GetCbxIMB() {
      $result = $this->Open($this->mSqlQueries['get_cbx_imb'], array());
      $array = eval('return '.str_replace('enum', 'array', $result[0]['Type']).';');
      foreach ($array as $key=>$val) $pagar[$key]['id'] = $pagar[$key]['name'] = $val;
      return $pagar;
   }

   function GetCbxPenguasaanBarang() {
      $result = $this->Open($this->mSqlQueries['get_cbx_penguasaan_barang'], array());
      return $result;
   }

   function GetListBiayaLain()
   {
      return $this->Open($this->mSqlQueries['get_list_biaya_lain'], array());
   }
   // ---------

   function GetPengadaanDetail()
   {
      $result = $this->Open($this->mSqlQueries['get_pengadaan_detail'], array($this->mPID));
      return $result[0];
   }

   function GetKomponenListCount ($filter)
   {
      extract($filter);

      $result = $this->Open($this->mSqlQueries['get_komponen_list_count'], array($this->mPID, "%$nama%", "%$nama%"));
      return $result[0]['total'];
   }

   function GetKomponenList ($filter, $start, $limit)
   {
      extract($filter);
      $result = $this->Open($this->mSqlQueries['get_komponen_list'], array($this->mPID, "%$nama%", "%$nama%", $start, $limit));
      return $result;
   }

   function GetPengadaanDetById($id)
   {
      $result = $this->Open($this->mSqlQueries['get_pengadaan_det_by_id'], array($id));
      if (empty($result)) return false;
      else $return = $result[0];

      // modifikasi data biar cocok dengan form
		$return['isSertifikat'] = ($return['pengadaanDetSertifikat']) ? 'checked' : '';
		$return['isAkta'] = ($return['pengadaanDetAkta']) ? 'checked' : '';
		$return['isBPKB'] = ($return['pengadaanDetBPKB']) ? 'checked' : '';
		$return['isSTNK'] = ($return['pengadaanDetSTNK']) ? 'checked' : '';
      // ---------

      // nambah data biaya lain
      $return['biaya_lain'] = array();
      $result = $this->Open($this->mSqlQueries['get_biaya_lain_by_id'], array($id));
      foreach ($result as $val)
         $return['biaya_lain'][$val['biayaId']] = $val['biayaNilai'];
      // ---------

      return $return;
   }

   function GetLastKomponenId()
   {
      $result = $this->Open($this->mSqlQueries['get_last_komponen_id'], array());
      return $result[0]['id'];
   }

   function GetDataByBarcode ($barcode)
   {
      $tmp = $this->GetPengadaanDetail();

      if ($tmp['jenis_pengadaan'] == 'BHP')
         $sql = $this->mSqlQueries['get_data_by_barcode_for_atk'];
      else $sql = $this->mSqlQueries['get_data_by_barcode'];

      $result = $this->Open($sql, array($barcode));
      if (empty($result)) return false;
      else $return = $result[0];

      // modifikasi data biar cocok dengan form
		$return['isBPKB'] = ($return['pengadaanDetBPKB']) ? 'checked' : '';
		$return['isSTNK'] = ($return['pengadaanDetSTNK']) ? 'checked' : '';
      // ---------

      // nambah data biaya lain
      $return['biaya_lain'] = array();

      if ($tmp['jenis_pengadaan'] == 'BHP')
         $sql = $this->mSqlQueries['get_biaya_lain_atk_by_id'];
      else $sql = $this->mSqlQueries['get_biaya_lain_inventarisasi_by_id'];

      $result = $this->Open($sql, array($return['id']));
      foreach ($result as $val)
         $return['biaya_lain'][$val['biayaId']] = $val['biayaNilai'];
      // ---------

      return $return;
   }

   function GetSisaDanaPengadaan ()
   {
      $result = $this->Open($this->mSqlQueries['get_sisa_dana_pengadaan'], array($this->mPID));
      return $result[0]['sisa'];
   }

   /////////
   // Do Function
   /////////

   function Add($data)
   {
      extract($data);
      if ($_FILES['denah']['name'] !== '')
      {
         $tmp = dechex(mt_rand(65536, 1048576));
         $pengadaanDetDenah = "denah-".$_FILES['denah']['name']."-$tmp.".$_FILES['denah']['ext'];
      }
      if ($_FILES['foto']['name'] !== '')
      {
         $tmp = dechex(mt_rand(65536, 1048576));
         $pengadaanDetFoto = "foto-".$_FILES['foto']['name']."-$tmp.".$_FILES['foto']['ext'];
      }
      $this->StartTrans();

      // Insert tabel pengadaan_detail
      if ($pengadaanDetJmlAset < 1) $pengadaanDetJmlAset = 1;
      $arg = array(
         $this->mPID,
         $pengadaanDetRuangId,
         $pengadaanDetBarcode,
         $pengadaanDetBrg,
         $pengadaanDetLabel,
         $pengadaanDetMerk,
         $pengadaanDetSpesifikasi,
         $pengadaanDetJmlAset,
         $pengadaanDetSatuan,
         $pengadaanDetKdGedung,
         $pengadaanDetNmGedung,
         $pengadaanDetLuasTanah,
         $pengadaanDetLuasGedung,
         $pengadaanDetUmrTeknis,
         $pengadaanDetNilaiPerolehan,
         $pengadaanDetHargaFaktur,
         $pengadaanDetNilaiResidu,
         $pengadaanDetTotalPerolehan,
         $pengadaanDetNJOP,
         $pengadaanDetSifatBangunan,
         $pengadaanDetIMB,
         $pengadaanDetPeruntukan,
         $pengadaanDetUmrEko,
         $pengadaanDetLokasi,
         $pengadaanDetKegunaan,
         $pengadaanDetSertifikat,
         $pengadaanDetBPKB,
         $pengadaanDetSTNK,
         $pengadaanDetAkta,
         $pengadaanDetNoMesin,
         $pengadaanDetNoRangka,
         $pengadaanDetNoPolisi,
         $pengadaanDetNoLain,
         $pengadaanDetWarna,
         $pengadaanDetPagar,
         $pengadaanDetBtsUtara,
         $pengadaanDetBtsSelatan,
         $pengadaanDetBtsTimur,
         $pengadaanDetBtsBarat,
         $pengadaanDetPmlkBrg,
         $pengadaanDetPenguasaan,
         $pengadaanDetKondisi,
         $pengadaanDetKetLain,
         $pengadaanDetDenah,
         $pengadaanDetFoto,
         $_SESSION['username']
      );

      if (!$this->Execute($this->mSqlQueries['add'], $arg))
      {
         $this->EndTrans(false);
         return 1;
      }

      $pengadaanDetId = $this->GetLastKomponenId();
      // ---------

      // Insert biaya lain
      $biaya = array();
      $arg = array();
      foreach ($biaya_lain as $key=>$value)
      {
         if (!is_numeric($value)) continue;
         $biaya[] = "(NULL, %s, %s, %s)";
         array_push($arg, $pengadaanDetId, $key, $value);
      }
      $sql = str_replace('%VALUES%', implode(',', $biaya), $this->mSqlQueries['add_biaya_lain']);

      if (!empty($biaya) && !$this->Execute($sql, $arg))
      {
         $this->EndTrans(false);
         return 2;
      }
      // ---------

      // upload file
      if
      (
         $_FILES['denah']['name'] !== '' AND
         !move_uploaded_file($_FILES['denah']['tmp_name'], GTFW_APP_DIR.'/'.$this->mImagePath.$pengadaanDetDenah)
      )
      {
         $this->EndTrans(false);
         return 3;
      }
      if
      (
         $_FILES['foto']['name'] !== '' AND
         !move_uploaded_file($_FILES['foto']['tmp_name'], $this->mImagePath.$pengadaanDetFoto)
      )
      {
         $this->EndTrans(false);
         return 4;
      }
      // ---------

      $this->EndTrans(true);
      return 0;
   }

   function Update($data, $id)
   {
      extract($data);
      if ($_FILES['denah']['name'] !== '')
      {
         $tmp = dechex(mt_rand(65536, 1048576));
         $pengadaanDetDenah = 'denah-'.$_FILES['denah']['name']."$tmp.".$_FILES['denah']['ext'];
      }
      else $pengadaanDetDenah = null;
      if ($_FILES['foto']['name'] !== '')
      {

         $tmp = dechex(mt_rand(65536, 1048576));
         $pengadaanDetFoto = 'foto-'.$_FILES['foto']['name']."-$tmp.".$_FILES['foto']['ext'];
      }
      else $pengadaanDetFoto = null;
      $this->StartTrans();

      // Proteksi data dari rekap
      $tmp = $this->GetPengadaanDetById($id);
      if ($tmp['pengadaanRekapId'] > 0)
      {
         $pengadaanDetBrg = $tmp['pengadaanDetBrg'];
         $pengadaanDetJmlAset = $tmp['pengadaanDetJmlAset'];
         $pengadaanDetSatuan = (empty($tmp['pengadaanDetSatuan']))?$tmp['barangSatuanbrgId']:$tmp['pengadaanDetSatuan'];
			(empty($pengadaanDetSatuan))?$pengadaanDetSatuan=1:$pengadaanDetSatuan;
         $pengadaanDetNilaiPerolehan = $tmp['pengadaanDetNilaiPerolehan'];
         $pengadaanDetTotalPerolehan = $tmp['pengadaanDetTotalPerolehan'];
      }
      // ---------

      // Update tabel pengadaan_detail
      if ($pengadaanDetJmlAset < 1) $pengadaanDetJmlAset = 1;
      $arg = array(
         $this->mPID,
         $pengadaanDetRuangId,
         $pengadaanDetBarcode,
         $pengadaanDetBrg,
         $pengadaanDetLabel,
         $pengadaanDetMerk,
         $pengadaanDetSpesifikasi,
         $pengadaanDetJmlAset,
         $pengadaanDetSatuan,
         $pengadaanDetKdGedung,
         $pengadaanDetNmGedung,
         $pengadaanDetLuasTanah,
         $pengadaanDetLuasGedung,
         $pengadaanDetUmrTeknis,
         $pengadaanDetNilaiPerolehan,
         $pengadaanDetHargaFaktur,
         $pengadaanDetNilaiResidu,
         $pengadaanDetTotalPerolehan,
         $pengadaanDetNJOP,
         $pengadaanDetSifatBangunan,
         $pengadaanDetIMB,
         $pengadaanDetPeruntukan,
         $pengadaanDetUmrEko,
         $pengadaanDetLokasi,
         $pengadaanDetKegunaan,
         $pengadaanDetSertifikat,
         $pengadaanDetBPKB,
         $pengadaanDetSTNK,
         $pengadaanDetAkta,
         $pengadaanDetNoMesin,
         $pengadaanDetNoRangka,
         $pengadaanDetNoPolisi,
         $pengadaanDetNoLain,
         $pengadaanDetWarna,
         $pengadaanDetPagar,
         $pengadaanDetBtsUtara,
         $pengadaanDetBtsSelatan,
         $pengadaanDetBtsTimur,
         $pengadaanDetBtsBarat,
         $pengadaanDetPmlkBrg,
         $pengadaanDetPenguasaan,
         $pengadaanDetKondisi,
         $pengadaanDetKetLain,
         $pengadaanDetDenah,
         $pengadaanDetFoto,
         $_SESSION['username'],
         $id
      );

      if (!$this->Execute($this->mSqlQueries['update'], $arg))
      {
         $this->EndTrans(false);
         return 1;
      }
      // ---------

      // Insert biaya lain
      # tidak ada penambahan biaya diawal pengadaan (default)
//       $this->Execute($this->mSqlQueries['delete_biaya_lain'], array($id));
//       $biaya = array();
//       $arg = array();
//       foreach ($biaya_lain as $key=>$value)
//       {
//          $biaya[] = "(NULL, %s, %s, %s)";
//          array_push($arg, $id, $key, $value);
//       }
//       $sql = str_replace('%VALUES%', implode(',', $biaya), $this->mSqlQueries['add_biaya_lain']);

//       if (!$this->Execute($sql, $arg))
//       {
//          $this->EndTrans(false);
//          return 2;
//       }
      // ---------

      // upload file
      if
      (
         $_FILES['denah']['name'] !== '' AND
         !move_uploaded_file($_FILES['denah']['tmp_name'], $this->mImagePath.$pengadaanDetDenah)
      )
      {
         $this->EndTrans(false);
         return 3;
      }
      if
      (
         $_FILES['foto']['name'] !== '' AND
         !move_uploaded_file($_FILES['foto']['tmp_name'], $this->mImagePath.$pengadaanDetFoto)
      )
      {
         $this->EndTrans(false);
         return 4;
      }
      // ---------

      $this->EndTrans(true);
      return 0;
   }

   function Delete ($id)
   {
      $sql = str_replace('%VALUE%', implode(',', array_fill(0, count($id), "%s")), $this->mSqlQueries['delete_pengadaan_detail']);
      if ($this->Execute($sql, $id)) return 0;

      // delete one by one
      $return = 0;
      $sql = str_replace('%VALUE%', '%s', $this->mSqlQueries['delete_pengadaan_detail']);
      foreach ($id as $value) if (!$this->Execute($sql, array($value))) $return++;
      return $return;
   }
}
?>