reports.php 9.9 KB

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