tests_admin_list.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var string $page_title
  4. */
  5. // Batch Action
  6. $submit = http::value("submit");
  7. if($submit) {
  8. try {
  9. Core::validateCSRFToken($submit);
  10. $test_ids = http::value("test_ids", "int");
  11. $batch_action = http::value("batch_action");
  12. $tests = Test::load($test_ids);
  13. $test_count = count($tests);
  14. $actioned_count = 0;
  15. switch($batch_action) {
  16. case "unassign":
  17. foreach($tests as $test) {
  18. $success = $test->unassignTester();
  19. $actioned_count += $success;
  20. }
  21. Core::addAlert("success", "$actioned_count of $test_count test(s) unassigned successfully.");
  22. break;
  23. case "mark_tester_invoice_paid":
  24. foreach($tests as $test) {
  25. $actioned_count += ($test->markTesterInvoicePaid()) ? 1 : 0;
  26. }
  27. Core::addAlert("success", "{$actioned_count} of {$test_count} test(s) marked as paid successfully.");
  28. break;
  29. case "reject_tester_invoice":
  30. foreach($tests as $test) {
  31. $actioned_count += ($test->rejectTesterInvoice()) ? 1 : 0;
  32. }
  33. Core::addAlert("success", "{$actioned_count} of {$test_count} invoices rejected successfully.");
  34. break;
  35. case "send_test_assigned_email":
  36. foreach($tests as $test) {
  37. $tester = $test->getTester();
  38. if($tester) {
  39. $test->sendAssignEmail("");
  40. $actioned_count++;
  41. }
  42. }
  43. Core::addAlert("success", "{$actioned_count} of {$test_count} emails sent successfully.");
  44. break;
  45. default:
  46. throw new RuntimeException("Invalid action.");
  47. }
  48. HTTP::redirect($_SERVER["REQUEST_URI"]);
  49. }
  50. catch(Exception $e) {
  51. Core::addAlert("danger", $e->getMessage());
  52. }
  53. }
  54. // Batch Action End
  55. $status = http::value("status", "int");
  56. $status_tester_invoice = http::value("status_tester_invoice");
  57. $customer_id = http::value("customer_id", "int");
  58. $project_id = http::value("project_id", "int");
  59. $location_id = http::value("location_id", "int");
  60. $location_name = http::value("location_name", "trim");
  61. $test_id = http::value("test_id", "int");
  62. $tester_id = http::value("tester_id", "int");
  63. $location_internal_id = http::value("location_internal_id", "int");
  64. $pass = http::value("pass", "int");
  65. $retest = http::value("retest", "int");
  66. $void = http::value("void", "int");
  67. $min_days_overdue = http::value("min_days_overdue", "int");
  68. $custom = http::value("custom");
  69. $filter_count_offset = 0;
  70. $nvp = [];
  71. // Status
  72. if(strlen($status) > 0) $nvp["status"] = $status;
  73. else {
  74. $nvp["status"] = Constraint::notEquals(Test::STATUS_CANCELLED);
  75. $filter_count_offset--;
  76. }
  77. // Status Tester Invoice
  78. if(strlen($status_tester_invoice)) {
  79. $status_tester_invoice = (int) $status_tester_invoice;
  80. $nvp["status_tester_invoice"] = $status_tester_invoice;
  81. }
  82. // Project
  83. if($project_id) $nvp["project_id"] = $project_id;
  84. else if($customer_id) {
  85. $projects = Project::query(["customer_id" => $customer_id]);
  86. $nvp["project_id"] = array_column($projects, "id");
  87. }
  88. // Location
  89. if($location_id) {
  90. $nvp["location_id"] = $location_id;
  91. }
  92. else if($location_internal_id) {
  93. $locations = Location::query(["internal_id" => $location_internal_id]);
  94. $nvp["location_id"] = $locations ? array_column($locations, "id") : "0";
  95. }
  96. else if($location_name) {
  97. $locations = Location::query([
  98. "name" => Constraint::like("%" . DB::escape($location_name) . "%")
  99. ]);
  100. $nvp["location_id"] = $locations ? array_column($locations, "id") : "0";
  101. }
  102. // Test ID
  103. if($test_id) $nvp["id"] = $test_id;
  104. // Tester ID
  105. if($tester_id) $nvp["tester_id"] = $tester_id;
  106. // Pass
  107. if(strlen($pass) > 0) $nvp["pass"] = $pass;
  108. if(isset($retest)) {
  109. if($retest === 0) {
  110. $nvp["retest"] = 0;
  111. }
  112. if($retest === 1) {
  113. $nvp["retest"] = 1;
  114. }
  115. }
  116. // Void
  117. if(isset($void)) {
  118. $nvp["void"] = (int) $void;
  119. }
  120. $filter_count = count($nvp);
  121. $sort = http::value("sort");
  122. switch($sort) {
  123. case "updated":
  124. $query_options = ["sort" => "dts_updated", "order" => "desc"];
  125. break;
  126. case "due":
  127. $query_options = ["sort" => "dts_due", "order" => "asc"];
  128. break;
  129. case "end":
  130. $query_options = ["sort" => "dts_end", "order" => "desc"];
  131. break;
  132. default:
  133. $sort = "status";
  134. $query_options = ["sort" => "status", "order" => "asc"];
  135. break;
  136. }
  137. $tests = Test::query($nvp, $query_options);
  138. $projects = Project::load(array_column($tests, "project_id"));
  139. switch($custom) {
  140. case "declined":
  141. $tests = array_filter($tests, static function(Test $test): bool {
  142. return (bool) $test->dts_rejected;
  143. });
  144. break;
  145. case "overdue_project":
  146. $tests = array_filter($tests, static function(Test $test) use ($min_days_overdue) {
  147. if($test->isCancelled()) {
  148. return false;
  149. }
  150. if($test->isCompleted()) {
  151. return false;
  152. }
  153. $project = $test->getProject();
  154. $dt_deadline = $project->getDateTimeDeadline();
  155. if(!$dt_deadline) {
  156. return false;
  157. }
  158. if(!$project->isOverdue()) {
  159. return false;
  160. }
  161. $days_overdue = $dt_deadline->diffInDays(null, true);
  162. if($min_days_overdue > 0 && $days_overdue > $min_days_overdue) {
  163. return false;
  164. }
  165. return true;
  166. });
  167. $filter_count++;
  168. break;
  169. case "overdue_test":
  170. $tests = array_filter($tests, static function(Test $test) use ($min_days_overdue) {
  171. if($test->isCancelled()) {
  172. return false;
  173. }
  174. if($test->hasDueDateTime() === false) {
  175. return false;
  176. }
  177. $dt_due = $test->getDateTimeDue();
  178. if(!$dt_due) {
  179. return false;
  180. }
  181. if(!($test->hasNotStarted() && $test->isOverdue())) {
  182. return false;
  183. }
  184. $days_overdue = $dt_due->diffInDays(null, true);
  185. if($min_days_overdue > 0 && $days_overdue < $min_days_overdue) {
  186. return false;
  187. }
  188. return true;
  189. });
  190. $filter_count++;
  191. break;
  192. case "payment_required_paid":
  193. $tests = array_filter($tests, static function(Test $test): bool {
  194. if(!$test->payment_required) {
  195. return false;
  196. }
  197. if($test->payment_required && $test->dts_paid) {
  198. return true;
  199. }
  200. return false;
  201. });
  202. $filter_count++;
  203. break;
  204. case "payment_required_unpaid":
  205. $tests = array_filter($tests, static function(Test $test): bool {
  206. if($test->payment_required && !$test->dts_paid) {
  207. return true;
  208. }
  209. return false;
  210. });
  211. $filter_count++;
  212. break;
  213. }
  214. $filter_count += $filter_count_offset;
  215. if(!$filter_count) $filter_count = "";
  216. $pagination_object = Util::getPaginationObject(count($tests));
  217. $tests = array_slice($tests, $pagination_object->offset, $pagination_object->pageSize);
  218. $testers = User::load(array_column($tests, "tester_id"));
  219. $customers = Customer::query([], ["sort" => "name"]);
  220. usort($projects, static function(Project $a, Project $b): int {
  221. return $a->name <=> $b->name;
  222. });
  223. $sorts = [
  224. "status" => "Status",
  225. "updated" => "Recently updated",
  226. "end" => "Test date",
  227. "due" => "Test due date",
  228. ];
  229. Core::addScript("public/js/app-test-list.js");
  230. View::includeOnce("header", compact("page_title"));
  231. View::include("test_list", compact("tests", "pagination_object", "sort", "sorts", "projects", "customers", "customer_id", "project_id", "location_id", "location_internal_id", "location_name", "pass", "retest", "void", "min_days_overdue", "custom", "test_id", "tester_id", "filter_count", "status", "status_tester_invoice"));