<?php
class ExtraDays{
	    // database connection and table name
	private $conn;
	private $table_name = "extra_days";

	// object properties

	public $id;
	public $city;
	public $date;
	public $id_no;
	public $customer_id;
	public $invoice_no;
	public $reason;
	public $days;
	public $entry_by;
	public $entry_date_time;

	public function __construct($db){
		$this->conn = $db;
	}

	function create(){
		$query = "INSERT INTO `" . $this->table_name . "` SET city=:city, date=:date, id_no=:id_no, customer_id=:customer_id, invoice_no=:invoice_no, reason=:reason, days=:days, entry_by=:entry_by, entry_date_time=:entry_date_time";

		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":city", $this->city);
		$stmt->bindParam(":date", $this->date);
		$stmt->bindParam(":id_no", $this->id_no);
		$stmt->bindParam(":customer_id", $this->customer_id);
		$stmt->bindParam(":invoice_no", $this->invoice_no);
		$stmt->bindParam(":reason", $this->reason);
		$stmt->bindParam(":days", $this->days);
		$stmt->bindParam(":entry_by", $this->entry_by);
		$stmt->bindParam(":entry_date_time", $this->entry_date_time);

		if($stmt->execute()){
			$this->id = $this->conn->lastInsertId();
			return true;
		}
		return false;		
	}

	function delete(){
		$query = "UPDATE " . $this->table_name . " set is_deleted=1 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 getAllRows(){
		// $condition = "";

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

		// if($condition!=""){
		// 	$condition = " WHERE " . $condition;
		// }
		$query = "SELECT extra_days.*,customer.name,phone FROM extra_days LEFT JOIN sales ON extra_days.id_no=sales.id_no and extra_days.invoice_no=sales.invoice_no LEFT JOIN customer ON sales.customer_id=customer.id
        where extra_days.city=:city and extra_days.is_deleted=0 and extra_days.date=:date";
		$stmt = $this->conn->prepare( $query );
		$stmt->bindParam(":city", $this->city);
		$stmt->bindParam(":date", $this->date);

		$stmt->execute();
		return $stmt;
	}

	function updateNewToTempDevice(){
		$query = "UPDATE " . $this->table_name . " SET id_no=:id_no where invoice_no=:invoice_no";
		$stmt = $this->conn->prepare($query);


		$stmt->bindParam(":id_no", $this->id_no);
		$stmt->bindParam(":invoice_no", $this->invoice_no);

		if($stmt->execute()){
			// $this->id = $this->conn->lastInsertId();
			return true;
		}	 
		return false;
	}

}
?>