<?php
class Purchase_Detail{

    // database connection and table name
	private $conn;
	private $table_name = "purchase_detail";

	public $id;
	public $purchase_id;
	public $item_code;
	public $item_name;
	public $quantity;
	public $price;
	public $is_deleted;

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

	// function getPurchaseDetail(){
	// 	$condition = "";
		
	// 	if($this->purchase_id){
	// 		if($condition!=""){ $condition .= " AND "; }
	// 		$condition .= " purchase_id =:purchase_id ";
	// 	}
		
	// 	if($condition!=""){
	// 		$condition = " WHERE " . $condition;
	// 	}
	// 	$query = "SELECT * FROM " . $this->table_name . $condition . "";
	// 	$stmt = $this->conn->prepare($query);

	// 	$this->purchase_id=htmlspecialchars(strip_tags($this->purchase_id));

	// 	if($this->purchase_id) $stmt->bindParam(":purchase_id", $this->purchase_id);

	// 	$stmt->execute();
	// 	return $stmt;
	// }
	
	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET purchase_id=:purchase_id, item_code=:item_code, item_name=:item_name, quantity=:quantity, price=:price";
		$stmt = $this->conn->prepare($query);

		$this->purchase_id=htmlspecialchars(strip_tags($this->purchase_id));
		$this->item_code=htmlspecialchars(strip_tags($this->item_code));
		$this->item_name=htmlspecialchars(strip_tags($this->item_name));
		$this->quantity=htmlspecialchars(strip_tags($this->quantity));
		$this->price=htmlspecialchars(strip_tags($this->price));

		$stmt->bindParam(":purchase_id", $this->purchase_id);
		$stmt->bindParam(":item_code", $this->item_code);
		$stmt->bindParam(":item_name", $this->item_name);
		$stmt->bindParam(":quantity", $this->quantity);
		$stmt->bindParam(":price", $this->price);

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

	function delete(){
		$query = "Update " . $this->table_name . " set is_deleted=:is_deleted WHERE purchase_id=:purchase_id";
		$stmt = $this->conn->prepare($query);
		$this->purchase_id=htmlspecialchars(strip_tags($this->purchase_id));
		$stmt->bindParam(":purchase_id", $this->purchase_id);
		$stmt->bindParam(":is_deleted", $this->is_deleted);
		if($stmt->execute()){
			return true;
		}
		return false;
	}
}
