<?php
class Income{
    private $conn;
    private $table_name = "`income`";
 
	public $id;
	public $date;
	public $voucher_no;
	public $income_reason;
	public $particular;
	public $quantity;
	public $amount;
	public $income_by;
	public $city;
	public $entry_by;
	public $entry_date_time;
	public $accounting_view;
	
    public function __construct($db){
        $this->conn = $db;
    }

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

		$stmt->bindParam(":date", $this->date);
		$stmt->bindParam(":voucher_no", $this->voucher_no);
		$stmt->bindParam(":income_reason", $this->income_reason);
		$stmt->bindParam(":particular", $this->particular);
		$stmt->bindParam(":quantity", $this->quantity);
		$stmt->bindParam(":amount", $this->amount);
		$stmt->bindParam(":income_by", $this->income_by);
        $stmt->bindParam(":city", $this->city);
		$stmt->bindParam(":entry_by", $this->entry_by);
		$stmt->bindParam(":entry_date_time", $this->entry_date_time);
		$stmt->bindParam(":accounting_view", $this->accounting_view);
		
		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, income_reason=:income_reason, particular=:particular, quantity=:quantity, amount=:amount, income_by=:income_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(":income_reason", $this->income_reason);
		$stmt->bindParam(":particular", $this->particular);
		$stmt->bindParam(":quantity", $this->quantity);
		$stmt->bindParam(":amount", $this->amount);
		$stmt->bindParam(":income_by", $this->income_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(){
		$condition = "";
		if($this->accounting_view==0){
			$condition .= " and accounting_view=0 ";
		}
		$query = "SELECT * from " . $this->table_name . " where city=:city and date=:date " . $condition;
		$stmt = $this->conn->prepare( $query );
		$stmt->bindParam(":city", $this->city);
        $stmt->bindParam(":date", $this->date);

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

	function search(){
		$condition = "";

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

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

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

			$condition .= " income_reason=:income_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,income_reason";
		$stmt = $this->conn->prepare($query);
		if($this->df) $stmt->bindParam(":df", $this->df);
		if($this->dt) $stmt->bindParam(":dt", $this->dt);
		if($this->income_reason) $stmt->bindParam(":income_reason", $this->income_reason);
		if($this->city) $stmt->bindParam(":city", $this->city);


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

}
?>
