reports.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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} UTC");
  145. $max_unix = strtotime("{$date_end} UTC");
  146. $date_range_label = date("d/m/Y", $min_unix) . " - " . date("d/m/Y", $max_unix);
  147. break;
  148. }
  149. // Check if date range is same year
  150. $same_year = ($min_year == $max_year);
  151. // Filter tests based on date range
  152. $tests = array_filter($tests, function($test) use ($min_unix, $max_unix) {
  153. $project = $test->getProject();
  154. $dts = $project->dts_report ?: $test->dts_start;
  155. $dt = new DateTime($dts . " UTC");
  156. $unix = $dt->format("U");
  157. if($unix < $min_unix) {
  158. return false;
  159. }
  160. if($unix > $max_unix) {
  161. return false;
  162. }
  163. return true;
  164. });
  165. // Empty data objects hook
  166. require $template->getProcessingFilePath("reports_create_empty_objects.php");
  167. $has_pass_fail = isset($empty_data->passCount);
  168. // Initialise date-array based on date range and resolution
  169. $keys = [];
  170. $key_labels = [];
  171. for($year = $min_year; $year <= $max_year; $year++) {
  172. switch($date_resolution) {
  173. case "monthly":
  174. $resolution_name = "Month";
  175. foreach(range(1, 12) as $month) {
  176. if(!$same_year) $label = date("M Y", mktime(0, 0, 0, $month, 1, date("Y")));
  177. else $label = date("M", mktime(0, 0, 0, $month, 1, date("Y")));
  178. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  179. $key = "$year-$month";
  180. $data[$key] = clone $empty_data;
  181. $data[$key]->label = $label;
  182. foreach($region_data as &$r) {
  183. $r[$key] = clone $empty_data;
  184. $r[$key]->label = $label;
  185. }
  186. foreach($area_manager_data as &$a) {
  187. $a[$key] = clone $empty_data;
  188. $a[$key]->label = $label;
  189. }
  190. $keys[] = $key;
  191. $key_labels[] = $label;
  192. }
  193. break;
  194. case "quarterly":
  195. default:
  196. $resolution_name = "Quarter";
  197. foreach(range(1, 4) as $quarter) {
  198. if(!$same_year) $label = "Q$quarter $year";
  199. else $label = "Q$quarter";
  200. $key = "$year-$quarter";
  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. }
  216. }
  217. $max_unix = 0;
  218. // Process test results
  219. foreach($tests as $test) {
  220. $tester = $test->getTester();
  221. $location = $test->getLocation();
  222. $project = $test->getProject();
  223. $test_values = $test_values_map[$test->id] ?? new stdClass;
  224. $region = $location->region;
  225. $area_manager = $location->area_manager;
  226. $dts = $project->dts_report ?: $test->dts_start;
  227. $dt = new DateTime($dts . " UTC");
  228. $dt->setTimezone(new DateTimeZone(TIMEZONE));
  229. $unix = $dt->format("U");
  230. $year = $dt->format("Y");
  231. $month = $dt->format("m");
  232. $quarter = ceil($month / 3);
  233. if($unix > $max_unix) {
  234. $max_unix = $unix;
  235. }
  236. switch($date_resolution) {
  237. case "monthly":
  238. $key = "$year-$month";
  239. break;
  240. case "quarterly":
  241. default:
  242. $key = "$year-$quarter";
  243. break;
  244. }
  245. $total_data->testCount++;
  246. $data[$key]->testCount++;
  247. if($region) $region_data[$region][$key]->testCount++;
  248. if($area_manager) $area_manager_data[$area_manager][$key]->testCount++;
  249. if($tester->gender == "M") {
  250. $data[$key]->maleTesterCount++;
  251. $total_data->maleTesterCount++;
  252. }
  253. if($tester->gender == "F") {
  254. $data[$key]->femaleTesterCount++;
  255. $total_data->femaleTesterCount++;
  256. }
  257. // Process test hook
  258. require $template->getProcessingFilePath("reports_process_test.php");
  259. }
  260. $current_month = date("m", $max_unix);
  261. $current_quarter = ceil(date("n", $max_unix) / 3);
  262. $x_data_labels = array_values(array_map(function($d) {
  263. return $d->label;
  264. }, $data));
  265. // Build charts hook
  266. require $template->getProcessingFilePath("reports_build_charts.php");
  267. }
  268. catch(FormException $e) {
  269. $e->generateAlerts();
  270. }
  271. catch(Exception $e) {
  272. Core::addAlert("danger", $e->getMessage());
  273. }
  274. // Export hook
  275. if(!empty($action)) {
  276. switch($action) {
  277. case "export-pptx":
  278. case "export-odp":
  279. require "reports_presentation.php";
  280. break;
  281. default:
  282. break;
  283. }
  284. }
  285. }
  286. View::includeOnce("header", compact("page_title"));
  287. 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"));
  288. View::includeOnce("footer");