location_search.php 3.7 KB

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