diff options
author | Camil Staps | 2016-07-27 21:29:57 +0200 |
---|---|---|
committer | Camil Staps | 2016-07-27 21:29:57 +0200 |
commit | 2acc7787e73c966c3fc1794d77dd758a3f56b566 (patch) | |
tree | 1def501ec9502dee79f0f54a470434a3b322f6a2 /classes | |
parent | Contact: use Model (diff) |
Offer: use Model
Diffstat (limited to 'classes')
-rw-r--r-- | classes/Offer.php | 272 |
1 files changed, 33 insertions, 239 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() { @@ -215,93 +178,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 * * @return int|null The date as a UNIX timestamp, or null if it wasn't received yet @@ -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); @@ -370,30 +207,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 * * Subtotal: the sum of the prices of the assignments excl. VAT @@ -444,25 +257,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 * * @param string $title The title for this assignment @@ -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); } |