From c67d248601031a0245dfe64b609fa6623868014b Mon Sep 17 00:00:00 2001
From: Camil Staps
Date: Wed, 27 Jul 2016 14:52:49 +0200
Subject: v0.4.2 moved payment_received to separate table/class payment(s)
---
README.md | 1 +
classes/constants.php | 2 +-
classes/offer.php | 93 +++++--
classes/payment.php | 142 +++++++++++
include/home.php | 632 ++++++++++++++++++++++++------------------------
include/offers-edit.php | 7 +-
install/index.php | 11 +-
install/upgrade.php | 22 ++
8 files changed, 568 insertions(+), 342 deletions(-)
create mode 100644 classes/payment.php
diff --git a/README.md b/README.md
index d5b44f1..263ffea 100644
--- a/README.md
+++ b/README.md
@@ -171,6 +171,7 @@ are listed by name and removal time. This way, you never really lose your file.
### 0.4 (Jul 26, 2016)
+0.4.2 Moved `offer.payment_received` to a separate table `payments`.
0.4.1 Some users are administrator and can create and delete users.
0.4 User authentication mechanism.
diff --git a/classes/constants.php b/classes/constants.php
index d7c7b7c..663d603 100644
--- a/classes/constants.php
+++ b/classes/constants.php
@@ -80,5 +80,5 @@ class constants {
const password_cost = 10;
/** @const version Version of BusinessAdmin. Don't change this yourself! */
- const version = '0.4.1';
+ const version = '0.4.2';
}
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();
@@ -180,6 +178,42 @@ class offer {
return $discounts;
}
+ /**
+ * 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
*
@@ -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 (?,?,?,?,?)");
@@ -505,6 +524,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
*
diff --git a/classes/payment.php b/classes/payment.php
new file mode 100644
index 0000000..9d4782c
--- /dev/null
+++ b/classes/payment.php
@@ -0,0 +1,142 @@
+.
+ */
+
+/**
+ * An interface to the payment table in the database
+ */
+class payment {
+ /**
+ * @var pdo $pdo The PDO class for database communication
+ * @var int $id The id of the payment
+ * @var int $offerId The id of the offer this payment is linked to
+ * @var int $date A unix timestamp describing the date of the payment
+ */
+ protected $pdo, $offerId, $id, $date;
+
+ /**
+ * Create a new instance
+ *
+ * @param PDO $pdo The PDO class, to access the database
+ * @param int $id The id of the payment to fetch
+ *
+ * @throws PDOException If something went wrong with the database
+ * @throws Exception If the payment could not be found
+ */
+ public function __construct($pdo, $id) {
+ $this->pdo = $pdo;
+
+ $stmt = $this->pdo->prepare("SELECT * FROM `".constants::db_prefix."payment` WHERE `id`=?");
+ $stmt->execute(array($id));
+ if ($stmt->rowCount() == 0) {
+ throw new Exception("The payment with id '$id' could not be found.");
+ }
+ $payment = $stmt->fetch(PDO::FETCH_ASSOC);
+
+ $this->id = $payment['id'];
+ $this->offerId = $payment['offerId'];
+ $this->date = strtotime($payment['date']);
+ }
+
+ //------------------------------------------------------------------------------
+ // Getters and setters
+ //------------------------------------------------------------------------------
+
+ /**
+ * Get the ID of the payment
+ *
+ * @return int The ID
+ */
+ public function getId() {
+ return $this->id;
+ }
+
+ /**
+ * Get the ID of the offer that this payment is linked to
+ *
+ * @return int The ID
+ */
+ public function getOfferId() {
+ return $this->offerId;
+ }
+
+ /**
+ * Get the offer that this payment is linked to
+ *
+ * @return offer The offer
+ */
+ public function getOffer() {
+ return new offer($this->pdo, $this->offerId);
+ }
+
+ /**
+ * Get the date of the payment
+ *
+ * @return int A unix timestamp describing the date of the payment
+ */
+ public function getDate() {
+ return $this->date;
+ }
+
+ /**
+ * Set the date of the payment
+ *
+ * @param int $date The new date for the payment
+ *
+ * @throws PDOException If something went wrong with the database
+ *
+ * @return bool True on success, false on failure
+ */
+ public function setDate($date) {
+ $stmt = $this->pdo->prepare("UPDATE `".constants::db_prefix."payment` SET `date`=? WHERE `id`=?");
+ $stmt->execute([date('Y-m-d H:i:s', $date), $this->id]);
+ if ($stmt->rowCount() == 1) {
+ $this->date = $date;
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ //------------------------------------------------------------------------------
+ // Other functions
+ //------------------------------------------------------------------------------
+
+ /**
+ * Remove this payment from the database
+ *
+ * If this doesn't succeed (i.e. false is returned), that means the payment was removed manually or by another instance of this class
+ *
+ * @throws PDOException If something went wrong with the database
+ *
+ * @return bool True on success, false on failure
+ */
+ public function delete() {
+ $stmt = $this->pdo->prepare("DELETE FROM `".constants::db_prefix."payment` WHERE `id`=?");
+ $stmt->execute(array($this->id));
+ if ($stmt->rowCount() != 1) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+}
diff --git a/include/home.php b/include/home.php
index cb624fe..c9d0cf3 100644
--- a/include/home.php
+++ b/include/home.php
@@ -2,17 +2,17 @@
/**
* BusinessAdmin: administrative software for small companies
* Copyright (C) 2015 Camil Staps (ViviSoft)
- *
+ *
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
#{$item['id']} to {$item['contactClientName']} ({$item['start']} - {$item['end']}; {$item['price_excl']} excl. VAT, {$item['price_incl']} incl. VAT){$item['percentage']}% complete
-# | -Contact | -Offer ended | -
---|---|---|
{$offer->getId()} | "; - echo "{$offer->getContact()->getClient()->getName()} | "; - echo "".BusinessAdmin::formatDate($offer->getEndDate(), false)." | "; - echo "
There are no offers that need an invoice. |
# | -Contact | -Invoice sent | -
---|---|---|
There are no currently open invoices. | ||
{$offer->getId()} | "; - echo "{$offer->getContact()->getClient()->getName()} | "; - echo "".BusinessAdmin::formatDate($offer->getInvoiceDate(), false)." | "; - echo "
{$assignment->getDescription()}
"; - $temp['assignments_header'] .= "{$assignment->getTitle()}".BusinessAdmin::formatDate($item['time'],false,true,true)."
#{$item['id']} to {$item['contactClientName']} ({$item['start']} - {$item['end']}; {$item['price_excl']} excl. VAT, {$item['price_incl']} incl. VAT){$item['percentage']}% complete
+# | +Contact | +Offer ended | +
---|---|---|
{$offer->getId()} | "; + echo "{$offer->getContact()->getClient()->getName()} | "; + echo "".BusinessAdmin::formatDate($offer->getEndDate(), false)." | "; + echo "
There are no offers that need an invoice. |
# | +Contact | +Invoice sent | +
---|---|---|
There are no currently open invoices. | ||
{$offer->getId()} | "; + echo "{$offer->getContact()->getClient()->getName()} | "; + echo "".BusinessAdmin::formatDate($offer->getInvoiceDate(), false)." | "; + echo "
{$assignment->getDescription()}
"; + $temp['assignments_header'] .= "{$assignment->getTitle()}".BusinessAdmin::formatDate($item['time'],false,true,true)."