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"); 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, 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 = []; $total_data = new stdClass; $locations_missing_region = []; $locations_missing_area_manager = []; $charts = []; $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" => (new Constraint("NOT_IN"))->setValues(...$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 !== false) { $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_end; $date_start = $date_end; $date_start = $temp; } list($min_year, $min_month, $min_day) = explode("-", $date_start); list($max_year, $max_month, $max_day) = explode("-", $date_end); $min_dts = "{$date_start} 00:00:00"; $max_dts = "{$date_end} 23:59:59"; $date_range_label = "{$min_day}/{$min_month}/{$min_year} - {$max_day}/{$max_month}/{$max_year}"; 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); // 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")); // Load locations $locations = Location::load(array_column($tests, "location_id")); // Get region and area manager IDs $regions = array_filter(array_column($locations, "region")); $area_managers = array_filter(array_column($locations, "area_manager")); uksort($regions, "strnatcasecmp"); uksort($area_managers, "strnatcasecmp"); foreach($regions as $region) { $region_data[$region] = []; } foreach($area_managers as $area_manager) { $area_manager_data[$area_manager] = []; } $has_regions = !empty($region_data); $has_area_managers = !empty($area_manager_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; } // 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 = []; 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]; $data[$key] = clone $empty_data; $data[$key]->label = $label; foreach($region_data as &$r) { $r[$key] = clone $empty_data; $r[$key]->label = $label; } foreach($area_manager_data as &$a) { $a[$key] = clone $empty_data; $a[$key]->label = $label; } } $max_unix = 0; // Process test results foreach($tests as $test) { $tester = $test->getTester(); $location = $test->getLocation(); $project = $test->getProject(); $test_values = $test_values_map[$test->id] ?? new stdClass; $region = $location->region; $area_manager = $location->area_manager; $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; } $total_data->testCount++; $data[$key]->testCount++; if($region) $region_data[$region][$key]->testCount++; if($area_manager) $area_manager_data[$area_manager][$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->setAreaManagerData($area_manager_data); // 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", "region_data", "area_manager_data", "key_labels", "has_pass_fail")); View::includeOnce("footer");