1
0

test_invoice.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var int $id
  4. */
  5. use \Mpdf\Mpdf;
  6. $test = Test::get($id);
  7. if(!$test) {
  8. http_response_code(404);
  9. throw new Exception(__("Test not found."));
  10. }
  11. if($test->status != Test::STATUS_COMPLETED) {
  12. http_response_code(403);
  13. throw new Exception(__("An invoice cannot be generated for this test yet."));
  14. }
  15. if(Core::$user->id != $test->tester_id && !Core::$user->canAdmin()) {
  16. http_response_code(403);
  17. throw new Exception(__("You do not have permission to access this resource."));
  18. }
  19. $project = $test->getProject();
  20. $template = $project->getTemplate();
  21. $location = $test->getLocation();
  22. $tester = $test->getTester();
  23. $values = $test->getValues(true);
  24. $location_address = $location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]);
  25. // Base cost
  26. $fee = $test->getTestFee();
  27. $travel_cost = $test->getTravelCosts();
  28. $invoice_total = $fee + $travel_cost;
  29. // Additional tester invoice fields
  30. $tester_invoice_fields = $template->getTesterInvoiceFields();
  31. foreach($tester_invoice_fields as $field) {
  32. $invoice_total += (float) $values[$field->name] ?? 0;
  33. }
  34. // Get invoice address
  35. $address = $tester->getInvoiceAddress();
  36. if(!$address) throw new Exception(__("Invoice address not found. Please set an address as the invoice address then try again."));
  37. if(!$tester->hasValidPaymentMethod()) throw new Exception(__("No valid payment method found. Please go to the Settings page and make sure all respective fields are filled in and then try again."));
  38. // Filename
  39. $reference = $test->getTesterInvoiceReference();
  40. $download_filename = str_replace("/", "_", $reference) . " - " . $tester->getDisplayName() . " - Invoice.pdf";
  41. // Check for existing PDF
  42. $directory = $test->getDirectory();
  43. $pdf_filepath = $test->getTesterInvoicePDFPath();
  44. if($test->hasTesterInvoicePDF()) {
  45. http::setDownloadHeaders($download_filename);
  46. header("Content-Type: application/pdf");
  47. readfile($pdf_filepath);
  48. exit;
  49. }
  50. // Initialise for PDF generation
  51. $address_parts = array_filter([
  52. $address->address_1,
  53. $address->address_2,
  54. $address->city,
  55. $address->county,
  56. $address->postal_code,
  57. ]);
  58. $bill_to_address_parts = explode("\n", ADDRESS);
  59. list($date_start, $time_start) = explode(" ", $test->dts_start);
  60. list($date_end, $time_end) = explode(" ", $test->dts_end);
  61. $dt_start = new DateTime($test->dts_start);
  62. $dt_end = new DateTime($test->dts_end);
  63. $diff = $dt_start->diff($dt_end, true);
  64. $date_start = date("d/m/Y", strtotime($test->dts_start));
  65. // A4 size: 210x297mm
  66. $mpdf = new Mpdf();
  67. $mpdf->SetSubject("Test Invoice");
  68. $mpdf->SetCreator(APP_NAME);
  69. $mpdf->SetAuthor(APP_NAME);
  70. $mpdf->SetTitle("Invoice ($reference)");
  71. $mpdf->useSubstitutions = true;
  72. ob_start();
  73. ?>
  74. <!DOCTYPE html>
  75. <html>
  76. <head>
  77. <style>
  78. body {
  79. font-family: sans-serif;
  80. }
  81. .title {
  82. font-size: 3rem;
  83. }
  84. td {
  85. padding: 8px;
  86. }
  87. </style>
  88. </head>
  89. <body>
  90. <div>
  91. <div style="float: left; width: 49.5%; font-size: 0.875rem;">
  92. <div style="float: left; width: 49.5%;">
  93. <strong><?=htmlspecialchars($tester->getDisplayName())?></strong><br />
  94. <?=nl2br(htmlspecialchars(implode("\n", $address_parts)))?><br />
  95. </div>
  96. <div style="float: right; width: 49.5%;">
  97. Tel: <?=htmlspecialchars($tester->phone)?><br />
  98. Email: <?=htmlspecialchars($tester->email)?><br />
  99. </div>
  100. <br />
  101. <br />
  102. <span color="gray">Bill To:</span><br />
  103. <div style="float: left; width: 49.5%;">
  104. <strong><?=htmlspecialchars(COMPANY_NAME)?></strong><br />
  105. <?=nl2br(htmlspecialchars(implode("\n", $bill_to_address_parts)))?>
  106. </div>
  107. <div style="float: right; width: 49.5%;">
  108. Tel: <?=htmlspecialchars(PHONE)?>
  109. </div>
  110. </div>
  111. <div style="float: right; width: 49.5%; text-align: right;">
  112. <div class="title">
  113. INVOICE
  114. </div>
  115. Ref: <?=$reference?><br />
  116. <br />
  117. <table width="100%" cellspacing="0">
  118. <tr>
  119. <td width="50%" align="right" color="gray">Date:</td>
  120. <td width="50%" align="right"><?=date("d/m/Y")?></td>
  121. </tr>
  122. <tr>
  123. <td align="right" color="gray">Due Date:</td>
  124. <td align="right"><?=date("d/m/Y", time() + 2592000)?></td>
  125. </tr>
  126. <tr bgcolor="#eee">
  127. <td align="right">Balance Due:</td>
  128. <td align="right">£<?=number_format($invoice_total, 2)?></td>
  129. </tr>
  130. </table>
  131. </div>
  132. <div style="clear: both;"></div>
  133. <br />
  134. <table width="100%" cellspacing="0" border="1">
  135. <tr bgcolor="#444">
  136. <td color="white" width="55%">Item</td>
  137. <td color="white">Quantity</td>
  138. <td color="white">Rate</td>
  139. <td color="white">Amount</td>
  140. </tr>
  141. <tr>
  142. <td>
  143. <strong><?=htmlspecialchars($template->name)?> Test</strong><br />
  144. <?=nl2br(htmlspecialchars("$location_address\n" . date("d/m/Y H:i", strtotime($test->dts_start . " UTC"))))?>
  145. </td>
  146. <td align="right" valign="top">1</td>
  147. <td align="right" valign="top">£<?=number_format($fee, 2)?></td>
  148. <td align="right" valign="top">£<?=number_format($fee, 2)?></td>
  149. </tr>
  150. <?php
  151. foreach($tester_invoice_fields as $field) {
  152. $value = $values[$field->name] ?? 0;
  153. if(!$value) continue;
  154. ?>
  155. <tr>
  156. <td>
  157. <strong><?=htmlspecialchars($field->label)?></strong>
  158. </td>
  159. <td align="right" valign="top">1</td>
  160. <td align="right" valign="top">£<?=number_format($value, 2)?></td>
  161. <td align="right" valign="top">£<?=number_format($value, 2)?></td>
  162. </tr>
  163. <?php
  164. }
  165. if($travel_cost) {
  166. ?>
  167. <tr>
  168. <td>
  169. <strong>Travel Costs</strong>
  170. </td>
  171. <td align="right" valign="top">1</td>
  172. <td align="right" valign="top">£<?=number_format($travel_cost, 2)?></td>
  173. <td align="right" valign="top">£<?=number_format($travel_cost, 2)?></td>
  174. </tr>
  175. <?php
  176. }
  177. ?>
  178. <tr>
  179. <td colspan="3" align="right" valign="top" color="gray">Total:</td>
  180. <td align="right" valign="top">£<?=number_format($invoice_total, 2)?></td>
  181. </tr>
  182. </table>
  183. <br />
  184. <span color="gray">Pay To:</span><br />
  185. <?=htmlspecialchars($tester->getDisplayName())?><br />
  186. <?php
  187. switch($tester->preferred_payment_method) {
  188. case User::PAYMENT_METHOD_BANK_TRANSFER:
  189. ?>
  190. <strong>Bank Transfer</strong><br />
  191. Bank Name: <?=htmlspecialchars($tester->bank_name)?>
  192. Sort Code: <?=htmlspecialchars($tester->bank_sort_code)?>
  193. Account Number: <?=htmlspecialchars($tester->bank_account_number)?>
  194. <?php
  195. break;
  196. case User::PAYMENT_METHOD_PAYPAL:
  197. ?>
  198. <strong>PayPal</strong><br />
  199. PayPal Address: <?=htmlspecialchars($tester->paypal_email)?>
  200. <?php
  201. break;
  202. }
  203. ?>
  204. </div>
  205. </body>
  206. </html>
  207. <?php
  208. $html = ob_get_clean();
  209. $mpdf->WriteHTML($html);
  210. // Save PDF to directory
  211. $pdf_string = $mpdf->Output("", "S");
  212. file_put_contents($pdf_filepath, $pdf_string);
  213. // Mark invoice generated
  214. $test->status_tester_invoice = Test::STATUS_TESTER_INVOICE_PENDING;
  215. $test->dts_tester_invoice_generated = Util::getDateTime();
  216. $test->commit();
  217. // Output to browser
  218. $mpdf->Output($download_filename, "D");