reports.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. $submit = http::value("submit");
  52. if($submit) {
  53. try {
  54. Core::validateCSRFToken($submit);
  55. $form_exception = new FormException;
  56. // Get all projects
  57. $projects = Project::query([
  58. "customer_id" => $customer->id,
  59. "id" => $project_ids
  60. ]);
  61. // Enforce unique template ID in selection
  62. $template_ids = array_unique(array_column($projects, "template_id"));
  63. if(count($template_ids) > 1) $form_exception->addError("Projects with different templates have been selected.");
  64. // Check template exists
  65. $template_id = array_shift($template_ids);
  66. $template = Template::get($template_id);
  67. if(!$template) $form_exception->addError("Template cannot be found.");
  68. if($form_exception->hasErrors()) throw $form_exception;
  69. // Get all completed tests
  70. $test_nvp = [
  71. "project_id" => $project_ids,
  72. "status" => Test::STATUS_COMPLETED
  73. ];
  74. if($location_ids) {
  75. $test_nvp["location_id"] = $location_ids;
  76. }
  77. $tests = Test::query($test_nvp);
  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($date_start, $date_end);
  137. if($check > 0) {
  138. $temp = $date_end;
  139. $date_start = $date_end;
  140. $date_start = $temp;
  141. }
  142. list($min_year, $min_month, $min_day) = explode("-", $date_start);
  143. list($max_year, $max_month, $max_day) = explode("-", $date_end);
  144. $min_unix = strtotime("{$date_start} 00:00:00 UTC");
  145. $max_unix = strtotime("{$date_end} 23:59:59 UTC");
  146. $date_range_label = date("d/m/Y", $min_unix) . " - " . date("d/m/Y", $max_unix);
  147. if(preg_match("/01-01$/", $date_start) && preg_match("/12-31$/", $date_end)) {
  148. if($min_year == $max_year) {
  149. $date_range_label = $min_year;
  150. }
  151. else {
  152. $date_range_label = "{$min_year} - {$max_year}";
  153. }
  154. }
  155. break;
  156. }
  157. // Check if date range is same year
  158. $same_year = ($min_year == $max_year);
  159. // Filter tests based on date range
  160. $tests = array_filter($tests, function($test) use ($min_unix, $max_unix) {
  161. $project = $test->getProject();
  162. $dts = $project->dts_report ?: $test->dts_start;
  163. $dt = new DateTime($dts . " UTC");
  164. $unix = $dt->format("U");
  165. if($unix < $min_unix) {
  166. return false;
  167. }
  168. if($unix > $max_unix) {
  169. return false;
  170. }
  171. return true;
  172. });
  173. // Empty data objects hook
  174. require $template->getProcessingFilePath("reports_create_empty_objects.php");
  175. $has_pass_fail = isset($empty_data->passCount);
  176. // Initialise date-array based on date range and resolution
  177. $keys = [];
  178. $key_labels = [];
  179. for($year = $min_year; $year <= $max_year; $year++) {
  180. switch($date_resolution) {
  181. case "monthly":
  182. $resolution_name = "Month";
  183. foreach(range(1, 12) as $month) {
  184. if(!$same_year) $label = date("M Y", mktime(0, 0, 0, $month, 1, date("Y")));
  185. else $label = date("M", mktime(0, 0, 0, $month, 1, date("Y")));
  186. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  187. $key = "$year-$month";
  188. $data[$key] = clone $empty_data;
  189. $data[$key]->label = $label;
  190. foreach($region_data as &$r) {
  191. $r[$key] = clone $empty_data;
  192. $r[$key]->label = $label;
  193. }
  194. foreach($area_manager_data as &$a) {
  195. $a[$key] = clone $empty_data;
  196. $a[$key]->label = $label;
  197. }
  198. $keys[] = $key;
  199. $key_labels[] = $label;
  200. }
  201. break;
  202. case "quarterly":
  203. default:
  204. $resolution_name = "Quarter";
  205. foreach(range(1, 4) as $quarter) {
  206. if(!$same_year) $label = "Q$quarter $year";
  207. else $label = "Q$quarter";
  208. $key = "$year-$quarter";
  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. }
  224. }
  225. $max_unix = 0;
  226. // Process test results
  227. foreach($tests as $test) {
  228. $tester = $test->getTester();
  229. $location = $test->getLocation();
  230. $project = $test->getProject();
  231. $test_values = $test_values_map[$test->id] ?? new stdClass;
  232. $region = $location->region;
  233. $area_manager = $location->area_manager;
  234. $dts = $project->dts_report ?: $test->dts_start;
  235. $dt = new DateTime($dts . " UTC");
  236. $dt->setTimezone(new DateTimeZone(TIMEZONE));
  237. $unix = $dt->format("U");
  238. $year = $dt->format("Y");
  239. $month = $dt->format("m");
  240. $quarter = ceil($month / 3);
  241. if($unix > $max_unix) {
  242. $max_unix = $unix;
  243. }
  244. switch($date_resolution) {
  245. case "monthly":
  246. $key = "$year-$month";
  247. break;
  248. case "quarterly":
  249. default:
  250. $key = "$year-$quarter";
  251. break;
  252. }
  253. $total_data->testCount++;
  254. $data[$key]->testCount++;
  255. if($region) $region_data[$region][$key]->testCount++;
  256. if($area_manager) $area_manager_data[$area_manager][$key]->testCount++;
  257. if($tester->gender == "M") {
  258. $data[$key]->maleTesterCount++;
  259. $total_data->maleTesterCount++;
  260. }
  261. if($tester->gender == "F") {
  262. $data[$key]->femaleTesterCount++;
  263. $total_data->femaleTesterCount++;
  264. }
  265. // Process test hook
  266. require $template->getProcessingFilePath("reports_process_test.php");
  267. }
  268. $current_month = date("m", $max_unix);
  269. $current_quarter = ceil(date("n", $max_unix) / 3);
  270. $x_data_labels = array_values(array_map(function($d) {
  271. return $d->label;
  272. }, $data));
  273. // Build charts hook
  274. require $template->getProcessingFilePath("reports_build_charts.php");
  275. }
  276. catch(FormException $e) {
  277. $e->generateAlerts();
  278. }
  279. catch(Exception $e) {
  280. Core::addAlert("danger", $e->getMessage());
  281. }
  282. // Export hook
  283. if(!empty($action)) {
  284. switch($action) {
  285. case "export-pptx":
  286. case "export-odp":
  287. require "reports_presentation.php";
  288. break;
  289. default:
  290. break;
  291. }
  292. }
  293. }
  294. View::includeOnce("header", compact("page_title"));
  295. 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"));
  296. View::includeOnce("footer");