1
0

tests_table.php 7.9 KB

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