| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- <?php /** @noinspection PhpIncludeInspection */
- /** @noinspection PhpUnhandledExceptionInspection */
- Core::addScript("public/js/Chart.bundle.min.js");
- Core::addScript("public/js/chartjs-plugin-datalabels.min.js");
- Core::addScript("public/js/app-reports.js");
- Core::addBreadcrumb("/account", "Account");
- Core::addBreadcrumb("/account/reports", "Reports");
- $customer = Customer::getForAdmin();
- if(!$customer) throw new Exception("You do not have permission to access this page.");
- $customer_slug = Util::slug($customer->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, 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 = [];
- $total_data = new stdClass;
- $locations_missing_region = [];
- $locations_missing_area_manager = [];
- $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" => (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 !== 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);
- // 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 = [];
- $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);
- }
- $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;
- }
- // 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($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");
|