1
0

location_search.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace Searches;
  3. use Constraint;
  4. use Customer;
  5. use DB;
  6. use Location;
  7. use Test;
  8. use User;
  9. class LocationSearch {
  10. private Customer $customer;
  11. private ?User $user = null;
  12. private ?int $id = null;
  13. private ?array $ids = null;
  14. private ?string $internalId = null;
  15. private ?string $name = null;
  16. private ?string $legalEntityName = null;
  17. private ?string $brandName = null;
  18. private ?string $regionName = null;
  19. private ?string $areaManagerName = null;
  20. private ?string $operationsManagerName = null;
  21. private ?string $gamingManagerName = null;
  22. private ?string $seniorGamingManagerName = null;
  23. private bool $includeArchived = false;
  24. private bool $includeClosed = false;
  25. private ?string $minTestDate = null;
  26. private ?string $maxTestDate = null;
  27. private ?string $filter = null;
  28. /** @var Test[] */
  29. private array $liveTests = [];
  30. /** @var Test[] */
  31. private array $allTests = [];
  32. private int $filterCount = 0;
  33. public function __construct(Customer $customer, ?User $user) {
  34. $this->customer = $customer;
  35. $this->user = $user;
  36. }
  37. public function getId(): ?int {
  38. return $this->id;
  39. }
  40. public function setId(?int $id): void {
  41. $this->id = $id;
  42. }
  43. public function getIds(): ?array {
  44. return $this->ids;
  45. }
  46. public function setIds(?array $ids): void {
  47. $this->ids = $ids;
  48. }
  49. public function getInternalId(): ?string {
  50. return $this->internalId;
  51. }
  52. public function setInternalId(?string $internalId): void {
  53. $this->internalId = $internalId;
  54. }
  55. public function getName(): ?string {
  56. return $this->name;
  57. }
  58. public function setName(?string $name): void {
  59. $this->name = $name;
  60. }
  61. public function getLegalEntityName(): ?string {
  62. return $this->legalEntityName;
  63. }
  64. public function setLegalEntityName(?string $legalEntityName): void {
  65. $this->legalEntityName = $legalEntityName;
  66. }
  67. public function isIncludeArchived(): bool {
  68. return $this->includeArchived;
  69. }
  70. public function setIncludeArchived(bool $includeArchived): void {
  71. $this->includeArchived = $includeArchived;
  72. }
  73. public function isIncludeClosed(): bool {
  74. return $this->includeClosed;
  75. }
  76. public function setIncludeClosed(bool $includeClosed): void {
  77. $this->includeClosed = $includeClosed;
  78. }
  79. public function getMinTestDate(): ?string {
  80. return $this->minTestDate;
  81. }
  82. public function setMinTestDate(?string $minTestDate): void {
  83. $this->minTestDate = $minTestDate;
  84. }
  85. public function getMaxTestDate(): ?string {
  86. return $this->maxTestDate;
  87. }
  88. public function setMaxTestDate(?string $maxTestDate): void {
  89. $this->maxTestDate = $maxTestDate;
  90. }
  91. public function getFilter(): ?string {
  92. return $this->filter;
  93. }
  94. public function setFilter(?string $filter): void {
  95. $this->filter = $filter;
  96. }
  97. /**
  98. * @return Location[]
  99. * @throws \Exception
  100. */
  101. public function run(): array {
  102. $nvp = [
  103. "customer_id" => $this->customer->id,
  104. "archived" => 0,
  105. "closed" => 0
  106. ];
  107. if($this->id) {
  108. $nvp["id"] = $this->id;
  109. $this->filterCount++;
  110. }
  111. if($this->ids) {
  112. $nvp["id"] = $this->ids;
  113. $this->filterCount++;
  114. }
  115. if($this->internalId) {
  116. $nvp["internal_id"] = Constraint::contains($this->internalId);
  117. $this->filterCount++;
  118. }
  119. if($this->name) {
  120. $nvp["name"] = Constraint::contains($this->name);
  121. $this->filterCount++;
  122. }
  123. if($this->legalEntityName) {
  124. $nvp["legal_entity_name"] = Constraint::contains($this->legalEntityName);
  125. $this->filterCount++;
  126. }
  127. if($this->includeArchived) {
  128. unset($nvp["archived"]);
  129. $this->filterCount++;
  130. }
  131. if($this->includeClosed) {
  132. unset($nvp["closed"]);
  133. $this->filterCount++;
  134. }
  135. if($this->brandName) {
  136. $nvp["brand"] = $this->brandName;
  137. $this->filterCount++;
  138. }
  139. if($this->regionName) {
  140. $nvp["region"] = $this->regionName;
  141. $this->filterCount++;
  142. }
  143. if($this->areaManagerName) {
  144. $nvp["area_manager"] = $this->areaManagerName;
  145. $this->filterCount++;
  146. }
  147. if($this->operationsManagerName) {
  148. $nvp["operations_manager_name"] = $this->operationsManagerName;
  149. $this->filterCount++;
  150. }
  151. if($this->gamingManagerName) {
  152. $nvp["gaming_manager_name"] = $this->gamingManagerName;
  153. $this->filterCount++;
  154. }
  155. if($this->seniorGamingManagerName) {
  156. $nvp["senior_gaming_manager_name"] = $this->seniorGamingManagerName;
  157. $this->filterCount++;
  158. }
  159. $locations = Location::query($nvp, ["sort" => "name", "order" => "asc"]);
  160. $base_test_nvp = [
  161. "status" => Constraint::notEquals(Test::STATUS_CANCELLED),
  162. "location_id" => array_column($locations, "id")
  163. ];
  164. if($this->user && $this->user->level <= User::LEVEL_CUSTOMER) {
  165. $base_test_nvp["status"] = Test::STATUS_COMPLETED;
  166. }
  167. $this->populateAllTests($base_test_nvp);
  168. $this->populateLiveTests($base_test_nvp);
  169. if($this->filter) {
  170. $exclude_location_flags = [];
  171. $tests = $this->allTests;
  172. if(preg_match("/current$/", $this->filter)) {
  173. $tests = $this->liveTests;
  174. }
  175. switch($this->filter) {
  176. case "untested-current":
  177. case "untested-all":
  178. foreach($tests as $test) {
  179. $exclude_location_flags[$test->location_id] = true;
  180. }
  181. break;
  182. case "failed-current":
  183. case "failed-all":
  184. foreach($tests as $test) {
  185. if(!$test->isFail() || $test->isVoid()) {
  186. $exclude_location_flags[$test->location_id] = true;
  187. }
  188. }
  189. foreach($locations as $location) {
  190. if(!isset($tests[$location->id])) {
  191. $exclude_location_flags[$location->id] = true;
  192. }
  193. }
  194. break;
  195. }
  196. $locations = array_filter($locations, static function(Location $location) use ($exclude_location_flags) {
  197. return empty($exclude_location_flags[$location->id]);
  198. });
  199. }
  200. return $locations;
  201. }
  202. /**
  203. * @param array $nvp
  204. * @return Test[]
  205. * @throws \Exception
  206. */
  207. public function populateAllTests(array $nvp): array {
  208. $all_tests = Test::query($nvp, ["sort" => "dts_start", "order" => "asc"]);
  209. foreach($all_tests as $test) {
  210. $this->allTests[$test->location_id] = $test;
  211. }
  212. return $this->allTests;
  213. }
  214. /**
  215. * @param array $nvp
  216. * @return Test[]
  217. * @throws \Exception
  218. */
  219. public function populateLiveTests(array $nvp): array {
  220. $nvp["dts_start"] = $this->getDateTimeConstraint();
  221. $this->liveTests = Test::query($nvp);
  222. return $this->liveTests;
  223. }
  224. public function getAllTests(): array {
  225. return $this->allTests;
  226. }
  227. private function getDateTimeConstraint(): Constraint {
  228. if($this->minTestDate && $this->maxTestDate) {
  229. $constraint = Constraint::between($this->minTestDate, $this->maxTestDate);
  230. }
  231. else {
  232. $constraint = $this->customer->getYearCycleConstraint(TIMEZONE);
  233. }
  234. return $constraint;
  235. }
  236. public function getBrandName(): ?string {
  237. return $this->brandName;
  238. }
  239. public function getRegionName(): ?string {
  240. return $this->regionName;
  241. }
  242. public function getAreaManagerName(): ?string {
  243. return $this->areaManagerName;
  244. }
  245. public function getOperationsManagerName(): ?string {
  246. return $this->operationsManagerName;
  247. }
  248. public function getGamingManagerName(): ?string {
  249. return $this->gamingManagerName;
  250. }
  251. public function getSeniorGamingManagerName(): ?string {
  252. return $this->seniorGamingManagerName;
  253. }
  254. public function setBrandName(?string $brandName): void {
  255. $this->brandName = $brandName;
  256. }
  257. public function setRegionName(?string $regionName): void {
  258. $this->regionName = $regionName;
  259. }
  260. public function setAreaManagerName(?string $areaManagerName): void {
  261. $this->areaManagerName = $areaManagerName;
  262. }
  263. public function setOperationsManagerName(?string $operationsManagerName): void {
  264. $this->operationsManagerName = $operationsManagerName;
  265. }
  266. public function setSeniorGamingManagerName(?string $seniorGamingManagerName): void {
  267. $this->seniorGamingManagerName = $seniorGamingManagerName;
  268. }
  269. public function setGamingManagerName(?string $gamingManagerName): void {
  270. $this->gamingManagerName = $gamingManagerName;
  271. }
  272. public function getFilterCount(): int {
  273. return $this->filterCount;
  274. }
  275. }