location_search.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace Searches;
  3. use Constraint;
  4. use Customer;
  5. use DB;
  6. use Location;
  7. use Test;
  8. class LocationSearch {
  9. private Customer $customer;
  10. private ?int $id = null;
  11. private ?array $ids = null;
  12. private ?string $internalId = null;
  13. private ?string $name = null;
  14. private ?string $legalEntityName = null;
  15. private bool $includeArchived = false;
  16. private bool $includeClosed = false;
  17. private ?string $minTestDate = null;
  18. private ?string $maxTestDate = null;
  19. private ?string $filter = null;
  20. /** @var Test[] */
  21. private array $liveTests = [];
  22. /** @var Test[] */
  23. private array $allTests = [];
  24. public function __construct(Customer $customer) {
  25. $this->customer = $customer;
  26. }
  27. public function getId(): ?int {
  28. return $this->id;
  29. }
  30. public function setId(?int $id): void {
  31. $this->id = $id;
  32. }
  33. public function getIds(): ?array {
  34. return $this->ids;
  35. }
  36. public function setIds(?array $ids): void {
  37. $this->ids = $ids;
  38. }
  39. public function getInternalId(): ?string {
  40. return $this->internalId;
  41. }
  42. public function setInternalId(?string $internalId): void {
  43. $this->internalId = $internalId;
  44. }
  45. public function getName(): ?string {
  46. return $this->name;
  47. }
  48. public function setName(?string $name): void {
  49. $this->name = $name;
  50. }
  51. public function getLegalEntityName(): ?string {
  52. return $this->legalEntityName;
  53. }
  54. public function setLegalEntityName(?string $legalEntityName): void {
  55. $this->legalEntityName = $legalEntityName;
  56. }
  57. public function isIncludeArchived(): bool {
  58. return $this->includeArchived;
  59. }
  60. public function setIncludeArchived(bool $includeArchived): void {
  61. $this->includeArchived = $includeArchived;
  62. }
  63. public function isIncludeClosed(): bool {
  64. return $this->includeClosed;
  65. }
  66. public function setIncludeClosed(bool $includeClosed): void {
  67. $this->includeClosed = $includeClosed;
  68. }
  69. public function getMinTestDate(): ?string {
  70. return $this->minTestDate;
  71. }
  72. public function setMinTestDate(?string $minTestDate): void {
  73. $this->minTestDate = $minTestDate;
  74. }
  75. public function getMaxTestDate(): ?string {
  76. return $this->maxTestDate;
  77. }
  78. public function setMaxTestDate(?string $maxTestDate): void {
  79. $this->maxTestDate = $maxTestDate;
  80. }
  81. public function getFilter(): ?string {
  82. return $this->filter;
  83. }
  84. public function setFilter(?string $filter): void {
  85. $this->filter = $filter;
  86. }
  87. /**
  88. * @return Location[]
  89. * @throws \Exception
  90. */
  91. public function run(): array {
  92. $nvp = [
  93. "customer_id" => $this->customer->id,
  94. "archived" => 0,
  95. "closed" => 0
  96. ];
  97. if($this->id) $nvp["id"] = $this->id;
  98. if($this->ids) $nvp["id"] = $this->ids;
  99. if($this->internalId) $nvp["internal_id"] = Db::escape($this->internalId);
  100. if($this->name) $nvp["name"] = Constraint::contains($this->name);
  101. if($this->legalEntityName) $nvp["legal_entity_name"] = Constraint::contains($this->legalEntityName);
  102. if($this->includeArchived) unset($nvp["archived"]);
  103. if($this->includeClosed) unset($nvp["closed"]);
  104. $locations = Location::query($nvp, ["sort" => "name", "order" => "asc"]);
  105. $base_test_nvp = [
  106. "status" => Constraint::notEquals(Test::STATUS_CANCELLED),
  107. "location_id" => array_column($locations, "id")
  108. ];
  109. $this->populateAllTests($base_test_nvp);
  110. $this->populateLiveTests($base_test_nvp);
  111. if($this->filter) {
  112. $exclude_location_flags = [];
  113. $tests = $this->allTests;
  114. if(preg_match("/current$/", $this->filter)) {
  115. $tests = $this->liveTests;
  116. }
  117. switch($this->filter) {
  118. case "untested-current":
  119. case "untested-all":
  120. foreach($tests as $test) {
  121. $exclude_location_flags[$test->location_id] = true;
  122. }
  123. break;
  124. case "failed-current":
  125. case "failed-all":
  126. foreach($tests as $test) {
  127. if(!$test->isFail() || $test->isVoid()) {
  128. $exclude_location_flags[$test->location_id] = true;
  129. }
  130. }
  131. foreach($locations as $location) {
  132. if(!isset($tests[$location->id])) {
  133. $exclude_location_flags[$location->id] = true;
  134. }
  135. }
  136. break;
  137. }
  138. $locations = array_filter($locations, static function(Location $location) use ($exclude_location_flags) {
  139. return empty($exclude_location_flags[$location->id]);
  140. });
  141. }
  142. return $locations;
  143. }
  144. /**
  145. * @param array $nvp
  146. * @return Test[]
  147. * @throws \Exception
  148. */
  149. public function populateAllTests(array $nvp): array {
  150. $all_tests = Test::query($nvp, ["sort" => "dts_start", "order" => "asc"]);
  151. foreach($all_tests as $test) {
  152. $this->allTests[$test->location_id] = $test;
  153. }
  154. return $this->allTests;
  155. }
  156. /**
  157. * @param array $nvp
  158. * @return Test[]
  159. * @throws \Exception
  160. */
  161. public function populateLiveTests(array $nvp): array {
  162. $nvp["dts_start"] = $this->getDateTimeConstraint();
  163. $this->liveTests = Test::query($nvp);
  164. return $this->liveTests;
  165. }
  166. public function getAllTests(): array {
  167. return $this->allTests;
  168. }
  169. private function getDateTimeConstraint(): Constraint {
  170. if($this->minTestDate && $this->maxTestDate) {
  171. $constraint = Constraint::between($this->minTestDate, $this->maxTestDate);
  172. }
  173. else {
  174. $constraint = $this->customer->getYearCycleConstraint(TIMEZONE);
  175. }
  176. return $constraint;
  177. }
  178. }