aboutsummaryrefslogtreecommitdiff
path: root/classes/offer.php
diff options
context:
space:
mode:
authorCamil Staps2016-07-27 14:52:49 +0200
committerCamil Staps2016-07-27 15:01:54 +0200
commitc67d248601031a0245dfe64b609fa6623868014b (patch)
tree640b5e040b1c045c361824d1cd3d5af1e6ecf8ec /classes/offer.php
parentv0.4.1 (diff)
v0.4.2 moved payment_received to separate table/class payment(s)
Diffstat (limited to 'classes/offer.php')
-rw-r--r--classes/offer.php93
1 files changed, 67 insertions, 26 deletions
diff --git a/classes/offer.php b/classes/offer.php
index 0eb2fa1..56a8878 100644
--- a/classes/offer.php
+++ b/classes/offer.php
@@ -34,9 +34,8 @@ class offer {
* @var int $invoice_date A UNIX timestamp of the invoice date
* @var bool $accepted Whether the offer is accepted or not
* @var null|int $invoice_fileId If an invoice has been generated, an the id of the file
- * @var null|int $payment_received A UNIX timestamp of the date the payment has been received
*/
- protected $pdo, $id, $contactId, $start_date, $end_date, $invoice_date, $accepted, $invoice_fileId, $payment_received;
+ protected $pdo, $id, $contactId, $start_date, $end_date, $invoice_date, $accepted, $invoice_fileId;
const SUBTOTAL = 1;
const VAT = 2;
@@ -70,7 +69,6 @@ class offer {
$this->invoice_date = strtotime($offer['invoice_date']);
$this->accepted = (bool) $offer['accepted'];
$this->invoice_fileId = $offer['invoice_fileId'];
- $this->payment_received = ($offer['payment_received'] == null) ? null : strtotime($offer['payment_received']);
}
//------------------------------------------------------------------------------
@@ -147,7 +145,7 @@ class offer {
/**
* Get all discount ids for this offer
*
- * @see offer::getDiscounts() This funtion returns instances of the discount class instead of just the ids
+ * @see offer::getDiscounts() This funtion returns instances of the discount class instead of just the ids
*
* @throws PDOException Is something went wrong with the database
*
@@ -165,11 +163,11 @@ class offer {
/**
* Get all discounts for this offer
*
- * @see offer::getDiscountIds() This function returns just the ids of the discounts, and not instances of the discount class
+ * @see offer::getDiscountIds() This function returns just the ids of the discounts, and not instances of the discount class
*
* @throws PDOException If something went wrong with the database
*
- * @return discount[] An array indexed by id of instances of the discount class
+ * @return discount[] An array indexed by id of instances of the discount class
*/
public function getDiscounts() {
$ids = $this->getDiscountIds();
@@ -181,6 +179,42 @@ class offer {
}
/**
+ * Get the payment id for this offer
+ *
+ * @see offer::getPayment() This funtion returns an instance of the payment class instead of just the id
+ *
+ * @throws PDOException Is something went wrong with the database
+ *
+ * @return int|null The id, or null if no payment exists
+ */
+ public function getPaymentId() {
+ $ids = array();
+ $payments = $this->pdo->query("SELECT `id` FROM `".constants::db_prefix."payment` WHERE `offerId`={$this->id}")->fetchAll(PDO::FETCH_ASSOC);
+ foreach ($payments as $payment) {
+ return $payment['id'];
+ }
+ return null;
+ }
+
+ /**
+ * Get the payment for this offer
+ *
+ * @see offer::getPaymentId() This function returns just the id of the payment, and not an instance of the payment class
+ *
+ * @throws PDOException If something went wrong with the database
+ *
+ * @return payment|null The payment, or null if it does not exist
+ */
+ public function getPayment() {
+ $id = $this->getPaymentId();
+ if (is_null($id)) {
+ return null;
+ } else {
+ return new payment($this->pdo, $id);
+ }
+ }
+
+ /**
* Get the start date of the assignment
*
* @return int The start date as a UNIX timestamp
@@ -273,26 +307,11 @@ class offer {
* @return int|null The date as a UNIX timestamp, or null if it wasn't received yet
*/
public function getPaymentReceived() {
- return $this->payment_received;
- }
-
- /**
- * Set the payment received date of the assignment
- *
- * @param int $payment_received The new date the payment has been received as a UNIX timestamp
- *
- * @throws PDOException If something went wrong with the database
- *
- * @return bool True on succes, false on failure
- */
- public function setPaymentReceived($payment_received) {
- $stmt = $this->pdo->prepare("UPDATE `".constants::db_prefix."offer` SET `payment_received`=? WHERE `id`=?");
- $stmt->execute(array(date('Y-m-d', $payment_received), $this->id));
- if ($stmt->rowCount() == 1) {
- $this->payment_received = $payment_received;
- return true;
+ $payment = $this->getPayment();
+ if (is_null($payment)) {
+ return null;
} else {
- return false;
+ return $payment->getDate();
}
}
@@ -486,7 +505,7 @@ class offer {
* @throws PDOException If something went wrong with the database
* @throws Exception If there was a problem with the input
*
- * @return discount A new instance of the discount class containing the new discount
+ * @return discount A new instance of the discount class containing the new discount
*/
public function createDiscount($title, $description, $value, $vat) {
$stmt = $this->pdo->prepare("INSERT INTO `".constants::db_prefix."discount` (`offerId`,`title`,`description`,`value`,`VAT_percentage`) VALUES (?,?,?,?,?)");
@@ -506,6 +525,28 @@ class offer {
}
/**
+ * Add a payment for this order
+ *
+ * @param string $date Optional: the date for the payment
+ *
+ * @throws PDOException If something went wrong with the database
+ * @throws Exception If there was a problem with the input
+ *
+ * @return payment A new instance of the payment class containing the new payment
+ */
+ public function createPayment($date=null) {
+ $date = is_null($date) ? time() : $date;
+ $stmt = $this->pdo->prepare("INSERT INTO `".constants::db_prefix."payment` (`offerId`,`date`) VALUES (?,?)");
+ $stmt->execute([$this->id, date('Y-m-d H:i:s', $date)]);
+ if ($stmt->rowCount() == 1) {
+ return new payment($this->pdo, $this->pdo->lastInsertId());
+ } else {
+ $error = $stmt->errorInfo();
+ throw new Exception($error[2]);
+ }
+ }
+
+ /**
* Generate a PDF invoice
*
* @throws PDOException If something went wrong with the database