diff options
-rw-r--r-- | classes/Calculatable.php | 63 | ||||
-rw-r--r-- | classes/Offer.php | 55 | ||||
-rw-r--r-- | include/home.php | 8 | ||||
-rw-r--r-- | include/offers-overview.php | 10 | ||||
-rw-r--r-- | include/offers-view.php | 4 | ||||
-rw-r--r-- | include/pay.php | 135 | ||||
-rw-r--r-- | nav.php | 2 |
7 files changed, 228 insertions, 49 deletions
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 <http://www.gnu.org/licenses/>. */ -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 @@ -138,6 +138,17 @@ class Offer extends Model{ } /** + * 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 * * @see offer::getPayment() This funtion returns an instance of the payment class instead of just the id @@ -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'] .= "<b>{$assignment->title}</b><br/><span class='smaller'>(".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_TOTAL)} incl. VAT)</span><br/><p>{$assignment->getHTMLDescription()}</p>"; - $temp['assignments_header'] .= "<b>{$assignment->title}</b><br/><span class='smaller'>(".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_TOTAL)} incl. VAT)</span><br/>"; + $temp['assignments'] .= "<b>{$assignment->title}</b><br/><span class='smaller'>(".Constants::invoice_valuta."{$assignment->calculate(Calculatable::SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(Calculatable::TOTAL)} incl. VAT)</span><br/><p>{$assignment->getHTMLDescription()}</p>"; + $temp['assignments_header'] .= "<b>{$assignment->title}</b><br/><span class='smaller'>(".Constants::invoice_valuta."{$assignment->calculate(Calculatable::SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(Calculatable::TOTAL)} incl. VAT)</span><br/>"; } $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'); <td class='col-min-width'><span title='{$offer->getContact()->getClient()->name}'>{$offer->getContact()->name}</span></td> <td class='col-max-width'>"; foreach ($offer->getAssignments() as $assignment) { - echo "<b>{$assignment->title}</b><br/><span class='smaller'>(".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_TOTAL)} incl. VAT)</span><br/><p>{$assignment->getHTMLDescription()}</p>"; + echo "<b>{$assignment->title}</b><br/><span class='smaller'>(".Constants::invoice_valuta."{$assignment->calculate(Calculatable::SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(Calculatable::TOTAL)} incl. VAT)</span><br/><p>{$assignment->getHTMLDescription()}</p>"; } foreach ($offer->getDiscounts() as $discount) { - echo "<b>{$discount->title}</b><br/><span class='smaller'>(".Constants::invoice_valuta."{$discount->calculate(CALCULATABLE_SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$discount->calculate(CALCULATABLE_TOTAL)} incl. VAT)</span><br/><p>{$discount->description}</p>"; + echo "<b>{$discount->title}</b><br/><span class='smaller'>(".Constants::invoice_valuta."{$discount->calculate(Calculatable::SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$discount->calculate(Calculatable::TOTAL)} incl. VAT)</span><br/><p>{$discount->description}</p>"; } echo "</td> <td class='col-min-width'> @@ -82,15 +82,15 @@ require_once('./login.php'); <table> <tr> <th style='padding-right:1em;'>Subtotal:</th> - <td>".Constants::invoice_valuta."{$offer->calculate(CALCULATABLE_SUBTOTAL)}</td> + <td>".Constants::invoice_valuta."{$offer->calculate(Calculatable::SUBTOTAL)}</td> </tr> <tr> <th style='padding-right:1em;'>VAT:</th> - <td>".Constants::invoice_valuta."{$offer->calculate(CALCULATABLE_VAT)}</td> + <td>".Constants::invoice_valuta."{$offer->calculate(Calculatable::VAT)}</td> <tr> </tr> <th style='padding-right:1em;'>Total:</th> - <td>".Constants::invoice_valuta."{$offer->calculate(CALCULATABLE_TOTAL)}</td> + <td>".Constants::invoice_valuta."{$offer->calculate(Calculatable::TOTAL)}</td> </tr> </table> </td> 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'] .= "<b>{$assignment->title}</b><br/><span class='smaller'>(".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_TOTAL)} incl. VAT)</span><br/><p>{$assignment->getHTMLDescription()}</p>"; - $temp['assignments_header'] .= "<b>{$assignment->title}</b><br/><span class='smaller'>(".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(CALCULATABLE_TOTAL)} incl. VAT)</span><br/>"; + $temp['assignments'] .= "<b>{$assignment->title}</b><br/><span class='smaller'>(".Constants::invoice_valuta."{$assignment->calculate(Calculatable::SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(Calculatable::TOTAL)} incl. VAT)</span><br/><p>{$assignment->getHTMLDescription()}</p>"; + $temp['assignments_header'] .= "<b>{$assignment->title}</b><br/><span class='smaller'>(".Constants::invoice_valuta."{$assignment->calculate(Calculatable::SUBTOTAL)} excl. VAT, ".Constants::invoice_valuta."{$assignment->calculate(Calculatable::TOTAL)} incl. VAT)</span><br/>"; } $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 @@ +<?php +/** + * 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 <http://www.gnu.org/licenses/>. + */ + +require_once('./index.php'); +require('./header.php'); +?> + +<div class="container"> + <div class="row"> + <div class="col-md-10 col-md-offset-1"> + <div class="panel panel-default payment-panel"> + <div class="panel-heading"> + <h3 class="panel-title">Pay<i class="fa fa-credit-card fa-fw fa-lg pull-right"></i></h3> + </div> + <div class="panel-body"> + <?php + if (!isset($_REQUEST['id']) || !isset($_REQUEST['key'])) { + $notFound = true; + } else { + $offerId = $_REQUEST['id']; + $offerKey = $_REQUEST['key']; + $notFound = false; + try { + $_offer = new Offer($_pdo, $offerId); + } catch (Exception $e) { + $notFound = true; + } + } + if ($notFound || $offerKey != $_offer->key) { + ?> + <div class='form-group alert alert-danger'>The invoice could not be found.</div> + <?php + } elseif (isset($_POST['payment_method_nonce'])) { + $nonce = $_POST['payment_method_nonce']; + $trans = Braintree_Transaction::sale([ + 'amount' => (string) $_offer->calculate(Calculatable::TOTAL), + 'paymentMethodNonce' => $nonce, + 'options' => [ + 'submitForSettlement' => true + ] + ]); + + if (!$trans->success) { + echo '<div class="form-group alert alert-danger">'; + echo '<h4>Your transaction could not be completed:</h4>'; + foreach ($trans->errors->deepAll() as $error) { + echo "{$error->attribute}: {$error->code} {$error->message}<br/>"; + } + echo '<b>Please try again, or <a href="mailto:'.Constants::invoice_email.'">contact us</a>.</b>'; + echo '</div>'; + } else { + try { + $payment = $_offer->createPayment(); + $payment->braintree_id = $trans->transaction->id; + echo '<div class="form-group alert alert-success">Thank you for your payment.</div>'; + } catch (Exception $e) { + echo '<div class="form-group alert alert-warning">Your payment has been received, but could not be stored in our database. Please <a href="mailto:'.Constants::invoice_email.'">contact us</a>.</div>'; + } + } + } else { + $subtotal = Constants::invoice_valuta . $_offer->calculate(Calculatable::SUBTOTAL); + $total = Constants::invoice_valuta . $_offer->calculate(Calculatable::TOTAL); + ?> + <div> + <div class='form-group alert alert-info'>Welcome to the checkout environment. Please review the invoice carefully.</div> + <table class="table table-bordered table-striped"> + <tr style="border-bottom:2px solid #666;"> + <th>Description</th> + <th>Price excl.</th> + <th>VAT</th> + <th>Price incl.</th> + </tr> + <?php + $i = 0; + foreach ($_offer->getItems() as $item) { + $i++; + echo '<tr>'; + echo "<td class='col-max-width'> + <b><a href='#collapse-item-$i' data-toggle='collapse'>{$item->title}</a></b> + <div class='collapse' id='collapse-item-$i'>{$item->getHTMLDescription()}</div> + </td>"; + echo "<td class='col-min-width'>".Constants::invoice_valuta."{$item->calculate(Calculatable::SUBTOTAL)}</td>"; + echo "<td class='col-min-width'>{$item->VAT_percentage}%</td>"; + echo "<td class='col-min-width'>".Constants::invoice_valuta."{$item->calculate(Calculatable::TOTAL)}</td>"; + echo '</tr>'; + } + ?> + <tr style="border-top:2px solid #666;"> + <th colspan="3" class="text-right">Subtotal</th> + <td><?=$subtotal?></td> + </tr> + <tr> + <th colspan="3" class="text-right">Total</th> + <td><?=$total?></td> + </tr> + </table> + </div> + <form id="checkout" method="post" action=""> + <div id="payment-form"></div> + <input type="submit" class="btn btn-success btn-lg pull-right" value="Pay <?=$total?>"/> + </form> + <?php + } + ?> + </div> + </div> + </div> + </div> +</div> + +<script src="https://js.braintreegateway.com/js/braintree-2.27.0.min.js"></script> +<script type="text/javascript"> + var clientToken = "<?=Braintree_ClientToken::generate()?>"; + braintree.setup(clientToken, "dropin", { container: "payment-form" }); +</script> + +<?php +require('./footer.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); |