<?php
class BankInOut{
    private $conn;
    private $table_name = "`bank_in_out`";
 
	public $id;
	public $city;
	public $date;
	public $description;
	public $amount;
	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 city=:city, date=:date, description=:description, amount=:amount, remark=:remark, entry_by=:entry_by, entry_date_time=:entry_date_time";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":city", $this->city);
		$stmt->bindParam(":date", $this->date);
		$stmt->bindParam(":description", $this->description);
		$stmt->bindParam(":amount", $this->amount);
		$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 update(){
		$query = "UPDATE  " . $this->table_name . " SET date=:date, description=:description, amount=:amount, remark=:remark, entry_by=:entry_by, entry_date_time=:entry_date_time WHERE id=:id";
		
		$stmt = $this->conn->prepare($query);
		
		$stmt->bindParam(":id", $this->id);	
		$stmt->bindParam(":date", $this->date);
		$stmt->bindParam(":description", $this->description);
		$stmt->bindParam(":amount", $this->amount);
		$stmt->bindParam(":remark", $this->remark);
		$stmt->bindParam(":entry_by", $this->entry_by);
		$stmt->bindParam(":entry_date_time", $this->entry_date_time);
		
		if($stmt->execute()){
			return true;
		}
		return false;
	}


	function getAllRows(){
		$query = "SELECT id,date,description,amount AS in_amount,0 as out_amount,remark FROM bank_in_out WHERE city=:city AND DESCRIPTION='Cash Deposit'
		UNION ALL
		SELECT id,date,description,amount AS in_amount,0 as out_amount,remark FROM bank_in_out WHERE city=:city AND DESCRIPTION='Interest Post Credit Customer'
		UNION ALL
		SELECT id,date,description,0 AS in_amount,amount as out_amount,remark FROM bank_in_out WHERE city=:city AND DESCRIPTION='Cash Withdrawal'
		ORDER BY DATE,id";
		$stmt = $this->conn->prepare( $query );
		$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;
	}

	function exist(){
		$query = "SELECT * FROM " . $this->table_name . " WHERE name = ? and city = ? LIMIT 0,1";
		$stmt = $this->conn->prepare( $query );
		$this->name=htmlspecialchars(strip_tags($this->name));
	 
		$stmt->bindParam(1, $this->name);
		$stmt->bindParam(2, $this->city);
		
		$stmt->execute();
		if($stmt->rowCount()>0){
			return true;
		}
		return false;
	}


}
?>
