<?php
class Payment{
    // database connection and table name
	private $conn;
	private $table_name = "payment";
	
	// object properties
	public $id;
	public $invoice_no;
	public $date;
    public $payment_type;
	public $amount;
	public $city;
	public $entry_by;
	public $entry_date_time;
    	// constructor with $db as database connection
	public function __construct($db){
		$this->conn = $db;
	}

	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET invoice_no=:invoice_no, date=:date, payment_type=:payment_type, amount=:amount, city=:city, entry_by=:entry_by, entry_date_time=:entry_date_time";
		
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":invoice_no", $this->invoice_no);
		$stmt->bindParam(":date", $this->date);
        $stmt->bindParam(":payment_type", $this->payment_type);
		$stmt->bindParam(":amount", $this->amount);
		$stmt->bindParam(":city", $this->city);
		$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 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 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 getOnePayment(){
		$query = "SELECT * FROM " . $this->table_name . " where invoice_no=:invoice_no order by id";
		$stmt = $this->conn->prepare( $query );
		$stmt->bindParam(":invoice_no", $this->invoice_no);
		$stmt->execute();
		return $stmt;
	}




	// function getPaymentSMSCount($c,$d){
	// 	$query = "SELECT COUNT(*) AS total_payment_count FROM " . $this->table_name . " WHERE date='$d' AND payment_done=0" . (($c=="")?"":" AND city='$c'");
	// 	$stmt = $this->conn->prepare( $query );
	// 	if($stmt->execute()){
	// 		$row = $stmt->fetch(PDO::FETCH_ASSOC);
	// 		return $row['total_payment_count'];
	// 	}
	// 	return false;
	// }

	// function getPaymentSMSList($c){

	// 	$query = "SELECT payment.id,sales.id_no,amount,mbps,days,payment.invoice_no,phone,ph_no FROM payment
	// 	LEFT JOIN sales ON payment.invoice_no=sales.invoice_no
	// 	LEFT JOIN customer ON sales.customer_id=customer.id 
	// 	LEFT JOIN city ON sales.city=city.name 
	// 	WHERE date=:d AND payment_done=0" . (($c=="")?"":" AND payment.city='$c'");
		
	// 	$stmt = $this->conn->prepare($query);
	// 	// if($this->df) $stmt->bindParam(":df", $this->df);
	// 	// if($this->dt) $stmt->bindParam(":dt", $this->dt);
	// 	if($this->d) $stmt->bindParam(":d", $this->d);

	// 	$stmt->execute();
	// 	return $stmt;
	// }

	// function updateSMSPaymentDone(){
	// 	$query = "UPDATE `" . $this->table_name . "` SET payment_done=1 WHERE id=:id ";

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

	// 	$stmt->bindParam(":id", $this->id);
		
	// 	if($stmt->execute()){
	// 		return true;
	// 	}	 
	// 	return false;
	// }


}
?>
