<?php
class SewaAset extends Database
{
   protected $mSqlFile;
   function __construct($connectionNumber = 0)
   {
      $this->mSqlFile = 'module/sewa_aset/business/sewa_aset.sql.php';
      parent::__construct($connectionNumber);
      #$this->setDebugOn();
   }

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

	public function ValidationDate($idAset, $tglAwal, $tglAkhir, $id)
	{
		$data = array(
			$tglAwal,
			$tglAkhir,
			$tglAwal,
			$tglAkhir,
			$tglAwal,
			$tglAkhir,
			$tglAkhir,
			$tglAwal,
			$tglAkhir,
			$idAset,
			$id
		);
		$result = $this->Open($this->mSqlQueries['validation_date'], $data);
		return $result;
	}

	public function ValidationDateById($idAset, $tglAwal, $jamAwal, $tglAkhir, $jamAkhir)
	{
		$data = array(
			$tglAwal, $jamAwal, $tglAkhir, $jamAkhir, $idAset
		);
		$result = $this->Open($this->mSqlQueries['validation_date_by_id'], $data);
		return $result[0];
	}

   public function GetHour()
   {
      for ($i = 1;$i < 24;$i++)
      {
         $hour[$i - 1]['id'] = $i;
         $hour[$i - 1]['name'] = $i;
			if($hour[$i - 1]['id'] < 10){
				$hour[$i - 1]['id'] = '0'.$hour[$i - 1]['id'];
			}
      }
      return $hour;
   }

   public function GetMinute()
   {
      for ($i = 0;$i < 60;$i++)
      {
         $minute[$i]['id'] = $i;
         $minute[$i]['name'] = str_pad($i,2,'0',STR_PAD_LEFT);
      }
      return $minute;
   }

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

	public function GetCountDataUnit($nama='')
   {
		$tmp = $this->GetDataUserByUsername($_SESSION['username']);
		$result = $this->Open($this->mSqlQueries['get_count_data_unit'], array($tmp['unit_kerja_id'], $tmp['role_id'], '%'.$nama.'%'));
		return count($result);
	}

	public function GetDataUnit($nama='', $start, $limit)
   {
		//$tmp = $this->GetDataUserByUsername($_SESSION['username']);
		$result = $this->Open($this->mSqlQueries['get_data_unit'], array(/*$tmp['unit_kerja_id'], $tmp['role_id'], */ '%'.$nama.'%', $start, $limit));
		return $result;
	}

	public function GetDataMitraCount($nama='', $alamat='')
   {
      $result = $this->Open($this->mSqlQueries['get_count_mitra_view'], array('%'.$nama.'%', '%'.$alamat.'%'));
      return count($result);
   }

   # multiunit
	public function GetDataMitra($nama='', $alamat='', $start, $limit)
	{
		$user = $this->GetDataUserByUsername($_SESSION['username']);
		$result = $this->Open($this->mSqlQueries['get_mitra_view'], array('%'.$nama.'%', '%'.$alamat.'%',$user['unitkerjaKodeSistem'],$user['unitkerjaKodeSistem'].'.%', $start, $limit));
		return $result;
	}

	public function GetDataAsetSewaCount($kode='', $label='')
   {
      $result = $this->Open($this->mSqlQueries['get_data_aset_sewa_count'], array('%'.$kode.'%', '%'.$label.'%'));
      if (is_array($result))
         return $result[0]['total'];
      else return 0;
   }

   # multiunit
	public function GetDataAsetSewa($kode='', $label='', $start, $limit)
	{
		$user = $this->GetDataUserByUsername($_SESSION['username']);
		$result = $this->Open($this->mSqlQueries['get_data_aset_sewa'], array('%'.$kode.'%', '%'.$label.'%',$user['unitkerjaKodeSistem'],$user['unitkerjaKodeSistem'].".%", $start, $limit));
		return $result;
	}

	public function GetDataSewaAsetById($sewaId)
	{
		if(is_array($sewaId)){
			$sewaId = implode("', '", $sewaId);
		}
		$result = $this->Open($this->mSqlQueries['get_data_sewa_aset_by_id'], array($sewaId));
		return $result;
	}

	//-- do ada disini
	function DoUpdateInvDet($idStatus, $idBarang)
	{
		$userId = Security::Instance()->mAuthentication->getcurrentuser()->GetUserId();
		$result = $this->Execute($this->mSqlQueries['do_update_investaris_detil'], array($idStatus, $userId, $idBarang));
		return $result;
	}
	
	public function DoAddSewaAset($nomorSewa, $penyewa, $kontak, $alamat, $keterangan, $jmlHari, $tglAwal, $wktAwal, $tglAkhir, $wktAkhir, $unit='', $mitra='', $userId)
	{
		$result = $this->Execute($this->mSqlQueries['do_add_sewa_aset'], array($nomorSewa, $penyewa, $kontak, $alamat, $keterangan, $jmlHari, $tglAwal, $wktAwal, $tglAkhir, $wktAkhir, $unit, $mitra, $userId));
		return $result;
	}

	public function DoAddSewaAsetDet($asetId, $harga, $potongan, $statusId) {
		$result = $this->Execute($this->mSqlQueries['do_add_sewa_aset_det'], array($asetId, $harga, $potongan));
		if($result){
			$result = $this->DoUpdateInvDet('4', $statusId);
		}
		return $result;
	}

	public function DoUpdateSewaAset($penyewa, $kontak, $alamat, $keterangan, $jmlHari, $tglAwal, $wktAwal, $tglAkhir, $wktAkhir, $unit='', $mitra='', $userId, $sewaId)
	{
		$result = $this->Execute($this->mSqlQueries['do_update_sewa_aset'], array($penyewa, $kontak, $alamat, $keterangan, $jmlHari, $tglAwal, $wktAwal, $tglAkhir, $wktAkhir, $unit, $mitra, $userId, $sewaId));

		return $result;
	}

	public function DoUpdateSewaAsetDet($statusId, $sewaId, $asetId, $harga, $potongan) {
		$result = $this->Execute($this->mSqlQueries['do_update_sewa_aset_det'], array($sewaId, $asetId, $harga, $potongan));
		if($result){
			$result = $this->DoUpdateInvDet('4', $statusId);
		}
		return $result;
	}

	public function GetCountDataSewaAset($kodeSewa, $status_isi = 'all') {
		$result = $this->Open($this->mSqlQueries['get_count_data_sewa_aset'], array('%'.$kodeSewa.'%', $status_isi, $status_isi));
		if (!$result)
		{
			return 0;
		}
			else
		{
			return $result[0]['total'];
		}
	}

	# multiunit
	public function GetDataSewaAset($startRec, $itemViewed, $kodeSewa, $status_isi = 'all') {
		$user = $this->GetDataUserByUsername($_SESSION['username']);
		$result = $this->Open($this->mSqlQueries['get_data_sewa_aset'], array('%'.$kodeSewa.'%', $user['unitkerjaKodeSistem'],$user['unitkerjaKodeSistem'].".%",$status_isi, $status_isi, $startRec, $itemViewed));
		return $result;
	}

	public function DoDeleteSewaAsetDetil($idSewa){
		$result = $this->Execute($this->mSqlQueries['do_delete_sewa_aset_det'], array($idSewa));
		return $result;
	}

	public function DoDeleteSewaAsetByArrayId($arrSewaId) {
		$sewaId = implode("', '", $arrSewaId);
		$result=$this->Execute($this->mSqlQueries['do_delete_sewa_aset_by_array_id'], array($sewaId));
		return $result;
	}
}
?>