1
0

project_invoice.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var int $id
  4. */
  5. use \Mpdf\Mpdf;
  6. $project = Project::get($id);
  7. if(!$project) throw new Exception(__("Project not found."));
  8. if(!$project->dts_finished) throw new Exception(__("An invoice cannot be generated for this project as it has not been marked as finished."));
  9. if(!Core::$user->canAdmin()) throw new Exception(__("You do not have permission to access this resource."));
  10. $invoice = $project->getCustomerInvoice();
  11. $line_items = $invoice->getLineItems();
  12. $customer = $project->getCustomer();
  13. $total_ex_vat = $invoice->getTotal();
  14. $vat = $invoice->getTax();
  15. $total_inc_vat = $invoice->getTotalIncTax();
  16. // Filename
  17. $reference = $invoice->getReference();
  18. $download_filename = str_replace("/", "_", $reference) . " - " . $project->name . " - Invoice.pdf";
  19. // Initialise for PDF generation
  20. $bill_to_address_parts = array_filter([
  21. $customer->address_1,
  22. $customer->address_2,
  23. $customer->city,
  24. implode(", ", array_filter([$customer->county, $customer->postal_code])),
  25. ]);
  26. $bill_from_address_parts = explode("\n", ADDRESS);
  27. // A4 size: 210x297mm
  28. $mpdf = new Mpdf();
  29. $mpdf->SetSubject("Project Invoice");
  30. $mpdf->SetCreator(APP_NAME);
  31. $mpdf->SetAuthor(APP_NAME);
  32. $mpdf->SetTitle("Invoice ({$reference})");
  33. $mpdf->useSubstitutions = true;
  34. ob_start();
  35. ?>
  36. <!DOCTYPE html>
  37. <html>
  38. <head>
  39. <style>
  40. body {
  41. font-family: sans-serif;
  42. }
  43. .title {
  44. font-size: 3rem;
  45. }
  46. table {
  47. border-collapse: collapse;
  48. }
  49. td {
  50. padding: 8px;
  51. font-size: 0.875rem;
  52. }
  53. .text-center {
  54. text-align: center;
  55. }
  56. .text-brand {
  57. color: #269926;
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <div>
  63. <div style="float: left; width: 49.5%; font-size: 0.875rem;">
  64. <img src="./public/img/logo.png" alt="<?=htmlspecialchars(COMPANY_NAME)?>" /><br />
  65. <br />
  66. </div>
  67. <div style="float: right; width: 49.5%; text-align: right;">
  68. <div class="title">
  69. INVOICE
  70. </div>
  71. </div>
  72. </div>
  73. <div>
  74. <div style="float: left; width: 49.5%; font-size: 0.875rem;">
  75. <strong><?=htmlspecialchars(COMPANY_NAME)?></strong><br />
  76. <?=nl2br(htmlspecialchars(implode("\n", $bill_from_address_parts)))?><br />
  77. Tel: <?=htmlspecialchars(PHONE)?>
  78. <br />
  79. <br />
  80. <span color="gray">Bill To:</span><br />
  81. <strong><?=htmlspecialchars($customer->name)?></strong><br />
  82. <?=htmlspecialchars($customer->invoice_contact_name)?><br />
  83. <?=nl2br(htmlspecialchars(implode("\n", $bill_to_address_parts)))?><br />
  84. <?php
  85. if($customer->phone) {
  86. ?>
  87. Tel: <?=htmlspecialchars($customer->phone)?><br />
  88. <?php
  89. }
  90. if($customer->email) {
  91. ?>
  92. Email: <?=htmlspecialchars($customer->email)?><br />
  93. <?php
  94. }
  95. ?>
  96. </div>
  97. <div style="float: right; width: 49.5%; text-align: right;">
  98. Ref: <?=$reference?><br />
  99. Name: <?=htmlspecialchars($project->name)?><br />
  100. <br />
  101. <table width="100%" cellspacing="0">
  102. <tr>
  103. <td width="50%" align="right" color="gray">Date:</td>
  104. <td width="50%" align="right"><?=date("d/m/Y")?></td>
  105. </tr>
  106. <tr>
  107. <td align="right" color="gray">Due Date:</td>
  108. <td align="right"><?=date("d/m/Y", time() + (30 * 86400))?></td>
  109. </tr>
  110. <tr bgcolor="#eee">
  111. <td align="right">Balance Due:</td>
  112. <td align="right">£<?=number_format($total_inc_vat, 2)?></td>
  113. </tr>
  114. </table>
  115. </div>
  116. <div style="clear: both;"></div>
  117. <br />
  118. <table width="100%" cellspacing="0" border="1">
  119. <tr bgcolor="#444">
  120. <td color="white" width="55%">Item</td>
  121. <td color="white">Quantity</td>
  122. <td color="white">Rate</td>
  123. <td color="white">Amount</td>
  124. </tr>
  125. <?php
  126. foreach($line_items as $item) {
  127. ?>
  128. <tr>
  129. <td>
  130. <strong><?=htmlspecialchars($item->getName())?></strong>
  131. </td>
  132. <td align="right" valign="top"><?=htmlspecialchars($item->getDisplayQuantity())?></td>
  133. <td align="right" valign="top"><?=htmlspecialchars($item->getDisplayPrice())?></td>
  134. <td align="right" valign="top"><?=htmlspecialchars($item->getDisplayTotal())?></td>
  135. </tr>
  136. <?php
  137. }
  138. ?>
  139. <tr>
  140. <td colspan="3" align="right" valign="top" color="gray">Total</td>
  141. <td align="right" valign="top">£<?=number_format($total_ex_vat, 2)?></td>
  142. </tr>
  143. <tr>
  144. <td colspan="3" align="right" valign="top" color="gray">VAT @ 20%</td>
  145. <td align="right" valign="top">£<?=number_format($vat, 2)?></td>
  146. </tr>
  147. <tr>
  148. <td colspan="3" align="right" valign="top" color="gray">Total (inc. VAT)</td>
  149. <td align="right" valign="top">£<?=number_format($total_inc_vat, 2)?></td>
  150. </tr>
  151. </table>
  152. <br />
  153. <span color="gray">Pay To:</span><br />
  154. <?=htmlspecialchars(COMPANY_NAME)?><br />
  155. <?=htmlspecialchars(INVOICE_BANK_NAME)?><br />
  156. Account Number: <?=htmlspecialchars(INVOICE_ACCOUNT_NUMBER)?><br />
  157. Sort Code: <?=htmlspecialchars(INVOICE_SORT_CODE)?><br />
  158. <p class="text-center">Payment is due within 30 days.</p>
  159. <p class="text-center">If you have any questions concerning this invoice, contact: <?=htmlspecialchars(INVOICE_CONTACT_EMAIL)?>
  160. <p class="text-brand text-center">
  161. <strong>THANK YOU FOR YOUR BUSINESS!</strong>
  162. </p>
  163. <p class="text-brand text-center">
  164. VAT Number: <?=htmlspecialchars(INVOICE_VAT_NUMBER)?>
  165. </p>
  166. </div>
  167. </body>
  168. </html>
  169. <?php
  170. $html = ob_get_clean();
  171. $mpdf->WriteHTML($html);
  172. // Output to browser
  173. $mpdf->Output($download_filename, "D");