From ae7e885619c0a3112765d9e32df2b3dd68a35d6a Mon Sep 17 00:00:00 2001
From: Camil Staps
Date: Wed, 27 Jul 2016 21:36:56 +0200
Subject: Move SUBTOTAL, VAT and TOTAL to Calculatable
---
classes/Assignment.php | 4 ----
classes/Calculatable.php | 6 +++++-
classes/Discount.php | 4 ----
classes/Offer.php | 34 +++++++++++++++-------------------
include/home.php | 8 ++++----
include/offers-overview.php | 10 +++++-----
include/offers-view.php | 4 ++--
nav.php | 2 +-
8 files changed, 32 insertions(+), 40 deletions(-)
diff --git a/classes/Assignment.php b/classes/Assignment.php
index d04568b..27efb4a 100644
--- a/classes/Assignment.php
+++ b/classes/Assignment.php
@@ -31,10 +31,6 @@ class Assignment extends Model {
$table = 'assignment',
$fillable_columns = ['offerId', 'title', 'description', 'hours', 'price_per_hour', 'VAT_percentage'];
- const SUBTOTAL = 1;
- const VAT = 2;
- const TOTAL = 3;
-
/**
* Get the offer that this assignment is linked to
*
diff --git a/classes/Calculatable.php b/classes/Calculatable.php
index 1d51f0f..086adc2 100644
--- a/classes/Calculatable.php
+++ b/classes/Calculatable.php
@@ -27,6 +27,10 @@
* total can be calculated
*/
trait Calculatable {
+ const SUBTOTAL = 1;
+ const VAT = 2;
+ const TOTAL = 3;
+
/**
* Calculate the subtotal
*
@@ -57,7 +61,7 @@ trait Calculatable {
* VAT: the sum of all the VAT from all the assignments
* Total: the sum of subtotal and total
*
- * @param int $what Any of offer::SUBTOTAL, offer::VAT and offer::TOTAL
+ * @param int $what Any of Calculatable::SUBTOTAL, Calculatable::VAT and Calculatable::TOTAL
* @param int $round How many decimals to round the result on
* @param bool $format Whether to format the number nicely (for output) or not (for calculations)
*
diff --git a/classes/Discount.php b/classes/Discount.php
index 3f12348..7216615 100644
--- a/classes/Discount.php
+++ b/classes/Discount.php
@@ -31,10 +31,6 @@ class Discount extends Model {
$table = 'discount',
$fillable_columns = ['offerId', 'title', 'description', 'value', 'VAT_percentage'];
- const SUBTOTAL = 1;
- const VAT = 2;
- const TOTAL = 3;
-
/**
* Get the offer that this discount is linked to
*
diff --git a/classes/Offer.php b/classes/Offer.php
index 200fcc1..0ef2881 100644
--- a/classes/Offer.php
+++ b/classes/Offer.php
@@ -29,10 +29,6 @@ class Offer extends Model{
$table = 'offer',
$fillable_columns = ['contactId', 'start_date', 'end_date', 'invoice_date', 'accepted', 'invoice_fileId'];
- const SUBTOTAL = 1;
- const VAT = 2;
- const TOTAL = 3;
-
protected function accessor($key, $value) {
switch ($key) {
case 'start_date':
@@ -215,7 +211,7 @@ class Offer extends Model{
*
* Total: the sum of subtotal and total
*
- * @param int $what Any of offer::SUBTOTAL, offer::VAT and offer::TOTAL
+ * @param int $what Any of Calculatable::SUBTOTAL, Calculatable::VAT and Calculatable::TOTAL
* @param int $round How many decimals to round the result on
* @param bool $format Whether to format the number nicely (for output) or not (for calculations)
*
@@ -223,28 +219,28 @@ class Offer extends Model{
*
* @return float|bool The calculated value rounded to $round decimals, or false on incorrect input
*/
- public function calculate($what = self::TOTAL, $round = 2, $format = true) {
+ public function calculate($what = Calculatable::TOTAL, $round = 2, $format = true) {
$return = 0;
switch ($what) {
- case self::SUBTOTAL:
+ case Calculatable::SUBTOTAL:
foreach ($this->getAssignments() as $assignment) {
- $return += $assignment->calculate(assignment::SUBTOTAL, $round + 1, false);
+ $return += $assignment->calculate(Calculatable::SUBTOTAL, $round + 1, false);
}
foreach ($this->getDiscounts() as $discount) {
- $return += $discount->calculate(discount::SUBTOTAL, $round + 1, false);
+ $return += $discount->calculate(Calculatable::SUBTOTAL, $round + 1, false);
}
break;
- case self::VAT:
+ case Calculatable::VAT:
$assignments = $this->getAssignments();
foreach ($assignments as $assignment) {
- $return += $assignment->calculate(assignment::VAT, $round + 1, false);
+ $return += $assignment->calculate(Calculatable::VAT, $round + 1, false);
}
foreach ($this->getDiscounts() as $discount) {
- $return += $discount->calculate(discount::VAT, $round + 1, false);
+ $return += $discount->calculate(Calculatable::VAT, $round + 1, false);
}
break;
- case self::TOTAL:
- $return = $this->calculate(self::SUBTOTAL, $round + 1, false) + $this->calculate(self::VAT, $round + 1, false);
+ case Calculatable::TOTAL:
+ $return = $this->calculate(Calculatable::SUBTOTAL, $round + 1, false) + $this->calculate(Calculatable::VAT, $round + 1, false);
break;
default:
return false;
@@ -376,9 +372,9 @@ class Offer extends Model{
foreach ($this->getDiscounts() as $discount)
$list[] = array(
$discount->title,
- $discount->calculate(discount::SUBTOTAL),
+ $discount->calculate(Calculatable::SUBTOTAL),
$discount->VAT_percentage . "%",
- $discount->calculate(discount::TOTAL)
+ $discount->calculate(Calculatable::TOTAL)
);
$pdf = new Correspondence();
@@ -466,7 +462,7 @@ class Offer extends Model{
$pdf->SetFont('','B');
$pdf->Cell($width[1] + $width[2],7,$pdf->_('amount'));
$pdf->SetFont('','');
- $pdf->Cell($width[3],7,correspondence::valuta() . $this->calculate(self::SUBTOTAL),'',0,'R');
+ $pdf->Cell($width[3],7,correspondence::valuta() . $this->calculate(Calculatable::SUBTOTAL),'',0,'R');
$pdf->Ln();
foreach ($btw as $p => $m) {
@@ -483,7 +479,7 @@ class Offer extends Model{
$pdf->SetFont('','B');
$pdf->Cell($width[1] + $width[2],7,$pdf->_('total'));
$pdf->SetFont('','');
- $pdf->Cell($width[3],7,correspondence::valuta() . $this->calculate(self::TOTAL),'T',0,'R');
+ $pdf->Cell($width[3],7,correspondence::valuta() . $this->calculate(Calculatable::TOTAL),'T',0,'R');
$pdf->Ln();
// Footer
@@ -521,7 +517,7 @@ class Offer extends Model{
$pdf->Cell(17.5,5);
$pdf->Cell(40,5,$invoice_nr);
$pdf->Cell(17.5,5);
- $pdf->Cell(40,5,correspondence::valuta() . $this->calculate(self::TOTAL));
+ $pdf->Cell(40,5,correspondence::valuta() . $this->calculate(Calculatable::TOTAL));
$pdf->SetY($oldY + 14);
diff --git a/include/home.php b/include/home.php
index 0ccff58..43cfeb1 100644
--- a/include/home.php
+++ b/include/home.php
@@ -167,8 +167,8 @@ require('./header.php');
'id' => $offer->id,
'contactClientName' => $offer->getContact()->getClient()->name,
'percentage' => $percentage,
- 'price_excl' => Constants::invoice_valuta . $offer->calculate(offer::SUBTOTAL),
- 'price_incl' => Constants::invoice_valuta . $offer->calculate(offer::TOTAL)
+ 'price_excl' => Constants::invoice_valuta . $offer->calculate(Calculatable::SUBTOTAL),
+ 'price_incl' => Constants::invoice_valuta . $offer->calculate(Calculatable::TOTAL)
);
}
krsort($list, SORT_STRING);
@@ -285,8 +285,8 @@ require('./header.php');
'assignments_header' => ''
);
foreach ($offer->getAssignments() as $assignment) {
- $temp['assignments'] .= "{$assignment->title}
(".Constants::invoice_valuta."{$assignment->calculate(assignment::SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(assignment::TOTAL)} incl. VAT)
{$assignment->getHTMLDescription()}
"; - $temp['assignments_header'] .= "{$assignment->title}{$assignment->getHTMLDescription()}
"; + $temp['assignments_header'] .= "{$assignment->title}{$assignment->getHTMLDescription()}
"; + echo "{$assignment->title}{$assignment->getHTMLDescription()}
"; } foreach ($offer->getDiscounts() as $discount) { - echo "{$discount->title}{$discount->description}
"; + echo "{$discount->title}{$discount->description}
"; } echo "Subtotal: | -".Constants::invoice_valuta."{$offer->calculate(offer::SUBTOTAL)} | +".Constants::invoice_valuta."{$offer->calculate(Calculatable::SUBTOTAL)} |
---|---|---|
VAT: | -".Constants::invoice_valuta."{$offer->calculate(offer::VAT)} | +".Constants::invoice_valuta."{$offer->calculate(Calculatable::VAT)} |
Total: | -".Constants::invoice_valuta."{$offer->calculate(offer::TOTAL)} | +".Constants::invoice_valuta."{$offer->calculate(Calculatable::TOTAL)} |
{$assignment->getHTMLDescription()}
"; - $temp['assignments_header'] .= "{$assignment->title}{$assignment->getHTMLDescription()}
"; + $temp['assignments_header'] .= "{$assignment->title}