test_invoice.php 6.4 KB

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