test_invoice.php 7.8 KB

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