|
|
@@ -0,0 +1,290 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+use \Mpdf\Mpdf;
|
|
|
+
|
|
|
+$project = Project::get($id);
|
|
|
+if(!$project) throw new Exception(_("Project not found."));
|
|
|
+if(!$project->dts_finished) throw new Exception(_("An invoice cannot be generated for this project as it has not been marked as finished."));
|
|
|
+if(!Core::$user->canAdmin()) throw new Exception(_("You do not have permission to access this resource."));
|
|
|
+
|
|
|
+$customer = $project->getCustomer();
|
|
|
+$template = $project->getTemplate();
|
|
|
+$invoice_fields = $template->getCustomerInvoiceFields();
|
|
|
+
|
|
|
+// Calculation
|
|
|
+$default_customer_fee = $project->default_customer_fee;
|
|
|
+$tests = $project->getTests();
|
|
|
+
|
|
|
+$invoice_field_totals = [];
|
|
|
+$test_fee_invoice_counts = [];
|
|
|
+
|
|
|
+$test_costs = 0;
|
|
|
+foreach($tests as $test) {
|
|
|
+
|
|
|
+ $values = $test->getValues(true);
|
|
|
+
|
|
|
+ $fee = $test->customer_fee ?? $default_customer_fee;
|
|
|
+ $test_costs += $fee;
|
|
|
+
|
|
|
+ $count = $test_fee_invoice_counts[$fee] ?? 0;
|
|
|
+ ++$count;
|
|
|
+ $test_fee_invoice_counts[$fee] = $count;
|
|
|
+
|
|
|
+ foreach($invoice_fields as $field) {
|
|
|
+ if(!isset($invoice_field_totals[$field->name])) {
|
|
|
+ $invoice_field_totals[$field->name] = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ $invoice_field_totals[$field->name] += (float) ($values[$field->name] ?? 0);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+$total_ex_vat = $test_costs;
|
|
|
+$vat = round($total_ex_vat * 0.20, 2);
|
|
|
+
|
|
|
+$total_inc_vat = $total_ex_vat + $vat;
|
|
|
+
|
|
|
+// Processing
|
|
|
+usort($tests, function($a, $b) {
|
|
|
+ return $a->dts_start <=> $b->dts_start;
|
|
|
+});
|
|
|
+
|
|
|
+Location::load(array_column($tests, "location_id"));
|
|
|
+Template::load(array_column($tests, "template_id"));
|
|
|
+
|
|
|
+// Filename
|
|
|
+$reference =
|
|
|
+ "P" . str_pad($project->id, 3, "0", STR_PAD_LEFT) .
|
|
|
+ "/C" . str_pad($customer->id, 3, "0", STR_PAD_LEFT);
|
|
|
+
|
|
|
+$download_filename = str_replace("/", "_", $reference) . " - " . $project->name . " - Invoice.pdf";
|
|
|
+
|
|
|
+// Initialise for PDF generation
|
|
|
+$bill_to_address_parts = explode("\n", ADDRESS);
|
|
|
+
|
|
|
+$bill_from_address_parts = [
|
|
|
+ "8 Market Street",
|
|
|
+ "Liskeard",
|
|
|
+ "Cornwall",
|
|
|
+ "PL14 3JJ",
|
|
|
+];
|
|
|
+
|
|
|
+// A4 size: 210x297mm
|
|
|
+$mpdf = new Mpdf();
|
|
|
+
|
|
|
+$mpdf->SetSubject("Project Invoice");
|
|
|
+$mpdf->SetCreator(APP_NAME);
|
|
|
+$mpdf->SetAuthor(APP_NAME);
|
|
|
+$mpdf->SetTitle("Invoice ({$reference})");
|
|
|
+
|
|
|
+$mpdf->useSubstitutions = true;
|
|
|
+
|
|
|
+ob_start();
|
|
|
+?>
|
|
|
+<!DOCTYPE html>
|
|
|
+<html>
|
|
|
+ <head>
|
|
|
+ <style>
|
|
|
+ body {
|
|
|
+ font-family: sans-serif;
|
|
|
+ }
|
|
|
+
|
|
|
+ .title {
|
|
|
+ font-size: 3rem;
|
|
|
+ }
|
|
|
+
|
|
|
+ table {
|
|
|
+ border-collapse: collapse;
|
|
|
+ }
|
|
|
+
|
|
|
+ td {
|
|
|
+ padding: 8px;
|
|
|
+ font-size: 0.875rem;
|
|
|
+ }
|
|
|
+
|
|
|
+ .text-center {
|
|
|
+ text-align: center;
|
|
|
+ }
|
|
|
+
|
|
|
+ .text-brand {
|
|
|
+ color: #269926;
|
|
|
+ }
|
|
|
+ </style>
|
|
|
+ </head>
|
|
|
+ <body>
|
|
|
+
|
|
|
+ <div>
|
|
|
+ <div style="float: left; width: 49.5%; font-size: 0.875rem;">
|
|
|
+ <img src="./public/img/logo.png" alt="<?=htmlspecialchars(COMPANY_NAME)?>" /><br />
|
|
|
+ <br />
|
|
|
+ </div>
|
|
|
+ <div style="float: right; width: 49.5%; text-align: right;">
|
|
|
+ <div class="title">
|
|
|
+ INVOICE
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <div>
|
|
|
+ <div style="float: left; width: 49.5%; font-size: 0.875rem;">
|
|
|
+
|
|
|
+ <strong><?=htmlspecialchars(COMPANY_NAME)?></strong><br />
|
|
|
+ <?=nl2br(htmlspecialchars(implode("\n", $bill_from_address_parts)))?><br />
|
|
|
+ Tel: <?=htmlspecialchars(PHONE)?>
|
|
|
+
|
|
|
+ <br />
|
|
|
+ <br />
|
|
|
+
|
|
|
+ <span color="gray">Bill To:</span><br />
|
|
|
+
|
|
|
+ <strong><?=htmlspecialchars($customer->name)?></strong><br />
|
|
|
+ <?=htmlspecialchars($customer->invoice_contact_name)?><br />
|
|
|
+ <?=nl2br(htmlspecialchars(implode("\n", $bill_to_address_parts)))?><br />
|
|
|
+
|
|
|
+ <?php
|
|
|
+ if($customer->phone) {
|
|
|
+ ?>
|
|
|
+ Tel: <?=htmlspecialchars($customer->phone)?><br />
|
|
|
+ <?php
|
|
|
+ }
|
|
|
+ if($customer->email) {
|
|
|
+ ?>
|
|
|
+ Email: <?=htmlspecialchars($customer->email)?><br />
|
|
|
+ <?php
|
|
|
+ }
|
|
|
+ ?>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ </div>
|
|
|
+ <div style="float: right; width: 49.5%; text-align: right;">
|
|
|
+ Ref: <?=$reference?><br />
|
|
|
+ Name: <?=htmlspecialchars($project->name)?><br />
|
|
|
+
|
|
|
+ <br />
|
|
|
+
|
|
|
+ <table width="100%" cellspacing="0">
|
|
|
+ <tr>
|
|
|
+ <td width="50%" align="right" color="gray">Date:</td>
|
|
|
+ <td width="50%" align="right"><?=date("d/m/Y")?></td>
|
|
|
+ </tr>
|
|
|
+ <tr>
|
|
|
+ <td align="right" color="gray">Due Date:</td>
|
|
|
+ <td align="right"><?=date("d/m/Y", time() + (30 * 86400))?></td>
|
|
|
+ </tr>
|
|
|
+ <tr bgcolor="#eee">
|
|
|
+ <td align="right">Balance Due:</td>
|
|
|
+ <td align="right">£<?=number_format($total_inc_vat, 2)?></td>
|
|
|
+ </tr>
|
|
|
+ </table>
|
|
|
+
|
|
|
+ </div>
|
|
|
+ <div style="clear: both;"></div>
|
|
|
+ <br />
|
|
|
+ <table width="100%" cellspacing="0" border="1">
|
|
|
+ <tr bgcolor="#444">
|
|
|
+ <td color="white" width="55%">Item</td>
|
|
|
+ <td color="white">Quantity</td>
|
|
|
+ <td color="white">Rate</td>
|
|
|
+ <td color="white">Amount</td>
|
|
|
+ </tr>
|
|
|
+
|
|
|
+ <?php
|
|
|
+ /*
|
|
|
+ foreach($tests as $test) {
|
|
|
+ $location = $test->getLocation();
|
|
|
+ $location_address = $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]);
|
|
|
+ ?>
|
|
|
+
|
|
|
+ <tr>
|
|
|
+ <td>
|
|
|
+ <strong><?=htmlspecialchars($location->name)?></strong><br />
|
|
|
+ <?=nl2br(htmlspecialchars("{$location_address}\n" . date("d/m/Y H:i", strtotime($test->dts_start . " UTC"))))?>
|
|
|
+ </td>
|
|
|
+ <td align="right" valign="top">1</td>
|
|
|
+ <td align="right" valign="top">£<?=number_format($default_customer_fee, 2)?></td>
|
|
|
+ <td align="right" valign="top">£<?=number_format($default_customer_fee, 2)?></td>
|
|
|
+ </tr>
|
|
|
+ <?php
|
|
|
+ }
|
|
|
+ */
|
|
|
+
|
|
|
+ foreach($test_fee_invoice_counts as $fee => $count) {
|
|
|
+ ?>
|
|
|
+ <tr>
|
|
|
+ <td>
|
|
|
+ <strong><?=htmlspecialchars($template->name)?> Testing</strong>
|
|
|
+ </td>
|
|
|
+ <td align="right" valign="top"><?=number_format($count)?></td>
|
|
|
+ <td align="right" valign="top">£<?=number_format($fee, 2)?></td>
|
|
|
+ <td align="right" valign="top">£<?=number_format($fee * $count, 2)?></td>
|
|
|
+ </tr>
|
|
|
+ <?php
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach($invoice_fields as $field) {
|
|
|
+ $total = $invoice_field_totals[$field->name] ?? 0;
|
|
|
+ if(!$total) continue;
|
|
|
+ ?>
|
|
|
+ <tr>
|
|
|
+ <td>
|
|
|
+ <strong><?=htmlspecialchars($field->label)?></strong>
|
|
|
+ </td>
|
|
|
+ <td align="center" valign="top">-</td>
|
|
|
+ <td align="center" valign="top">-</td>
|
|
|
+ <td align="right" valign="top">£<?=number_format($total, 2)?></td>
|
|
|
+ </tr>
|
|
|
+ <?php
|
|
|
+ }
|
|
|
+
|
|
|
+ ?>
|
|
|
+
|
|
|
+ <tr>
|
|
|
+ <td colspan="3" align="right" valign="top" color="gray">Total</td>
|
|
|
+ <td align="right" valign="top">£<?=number_format($total_ex_vat, 2)?></td>
|
|
|
+ </tr>
|
|
|
+
|
|
|
+ <tr>
|
|
|
+ <td colspan="3" align="right" valign="top" color="gray">VAT @ 20%</td>
|
|
|
+ <td align="right" valign="top">£<?=number_format($vat, 2)?></td>
|
|
|
+ </tr>
|
|
|
+
|
|
|
+ <tr>
|
|
|
+ <td colspan="3" align="right" valign="top" color="gray">Total (inc. VAT)</td>
|
|
|
+ <td align="right" valign="top">£<?=number_format($total_inc_vat, 2)?></td>
|
|
|
+ </tr>
|
|
|
+
|
|
|
+ </table>
|
|
|
+
|
|
|
+ <br />
|
|
|
+
|
|
|
+ <span color="gray">Pay To:</span><br />
|
|
|
+
|
|
|
+ <?=htmlspecialchars(COMPANY_NAME)?><br />
|
|
|
+ <?=htmlspecialchars(INVOICE_BANK_NAME)?><br />
|
|
|
+ Account Number: <?=htmlspecialchars(INVOICE_ACCOUNT_NUMBER)?><br />
|
|
|
+ Sort Code: <?=htmlspecialchars(INVOICE_SORT_CODE)?><br />
|
|
|
+
|
|
|
+ <p class="text-center">Payment is due within 30 days.</p>
|
|
|
+ <p class="text-center">If you have any questions concerning this invoice, contact: <?=htmlspecialchars(INVOICE_CONTACT_EMAIL)?>
|
|
|
+
|
|
|
+ <p class="text-brand text-center">
|
|
|
+ <strong>THANK YOU FOR YOUR BUSINESS!</strong>
|
|
|
+ </p>
|
|
|
+
|
|
|
+ <p class="text-brand text-center">
|
|
|
+ VAT Number: <?=htmlspecialchars(INVOICE_VAT_NUMBER)?>
|
|
|
+ </p>
|
|
|
+
|
|
|
+
|
|
|
+ </div>
|
|
|
+ </body>
|
|
|
+</html>
|
|
|
+<?php
|
|
|
+
|
|
|
+$html = ob_get_clean();
|
|
|
+
|
|
|
+$mpdf->WriteHTML($html);
|
|
|
+
|
|
|
+// Output to browser
|
|
|
+$mpdf->Output($download_filename, "D");
|