<?php
class SalesDetail{
	    // database connection and table name
	private $conn;
	private $table_name = "sales_detail";

	// object properties

	public $id;
	public $invoice_no;
	public $promotion_id;
	public $extra_days_id;
    public $mbps;
	public $days;
	public $package_percent;
	public $fee_percent;
	public $gift;
	public $status;
	public $entry_by;
	public $entry_date_time;

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

	function create(){
		$query = "INSERT INTO `" . $this->table_name . "` SET invoice_no=:invoice_no, promotion_id=:promotion_id, extra_days_id=:extra_days_id, mbps=:mbps, days=:days
        , package_percent=:package_percent, fee_percent=:fee_percent, gift=:gift, status=:status, entry_by=:entry_by, entry_date_time=:entry_date_time ";

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

		$stmt->bindParam(":invoice_no", $this->invoice_no);
        $stmt->bindParam(":promotion_id", $this->promotion_id);
		$stmt->bindParam(":extra_days_id", $this->extra_days_id);
        $stmt->bindParam(":mbps", $this->mbps);
		$stmt->bindParam(":days", $this->days);
		$stmt->bindParam(":package_percent", $this->package_percent);
		$stmt->bindParam(":fee_percent", $this->fee_percent);
		$stmt->bindParam(":gift", $this->gift);
		$stmt->bindParam(":status", $this->status);
		$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 createSuspend(){
		$query = "INSERT INTO `" . $this->table_name . "` SET invoice_no=:invoice_no, suspend_id=:suspend_id, days=:days, gift=''
        , status=:status, entry_by=:entry_by, entry_date_time=:entry_date_time ";

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

		$stmt->bindParam(":invoice_no", $this->invoice_no);
        $stmt->bindParam(":suspend_id", $this->suspend_id);
		$stmt->bindParam(":days", $this->days);
		$stmt->bindParam(":status", $this->status);
		$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 updateSuspend(){
		$query = "UPDATE `" . $this->table_name . "` SET days=:days where suspend_id=:suspend_id ";

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

		$stmt->bindParam(":days", $this->days);
        $stmt->bindParam(":suspend_id", $this->suspend_id);

		if($stmt->execute()){	
			$this->id = $this->conn->lastInsertId();
			return true;
		}
		return false;		
    }

	function chksuspend(){
		$query = "SELECT * FROM " . $this->table_name . " WHERE suspend_id=:suspend_id ";
		$stmt = $this->conn->prepare($query);	

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

	}

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

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

	function deleteSales(){
		$query = "DELETE FROM " . $this->table_name . " WHERE invoice_no =:invoice_no";
		$stmt = $this->conn->prepare($query);
		$this->invoice_no = htmlspecialchars(strip_tags($this->invoice_no));
		$stmt->bindParam(":invoice_no", $this->invoice_no);
		if($stmt->execute()){
			return true;
		}
		return false;
	}
	
	function editFromSales(){
		$query = "UPDATE `" . $this->table_name . "` SET mbps=:mbps, days=:days where invoice_no=:invoice_no and status='Sales'";

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

		$stmt->bindParam(":invoice_no", $this->invoice_no);
		$stmt->bindParam(":mbps", $this->mbps);
		$stmt->bindParam(":days", $this->days);

		if($stmt->execute()){	
			$this->id = $this->conn->lastInsertId();
			return true;
		}
		return false;		
    }

}
?>