test_invoice.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. header("Content-Type: application/pdf");
  34. readfile($pdf_filepath);
  35. exit;
  36. }
  37. // Initialise for PDF generation
  38. $address_parts = array_filter([
  39. $address->address_1,
  40. $address->address_2,
  41. $address->city,
  42. $address->county,
  43. $address->postal_code,
  44. ]);
  45. $bill_to_address_parts = [
  46. "8 Market Street",
  47. "Liskeard",
  48. "Cornwall",
  49. "PL14 3JJ",
  50. ];
  51. list($date_start, $time_start) = explode(" ", $test->dts_start);
  52. list($date_end, $time_end) = explode(" ", $test->dts_end);
  53. $dt_start = new DateTime($test->dts_start);
  54. $dt_end = new DateTime($test->dts_end);
  55. $diff = $dt_start->diff($dt_end, true);
  56. $date_start = date("d/m/Y", strtotime($test->dts_start));
  57. // A4 size: 210x297mm
  58. $mpdf = new Mpdf();
  59. $mpdf->SetSubject("Test Invoice");
  60. $mpdf->SetCreator(APP_NAME);
  61. $mpdf->SetAuthor(APP_NAME);
  62. $mpdf->SetTitle("Invoice ({$reference})");
  63. $mpdf->useSubstitutions = true;
  64. ob_start();
  65. ?>
  66. <!DOCTYPE html>
  67. <html>
  68. <head>
  69. <style>
  70. body {
  71. font-family: sans-serif;
  72. }
  73. .title {
  74. font-size: 3rem;
  75. }
  76. td {
  77. padding: 8px;
  78. }
  79. </style>
  80. </head>
  81. <body>
  82. <div>
  83. <div style="float: left; width: 49.5%; font-size: 0.875rem;">
  84. <div style="float: left; width: 49.5%;">
  85. <strong><?=htmlspecialchars($tester->getDisplayName())?></strong><br />
  86. <?=nl2br(htmlspecialchars(implode("\n", $address_parts)))?><br />
  87. </div>
  88. <div style="float: right; width: 49.5%;">
  89. Tel: <?=htmlspecialchars($tester->phone)?><br />
  90. Email: <?=htmlspecialchars($tester->email)?><br />
  91. </div>
  92. <br />
  93. <br />
  94. <span color="gray">Bill To:</span><br />
  95. <div style="float: left; width: 49.5%;">
  96. <strong><?=htmlspecialchars(COMPANY_NAME)?></strong><br />
  97. <?=nl2br(htmlspecialchars(implode("\n", $bill_to_address_parts)))?>
  98. </div>
  99. <div style="float: right; width: 49.5%;">
  100. Tel: <?=htmlspecialchars(PHONE)?>
  101. </div>
  102. </div>
  103. <div style="float: right; width: 49.5%; text-align: right;">
  104. <div class="title">
  105. INVOICE
  106. </div>
  107. Ref: <?=$reference?><br />
  108. <br />
  109. <table width="100%" cellspacing="0">
  110. <tr>
  111. <td width="50%" align="right" color="gray">Date:</td>
  112. <td width="50%" align="right"><?=date("d/m/Y")?></td>
  113. </tr>
  114. <tr>
  115. <td align="right" color="gray">Due Date:</td>
  116. <td align="right"><?=date("d/m/Y", time() + 2592000)?></td>
  117. </tr>
  118. <tr bgcolor="#eee">
  119. <td align="right">Balance Due:</td>
  120. <td align="right">£<?=number_format($invoice_total, 2)?></td>
  121. </tr>
  122. </table>
  123. </div>
  124. <div style="clear: both;"></div>
  125. <br />
  126. <table width="100%" cellspacing="0" border="1">
  127. <tr bgcolor="#444">
  128. <td color="white" width="55%">Item</td>
  129. <td color="white">Quantity</td>
  130. <td color="white">Rate</td>
  131. <td color="white">Amount</td>
  132. </tr>
  133. <tr>
  134. <td>
  135. <strong><?=htmlspecialchars($template->name)?> Test</strong><br />
  136. <?=nl2br(htmlspecialchars("$location_address\n" . date("d/m/Y H:i", strtotime($test->dts_start . " UTC"))))?>
  137. </td>
  138. <td align="right" valign="top">1</td>
  139. <td align="right" valign="top">£<?=number_format($fee, 2)?></td>
  140. <td align="right" valign="top">£<?=number_format($fee, 2)?></td>
  141. </tr>
  142. <?php
  143. if($play_spent) {
  144. ?>
  145. <tr>
  146. <td>
  147. <strong>Play Costs</strong>
  148. </td>
  149. <td align="right" valign="top">1</td>
  150. <td align="right" valign="top">£<?=number_format($play_spent, 2)?></td>
  151. <td align="right" valign="top">£<?=number_format($play_spent, 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");