reports.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. /**
  3. * @var Customer $customer
  4. * @var Chart[] $charts
  5. * @var string[] $key_labels
  6. * @var bool $has_pass_fail
  7. *
  8. * @var string $date_range
  9. * @var string $date_resolution
  10. * @var string $date_start
  11. * @var string $date_end
  12. *
  13. * @var int[] $location_ids
  14. * @var int $retests
  15. * @var int[] $exclude_project_ids
  16. *
  17. * @var array $project_table_data
  18. * @var Location[] $locations_missing_region
  19. * @var Location[] $locations_missing_area_manager
  20. * @var Location[] $locations_missing_brand
  21. *
  22. * @var array $total_data
  23. * @var array $region_data
  24. * @var array $area_manager_data
  25. * @var array $brand_data
  26. */
  27. $user = Core::$user;
  28. $date_resolutions = [
  29. "quarterly" => "Quarterly",
  30. "monthly" => "Monthly",
  31. ];
  32. $date_ranges = [
  33. "ytd" => "Year to Date",
  34. //"unbound" => "Unbound",
  35. "custom" => "Custom",
  36. ];
  37. $retests_options = [
  38. 0 => "Exclude",
  39. 1 => "Retests Only"
  40. ];
  41. $years = range(2018, date("Y"));
  42. $date_range_custom_style = ($date_range === "custom") ? "" : "display: none";
  43. $date_range_value = "";
  44. if($date_start && $date_end) {
  45. list($min_year, $min_month, $min_day) = explode("-", $date_start);
  46. list($max_year, $max_month, $max_day) = explode("-", $date_end);
  47. $date_range_value = "$min_day/$min_month/$min_year - $max_day/$max_month/$max_year";
  48. }
  49. ?>
  50. <script>
  51. let selectedProjectIds = <?=json_encode($exclude_project_ids)?>;
  52. let projectData = <?=json_encode($project_table_data)?>;
  53. </script>
  54. <div class="container mt-3 py-4">
  55. <div class="row d-print-none">
  56. <div class="col">
  57. <h2><?=htmlspecialchars($customer->name)?> Reports</h2>
  58. </div>
  59. <div class="col-auto">
  60. <?php
  61. if($charts) {
  62. ?>
  63. <div class="dropdown">
  64. <button type="button" class="btn btn-secondary dropdown-toggle" id="actions-button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  65. Actions
  66. </button>
  67. <div class="dropdown-menu" aria-labelledby="actions-button">
  68. <a class="dropdown-item" href="#" id="export-pptx-action">Export PPTX</a>
  69. </div>
  70. </div>
  71. <?php
  72. }
  73. ?>
  74. </div>
  75. </div>
  76. <form class="mb-4 d-print-none" method="post">
  77. <input type="hidden" name="customer_id" value="<?=htmlspecialchars($customer->id)?>" />
  78. <div class="row">
  79. <div class="col-md">
  80. <div class="form-group">
  81. <label class="form-label" for="date-range">
  82. Date Range
  83. </label><br / >
  84. <?php
  85. foreach($date_ranges as $value => $display) {
  86. $checked = ($value === $date_range) ? "checked" : "";
  87. ?>
  88. <div class="form-check form-check-inline">
  89. <input class="form-check-input" type="radio" name="date_range" id="date-range-<?=htmlspecialchars($value)?>" value="<?=htmlspecialchars($value)?>" <?=$checked?> data-date-range-radio-input />
  90. <label class="form-check-label" for="date-range-<?=htmlspecialchars($value)?>"><?=htmlspecialchars($display)?></label>
  91. </div>
  92. <?php
  93. }
  94. ?>
  95. <div class="row mt-1" id="date-range-custom-fields" style="<?=$date_range_custom_style?>">
  96. <div class="col-lg">
  97. <input class="form-control" type="text" id="date-range" value="<?=htmlspecialchars($date_range_value)?>" />
  98. <input type="hidden" name="date_start" id="date-start" value="<?=htmlspecialchars($date_start)?>" />
  99. <input type="hidden" name="date_end" id="date-end" value="<?=htmlspecialchars($date_end)?>" />
  100. </div>
  101. </div>
  102. </div>
  103. <div class="form-group">
  104. <label for="date-resolution">
  105. Date Resolution
  106. </label>
  107. <select class="form-control" name="date_resolution" id="date-resolution">
  108. <?php
  109. foreach($date_resolutions as $value => $display) {
  110. $selected = ($value === $date_resolution) ? "selected" : "";
  111. ?>
  112. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  113. <?php
  114. }
  115. ?>
  116. </select>
  117. </div>
  118. <div class="form-group">
  119. <label for="location-ids">
  120. Location IDs
  121. </label>
  122. <input class="form-control" name="location_ids" type="text" id="location-ids" value="<?=htmlspecialchars(implode(" ", $location_ids))?>" placeholder="Enter location IDs" />
  123. <small class="form-text text-muted">Separate each location ID with a space.<br />Leave blank to generate a report for all locations.</small>
  124. </div>
  125. <div class="form-group">
  126. <label for="retests">Retests</label>
  127. <select name="retests" id="retests" class="form-control">
  128. <option value="">Include</option>
  129. <?php
  130. foreach($retests_options as $value => $display) {
  131. $selected = $value === $retests ? "selected" : "";
  132. ?>
  133. <option value="<?=$value?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  134. <?php
  135. }
  136. ?>
  137. </select>
  138. </div>
  139. <button class="btn btn-primary" type="submit" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>">
  140. Submit
  141. </button>
  142. </div>
  143. <div class="col-md">
  144. <div class="form-group">
  145. <label class="d-block">
  146. Exclude Projects
  147. </label>
  148. <button id="show-projects-list-button" type="button" class="btn btn-secondary">
  149. <?=htmlspecialchars(__("Show project list"))?>
  150. </button>
  151. <div id="projects-table-wrapper" style="display: none;">
  152. <table class="table table-sm table-sm-text table-hover table-pointer" id="projects-table">
  153. <thead>
  154. <tr>
  155. <th>
  156. <label for="exclude-projects-header-checkbox" class="sr-only">
  157. <?=htmlspecialchars(__("Exclude All"))?>
  158. </label>
  159. <input id="exclude-projects-header-checkbox" type="checkbox" />
  160. </th>
  161. <th>ID</th>
  162. <th>Name</th>
  163. <th><span title="Time of Day" data-toggle="tooltip">TOD</span></th>
  164. <th>Tests</th>
  165. <th>Report Month</th>
  166. </tr>
  167. </thead>
  168. </table>
  169. </div>
  170. </div>
  171. </div>
  172. </div>
  173. </form>
  174. <?php
  175. if($user->canSeeMissingData()) {
  176. /**
  177. * @param string $field
  178. * @param Location[] $locations
  179. * @return void
  180. */
  181. $render_locations_missing_data = static function(string $field, array $locations): void {
  182. ?>
  183. <div class="alert alert-warning limit-height-1 d-print-none">
  184. There are some locations that are missing "<?=htmlspecialchars($field)?>" information:
  185. <ul class="mb-0">
  186. <?php
  187. foreach($locations as $location) {
  188. $url = (Core::$user->canAdmin()) ? "/account/locations/$location->id/edit" : "#";
  189. ?>
  190. <li>
  191. <a href="<?=htmlspecialchars($url)?>"><?=htmlspecialchars($location->name)?></a>
  192. <?php
  193. if($location->internal_id) {
  194. ?>
  195. <small class="text-info">(Internal ID: <?=htmlspecialchars($location->internal_id)?>)</small>
  196. <?php
  197. }
  198. ?>
  199. </li>
  200. <?php
  201. }
  202. ?>
  203. </ul>
  204. </div>
  205. <?php
  206. };
  207. if($locations_missing_region) {
  208. $render_locations_missing_data("region", $locations_missing_region);
  209. }
  210. if($locations_missing_area_manager) {
  211. $render_locations_missing_data("area manager", $locations_missing_area_manager);
  212. }
  213. if($locations_missing_brand) {
  214. $render_locations_missing_data("brand", $locations_missing_brand);
  215. }
  216. }
  217. if($charts) {
  218. ?>
  219. <h2>
  220. Report
  221. </h2>
  222. <p>
  223. <?php
  224. if($retests === 0) {
  225. ?>
  226. <span class="badge badge-warning">Retests Excluded</span>
  227. <?php
  228. }
  229. else if($retests === 1) {
  230. ?>
  231. <span class="badge badge-warning">Retests Only</span>
  232. <?php
  233. }
  234. else {
  235. ?>
  236. <span class="badge badge-success">Retests Included</span>
  237. <?php
  238. }
  239. ?>
  240. </p>
  241. <div class="row">
  242. <div class="col-md-6">
  243. <div class="card card-body mb-3">
  244. Test Count
  245. <div class="stat">
  246. <?=$total_data->testCount?>
  247. </div>
  248. </div>
  249. </div>
  250. <div class="col-md-6">
  251. <?php
  252. if(isset($total_data->passCount)) {
  253. $pass_percentage = 0;
  254. if($total_data->testCount > 0) {
  255. $pass_percentage = number_format(($total_data->passCount * 100) / $total_data->testCount, 2);
  256. }
  257. ?>
  258. <div class="card card-body mb-3">
  259. Pass/Fail Count
  260. <div class="stat">
  261. <?=$total_data->passCount?> /
  262. <?=$total_data->failCount?> (<?=$pass_percentage?>%)
  263. </div>
  264. </div>
  265. <?php
  266. }
  267. ?>
  268. </div>
  269. </div>
  270. <div class="row mb-4">
  271. <div class="col-md-4 d-print-none" style="position: relative;">
  272. <h3>Chart List</h3>
  273. <div class="list-group limit-height-2" style="position: sticky; top: 7rem;">
  274. <?php
  275. foreach($charts as $index => $chart) {
  276. ?>
  277. <a class="list-group-item list-group-item-action" href="#chart-<?=$index?>">
  278. <?=htmlspecialchars($chart->getTitle())?>
  279. </a>
  280. <?php
  281. }
  282. ?>
  283. </div>
  284. </div>
  285. <div class="col-md-8">
  286. <?php
  287. foreach($charts as $index => $chart) {
  288. if($index > 0) {
  289. ?>
  290. <br />
  291. <?php
  292. }
  293. if($chart->beforeHtml) {
  294. echo $chart->beforeHtml;
  295. }
  296. ?>
  297. <a id="chart-<?=$index?>" style="display: block; position: relative; top: -7rem; visibility: hidden;"></a>
  298. <canvas id="<?=$chart->getID()?>" data-chart="<?=htmlspecialchars($chart->getJSON())?>" width="400" height="300">
  299. </canvas>
  300. <?php
  301. if($chart->print) {
  302. $chart->printDataTable();
  303. }
  304. if($chart->afterHtml) {
  305. echo $chart->afterHtml;
  306. }
  307. }
  308. ?>
  309. </div>
  310. </div>
  311. <?php
  312. foreach(["pptx", "odp"] as $format) {
  313. ?>
  314. <form id="form-<?=$format?>" method="post" action="/account/reports/export-<?=$format?>" data-rendered="0">
  315. <input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
  316. <input type="hidden" name="customer_id" value="<?=intval($customer->id)?>" />
  317. <input type="hidden" name="date_range" value="<?=htmlspecialchars($date_range)?>" />
  318. <input type="hidden" name="date_start" id="date-start" value="<?=htmlspecialchars($date_start)?>" />
  319. <input type="hidden" name="date_end" id="date-end" value="<?=htmlspecialchars($date_end)?>" />
  320. <input type="hidden" name="date_resolution" value="<?=htmlspecialchars($date_resolution)?>" />
  321. <input type="hidden" name="retests" value="<?=htmlspecialchars($retests)?>" />
  322. <?php
  323. foreach($exclude_project_ids as $project_id) {
  324. ?>
  325. <input type="hidden" name="exclude_project_ids[]" value="<?=intval($project_id)?>" />
  326. <?php
  327. }
  328. ?>
  329. <input type="submit" value="Submit" class="d-none" />
  330. </form>
  331. <?php
  332. }
  333. if($has_pass_fail) {
  334. $tables = [
  335. (object) [
  336. "name" => "Region",
  337. "data" => $region_data
  338. ],
  339. (object) [
  340. "name" => "Area Manager",
  341. "data" => $area_manager_data
  342. ],
  343. (object) [
  344. "name" => "Brand",
  345. "data" => $brand_data
  346. ]
  347. ]
  348. ?>
  349. <div class="d-print-none">
  350. <h2>Extra Information</h2>
  351. <?php
  352. foreach($tables as $table) {
  353. $title = $table->name . " Data";
  354. $entity_type = $table->name;
  355. ?>
  356. <h3><?=htmlspecialchars($title)?></h3>
  357. <?php
  358. foreach($table->data as $entity_name => $rows) {
  359. ?>
  360. <h4><?=htmlspecialchars($entity_type)?>: <?=htmlspecialchars($entity_name)?></h4>
  361. <table class="table">
  362. <colgroup>
  363. <col style="width: 20%" />
  364. <col style="width: 20%" />
  365. <col style="width: 20%" />
  366. <col style="width: 20%" />
  367. <col style="width: 20%" />
  368. </colgroup>
  369. <thead>
  370. <tr>
  371. <th>Period</th>
  372. <th>Tests</th>
  373. <th>Passes</th>
  374. <th>Fails</th>
  375. <th>Rate</th>
  376. </tr>
  377. </thead>
  378. <tbody>
  379. <?php
  380. $index = 0;
  381. foreach($rows as $key => $data) {
  382. $rate = ($data->testCount == 0) ? "0.00" : number_format(($data->passCount * 100) / $data->testCount, 2);
  383. $label = $key_labels[$index];
  384. ?>
  385. <tr>
  386. <td><?=htmlspecialchars($label)?></td>
  387. <td><?=number_format($data->testCount)?></td>
  388. <td><?=number_format($data->passCount)?></td>
  389. <td><?=number_format($data->failCount)?></td>
  390. <td><?=$rate?>%</td>
  391. </tr>
  392. <?php
  393. $index++;
  394. }
  395. ?>
  396. </tbody>
  397. </table>
  398. <?php
  399. }
  400. }
  401. ?>
  402. </div>
  403. <?php
  404. }
  405. }
  406. ?>
  407. </div>