<?php
class InstallationDetail{

	private $conn;
	private $table_name = "installation_detail";

	public $id;	
	public $installation_id;		
    public $item_code;	
    public $item_name;	
    public $quantity;	
    public $unit;	
    public $entry_by;
    public $entry_date_time;

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

	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET installation_id=:installation_id, item_code=:item_code, item_name=:item_name
        , quantity=:quantity, unit=:unit, entry_by=:entry_by, entry_date_time=:entry_date_time, install_type_=:install_type_, install_date_=:install_date_, city_=:city_
        , id_no_=:id_no_";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":installation_id", $this->installation_id);
		$stmt->bindParam(":item_code", $this->item_code);
        $stmt->bindParam(":item_name", $this->item_name);
		$stmt->bindParam(":quantity", $this->quantity);
        $stmt->bindParam(":unit", $this->unit);
		$stmt->bindParam(":entry_by", $this->entry_by);
        $stmt->bindParam(":entry_date_time", $this->entry_date_time);
        $stmt->bindParam(":install_type_", $this->install_type_);
        $stmt->bindParam(":install_date_", $this->install_date_);
        $stmt->bindParam(":city_", $this->city_);
        $stmt->bindParam(":id_no_", $this->id_no_);

		if($stmt->execute()){
			return true;
		}	 
		return false;
	}

	function getInstallationDetail(){
		$query = "SELECT *,ifnull(q,0) AS quantity FROM install_items 
		LEFT JOIN (SELECT item_code AS aa,ifnull(quantity,0) AS q FROM installation_detail WHERE installation_id=:installation_id order by item_code) AS a ON install_items.item_code=a.aa
		WHERE category=(SELECT install_type_ FROM installation_detail WHERE installation_id=:installation_id GROUP BY install_type_)";

		$stmt = $this->conn->prepare( $query );
		$stmt->bindParam(":installation_id", $this->installation_id);
		$stmt->execute();
		return $stmt;
	}

	function delete(){
		$query = "DELETE FROM " . $this->table_name . " WHERE installation_id = ?";
		$stmt = $this->conn->prepare($query);
		$stmt->bindParam(1, $this->installation_id);
		if($stmt->execute()){
			return true;
		}
		return false;
    }
	// function exist(){
	// 	$query = "SELECT id FROM " . $this->table_name . " WHERE category = ? and brand= ? LIMIT 0,1";
	// 	$stmt = $this->conn->prepare( $query );	
	 
	// 	$stmt->bindParam(1, $this->category);
	// 	$stmt->bindParam(2, $this->brand);
		
	// 	$stmt->execute();
	// 	if($stmt->rowCount()>0){
	// 		return true;
	// 	}
	// 	return false;
	// }

	// function getAllRows(){
	// 	$query = "SELECT * FROM " . $this->table_name . " ORDER BY item_code ";
	// 	$stmt = $this->conn->prepare($query);
	// 	$stmt->execute();
	// 	return $stmt;
	// }

    // function delete(){
	// 	$query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
	// 	$stmt = $this->conn->prepare($query);
	// 	$stmt->bindParam(1, $this->id);
	// 	if($stmt->execute()){
	// 		return true;
	// 	}
	// 	return false;
    // }
    

}
?>
