reports.php 9.8 KB

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