test_invoice.php 7.6 KB

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