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 . */ @@ -24,317 +24,323 @@ require('./header.php');
- - -
-
-
-

Welcome

-
- -
- -
-
- -
-
-
-
- -
-
-
- -
-
-
- - - -
-
-
- = CURDATE()"))); - ?> -
-
-
-
- -
-
-
- -
-
-
- - - -
-
-
- CURDATE()"))); - ?> -
-
-
-
- -
-
-
- -
-
-
- - - -
-
-
- '1970-01-01'", "`invoice_date`<=CURDATE()", "`payment_received` <= '1970-01-01' OR `payment_received` IS NULL"))); - ?> -
-
-
-
- -
-
-
- -
-
-
- - - -
-
-
- -
-
-
-
- Currently active offers -
- -
- = CURDATE()")); - $list = array(); - foreach ($offers as $offer) { - $start = BusinessAdmin::formatDate($offer->getStartDate(), false); - $end = BusinessAdmin::formatDate($offer->getEndDate(), false); - $since = mktime(0,0,0,date("n"),date("j"),date("Y")) - $offer->getStartDate(); - $total = $offer->getEndDate() - $offer->getStartDate(); - $percentage = ($total == 0) ? 100 : round($since / $total * 100); + - // We want to sort on percentage (DESC) and secondly end date (ASC) so start date (DESC) - $base = str_pad($percentage, 3, '0', STR_PAD_LEFT) . $offer->getStartDate(); - for ($i = 0; isset($list["$base-$i"]); $i++); - $list["$base-$i"] = array( - 'start' => $start, - 'end' => $end, - 'id' => $offer->getId(), - 'contactClientName' => $offer->getContact()->getClient()->getName(), - 'percentage' => $percentage, - 'price_excl' => constants::invoice_valuta . $offer->calculate(offer::SUBTOTAL), - 'price_incl' => constants::invoice_valuta . $offer->calculate(offer::TOTAL) - ); - } - krsort($list, SORT_STRING); - foreach ($list as $item) { - echo "

#{$item['id']} to {$item['contactClientName']} ({$item['start']} - {$item['end']}; {$item['price_excl']} excl. VAT, {$item['price_incl']} incl. VAT){$item['percentage']}% complete

