<?php
class IncomeReason{

	private $conn;
	private $table_name = "income_reason";

	public $id;	
	public $income_reason;	
	public $city;

	public function __construct($db){
		$this->conn = $db;
	}
	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET income_reason=:income_reason, city=:city";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":income_reason", $this->income_reason);
		$stmt->bindParam(":city", $this->city);
		
		if($stmt->execute()){
			return true;
		}	 
		return false;
	}

	function getAllRows(){
		$condition = "";
		if($this->city){
			$condition .= " where city=:city ";
		}

		$query = "SELECT * FROM " . $this->table_name . $condition . " group by income_reason ORDER BY income_reason ";
		$stmt = $this->conn->prepare($query);
		if($this->city) $stmt->bindParam(":city", $this->city);
		$stmt->execute();
		return $stmt;
	}
	function exist(){
		$query = "SELECT * FROM " . $this->table_name . " WHERE income_reason = ? LIMIT 0,1";
		$stmt = $this->conn->prepare( $query );
		// $this->name=htmlspecialchars(strip_tags($this->name));
	 
		$stmt->bindParam(1, $this->income_reason);
		
		$stmt->execute();
		if($stmt->rowCount()>0){
			return true;
		}
		return false;
	}

	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;
	}
}
?>
