From a9c5778232079a09000c519a414563a1e04e112d Mon Sep 17 00:00:00 2001
From: Camil Staps
Date: Thu, 28 Jul 2016 09:05:20 +0200
Subject: Split Calculatable in trait and interface
---
classes/Calculatable.php | 63 ++++++++++++++++-----
classes/Offer.php | 55 ++++++++++--------
include/home.php | 8 +--
include/offers-overview.php | 10 ++--
include/offers-view.php | 4 +-
include/pay.php | 135 ++++++++++++++++++++++++++++++++++++++++++++
nav.php | 2 +-
7 files changed, 228 insertions(+), 49 deletions(-)
create mode 100644 include/pay.php
diff --git a/classes/Calculatable.php b/classes/Calculatable.php
index 5d60704..76f89eb 100644
--- a/classes/Calculatable.php
+++ b/classes/Calculatable.php
@@ -22,35 +22,68 @@
* along with this program. If not, see .
*/
-define('CALCULATABLE_SUBTOTAL', 1);
-define('CALCULATABLE_VAT', 2);
-define('CALCULATABLE_TOTAL', 3);
-
/**
- * The calculatable trait, to be used by something of which subtotal, VAT and
- * total can be calculated
+ * The calculatable interface, to be used by something of which subtotal, VAT
+ * and total can be calculated
*/
-trait Calculatable {
+interface Calculatable {
+ const SUBTOTAL = 1;
+ const VAT = 2;
+ const TOTAL = 3;
+
/**
* Calculate the subtotal
*
* @return float The subtotal
*/
- abstract protected function calculateSubtotal();
+ function calculateSubtotal();
/**
* Calculate the VAT
*
* @return float The VAT
*/
- abstract protected function calculateVAT();
+ function calculateVAT();
+
+ /**
+ * Calculate the total
+ *
+ * @return float The total
+ */
+ function calculateTotal();
+
+ /**
+ * Calculate and possibly format
+ *
+ * Subtotal: the sum of the prices of the assignments excl. VAT
+ * VAT: the sum of all the VAT from all the assignments
+ * Total: the sum of subtotal and 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)
+ *
+ * @throws PDOException If something went wrong with the database
+ *
+ * @return float|bool The calculated value rounded to $round decimals, or false on incorrect input
+ */
+ function calculate($what = Calculatable::TOTAL, $round = 2, $format = true);
+}
+
+/**
+ * The calculatable trait, to be used by something of which subtotal, VAT and
+ * total can be calculated
+ */
+trait StandardCalculatable {
+ abstract public function calculateSubtotal();
+ abstract public function calculateVAT();
/**
* Calculate the total
*
* @return float The total
*/
- protected function calculateTotal() {
+ public function calculateTotal() {
return $this->calculateSubtotal() + $this->calculateVAT();
}
@@ -61,7 +94,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 CALCULATABLE_SUBTOTAL, CALCULATABLE_VAT and CALCULATABLE_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)
*
@@ -69,16 +102,16 @@ trait Calculatable {
*
* @return float|bool The calculated value rounded to $round decimals, or false on incorrect input
*/
- public function calculate($what = CALCULATABLE_TOTAL, $round = 2, $format = true) {
+ public function calculate($what = Calculatable::TOTAL, $round = 2, $format = true) {
$return = 0;
switch ($what) {
- case CALCULATABLE_SUBTOTAL:
+ case Calculatable::SUBTOTAL:
$return = $this->calculateSubtotal();
break;
- case CALCULATABLE_VAT:
+ case Calculatable::VAT:
$return = $this->calculateVAT();
break;
- case CALCULATABLE_TOTAL:
+ case Calculatable::TOTAL:
$return = $this->calculateTotal();
break;
default:
diff --git a/classes/Offer.php b/classes/Offer.php
index e24154f..815f626 100644
--- a/classes/Offer.php
+++ b/classes/Offer.php
@@ -137,6 +137,17 @@ class Offer extends Model{
return $discounts;
}
+ /**
+ * Get all assignments and discounts for this offer
+ *
+ * @throws PDOException If something went wrong with the database
+ *
+ * @return mixed[] An array of assignments and discounts
+ */
+ public function getItems() {
+ return array_merge($this->getAssignments(), $this->getDiscounts());
+ }
+
/**
* Get the payment id for this offer
*
@@ -211,7 +222,7 @@ class Offer extends Model{
*
* Total: the sum of subtotal and total
*
- * @param int $what Any of CALCULATABLE_SUBTOTAL, CALCULATABLE_VAT and CALCULATABLE_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)
*
@@ -219,28 +230,28 @@ class Offer extends Model{
*
* @return float|bool The calculated value rounded to $round decimals, or false on incorrect input
*/
- public function calculate($what = CALCULATABLE_TOTAL, $round = 2, $format = true) {
+ public function calculate($what = Calculatable::TOTAL, $round = 2, $format = true) {
$return = 0;
switch ($what) {
- case CALCULATABLE_SUBTOTAL:
+ case Calculatable::SUBTOTAL:
foreach ($this->getAssignments() as $assignment) {
- $return += $assignment->calculate(CALCULATABLE_SUBTOTAL, $round + 1, false);
+ $return += $assignment->calculate(Calculatable::SUBTOTAL, $round + 1, false);
}
foreach ($this->getDiscounts() as $discount) {
- $return += $discount->calculate(CALCULATABLE_SUBTOTAL, $round + 1, false);
+ $return += $discount->calculate(Calculatable::SUBTOTAL, $round + 1, false);
}
break;
- case CALCULATABLE_VAT:
+ case Calculatable::VAT:
$assignments = $this->getAssignments();
foreach ($assignments as $assignment) {
- $return += $assignment->calculate(CALCULATABLE_VAT, $round + 1, false);
+ $return += $assignment->calculate(Calculatable::VAT, $round + 1, false);
}
foreach ($this->getDiscounts() as $discount) {
- $return += $discount->calculate(CALCULATABLE_VAT, $round + 1, false);
+ $return += $discount->calculate(Calculatable::VAT, $round + 1, false);
}
break;
- case CALCULATABLE_TOTAL:
- $return = $this->calculate(CALCULATABLE_SUBTOTAL, $round + 1, false) + $this->calculate(CALCULATABLE_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;
@@ -372,20 +383,20 @@ class Offer extends Model{
foreach ($this->getDiscounts() as $discount)
$list[] = array(
$discount->title,
- $discount->calculate(CALCULATABLE_SUBTOTAL),
+ $discount->calculate(Calculatable::SUBTOTAL),
$discount->VAT_percentage . "%",
- $discount->calculate(CALCULATABLE_TOTAL)
+ $discount->calculate(Calculatable::TOTAL)
);
$pdf = new Correspondence();
$pdf->SetContact($this->getContact());
$pdf->SetTitle($pdf->_('invoice') . ' ' . $invoice_nr);
$pdf->AddPage();
- $pdf->correspondenceHeader();
+ $pdf->CorrespondenceHeader();
$pdf->SetY(100);
$pdf->SetFont('','B',14);
- $pdf->SetTextColor(correspondence::HEAD_RED, correspondence::HEAD_GREEN, correspondence::HEAD_BLUE);
+ $pdf->SetTextColor(Correspondence::HEAD_RED, Correspondence::HEAD_GREEN, Correspondence::HEAD_BLUE);
$pdf->Cell(60,6, $pdf->_('invoice'),'B');
$pdf->SetTextColor(0);
$pdf->Ln();
@@ -423,7 +434,7 @@ class Offer extends Model{
// Table
$pdf->SetFont('','B',11);
- $pdf->SetTextColor(correspondence::HEAD_RED, correspondence::HEAD_GREEN, correspondence::HEAD_BLUE);
+ $pdf->SetTextColor(Correspondence::HEAD_RED, Correspondence::HEAD_GREEN, Correspondence::HEAD_BLUE);
$pdf->Cell($width[0],7,$pdf->_('description'),'B');
$pdf->Cell($width[1],7,$pdf->_('price-excl'),'B',0,'R');
$pdf->Cell($width[2],7,$pdf->_('vat'),'B',0,'R');
@@ -438,9 +449,9 @@ class Offer extends Model{
$pdf->MultiCell($width[0],6,iconv('utf-8', 'iso-8859-1', $row[0]),0,'L');
$newy = $pdf->getY();
$pdf->SetXY($x + $width[0], $y);
- $pdf->Cell($width[1],6,correspondence::valuta().number_format($row[1],2),'',0,'R');
+ $pdf->Cell($width[1],6,Correspondence::valuta().number_format($row[1],2),'',0,'R');
$pdf->Cell($width[2],6,round($row[2],0) . '%','',0,'R');
- $pdf->Cell($width[3],6,correspondence::valuta().number_format($row[3],2),'',0,'R');
+ $pdf->Cell($width[3],6,Correspondence::valuta().number_format($row[3],2),'',0,'R');
$pdf->Ln();
$pdf->SetY($newy);
$pdf->addPageIfOnEnd();
@@ -462,13 +473,13 @@ 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(CALCULATABLE_SUBTOTAL),'',0,'R');
+ $pdf->Cell($width[3],7,Correspondence::valuta() . $this->calculate(Calculatable::SUBTOTAL),'',0,'R');
$pdf->Ln();
foreach ($btw as $p => $m) {
$pdf->Cell($width[0],7);
$pdf->Cell($width[1] + $width[2],7,$pdf->_('vat') . ' '.round($p,0).'%');
- $pdf->Cell($width[3],7,correspondence::valuta() . number_format($m,2),'',0,'R');
+ $pdf->Cell($width[3],7,Correspondence::valuta() . number_format($m,2),'',0,'R');
$pdf->Ln();
}
@@ -479,7 +490,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(CALCULATABLE_TOTAL),'T',0,'R');
+ $pdf->Cell($width[3],7,Correspondence::valuta() . $this->calculate(Calculatable::TOTAL),'T',0,'R');
$pdf->Ln();
// Footer
@@ -497,7 +508,7 @@ class Offer extends Model{
$pdf->Cell(45,20,'',1);
$pdf->SetFont('','B',10);
- $pdf->SetTextColor(correspondence::HEAD_RED, correspondence::HEAD_GREEN, correspondence::HEAD_BLUE);
+ $pdf->SetTextColor(Correspondence::HEAD_RED, Correspondence::HEAD_GREEN, Correspondence::HEAD_BLUE);
$pdf->SetY($oldY + 3);
$pdf->Cell(5,5);
$pdf->Cell(40,5,$pdf->_('iban'));
@@ -517,7 +528,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(CALCULATABLE_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 3903ab2..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(CALCULATABLE_SUBTOTAL),
- 'price_incl' => Constants::invoice_valuta . $offer->calculate(CALCULATABLE_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(CALCULATABLE_SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_TOTAL)} incl. VAT)
{$assignment->getHTMLDescription()}
";
- $temp['assignments_header'] .= "{$assignment->title}
(".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_TOTAL)} incl. VAT)
";
+ $temp['assignments'] .= "{$assignment->title}
(".Constants::invoice_valuta."{$assignment->calculate(Calculatable::SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(Calculatable::TOTAL)} incl. VAT)
{$assignment->getHTMLDescription()}
";
+ $temp['assignments_header'] .= "{$assignment->title}
(".Constants::invoice_valuta."{$assignment->calculate(Calculatable::SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(Calculatable::TOTAL)} incl. VAT)
";
}
$list[] = array_merge($temp, array('type' => 'start', 'time' => $offer->start_date, 'description' => 'Offer started'));
$sort_list[] = $offer->start_date . $offer->id . 0;
diff --git a/include/offers-overview.php b/include/offers-overview.php
index 6c5135a..1118793 100644
--- a/include/offers-overview.php
+++ b/include/offers-overview.php
@@ -46,10 +46,10 @@ require_once('./login.php');
{$offer->getContact()->name} |
";
foreach ($offer->getAssignments() as $assignment) {
- echo "{$assignment->title} (".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_TOTAL)} incl. VAT)
{$assignment->getHTMLDescription()} ";
+ echo "{$assignment->title} (".Constants::invoice_valuta."{$assignment->calculate(Calculatable::SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(Calculatable::TOTAL)} incl. VAT)
{$assignment->getHTMLDescription()} ";
}
foreach ($offer->getDiscounts() as $discount) {
- echo "{$discount->title} (".Constants::invoice_valuta."{$discount->calculate(CALCULATABLE_SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$discount->calculate(CALCULATABLE_TOTAL)} incl. VAT)
{$discount->description} ";
+ echo "{$discount->title} (".Constants::invoice_valuta."{$discount->calculate(Calculatable::SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$discount->calculate(Calculatable::TOTAL)} incl. VAT)
{$discount->description} ";
}
echo " |
@@ -82,15 +82,15 @@ require_once('./login.php');
Subtotal: |
- ".Constants::invoice_valuta."{$offer->calculate(CALCULATABLE_SUBTOTAL)} |
+ ".Constants::invoice_valuta."{$offer->calculate(Calculatable::SUBTOTAL)} |
VAT: |
- ".Constants::invoice_valuta."{$offer->calculate(CALCULATABLE_VAT)} |
+ ".Constants::invoice_valuta."{$offer->calculate(Calculatable::VAT)} |
Total: |
- ".Constants::invoice_valuta."{$offer->calculate(CALCULATABLE_TOTAL)} |
+ ".Constants::invoice_valuta."{$offer->calculate(Calculatable::TOTAL)} |
|
diff --git a/include/offers-view.php b/include/offers-view.php
index 2df495a..802f42f 100644
--- a/include/offers-view.php
+++ b/include/offers-view.php
@@ -40,8 +40,8 @@ $_offer = new Offer($_pdo, $_id);
'assignments_header' => ''
);
foreach ($_offer->getAssignments() as $assignment) {
- $temp['assignments'] .= "{$assignment->title}
(".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_TOTAL)} incl. VAT)
{$assignment->getHTMLDescription()}
";
- $temp['assignments_header'] .= "{$assignment->title}
(".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_TOTAL)} incl. VAT)
";
+ $temp['assignments'] .= "{$assignment->title}
(".Constants::invoice_valuta."{$assignment->calculate(Calculatable::SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(Calculatable::TOTAL)} incl. VAT)
{$assignment->getHTMLDescription()}
";
+ $temp['assignments_header'] .= "{$assignment->title}
(".Constants::invoice_valuta."{$assignment->calculate(Calculatable::SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(Calculatable::TOTAL)} incl. VAT)
";
}
$list[] = array_merge($temp, array('type' => 'start', 'time' => $_offer->start_date, 'description' => 'Offer started'));
$sort_list[] = $_offer->start_date . $_offer->id . 0;
diff --git a/include/pay.php b/include/pay.php
new file mode 100644
index 0000000..596251c
--- /dev/null
+++ b/include/pay.php
@@ -0,0 +1,135 @@
+.
+ */
+
+require_once('./index.php');
+require('./header.php');
+?>
+
+
+
+
+
+
+
Pay
+
+
+ key) {
+ ?>
+
The invoice could not be found.
+ (string) $_offer->calculate(Calculatable::TOTAL),
+ 'paymentMethodNonce' => $nonce,
+ 'options' => [
+ 'submitForSettlement' => true
+ ]
+ ]);
+
+ if (!$trans->success) {
+ echo '
';
+ } else {
+ try {
+ $payment = $_offer->createPayment();
+ $payment->braintree_id = $trans->transaction->id;
+ echo '
Thank you for your payment.
';
+ } catch (Exception $e) {
+ echo '
';
+ }
+ }
+ } else {
+ $subtotal = Constants::invoice_valuta . $_offer->calculate(Calculatable::SUBTOTAL);
+ $total = Constants::invoice_valuta . $_offer->calculate(Calculatable::TOTAL);
+ ?>
+
+
Welcome to the checkout environment. Please review the invoice carefully.
+
+
+ Description |
+ Price excl. |
+ VAT |
+ Price incl. |
+
+ getItems() as $item) {
+ $i++;
+ echo '';
+ echo "
+ {$item->title}
+ {$item->getHTMLDescription()}
+ | ";
+ echo "".Constants::invoice_valuta."{$item->calculate(Calculatable::SUBTOTAL)} | ";
+ echo "{$item->VAT_percentage}% | ";
+ echo "".Constants::invoice_valuta."{$item->calculate(Calculatable::TOTAL)} | ";
+ echo '
';
+ }
+ ?>
+
+ Subtotal |
+ =$subtotal?> |
+
+
+ Total |
+ =$total?> |
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/nav.php b/nav.php
index 1cc43c1..22875cb 100644
--- a/nav.php
+++ b/nav.php
@@ -59,7 +59,7 @@
'id' => $offer->id,
'contactClientName' => $offer->getContact()->getClient()->name,
'percentage' => $percentage,
- 'price' => Constants::invoice_valuta . $offer->calculate(CALCULATABLE_SUBTOTAL)
+ 'price' => Constants::invoice_valuta . $offer->calculate(Calculatable::SUBTOTAL)
);
}
krsort($list, SORT_STRING);
--
cgit v1.2.3