<?php
class Promotion{
    private $conn;
    private $table_name = "promotion";
 
	public $id;
	public $name;
	public $city;
	public $package;
	public $months;
	public $period_from;
	public $period_to;
	public $bandwidth;
	public $package_price;
	public $installation_fees;
	public $extra_days;
	public $gift;
	public $entry_by;
	public $entry_date_time;
	
    public function __construct($db){
        $this->conn = $db;
    }

    function create(){
		$query = "INSERT INTO `" . $this->table_name . "` SET name=:name, city=:city, months=:months, package=:package, period_from=:period_from, period_to=:period_to, bandwidth=:bandwidth, package_price=:package_price, installation_fees=:installation_fees, extra_days=:extra_days, gift=:gift, entry_by=:entry_by, entry_date_time=:entry_date_time";

		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":name", $this->name);
		$stmt->bindParam(":city", $this->city);
		$stmt->bindParam(":package", $this->package);
		$stmt->bindParam(":months", $this->months);
		$stmt->bindParam(":period_from", $this->period_from);
		$stmt->bindParam(":period_to", $this->period_to);
		$stmt->bindParam(":bandwidth", $this->bandwidth);
		$stmt->bindParam(":package_price", $this->package_price);
		$stmt->bindParam(":installation_fees", $this->installation_fees);
		$stmt->bindParam(":extra_days", $this->extra_days);
		$stmt->bindParam(":gift", $this->gift);
		$stmt->bindParam(":entry_by", $this->entry_by);
		$stmt->bindParam(":entry_date_time", $this->entry_date_time);

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

	function exist(){
		$query = "SELECT * FROM `" . $this->table_name . "` WHERE `name`=:name AND city=:city AND period_from=:period_from AND period_to=:period_to";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":name", $this->name);
		$stmt->bindParam(":city", $this->city);
		$stmt->bindParam(":period_from", $this->period_from);
		$stmt->bindParam(":period_to", $this->period_to);
		$stmt->execute();
		if($stmt->rowCount()>0){
			return true;
		}
		return false;		
	}

	function delete(){
		$query = "DELETE FROM `" . $this->table_name . "` WHERE id=:id";
		$stmt = $this->conn->prepare($query);
		$stmt->bindParam(":id", $this->id);
		if($stmt->execute()){
			return true;
		}
		return false;		
	}

	function getPromotionByCity(){
		$condition = "";
		if($this->city){
			$condition = " WHERE city=:city ";
		}
		$query = "SELECT *, LEFT(entry_date_time, 10) AS `date` FROM " . $this->table_name . $condition . " ORDER BY city, id";
		$stmt = $this->conn->prepare( $query );	
        if($this->city) $stmt->bindParam(":city", $this->city);	
        $stmt->execute();
		return $stmt;	
	}

	function getPromotion(){

		$query = "SELECT * FROM " . $this->table_name . " where city=:city and (:date between period_from and period_to) and package=:package and months=:months ORDER BY entry_date_time";
		$stmt = $this->conn->prepare( $query );	
        $stmt->bindParam(":city", $this->city);	
		$stmt->bindParam(":date", $this->date);	
		$stmt->bindParam(":package", $this->package);	
		$stmt->bindParam(":months", $this->months);	
        $stmt->execute();
		return $stmt;	
	}

	function getOneRow(){

		$query = "SELECT * FROM " . $this->table_name . " where city=:city and (:date between period_from and period_to) and package=:package ";
		$stmt = $this->conn->prepare( $query );	
        if($this->city) $stmt->bindParam(":city", $this->city);	
		if($this->date) $stmt->bindParam(":date", $this->date);	
		if($this->package) $stmt->bindParam(":package", $this->package);	
        $stmt->execute();
		return $stmt;	
	}
}
?>
