<?php
class StockTransfer{
 
    // database connection and table name
    private $conn;
    private $table_name = "stock_transfer";
 
	// object properties
	public $id;
	public $stock_transfer_date;
    public $store_from;
    public $store_to;
	public $stock_transfer_by;
	public $item_code;
	public $item_name;
	public $quantity;
	public $category;
	public $brand;
	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_transfer_date=:stock_transfer_date, store_from=:store_from, store_to=:store_to, stock_transfer_by=:stock_transfer_by, item_code=:item_code, item_name=:item_name, quantity=:quantity, remark=:remark, entry_by=:entry_by, entry_date_time=:entry_date_time";
		$stmt = $this->conn->prepare($query);
	 
	 
		$stmt->bindParam(":stock_transfer_date", $this->stock_transfer_date);
		$stmt->bindParam(":store_from", $this->store_from);
        $stmt->bindParam(":store_to", $this->store_to);
		$stmt->bindParam(":stock_transfer_by", $this->stock_transfer_by);
		$stmt->bindParam(":item_code", $this->item_code);
		$stmt->bindParam(":item_name", $this->item_name);
		$stmt->bindParam(":quantity", $this->quantity);
		$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_transfer_date){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " stock_transfer_date =:stock_transfer_date";
		}

		if($condition!=""){
			$condition = " WHERE " . $condition;
		}
		$query = "SELECT * FROM " . $this->table_name . $condition . " ORDER BY stock_transfer.id DESC ";
		$stmt = $this->conn->prepare($query);
		$this->stock_transfer_date=htmlspecialchars(strip_tags($this->stock_transfer_date));

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

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

	function search(){
		$condition = "";

		if($this->stock_transfer_date){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " stock_transfer_date =:stock_transfer_date";
		}
		if($this->df){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " stock_transfer_date >=:df";
		}
		if($this->store_from){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " store_from =:store_from";
		}
		if($this->store_to){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " store_to =:store_to";
		}
		if($this->dt){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " stock_transfer_date <=:dt";
		}
		if($this->item_code){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " stock_transfer.item_code =:item_code";
		}
		if($this->item_name){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " (stock_transfer.item_name LIKE  :item_name '%' or stock_transfer.item_name LIKE '%' :item_name '%' or stock_transfer.item_name Like '%' :item_name ) ";
		}

		if($condition!=""){
			$condition = " WHERE " . $condition;
		}
		$query = "SELECT stock_transfer.* FROM " . $this->table_name . " left join item on stock_transfer.item_code=item.item_code " . $condition . " ORDER BY stock_transfer_date,store_from,store_to,stock_transfer.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_transfer_date) $stmt->bindParam(":stock_transfer_date", $this->stock_transfer_date);
		if($this->df) $stmt->bindParam(":df", $this->df);
		if($this->dt) $stmt->bindParam(":dt", $this->dt);
		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_from) $stmt->bindParam(":store_from", $this->store_from);
		if($this->store_to) $stmt->bindParam(":store_to", $this->store_to);


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