1
0

project_invoice.php 5.7 KB

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