<?php
class Deposit{
    // database connection and table name
    private $conn;
    private $table_name = "deposit";
 
    // object properties
	public $id;
	public $checkin_id;
	public $reservation_no;
	public $room_no;
	public $deposit_status;
	public $deposit_date;
	public $deposit_type;
	public $deposit_amount;
	public $entry_by;
	public $entry_date_time;

	public $checkout_paid;

	// public $deposit_by;
	// public $df;
	// public $dt;
 
    public function __construct($db){
        $this->conn = $db;
    }
	
	function getAllRowsFromReservation(){
		$query = "SELECT * FROM " . $this->table_name . " WHERE booking_no =:reservation_no and vehicle_no=:room_no ORDER BY id";
		$stmt = $this->conn->prepare($query);
		
		$stmt->bindParam(":reservation_no", $this->reservation_no);
		$stmt->bindParam(":room_no", $this->room_no);
		$stmt->execute();
		return $stmt;
	}

	function getAllRowsFromCheckIn(){
		$query = "SELECT * FROM " . $this->table_name . " WHERE pickup_id =:checkin_id ORDER BY id";
		$stmt = $this->conn->prepare($query);
		
		$stmt->bindParam(":checkin_id", $this->checkin_id);
		$stmt->execute();
		return $stmt;
	}

	function getAllRows(){
		$query = "SELECT * FROM " . $this->table_name . " WHERE pickup_id = ? ORDER BY id";
		$stmt = $this->conn->prepare($query);
		$this->checkin_id = htmlspecialchars(strip_tags($this->checkin_id));
		$stmt->bindParam(1, $this->checkin_id);
		$stmt->execute();
		return $stmt;
	}
	
	function delete(){
		$query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
		$stmt = $this->conn->prepare($query);
		$this->id = htmlspecialchars(strip_tags($this->id));
		$stmt->bindParam(1, $this->id);
		if($stmt->execute()){
			return true;
		}
		return false;
	}

	function deleteFromOther(){
		$query = "DELETE FROM " . $this->table_name . " WHERE booking_no=:reservation_no and vehicle_no=:room_no and deposit_status='reservation'";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":reservation_no", $this->reservation_no);
		$stmt->bindParam(":room_no", $this->room_no);
		if($stmt->execute()){
			return true;
		}
		return false;
	}

	function deleteFromOtherCID(){
		$query = "DELETE FROM " . $this->table_name . " WHERE pickup_id=:checkin_id and deposit_status<>'reservation'";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":checkin_id", $this->checkin_id);
		if($stmt->execute()){
			return true;
		}
		return false;
	}

	function updateFromReserveCheckIn(){
		$query = "update " . $this->table_name . " set pickup_id=:checkin_id WHERE booking_no=:reservation_no and vehicle_no=:room_no";
		$stmt = $this->conn->prepare($query);
		$stmt->bindParam(":checkin_id", $this->checkin_id);
		$stmt->bindParam(":reservation_no", $this->reservation_no);
		$stmt->bindParam(":room_no", $this->room_no);
		if($stmt->execute()){
			return true;
		}
		return false;
	}

	function updateCheckOut(){
		$query = "update " . $this->table_name . " set return_paid=1 WHERE pickup_id=:checkin_id";
		$stmt = $this->conn->prepare($query);
		$stmt->bindParam(":checkin_id", $this->checkin_id);
		if($stmt->execute()){
			return true;
		}
		return false;
	}
	
	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET pickup_id=:checkin_id,booking_no=:reservation_no,vehicle_no=:room_no,deposit_status=:deposit_status, `deposit_date`=:deposit_date, deposit_amount=:deposit_amount, deposit_type=:deposit_type, entry_by=:entry_by";
		$stmt = $this->conn->prepare($query);
	 
		$stmt->bindParam(":checkin_id", $this->checkin_id);
		$stmt->bindParam(":reservation_no", $this->reservation_no);
		$stmt->bindParam(":room_no", $this->room_no);
		$stmt->bindParam(":deposit_status", $this->deposit_status);
		$stmt->bindParam(":deposit_date", $this->deposit_date);
		$stmt->bindParam(":deposit_amount", $this->deposit_amount);
		$stmt->bindParam(":deposit_type", $this->deposit_type);
		$stmt->bindParam(":entry_by", $this->entry_by);
	 
		if($stmt->execute()){
			return true;
		}	 
		return false;
	}	
	
	    function updateReservation() {
        $query = "UPDATE deposit set booking_no=:reservation_no where booking_no=:reservation_no_old ";
        $stmt = $this->conn->prepare($query);

        $stmt->bindParam(":reservation_no", $this->reservation_no);
        $stmt->bindParam(":reservation_no_old", $this->reservation_no_old);
        if ($stmt->execute()) {
            return true;
        }
        return false;
    }
    
    
    function DeleteReservation(){
		$query = "Delete From " . $this->table_name . " WHERE booking_no=:reservation_no";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":reservation_no", $this->reservation_no);
		if($stmt->execute()){
			return true;
		}
		return false;
	}

// 	function search(){

// 		$condition = "";

// 		if($this->df){
// 			if($condition!=""){
// 				$condition .= " AND ";
// 			}
// 			$condition .= " deposit.date >=:df";
// 		}

// 		if($this->dt){
// 			if($condition!=""){
// 				$condition .= " AND ";
// 			}
// 			$condition .= " deposit.date <=:dt";
// 		}

// 		if($this->deposit_by){
// 			if($condition!=""){ $condition .= " AND "; }
// 			$condition .= " (checkin_guests.name LIKE  :deposit_by '%' or checkin_guests.name LIKE '%' :deposit_by '%' or checkin_guests.name Like '%' :deposit_by ) ";
// 		}

// 		if($condition!=""){
// 			$condition = " WHERE " . $condition;
// 		}

// 		$query = "select  deposit.checkin_id, deposit.date as deposit_date, checkin.reservation_no,checkin.room_type,checkin.room_no, 
// checkin_guests.name as deposit_by, sum(deposit.amount) as amount, deposit.entry_by as receive_by
// from deposit left join checkin on checkin.id=deposit.checkin_id 
// left join checkin_guests on checkin_guests.checkin_id=checkin.id " . $condition . " group by deposit.checkin_id order by deposit_date,room_type,room_no";

// 		$stmt = $this->conn->prepare($query);
// 		$this->df=htmlspecialchars(strip_tags($this->df));
// 		$this->dt=htmlspecialchars(strip_tags($this->dt));
// 		$this->deposit_by=htmlspecialchars(strip_tags($this->deposit_by));
		

// 		if($this->df) $stmt->bindParam(":df", $this->df);
// 		if($this->dt) $stmt->bindParam(":dt", $this->dt);
// 		if($this->deposit_by) $stmt->bindParam(":deposit_by", $this->deposit_by);
// 		$stmt->execute();
// 		return $stmt;
// 	}

// 	function getDepositByCID(){
// 		$query = "SELECT IFNULL(SUM(amount), 0) AS amount FROM " . $this->table_name . " WHERE checkin_id=:checkin_id AND payment_type='From Reservation'";
// 		$stmt = $this->conn->prepare( $query );
// 		$this->checkin_id=htmlspecialchars(strip_tags($this->checkin_id));
	 
// 		$stmt->bindParam(":checkin_id", $this->checkin_id);
		
// 		$stmt->execute();
// 		if($stmt->rowCount()>0){
// 			$row = $stmt->fetch(PDO::FETCH_ASSOC);
// 			return (int)$row['amount'];
// 		}
// 		return false;
// 	}
}
?>
