<?php
class Transfer{
    private $conn;
    private $table_name = "`transfer`";
 
	public $id;
	public $date;
	public $voucher_no;
	public $transfer_reason;
	public $transfer_from;
	public $transfer_to;
	public $particular;
	public $quantity;
	public $amount;
	public $transfer_by;
	public $city;
	public $entry_by;
	public $entry_date_time;

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

	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET date=:date, voucher_no=:voucher_no, transfer_reason=:transfer_reason, transfer_from=:transfer_from, transfer_to=:transfer_to, particular=:particular, quantity=:quantity, amount=:amount, transfer_by=:transfer_by, city=:city, entry_by=:entry_by, entry_date_time=:entry_date_time";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":date", $this->date);
		$stmt->bindParam(":voucher_no", $this->voucher_no);
		$stmt->bindParam(":transfer_reason", $this->transfer_reason);
		$stmt->bindParam(":transfer_from", $this->transfer_from);
		$stmt->bindParam(":transfer_to", $this->transfer_to);
		$stmt->bindParam(":particular", $this->particular);
		$stmt->bindParam(":quantity", $this->quantity);
		$stmt->bindParam(":amount", $this->amount);
		$stmt->bindParam(":transfer_by", $this->transfer_by);
        $stmt->bindParam(":city", $this->city);
		$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 update(){
		$query = "UPDATE " . $this->table_name . " SET date=:date, voucher_no=:voucher_no, transfer_reason=:transfer_reason, transfer_from=:transfer_from, transfer_to=:transfer_to, particular=:particular, quantity=:quantity, amount=:amount, transfer_by=:transfer_by, entry_by=:entry_by, entry_date_time=:entry_date_time where id=:id and city=:city";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":id", $this->id);
		$stmt->bindParam(":date", $this->date);
		$stmt->bindParam(":voucher_no", $this->voucher_no);
		$stmt->bindParam(":transfer_reason", $this->transfer_reason);
		$stmt->bindParam(":transfer_from", $this->transfer_from);
		$stmt->bindParam(":transfer_to", $this->transfer_to);
		$stmt->bindParam(":particular", $this->particular);
		$stmt->bindParam(":quantity", $this->quantity);
		$stmt->bindParam(":amount", $this->amount);
		$stmt->bindParam(":transfer_by", $this->transfer_by);
        $stmt->bindParam(":city", $this->city);
		$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 getAllRows(){
		$query = "SELECT * from " . $this->table_name . " where city=:city and date=:date ";
		$stmt = $this->conn->prepare( $query );
		$stmt->bindParam(":city", $this->city);
        $stmt->bindParam(":date", $this->date);

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

	function search(){
		$condition = "";

		if($this->df){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= "date>=:df ";
		}

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

		if($this->transfer_reason){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " transfer_reason=:transfer_reason ";
			// $condition .= " (sales.sales_no LIKE  :sales_no '%' or sales.sales_no LIKE '%' :sales_no '%' or sales.sales_no Like '%' :sales_no ) ";
		}

		if($this->city){
			if($condition!=""){
				$condition .= " AND ";
			}
			$condition .= " city=:city ";
		}

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

		// $query = "SELECT * FROM sales " . $condition . " and city=:city order by id_no";
		$query = "SELECT * from " . $this->table_name . $condition . " order by date,transfer_reason";
		$stmt = $this->conn->prepare($query);
		if($this->df) $stmt->bindParam(":df", $this->df);
		if($this->dt) $stmt->bindParam(":dt", $this->dt);
		if($this->transfer_reason) $stmt->bindParam(":transfer_reason", $this->transfer_reason);
		if($this->city) $stmt->bindParam(":city", $this->city);


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

	function delete(){
		$query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
		$stmt = $this->conn->prepare($query);
		$this->id = htmlspecialchars(strip_tags($this->id));
		$stmt->bindParam(1, $this->id);
		if($stmt->execute()){
			return true;
		}
		return false;
	}

	
}
?>
