1
0

test_verify_form.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var int $id
  4. * @var Test $test
  5. * @var string $page_title
  6. * @var Location $location
  7. * @var Project $project
  8. * @var Template $template
  9. * @var string[] $template_keys
  10. */
  11. use Helpers\StripeHelper;
  12. $customer = $project->getCustomer();
  13. $tester = $test->getTester();
  14. $fields = $test->getFieldsForVerifyForm();
  15. $directory = $test->getDirectory();
  16. $submit = http::value("submit");
  17. $tester_message = http::value("tester_message", "trim");
  18. if($submit) {
  19. try {
  20. Core::validateCSRFToken($submit);
  21. $form_exception = new FormException;
  22. $accept = http::value("accept");
  23. $reject = http::value("reject");
  24. $accept_tester_invoice = http::value("accept_tester_invoice");
  25. $reject_tester_invoice = http::value("reject_tester_invoice");
  26. $payment_required = (bool) http::value("payment_required", "int");
  27. $invoice_amount = http::value("invoice_amount", "decimal-2");
  28. if(($accept || $reject) && $test->getStatus() !== Test::STATUS_PENDING_VERIFICATION) {
  29. throw new RuntimeException("This test is no longer pending verification.");
  30. }
  31. if(empty($test->verifier_id)) {
  32. $test->verifier_id = Core::$user->id;
  33. $test->dts_verified = Util::getDateTime();
  34. }
  35. $test->verifier_notes = http::value("verifier_notes", "trim");
  36. // Validation
  37. // Check selected retest project
  38. $new_test_project = false;
  39. $new_test_project_id = http::value("new_test_project_id", "int");
  40. $new_test_is_retest = http::value("new_test_is_retest", "int");
  41. if($new_test_project_id) {
  42. $new_test_project = Project::get($new_test_project_id);
  43. if(!$new_test_project || $new_test_project->customer_id != $customer->id) {
  44. throw new RuntimeException("The new test project selected is invalid.");
  45. }
  46. if($payment_required) {
  47. if($invoice_amount < 0) {
  48. throw new RuntimeException(__("Invoice amount cannot be less than 0.00."));
  49. }
  50. if(!$location->email) {
  51. throw new RuntimeException(__("Location does not have an email address set."));
  52. }
  53. }
  54. }
  55. // Modify test values
  56. // Overrides
  57. $unix_start = strtotime($test->dts_start . " UTC");
  58. $unix_end = strtotime($test->dts_end . " UTC");
  59. $pass_override = http::value("pass_override");
  60. if(strlen($pass_override) > 0) {
  61. $test->pass = $pass_override;
  62. }
  63. $start_override = HTTP::value("start_override");
  64. if($start_override) {
  65. $dt_start = new DateTime($start_override, new DateTimeZone(TIMEZONE));
  66. $unix_start = $dt_start->format("U");
  67. }
  68. $end_override = HTTP::value("end_override");
  69. if($end_override) {
  70. $dt_end = new DateTime($end_override, new DateTimeZone(TIMEZONE));
  71. $unix_end = $dt_end->format("U");
  72. }
  73. if($unix_start > $unix_end) {
  74. $form_exception->addError("Start time cannot be after end time.");
  75. }
  76. else {
  77. $test->dts_start = Util::getDateTime($unix_start);
  78. $test->dts_end = Util::getDateTime($unix_end);
  79. }
  80. $test->customer_fee = null;
  81. $customer_fee = http::value("customer_fee", "float");
  82. if($customer_fee !== null && $customer_fee != $project->default_customer_fee) {
  83. $test->customer_fee = $customer_fee;
  84. }
  85. $test->test_fee = null;
  86. $tester_fee = http::value("tester_fee", "float");
  87. if($tester_fee !== null && $tester_fee != $project->default_tester_fee) {
  88. $test->test_fee = $tester_fee;
  89. }
  90. $test->test_travel_cost = HTTP::value("travel_cost", "float") ?? 0;
  91. if($test->test_travel_cost < 0 || $test->test_travel_cost > 999.99) {
  92. $form_exception->addError("Travel costs must be between 0 and 999.99.");
  93. }
  94. // Test forms
  95. foreach($fields as $field) {
  96. if($field->type === "header") continue;
  97. if($field->type === "file") continue;
  98. if(empty($field->name)) continue;
  99. $required_key = $field->requiredKey ?? null;
  100. if($required_key && !in_array($required_key, $template_keys)) continue;
  101. $value = http::value($field->name, "trim");
  102. if($value === "") {
  103. $value = null;
  104. }
  105. if(in_array($field->name, Test::TEST_FIELDS, true)) {
  106. $test->{$field->name} = $value;
  107. }
  108. else {
  109. $test_value = TestValue::get($id, $field->name);
  110. if(!$test_value) {
  111. $test_value = new TestValue;
  112. $test_value->test_id = $id;
  113. $test_value->name = $field->name;
  114. }
  115. $test_value->value = $value;
  116. $test_value->commit();
  117. }
  118. }
  119. $test->void = (int) HTTP::value("void", "int");
  120. $test->void_primary = (int) HTTP::value("void_primary", "int");
  121. $test->void_secondary = (int) HTTP::value("void_secondary", "int");
  122. $test->void_notes = HTTP::value("void_notes", "trim");
  123. if($test->void || $test->void_primary || $test->void_secondary) {
  124. if(!$test->void_notes) {
  125. throw new RuntimeException(__("Void notes are required if any part of the test was void."));
  126. }
  127. }
  128. // Accept test as completed
  129. if($accept) {
  130. $test->status = Test::STATUS_COMPLETED;
  131. }
  132. // Reject test
  133. if($reject) {
  134. $test->status = Test::STATUS_CANCELLED;
  135. }
  136. if($form_exception->hasErrors()) throw $form_exception;
  137. $test->retest = (int) http::value("retest", "int");
  138. $test->dts_updated = Util::getDateTime();
  139. $success = $test->commit();
  140. if(!$success) {
  141. throw new RuntimeException("Failed to update the test. Please try again later.");
  142. }
  143. if($new_test_project) {
  144. $new_test = new Test();
  145. $new_test->project_id = $new_test_project->id;
  146. $new_test->location_id = $location->id;
  147. $new_test->previous_test_id = $test->id;
  148. $new_test->status = Test::STATUS_UNASSIGNED;
  149. $new_test->dts_created = Util::getDateTime();
  150. if($new_test_is_retest) {
  151. $new_test->retest = 1;
  152. }
  153. $new_test->payment_required = (int) $payment_required;
  154. $success = $new_test->commit();
  155. if($payment_required) {
  156. $helper = StripeHelper::getInstance();
  157. $helper->createInvoice($new_test, $invoice_amount);
  158. }
  159. }
  160. // If rejected, recreate empty test for the location
  161. if($reject) {
  162. $new_test = new Test();
  163. $new_test->project_id = $test->project_id;
  164. $new_test->location_id = $test->location_id;
  165. $new_test->status = Test::STATUS_UNASSIGNED;
  166. $new_test->dts_created = Util::getDateTime();
  167. $success = $new_test->commit();
  168. if(!$success) {
  169. throw new RuntimeException("Failed to create a new test. Please try again later.");
  170. }
  171. }
  172. if($accept) {
  173. $tester->score++;
  174. $tester->commit();
  175. if($test->isVoid() === false) {
  176. $mpdf = $test->getMPDF();
  177. $pdf_string = $mpdf->Output("", "S");
  178. $pdf_path = $directory . "/pdf.pdf";
  179. $success = file_put_contents($pdf_path, $pdf_string);
  180. $email_customer = http::value("email_customer");
  181. $email_location = http::value("email_location");
  182. $email_location_area_manager = http::value("email_location_area_manager");
  183. $email_location_operations_manager = http::value("email_location_operations_manager");
  184. $email_location_gaming_manager = http::value("email_location_gaming_manager");
  185. $email_location_senior_gaming_manager = http::value("email_location_senior_gaming_manager");
  186. $include_previous_test = (bool) http::value("include_previous_test");
  187. $email_addresses = [];
  188. if($email_customer && $customer->email_after_test && $customer->email) {
  189. $email_addresses[] = $customer->email;
  190. foreach($customer->getCcEmailAddresses() as $cc_email_address) {
  191. $email_addresses[] = $cc_email_address;
  192. }
  193. }
  194. if($email_location && $customer->email_locations_after_test && $location->email) {
  195. $email_addresses[] = $location->email;
  196. }
  197. if($email_location_area_manager && $customer->email_locations_after_test && $location->area_manager_email) {
  198. $email_addresses[] = $location->area_manager_email;
  199. }
  200. if($email_location_operations_manager && $customer->email_locations_after_test && $location->operations_manager_email) {
  201. $email_addresses[] = $location->operations_manager_email;
  202. }
  203. if($email_location_gaming_manager && $customer->email_locations_after_test && $location->gaming_manager_email) {
  204. $email_addresses[] = $location->gaming_manager_email;
  205. }
  206. if($email_location_senior_gaming_manager && $customer->email_locations_after_test && $location->senior_gaming_manager_email) {
  207. $email_addresses[] = $location->senior_gaming_manager_email;
  208. }
  209. if(EMAIL_TEST_RESULT) {
  210. $email_addresses[] = EMAIL_TEST_RESULT;
  211. }
  212. if(EMAIL_TEST_RESULT_COPY) {
  213. $email_addresses[] = EMAIL_TEST_RESULT_COPY;
  214. }
  215. $email_addresses = array_unique($email_addresses);
  216. foreach($email_addresses as $email_address) {
  217. $test->sendResultEmail($email_address, $include_previous_test);
  218. }
  219. }
  220. }
  221. if($test->getTesterInvoiceStatus() === Test::STATUS_TESTER_INVOICE_PENDING) {
  222. if($accept_tester_invoice) {
  223. $success = $test->markTesterInvoicePaid();
  224. if(!$success) {
  225. throw new RuntimeException("Failed to mark tester invoice as paid.");
  226. }
  227. }
  228. if($reject_tester_invoice) {
  229. $success = $test->rejectTesterInvoice();
  230. if(!$success) {
  231. throw new RuntimeException("Failed to reject tester invoice.");
  232. }
  233. }
  234. }
  235. Core::addAlert("success", "Test updated successfully.");
  236. HTTP::redirect("/account/tests/$id");
  237. }
  238. catch(FormException $e) {
  239. $e->generateAlerts();
  240. }
  241. catch(Exception $e) {
  242. Core::addAlert("danger", $e->getMessage());
  243. }
  244. }
  245. $projects = $customer->getProjects();
  246. $future_projects = array_filter($projects, static function(Project $p) use ($project) {
  247. if($p->id == $project->id) {
  248. return false;
  249. }
  250. if($p->template_id != $project->template_id) {
  251. return false;
  252. }
  253. $unix_deadline = Util::getUnixTimeFromUTC($p->dts_deadline);
  254. return ($unix_deadline > time());
  255. });
  256. Core::addScript("public/js/app-test-verify-form.js");
  257. View::includeOnce("header", compact("page_title"));
  258. View::include("test_verify_form", compact("test", "tester", "location", "project", "future_projects", "customer", "template", "fields", "tester_message"));
  259. View::includeOnce("footer");