<?php
class StockOut{
 
    // database connection and table name
    private $conn;
    private $table_name = "stock_out";
 
	// object properties
	public $id;
	public $stock_out_date;
    public $store;
	public $stock_out_by;
	public $item_code;
	public $item_name;
	public $quantity;
	public $category;
	public $brand;
	public $status;
	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_out_date=:stock_out_date, store=:store, stock_out_by=:stock_out_by, item_code=:item_code, item_name=:item_name, quantity=:quantity, `status`=:status, remark=:remark, entry_by=:entry_by, entry_date_time=:entry_date_time";
		$stmt = $this->conn->prepare($query);
	 
	 
		$stmt->bindParam(":stock_out_date", $this->stock_out_date);
		$stmt->bindParam(":store", $this->store);
		$stmt->bindParam(":stock_out_by", $this->stock_out_by);
		$stmt->bindParam(":item_code", $this->item_code);
		$stmt->bindParam(":item_name", $this->item_name);
		$stmt->bindParam(":quantity", $this->quantity);
		$stmt->bindParam(":status", $this->status);
		$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_out_date){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " stock_out_date =:stock_out_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_out.id DESC ";
		$stmt = $this->conn->prepare($query);
		$this->stock_out_date=htmlspecialchars(strip_tags($this->stock_out_date));

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

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

	function search(){
		$condition = "";

		if($this->df){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " stock_out_date >=:df";
		}
		if($this->dt){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " stock_out_date <=:dt";
		}
		if($this->item_code){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " stock_out.item_code =:item_code";
		}
		if($this->item_name){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " (stock_out.item_name LIKE  :item_name '%' or stock_out.item_name LIKE '%' :item_name '%' or stock_out.item_name Like '%' :item_name ) ";
		}
		if($this->status){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " `status` =:status";
		}
		if($this->store){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " `store` =:store";
		}
		if($condition!=""){
			$condition = " WHERE " . $condition;
		}
		$query = "SELECT stock_out.* FROM " . $this->table_name . " left join item on stock_out.item_code=item.item_code " . $condition . " ORDER BY stock_out_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_out_date) $stmt->bindParam(":stock_out_date", $this->stock_out_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->status) $stmt->bindParam(":status", $this->status);
		if($this->store) $stmt->bindParam(":store", $this->store);

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