reports.php 13 KB

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