reports.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var Customer $customer
  4. * @var Chart[] $charts
  5. * @var string[] $key_labels
  6. * @var bool $has_pass_fail
  7. *
  8. * @var array $exclude_categories
  9. * @var object $template_config
  10. *
  11. * @var string $date_range
  12. * @var string $date_resolution
  13. * @var string $date_start
  14. * @var string $date_end
  15. *
  16. * @var int[] $location_ids
  17. * @var int $retests
  18. * @var int[] $exclude_project_ids
  19. *
  20. * @var array $project_table_data
  21. * @var Location[] $locations_missing_region
  22. * @var Location[] $locations_missing_area_manager
  23. * @var Location[] $locations_missing_brand
  24. * @var Location[] $locations_missing_operations_manager
  25. *
  26. * @var array $total_data
  27. * @var array $region_data
  28. * @var array $area_manager_data
  29. * @var array $brand_data
  30. * @var array $operations_manager_data
  31. */
  32. $user = Core::$user;
  33. $date_resolutions = [
  34. "quarterly" => "Quarterly",
  35. "monthly" => "Monthly",
  36. ];
  37. $date_ranges = [
  38. "ytd" => "Year to Date",
  39. //"unbound" => "Unbound",
  40. "custom" => "Custom",
  41. ];
  42. $retests_options = [
  43. 0 => "Exclude",
  44. 1 => "Retests Only"
  45. ];
  46. $years = range(2018, date("Y"));
  47. $date_range_custom_style = ($date_range === "custom") ? "" : "display: none";
  48. $date_range_value = "";
  49. if($date_start && $date_end) {
  50. [$min_year, $min_month, $min_day] = explode("-", $date_start);
  51. [$max_year, $max_month, $max_day] = explode("-", $date_end);
  52. $date_range_value = "$min_day/$min_month/$min_year - $max_day/$max_month/$max_year";
  53. }
  54. $brands = $customer->getBrands();
  55. $regions = $customer->getRegions();
  56. $area_managers = $customer->getAreaManagerNames();
  57. $operations_managers = $customer->getOperationsManagerNames();
  58. $gaming_managers = $customer->getGamingManagerNames();
  59. $senior_gaming_managers = $customer->getSeniorGamingManagerNames();
  60. ?>
  61. <script>
  62. let selectedProjectIds = <?=json_encode($exclude_project_ids)?>;
  63. let projectData = <?=json_encode($project_table_data)?>;
  64. </script>
  65. <div class="container mt-3 py-4">
  66. <div class="row d-print-none">
  67. <div class="col">
  68. <h2><?=htmlspecialchars($customer->name)?> Reports</h2>
  69. </div>
  70. <div class="col-auto">
  71. <?php
  72. if($charts) {
  73. ?>
  74. <div class="dropdown">
  75. <button type="button" class="btn btn-secondary dropdown-toggle" id="actions-button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  76. Actions
  77. </button>
  78. <div class="dropdown-menu" aria-labelledby="actions-button">
  79. <a class="dropdown-item" href="#" id="export-pptx-action">Export PPTX</a>
  80. </div>
  81. </div>
  82. <?php
  83. }
  84. ?>
  85. </div>
  86. </div>
  87. <form class="mb-4 d-print-none" method="post">
  88. <input type="hidden" name="customer_id" value="<?=htmlspecialchars($customer->id)?>" />
  89. <div class="row">
  90. <div class="col-md">
  91. <div class="form-group">
  92. <label class="form-label" for="date-range">
  93. Date Range
  94. </label><br />
  95. <?php
  96. foreach($date_ranges as $value => $display) {
  97. $checked = ($value === $date_range) ? "checked" : "";
  98. ?>
  99. <div class="form-check form-check-inline">
  100. <input class="form-check-input" type="radio" name="date_range" id="date-range-<?=htmlspecialchars($value)?>" value="<?=htmlspecialchars($value)?>" <?=$checked?> data-date-range-radio-input />
  101. <label class="form-check-label" for="date-range-<?=htmlspecialchars($value)?>"><?=htmlspecialchars($display)?></label>
  102. </div>
  103. <?php
  104. }
  105. ?>
  106. <div class="row mt-1" id="date-range-custom-fields" style="<?=$date_range_custom_style?>">
  107. <div class="col-lg">
  108. <input class="form-control" type="text" id="date-range" value="<?=htmlspecialchars($date_range_value)?>" />
  109. <input type="hidden" name="date_start" id="date-start" value="<?=htmlspecialchars($date_start)?>" />
  110. <input type="hidden" name="date_end" id="date-end" value="<?=htmlspecialchars($date_end)?>" />
  111. </div>
  112. </div>
  113. </div>
  114. <div class="form-group">
  115. <label for="date-resolution">
  116. Date Resolution
  117. </label>
  118. <select class="form-control" name="date_resolution" id="date-resolution">
  119. <?php
  120. foreach($date_resolutions as $value => $display) {
  121. $selected = ($value === $date_resolution) ? "selected" : "";
  122. ?>
  123. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  124. <?php
  125. }
  126. ?>
  127. </select>
  128. </div>
  129. <div class="form-group">
  130. <label for="location-ids">
  131. Location IDs
  132. </label>
  133. <input class="form-control" name="location_ids" type="text" id="location-ids" value="<?=htmlspecialchars(implode(" ", $location_ids))?>" placeholder="Enter location IDs" />
  134. <small class="form-text text-muted">Separate each location ID with a space.<br />Leave blank to generate a report for all locations.</small>
  135. </div>
  136. <div class="form-group">
  137. <label for="retests">Retests</label>
  138. <select name="retests" id="retests" class="form-control">
  139. <option value="">Include</option>
  140. <?php
  141. foreach($retests_options as $value => $display) {
  142. $selected = $value === $retests ? "selected" : "";
  143. ?>
  144. <option value="<?=$value?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
  145. <?php
  146. }
  147. ?>
  148. </select>
  149. </div>
  150. <?php
  151. $categories = $template_config->reports->categories ?? [];
  152. if($total_data && $categories) {
  153. ?>
  154. <div class="form-group">
  155. <label><?=htmlspecialchars(__("Exclude Categories"))?></label><br />
  156. <?php
  157. foreach($categories as $category) {
  158. $id = sprintf("exclude-category-%s", strtolower($category));
  159. $checked = in_array($category, $exclude_categories) ? "checked" : "";
  160. ?>
  161. <div class="custom-control custom-switch">
  162. <input class="custom-control-input" id="<?=$id?>" name="exclude_categories[]" type="checkbox" value="<?=htmlspecialchars($category)?>" <?=$checked?> />
  163. <label class="custom-control-label" for="<?=$id?>"><?=htmlspecialchars($category)?></label>
  164. </div>
  165. <?php
  166. }
  167. ?>
  168. </div>
  169. <?php
  170. }
  171. ?>
  172. <?php
  173. /**
  174. * @param string $name
  175. * @param string $id
  176. * @param string $label
  177. * @param array $values
  178. * @return void
  179. * @throws Exception
  180. */
  181. $render_location_entity_filter = static function(string $name, string $id, string $label, array $values): void {
  182. if(!$values) {
  183. return;
  184. }
  185. $request_name = preg_replace("/\[]/", "", $name);
  186. $selected_values = http::value($request_name, "trim") ?? [];
  187. ?>
  188. <div class="form-group">
  189. <label for="<?=$id?>"><?=htmlspecialchars($label)?></label>
  190. <select class="form-control" name="<?=$name?>" id="<?=$id?>" data-toggle="select2" data-placeholder="<?=htmlspecialchars(__("All"))?>" multiple>
  191. <?php
  192. foreach($values as $value) {
  193. $selected = in_array($value, $selected_values) ? "selected" : ""
  194. ?>
  195. <option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($value)?></option>
  196. <?php
  197. }
  198. ?>
  199. </select>
  200. </div>
  201. <?php
  202. };
  203. ?>
  204. </div>
  205. <div class="col-md">
  206. <?php
  207. $render_location_entity_filter("regions[]", "regions", __("Regions"), $regions);
  208. $render_location_entity_filter("area_managers[]", "area_managers", __("Area Managers"), $area_managers);
  209. $render_location_entity_filter("operations_managers[]", "operation_managers", __("Operations Managers"), $operations_managers);
  210. $render_location_entity_filter("gaming_managers[]", "gaming_managers", __("Gaming Managers"), $gaming_managers);
  211. $render_location_entity_filter("senior_gaming_managers[]", "senior_gaming_managers", __("Senior Gaming Managers"), $senior_gaming_managers);
  212. $render_location_entity_filter("brands[]", "brands", __("Brands"), $brands);
  213. ?>
  214. <div class="form-group">
  215. <label class="d-block">
  216. Exclude Projects
  217. </label>
  218. <button id="show-projects-list-button" type="button" class="btn btn-secondary">
  219. <?=htmlspecialchars(__("Show project list"))?>
  220. </button>
  221. <div id="projects-table-wrapper" style="display: none;">
  222. <table class="table table-sm table-sm-text table-hover table-pointer" id="projects-table">
  223. <thead>
  224. <tr>
  225. <th>
  226. <label for="exclude-projects-header-checkbox" class="sr-only">
  227. <?=htmlspecialchars(__("Exclude All"))?>
  228. </label>
  229. <input id="exclude-projects-header-checkbox" type="checkbox" />
  230. </th>
  231. <th>ID</th>
  232. <th>Name</th>
  233. <th><span title="Time of Day" data-toggle="tooltip">TOD</span></th>
  234. <th>Tests</th>
  235. <th>Report Month</th>
  236. </tr>
  237. </thead>
  238. </table>
  239. </div>
  240. </div>
  241. </div>
  242. </div>
  243. <button class="btn btn-primary" type="submit" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>">
  244. Submit
  245. </button>
  246. </form>
  247. <?php
  248. if($user->canSeeMissingData()) {
  249. /**
  250. * @param string $field
  251. * @param Location[] $locations
  252. * @return void
  253. */
  254. $render_locations_missing_data = static function(string $field, array $locations): void {
  255. ?>
  256. <div class="alert alert-warning limit-height-1 d-print-none">
  257. There are some locations that are missing "<?=htmlspecialchars($field)?>" information:
  258. <ul class="mb-0">
  259. <?php
  260. foreach($locations as $location) {
  261. $url = (Core::$user->canAdmin()) ? "/account/locations/$location->id/edit" : "#";
  262. ?>
  263. <li>
  264. <a href="<?=htmlspecialchars($url)?>"><?=htmlspecialchars($location->name)?></a>
  265. <?php
  266. if($location->internal_id) {
  267. ?>
  268. <small class="text-info">(Internal ID: <?=htmlspecialchars($location->internal_id)?>)</small>
  269. <?php
  270. }
  271. ?>
  272. </li>
  273. <?php
  274. }
  275. ?>
  276. </ul>
  277. </div>
  278. <?php
  279. };
  280. if($locations_missing_region) {
  281. $render_locations_missing_data("region", $locations_missing_region);
  282. }
  283. if($locations_missing_area_manager) {
  284. $render_locations_missing_data("area manager", $locations_missing_area_manager);
  285. }
  286. if($locations_missing_operations_manager) {
  287. $render_locations_missing_data("operations manager", $locations_missing_operations_manager);
  288. }
  289. if($locations_missing_brand) {
  290. $render_locations_missing_data("brand", $locations_missing_brand);
  291. }
  292. }
  293. if($charts) {
  294. ?>
  295. <h2>
  296. Report
  297. </h2>
  298. <p>
  299. <?php
  300. if($retests === 0) {
  301. ?>
  302. <span class="badge badge-warning">Retests Excluded</span>
  303. <?php
  304. }
  305. else if($retests === 1) {
  306. ?>
  307. <span class="badge badge-warning">Retests Only</span>
  308. <?php
  309. }
  310. else {
  311. ?>
  312. <span class="badge badge-success">Retests Included</span>
  313. <?php
  314. }
  315. ?>
  316. </p>
  317. <div class="row">
  318. <div class="col-md-6">
  319. <div class="card card-body mb-3">
  320. Test Count
  321. <div class="stat">
  322. <?=$total_data->testCount?>
  323. </div>
  324. </div>
  325. </div>
  326. <?php
  327. $render_pass_fail_card = static function(string $title, $pass, $fail) {
  328. $total = $pass + $fail;
  329. $pass_percentage = 0;
  330. if($total > 0) {
  331. $pass_percentage = number_format(($pass * 100) / $total, 2);
  332. }
  333. ?>
  334. <div class="col-md-6">
  335. <div class="card card-body mb-3">
  336. <?=htmlspecialchars($title)?>
  337. <div class="stat">
  338. <?=$pass?> /
  339. <?=$fail?> (<?=$pass_percentage?>%)
  340. </div>
  341. </div>
  342. </div>
  343. <?php
  344. };
  345. if(isset($total_data->passCount)) {
  346. $render_pass_fail_card("Gaming Pass / Fails", $total_data->passCount, $total_data->failCount);
  347. }
  348. if(isset($total_data->barCounts)) {
  349. $render_pass_fail_card("Alcohol Pass / Fails", $total_data->barCounts["pass"], $total_data->barCounts["fail"]);
  350. }
  351. if(isset($total_data->combinedCounts)) {
  352. $render_pass_fail_card("Combined Pass / Fails", $total_data->combinedCounts["pass"], $total_data->combinedCounts["fail"]);
  353. }
  354. ?>
  355. </div>
  356. <div class="row mb-4">
  357. <div class="col-md-4 d-print-none" style="position: relative;">
  358. <h3>Chart List</h3>
  359. <div class="list-group limit-height-2" style="position: sticky; top: 7rem;">
  360. <?php
  361. foreach($charts as $index => $chart) {
  362. ?>
  363. <a class="list-group-item list-group-item-action" href="#chart-<?=$index?>">
  364. <?=htmlspecialchars($chart->getTitle())?>
  365. </a>
  366. <?php
  367. }
  368. ?>
  369. </div>
  370. </div>
  371. <div class="col-md-8">
  372. <?php
  373. foreach($charts as $index => $chart) {
  374. if($index > 0) {
  375. ?>
  376. <br />
  377. <?php
  378. }
  379. if($chart->beforeHtml) {
  380. echo $chart->beforeHtml;
  381. }
  382. ?>
  383. <a id="chart-<?=$index?>" style="display: block; position: relative; top: -7rem; visibility: hidden;"></a>
  384. <canvas id="<?=$chart->getID()?>" data-chart="<?=htmlspecialchars($chart->getJSON())?>" width="400" height="300">
  385. </canvas>
  386. <?php
  387. if($chart->print) {
  388. $chart->printDataTable();
  389. }
  390. if($chart->afterHtml) {
  391. echo $chart->afterHtml;
  392. }
  393. }
  394. ?>
  395. </div>
  396. </div>
  397. <?php
  398. foreach(["pptx", "odp"] as $format) {
  399. ?>
  400. <form id="form-<?=$format?>" method="post" action="/account/reports/export-<?=$format?>" data-rendered="0">
  401. <input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
  402. <input type="hidden" name="customer_id" value="<?=intval($customer->id)?>" />
  403. <input type="hidden" name="date_range" value="<?=htmlspecialchars($date_range)?>" />
  404. <input type="hidden" name="date_start" id="date-start" value="<?=htmlspecialchars($date_start)?>" />
  405. <input type="hidden" name="date_end" id="date-end" value="<?=htmlspecialchars($date_end)?>" />
  406. <input type="hidden" name="date_resolution" value="<?=htmlspecialchars($date_resolution)?>" />
  407. <input type="hidden" name="retests" value="<?=htmlspecialchars($retests)?>" />
  408. <?php
  409. foreach($exclude_project_ids as $project_id) {
  410. ?>
  411. <input type="hidden" name="exclude_project_ids[]" value="<?=intval($project_id)?>" />
  412. <?php
  413. }
  414. ?>
  415. <input type="submit" value="Submit" class="d-none" />
  416. </form>
  417. <?php
  418. }
  419. if($has_pass_fail) {
  420. $tables = [
  421. (object) [
  422. "name" => "Region",
  423. "data" => $region_data
  424. ],
  425. (object) [
  426. "name" => "Area Manager",
  427. "data" => $area_manager_data
  428. ],
  429. (object) [
  430. "name" => "Operations Manager",
  431. "data" => $operations_manager_data
  432. ],
  433. (object) [
  434. "name" => "Brand",
  435. "data" => $brand_data
  436. ],
  437. ]
  438. ?>
  439. <div class="d-print-none">
  440. <h2>Extra Information</h2>
  441. <?php
  442. foreach($tables as $table) {
  443. $title = $table->name . " Data";
  444. $entity_type = $table->name;
  445. // Calculate totals
  446. $totals = [];
  447. foreach($table->data as $entity_name => $rows) {
  448. $totals[] = [
  449. "name" => $entity_name,
  450. "testCount" => array_sum(array_column($rows, "testCount")),
  451. "passCount" => array_sum(array_column($rows, "passCount")),
  452. "failCount" => array_sum(array_column($rows, "failCount")),
  453. ];
  454. }
  455. if(!$totals) {
  456. continue;
  457. }
  458. ?>
  459. <h3><?=htmlspecialchars($title)?></h3>
  460. <h4><?=htmlspecialchars($entity_type)?>: Totals</h4>
  461. <?php
  462. usort($totals, static function($a, $b) {
  463. return strnatcasecmp($a["name"], $b["name"]);
  464. });
  465. $total_test_count = 0;
  466. $total_pass_count = 0;
  467. $total_fail_count = 0;
  468. ?>
  469. <table class="table table-sm table-bordered">
  470. <colgroup>
  471. <col style="width: 20%" />
  472. <col style="width: 20%" />
  473. <col style="width: 20%" />
  474. <col style="width: 20%" />
  475. <col style="width: 20%" />
  476. </colgroup>
  477. <thead>
  478. <tr>
  479. <th><?=htmlspecialchars($title)?></th>
  480. <th>Tests</th>
  481. <th>Passes</th>
  482. <th>Fails</th>
  483. <th>Rate</th>
  484. </tr>
  485. </thead>
  486. <tbody>
  487. <?php
  488. foreach($totals as $data) {
  489. $total_test_count += $data["testCount"];
  490. $total_pass_count += $data["passCount"];
  491. $total_fail_count += $data["failCount"];
  492. $rate = ($data["testCount"] === 0) ? "0.00" : number_format(($data["passCount"] * 100) / $data["testCount"], 2);
  493. ?>
  494. <tr>
  495. <td><?=htmlspecialchars($data["name"])?></td>
  496. <td class="text-right"><?=number_format($data["testCount"])?></td>
  497. <td class="text-right"><?=number_format($data["passCount"])?></td>
  498. <td class="text-right"><?=number_format($data["failCount"])?></td>
  499. <td class="text-right"><?=$rate?>%</td>
  500. </tr>
  501. <?php
  502. }
  503. $total_rate = ($total_test_count === 0) ? "0.00" : number_format(($total_pass_count * 100) / $total_test_count, 2);
  504. ?>
  505. </tbody>
  506. <tfoot>
  507. <tr class="table-active">
  508. <th>
  509. Total
  510. </th>
  511. <th class="text-right">
  512. <?=number_format($total_test_count)?>
  513. </th>
  514. <th class="text-right">
  515. <?=number_format($total_pass_count)?>
  516. </th>
  517. <th class="text-right">
  518. <?=number_format($total_fail_count)?>
  519. </th>
  520. <th class="text-right"><?=$total_rate?>%</th>
  521. </tr>
  522. </tfoot>
  523. </table>
  524. <?php
  525. foreach($table->data as $entity_name => $rows) {
  526. ?>
  527. <h4><?=htmlspecialchars($entity_type)?>: <?=htmlspecialchars($entity_name)?></h4>
  528. <table class="table table-sm table-bordered">
  529. <colgroup>
  530. <col style="width: 20%" />
  531. <col style="width: 20%" />
  532. <col style="width: 20%" />
  533. <col style="width: 20%" />
  534. <col style="width: 20%" />
  535. </colgroup>
  536. <thead>
  537. <tr>
  538. <th>Period</th>
  539. <th>Tests</th>
  540. <th>Passes</th>
  541. <th>Fails</th>
  542. <th>Rate</th>
  543. </tr>
  544. </thead>
  545. <tbody>
  546. <?php
  547. $index = 0;
  548. foreach($rows as $key => $data) {
  549. $rate = ($data->testCount == 0) ? "0.00" : number_format(($data->passCount * 100) / $data->testCount, 2);
  550. $label = $key_labels[$index];
  551. ?>
  552. <tr>
  553. <td><?=htmlspecialchars($label)?></td>
  554. <td class="text-right"><?=number_format($data->testCount)?></td>
  555. <td class="text-right"><?=number_format($data->passCount)?></td>
  556. <td class="text-right"><?=number_format($data->failCount)?></td>
  557. <td class="text-right"><?=$rate?>%</td>
  558. </tr>
  559. <?php
  560. $index++;
  561. }
  562. ?>
  563. </tbody>
  564. </table>
  565. <?php
  566. }
  567. }
  568. ?>
  569. </div>
  570. <?php
  571. }
  572. }
  573. ?>
  574. </div>