test_invoice.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 = [
  52. "8 Market Street",
  53. "Liskeard",
  54. "Cornwall",
  55. "PL14 3JJ",
  56. ];
  57. list($date_start, $time_start) = explode(" ", $test->dts_start);
  58. list($date_end, $time_end) = explode(" ", $test->dts_end);
  59. $dt_start = new DateTime($test->dts_start);
  60. $dt_end = new DateTime($test->dts_end);
  61. $diff = $dt_start->diff($dt_end, true);
  62. $date_start = date("d/m/Y", strtotime($test->dts_start));
  63. // A4 size: 210x297mm
  64. $mpdf = new Mpdf();
  65. $mpdf->SetSubject("Test Invoice");
  66. $mpdf->SetCreator(APP_NAME);
  67. $mpdf->SetAuthor(APP_NAME);
  68. $mpdf->SetTitle("Invoice ({$reference})");
  69. $mpdf->useSubstitutions = true;
  70. ob_start();
  71. ?>
  72. <!DOCTYPE html>
  73. <html>
  74. <head>
  75. <style>
  76. body {
  77. font-family: sans-serif;
  78. }
  79. .title {
  80. font-size: 3rem;
  81. }
  82. td {
  83. padding: 8px;
  84. }
  85. </style>
  86. </head>
  87. <body>
  88. <div>
  89. <div style="float: left; width: 49.5%; font-size: 0.875rem;">
  90. <div style="float: left; width: 49.5%;">
  91. <strong><?=htmlspecialchars($tester->getDisplayName())?></strong><br />
  92. <?=nl2br(htmlspecialchars(implode("\n", $address_parts)))?><br />
  93. </div>
  94. <div style="float: right; width: 49.5%;">
  95. Tel: <?=htmlspecialchars($tester->phone)?><br />
  96. Email: <?=htmlspecialchars($tester->email)?><br />
  97. </div>
  98. <br />
  99. <br />
  100. <span color="gray">Bill To:</span><br />
  101. <div style="float: left; width: 49.5%;">
  102. <strong><?=htmlspecialchars(COMPANY_NAME)?></strong><br />
  103. <?=nl2br(htmlspecialchars(implode("\n", $bill_to_address_parts)))?>
  104. </div>
  105. <div style="float: right; width: 49.5%;">
  106. Tel: <?=htmlspecialchars(PHONE)?>
  107. </div>
  108. </div>
  109. <div style="float: right; width: 49.5%; text-align: right;">
  110. <div class="title">
  111. INVOICE
  112. </div>
  113. Ref: <?=$reference?><br />
  114. <br />
  115. <table width="100%" cellspacing="0">
  116. <tr>
  117. <td width="50%" align="right" color="gray">Date:</td>
  118. <td width="50%" align="right"><?=date("d/m/Y")?></td>
  119. </tr>
  120. <tr>
  121. <td align="right" color="gray">Due Date:</td>
  122. <td align="right"><?=date("d/m/Y", time() + 2592000)?></td>
  123. </tr>
  124. <tr bgcolor="#eee">
  125. <td align="right">Balance Due:</td>
  126. <td align="right">£<?=number_format($invoice_total, 2)?></td>
  127. </tr>
  128. </table>
  129. </div>
  130. <div style="clear: both;"></div>
  131. <br />
  132. <table width="100%" cellspacing="0" border="1">
  133. <tr bgcolor="#444">
  134. <td color="white" width="55%">Item</td>
  135. <td color="white">Quantity</td>
  136. <td color="white">Rate</td>
  137. <td color="white">Amount</td>
  138. </tr>
  139. <tr>
  140. <td>
  141. <strong><?=htmlspecialchars($template->name)?> Test</strong><br />
  142. <?=nl2br(htmlspecialchars("$location_address\n" . date("d/m/Y H:i", strtotime($test->dts_start . " UTC"))))?>
  143. </td>
  144. <td align="right" valign="top">1</td>
  145. <td align="right" valign="top">£<?=number_format($fee, 2)?></td>
  146. <td align="right" valign="top">£<?=number_format($fee, 2)?></td>
  147. </tr>
  148. <?php
  149. foreach($tester_invoice_fields as $field) {
  150. $value = $values[$field->name] ?? 0;
  151. if(!$value) continue;
  152. ?>
  153. <tr>
  154. <td>
  155. <strong><?=htmlspecialchars($field->label)?></strong>
  156. </td>
  157. <td align="right" valign="top">1</td>
  158. <td align="right" valign="top">£<?=number_format($value, 2)?></td>
  159. <td align="right" valign="top">£<?=number_format($value, 2)?></td>
  160. </tr>
  161. <?php
  162. }
  163. if($travel_cost) {
  164. ?>
  165. <tr>
  166. <td>
  167. <strong>Travel Costs</strong>
  168. </td>
  169. <td align="right" valign="top">1</td>
  170. <td align="right" valign="top">£<?=number_format($travel_cost, 2)?></td>
  171. <td align="right" valign="top">£<?=number_format($travel_cost, 2)?></td>
  172. </tr>
  173. <?php
  174. }
  175. ?>
  176. <tr>
  177. <td colspan="3" align="right" valign="top" color="gray">Total:</td>
  178. <td align="right" valign="top">£<?=number_format($invoice_total, 2)?></td>
  179. </tr>
  180. </table>
  181. <br />
  182. <span color="gray">Pay To:</span><br />
  183. <?=htmlspecialchars($tester->getDisplayName())?><br />
  184. <?php
  185. switch($tester->preferred_payment_method) {
  186. case User::PAYMENT_METHOD_BANK_TRANSFER:
  187. ?>
  188. <strong>Bank Transfer</strong><br />
  189. Bank Name: <?=htmlspecialchars($tester->bank_name)?>
  190. Sort Code: <?=htmlspecialchars($tester->bank_sort_code)?>
  191. Account Number: <?=htmlspecialchars($tester->bank_account_number)?>
  192. <?php
  193. break;
  194. case User::PAYMENT_METHOD_PAYPAL:
  195. ?>
  196. <strong>PayPal</strong><br />
  197. PayPal Address: <?=htmlspecialchars($tester->paypal_email)?>
  198. <?php
  199. break;
  200. }
  201. ?>
  202. </div>
  203. </body>
  204. </html>
  205. <?php
  206. $html = ob_get_clean();
  207. $mpdf->WriteHTML($html);
  208. // Save PDF to directory
  209. $pdf_string = $mpdf->Output("", "S");
  210. file_put_contents($pdf_filepath, $pdf_string);
  211. // Mark invoice generated
  212. $test->status_tester_invoice = Test::STATUS_TESTER_INVOICE_PENDING;
  213. $test->dts_tester_invoice_generated = Util::getDateTime();
  214. $test->commit();
  215. // Output to browser
  216. $mpdf->Output($download_filename, "D");