reports.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php /** @noinspection PhpIncludeInspection */
  2. /** @noinspection PhpUnhandledExceptionInspection */
  3. Core::addScript("public/js/Chart.bundle.min.js");
  4. Core::addScript("public/js/chartjs-plugin-datalabels.min.js");
  5. Core::addScript("public/js/app-reports.js");
  6. Core::addBreadcrumb("/account", "Account");
  7. Core::addBreadcrumb("/account/reports", "Reports");
  8. $customer = Customer::getForAdmin();
  9. if(!$customer) throw new Exception("You do not have permission to access this page.");
  10. $customer_slug = Util::slug($customer->name);
  11. $customer_slug = str_replace("-", "_", $customer_slug);
  12. $logo_file_path = ROOT_DIR . "/public/img/customer_logos/{$customer_slug}_logo.png";
  13. $has_logo = file_exists($logo_file_path);
  14. $page_title = "$customer->name Reports";
  15. Core::addBreadcrumb("/account/reports?customer_id=$customer->id", $page_title);
  16. $date_range = http::value("date_range");
  17. if(!$date_range) $date_range = "ytd";
  18. $date_resolution = http::value("date_resolution");
  19. $date_start = http::value("date_start");
  20. $date_end = http::value("date_end");
  21. $exclude_project_ids = http::value("exclude_project_ids", "int");
  22. if(!is_array($exclude_project_ids)) $exclude_project_ids = [];
  23. $location_ids = http::value("location_ids");
  24. if(!empty($location_ids)) {
  25. $location_ids = explode(" ", $location_ids);
  26. $location_ids = array_map("intval", $location_ids);
  27. $location_ids = array_filter($location_ids);
  28. }
  29. $retests = http::value("retests", "int");
  30. if(!is_array($location_ids)) {
  31. $location_ids = [];
  32. }
  33. $all_projects = Project::query([
  34. "customer_id" => $customer->id
  35. ]);
  36. if(Core::$user->isCustomer()) {
  37. // Filter to only projects that have past the deadline
  38. $all_projects = array_filter($all_projects, function(Project $project) {
  39. return $project->isFinished();
  40. });
  41. }
  42. $project_table_data = [];
  43. foreach($all_projects as $p) {
  44. $project_table_data[] = $p->getDataTableRow();
  45. }
  46. $all_project_ids = array_column($all_projects, "id");
  47. $data = [];
  48. $region_data = [];
  49. $area_manager_data = [];
  50. $total_data = new stdClass;
  51. $locations_missing_region = [];
  52. $locations_missing_area_manager = [];
  53. $charts = [];
  54. $submit = http::value("submit");
  55. if($submit) {
  56. try {
  57. Core::validateCSRFToken($submit);
  58. $form_exception = new FormException;
  59. // Get all projects
  60. $projects = Project::query([
  61. "customer_id" => $customer->id,
  62. "id" => (new Constraint("NOT_IN"))->setValues(...$exclude_project_ids)
  63. ]);
  64. $project_ids = array_intersect($all_project_ids, array_column($projects, "id"));
  65. // Enforce unique template ID in selection
  66. $template_ids = array_unique(array_column($projects, "template_id"));
  67. if(count($template_ids) > 1) $form_exception->addError("Projects with different templates have been selected.");
  68. // Check template exists
  69. $template_id = array_shift($template_ids);
  70. $template = Template::get($template_id);
  71. if(!$template) $form_exception->addError("Template cannot be found.");
  72. if($form_exception->hasErrors()) throw $form_exception;
  73. // Get all completed tests
  74. $test_nvp = [
  75. "project_id" => $project_ids,
  76. "status" => Test::STATUS_COMPLETED
  77. ];
  78. if($location_ids) {
  79. $test_nvp["location_id"] = $location_ids;
  80. }
  81. if($retests !== false) {
  82. $test_nvp["retest"] = $retests;
  83. }
  84. $tests = Test::query($test_nvp);
  85. $test_result_set = new TestResultSet($tests);
  86. // Calculate date ranges
  87. $min_year = date("Y");
  88. $max_year = date("Y");
  89. switch($date_range) {
  90. case "ytd":
  91. default:
  92. // Nothing to do
  93. $date_range_label = "YTD";
  94. $min_dts = "$min_year-01-01";
  95. $max_dts = "$max_year-12-31 23:59:59";
  96. break;
  97. case "unbound":
  98. // Work out year range
  99. foreach($tests as $test) {
  100. $dt = new DateTime($test->dts_start . " UTC");
  101. $dt->setTimezone(new DateTimeZone(TIMEZONE));
  102. $year = $dt->format("Y");
  103. if($year < $min_year) $min_year = $year;
  104. if($year > $max_year) $max_year = $year;
  105. }
  106. $date_range_label = "$min_year - $max_year";
  107. $min_dts = "$min_year-01-01";
  108. $max_dts = "$max_year-12-31 23:59:59";
  109. break;
  110. case "custom":
  111. $check = strcmp($date_start, $date_end);
  112. if($check > 0) {
  113. $temp = $date_end;
  114. $date_start = $date_end;
  115. $date_start = $temp;
  116. }
  117. list($min_year, $min_month, $min_day) = explode("-", $date_start);
  118. list($max_year, $max_month, $max_day) = explode("-", $date_end);
  119. $min_dts = "{$date_start} 00:00:00";
  120. $max_dts = "{$date_end} 23:59:59";
  121. $date_range_label = "{$min_day}/{$min_month}/{$min_year} - {$max_day}/{$max_month}/{$max_year}";
  122. if(preg_match("/01-01$/", $date_start) && preg_match("/12-31$/", $date_end)) {
  123. $date_range_label = $min_year == $max_year ? $min_year : "{$min_year} - {$max_year}";
  124. }
  125. break;
  126. }
  127. $tests = $test_result_set->filterByDateRange($min_dts, $max_dts);
  128. // Get test values
  129. $test_ids = array_column($tests, "id");
  130. $test_values = TestValue::query([
  131. "test_id" => $test_ids,
  132. ]);
  133. $test_values_map = [];
  134. foreach($test_values as $test_value) {
  135. $test_id = $test_value->test_id;
  136. $name = $test_value->name;
  137. if(!isset($test_values_map[$test_id])) {
  138. $test_values_map[$test_id] = new stdClass;
  139. }
  140. $test_values_map[$test_id]->{$name} = $test_value->value;
  141. }
  142. // Load users
  143. User::load(array_column($tests, "tester_id"));
  144. // Load locations
  145. $locations = Location::load(array_column($tests, "location_id"));
  146. // Get region and area manager IDs
  147. $regions = array_filter(array_column($locations, "region"));
  148. $area_managers = array_filter(array_column($locations, "area_manager"));
  149. uksort($regions, "strnatcasecmp");
  150. uksort($area_managers, "strnatcasecmp");
  151. foreach($regions as $region) {
  152. $region_data[$region] = [];
  153. }
  154. foreach($area_managers as $area_manager) {
  155. $area_manager_data[$area_manager] = [];
  156. }
  157. $has_regions = !empty($region_data);
  158. $has_area_managers = !empty($area_manager_data);
  159. foreach($locations as $location) {
  160. if($has_regions && !$location->region) $locations_missing_region[] = $location;
  161. if($has_area_managers && !$location->area_manager) $locations_missing_area_manager[] = $location;
  162. }
  163. // Check if date range is same year
  164. $same_year = ($min_year == $max_year);
  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. $keys[] = "$year-$month";
  180. $key_labels[] = $label;
  181. }
  182. break;
  183. case "quarterly":
  184. default:
  185. $resolution_name = "Quarter";
  186. foreach(range(1, 4) as $quarter) {
  187. if(!$same_year) $label = "Q$quarter $year";
  188. else $label = "Q$quarter";
  189. $keys[] = "$year-$quarter";
  190. $key_labels[] = $label;
  191. }
  192. break;
  193. }
  194. }
  195. foreach($keys as $index => $key) {
  196. $label = $key_labels[$index];
  197. $data[$key] = clone $empty_data;
  198. $data[$key]->label = $label;
  199. foreach($region_data as &$r) {
  200. $r[$key] = clone $empty_data;
  201. $r[$key]->label = $label;
  202. }
  203. foreach($area_manager_data as &$a) {
  204. $a[$key] = clone $empty_data;
  205. $a[$key]->label = $label;
  206. }
  207. }
  208. $max_unix = 0;
  209. // Process test results
  210. foreach($tests as $test) {
  211. $tester = $test->getTester();
  212. $location = $test->getLocation();
  213. $project = $test->getProject();
  214. $test_values = $test_values_map[$test->id] ?? new stdClass;
  215. $region = $location->region;
  216. $area_manager = $location->area_manager;
  217. $dts = $project->dts_report ?: $test->dts_start;
  218. $dt = new DateTime($dts . " UTC");
  219. $dt->setTimezone(new DateTimeZone(TIMEZONE));
  220. $unix = $dt->format("U");
  221. $year = $dt->format("Y");
  222. $month = $dt->format("m");
  223. $quarter = ceil($month / 3);
  224. if($unix > $max_unix) {
  225. $max_unix = $unix;
  226. }
  227. switch($date_resolution) {
  228. case "monthly":
  229. $key = "$year-$month";
  230. break;
  231. case "quarterly":
  232. default:
  233. $key = "$year-$quarter";
  234. break;
  235. }
  236. $total_data->testCount++;
  237. $data[$key]->testCount++;
  238. if($region) $region_data[$region][$key]->testCount++;
  239. if($area_manager) $area_manager_data[$area_manager][$key]->testCount++;
  240. if($tester->gender == "M") {
  241. $data[$key]->maleTesterCount++;
  242. $total_data->maleTesterCount++;
  243. }
  244. if($tester->gender == "F") {
  245. $data[$key]->femaleTesterCount++;
  246. $total_data->femaleTesterCount++;
  247. }
  248. // Process test hook
  249. require $template->getProcessingFilePath("reports_process_test.php");
  250. }
  251. $current_month = date("m", $max_unix);
  252. $current_quarter = ceil(date("n", $max_unix) / 3);
  253. // Initialise generator
  254. $generator = new ChartGenerator($total_data, $data);
  255. $generator->setDateRangeLabel($date_range_label);
  256. $generator->setResolutionName($resolution_name);
  257. $generator->setPeriodKeysAndLabels($keys, $key_labels);
  258. $generator->setRegionData($region_data);
  259. $generator->setAreaManagerData($area_manager_data);
  260. // Build charts hook
  261. require $template->getProcessingFilePath("reports_build_charts.php");
  262. }
  263. catch(FormException $e) {
  264. $e->generateAlerts();
  265. }
  266. catch(Exception $e) {
  267. Core::addAlert("danger", $e->getMessage());
  268. }
  269. // Export hook
  270. if(!empty($action)) {
  271. switch($action) {
  272. case "export-pptx":
  273. case "export-odp":
  274. require "reports_presentation.php";
  275. break;
  276. default:
  277. break;
  278. }
  279. }
  280. }
  281. View::includeOnce("header", compact("page_title"));
  282. View::include("reports", compact("customer", "date_range", "date_start", "date_end", "date_resolution", "all_projects", "project_table_data", "exclude_project_ids", "location_ids", "retests", "charts", "data", "total_data", "locations_missing_region", "locations_missing_area_manager", "region_data", "area_manager_data", "key_labels", "has_pass_fail"));
  283. View::includeOnce("footer");