<?php
class Plans{
	    // database connection and table name
	private $conn;
	private $table_name = "plans";

	// object properties

	public $id;
	public $plan;
	public $mbps;
	public $month;
	public $price;
	public $amount;
	public $entry_by;
	public $entry_date_time;

	public function __construct($db){
		$this->conn = $db;
	}

	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET plan=:plan,mbps=:mbps,month=:month,price=:price,amount=:amount, entry_by=:entry_by, entry_date_time=:entry_date_time";

		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":plan", $this->plan);
		$stmt->bindParam(":mbps", $this->mbps);
		$stmt->bindParam(":month", $this->month);
		$stmt->bindParam(":price", $this->price);
		$stmt->bindParam(":amount", $this->amount);
		$stmt->bindParam(":entry_by", $this->entry_by);
		$stmt->bindParam(":entry_date_time", $this->entry_date_time);

		if($stmt->execute()){
			return true;
		}
		return false;		
	}

	function delete(){
		$query = "DELETE FROM " . $this->table_name . " WHERE purchase_id = ?";
		$stmt = $this->conn->prepare($query);
		$stmt->bindParam(1, $this->purchase_id);
		if($stmt->execute()){
			return true;
		}
		return false;
	}

	function getPrice($mb,$d){
		$query = "SELECT price FROM `" . $this->table_name . "` WHERE mbps = ? and `days` = ? LIMIT 0,1";
		$stmt = $this->conn->prepare( $query );
		$stmt->bindParam(1, $mb);
		$stmt->bindParam(2, $d);
		$stmt->execute();
		if($stmt->rowCount()>0){
			$row = $stmt->fetch(PDO::FETCH_ASSOC);
			$this->price = $row['price'];
		}else{
			$query = "SELECT price FROM `" . $this->table_name . "` WHERE mbps = ? and `days` < ? LIMIT 0,1";
			$stmt = $this->conn->prepare( $query );
			$stmt->bindParam(1, $mb);
			$stmt->bindParam(2, $d);
			$stmt->execute();
			$row = $stmt->fetch(PDO::FETCH_ASSOC);
			$this->price = $row['price'];
		}
	}
}
?>