test_invoice.php 7.1 KB

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