<?php
class StockIn{

	private $conn;
	private $table_name = "stock_in";
	public $id;	
	public $stock_in_date;	
	public $voucher_no;
	public $store;	
	public $item_code;
	public $item_name;	
	public $quantity;	
	public $price;
	public $entry_by;
	public $is_deleted;

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


	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET stock_in_date=:stock_in_date, voucher_no=:voucher_no, store=:store, item_code=:item_code, item_name=:item_name, quantity=:quantity, price=:price, entry_by=:entry_by";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":stock_in_date", $this->stock_in_date);
		$stmt->bindParam(":voucher_no", $this->voucher_no);
		$stmt->bindParam(":store", $this->store);
		$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(":entry_by", $this->entry_by);

		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 id=:id";
		$stmt = $this->conn->prepare($query);
		$this->id=htmlspecialchars(strip_tags($this->id));
		$stmt->bindParam(":id", $this->id);
		$stmt->bindParam(":is_deleted", $this->is_deleted);
		if($stmt->execute()){
			return true;
		}
		return false;
	}

	// function deleteFromPurchase(){
	// 	$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 getOneRow(){
	// 	$query = "SELECT * FROM " . $this->table_name . "  WHERE id = ?";
	// 	$stmt = $this->conn->prepare($query);
	// 	$stmt->bindParam(1, $this->id);
	// 	$stmt->execute();
	// 	$row = $stmt->fetch(PDO::FETCH_ASSOC);
	// 	$this->store = $row['store'];
	// 	$this->item_code = $row['item_code'];
	// 	$this->item_name = $row['item_name'];
	// 	$this->quantity = $row['quantity'];

	// }

	function getAllRows(){
		$condition = "";
		if($this->stock_in_date){
			// if($condition!=""){
			// 	$condition .= " AND ";
			// }
			$condition .= " and stock_in_date =:stock_in_date";
		}

		// if($condition!=""){
		// 	$condition = " WHERE " . $condition;
		// }
		$query = "SELECT * FROM " . $this->table_name . " where is_deleted is null " . $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);

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

	function search(){
		$condition = "";
		if($this->df){
			// if($condition!=""){ $condition .= " AND "; }
			$condition .= " and stock_in_date >=:df ";
		}

		if($this->dt){
			// if($condition!=""){ $condition .= " AND "; }
			$condition .= " and stock_in_date <=:dt ";
		}

		if($this->store){
			// if($condition!=""){ $condition .= " AND "; }
			$condition .= " and store =:store ";
		}
		
		// 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($this->stock_in_by){
		// 	if($condition!=""){ $condition .= " AND "; }
		// 	$condition .= " (stock_in_by LIKE  :stock_in_by '%' or stock_in_by LIKE '%' :stock_in_by '%' or stock_in_by Like '%' :stock_in_by ) ";
		// }
		
		// if($condition!=""){
		// 	$condition = " WHERE " . $condition;
		// }
		$query = "select * from stock_in where is_deleted is null " . $condition . " order by stock_in_date";
		$stmt = $this->conn->prepare($query);
		$this->df=htmlspecialchars(strip_tags($this->df));
		$this->dt=htmlspecialchars(strip_tags($this->dt));
		$this->store=htmlspecialchars(strip_tags($this->store));
		// $this->item_code=htmlspecialchars(strip_tags($this->item_code));
		// $this->item_name=htmlspecialchars(strip_tags($this->item_name));
		// $this->stock_in_by=htmlspecialchars(strip_tags($this->stock_in_by));
		
		if($this->df) $stmt->bindParam(":df", $this->df);
		if($this->dt) $stmt->bindParam(":dt", $this->dt);
		if($this->store) $stmt->bindParam(":store", $this->store);
		// if($this->item_code) $stmt->bindParam(":item_code", $this->item_code);
		// if($this->item_name) $stmt->bindParam(":item_name", $this->item_name);
		// if($this->stock_in_by) $stmt->bindParam(":stock_in_by", $this->stock_in_by);
		$stmt->execute();
		return $stmt;
	}

	// function getOneVoucherNo(){
	// 	$query = "SELECT * FROM " . $this->table_name . "  WHERE id = ?";
	// 	$stmt = $this->conn->prepare($query);
	// 	$stmt->bindParam(1, $this->id);
	// 	$stmt->execute();
	// 	$row = $stmt->fetch(PDO::FETCH_ASSOC);
		
	// 	$this->id = $row['id'];
	// 	$this->voucher_no = $row['voucher_no'];
	// }

	// function getListByVoucherNo(){
	// 	$query = "SELECT * FROM " . $this->table_name . " where voucher_no=:voucher_no";
	// 	$stmt = $this->conn->prepare($query);
	// 	if($this->voucher_no) $stmt->bindParam(":voucher_no", $this->voucher_no);
	// 	$stmt->execute();
	// 	return $stmt;
	// }
}
