| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626 |
- <?php /** @noinspection PhpUnhandledExceptionInspection */
- use Carbon\CarbonImmutable;
- Core::addScript("public/js/app-reports.js");
- Core::addBreadcrumb("/account", "Account");
- Core::addBreadcrumb("/account/reports", "Reports");
- $customer = Customer::getForAdmin();
- if(!$customer) {
- throw new RuntimeException("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);
- }
- $exclude_categories = http::value("exclude_categories");
- $project_id = http::value("project_id", "int");
- $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
- ], ["sort" => "dts_start", "order" => "desc"]);
- 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 = [];
- $operations_manager_data = [];
- $gaming_manager_data = [];
- $senior_gaming_manager_data = [];
- $total_data = new stdClass;
- $locations_missing_region = [];
- $locations_missing_area_manager = [];
- $locations_missing_operations_manager = [];
- $locations_missing_brand = [];
- $locations_missing_gaming_manager = [];
- $locations_missing_senior_gaming_manager = [];
- $charts = [];
- $key_labels = [];
- $has_pass_fail = false;
- $template_config = new stdClass();
- $challenge_stages = null;
- $submit = http::value("submit");
- if($submit) {
- try {
- Core::validateCSRFToken($submit);
- $form_exception = new FormException();
- // Specific project
- if($project_id) {
- $projects = Project::query([
- "customer_id" => $customer->id,
- "id" => $project_id
- ]);
- if(!$projects) {
- $form_exception->addError("Project not found.");
- }
- }
- else {
- $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;
- }
- $template_config = $template->getConfiguration();
- $fields = $template->getFields();
- $challenge_stages = [];
- if(isset($fields["challenge_stage"])) {
- $field = $fields["challenge_stage"];
- $values = $field->values;
- $displays = $field->displayStrings;
- foreach($values as $index => $value) {
- if($value) {
- $challenge_stages[$value] = $displays[$index];
- }
- }
- }
- // Exclude categories
- if(!$exclude_categories || !is_array($exclude_categories)) {
- $exclude_categories = $template_config->reports->excludeCategories ?? [];
- }
- // 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";
- $dt_min = CarbonImmutable::parse($min_dts, "UTC")->startOfDay();
- $dt_max = CarbonImmutable::parse($max_dts, "UTC")->endOfDay();
- 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";
- $dt_min = CarbonImmutable::parse($min_dts, "UTC")->startOfDay();
- $dt_max = CarbonImmutable::parse($max_dts, "UTC")->endOfDay();
- 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";
- }
- $dt_min = CarbonImmutable::parse($min_dts, "UTC")->startOfDay();
- $dt_max = CarbonImmutable::parse($max_dts, "UTC")->endOfDay();
- break;
- }
- if($project_id) {
- $tests = $test_result_set->getTests();
- $project = $projects[0];
- $date_range_label = $project->getDisplayReportMonth();
- }
- else {
- $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;
- });
- // Filter down locations
- $locations = Location::load(array_column($tests, "location_id"));
- // 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"));
- $operations_managers = array_filter(array_column($locations, "operations_manager_name"));
- $gaming_managers = array_filter(array_column($locations, "gaming_manager_name"));
- $senior_gaming_managers = array_filter(array_column($locations, "senior_gaming_manager_name"));
- uksort($regions, "strnatcasecmp");
- uksort($area_managers, "strnatcasecmp");
- uksort($brands, "strnatcasecmp");
- uksort($operations_managers, "strnatcasecmp");
- uksort($gaming_managers, "strnatcasecmp");
- uksort($senior_gaming_managers, "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] = [];
- }
- foreach($operations_managers as $operations_manager) {
- $operations_manager_data[$operations_manager] = [];
- }
- foreach($gaming_managers as $gaming_manager) {
- $gaming_manager_data[$gaming_manager] = [];
- }
- foreach($senior_gaming_managers as $senior_gaming_manager) {
- $senior_gaming_manager_data[$senior_gaming_manager] = [];
- }
- $has_regions = !empty($region_data);
- $has_area_managers = !empty($area_manager_data);
- $has_brands = !empty($brand_data);
- $has_operation_managers = !empty($operations_manager_data);
- $has_gaming_managers = !empty($gaming_manager_data);
- $has_senior_gaming_managers = !empty($senior_gaming_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;
- if($has_brands && !$location->brand) $locations_missing_brand[] = $location;
- if($has_operation_managers && !$location->operations_manager_name) $locations_missing_operations_manager[] = $location;
- if($has_gaming_managers && !$location->gaming_manager_name) $locations_missing_gaming_manager[] = $location;
- if($has_senior_gaming_managers && !$location->senior_gaming_manager_name) $locations_missing_senior_gaming_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";
- if($date_resolution === "monthly") {
- $resolution_name = "Month";
- $dt_month = $dt_min->startOfMonth();
- do {
- $year = $dt_month->year;
- if(!$same_year) {
- $label = $dt_month->format("M Y");
- }
- else {
- $label = $dt_month->format("M");
- }
- $month = $dt_month->format("m");
- $keys[] = "$year-$month";
- $key_labels[] = $label;
- $dt_month = $dt_month->addMonth();
- } while($dt_month < $dt_max);
- }
- else {
- for($year = $min_year; $year <= $max_year; $year++) {
- switch($date_resolution) {
- 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;
- }
- }
- }
- /**
- * @var object $empty_data
- */
- 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;
- }
- 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);
- foreach($operations_manager_data as &$om) {
- $om[$key] = clone $empty_data;
- $om[$key]->label = $label;
- }
- unset($om);
- foreach($gaming_manager_data as &$gm) {
- $gm[$key] = clone $empty_data;
- $gm[$key]->label = $label;
- }
- unset($gm);
- foreach($senior_gaming_manager_data as &$sgm) {
- $sgm[$key] = clone $empty_data;
- $sgm[$key]->label = $label;
- }
- unset($sgm);
- }
- $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
- /** @var Test $test */
- foreach($tests as $test) {
- $tester = $test->getTester();
- /** @var Location $location */
- $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;
- $operations_manager = $location->operations_manager_name;
- $gaming_manager = $location->gaming_manager_name;
- $senior_gaming_manager = $location->senior_gaming_manager_name;
- $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($operations_manager) {
- $operations_manager_data[$operations_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->setBrandData($brand_data);
- $generator->setAreaManagerData($area_manager_data);
- $generator->setOperationsManagerData($operations_manager_data);
- $generator->setTemplate($template);
- $generator->setCustomer($customer);
- // 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", "project_id", "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_operations_manager", "locations_missing_brand", "locations_missing_gaming_manager", "locations_missing_senior_gaming_manager", "region_data", "area_manager_data", "brand_data", "operations_manager_data", "gaming_manager_data", "senior_gaming_manager_data", "key_labels", "has_pass_fail", "template_config", "exclude_categories", "challenge_stages"));
- View::includeOnce("footer");
- file_put_contents("periodic_data.bin", serialize($data));
- file_put_contents("total_data.bin", serialize($total_data));
|