From 2acc7787e73c966c3fc1794d77dd758a3f56b566 Mon Sep 17 00:00:00 2001
From: Camil Staps
Date: Wed, 27 Jul 2016 21:29:57 +0200
Subject: Offer: use Model
---
classes/Offer.php | 272 +++++----------------------------------
include/assignments-overview.php | 2 +-
include/discounts-overview.php | 2 +-
include/home.php | 38 +++---
include/offers-edit.php | 6 +-
include/offers-new.php | 2 +-
include/offers-overview.php | 18 +--
include/offers-view.php | 22 ++--
include/offers.php | 21 ++-
nav.php | 12 +-
10 files changed, 93 insertions(+), 302 deletions(-)
diff --git a/classes/Offer.php b/classes/Offer.php
index 84a7da4..200fcc1 100644
--- a/classes/Offer.php
+++ b/classes/Offer.php
@@ -24,82 +24,45 @@
/**
* An interface to the offer table in the database
*/
-class Offer {
- /**
- * @var pdo $pdo The PDO class for database communication
- * @var int $id The id of the offer
- * @var int $contactId The id of the contact this offer is linked to
- * @var int $start_date A UNIX timestamp of the start date
- * @var int $end_date A UNIX timestamp of the end date
- * @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
- */
- protected $pdo, $id, $contactId, $start_date, $end_date, $invoice_date, $accepted, $invoice_fileId;
+class Offer extends Model{
+ public
+ $table = 'offer',
+ $fillable_columns = ['contactId', 'start_date', 'end_date', 'invoice_date', 'accepted', 'invoice_fileId'];
const SUBTOTAL = 1;
const VAT = 2;
const TOTAL = 3;
- /**
- * Create a new instance
- *
- * Blah
- *
- * @param PDO $pdo The PDO class, to access the database
- * @param int $id The id of the offer to fetch
- *
- * @throws PDOException If something went wrong with the database
- * @throws Exception If the offer could not be found
- */
- public function __construct($pdo, $id) {
- $this->pdo = $pdo;
-
- $stmt = $this->pdo->prepare("SELECT * FROM `".Constants::db_prefix."offer` WHERE `id`=?");
- $stmt->execute(array($id));
- if ($stmt->rowCount() == 0) {
- throw new Exception("The offer with id '$id' could not be found.");
+ protected function accessor($key, $value) {
+ switch ($key) {
+ case 'start_date':
+ case 'end_date':
+ case 'invoice_date':
+ return strtotime($value);
+ break;
+ case 'accepted':
+ return (bool) $value;
+ case 'invoice_fileId':
+ return $value == null ? null : (int) $value;
+ default:
+ return parent::accessor($key, $value);
}
- $offer = $stmt->fetch(PDO::FETCH_ASSOC);
-
- $this->id = $offer['id'];
- $this->contactId = $offer['contactId'];
- $this->start_date = strtotime($offer['start_date']);
- $this->end_date = strtotime($offer['end_date']);
- $this->invoice_date = strtotime($offer['invoice_date']);
- $this->accepted = (bool) $offer['accepted'];
- $this->invoice_fileId = $offer['invoice_fileId'];
- }
-
- //------------------------------------------------------------------------------
- // Getters and setters
- //------------------------------------------------------------------------------
-
- /**
- * Get the ID of the offer
- *
- * @return int The ID
- */
- public function getId() {
- return $this->id;
}
- /**
- * Get the ID of the contact that this offer is linked to
- *
- * @see offer::getContact() This function returns the contact as an instance of the object class
- *
- * @return int The ID
- */
- public function getContactId() {
- return $this->contactId;
+ protected function mutator($key, $value) {
+ switch ($key) {
+ case 'start_date':
+ case 'end_date':
+ case 'invoice_date':
+ return is_string($value) ? $value : date('Y-m-d', $value);
+ default:
+ return parent::mutator($key, $value);
+ }
}
/**
* Get the contact that this offer is linked to
*
- * @see offer::getContactId() This function returns just the id
- *
* @return contact The contact
*/
public function getContact() {
@@ -214,93 +177,6 @@ class Offer {
}
}
- /**
- * Get the start date of the assignment
- *
- * @return int The start date as a UNIX timestamp
- */
- public function getStartDate() {
- return $this->start_date;
- }
-
- /**
- * Set the start date of the assignment
- *
- * @param int $start_date The new start date for the assignment as a UNIX timestamp
- *
- * @throws PDOException If something went wrong with the database
- *
- * @return bool True on succes, false on failure
- */
- public function setStartDate($start_date) {
- $stmt = $this->pdo->prepare("UPDATE `".Constants::db_prefix."offer` SET `start_date`=? WHERE `id`=?");
- $stmt->execute(array(date('Y-m-d', $start_date), $this->id));
- if ($stmt->rowCount() == 1) {
- $this->start_date = $start_date;
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * Get the end date of the assignment
- *
- * @return int The end date as a UNIX timestamp
- */
- public function getEndDate() {
- return $this->end_date;
- }
-
- /**
- * Set the end date of the assignment
- *
- * @param int $end_date The new end date for the assignment as a UNIX timestamp
- *
- * @throws PDOException If something went wrong with the database
- *
- * @return bool True on succes, false on failure
- */
- public function setEndDate($end_date) {
- $stmt = $this->pdo->prepare("UPDATE `".Constants::db_prefix."offer` SET `end_date`=? WHERE `id`=?");
- $stmt->execute(array(date('Y-m-d', $end_date), $this->id));
- if ($stmt->rowCount() == 1) {
- $this->end_date = $end_date;
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * Get the invoice date of the assignment
- *
- * @return int The invoice date as a UNIX timestamp
- */
- public function getInvoiceDate() {
- return $this->invoice_date;
- }
-
- /**
- * Set the invoice date of the assignment
- *
- * @param int $invoice_date The new invoice date for the assignment as a UNIX timestamp
- *
- * @throws PDOException If something went wrong with the database
- *
- * @return bool True on succes, false on failure
- */
- public function setInvoiceDate($invoice_date) {
- $stmt = $this->pdo->prepare("UPDATE `".Constants::db_prefix."offer` SET `invoice_date`=? WHERE `id`=?");
- $stmt->execute(array(date('Y-m-d', $invoice_date), $this->id));
- if ($stmt->rowCount() == 1) {
- $this->invoice_date = $invoice_date;
- return true;
- } else {
- return false;
- }
- }
-
/**
* Get the date the payment was received
*
@@ -316,51 +192,12 @@ class Offer {
}
/**
- * Check if the offer is accepted or not
- *
- * @return bool True if the offer is accepted, false if not
- */
- public function isAccepted() {
- return $this->accepted;
- }
-
- /**
- * Toggle the `accepted' status of the offer
- *
- * @throws PDOException If something went wrong with the database
- *
- * @return bool True on success, false on failure
- */
- public function toggleAccepted() {
- $stmt = $this->pdo->prepare("UPDATE `".Constants::db_prefix."offer` SET `accepted`=? WHERE `id`=?");
- $new_value = !$this->accepted;
- $stmt->execute(array($new_value, $this->id));
- if ($stmt->rowCount() == 1) {
- $this->accepted = $new_value;
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * Get the ID of the file that the invoice of this offer is linked to
- *
- * @see offer::getInvoiceFile() This function returns the file as an instance of the file class
- *
- * @return int The ID
- */
- public function getInvoiceFileId() {
- return $this->invoice_fileId;
- }
-
- /**
- * Get the file that the invoice this offer is linked to
- *
- * @see offer::getInvoiceId() This function returns just the id
- *
- * @return file|null The file, or null if it doesn't exist
- */
+ * Get the file that the invoice this offer is linked to
+ *
+ * @see offer::getInvoiceId() This function returns just the id
+ *
+ * @return file|null The file, or null if it doesn't exist
+ */
public function getInvoiceFile() {
if ($this->invoice_fileId != null) {
return new File($this->pdo, $this->invoice_fileId);
@@ -369,30 +206,6 @@ class Offer {
}
}
- /**
- * Set the invoice file id of the assignment
- *
- * @param int $invoice_fileId The new invoice file id for the assignment
- *
- * @throws PDOException If something went wrong with the database
- *
- * @return bool True on succes, false on failure
- */
- public function setInvoiceFileId($invoice_fileId) {
- $stmt = $this->pdo->prepare("UPDATE `".Constants::db_prefix."offer` SET `invoice_fileId`=? WHERE `id`=?");
- $stmt->execute(array($invoice_fileId, $this->id));
- if ($stmt->rowCount() == 1) {
- $this->invoice_fileId = $invoice_fileId;
- return true;
- } else {
- return false;
- }
- }
-
- //------------------------------------------------------------------------------
- // Other functions
- //------------------------------------------------------------------------------
-
/**
* Calculate a handy number about the invoice
*
@@ -443,25 +256,6 @@ class Offer {
}
}
- /**
- * Remove this offer from the database
- *
- * If this doesn't succeed (i.e. false is returned), that means the offer 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."offer` WHERE `id`=?");
- $stmt->execute(array($this->id));
- if ($stmt->rowCount() != 1) {
- return false;
- } else {
- return true;
- }
- }
-
/**
* Make a new assignment linked to this order
*
@@ -566,7 +360,7 @@ class Offer {
} while (file_exists(Constants::files_folder . $filename));
$file = BusinessAdmin::createFile($this->pdo, $filename);
- $this->setInvoiceFileId($file->id);
+ $this->invoice_fileId = $file->id;
} else {
$invoice_nr = str_replace(array('invoice-','.pdf'), array('',''), $file->filename);
}
diff --git a/include/assignments-overview.php b/include/assignments-overview.php
index e10323b..c496629 100644
--- a/include/assignments-overview.php
+++ b/include/assignments-overview.php
@@ -86,7 +86,7 @@ require_once('./login.php');
diff --git a/include/discounts-overview.php b/include/discounts-overview.php
index 5253fd2..4bf2b6f 100644
--- a/include/discounts-overview.php
+++ b/include/discounts-overview.php
@@ -83,7 +83,7 @@ require_once('./login.php');
diff --git a/include/home.php b/include/home.php
index f90d9d3..0ccff58 100644
--- a/include/home.php
+++ b/include/home.php
@@ -152,19 +152,19 @@ require('./header.php');
$offers = BusinessAdmin::getOffers($_pdo, array("`accepted`=1", "`start_date` <= CURDATE()", "`end_date` >= 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();
+ $start = BusinessAdmin::formatDate($offer->start_date, false);
+ $end = BusinessAdmin::formatDate($offer->end_date, false);
+ $since = mktime(0,0,0,date("n"),date("j"),date("Y")) - $offer->start_date;
+ $total = $offer->end_date - $offer->start_date;
$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();
+ $base = str_pad($percentage, 3, '0', STR_PAD_LEFT) . $offer->start_date;
for ($i = 0; isset($list["$base-$i"]); $i++);
$list["$base-$i"] = array(
'start' => $start,
'end' => $end,
- 'id' => $offer->getId(),
+ 'id' => $offer->id,
'contactClientName' => $offer->getContact()->getClient()->name,
'percentage' => $percentage,
'price_excl' => Constants::invoice_valuta . $offer->calculate(offer::SUBTOTAL),
@@ -207,9 +207,9 @@ require('./header.php');
$offers = BusinessAdmin::getOffers($_pdo, array("`accepted`=1", "`end_date` <= CURDATE()", "`invoice_date` IS NULL OR `invoice_date`='1970-01-01' OR `invoice_date`>CURDATE()"));
foreach ($offers as $offer) {
echo "