| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?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;
- private ?string $filter = null;
- private array $latestTests = [];
- 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;
- }
- public function getFilter(): ?string {
- return $this->filter;
- }
- public function setFilter(?string $filter): void {
- $this->filter = $filter;
- }
- /**
- * @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"]);
- $base_test_nvp = [
- "status" => Constraint::notEquals(Test::STATUS_CANCELLED),
- "location_id" => array_column($locations, "id")
- ];
- if(!$this->includeTested) {
- $test_nvp = $base_test_nvp;
- 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;
- }
- unset($tests);
- $locations = array_filter($locations, static function(Location $location) use ($location_test_flags) {
- return !isset($location_test_flags[$location->id]);
- });
- }
- $all_tests = Test::query($base_test_nvp, ["sort" => "dts_start", "order" => "asc"]);
- foreach($all_tests as $test) {
- $this->latestTests[$test->location_id] = $test;
- }
- return $locations;
- }
- public function getLatestTests(): array {
- return $this->latestTests;
- }
- }
|