<?php

class LelangBarang extends Database {

	protected $mSqlFile= 'module/pelelangan_barang/business/lelangbarang.sql.php';
	
	function __construct($connectionNumber=0) {
		parent::__construct($connectionNumber);
		
	}
	
	function GetDataLelangBarang ($offset, $limit, $nomorBA, $idMitra, $namaMitra) {
	if ($idMitra == 0)
		$sql = str_replace("%OPERATOR%", "OR", $this->mSqlQueries['get_data_lelang_barang']);
   else 
		$sql = str_replace("%OPERATOR%", "AND", $this->mSqlQueries['get_data_lelang_barang']);
		$result = $this->Open($sql, array('%'.$nomorBA.'%' ,$idMitra, '%'.$namaMitra.'%',$offset, $limit));		
			
		return $result;
	}
	
	function GetCount(){
		$result = $this->Open($this->mSqlQueries['get_count'],array());
		return $result[0]['total'];
	}
	
	function GetCountDataLelangBarang($nomorBA, $idMitra, $namaMitra) {
	if ($idMitra == 0)
		$sql = str_replace("%OPERATOR%", "OR", $this->mSqlQueries['get_count_data_lelang_barang']);
   else 
		$sql = str_replace("%OPERATOR%", "AND", $this->mSqlQueries['get_count_data_lelang_barang']);

		$result = $this->Open($sql, array('%'.$nomorBA.'%' , $idMitra, '%'.$namaMitra.'%'));
		
		if (!$result) {
			return 0;
		} else {
			return $result[0]['total'];
		}
	}
	
	function GetDataLelangBarangById ($idLelang) {
		if(is_array($idLelang)){
			$idLelang = implode("', '", $idLelang);
		}
		$result = $this->Open($this->mSqlQueries['get_data_lelang_barang_by_id'], array($idLelang));
		return $result;
	}
	
	//---DO--
	function DoAddLelangBarang($nomorBa, $mitra, $pebaikanDate, $totalHarga, $catatan, $idUser)
	{
		$result = $this->Execute($this->mSqlQueries['do_add_lelang_barang'], array($nomorBa, $mitra, $pebaikanDate, $totalHarga, $catatan, $idUser));
		return $result;
	}
	
	function DoAddLelangBarangDet($barangId, $pengBrgDetId,$biaya, $idUser) {
		$result = $this->Execute($this->mSqlQueries['do_add_lelang_barang_det'], array($barangId, $pengBrgDetId,$biaya, $idUser));
		if($result){
			$result = $this->DoUpdateInvDet('2', $idUser, $barangId);
		}
		return $result;
	}
	
	function DoUpdateLelangBarang($nomorBa, $mitra, $pebaikanDate, $totalHarga, $catatan, $idUser, $idLelang)
	{
		$result = $this->Execute($this->mSqlQueries['do_update_lelang_barang'], array($nomorBa, $mitra, $pebaikanDate, $totalHarga, $catatan, $idUser, $idLelang));
		return $result;
	}
	
	function DoUpdateInvDet($idStatus, $userId, $idBarang)
	{
		$result = $this->Execute($this->mSqlQueries['do_update_investaris_detil'], array($idStatus, $userId, $idBarang));
		return $result;
	}
	
	function DoUpdateLelangBarangDet($idLelang, $barangId, $pengBrgDetId,$biaya, $idUser) {
		$result = $this->Execute($this->mSqlQueries['do_update_lelang_barang_det'], array($idLelang, $barangId, $pengBrgDetId,$biaya, $idUser));
		if($result){
			$result = $this->DoUpdateInvDet('2', $idUser, $barangId);
		}
		return $result;
	}
	
	function DoDeleteLelangBarangDetil($idLelang){
		$result = $this->Execute($this->mSqlQueries['do_delete_lelang_barang_det'], array($idLelang));
		return $result;
	}
	
	function DoDeleteLelangBarangByArrayId($arrLelangId) {
		$lelangId = implode("', '", $arrLelangId);
		$result=$this->Execute($this->mSqlQueries['do_delete_lelang_barang_by_array_id'], array($lelangId));
		return $result;
	}
	
	
	//untuk popup mitra kerja
	 function GetMitraView ($mkNama = '', $mkAlamat = '',  $mkBidUsaha = 'all', $start = 0, $limit = 1000)
   {
      if ($mkBidUsaha == 'all')
		$sql = str_replace("%OPERATOR%", "OR", $this->mSqlQueries['get_mitra_view']);
      else $sql = str_replace("%OPERATOR%", "AND", $this->mSqlQueries['get_mitra_view']);
      
	  $result = $this->Open($sql, array($mkBidUsaha, "%$mkNama%", "%$mkAlamat%", $start, $limit));
      return $result;
   }
   
   function GetCountMitraView ($mkNama = '', $mkAlamat = '', $mkBidUsaha = 'all')
   {
      if ($mkBidUsaha == 'all')
		$sql = str_replace("%OPERATOR%", "OR", $this->mSqlQueries['get_count_mitra_view']);
      else $sql = str_replace("%OPERATOR%", "AND", $this->mSqlQueries['get_count_mitra_view']);
      $result = $this->Open($sql , array($mkBidUsaha, "%$mkNama%", "%$mkAlamat%"));
      return count($result);
   }
   
   function GetBidangUsahaList ()
   {
      $result = $this->Open($this->mSqlQueries['get_bidang_usaha_list'], array());
      return $result;
   }
	
}
?>