test_invoice.php 6.8 KB

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