reports.php 16 KB

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