test_invoice.php 6.3 KB

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