| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?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."));
- $invoice = $project->getCustomerInvoice();
- $line_items = $invoice->getLineItems();
- $customer = $project->getCustomer();
- $total_ex_vat = $invoice->getTotal();
- $vat = $invoice->getTax();
- $total_inc_vat = $invoice->getTotalIncTax();
- // Filename
- $reference = $invoice->getReference();
- $download_filename = str_replace("/", "_", $reference) . " - " . $project->name . " - Invoice.pdf";
- // Initialise for PDF generation
- $bill_to_address_parts = array_filter([
- $customer->address_1,
- $customer->address_2,
- $customer->city,
- implode(", ", array_filter([$customer->county, $customer->postal_code])),
- ]);
- $bill_from_address_parts = explode("\n", ADDRESS);
- // 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($line_items as $item) {
- ?>
- <tr>
- <td>
- <strong><?=htmlspecialchars($item->getName())?></strong>
- </td>
- <td align="right" valign="top"><?=htmlspecialchars($item->getDisplayQuantity())?></td>
- <td align="right" valign="top"><?=htmlspecialchars($item->getDisplayPrice())?></td>
- <td align="right" valign="top"><?=htmlspecialchars($item->getDisplayTotal())?></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");
|