test_invoice.php 5.3 KB

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