<?php
class Pengadaan extends Database
{

	protected $mSqlFile= 'module/pengadaan/business/Pengadaan.sql.php';
   public $mFilePath = 'file/';

	function __construct($connectionNumber=0)
   {
		parent::__construct($connectionNumber);
#      $this->SetDebugOn();
	}

   function GetDataUserById ($userId)
   {
      $result = $this->Open($this->mSqlQueries['get_data_user_by_id'], array($userId));
      return $result[0];
   }

   function GetDataUserByUsername ($name)
   {
      $result = $this->Open($this->mSqlQueries['get_data_user_by_username'], array($name));
      return $result[0];
   }

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

   function GetComboTahun ($awal, $akhir, $isAsc = false)
   {
      $return = array();
      for ($i = $akhir; $i >= $awal; $i--)
         $return[$i]['name'] = $return[$i]['id'] = $i;
      if ($isAsc) ksort($return);
      return array_values($return);
   }

	function GetComboSumberDana ()
   {
      return $this->Open($this->mSqlQueries['combo_sumber_dana'], array());
	}

   function GetComboJnsPengadaan ()
   {
      return $this->Open($this->mSqlQueries['combo_jenis_pengadaan'], array());
   }

   function GetComboJlrPengadaan ()
   {
      return $this->Open($this->mSqlQueries['combo_jalur_pengadaan'], array());
   }

   function GetComboMitraKerja ()
   {
      return $this->Open($this->mSqlQueries['combo_mitra_kerja'], array());
   }

   function GetComboJabatan ()
   {
      return $this->Open($this->mSqlQueries['combo_jabatan_ref'], array());
   }

   function GetComboUnit ()
   {
      $tmp = $this->GetDataUserByUsername($_SESSION['username']);
      return $this->Open($this->mSqlQueries['get_combo_unit'], array($tmp['unit_kerja_id'], $tmp['role_id']));
   }

   function GetListPengadaanCount ($filter)
   {
      extract($filter);
      $filter = array
      (
         $pengadaanTahun, ("$pengadaanTahun" == 'all') ? 1 : 0,
         "%$pengadaanMAK%",
         $pengadaanSumberDanaId, ("$pengadaanSumberDanaId" == 'all') ? 1 : 0,
         "%$pengadaanDetLabel%",
         "%$pengadaanNoSPK%"
      );

		$result = $this->Open($this->mSqlQueries['get_list_pengadaan_count'], $filter);

      return $result[0]['total'];
   }

   function GetListPengadaan ($filter, $start = 0, $limit = 1048576)
   {
      extract($filter);
      $filter = array
      (
         $pengadaanTahun, ("$pengadaanTahun" == 'all') ? 1 : 0,
         "%$pengadaanMAK%",
         $pengadaanSumberDanaId, ("$pengadaanSumberDanaId" == 'all') ? 1 : 0,
         "%$pengadaanDetLabel%",
         "%$pengadaanNoSPK%",
         $start, $limit
      );
		$result = $this->Open($this->mSqlQueries['get_list_pengadaan'], $filter);
      return $result;
   }

   function GetPengadaanMstById ($id)
   {
      $result = $this->Open($this->mSqlQueries['get_pengadaan_mst_by_id'], array($id));

      if (empty($result))
         return false;
      else $return = $result[0];
      $return['oldFileSPK'] = $return['pengadaanFileSPK'];

      // data panitia pengadaan
      $result = $this->Open($this->mSqlQueries['get_panitia_pengadaan_by_pengadaan_id'], array($id));
      if (!empty($result)) $return['panitia'] = $result;

      // data rekap
      $result = $this->Open($this->mSqlQueries['get_rekap_pengadaan_by_pengadaan_id'], array($id));
      if (!empty($result)) $return['rekapUsulan'] = $result;

      return $return;
   }

