reports.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. Core::addScript("public/js/app-reports.js");
  3. Core::addBreadcrumb("/account", "Account");
  4. Core::addBreadcrumb("/account/reports", "Reports");
  5. $customer = Customer::getForAdmin();
  6. if(!$customer) {
  7. throw new RuntimeException("You do not have permission to access this page.");
  8. }
  9. $customer_slug = Util::slug($customer->name);
  10. $customer_slug = str_replace("-", "_", $customer_slug);
  11. $logo_file_path = ROOT_DIR . "/public/img/customer_logos/{$customer_slug}_logo.png";
  12. $has_logo = file_exists($logo_file_path);
  13. $page_title = "$customer->name Reports";
  14. Core::addBreadcrumb("/account/reports?customer_id=$customer->id", $page_title);
  15. $date_range = http::value("date_range");
  16. if(!$date_range) {
  17. $date_range = "ytd";
  18. }
  19. $date_resolution = http::value("date_resolution");
  20. $date_start = http::value("date_start");
  21. $date_end = http::value("date_end");
  22. $exclude_project_ids = http::value("exclude_project_ids", "int");
  23. if(!is_array($exclude_project_ids)) {
  24. $exclude_project_ids = [];
  25. }
  26. $location_ids = http::value("location_ids");
  27. if(!empty($location_ids)) {
  28. $location_ids = explode(" ", $location_ids);
  29. $location_ids = array_map("intval", $location_ids);
  30. $location_ids = array_filter($location_ids);
  31. }
  32. $exclude_categories = http::value("exclude_categories");
  33. $project_id = http::value("project_id", "int");
  34. $retests = http::value("retests", "int");
  35. $location_filters = [
  36. ["field" => "region", "values" => http::value("regions", "trim")],
  37. ["field" => "area_manager", "values" => http::value("area_managers", "trim")],
  38. ["field" => "operations_manager_name", "values" => http::value("operations_managers", "trim")],
  39. ["field" => "gaming_manager_name", "values" => http::value("gaming_managers", "trim")],
  40. ["field" => "senior_gaming_manager_name", "values" => http::value("senior_gaming_managers", "trim")],
  41. ["field" => "brand", "values" => http::value("brands", "trim")],
  42. ];
  43. if(!is_array($location_ids)) {
  44. $location_ids = [];
  45. }
  46. $all_projects = Project::query([
  47. "customer_id" => $customer->id
  48. ], ["sort" => "dts_start", "order" => "desc"]);
  49. if(Core::$user->isCustomer()) {
  50. // Filter to only projects that have past the deadline
  51. $all_projects = array_filter($all_projects, static function(Project $project) {
  52. return $project->isFinished();
  53. });
  54. }
  55. $project_table_data = [];
  56. foreach($all_projects as $p) {
  57. $project_table_data[] = $p->getDataTableRow();
  58. }
  59. $all_project_ids = array_column($all_projects, "id");
  60. $data = [];
  61. $region_data = [];
  62. $area_manager_data = [];
  63. $brand_data = [];
  64. $operations_manager_data = [];
  65. $gaming_manager_data = [];
  66. $senior_gaming_manager_data = [];
  67. $total_data = new stdClass;
  68. $locations_missing_region = [];
  69. $locations_missing_area_manager = [];
  70. $locations_missing_operations_manager = [];
  71. $locations_missing_brand = [];
  72. $locations_missing_gaming_manager = [];
  73. $locations_missing_senior_gaming_manager = [];
  74. $charts = [];
  75. $key_labels = [];
  76. $has_pass_fail = false;
  77. $template_config = new stdClass();
  78. $challenge_stages = null;
  79. $submit = http::value("submit");
  80. if($submit) {
  81. try {
  82. Core::validateCSRFToken($submit);
  83. $form_exception = new FormException();
  84. // Specific project
  85. if($project_id) {
  86. $projects = Project::query([
  87. "customer_id" => $customer->id,
  88. "id" => $project_id
  89. ]);
  90. if(!$projects) {
  91. $form_exception->addError("Project not found.");
  92. }
  93. }
  94. else {
  95. $projects = Project::query([
  96. "customer_id" => $customer->id,
  97. "id" => Constraint::notIn($exclude_project_ids)
  98. ]);
  99. }
  100. $project_ids = array_intersect($all_project_ids, array_column($projects, "id"));
  101. // Enforce unique template ID in selection
  102. $template_ids = array_unique(array_column($projects, "template_id"));
  103. if(count($template_ids) > 1) {
  104. $form_exception->addError("Projects with different templates have been selected.");
  105. }
  106. // Check template exists
  107. $template_id = array_shift($template_ids);
  108. $template = Template::get($template_id);
  109. if(!$template) {
  110. $form_exception->addError("Template cannot be found.");
  111. }
  112. if($form_exception->hasErrors()) {
  113. throw $form_exception;
  114. }
  115. $template_config = $template->getConfiguration();
  116. $fields = $template->getFields();
  117. $challenge_stages = [];
  118. if(isset($fields["challenge_stage"])) {
  119. $field = $fields["challenge_stage"];
  120. $values = $field->values;
  121. $displays = $field->displayStrings;
  122. foreach($values as $index => $value) {
  123. if($value) {
  124. $challenge_stages[$value] = $displays[$index];
  125. }
  126. }
  127. }
  128. // Exclude categories
  129. if(!$exclude_categories || !is_array($exclude_categories)) {
  130. $exclude_categories = $template_config->reports->excludeCategories ?? [];
  131. }
  132. // Get all completed tests
  133. $test_nvp = [
  134. "project_id" => $project_ids,
  135. "status" => Test::STATUS_COMPLETED
  136. ];
  137. if($location_ids) {
  138. $test_nvp["location_id"] = $location_ids;
  139. }
  140. if($retests !== null) {
  141. $test_nvp["retest"] = $retests;
  142. }
  143. $tests = Test::query($test_nvp);
  144. $test_result_set = new TestResultSet($tests);
  145. // Calculate date ranges
  146. $min_year = date("Y");
  147. $max_year = date("Y");
  148. switch($date_range) {
  149. case "ytd":
  150. default:
  151. // Nothing to do
  152. $date_range_label = "YTD";
  153. $min_dts = "$min_year-01-01";
  154. $max_dts = "$max_year-12-31 23:59:59";
  155. break;
  156. case "unbound":
  157. // Work out year range
  158. foreach($tests as $test) {
  159. $dt = new DateTime($test->dts_start . " UTC");
  160. $dt->setTimezone(new DateTimeZone(TIMEZONE));
  161. $year = $dt->format("Y");
  162. if($year < $min_year) $min_year = $year;
  163. if($year > $max_year) $max_year = $year;
  164. }
  165. $date_range_label = "$min_year - $max_year";
  166. $min_dts = "$min_year-01-01";
  167. $max_dts = "$max_year-12-31 23:59:59";
  168. break;
  169. case "custom":
  170. $check = strcmp($date_start, $date_end);
  171. if($check > 0) {
  172. $temp = $date_start;
  173. $date_start = $date_end;
  174. $date_end = $temp;
  175. }
  176. [$min_year, $min_month, $min_day] = explode("-", $date_start);
  177. [$max_year, $max_month, $max_day] = explode("-", $date_end);
  178. $min_dts = "$date_start 00:00:00";
  179. $max_dts = "$date_end 23:59:59";
  180. $min_time = strtotime($min_dts);
  181. $max_time = strtotime($max_dts);
  182. $date_range_label = sprintf("%s - %s", date("j M y", $min_time), date("j M y", $max_time));
  183. if(preg_match("/01-01$/", $date_start) && preg_match("/12-31$/", $date_end)) {
  184. $date_range_label = $min_year === $max_year ? $min_year : "$min_year - $max_year";
  185. }
  186. break;
  187. }
  188. if($project_id) {
  189. $tests = $test_result_set->getTests();
  190. $project = $projects[0];
  191. $date_range_label = $project->getDisplayReportMonth();
  192. }
  193. else {
  194. $tests = $test_result_set->filterByDateRange($min_dts, $max_dts);
  195. }
  196. // Load locations
  197. $locations = Location::load(array_column($tests, "location_id"));
  198. // Filter by location fields
  199. $tests = array_filter($tests, static function(Test $test) use ($location_filters) {
  200. $location = $test->getLocation();
  201. foreach($location_filters as $filter) {
  202. $field = $filter["field"];
  203. $values = $filter["values"];
  204. if($values && !in_array($location->$field, $values)) {
  205. return false;
  206. }
  207. }
  208. return true;
  209. });
  210. // Filter down locations
  211. $locations = Location::load(array_column($tests, "location_id"));
  212. // Get test values
  213. $test_ids = array_column($tests, "id");
  214. $test_values = TestValue::query([
  215. "test_id" => $test_ids,
  216. ]);
  217. $test_values_map = [];
  218. foreach($test_values as $test_value) {
  219. $test_id = $test_value->test_id;
  220. $name = $test_value->name;
  221. if(!isset($test_values_map[$test_id])) {
  222. $test_values_map[$test_id] = new stdClass;
  223. }
  224. $test_values_map[$test_id]->{$name} = $test_value->value;
  225. }
  226. // Load users
  227. User::load(array_column($tests, "tester_id"));
  228. // Get region and area manager IDs
  229. $regions = array_filter(array_column($locations, "region"));
  230. $area_managers = array_filter(array_column($locations, "area_manager"));
  231. $brands = array_filter(array_column($locations, "brand"));
  232. $operations_managers = array_filter(array_column($locations, "operations_manager_name"));
  233. $gaming_managers = array_filter(array_column($locations, "gaming_manager_name"));
  234. $senior_gaming_managers = array_filter(array_column($locations, "senior_gaming_manager_name"));
  235. uksort($regions, "strnatcasecmp");
  236. uksort($area_managers, "strnatcasecmp");
  237. uksort($brands, "strnatcasecmp");
  238. uksort($operations_managers, "strnatcasecmp");
  239. uksort($gaming_managers, "strnatcasecmp");
  240. uksort($senior_gaming_managers, "strnatcasecmp");
  241. foreach($regions as $region) {
  242. $region_data[$region] = [];
  243. }
  244. foreach($area_managers as $area_manager) {
  245. $area_manager_data[$area_manager] = [];
  246. }
  247. foreach($brands as $brand) {
  248. $brand_data[$brand] = [];
  249. }
  250. foreach($operations_managers as $operations_manager) {
  251. $operations_manager_data[$operations_manager] = [];
  252. }
  253. foreach($gaming_managers as $gaming_manager) {
  254. $gaming_manager_data[$gaming_manager] = [];
  255. }
  256. foreach($senior_gaming_managers as $senior_gaming_manager) {
  257. $senior_gaming_manager_data[$senior_gaming_manager] = [];
  258. }
  259. $has_regions = !empty($region_data);
  260. $has_area_managers = !empty($area_manager_data);
  261. $has_brands = !empty($brand_data);
  262. $has_operation_managers = !empty($operations_manager_data);
  263. $has_gaming_managers = !empty($gaming_manager_data);
  264. $has_senior_gaming_managers = !empty($senior_gaming_manager_data);
  265. foreach($locations as $location) {
  266. if($has_regions && !$location->region) $locations_missing_region[] = $location;
  267. if($has_area_managers && !$location->area_manager) $locations_missing_area_manager[] = $location;
  268. if($has_brands && !$location->brand) $locations_missing_brand[] = $location;
  269. if($has_operation_managers && !$location->operations_manager_name) $locations_missing_operations_manager[] = $location;
  270. if($has_gaming_managers && !$location->gaming_manager_name) $locations_missing_gaming_manager[] = $location;
  271. if($has_senior_gaming_managers && !$location->senior_gaming_manager_name) $locations_missing_senior_gaming_manager[] = $location;
  272. }
  273. // Check if date range is same year
  274. $same_year = ($min_year === $max_year);
  275. // Empty data objects hook
  276. require $template->getProcessingFilePath("reports_create_empty_objects.php");
  277. $has_pass_fail = isset($empty_data->passCount);
  278. // Initialise date-array based on date range and resolution
  279. $keys = [];
  280. $key_labels = [];
  281. $resolution_name = "Quarter";
  282. for($year = $min_year; $year <= $max_year; $year++) {
  283. switch($date_resolution) {
  284. case "monthly":
  285. $resolution_name = "Month";
  286. foreach(range(1, 12) as $month) {
  287. if(!$same_year) $label = date("M Y", mktime(0, 0, 0, $month, 1, date("Y")));
  288. else $label = date("M", mktime(0, 0, 0, $month, 1, date("Y")));
  289. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  290. $keys[] = "$year-$month";
  291. $key_labels[] = $label;
  292. }
  293. break;
  294. case "quarterly":
  295. default:
  296. $resolution_name = "Quarter";
  297. foreach(range(1, 4) as $quarter) {
  298. if(!$same_year) $label = "Q$quarter $year";
  299. else $label = "Q$quarter";
  300. $keys[] = "$year-$quarter";
  301. $key_labels[] = $label;
  302. }
  303. break;
  304. }
  305. }
  306. /**
  307. * @var object $empty_data
  308. */
  309. foreach($keys as $index => $key) {
  310. $label = $key_labels[$index];
  311. $data[$key] = clone $empty_data;
  312. $data[$key]->label = $label;
  313. foreach($region_data as &$r) {
  314. $r[$key] = clone $empty_data;
  315. $r[$key]->label = $label;
  316. }
  317. unset($r);
  318. foreach($area_manager_data as &$a) {
  319. $a[$key] = clone $empty_data;
  320. $a[$key]->label = $label;
  321. }
  322. unset($a);
  323. foreach($brand_data as &$b) {
  324. $b[$key] = clone $empty_data;
  325. $b[$key]->label = $label;
  326. }
  327. unset($b);
  328. foreach($operations_manager_data as &$om) {
  329. $om[$key] = clone $empty_data;
  330. $om[$key]->label = $label;
  331. }
  332. unset($om);
  333. foreach($gaming_manager_data as &$gm) {
  334. $gm[$key] = clone $empty_data;
  335. $gm[$key]->label = $label;
  336. }
  337. unset($gm);
  338. foreach($senior_gaming_manager_data as &$sgm) {
  339. $sgm[$key] = clone $empty_data;
  340. $sgm[$key]->label = $label;
  341. }
  342. unset($sgm);
  343. }
  344. $max_unix = 0;
  345. $venue_type_data = [];
  346. // Process locations
  347. foreach($tests as $test) {
  348. foreach($test->getLocation()->getVenueTypes() as $type) {
  349. $venue_type_data[$type->name] = [
  350. "passCount" => 0,
  351. "failCount" => 0,
  352. "testCount" => 0,
  353. "stageCounts" => [0, 0, 0, 0, 0]
  354. ];
  355. }
  356. }
  357. // Process test results
  358. /** @var Test $test */
  359. foreach($tests as $test) {
  360. $tester = $test->getTester();
  361. /** @var Location $location */
  362. $location = $test->getLocation();
  363. $venue_types = $location->getVenueTypes();
  364. $project = $test->getProject();
  365. $test_values = $test_values_map[$test->id] ?? new stdClass;
  366. if($template->hasPassFail()) {
  367. foreach($location->getVenueTypes() as $type) {
  368. $venue_type_data[$type->name]["testCount"]++;
  369. if($test->isPass()) {
  370. $venue_type_data[$type->name]["passCount"]++;
  371. }
  372. else {
  373. $venue_type_data[$type->name]["failCount"]++;
  374. }
  375. if($test->challenge_stage > 0) {
  376. $venue_type_data[$type->name]["stageCounts"][$test->challenge_stage - 1]++;
  377. }
  378. }
  379. }
  380. $region = $location->region;
  381. $area_manager = $location->area_manager;
  382. $brand = $location->brand;
  383. $operations_manager = $location->operations_manager_name;
  384. $gaming_manager = $location->gaming_manager_name;
  385. $senior_gaming_manager = $location->senior_gaming_manager_name;
  386. $dts = $project->dts_report ?: $test->dts_start;
  387. $dt = new DateTime($dts . " UTC");
  388. $dt->setTimezone(new DateTimeZone(TIMEZONE));
  389. $unix = $dt->format("U");
  390. $year = $dt->format("Y");
  391. $month = $dt->format("m");
  392. $quarter = ceil($month / 3);
  393. if($unix > $max_unix) {
  394. $max_unix = $unix;
  395. }
  396. switch($date_resolution) {
  397. case "monthly":
  398. $key = "$year-$month";
  399. break;
  400. case "quarterly":
  401. default:
  402. $key = "$year-$quarter";
  403. break;
  404. }
  405. // Skip processing if test is void
  406. if($test->isVoid()) {
  407. $data[$key]->voidCount++;
  408. $total_data->voidCount++;
  409. }
  410. else {
  411. $total_data->testCount++;
  412. $data[$key]->testCount++;
  413. if($region) {
  414. $region_data[$region][$key]->testCount++;
  415. }
  416. if($area_manager) {
  417. $area_manager_data[$area_manager][$key]->testCount++;
  418. }
  419. if($brand) {
  420. $brand_data[$brand][$key]->testCount++;
  421. }
  422. if($operations_manager) {
  423. $operations_manager_data[$operations_manager][$key]->testCount++;
  424. }
  425. if($tester->gender === "M") {
  426. $data[$key]->maleTesterCount++;
  427. $total_data->maleTesterCount++;
  428. }
  429. if($tester->gender === "F") {
  430. $data[$key]->femaleTesterCount++;
  431. $total_data->femaleTesterCount++;
  432. }
  433. // Process test hook
  434. require $template->getProcessingFilePath("reports_process_test.php");
  435. }
  436. }
  437. $current_month = date("m", $max_unix);
  438. $current_quarter = ceil(date("n", $max_unix) / 3);
  439. // Initialise generator
  440. $generator = new ChartGenerator($total_data, $data);
  441. $generator->setDateRangeLabel($date_range_label);
  442. $generator->setResolutionName($resolution_name);
  443. $generator->setPeriodKeysAndLabels($keys, $key_labels);
  444. $generator->setRegionData($region_data);
  445. $generator->setBrandData($brand_data);
  446. $generator->setAreaManagerData($area_manager_data);
  447. $generator->setOperationsManagerData($operations_manager_data);
  448. $generator->setTemplate($template);
  449. $generator->setCustomer($customer);
  450. // Build charts hook
  451. require $template->getProcessingFilePath("reports_build_charts.php");
  452. }
  453. catch(FormException $e) {
  454. $e->generateAlerts();
  455. }
  456. catch(Exception $e) {
  457. Core::addAlert("danger", $e->getMessage());
  458. }
  459. // Export hook
  460. if(!empty($action)) {
  461. switch($action) {
  462. case "export-pptx":
  463. case "export-odp":
  464. require "reports_presentation.php";
  465. break;
  466. default:
  467. break;
  468. }
  469. }
  470. }
  471. View::includeOnce("header", compact("page_title"));
  472. 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"));
  473. View::includeOnce("footer");
  474. file_put_contents("periodic_data.bin", serialize($data));
  475. file_put_contents("total_data.bin", serialize($total_data));