reports.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. Core::addScript("public/js/Chart.bundle.min.js");
  3. Core::addScript("public/js/chartjs-plugin-datalabels.min.js");
  4. Core::addScript("public/js/app-reports.js");
  5. Core::addBreadcrumb("/account", "Account");
  6. Core::addBreadcrumb("/account/reports", "Reports");
  7. $customer = Customer::getForAdmin();
  8. if(!$customer) throw new Exception("You do not have permission to access this page.");
  9. $page_title = "$customer->name Reports";
  10. Core::addBreadcrumb("/account/reports?customer_id=$customer->id", $page_title);
  11. $date_range = http::value("date_range");
  12. if(!$date_range) $date_range = "ytd";
  13. $date_resolution = http::value("date_resolution");
  14. $from_quarter = http::value("from_quarter");
  15. $to_quarter = http::value("to_quarter");
  16. $project_ids = http::value("project_ids", "int");
  17. if(!is_array($project_ids)) $project_ids = [];
  18. $all_projects = Project::query([
  19. "customer_id" => $customer->id
  20. ]);
  21. if(Core::$user->isCustomer()) {
  22. // Filter to only projects that have past the deadline
  23. $all_projects = array_filter($all_projects, function($project) {
  24. return $project->isPastDeadline();
  25. });
  26. }
  27. $data = [];
  28. $region_data = [];
  29. $area_manager_data = [];
  30. $total_data = false;
  31. $locations_missing_region = [];
  32. $locations_missing_area_manager = [];
  33. $charts = [];
  34. $colours = [
  35. "#f44336",
  36. "#673ab7",
  37. "#03a9f4",
  38. "#4caf50",
  39. "#ffeb3b",
  40. "#ff5722",
  41. "#607d8b",
  42. "#e91e63",
  43. "#3f51b5",
  44. "#00bcd4",
  45. "#8bc34a",
  46. "#ffc107",
  47. "#795548",
  48. "#9c27b0",
  49. "#2196f3",
  50. "#009688",
  51. "#cddc39",
  52. "#ff9800",
  53. "#9e9e9e",
  54. ];
  55. $submit = http::value("submit");
  56. if($submit) {
  57. try {
  58. Core::validateCSRFToken($submit);
  59. $form_exception = new FormException;
  60. // Get all projects
  61. $projects = Project::query([
  62. "customer_id" => $customer->id,
  63. "id" => $project_ids
  64. ]);
  65. // Enforce unique template ID in selection
  66. $template_ids = array_unique(array_column($projects, "template_id"));
  67. if(count($template_ids) > 1) $form_exception->addError("Projects with different templates have been selected.");
  68. // Check template exists
  69. $template_id = array_shift($template_ids);
  70. $template = Template::get($template_id);
  71. if(!$template) $form_exception->addError("Template cannot be found.");
  72. if($form_exception->hasErrors()) throw $form_exception;
  73. // Get all completed tests
  74. $tests = Test::query([
  75. "project_id" => $project_ids,
  76. "status" => Test::STATUS_COMPLETED
  77. ]);
  78. $test_count = count($tests);
  79. // Get test values
  80. $test_ids = array_column($tests, "id");
  81. $test_values = TestValue::query([
  82. "test_id" => $test_ids,
  83. ]);
  84. $test_values_map = [];
  85. foreach($test_values as $test_value) {
  86. $test_id = $test_value->test_id;
  87. $name = $test_value->name;
  88. if(!isset($test_values_map[$test_id])) {
  89. $test_values_map[$test_id] = new stdClass;
  90. }
  91. $test_values_map[$test_id]->{$name} = $test_value->value;
  92. }
  93. // Load users
  94. User::load(array_column($tests, "tester_id"));
  95. // Load locations
  96. $locations = Location::load(array_column($tests, "location_id"));
  97. // Get region and area manager IDs
  98. $regions = array_filter(array_column($locations, "region"));
  99. $area_managers = array_filter(array_column($locations, "area_manager"));
  100. uksort($regions, "strnatcasecmp");
  101. uksort($area_managers, "strnatcasecmp");
  102. foreach($regions as $region) {
  103. $region_data[$region] = [];
  104. }
  105. foreach($area_managers as $area_manager) {
  106. $area_manager_data[$area_manager] = [];
  107. }
  108. $has_regions = !empty($region_data);
  109. $has_area_managers = !empty($area_manager_data);
  110. foreach($locations as $location) {
  111. if($has_regions && !$location->region) $locations_missing_region[] = $location;
  112. if($has_area_managers && !$location->area_manager) $locations_missing_area_manager[] = $location;
  113. }
  114. // Calculate date ranges
  115. $min_year = date("Y");
  116. $max_year = date("Y");
  117. $min_unix = strtotime("$min_year-01-01 UTC");
  118. $max_unix = strtotime("$max_year-12-31 23:59:59 UTC");
  119. switch($date_range) {
  120. case "ytd":
  121. // Nothing to do
  122. $date_range_label = "YTD";
  123. break;
  124. case "unbound":
  125. // Work out year range
  126. foreach($tests as $test) {
  127. $dt = new DateTime($test->dts_start . " UTC");
  128. $dt->setTimezone(new DateTimeZone(TIMEZONE));
  129. $year = $dt->format("Y");
  130. if($year < $min_year) $min_year = $year;
  131. if($year > $max_year) $max_year = $year;
  132. }
  133. $date_range_label = "$min_year - $max_year";
  134. break;
  135. case "custom":
  136. $check = strcmp($from_quarter, $to_quarter);
  137. if($check > 0) {
  138. $temp = $to_quarter;
  139. $from_quarter = $to_quarter;
  140. $to_quarter = $temp;
  141. }
  142. list($min_year, $min_quarter) = explode("-", $from_quarter);
  143. list($max_year, $max_quarter) = explode("-", $to_quarter);
  144. switch($min_quarter) {
  145. case 1: $unix_string = "$min_year-01-01 UTC"; break;
  146. case 2: $unix_string = "$min_year-04-01 UTC"; break;
  147. case 3: $unix_string = "$min_year-07-01 UTC"; break;
  148. case 4: $unix_string = "$min_year-10-01 UTC"; break;
  149. }
  150. $min_unix = strtotime($unix_string);
  151. switch($max_quarter) {
  152. case 1: $unix_string = "$max_year-03-31 23:59:59 UTC"; break;
  153. case 2: $unix_string = "$max_year-06-30 23:59:59 UTC"; break;
  154. case 3: $unix_string = "$max_year-09-30 23:59:59 UTC"; break;
  155. case 4: $unix_string = "$max_year-12-31 23:59:59 UTC"; break;
  156. }
  157. $max_unix = strtotime($unix_string);
  158. $date_range_label = "$min_year Q$min_quarter - $max_year Q$max_quarter";
  159. break;
  160. }
  161. // Check if date range is same year
  162. $same_year = ($min_year == $max_year);
  163. // Filter tests based on date range
  164. $tests = array_filter($tests, function($test) use ($min_unix, $max_unix) {
  165. $project = $test->getProject();
  166. $dts = $project->dts_report ?: $test->dts_start;
  167. $dt = new DateTime($dts . " UTC");
  168. $unix = $dt->format("U");
  169. if($unix < $min_unix) {
  170. return false;
  171. }
  172. if($unix > $max_unix) {
  173. return false;
  174. }
  175. return true;
  176. });
  177. // Empty data objects hook
  178. require $template->getProcessingFilePath("reports_create_empty_objects.php");
  179. $has_pass_fail = isset($empty_data->passCount);
  180. // Initialise date-array based on date range and resolution
  181. $keys = [];
  182. $key_labels = [];
  183. for($year = $min_year; $year <= $max_year; $year++) {
  184. switch($date_resolution) {
  185. case "monthly":
  186. $resolution_name = "Month";
  187. foreach(range(1, 12) as $month) {
  188. if(!$same_year) $label = date("M Y", mktime(0, 0, 0, $month, 1, date("Y")));
  189. else $label = date("M", mktime(0, 0, 0, $month, 1, date("Y")));
  190. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  191. $key = "$year-$month";
  192. $data[$key] = clone $empty_data;
  193. $data[$key]->label = $label;
  194. foreach($region_data as &$r) {
  195. $r[$key] = clone $empty_data;
  196. $r[$key]->label = $label;
  197. }
  198. foreach($area_manager_data as &$a) {
  199. $a[$key] = clone $empty_data;
  200. $a[$key]->label = $label;
  201. }
  202. $keys[] = $key;
  203. $key_labels[] = $label;
  204. }
  205. break;
  206. case "quarterly":
  207. default:
  208. $resolution_name = "Quarter";
  209. foreach(range(1, 4) as $quarter) {
  210. if(!$same_year) $label = "Q$quarter $year";
  211. else $label = "Q$quarter";
  212. $key = "$year-$quarter";
  213. $data[$key] = clone $empty_data;
  214. $data[$key]->label = $label;
  215. foreach($region_data as &$r) {
  216. $r[$key] = clone $empty_data;
  217. $r[$key]->label = $label;
  218. }
  219. foreach($area_manager_data as &$a) {
  220. $a[$key] = clone $empty_data;
  221. $a[$key]->label = $label;
  222. }
  223. $keys[] = $key;
  224. $key_labels[] = $label;
  225. }
  226. break;
  227. }
  228. }
  229. $max_unix = 0;
  230. // Process test results
  231. foreach($tests as $test) {
  232. $tester = $test->getTester();
  233. $location = $test->getLocation();
  234. $project = $test->getProject();
  235. $test_values = $test_values_map[$test->id] ?? new stdClass;
  236. $region = $location->region;
  237. $area_manager = $location->area_manager;
  238. $dts = $project->dts_report ?: $test->dts_start;
  239. $dt = new DateTime($dts . " UTC");
  240. $dt->setTimezone(new DateTimeZone(TIMEZONE));
  241. $unix = $dt->format("U");
  242. $year = $dt->format("Y");
  243. $month = $dt->format("m");
  244. $quarter = ceil($month / 3);
  245. if($unix > $max_unix) {
  246. $max_unix = $unix;
  247. }
  248. switch($date_resolution) {
  249. case "monthly":
  250. $key = "$year-$month";
  251. break;
  252. case "quarterly":
  253. default:
  254. $key = "$year-$quarter";
  255. break;
  256. }
  257. $total_data->testCount++;
  258. $data[$key]->testCount++;
  259. if($region) $region_data[$region][$key]->testCount++;
  260. if($area_manager) $area_manager_data[$area_manager][$key]->testCount++;
  261. if($tester->gender == "M") {
  262. $data[$key]->maleTesterCount++;
  263. $total_data->maleTesterCount++;
  264. }
  265. if($tester->gender == "F") {
  266. $data[$key]->femaleTesterCount++;
  267. $total_data->femaleTesterCount++;
  268. }
  269. // Process test hook
  270. require $template->getProcessingFilePath("reports_process_test.php");
  271. }
  272. $current_month = date("m", $max_unix);
  273. $current_quarter = ceil(date("n", $max_unix) / 3);
  274. $x_data_labels = array_values(array_map(function($d) {
  275. return $d->label;
  276. }, $data));
  277. // Build charts hook
  278. require $template->getProcessingFilePath("reports_build_charts.php");
  279. }
  280. catch(FormException $e) {
  281. $e->generateAlerts();
  282. }
  283. catch(Exception $e) {
  284. Core::addAlert("danger", $e->getMessage());
  285. }
  286. // Export hook
  287. if(!empty($action)) {
  288. switch($action) {
  289. case "export-pptx":
  290. case "export-odp":
  291. require "reports_presentation.php";
  292. break;
  293. default:
  294. break;
  295. }
  296. }
  297. }
  298. View::includeOnce("header", compact("page_title"));
  299. View::include("reports", compact("customer", "date_range", "from_quarter", "to_quarter", "date_resolution", "all_projects", "project_ids", "charts", "data", "total_data", "locations_missing_region", "locations_missing_area_manager", "region_data", "area_manager_data", "key_labels", "has_pass_fail"));
  300. View::includeOnce("footer");
  301. ?>