test_invoice.php 7.0 KB

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