1
0

test_invoice.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. $test = Test::get($id);
  3. if(!$test) throw new Exception("Test not found.");
  4. if($test->status != Test::STATUS_COMPLETED) throw new Exception("An invoice cannot be generated for this test yet.");
  5. if(Core::$user->id != $test->tester_id && !Core::$user->canAdmin()) throw new Exception("You do not have permission to access this resource.");
  6. $project = $test->getProject();
  7. $location = $test->getLocation();
  8. $tester = $test->getTester();
  9. $values = $test->getValues(true);
  10. $location_address = $location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]);
  11. $fee = floatval($test->test_fee) > 0 ? $test->test_fee : $project->default_tester_fee;
  12. $fee = (float) $fee;
  13. $play_spent = (float) ($values["play_spent"] ?? 0);
  14. $travel_cost = (float) http::value("travel_cost", "float");
  15. $invoice_total = $fee + $play_spent + $travel_cost;
  16. $test->test_travel_cost = $travel_cost;
  17. $test->commit();
  18. $address = $tester->getInvoiceAddress();
  19. if(!$address) throw new Exception("Invoice address not found. Please set an address as the invoice address then try again.");
  20. 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.");
  21. $address_parts = array_filter([
  22. $address->address_1,
  23. $address->address_2,
  24. $address->city,
  25. $address->county,
  26. $address->postal_code,
  27. str_repeat("-", 32),
  28. "Tel: " . $tester->phone,
  29. "Email: " . $tester->email,
  30. ]);
  31. $reference =
  32. "T" . str_pad($test->id, 3, "0", STR_PAD_LEFT) .
  33. "/L" . str_pad($location->id, 3, "0", STR_PAD_LEFT) .
  34. "/U" . str_pad($tester->id, 3, "0", STR_PAD_LEFT);
  35. $tiny_font_size = 8;
  36. $small_font_size = 10;
  37. $normal_font_size = 10;
  38. $header_font_size = 14;
  39. $cell_height = 5;
  40. $cell_margin = 2;
  41. list($date_start, $time_start) = explode(" ", $test->dts_start);
  42. list($date_end, $time_end) = explode(" ", $test->dts_end);
  43. $dt_start = new DateTime($test->dts_start);
  44. $dt_end = new DateTime($test->dts_end);
  45. $diff = $dt_start->diff($dt_end, true);
  46. $date_start = date("d/m/Y", strtotime($test->dts_start));
  47. // A4 size: 210x297mm
  48. // Full Width = 210 - 20 = 190
  49. // Half Width = (210 - 20 - 10) / 2 = 90
  50. // Column 1 x = 10
  51. // Column 2 x = 110
  52. $pdf = new TestPDF("P", "mm", "A4");
  53. $pdf->SetSubject("Test Invoice");
  54. $pdf->SetCreator(APP_NAME);
  55. $pdf->SetAuthor(APP_NAME);
  56. $pdf->SetTitle("Invoice ($reference)");
  57. $pdf->AddPage();
  58. $pdf->SetFont("Arial");
  59. $pdf->SetDrawColor(240, 240, 240);
  60. $pdf->SetFillColor(240);
  61. $pdf->SetAutoPageBreak(false);
  62. // Right title
  63. $pdf->SetLeftMargin(110);
  64. $pdf->SetFont("Arial", "", 30);
  65. $pdf->MultiCell(90, 8, "INVOICE", 0, "R");
  66. $pdf->SetFontSize($normal_font_size);
  67. $pdf->MultiCell(90, 8, "Ref: $reference", 0, "R");
  68. $pdf->MoveY(8);
  69. $pdf->SetTextColor(128);
  70. $pdf->Cell(45, 8, "Date:", 0, 0, "R");
  71. $pdf->SetTextColor(0);
  72. $pdf->Cell(45, 8, date("d/m/Y"), 0, 1, "R");
  73. $pdf->SetTextColor(128);
  74. $pdf->Cell(45, 8, "Due Date:", 0, 0, "R");
  75. $pdf->SetTextColor(0);
  76. $pdf->Cell(45, 8, date("d/m/Y", time() + 2592000), 0, 1, "R");
  77. $pdf->Rect(110, $pdf->GetY(), 90, 8, "F");
  78. $pdf->Cell(45, 8, "Balance Due:", 0, 0, "R");
  79. $pdf->SetTextColor(0);
  80. $pdf->Cell(45, 8, utf8_decode("£") . number_format($invoice_total, 2), 0, 1, "R");
  81. // Left addresses
  82. $pdf->SetXY(10, 10);
  83. $pdf->SetLeftMargin(10);
  84. $pdf->SetTextColor(0);
  85. $pdf->SetFont("Arial", "B", $normal_font_size);
  86. $pdf->Cell(90, 4, $tester->getDisplayName(), 0, 1);
  87. $pdf->SetFont("Arial");
  88. $pdf->MultiCell(90, 4, implode("\n", $address_parts));
  89. $pdf->MoveY(8);
  90. $pdf->SetTextColor(128);
  91. $pdf->Cell(90, 4, "Bill To:", 0, 1);
  92. $pdf->SetFont("Arial", "B", $normal_font_size);
  93. $pdf->SetTextColor(0, 0, 0);
  94. $pdf->Cell(90, 4, APP_NAME, 0, 1);
  95. $pdf->SetFont("Arial");
  96. $pdf->MultiCell(90, 4, implode("\n", ["8 Market Street", "Liskeard", "Cornwall", "PL14 3JJ", str_repeat("-", 32), "Tel: 07841 353775"]));
  97. // Header
  98. $pdf->MoveY(8);
  99. $pdf->SetFillColor(64);
  100. $pdf->Rect(10, $pdf->GetY(), 190, 8, "F");
  101. $pdf->SetTextColor(255);
  102. $pdf->Cell(115, 8, "Item");
  103. $pdf->Cell(15, 8, "Quantity");
  104. $pdf->Cell(30, 8, "Rate", 0, 0, "R");
  105. $pdf->Cell(30, 8, "Amount", 0, 0, "R");
  106. $pdf->MoveY(10);
  107. // Item
  108. $pdf->SetTextColor(0);
  109. $pdf->SetFont("Arial", "", $normal_font_size);
  110. $y = $pdf->GetY();
  111. $pdf->MultiCell(115, 5, "Age Verification Test\n$location_address\n" . date("d/m/Y H:i", strtotime($test->dts_start . " UTC")), 0, "L");
  112. $next_y = $pdf->GetY();
  113. $pdf->SetXY(125, $y);
  114. $pdf->Cell(15, 5, 1, 0, 0, "R");
  115. $pdf->Cell(30, 5, utf8_decode("£") . number_format($fee, 2), 0, 0, "R");
  116. $pdf->Cell(30, 5, utf8_decode("£") . number_format($fee, 2), 0, 0, "R");
  117. $pdf->SetXY(10, $next_y);
  118. if($play_spent) {
  119. $pdf->MoveY(5);
  120. $y = $pdf->GetY();
  121. $pdf->MultiCell(115, 5, "Play Costs", 0, "L");
  122. $next_y = $pdf->GetY();
  123. $pdf->SetXY(125, $y);
  124. $pdf->Cell(15, 5, 1, 0, 0, "R");
  125. $pdf->Cell(30, 5, utf8_decode("£") . number_format($play_spent, 2), 0, 0, "R");
  126. $pdf->Cell(30, 5, utf8_decode("£") . number_format($play_spent, 2), 0, 0, "R");
  127. $pdf->SetXY(10, $next_y);
  128. }
  129. if($travel_cost) {
  130. $pdf->MoveY(5);
  131. $y = $pdf->GetY();
  132. $pdf->MultiCell(115, 5, "Travel Costs", 0, "L");
  133. $next_y = $pdf->GetY();
  134. $pdf->SetXY(125, $y);
  135. $pdf->Cell(15, 5, 1, 0, 0, "R");
  136. $pdf->Cell(30, 5, utf8_decode("£") . number_format($travel_cost, 2), 0, 0, "R");
  137. $pdf->Cell(30, 5, utf8_decode("£") . number_format($travel_cost, 2), 0, 0, "R");
  138. $pdf->SetXY(10, $next_y);
  139. }
  140. $pdf->MoveY(10);
  141. $pdf->SetTextColor(128);
  142. $pdf->Cell(150, 5, "Total:", 0, 0, "R");
  143. $pdf->SetTextColor(0);
  144. $pdf->Cell(40, 5, utf8_decode("£") . number_format($invoice_total, 2), 0, 1, "R");
  145. $pdf->MoveY(10);
  146. $pdf->SetTextColor(128);
  147. $pdf->MultiCell(190, 6, "Pay To:");
  148. $pdf->SetTextColor(0);
  149. $pdf->MultiCell(190, 6, $tester->getDisplayName());
  150. switch($tester->preferred_payment_method) {
  151. case User::PAYMENT_METHOD_BANK_TRANSFER: {
  152. $pdf->SetFont("Arial", "B");
  153. $pdf->MultiCell(190, 6, "Bank Transfer");
  154. $pdf->SetFont("Arial");
  155. $pdf->MultiCell(190, 6, "Bank Name: $tester->bank_name");
  156. $pdf->MultiCell(190, 6, "Sort Code: $tester->bank_sort_code");
  157. $pdf->MultiCell(190, 6, "Account Number: $tester->bank_account_number");
  158. break;
  159. }
  160. case User::PAYMENT_METHOD_PAYPAL: {
  161. $pdf->SetFont("Arial", "B");
  162. $pdf->MultiCell(190, 6, "PayPal");
  163. $pdf->SetFont("Arial");
  164. $pdf->MultiCell(190, 6, "PayPal Address: $tester->paypal_email");
  165. break;
  166. }
  167. }
  168. // Output
  169. $name = str_replace("/", "_", $reference) . " - " . $tester->getDisplayName() . " - Invoice.pdf";
  170. $pdf->Output("D", $name);
  171. ?>