|
|
@@ -0,0 +1,159 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Searches;
|
|
|
+
|
|
|
+use Constraint;
|
|
|
+use Customer;
|
|
|
+use DB;
|
|
|
+use Location;
|
|
|
+use Test;
|
|
|
+
|
|
|
+class LocationSearch {
|
|
|
+
|
|
|
+ private Customer $customer;
|
|
|
+ private ?int $id = null;
|
|
|
+ private ?array $ids = null;
|
|
|
+ private ?string $internalId = null;
|
|
|
+ private ?string $name = null;
|
|
|
+ private ?string $legalEntityName = null;
|
|
|
+ private bool $includeArchived = false;
|
|
|
+ private bool $includeClosed = false;
|
|
|
+ private ?string $minTestDate = null;
|
|
|
+ private ?string $maxTestDate = null;
|
|
|
+ private bool $includeTested = true;
|
|
|
+
|
|
|
+ public function __construct(Customer $customer) {
|
|
|
+ $this->customer = $customer;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getId(): ?int {
|
|
|
+ return $this->id;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setId(?int $id): void {
|
|
|
+ $this->id = $id;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getIds(): ?array {
|
|
|
+ return $this->ids;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setIds(?array $ids): void {
|
|
|
+ $this->ids = $ids;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getInternalId(): ?string {
|
|
|
+ return $this->internalId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setInternalId(?string $internalId): void {
|
|
|
+ $this->internalId = $internalId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getName(): ?string {
|
|
|
+ return $this->name;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setName(?string $name): void {
|
|
|
+ $this->name = $name;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getLegalEntityName(): ?string {
|
|
|
+ return $this->legalEntityName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setLegalEntityName(?string $legalEntityName): void {
|
|
|
+ $this->legalEntityName = $legalEntityName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function isIncludeArchived(): bool {
|
|
|
+ return $this->includeArchived;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setIncludeArchived(bool $includeArchived): void {
|
|
|
+ $this->includeArchived = $includeArchived;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function isIncludeClosed(): bool {
|
|
|
+ return $this->includeClosed;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setIncludeClosed(bool $includeClosed): void {
|
|
|
+ $this->includeClosed = $includeClosed;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getMinTestDate(): ?string {
|
|
|
+ return $this->minTestDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setMinTestDate(?string $minTestDate): void {
|
|
|
+ $this->minTestDate = $minTestDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getMaxTestDate(): ?string {
|
|
|
+ return $this->maxTestDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setMaxTestDate(?string $maxTestDate): void {
|
|
|
+ $this->maxTestDate = $maxTestDate;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function isIncludeTested(): bool {
|
|
|
+ return $this->includeTested;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setIncludeTested(bool $includeTested): void {
|
|
|
+ $this->includeTested = $includeTested;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return Location[]
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function run(): array {
|
|
|
+ $nvp = [
|
|
|
+ "customer_id" => $this->customer->id,
|
|
|
+ "archived" => 0,
|
|
|
+ "closed" => 0
|
|
|
+ ];
|
|
|
+
|
|
|
+ if($this->id) $nvp["id"] = $this->id;
|
|
|
+ if($this->ids) $nvp["id"] = $this->ids;
|
|
|
+ if($this->internalId) $nvp["internal_id"] = Db::escape($this->internalId);
|
|
|
+ if($this->name) $nvp["name"] = Constraint::contains($this->name);
|
|
|
+ if($this->legalEntityName) $nvp["legal_entity_name"] = Constraint::contains($this->legalEntityName);
|
|
|
+ if($this->includeArchived) unset($nvp["archived"]);
|
|
|
+ if($this->includeClosed) unset($nvp["closed"]);
|
|
|
+
|
|
|
+ $locations = Location::query($nvp, ["sort" => "name", "order" => "asc"]);
|
|
|
+
|
|
|
+ if(!$this->includeTested) {
|
|
|
+ $test_nvp = [
|
|
|
+ "status" => Constraint::notEquals(Test::STATUS_CANCELLED),
|
|
|
+ "location_id" => array_column($locations, "id")
|
|
|
+ ];
|
|
|
+
|
|
|
+ if($this->minTestDate && $this->maxTestDate) {
|
|
|
+ $test_nvp["dts_start"] = Constraint::between($this->minTestDate, $this->maxTestDate);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ [$min_dts, $max_dts] = $this->customer->getYearCycleDates();
|
|
|
+ $test_nvp["dts_start"] = Constraint::between($min_dts, $max_dts);
|
|
|
+ }
|
|
|
+
|
|
|
+ $tests = Test::query($test_nvp);
|
|
|
+
|
|
|
+ $location_test_flags = [];
|
|
|
+ foreach($tests as $test) {
|
|
|
+ $location_test_flags[$test->location_id] = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ $locations = array_filter($locations, static function(Location $location) use ($location_test_flags) {
|
|
|
+ return !isset($location_test_flags[$location->id]);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ return $locations;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|