-
-
-
"; - } - if (count($list) == 0) { - echo "There are no currently active offers."; - } - ?> -
- -
- -
-
-
-
- Finished offers -
- -
- - - - - - - - - - CURDATE()")); - foreach ($offers as $offer) { - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - } - if (count($offers) == 0) { - echo ""; - } - ?> - -
#ContactOffer ended
{$offer->getId()}{$offer->getContact()->getClient()->getName()}".BusinessAdmin::formatDate($offer->getEndDate(), false)."
There are no offers that need an invoice.
-
- -
- -
-
-
-
- Currently open invoices -
-
- - - - - - - - - - '1970-01-01'", "`invoice_date`<=CURDATE()", "`payment_received` <= '1970-01-01' OR `payment_received` IS NULL")); - if (count($offers) == 0) { - echo ""; - } else { - foreach ($offers as $offer) { - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - } - } - ?> - -
#ContactInvoice sent
There are no currently open invoices.
{$offer->getId()}{$offer->getContact()->getClient()->getName()}".BusinessAdmin::formatDate($offer->getInvoiceDate(), false)."
-
- -
- -
-
- -
-
-
-
- Timeline -
- -
-
    - $offer->getId(), - 'contact' => $offer->getContact()->getName(), - 'assignments' => '', - 'assignments_header' => '' - ); - foreach ($offer->getAssignments() as $assignment) { - $temp['assignments'] .= "{$assignment->getTitle()}
    (".constants::invoice_valuta."{$assignment->calculate(assignment::SUBTOTAL)} excl. VAT, ".constants::invoice_valuta."{$assignment->calculate(assignment::TOTAL)} incl. VAT)

    {$assignment->getDescription()}

    "; - $temp['assignments_header'] .= "{$assignment->getTitle()}
    (".constants::invoice_valuta."{$assignment->calculate(assignment::SUBTOTAL)} excl. VAT, ".constants::invoice_valuta."{$assignment->calculate(assignment::TOTAL)} incl. VAT)
    "; - } - $list[] = array_merge($temp, array('type' => 'start', 'time' => $offer->getStartDate(), 'description' => 'Offer started')); - $sort_list[] = $offer->getStartDate() . $offer->getId() . 0; - $list[] = array_merge($temp, array('type' => 'end', 'time' => $offer->getEndDate(), 'description' => 'Offer ended')); - $sort_list[] = $offer->getEndDate() . $offer->getId() . 1; - if ($offer->getInvoiceDate() > 0) { - $list[] = array_merge($temp, array('type' => 'invoice', 'time' => $offer->getInvoiceDate(), 'description' => 'Invoice sent')); - $sort_list[] = $offer->getInvoiceDate() . $offer->getId() . 2; - if ($offer->getPaymentReceived() > 0) { - $list[] = array_merge($temp, array('type' => 'payment_received', 'time' => $offer->getPaymentReceived(), 'description' => 'Payment received')); - $sort_list[] = $offer->getPaymentReceived() . $offer->getId() . 3; - } - } - } - array_multisort($sort_list, SORT_DESC, $list); - foreach ($list as $item) { - if ($item['time'] > time()) { - continue; - } - echo ""; - switch ($item['type']) { - case 'start': echo "
    "; break; - case 'end': echo "
    "; break; - case 'invoice': echo "
    "; break; - case 'payment_received': echo "
    "; break; - } - echo "
    "; - echo "

    #{$item['id']} to {$item['contact']}: {$item['description']}

    ".BusinessAdmin::formatDate($item['time'],false,true,true)."

    "; - switch ($item['type']) { - case 'start': echo "
    {$item['assignments']}
    "; break; - default: echo "
    {$item['assignments_header']}
    "; - } - echo "
    "; - echo ""; - } - if (count($list) == 0) { - echo '
  • -
    -
    -

    Welcome to BusinessAdmin!

    -
    When you start adding projects, a timeline will appear here.
    -
    -
  • '; - } - ?> -
-
- -
- -
-
-
- +
+
+
+

Welcome

+
+ +
+ +
+
+ +
+
+
+
+ +
+
+
+ +
+
+
+ + + +
+
+
+ = CURDATE()"))); + ?> +
+
+
+
+ +
+
+
+ +
+
+
+ + + +
+
+
+ CURDATE()"))); + ?> +
+
+
+
+ +
+
+
+ +
+
+
+ + + +
+
+
+ '1970-01-01'", + "`invoice_date`<=CURDATE()", + "NOT EXISTS (SELECT * FROM `".constants::db_prefix."payment` WHERE `offerId`=`".constants::db_prefix."offer`.`id` AND `date` != TIMESTAMP(0))"))); + ?> +
+
+
+
+ +
+
+
+ +
+
+
+ + + +
+
+
+ +
+
+
+
+ Currently active offers +
+ +
+ = CURDATE()")); + $list = array(); + foreach ($offers as $offer) { + $start = BusinessAdmin::formatDate($offer->getStartDate(), false); + $end = BusinessAdmin::formatDate($offer->getEndDate(), false); + $since = mktime(0,0,0,date("n"),date("j"),date("Y")) - $offer->getStartDate(); + $total = $offer->getEndDate() - $offer->getStartDate(); + $percentage = ($total == 0) ? 100 : round($since / $total * 100); + + // We want to sort on percentage (DESC) and secondly end date (ASC) so start date (DESC) + $base = str_pad($percentage, 3, '0', STR_PAD_LEFT) . $offer->getStartDate(); + for ($i = 0; isset($list["$base-$i"]); $i++); + $list["$base-$i"] = array( + 'start' => $start, + 'end' => $end, + 'id' => $offer->getId(), + 'contactClientName' => $offer->getContact()->getClient()->getName(), + 'percentage' => $percentage, + 'price_excl' => constants::invoice_valuta . $offer->calculate(offer::SUBTOTAL), + 'price_incl' => constants::invoice_valuta . $offer->calculate(offer::TOTAL) + ); + } + krsort($list, SORT_STRING); + foreach ($list as $item) { + echo "

#{$item['id']} to {$item['contactClientName']} ({$item['start']} - {$item['end']}; {$item['price_excl']} excl. VAT, {$item['price_incl']} incl. VAT){$item['percentage']}% complete

+
+
+
"; + } + if (count($list) == 0) { + echo "There are no currently active offers."; + } + ?> +
+ +
+ +
+
+
+
+ Finished offers +
+ +
+ + + + + + + + + + CURDATE()")); + foreach ($offers as $offer) { + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + } + if (count($offers) == 0) { + echo ""; + } + ?> + +
#ContactOffer ended
{$offer->getId()}{$offer->getContact()->getClient()->getName()}".BusinessAdmin::formatDate($offer->getEndDate(), false)."
There are no offers that need an invoice.
+
+ +
+ +
+
+
+
+ Currently open invoices +
+
+ + + + + + + + + + '1970-01-01'", + "`invoice_date`<=CURDATE()", + "NOT EXISTS (SELECT * FROM `".constants::db_prefix."payment` WHERE `offerId`=`".constants::db_prefix."offer`.`id` AND `date` != TIMESTAMP(0))")); + if (count($offers) == 0) { + echo ""; + } else { + foreach ($offers as $offer) { + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + } + } + ?> + +
#ContactInvoice sent
There are no currently open invoices.
{$offer->getId()}{$offer->getContact()->getClient()->getName()}".BusinessAdmin::formatDate($offer->getInvoiceDate(), false)."
+
+ +
+ +
+
+ +
+
+
+
+ Timeline +
+ +
+
    + $offer->getId(), + 'contact' => $offer->getContact()->getName(), + 'assignments' => '', + 'assignments_header' => '' + ); + foreach ($offer->getAssignments() as $assignment) { + $temp['assignments'] .= "{$assignment->getTitle()}
    (".constants::invoice_valuta."{$assignment->calculate(assignment::SUBTOTAL)} excl. VAT, ".constants::invoice_valuta."{$assignment->calculate(assignment::TOTAL)} incl. VAT)

    {$assignment->getDescription()}

    "; + $temp['assignments_header'] .= "{$assignment->getTitle()}
    (".constants::invoice_valuta."{$assignment->calculate(assignment::SUBTOTAL)} excl. VAT, ".constants::invoice_valuta."{$assignment->calculate(assignment::TOTAL)} incl. VAT)
    "; + } + $list[] = array_merge($temp, array('type' => 'start', 'time' => $offer->getStartDate(), 'description' => 'Offer started')); + $sort_list[] = $offer->getStartDate() . $offer->getId() . 0; + $list[] = array_merge($temp, array('type' => 'end', 'time' => $offer->getEndDate(), 'description' => 'Offer ended')); + $sort_list[] = $offer->getEndDate() . $offer->getId() . 1; + if ($offer->getInvoiceDate() > 0) { + $list[] = array_merge($temp, array('type' => 'invoice', 'time' => $offer->getInvoiceDate(), 'description' => 'Invoice sent')); + $sort_list[] = $offer->getInvoiceDate() . $offer->getId() . 2; + if ($offer->getPaymentReceived() > 0) { + $list[] = array_merge($temp, array('type' => 'payment_received', 'time' => $offer->getPaymentReceived(), 'description' => 'Payment received')); + $sort_list[] = $offer->getPaymentReceived() . $offer->getId() . 3; + } + } + } + array_multisort($sort_list, SORT_DESC, $list); + foreach ($list as $item) { + if ($item['time'] > time()) { + continue; + } + echo ""; + switch ($item['type']) { + case 'start': echo "
    "; break; + case 'end': echo "
    "; break; + case 'invoice': echo "
    "; break; + case 'payment_received': echo "
    "; break; + } + echo "
    "; + echo "

    #{$item['id']} to {$item['contact']}: {$item['description']}

    ".BusinessAdmin::formatDate($item['time'],false,true,true)."

    "; + switch ($item['type']) { + case 'start': echo "
    {$item['assignments']}
    "; break; + default: echo "
    {$item['assignments_header']}
    "; + } + echo "
    "; + echo ""; + } + if (count($list) == 0) { + echo '
  • +
    +
    +

    Welcome to BusinessAdmin!

    +
    When you start adding projects, a timeline will appear here.
    +
    +
  • '; + } + ?> +
+
+ +
+ +
+
+
+
diff --git a/include/offers-edit.php b/include/offers-edit.php index 95de9b3..5ed95c4 100644 --- a/include/offers-edit.php +++ b/include/offers-edit.php @@ -38,7 +38,12 @@ try { $response->success = $offer->setInvoiceDate(strtotime($_REQUEST['value'])); break; case 'payment_received': - $response->success = $offer->setPaymentReceived(strtotime($_REQUEST['value'])); + $payment = $offer->getPayment(); + if (is_null($payment)) { + $response->success = $offer->createPayment(strtotime($_REQUEST['value'])); + } else { + $response->success = $payment->setDate(strtotime($_REQUEST['value'])); + } break; default: $response->http_response_code(404); diff --git a/install/index.php b/install/index.php index 213eec7..f565696 100644 --- a/install/index.php +++ b/install/index.php @@ -82,13 +82,19 @@ if (isset($_GET['create_tables'])) { `invoice_date` date NOT NULL, `accepted` tinyint(1) unsigned NOT NULL DEFAULT '0', `invoice_fileId` smallint(5) unsigned DEFAULT NULL, - `payment_received` date DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `invoice_fileId` (`invoice_fileId`), KEY `contactId` (`contactId`), KEY `contactId_2` (`contactId`), KEY `invoice_fileId_2` (`invoice_fileId`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1"); + + $_pdo->query("CREATE TABLE IF NOT EXISTS `payment` ( + `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT, + `offerId` smallint(5) unsigned NOT NULL, + `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`) + ) ENGINE=InnoDB DEFAULT CHARSET=latin1;"); $_pdo->query("CREATE TABLE IF NOT EXISTS `".constants::db_prefix."user` ( `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT, @@ -110,6 +116,9 @@ if (isset($_GET['create_tables'])) { ADD CONSTRAINT `offer_ibfk_1` FOREIGN KEY (`invoice_fileId`) REFERENCES `".constants::db_prefix."file` (`id`), ADD CONSTRAINT `offer_ibfk_2` FOREIGN KEY (`contactId`) REFERENCES `".constants::db_prefix."contact` (`id`)"); + $_pdo->query("ALTER TABLE `payment` + ADD CONSTRAINT `payment_ibfk_1` FOREIGN KEY (`offerId`) REFERENCES `offer` (`id`);"); + echo "Succeeded creating the database tables."; } catch (PDOException $e) { diff --git a/install/upgrade.php b/install/upgrade.php index e145ba7..78f5dff 100644 --- a/install/upgrade.php +++ b/install/upgrade.php @@ -86,6 +86,28 @@ if (isset($_GET['upgrade'])) { } } + if (lower_version($_GET['upgrade'], '0.4.2')) { + try { + $_pdo->query("CREATE TABLE IF NOT EXISTS `".constants::db_prefix."payment` ( + `id` smallint(5) unsigned NOT NULL, + `offerId` smallint(5) unsigned NOT NULL, + `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP + ) ENGINE=InnoDB DEFAULT CHARSET=latin1;"); + + $offers = $_pdo->query("SELECT `id`,`payment_received` FROM `".constants::db_prefix."offer` WHERE `payment_received` IS NOT NULL"); + $offers = $offers->fetchAll(PDO::FETCH_ASSOC); + foreach ($offers as $offer) { + $received = $offer['payment_received']; + $offer = new offer($_pdo, $offer['id']); + $offer->createPayment($received); + } + + $_pdo->query("ALTER TABLE `".constants::db_prefix."offer` DROP `payment_received`;"); + } catch (PDOException $e) { + echo "Altering the database structure failed with a PDOException ({$e->getCode()}): {$e->getMessage()}
" . $e->getTraceAsString(); + } + } + echo "
All done."; } ?> -- cgit v1.2.3