<?php
class StockIn{
 
    // database connection and table name
    private $conn;
    private $table_name = "stock_in";
 
	// object properties
	public $id;
	public $stock_in_date;
    public $store;
	public $supplier;
	public $item_code;
	public $item_name;
	public $quantity;
	public $price;
	public $remark;
	public $entry_by;
	public $entry_date_time;
	
    public function __construct($db){
        $this->conn = $db;
    }

	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET stock_in_date=:stock_in_date, store=:store, supplier=:supplier, item_code=:item_code, item_name=:item_name, quantity=:quantity, price=:price, remark=:remark, entry_by=:entry_by, entry_date_time=:entry_date_time";
		$stmt = $this->conn->prepare($query);
	 
	 
		$stmt->bindParam(":stock_in_date", $this->stock_in_date);
		$stmt->bindParam(":store", $this->store);
		$stmt->bindParam(":supplier", $this->supplier);
		$stmt->bindParam(":item_code", $this->item_code);
		$stmt->bindParam(":item_name", $this->item_name);
		$stmt->bindParam(":quantity", $this->quantity);
		$stmt->bindParam(":price", $this->price);
		$stmt->bindParam(":remark", $this->remark);
		$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 = "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 getAllRows(){
		$condition = "";
		if($this->stock_in_date){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " stock_in_date =:stock_in_date";
		}
		if($this->store){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " store =:store";
		}
		if($condition!=""){
			$condition = " WHERE " . $condition;
		}
		$query = "SELECT * FROM " . $this->table_name . $condition . " ORDER BY stock_in.id DESC ";
		$stmt = $this->conn->prepare($query);
		$this->stock_in_date=htmlspecialchars(strip_tags($this->stock_in_date));

		if($this->stock_in_date) $stmt->bindParam(":stock_in_date", $this->stock_in_date);
		if($this->store) $stmt->bindParam(":store", $this->store);
		$stmt->execute();
		return $stmt;
	}

	function search(){
		$condition = "";
		if($this->stock_in_date){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " stock_in_date =:stock_in_date";
		}
		if($this->df){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " stock_in_date >=:df";
		}
		if($this->dt){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " stock_in_date <=:dt";
		}
		if($this->store){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " stock_in.store =:store";
		}
		if($this->item_code){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " stock_in.item_code =:item_code";
		}
		if($this->item_name){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " (stock_in.item_name LIKE  :item_name '%' or stock_in.item_name LIKE '%' :item_name '%' or stock_in.item_name Like '%' :item_name ) ";
		}
		if($this->supplier){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " (stock_in.supplier LIKE  :supplier '%' or stock_in.supplier LIKE '%' :supplier '%' or stock_in.supplier Like '%' :supplier ) ";
		}

		if($condition!=""){
			$condition = " WHERE " . $condition;
		}
		$query = "SELECT stock_in.* FROM " . $this->table_name . $condition . " ORDER BY stock_in_date,store,item_code ";
		$stmt = $this->conn->prepare($query);
		$this->item_code=htmlspecialchars(strip_tags($this->item_code));
		$this->item_name=htmlspecialchars(strip_tags($this->item_name));


		if($this->stock_in_date) $stmt->bindParam(":stock_in_date", $this->stock_in_date);
		if($this->df) $stmt->bindParam(":df", $this->df);
		if($this->dt) $stmt->bindParam(":dt", $this->dt);
		if($this->supplier) $stmt->bindParam(":supplier", $this->supplier);
		if($this->item_code) $stmt->bindParam(":item_code", $this->item_code);
		if($this->item_name) $stmt->bindParam(":item_name", $this->item_name);
		if($this->store) $stmt->bindParam(":store", $this->store);

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

	function getOneRow(){
		$query = "SELECT * FROM " . $this->table_name . " WHERE id = ?";
		$stmt = $this->conn->prepare($query);
		// $this->id = htmlspecialchars(strip_tags($this->id));
		$stmt->bindParam(1, $this->id);
		$stmt->execute();
		$row = $stmt->fetch(PDO::FETCH_ASSOC);
		$this->store = $row['store'];
		$this->item_code = $row['item_code'];
		$this->quantity = $row['quantity'];
		// $this->qty_piece = $row['qty_piece'];
		$this->stock_in_date = $row['stock_in_date'];
				
	}

	function autocompleteSupplier(){

		$query = "SELECT supplier FROM " . $this->table_name . " where (stock_in.supplier LIKE  :supplier '%' or stock_in.supplier LIKE '%' :supplier '%' or stock_in.supplier Like '%' :supplier ) group by supplier order by supplier ";
		$stmt = $this->conn->prepare($query);
		$stmt->bindParam(":supplier", $this->supplier);
		$stmt->execute();
		return $stmt;
	}
	
}