   // DO QUERY ---------
   function AddPengadaan ($data)
   {
      extract($data);
      $this->StartTrans();

      if ($_FILES['pengadaanFileSPK']['name'] !== '')
      {
         $tmp = dechex(mt_rand(65536, 1048576));
         $pengadaanFileSPK = "SPK-".$_FILES['pengadaanFileSPK']['name']."-$tmp.".$_FILES['pengadaanFileSPK']['ext'];
      }

      $pengadaanTglSPK = date('Y-m-d H:i:s', strtotime("$spkTanggal-$spkBulan-$spkTahun"));

      $arg = array
      (
         $pengadaanJnsPengBrgId,
         $pengadaanTahun,
         $pengadaanMAK,
         $pengadaanNominalPengadaan,
         $pengadaanNoSPK,
         $pengadaanNama,
         $pengadaanTglSPK,
         $pengadaanJlrPengBrgId,
         $pengadaanSumberdanaId,
         $pengadaanMitraId,
         $pengadaanUnitkerjaId,
         $pengadaanNominal,
         $pengadaanFileSPK,
         $_SESSION['username']
      );

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

      // panitia pengadaan
      if (!empty($panitia))
      {
         $sql = str_replace('%VALUE%', implode(',', array_fill(0, count($panitia), "((SELECT MAX(pengadaanId) FROM pengadaan_mst),%s,%s,%s)")), $this->mSqlQueries['add_pengadaan_panitia']);
         $arg = array();
         foreach ($panitia as $value) array_push($arg, $value['NIP'], $value['nama'], $value['status']);
         if (!$this->Execute($sql, $arg))
         {
            $this->EndTrans(false);
            return 2;
         }
      }

      // Rekap Usulan
      if (!empty($rekapUsulan))
      {
         $sql1 = str_replace('%VALUE%', implode(',', array_fill(0, count($rekapUsulan), "((SELECT MAX(pengadaanId) FROM pengadaan_mst),%s)")), $this->mSqlQueries['add_pengadaan_rekap']);
         $sql2 = str_replace('%s', "(SELECT MAX(pengadaanId) FROM  pengadaan_mst)", $this->mSqlQueries['add_pengadaan_detail_from_rekap']);
         $arg = array();
         foreach ($rekapUsulan as $value) $arg[] = $value['rekapId'];
         if (!$this->Execute($sql1, $arg) OR !$this->Execute($sql2, array()))
         {
            $this->EndTrans(false);
            return 4;
         }
      }

      // upload file SPK
      if ($_FILES['pengadaanFileSPK']['name'] != '' AND !move_uploaded_file($_FILES['pengadaanFileSPK']['tmp_name'], GTFW_APP_DIR.'/'.$this->mFilePath.'/'.$pengadaanFileSPK))
      {
         $this->EndTrans(false);
         return 3;
      }

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

   function UpdatePengadaan ($data)
   {
      extract($data);
      $id = $_GET['id']->Integer()->Raw();
      $this->StartTrans();

      // pengadaan_mst
      if ($_FILES['pengadaanFileSPK']['name'] !== '')
      {
         $tmp = dechex(mt_rand(65536, 1048576));
         $pengadaanFileSPK = "SPK-".$_FILES['pengadaanFileSPK']['name']."-$tmp.".$_FILES['pengadaanFileSPK']['ext'];
      }
      else $pengadaanFileSPK = $oldFileSPK;

      $pengadaanTglSPK = "$spkTahun-$spkBulan-$spkTanggal 09:18:00";

      $arg = array
      (
         $pengadaanJnsPengBrgId,
         $pengadaanTahun,
         $pengadaanMAK,
         $pengadaanNominalPengadaan,
         $pengadaanNoSPK,
         $pengadaanNama,
         $pengadaanTglSPK,
         $pengadaanJlrPengBrgId,
         $pengadaanSumberdanaId,
         $pengadaanMitraId,
         $pengadaanUnitkerjaId,
         $pengadaanNominal,
         $pengadaanFileSPK,
         $_SESSION['username'],
         $id
      );
      if (!$this->Execute($this->mSqlQueries['update_pengadaan_mst'], $arg))
      {
         $this->EndTrans(false);
         return 1;
      }
      // ---------

      // pengadaan_panitia
      $sqlDelete = $this->mSqlQueries['delete_pengadaan_panitia'];
      if (!$this->Execute($sqlDelete, array($id)))
      {
         $this->EndTrans(false);
         return 2;
      }

      if (count($panitia) > 0)
      {
         $arg = array();
         $sqlInsert = str_replace('%VALUE%', implode(',', array_fill(0, count($panitia), "(%s,%s,%s,%s)")), $this->mSqlQueries['add_pengadaan_panitia']);
         foreach ($panitia as $value) array_push($arg, $id, $value['NIP'], $value['nama'], $value['status']);

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

      // rekap
      $result = $this->Open($this->mSqlQueries['get_rekap_pengadaan_by_pengadaan_id'], array($id));
      $old = array();
      foreach ($result as $value) $old[] = $value['rekapId'];
      $new = array();
      if (!empty($rekapUsulan)) foreach ($rekapUsulan as $value) $new[] = $value['rekapId'];
      $add = array();
      foreach ($new as $value) if (!in_array($value, $old)) $add[] = $value;
      $delete = array();
      foreach ($old as $value) if (!in_array($value, $new)) $delete[] = $value;

      if (!empty($add))
      {
         $sql1 = str_replace('%VALUE%', implode(',', array_fill(0, count($add), "(%s,%s)")), $this->mSqlQueries['add_pengadaan_rekap']);
         $sql2 = $this->mSqlQueries['add_pengadaan_detail_from_rekap'];
         $arg = array();
         foreach ($add as $value) array_push($arg, $id, $value);
         if (!$this->Execute($sql1, $arg) OR !$this->Execute($sql2, array($id)))
         {
            $this->EndTrans(false);
            return 4;
         }
      }

      if (!empty($delete))
      {
         $sql1 = str_replace('%s', implode(',', array_fill(0, count($delete), "%s")), $this->mSqlQueries['delete_pengadaan_detail_by_rekap']);
         $sql2 = str_replace('%s', implode(',', array_fill(0, count($delete), "%s")), $this->mSqlQueries['delete_pengadaan_rekap']);
         $arg = array();
         foreach ($delete as $value) $arg[] = $value;
         if (!$this->Execute($sql1, $arg) OR !$this->Execute($sql2, $arg))
         {
            $this->EndTrans(false);
            return 5;
         }
      }
      // ---------

      // upload file SPK
      if ($_FILES['pengadaanFileSPK']['name'] != '' AND !move_uploaded_file($_FILES['pengadaanFileSPK']['tmp_name'], GTFW_APP_DIR.'/'.$this->mFilePath.'/'.$pengadaanFileSPK))
      {
         $this->EndTrans(false);
         return 3;
      }

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

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

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