<?php
class PengadaanBarang extends Database
{

	protected $mSqlFile= 'module/pb_pengadaan_barang/business/pengadaan_barang.sql.php';
   public $mFilePath = 'file/';

	function __construct($connectionNumber=0)
   {
		parent::__construct($connectionNumber);
		$this->uId = Security::Instance()->mAuthentication->getcurrentuser()->GetUserId();
	}

   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 GetComboJenisBarang()
   {
      return $this->Open($this->mSqlQueries['combo_jenis_barang'], array());
	}
	
	function GetComboJlrPengadaan ()
   {
      return $this->Open($this->mSqlQueries['combo_jalur_pengadaan'], array());
   }
	
	function GetComboSumberDana ()
   {
      return $this->Open($this->mSqlQueries['combo_sumber_dana'], array());
	}
	
	function GetComboMitraKerja ()
   {
      return $this->Open($this->mSqlQueries['combo_mitra_kerja'], array());
   }
	
	function GetDataPengadaanBarangById ($id)
   {
		$result = $this->Open($this->mSqlQueries['get_pengadaan_barang_by_id'], array($id));
      return $result;
   }
	
	function GetDataPengadaanBarangCount ($data)
   {
      extract($data);
		$arr = array(
			$pengadaanSumberdanaId,
			$pengadaanSumberdanaId,
			'%'.$nomorPengadaan.'%'
		);
		$result = $this->Open($this->mSqlQueries['get_pengadaan_barang_count'], $arr);
      return $result[0]['total'];
   }
	
	function GetDataPengadaanBarang ($data, $start, $limit)
   {
		extract($data);
		$arr = array(
			$pengadaanSumberdanaId,
			$pengadaanSumberdanaId,
			'%'.$nomorPengadaan.'%',
			$start, 
			$limit
		);
      return $this->Open($this->mSqlQueries['get_pengadaan_barang'], $arr);
   }
	
	//-- do here
	function AddPengadaanBarang($data)
   {
      extract($data);
      $this->StartTrans();
		
		$tanggal = $pb_tanggal.'-'.$pb_bulan.'-'.$pb_tahun;
      $tanggalPengadaan = date('Y-m-d H:i:s', strtotime("$tanggal"));

      $arg = array
      (
         $nomorPengadaan,
         $tanggalPengadaan,
         $jenis_barang,
         $pengadaanMAKId,
         $pengadaanMakNama,
         $pengadaanJlrPengBrgId,
         $pengadaanSumberdanaId,
         $pengadaanMitraId,
         $pic,
			$this->uId
			
      );

      if (!$this->Execute($this->mSqlQueries['add_pengadaan_barang'], $arg))
      {
         $this->EndTrans(false);
         return 1;
      }
		
		if (!empty($barangUsulList))
      {
         $arg = array();
			$mstId = $this->LastInsertId();
			foreach ($barangUsulList as $value){
				$arg = array($mstId, $value['brgId'], $value['brgNilaiSat'], $value['brgNominalSat'], $value['brgNominalVat'], $value['brgProsenVat']);
				if (!$this->Execute($this->mSqlQueries['add_pengadaan_barang_detil'], $arg)){
					$this->EndTrans(false);
					return 2;
				}
			}
      }
		
      $this->EndTrans(true);
      return 0;
   }
	
	function UpdatePengadaanBarang($data, $id)
   {
      extract($data);
      $this->StartTrans();
		
		$tanggal = $pb_tanggal.'-'.$pb_bulan.'-'.$pb_tahun;
      $tanggalPengadaan = date('Y-m-d H:i:s', strtotime("$tanggal"));

      $arg = array
      (
         $nomorPengadaan,
         $tanggalPengadaan,
         $jenis_barang,
         $pengadaanMAKId,
         $pengadaanMakNama,
         $pengadaanJlrPengBrgId,
         $pengadaanSumberdanaId,
         $pengadaanMitraId,
         $pic,
			$this->uId,
			$id	
      );

      if (!$this->Execute($this->mSqlQueries['update_pengadaan_barang'], $arg))
      {
         $this->EndTrans(false);
         return 1;
      }
		
      if (!$this->Execute($this->mSqlQueries['delete_pengadaan_barang_detil'], array($id)))
      {
         $this->EndTrans(false);
         return 2;
      }
		
		if (!empty($barangUsulList)){
         $arg = array();
			foreach ($barangUsulList as $value){				
				$arg = array($id, $value['brgId'], $value['brgNilaiSat'], $value['brgNominalSat'], $value['brgNominalVat'], $value['brgProsenVat']);
				if (!$this->Execute($this->mSqlQueries['add_pengadaan_barang_detil'], $arg)){
					$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_barang']);
      if ($this->Execute($sql, $id)) return 0;

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