project_invoice.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. use \Mpdf\Mpdf;
  3. $project = Project::get($id);
  4. if(!$project) throw new Exception(__("Project not found."));
  5. if(!$project->dts_finished) throw new Exception(__("An invoice cannot be generated for this project as it has not been marked as finished."));
  6. if(!Core::$user->canAdmin()) throw new Exception(__("You do not have permission to access this resource."));
  7. $customer = $project->getCustomer();
  8. $template = $project->getTemplate();
  9. $invoice_fields = $template->getCustomerInvoiceFields();
  10. // Calculation
  11. $default_customer_fee = $project->default_customer_fee;
  12. $tests = $project->getTests();
  13. $invoice_field_totals = [];
  14. $test_fee_invoice_counts = [];
  15. $test_costs = 0;
  16. foreach($tests as $test) {
  17. $values = $test->getValues(true);
  18. $fee = $test->customer_fee ?? $default_customer_fee;
  19. $test_costs += $fee;
  20. $count = $test_fee_invoice_counts[$fee] ?? 0;
  21. ++$count;
  22. $test_fee_invoice_counts[$fee] = $count;
  23. foreach($invoice_fields as $field) {
  24. if(!isset($invoice_field_totals[$field->name])) {
  25. $invoice_field_totals[$field->name] = 0;
  26. }
  27. $invoice_field_totals[$field->name] += (float) ($values[$field->name] ?? 0);
  28. }
  29. }
  30. $total_ex_vat = $test_costs + array_sum($invoice_field_totals);
  31. $vat = round($total_ex_vat * 0.20, 2);
  32. $total_inc_vat = $total_ex_vat + $vat;
  33. // Processing
  34. usort($tests, function($a, $b) {
  35. return $a->dts_start <=> $b->dts_start;
  36. });
  37. Location::load(array_column($tests, "location_id"));
  38. Template::load(array_column($tests, "template_id"));
  39. // Filename
  40. $reference =
  41. "P" . str_pad($project->id, 3, "0", STR_PAD_LEFT) .
  42. "/C" . str_pad($customer->id, 3, "0", STR_PAD_LEFT);
  43. $download_filename = str_replace("/", "_", $reference) . " - " . $project->name . " - Invoice.pdf";
  44. // Initialise for PDF generation
  45. $bill_to_address_parts = array_filter([
  46. $customer->address_1,
  47. $customer->address_2,
  48. $customer->city,
  49. implode(", ", array_filter([$customer->county, $customer->postal_code])),
  50. ]);
  51. $bill_from_address_parts = explode("\n", ADDRESS);
  52. // A4 size: 210x297mm
  53. $mpdf = new Mpdf();
  54. $mpdf->SetSubject("Project Invoice");
  55. $mpdf->SetCreator(APP_NAME);
  56. $mpdf->SetAuthor(APP_NAME);
  57. $mpdf->SetTitle("Invoice ({$reference})");
  58. $mpdf->useSubstitutions = true;
  59. ob_start();
  60. ?>
  61. <!DOCTYPE html>
  62. <html>
  63. <head>
  64. <style>
  65. body {
  66. font-family: sans-serif;
  67. }
  68. .title {
  69. font-size: 3rem;
  70. }
  71. table {
  72. border-collapse: collapse;
  73. }
  74. td {
  75. padding: 8px;
  76. font-size: 0.875rem;
  77. }
  78. .text-center {
  79. text-align: center;
  80. }
  81. .text-brand {
  82. color: #269926;
  83. }
  84. </style>
  85. </head>
  86. <body>
  87. <div>
  88. <div style="float: left; width: 49.5%; font-size: 0.875rem;">
  89. <img src="./public/img/logo.png" alt="<?=htmlspecialchars(COMPANY_NAME)?>" /><br />
  90. <br />
  91. </div>
  92. <div style="float: right; width: 49.5%; text-align: right;">
  93. <div class="title">
  94. INVOICE
  95. </div>
  96. </div>
  97. </div>
  98. <div>
  99. <div style="float: left; width: 49.5%; font-size: 0.875rem;">
  100. <strong><?=htmlspecialchars(COMPANY_NAME)?></strong><br />
  101. <?=nl2br(htmlspecialchars(implode("\n", $bill_from_address_parts)))?><br />
  102. Tel: <?=htmlspecialchars(PHONE)?>
  103. <br />
  104. <br />
  105. <span color="gray">Bill To:</span><br />
  106. <strong><?=htmlspecialchars($customer->name)?></strong><br />
  107. <?=htmlspecialchars($customer->invoice_contact_name)?><br />
  108. <?=nl2br(htmlspecialchars(implode("\n", $bill_to_address_parts)))?><br />
  109. <?php
  110. if($customer->phone) {
  111. ?>
  112. Tel: <?=htmlspecialchars($customer->phone)?><br />
  113. <?php
  114. }
  115. if($customer->email) {
  116. ?>
  117. Email: <?=htmlspecialchars($customer->email)?><br />
  118. <?php
  119. }
  120. ?>
  121. </div>
  122. <div style="float: right; width: 49.5%; text-align: right;">
  123. Ref: <?=$reference?><br />
  124. Name: <?=htmlspecialchars($project->name)?><br />
  125. <br />
  126. <table width="100%" cellspacing="0">
  127. <tr>
  128. <td width="50%" align="right" color="gray">Date:</td>
  129. <td width="50%" align="right"><?=date("d/m/Y")?></td>
  130. </tr>
  131. <tr>
  132. <td align="right" color="gray">Due Date:</td>
  133. <td align="right"><?=date("d/m/Y", time() + (30 * 86400))?></td>
  134. </tr>
  135. <tr bgcolor="#eee">
  136. <td align="right">Balance Due:</td>
  137. <td align="right">£<?=number_format($total_inc_vat, 2)?></td>
  138. </tr>
  139. </table>
  140. </div>
  141. <div style="clear: both;"></div>
  142. <br />
  143. <table width="100%" cellspacing="0" border="1">
  144. <tr bgcolor="#444">
  145. <td color="white" width="55%">Item</td>
  146. <td color="white">Quantity</td>
  147. <td color="white">Rate</td>
  148. <td color="white">Amount</td>
  149. </tr>
  150. <?php
  151. /*
  152. foreach($tests as $test) {
  153. $location = $test->getLocation();
  154. $location_address = $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]);
  155. ?>
  156. <tr>
  157. <td>
  158. <strong><?=htmlspecialchars($location->name)?></strong><br />
  159. <?=nl2br(htmlspecialchars("{$location_address}\n" . date("d/m/Y H:i", strtotime($test->dts_start . " UTC"))))?>
  160. </td>
  161. <td align="right" valign="top">1</td>
  162. <td align="right" valign="top">£<?=number_format($default_customer_fee, 2)?></td>
  163. <td align="right" valign="top">£<?=number_format($default_customer_fee, 2)?></td>
  164. </tr>
  165. <?php
  166. }
  167. */
  168. foreach($test_fee_invoice_counts as $fee => $count) {
  169. ?>
  170. <tr>
  171. <td>
  172. <strong><?=htmlspecialchars($template->name)?> Testing</strong>
  173. </td>
  174. <td align="right" valign="top"><?=number_format($count)?></td>
  175. <td align="right" valign="top">£<?=number_format($fee, 2)?></td>
  176. <td align="right" valign="top">£<?=number_format($fee * $count, 2)?></td>
  177. </tr>
  178. <?php
  179. }
  180. foreach($invoice_fields as $field) {
  181. $total = $invoice_field_totals[$field->name] ?? 0;
  182. if(!$total) continue;
  183. ?>
  184. <tr>
  185. <td>
  186. <strong><?=htmlspecialchars($field->label)?></strong>
  187. </td>
  188. <td align="center" valign="top">-</td>
  189. <td align="center" valign="top">-</td>
  190. <td align="right" valign="top">£<?=number_format($total, 2)?></td>
  191. </tr>
  192. <?php
  193. }
  194. ?>
  195. <tr>
  196. <td colspan="3" align="right" valign="top" color="gray">Total</td>
  197. <td align="right" valign="top">£<?=number_format($total_ex_vat, 2)?></td>
  198. </tr>
  199. <tr>
  200. <td colspan="3" align="right" valign="top" color="gray">VAT @ 20%</td>
  201. <td align="right" valign="top">£<?=number_format($vat, 2)?></td>
  202. </tr>
  203. <tr>
  204. <td colspan="3" align="right" valign="top" color="gray">Total (inc. VAT)</td>
  205. <td align="right" valign="top">£<?=number_format($total_inc_vat, 2)?></td>
  206. </tr>
  207. </table>
  208. <br />
  209. <span color="gray">Pay To:</span><br />
  210. <?=htmlspecialchars(COMPANY_NAME)?><br />
  211. <?=htmlspecialchars(INVOICE_BANK_NAME)?><br />
  212. Account Number: <?=htmlspecialchars(INVOICE_ACCOUNT_NUMBER)?><br />
  213. Sort Code: <?=htmlspecialchars(INVOICE_SORT_CODE)?><br />
  214. <p class="text-center">Payment is due within 30 days.</p>
  215. <p class="text-center">If you have any questions concerning this invoice, contact: <?=htmlspecialchars(INVOICE_CONTACT_EMAIL)?>
  216. <p class="text-brand text-center">
  217. <strong>THANK YOU FOR YOUR BUSINESS!</strong>
  218. </p>
  219. <p class="text-brand text-center">
  220. VAT Number: <?=htmlspecialchars(INVOICE_VAT_NUMBER)?>
  221. </p>
  222. </div>
  223. </body>
  224. </html>
  225. <?php
  226. $html = ob_get_clean();
  227. $mpdf->WriteHTML($html);
  228. // Output to browser
  229. $mpdf->Output($download_filename, "D");