| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- <?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");
|