reports.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. <div class="col-md-6">
  327. <?php
  328. if(isset($total_data->passCount)) {
  329. $pass_percentage = 0;
  330. if($total_data->testCount > 0) {
  331. $pass_percentage = number_format(($total_data->passCount * 100) / $total_data->testCount, 2);
  332. }
  333. ?>
  334. <div class="card card-body mb-3">
  335. Pass/Fail Count
  336. <div class="stat">
  337. <?=$total_data->passCount?> /
  338. <?=$total_data->failCount?> (<?=$pass_percentage?>%)
  339. </div>
  340. </div>
  341. <?php
  342. }
  343. ?>
  344. </div>
  345. </div>
  346. <div class="row mb-4">
  347. <div class="col-md-4 d-print-none" style="position: relative;">
  348. <h3>Chart List</h3>
  349. <div class="list-group limit-height-2" style="position: sticky; top: 7rem;">
  350. <?php
  351. foreach($charts as $index => $chart) {
  352. ?>
  353. <a class="list-group-item list-group-item-action" href="#chart-<?=$index?>">
  354. <?=htmlspecialchars($chart->getTitle())?>
  355. </a>
  356. <?php
  357. }
  358. ?>
  359. </div>
  360. </div>
  361. <div class="col-md-8">
  362. <?php
  363. foreach($charts as $index => $chart) {
  364. if($index > 0) {
  365. ?>
  366. <br />
  367. <?php
  368. }
  369. if($chart->beforeHtml) {
  370. echo $chart->beforeHtml;
  371. }
  372. ?>
  373. <a id="chart-<?=$index?>" style="display: block; position: relative; top: -7rem; visibility: hidden;"></a>
  374. <canvas id="<?=$chart->getID()?>" data-chart="<?=htmlspecialchars($chart->getJSON())?>" width="400" height="300">
  375. </canvas>
  376. <?php
  377. if($chart->print) {
  378. $chart->printDataTable();
  379. }
  380. if($chart->afterHtml) {
  381. echo $chart->afterHtml;
  382. }
  383. }
  384. ?>
  385. </div>
  386. </div>
  387. <?php
  388. foreach(["pptx", "odp"] as $format) {
  389. ?>
  390. <form id="form-<?=$format?>" method="post" action="/account/reports/export-<?=$format?>" data-rendered="0">
  391. <input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
  392. <input type="hidden" name="customer_id" value="<?=intval($customer->id)?>" />
  393. <input type="hidden" name="date_range" value="<?=htmlspecialchars($date_range)?>" />
  394. <input type="hidden" name="date_start" id="date-start" value="<?=htmlspecialchars($date_start)?>" />
  395. <input type="hidden" name="date_end" id="date-end" value="<?=htmlspecialchars($date_end)?>" />
  396. <input type="hidden" name="date_resolution" value="<?=htmlspecialchars($date_resolution)?>" />
  397. <input type="hidden" name="retests" value="<?=htmlspecialchars($retests)?>" />
  398. <?php
  399. foreach($exclude_project_ids as $project_id) {
  400. ?>
  401. <input type="hidden" name="exclude_project_ids[]" value="<?=intval($project_id)?>" />
  402. <?php
  403. }
  404. ?>
  405. <input type="submit" value="Submit" class="d-none" />
  406. </form>
  407. <?php
  408. }
  409. if($has_pass_fail) {
  410. $tables = [
  411. (object) [
  412. "name" => "Region",
  413. "data" => $region_data
  414. ],
  415. (object) [
  416. "name" => "Area Manager",
  417. "data" => $area_manager_data
  418. ],
  419. (object) [
  420. "name" => "Operations Manager",
  421. "data" => $operations_manager_data
  422. ],
  423. (object) [
  424. "name" => "Brand",
  425. "data" => $brand_data
  426. ],
  427. ]
  428. ?>
  429. <div class="d-print-none">
  430. <h2>Extra Information</h2>
  431. <?php
  432. foreach($tables as $table) {
  433. $title = $table->name . " Data";
  434. $entity_type = $table->name;
  435. // Calculate totals
  436. $totals = [];
  437. foreach($table->data as $entity_name => $rows) {
  438. $totals[] = [
  439. "name" => $entity_name,
  440. "testCount" => array_sum(array_column($rows, "testCount")),
  441. "passCount" => array_sum(array_column($rows, "passCount")),
  442. "failCount" => array_sum(array_column($rows, "failCount")),
  443. ];
  444. }
  445. if(!$totals) {
  446. continue;
  447. }
  448. ?>
  449. <h3><?=htmlspecialchars($title)?></h3>
  450. <h4><?=htmlspecialchars($entity_type)?>: Totals</h4>
  451. <?php
  452. usort($totals, static function($a, $b) {
  453. return strnatcasecmp($a["name"], $b["name"]);
  454. });
  455. $total_test_count = 0;
  456. $total_pass_count = 0;
  457. $total_fail_count = 0;
  458. ?>
  459. <table class="table table-sm table-bordered">
  460. <colgroup>
  461. <col style="width: 20%" />
  462. <col style="width: 20%" />
  463. <col style="width: 20%" />
  464. <col style="width: 20%" />
  465. <col style="width: 20%" />
  466. </colgroup>
  467. <thead>
  468. <tr>
  469. <th><?=htmlspecialchars($title)?></th>
  470. <th>Tests</th>
  471. <th>Passes</th>
  472. <th>Fails</th>
  473. <th>Rate</th>
  474. </tr>
  475. </thead>
  476. <tbody>
  477. <?php
  478. foreach($totals as $data) {
  479. $total_test_count += $data["testCount"];
  480. $total_pass_count += $data["passCount"];
  481. $total_fail_count += $data["failCount"];
  482. $rate = ($data["testCount"] === 0) ? "0.00" : number_format(($data["passCount"] * 100) / $data["testCount"], 2);
  483. ?>
  484. <tr>
  485. <td><?=htmlspecialchars($data["name"])?></td>
  486. <td class="text-right"><?=number_format($data["testCount"])?></td>
  487. <td class="text-right"><?=number_format($data["passCount"])?></td>
  488. <td class="text-right"><?=number_format($data["failCount"])?></td>
  489. <td class="text-right"><?=$rate?>%</td>
  490. </tr>
  491. <?php
  492. }
  493. $total_rate = ($total_test_count === 0) ? "0.00" : number_format(($total_pass_count * 100) / $total_test_count, 2);
  494. ?>
  495. </tbody>
  496. <tfoot>
  497. <tr class="table-active">
  498. <th>
  499. Total
  500. </th>
  501. <th class="text-right">
  502. <?=number_format($total_test_count)?>
  503. </th>
  504. <th class="text-right">
  505. <?=number_format($total_pass_count)?>
  506. </th>
  507. <th class="text-right">
  508. <?=number_format($total_fail_count)?>
  509. </th>
  510. <th class="text-right"><?=$total_rate?>%</th>
  511. </tr>
  512. </tfoot>
  513. </table>
  514. <?php
  515. foreach($table->data as $entity_name => $rows) {
  516. ?>
  517. <h4><?=htmlspecialchars($entity_type)?>: <?=htmlspecialchars($entity_name)?></h4>
  518. <table class="table table-sm table-bordered">
  519. <colgroup>
  520. <col style="width: 20%" />
  521. <col style="width: 20%" />
  522. <col style="width: 20%" />
  523. <col style="width: 20%" />
  524. <col style="width: 20%" />
  525. </colgroup>
  526. <thead>
  527. <tr>
  528. <th>Period</th>
  529. <th>Tests</th>
  530. <th>Passes</th>
  531. <th>Fails</th>
  532. <th>Rate</th>
  533. </tr>
  534. </thead>
  535. <tbody>
  536. <?php
  537. $index = 0;
  538. foreach($rows as $key => $data) {
  539. $rate = ($data->testCount == 0) ? "0.00" : number_format(($data->passCount * 100) / $data->testCount, 2);
  540. $label = $key_labels[$index];
  541. ?>
  542. <tr>
  543. <td><?=htmlspecialchars($label)?></td>
  544. <td class="text-right"><?=number_format($data->testCount)?></td>
  545. <td class="text-right"><?=number_format($data->passCount)?></td>
  546. <td class="text-right"><?=number_format($data->failCount)?></td>
  547. <td class="text-right"><?=$rate?>%</td>
  548. </tr>
  549. <?php
  550. $index++;
  551. }
  552. ?>
  553. </tbody>
  554. </table>
  555. <?php
  556. }
  557. }
  558. ?>
  559. </div>
  560. <?php
  561. }
  562. }
  563. ?>
  564. </div>