| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- <?php /** @noinspection PhpUnhandledExceptionInspection */
- /**
- * @var string $page_title
- */
- $user = Core::$user;
- if(!$user) {
- throw new RuntimeException(__("No permission to access this page."));
- }
- $is_customer = $user->isCustomer();
- $is_staff = $user->isTesterManagerOrHigher();
- // Batch Action
- $submit = http::value("submit");
- if($submit && $is_staff) {
- try {
- Core::validateCSRFToken($submit);
- $test_ids = http::value("test_ids", "int");
- $batch_action = http::value("batch_action");
- $tests = Test::load($test_ids);
- $test_count = count($tests);
- $actioned_count = 0;
- switch($batch_action) {
- case "unassign":
- foreach($tests as $test) {
- $success = $test->unassignTester();
- $actioned_count += $success;
- }
- Core::addAlert("success", "$actioned_count of $test_count test(s) unassigned successfully.");
- break;
- case "mark_tester_invoice_paid":
- foreach($tests as $test) {
- $actioned_count += ($test->markTesterInvoicePaid()) ? 1 : 0;
- }
- Core::addAlert("success", "{$actioned_count} of {$test_count} test(s) marked as paid successfully.");
- break;
- case "reject_tester_invoice":
- foreach($tests as $test) {
- $actioned_count += ($test->rejectTesterInvoice()) ? 1 : 0;
- }
- Core::addAlert("success", "{$actioned_count} of {$test_count} invoices rejected successfully.");
- break;
- case "send_test_assigned_email":
- foreach($tests as $test) {
- $tester = $test->getTester();
- if($tester) {
- $test->sendAssignEmail("");
- $actioned_count++;
- }
- }
- Core::addAlert("success", "{$actioned_count} of {$test_count} emails sent successfully.");
- break;
- default:
- throw new RuntimeException("Invalid action.");
- }
- HTTP::redirect($_SERVER["REQUEST_URI"]);
- }
- catch(Exception $e) {
- Core::addAlert("danger", $e->getMessage());
- }
- }
- // Batch Action End
- $min_days_overdue = http::value("min_days_overdue", "int");
- $custom = http::value("custom");
- $filter_count_offset = 0;
- $nvp = [];
- // Staff only filters
- if($is_staff) {
- // Status
- $status = http::value("status", "int");
- if(strlen($status) > 0) {
- $nvp["status"] = $status;
- }
- else {
- $nvp["status"] = Constraint::notEquals(Test::STATUS_CANCELLED);
- $filter_count_offset--;
- }
- // Status Tester Invoice
- $status_tester_invoice = http::value("status_tester_invoice");
- if(strlen($status_tester_invoice)) {
- $status_tester_invoice = (int) $status_tester_invoice;
- $nvp["status_tester_invoice"] = $status_tester_invoice;
- }
- // Tester ID
- $tester_id = http::value("tester_id", "int");
- if($tester_id) {
- $nvp["tester_id"] = $tester_id;
- }
- }
- else {
- $status = null;
- $status_tester_invoice = null;
- $tester_id = null;
- }
- if($is_customer) {
- $customers = $user->getCustomers();
- $projects = Project::query([
- "customer_id" => array_column($customers, "id"),
- "dts_finished" => Constraint::isNotNull(),
- ]);
- $valid_project_ids = array_column($projects, "id");
- }
- else {
- $customers = Customer::query([], ["sort" => "name"]);
- $valid_project_ids = [];
- }
- // Project
- $project_id = http::value("project_id", "int");
- $customer_id = null;
- if($project_id) {
- if($is_staff || in_array($project_id, $valid_project_ids)) {
- $nvp["project_id"] = $project_id;
- }
- else {
- $nvp["project_id"] = 0;
- }
- }
- else if($is_staff) {
- $customer_id = http::value("customer_id", "int");
- if($customer_id) {
- $projects = Project::query(["customer_id" => $customer_id]);
- $nvp["project_id"] = array_column($projects, "id");
- }
- }
- else {
- $nvp["project_id"] = $valid_project_ids;
- }
- // Location
- $location_id = http::value("location_id", "int");
- $location_internal_id = http::value("location_internal_id", "int");
- $location_name = http::value("location_name", "trim");
- if($location_id) {
- $nvp["location_id"] = $location_id;
- }
- else if($location_internal_id) {
- $locations = Location::query(["internal_id" => Constraint::contains($location_internal_id)]);
- $nvp["location_id"] = $locations ? array_column($locations, "id") : "0";
- }
- else if($location_name) {
- $locations = Location::query([
- "name" => Constraint::like("%" . DB::escape($location_name) . "%")
- ]);
- $nvp["location_id"] = $locations ? array_column($locations, "id") : "0";
- }
- // Test ID
- $test_id = http::value("test_id", "int");
- if($test_id) {
- $nvp["id"] = $test_id;
- }
- // Pass
- $pass = http::value("pass", "int");
- if(strlen($pass) > 0) {
- $nvp["pass"] = $pass;
- }
- // Retest
- $retest = http::value("retest", "int");
- if(isset($retest)) {
- if($retest === 0) {
- $nvp["retest"] = 0;
- }
- if($retest === 1) {
- $nvp["retest"] = 1;
- }
- }
- // Void
- $void = http::value("void", "int");
- if(isset($void)) {
- $nvp["void"] = (int) $void;
- }
- $filter_count = count($nvp);
- if($is_staff) {
- $sort = http::value("sort");
- }
- else {
- $sort = "end";
- }
- switch($sort) {
- case "updated":
- $query_options = ["sort" => "dts_updated", "order" => "desc"];
- break;
- case "due":
- $query_options = ["sort" => "dts_due", "order" => "asc"];
- break;
- case "end":
- $query_options = ["sort" => "dts_end", "order" => "desc"];
- break;
- default:
- $sort = "status";
- $query_options = ["sort" => "status", "order" => "asc"];
- break;
- }
- $tests = Test::query($nvp, $query_options);
- if($is_staff) {
- $projects = Project::load(array_column($tests, "project_id"));
- }
- switch($custom) {
- case "declined":
- $tests = array_filter($tests, static function(Test $test): bool {
- return (bool) $test->dts_rejected;
- });
- break;
- case "overdue_project":
- $tests = array_filter($tests, static function(Test $test) use ($min_days_overdue) {
- if($test->isCancelled()) {
- return false;
- }
- if($test->isCompleted()) {
- return false;
- }
- $project = $test->getProject();
- $dt_deadline = $project->getDateTimeDeadline();
- if(!$dt_deadline) {
- return false;
- }
- if(!$project->isOverdue()) {
- return false;
- }
- $days_overdue = $dt_deadline->diffInDays(null, true);
- if($min_days_overdue > 0 && $days_overdue > $min_days_overdue) {
- return false;
- }
- return true;
- });
- $filter_count++;
- break;
- case "overdue_test":
- $tests = array_filter($tests, static function(Test $test) use ($min_days_overdue) {
- if($test->isCancelled()) {
- return false;
- }
- if($test->hasDueDateTime() === false) {
- return false;
- }
- $dt_due = $test->getDateTimeDue();
- if(!$dt_due) {
- return false;
- }
- if(!($test->hasNotStarted() && $test->isOverdue())) {
- return false;
- }
- $days_overdue = $dt_due->diffInDays(null, true);
- if($min_days_overdue > 0 && $days_overdue < $min_days_overdue) {
- return false;
- }
- return true;
- });
- $filter_count++;
- break;
- case "payment_required_paid":
- $tests = array_filter($tests, static function(Test $test): bool {
- if(!$test->payment_required) {
- return false;
- }
- if($test->payment_required && $test->dts_paid) {
- return true;
- }
- return false;
- });
- $filter_count++;
- break;
- case "payment_required_unpaid":
- $tests = array_filter($tests, static function(Test $test): bool {
- if($test->payment_required && !$test->dts_paid) {
- return true;
- }
- return false;
- });
- $filter_count++;
- break;
- }
- $filter_count += $filter_count_offset;
- if(!$filter_count) {
- $filter_count = "";
- }
- $pagination_object = Util::getPaginationObject(count($tests));
- $tests = array_slice($tests, $pagination_object->offset, $pagination_object->pageSize);
- $testers = User::load(array_column($tests, "tester_id"));
- usort($projects, static function(Project $a, Project $b): int {
- return $a->name <=> $b->name;
- });
- if($is_staff) {
- $sorts = [
- "status" => "Status",
- "updated" => "Recently updated",
- "end" => "Test date",
- "due" => "Test due date",
- ];
- }
- else {
- $sorts = [];
- }
- Core::addScript("public/js/app-test-list.js");
- View::includeOnce("header", compact("page_title"));
- 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"));
|