<?php
class Item{

	private $conn;
	private $table_name = "item";

	public $id;		
	// public $city;
    public $item_code;
    public $item_name;
	public $min_stock_level;
	public $price;
	public $sales_price;
	public $entry_by;
	public $entry_date_time;

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

	function create(){
        $query = "INSERT INTO " . $this->table_name . " SET item_code=:item_code
        , item_name=:item_name, min_stock_level=:min_stock_level, entry_by=:entry_by, entry_date_time=:entry_date_time";
		$stmt = $this->conn->prepare($query);

		// $stmt->bindParam(":city", $this->city);
        $stmt->bindParam(":item_code", $this->item_code);
        $stmt->bindParam(":item_name", $this->item_name);
		$stmt->bindParam(":min_stock_level", $this->min_stock_level);
		$stmt->bindParam(":entry_by", $this->entry_by);
		$stmt->bindParam(":entry_date_time", $this->entry_date_time);

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

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

	function getAllRows(){

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

	function search(){
		$condition = "";

		if($this->item_code){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " item_code=:item_code ";
		}
		if($this->item_name){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " (item_name LIKE  :item_name '%' or item_name LIKE '%' :item_name '%' or item_name Like '%' :item_name ) ";
		}

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

		$query = "SELECT * FROM " . $this->table_name . $condition . " ORDER BY item_code ";
		$stmt = $this->conn->prepare($query);
		if($this->item_code) $stmt->bindParam(":item_code", $this->item_code);
		if($this->item_name) $stmt->bindParam(":item_name", $this->item_name);

		$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;
	}
	
	function getItemDetail($code){
		$query = "SELECT * FROM `" . $this->table_name . "` WHERE item_code = ? LIMIT 0,1";
		$stmt = $this->conn->prepare( $query );
		$stmt->bindParam(1, $code);
		$stmt->execute();
		if($stmt->rowCount()>0){
			$row = $stmt->fetch(PDO::FETCH_ASSOC);
			$this->id = $row['id'];	
			$this->item_code = $row['item_code'];
			$this->item_name = $row['item_name'];
			$this->min_stock_level = $row['min_stock_level'];
			$this->price = $row['price'];
			$this->sales_price = $row['sales_price'];
		}
	}

	function update(){
        $query = "UPDATE " . $this->table_name . " SET item_name=:item_name, min_stock_level=:min_stock_level, entry_by=:entry_by, entry_date_time=:entry_date_time where item_code=:item_code";
		$stmt = $this->conn->prepare($query);

        $stmt->bindParam(":item_code", $this->item_code);
        $stmt->bindParam(":item_name", $this->item_name);
		$stmt->bindParam(":min_stock_level", $this->min_stock_level);
		$stmt->bindParam(":entry_by", $this->entry_by);
		$stmt->bindParam(":entry_date_time", $this->entry_date_time);

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

	function updateSalesPrice(){
        $query = "UPDATE " . $this->table_name . " SET sales_price=:sales_price where item_code=:item_code";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":item_code", $this->item_code);
		$stmt->bindParam(":sales_price", $this->price);


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

	function updatePrice(){
        $query = "UPDATE " . $this->table_name . " SET price=:price where item_code=:item_code";
		$stmt = $this->conn->prepare($query);

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


		if($stmt->execute()){
			return true;
		}	 
		return false;
	}
    
	function autocomplete(){
		$condition = "";
		
		// if($this->city){
		// 	if($condition!=""){
		// 		$condition .= " AND ";
		// 	}
		// 	$condition .= " city=:city ";
		// }
		if($this->item_name){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " (item_name LIKE  :item_name '%' or item_name LIKE '%' :item_name '%' or item_name Like '%' :item_name ) ";
		}
		if($condition!=""){
			$condition = " WHERE " . $condition;
		}

		$query = "SELECT * FROM " . $this->table_name . $condition . " ORDER BY item_code";
		$stmt = $this->conn->prepare($query);
		// if($this->city) $stmt->bindParam(":city", $this->city);
		if($this->item_name) $stmt->bindParam(":item_name", $this->item_name);
		$stmt->execute();
		return $stmt;
	}

}
?>
