1
0

reports.php 10 KB

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