<?php
class branch{

	private $conn;
	private $table_name = "branch";

	public $id;	
	public $code;	
	public $branch;	
	public $entry_by;
	public $entry_date_time;

	public $branch_id;	

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

	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET code=:code, branch=:branch, entry_by=:entry_by, entry_date_time=:entry_date_time";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":code", $this->code);
		$stmt->bindParam(":branch", $this->branch);
		$stmt->bindParam(":entry_by", $this->entry_by);
		$stmt->bindParam(":entry_date_time", $this->entry_date_time);

		if($stmt->execute()){
			return true;
		}	 
		return false;
	}

	function update(){
		$query = "UPDATE " . $this->table_name . " SET code=:code, branch=:branch, 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(":code", $this->code);
		$stmt->bindParam(":branch", $this->branch);
		$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 exist(){
		$query = "SELECT id FROM " . $this->table_name . " WHERE  code= :code or branch= :branch LIMIT 0,1";
		$stmt = $this->conn->prepare( $query );	
	 
		$stmt->bindParam(":code", $this->code);
		$stmt->bindParam(":branch", $this->branch);
		
		$stmt->execute();
		if($stmt->rowCount()>0){
			return true;
		}
		return false;
	}

	// function getAllRows(){
	// 	$condition = "";
	// 	if($this->branch){
	// 		$condition .= " WHERE branch=:branch ";
	// 	}
	// 	$query = "SELECT * FROM branch  order by branch";
	// 	$stmt = $this->conn->prepare($query);
	// 	if($this->branch) $stmt->bindParam(":branch", $this->branch);
	// 	$stmt->execute();
	// 	return $stmt;
	// }

	function getAllRows(){
		$condition = "";
		if($this->branch_id != 0){
			$condition .= " WHERE id=:branch_id ";
		}
		$query = "SELECT * FROM branch " . $condition . " order by id";
		$stmt = $this->conn->prepare($query);
		if($this->branch_id != 0) $stmt->bindParam(":branch_id", $this->branch_id);
		$stmt->execute();
		return $stmt;
	}

	function getAllRowsForTransfer(){

		$query = "SELECT * FROM branch order by id";
		$stmt = $this->conn->prepare($query);
		$stmt->execute();
		return $stmt;
	}

    function delete(){
		$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;
    }
    

}
?>
