project_invoice.php 5.3 KB

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