<?php

/*
@ClassName : PenggunaanRuang
@Copyright : PT Gamatechno Indonesia
@Analyzed By : Nanang Ruswianto <nanang@gamatechno.com>
@Author By : Dyan Galih <galih@gamatechno.com>
@Version : 0.1
@StartDate : 2010-06-18
@LastUpdate : 2010-06-18
@Description :
*/

class PenggunaanRuang extends Database
{

   protected $mSqlFile;
   function __construct($connectionNumber = 0)
   {
      $this->mSqlFile = 'module/penggunaan_ruang/business/penggunaan_ruang.sql.php';
      parent::__construct($connectionNumber);
      #$this->setDebugOn();
	}

   public function GetHour()
   {

      for ($i = 1;$i < 24;$i++)
      {
         $hour[$i - 1]['id'] = $i;
         $hour[$i - 1]['name'] = $i;
      }

      return $hour;
   }

   public function GetMinute()
   {

      for ($i = 0;$i < 60;$i++)
      {

         # code...
         $minute[$i]['id'] = $i;
         $minute[$i]['name'] = $i;
      }

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

   # multiunit
   public function GetListRuang($offset, $limit, $pencarian)
   {
   	  $user = $this->GetDataUserByUsername($_SESSION['username']);
      $result = $this->Open($this->mSqlQueries['get_list_ruang'], array(
         '%' . $pencarian . '%',
         '%' . $pencarian . '%',
      	 $user['unitkerjaKodeSistem'],
      	 $user['unitkerjaKodeSistem'].'.%',
         $offset,
         $limit
      ));

      return $result;
   }

	public function ValidationDate($tglMulai, $tglSelesai, $ruangId, $id)
   {
      $result = $this->Open($this->mSqlQueries['validation_date'], array($tglMulai, $tglSelesai, $tglMulai, $tglSelesai, $tglMulai, $tglSelesai, $tglSelesai, $tglMulai, $tglSelesai, $ruangId, $id));
		return $result;
   }

	public function ValidationDateAtUpdateById($tglMulai, $tglSelesai, $jamMulai, $jamAkhir, $id)
   {
      $result = $this->Open($this->mSqlQueries['validation_date_at_update_by_id'], array($tglMulai, $tglSelesai, $jamMulai, $jamAkhir, $id));
		return $result[0];
   }

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

   public function GetCount()
   {
      $result = $this->Open($this->mSqlQueries['get_search_count'], array());

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

   public function GetUnitList($offset, $limit, $pencarian)
   {
      $result = $this->Open($this->mSqlQueries['get_list_unit'], array(
         '%' . $pencarian . '%',
         '%' . $pencarian . '%',
         $offset,
         $limit
      ));

      return $result;
   }

   public function GetPenggunaanRuangUnit($id)
   {
      $result = $this->Open($this->mSqlQueries['get_penggunaan_ruang_unit'], array(
         $id
      ));

      return $result;
   }

   # multiunit
   public function GetListPenggunaanRuang($tglAwal, $tglAkhir, $ruang)
   {
   	  $user = $this->GetDataUserByUsername($_SESSION['username']);
      $result = $this->Open($this->mSqlQueries['get_list_penggunaan_ruang'], array(
         $tglAwal,
         $tglAwal,
         $tglAkhir,
         $tglAkhir,
         '%' . $ruang . '%',
         '%' . $ruang . '%',
      	 $user['unitkerjaKodeSistem'],
      	 $user['unitkerjaKodeSistem'].'.%'
      ));
      return $result;
   }

   public function GetPenggunaanRuangById($id)
   {
      $result = $this->Open($this->mSqlQueries['get_penggunaan_ruang_by_id'], array(
         $id
      ));

      return $result[0];
   }

   #execute

   public function Add($dataList)
   {
      $this->StartTrans();
      $ruang = $dataList['unit'];
      $arrayData = array(
         $dataList['ruang_id'],$dataList['jenis'],
         $dataList['tgl_awal_year'] . '-' . $dataList['tgl_awal_mon'] . '-' . $dataList['tgl_awal_day'],
         $dataList['jam_awal'] . ':' . $dataList['menit_awal'] . ':00',
         $dataList['tgl_akhir_year'] . '-' . $dataList['tgl_akhir_mon'] . '-' . $dataList['tgl_akhir_day'],
         $dataList['jam_akhir'] . ':' . $dataList['menit_akhir'] . ':00',
         $dataList['keterangan'],
			$dataList['rupengpemegangkunci'],
         $dataList['user_id'],
         $dataList['user_id'],
         $dataList['penyewa'],
         $dataList['telp'],
         $dataList['alamat'],
         $dataList['biaya'],
			$dataList['potongan']
      );

      $result = $this->Execute($this->mSqlQueries['add_penggunaan_ruang'], $arrayData);

      if (!$this->AffectedRows())
      {
         $this->EndTrans(false);

         return false;
      }
      $id = $this->LastInsertId();

      if ($ruang != '')
      {

         foreach($ruang as $key => $value)
         {
            $this->Execute($this->mSqlQueries['add_penggunaan_ruang_unit'], array(
               $value,
               $id
            ));

            if (!$this->AffectedRows())
            {
               $this->EndTrans(false);

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

      return true;
   }

   public function Update($dataList, $updateId)
   {
      $this->StartTrans();
      $ruang = $dataList['unit'];

      $arrayData = array(
         $dataList['ruang_id'], $dataList['jenis'],
         $dataList['tgl_awal_year'] . '-' . $dataList['tgl_awal_mon'] . '-' . $dataList['tgl_awal_day'],
         $dataList['jam_awal'] . ':' . $dataList['menit_awal'] . ':00',
         $dataList['tgl_akhir_year'] . '-' . $dataList['tgl_akhir_mon'] . '-' . $dataList['tgl_akhir_day'],
         $dataList['jam_akhir'] . ':' . $dataList['menit_akhir'] . ':00',
         $dataList['keterangan'],
			$dataList['rupengpemegangkunci'],
         $dataList['user_id'],
         $dataList['user_id'],
			$dataList['penyewa'],
         $dataList['telp'],
         $dataList['alamat'],
         $dataList['biaya'],
			$dataList['potongan'],
         $updateId
      );
      $result = $this->Execute($this->mSqlQueries['update_penggunaan_ruang'], $arrayData);

      if (!$result)
      {
         $this->EndTrans(false);
         return false;
      }

		$search = $this->GetPenggunaanRuangUnit($updateId);
		if(count($search) != ''){
			$result = $this->Execute($this->mSqlQueries['delete_penggunaan_ruang_unit'], array(
				$updateId
			));

			if (!$this->AffectedRows())
			{
				$this->EndTrans(false);
				return false;
			}

			if ($ruang != '')
			{
				foreach($ruang as $key => $value)
				{
					$this->Execute($this->mSqlQueries['add_penggunaan_ruang_unit'], array(
						$key,
						$updateId
					));

					if (!$this->AffectedRows())
					{
						$this->EndTrans(false);
						return false;
					}
				}
			}
		} else {
			if ($ruang != '')
			{
				foreach($ruang as $key => $value)
				{
					$this->Execute($this->mSqlQueries['add_penggunaan_ruang_unit'], array(
						$key,
						$updateId
					));

					if (!$this->AffectedRows())
					{
						$this->EndTrans(false);
						return false;
					}
				}
			}
		}

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

   public function Delete($id)
   {
      $result = $this->Execute($this->mSqlQueries['delete_penggunaan_ruang'], array(
         $id
      ));

      return $this->AffectedRows();
   }

   # multiunit
	public function GetDataGridForPrintRtf()
   {
   	  $user = $this->GetDataUserByUsername($_SESSION['username']);
      $result = $this->Open($this->mSqlQueries['get_list_penggunaan_ruang_rtf'], array($user['unitkerjaKodeSistem'],$user['unitkerjaKodeSistem'].'.%'));
		return $result;
   }
}
?>