location_search.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 bool $includeTested = true;
  20. private ?string $filter = null;
  21. private array $latestTests = [];
  22. public function __construct(Customer $customer) {
  23. $this->customer = $customer;
  24. }
  25. public function getId(): ?int {
  26. return $this->id;
  27. }
  28. public function setId(?int $id): void {
  29. $this->id = $id;
  30. }
  31. public function getIds(): ?array {
  32. return $this->ids;
  33. }
  34. public function setIds(?array $ids): void {
  35. $this->ids = $ids;
  36. }
  37. public function getInternalId(): ?string {
  38. return $this->internalId;
  39. }
  40. public function setInternalId(?string $internalId): void {
  41. $this->internalId = $internalId;
  42. }
  43. public function getName(): ?string {
  44. return $this->name;
  45. }
  46. public function setName(?string $name): void {
  47. $this->name = $name;
  48. }
  49. public function getLegalEntityName(): ?string {
  50. return $this->legalEntityName;
  51. }
  52. public function setLegalEntityName(?string $legalEntityName): void {
  53. $this->legalEntityName = $legalEntityName;
  54. }
  55. public function isIncludeArchived(): bool {
  56. return $this->includeArchived;
  57. }
  58. public function setIncludeArchived(bool $includeArchived): void {
  59. $this->includeArchived = $includeArchived;
  60. }
  61. public function isIncludeClosed(): bool {
  62. return $this->includeClosed;
  63. }
  64. public function setIncludeClosed(bool $includeClosed): void {
  65. $this->includeClosed = $includeClosed;
  66. }
  67. public function getMinTestDate(): ?string {
  68. return $this->minTestDate;
  69. }
  70. public function setMinTestDate(?string $minTestDate): void {
  71. $this->minTestDate = $minTestDate;
  72. }
  73. public function getMaxTestDate(): ?string {
  74. return $this->maxTestDate;
  75. }
  76. public function setMaxTestDate(?string $maxTestDate): void {
  77. $this->maxTestDate = $maxTestDate;
  78. }
  79. public function isIncludeTested(): bool {
  80. return $this->includeTested;
  81. }
  82. public function setIncludeTested(bool $includeTested): void {
  83. $this->includeTested = $includeTested;
  84. }
  85. public function getFilter(): ?string {
  86. return $this->filter;
  87. }
  88. public function setFilter(?string $filter): void {
  89. $this->filter = $filter;
  90. }
  91. /**
  92. * @return Location[]
  93. * @throws \Exception
  94. */
  95. public function run(): array {
  96. $nvp = [
  97. "customer_id" => $this->customer->id,
  98. "archived" => 0,
  99. "closed" => 0
  100. ];
  101. if($this->id) $nvp["id"] = $this->id;
  102. if($this->ids) $nvp["id"] = $this->ids;
  103. if($this->internalId) $nvp["internal_id"] = Db::escape($this->internalId);
  104. if($this->name) $nvp["name"] = Constraint::contains($this->name);
  105. if($this->legalEntityName) $nvp["legal_entity_name"] = Constraint::contains($this->legalEntityName);
  106. if($this->includeArchived) unset($nvp["archived"]);
  107. if($this->includeClosed) unset($nvp["closed"]);
  108. $locations = Location::query($nvp, ["sort" => "name", "order" => "asc"]);
  109. $base_test_nvp = [
  110. "status" => Constraint::notEquals(Test::STATUS_CANCELLED),
  111. "location_id" => array_column($locations, "id")
  112. ];
  113. if(!$this->includeTested) {
  114. $test_nvp = $base_test_nvp;
  115. if($this->minTestDate && $this->maxTestDate) {
  116. $test_nvp["dts_start"] = Constraint::between($this->minTestDate, $this->maxTestDate);
  117. }
  118. else {
  119. [$min_dts, $max_dts] = $this->customer->getYearCycleDates();
  120. $test_nvp["dts_start"] = Constraint::between($min_dts, $max_dts);
  121. }
  122. $tests = Test::query($test_nvp);
  123. $location_test_flags = [];
  124. foreach($tests as $test) {
  125. $location_test_flags[$test->location_id] = true;
  126. }
  127. unset($tests);
  128. $locations = array_filter($locations, static function(Location $location) use ($location_test_flags) {
  129. return !isset($location_test_flags[$location->id]);
  130. });
  131. }
  132. $all_tests = Test::query($base_test_nvp, ["sort" => "dts_start", "order" => "asc"]);
  133. foreach($all_tests as $test) {
  134. $this->latestTests[$test->location_id] = $test;
  135. }
  136. return $locations;
  137. }
  138. public function getLatestTests(): array {
  139. return $this->latestTests;
  140. }
  141. }