name); $customer_slug = str_replace("-", "_", $customer_slug); $logo_file_path = ROOT_DIR . "/public/img/customer_logos/{$customer_slug}_logo.png"; $has_logo = file_exists($logo_file_path); $page_title = "$customer->name Reports"; Core::addBreadcrumb("/account/reports?customer_id=$customer->id", $page_title); $date_range = http::value("date_range"); if(!$date_range) { $date_range = "ytd"; } $date_resolution = http::value("date_resolution"); $date_start = http::value("date_start"); $date_end = http::value("date_end"); $exclude_project_ids = http::value("exclude_project_ids", "int"); if(!is_array($exclude_project_ids)) { $exclude_project_ids = []; } $location_ids = http::value("location_ids"); if(!empty($location_ids)) { $location_ids = explode(" ", $location_ids); $location_ids = array_map("intval", $location_ids); $location_ids = array_filter($location_ids); } $retests = http::value("retests", "int"); $location_filters = [ ["field" => "region", "values" => http::value("regions", "trim")], ["field" => "area_manager", "values" => http::value("area_managers", "trim")], ["field" => "operations_manager_name", "values" => http::value("operations_managers", "trim")], ["field" => "gaming_manager_name", "values" => http::value("gaming_managers", "trim")], ["field" => "senior_gaming_manager_name", "values" => http::value("senior_gaming_managers", "trim")], ["field" => "brand", "values" => http::value("brands", "trim")], ]; if(!is_array($location_ids)) { $location_ids = []; } $all_projects = Project::query([ "customer_id" => $customer->id ]); if(Core::$user->isCustomer()) { // Filter to only projects that have past the deadline $all_projects = array_filter($all_projects, static function(Project $project) { return $project->isFinished(); }); } $project_table_data = []; foreach($all_projects as $p) { $project_table_data[] = $p->getDataTableRow(); } $all_project_ids = array_column($all_projects, "id"); $data = []; $region_data = []; $area_manager_data = []; $brand_data = []; $total_data = new stdClass; $locations_missing_region = []; $locations_missing_area_manager = []; $locations_missing_brand = []; $charts = []; $key_labels = []; $has_pass_fail = false; $submit = http::value("submit"); if($submit) { try { Core::validateCSRFToken($submit); $form_exception = new FormException; // Get all projects $projects = Project::query([ "customer_id" => $customer->id, "id" => Constraint::notIn($exclude_project_ids) ]); $project_ids = array_intersect($all_project_ids, array_column($projects, "id")); // Enforce unique template ID in selection $template_ids = array_unique(array_column($projects, "template_id")); if(count($template_ids) > 1) { $form_exception->addError("Projects with different templates have been selected."); } // Check template exists $template_id = array_shift($template_ids); $template = Template::get($template_id); if(!$template) { $form_exception->addError("Template cannot be found."); } if($form_exception->hasErrors()) { throw $form_exception; } // Get all completed tests $test_nvp = [ "project_id" => $project_ids, "status" => Test::STATUS_COMPLETED ]; if($location_ids) { $test_nvp["location_id"] = $location_ids; } if($retests !== null) { $test_nvp["retest"] = $retests; } $tests = Test::query($test_nvp); $test_result_set = new TestResultSet($tests); // Calculate date ranges $min_year = date("Y"); $max_year = date("Y"); switch($date_range) { case "ytd": default: // Nothing to do $date_range_label = "YTD"; $min_dts = "$min_year-01-01"; $max_dts = "$max_year-12-31 23:59:59"; break; case "unbound": // Work out year range foreach($tests as $test) { $dt = new DateTime($test->dts_start . " UTC"); $dt->setTimezone(new DateTimeZone(TIMEZONE)); $year = $dt->format("Y"); if($year < $min_year) $min_year = $year; if($year > $max_year) $max_year = $year; } $date_range_label = "$min_year - $max_year"; $min_dts = "$min_year-01-01"; $max_dts = "$max_year-12-31 23:59:59"; break; case "custom": $check = strcmp($date_start, $date_end); if($check > 0) { $temp = $date_start; $date_start = $date_end; $date_end = $temp; } [$min_year, $min_month, $min_day] = explode("-", $date_start); [$max_year, $max_month, $max_day] = explode("-", $date_end); $min_dts = "$date_start 00:00:00"; $max_dts = "$date_end 23:59:59"; $min_time = strtotime($min_dts); $max_time = strtotime($max_dts); $date_range_label = sprintf("%s - %s", date("j M y", $min_time), date("j M y", $max_time)); if(preg_match("/01-01$/", $date_start) && preg_match("/12-31$/", $date_end)) { $date_range_label = $min_year === $max_year ? $min_year : "$min_year - $max_year"; } break; } $tests = $test_result_set->filterByDateRange($min_dts, $max_dts); // Load locations $locations = Location::load(array_column($tests, "location_id")); // Filter by location fields $tests = array_filter($tests, static function(Test $test) use ($location_filters) { $location = $test->getLocation(); foreach($location_filters as $filter) { $field = $filter["field"]; $values = $filter["values"]; if($values && !in_array($location->$field, $values)) { return false; } } return true; }); // Get test values $test_ids = array_column($tests, "id"); $test_values = TestValue::query([ "test_id" => $test_ids, ]); $test_values_map = []; foreach($test_values as $test_value) { $test_id = $test_value->test_id; $name = $test_value->name; if(!isset($test_values_map[$test_id])) { $test_values_map[$test_id] = new stdClass; } $test_values_map[$test_id]->{$name} = $test_value->value; } // Load users User::load(array_column($tests, "tester_id")); // Get region and area manager IDs $regions = array_filter(array_column($locations, "region")); $area_managers = array_filter(array_column($locations, "area_manager")); $brands = array_filter(array_column($locations, "brand")); uksort($regions, "strnatcasecmp"); uksort($area_managers, "strnatcasecmp"); uksort($brands, "strnatcasecmp"); foreach($regions as $region) { $region_data[$region] = []; } foreach($area_managers as $area_manager) { $area_manager_data[$area_manager] = []; } foreach($brands as $brand) { $brand_data[$brand] = []; } $has_regions = !empty($region_data); $has_area_managers = !empty($area_manager_data); $has_brands = !empty($brand_data); foreach($locations as $location) { if($has_regions && !$location->region) $locations_missing_region[] = $location; if($has_area_managers && !$location->area_manager) $locations_missing_area_manager[] = $location; if($has_brands && !$location->brand) $locations_missing_brand[] = $location; } // Check if date range is same year $same_year = ($min_year === $max_year); // Empty data objects hook require $template->getProcessingFilePath("reports_create_empty_objects.php"); $has_pass_fail = isset($empty_data->passCount); // Initialise date-array based on date range and resolution $keys = []; $key_labels = []; $resolution_name = "Quarter"; for($year = $min_year; $year <= $max_year; $year++) { switch($date_resolution) { case "monthly": $resolution_name = "Month"; foreach(range(1, 12) as $month) { if(!$same_year) $label = date("M Y", mktime(0, 0, 0, $month, 1, date("Y"))); else $label = date("M", mktime(0, 0, 0, $month, 1, date("Y"))); $month = str_pad($month, 2, "0", STR_PAD_LEFT); $keys[] = "$year-$month"; $key_labels[] = $label; } break; case "quarterly": default: $resolution_name = "Quarter"; foreach(range(1, 4) as $quarter) { if(!$same_year) $label = "Q$quarter $year"; else $label = "Q$quarter"; $keys[] = "$year-$quarter"; $key_labels[] = $label; } break; } } foreach($keys as $index => $key) { $label = $key_labels[$index]; /** @var object $empty_data */ $data[$key] = clone $empty_data; $data[$key]->label = $label; foreach($region_data as &$r) { $r[$key] = clone $empty_data; $r[$key]->label = $label; } unset($r); foreach($area_manager_data as &$a) { $a[$key] = clone $empty_data; $a[$key]->label = $label; } unset($a); foreach($brand_data as &$b) { $b[$key] = clone $empty_data; $b[$key]->label = $label; } unset($b); } $max_unix = 0; $venue_type_data = []; // Process locations foreach($tests as $test) { foreach($test->getLocation()->getVenueTypes() as $type) { $venue_type_data[$type->name] = [ "passCount" => 0, "failCount" => 0, "testCount" => 0, "stageCounts" => [0, 0, 0, 0, 0] ]; } } // Process test results foreach($tests as $test) { $tester = $test->getTester(); $location = $test->getLocation(); $venue_types = $location->getVenueTypes(); $project = $test->getProject(); $test_values = $test_values_map[$test->id] ?? new stdClass; if($template->hasPassFail()) { foreach($location->getVenueTypes() as $type) { $venue_type_data[$type->name]["testCount"]++; if($test->isPass()) { $venue_type_data[$type->name]["passCount"]++; } else { $venue_type_data[$type->name]["failCount"]++; } if($test->challenge_stage > 0) { $venue_type_data[$type->name]["stageCounts"][$test->challenge_stage - 1]++; } } } $region = $location->region; $area_manager = $location->area_manager; $brand = $location->brand; $dts = $project->dts_report ?: $test->dts_start; $dt = new DateTime($dts . " UTC"); $dt->setTimezone(new DateTimeZone(TIMEZONE)); $unix = $dt->format("U"); $year = $dt->format("Y"); $month = $dt->format("m"); $quarter = ceil($month / 3); if($unix > $max_unix) { $max_unix = $unix; } switch($date_resolution) { case "monthly": $key = "$year-$month"; break; case "quarterly": default: $key = "$year-$quarter"; break; } // Skip processing if test is void if($test->isVoid()) { $data[$key]->voidCount++; $total_data->voidCount++; } else { $total_data->testCount++; $data[$key]->testCount++; if($region) { $region_data[$region][$key]->testCount++; } if($area_manager) { $area_manager_data[$area_manager][$key]->testCount++; } if($brand) { $brand_data[$brand][$key]->testCount++; } if($tester->gender === "M") { $data[$key]->maleTesterCount++; $total_data->maleTesterCount++; } if($tester->gender === "F") { $data[$key]->femaleTesterCount++; $total_data->femaleTesterCount++; } // Process test hook require $template->getProcessingFilePath("reports_process_test.php"); } } $current_month = date("m", $max_unix); $current_quarter = ceil(date("n", $max_unix) / 3); // Initialise generator $generator = new ChartGenerator($total_data, $data); $generator->setDateRangeLabel($date_range_label); $generator->setResolutionName($resolution_name); $generator->setPeriodKeysAndLabels($keys, $key_labels); $generator->setRegionData($region_data); $generator->setBrandData($brand_data); $generator->setAreaManagerData($area_manager_data); $generator->setTemplate($template); // Build charts hook require $template->getProcessingFilePath("reports_build_charts.php"); } catch(FormException $e) { $e->generateAlerts(); } catch(Exception $e) { Core::addAlert("danger", $e->getMessage()); } // Export hook if(!empty($action)) { switch($action) { case "export-pptx": case "export-odp": require "reports_presentation.php"; break; default: break; } } } View::includeOnce("header", compact("page_title")); View::include("reports", compact("customer", "date_range", "date_start", "date_end", "date_resolution", "all_projects", "project_table_data", "exclude_project_ids", "location_ids", "retests", "charts", "data", "total_data", "locations_missing_region", "locations_missing_area_manager", "locations_missing_brand", "region_data", "area_manager_data", "brand_data", "key_labels", "has_pass_fail")); View::includeOnce("footer");