| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?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 ?string $filter = null;
- /** @var Test[] */
- private array $liveTests = [];
- /** @var Test[] */
- private array $allTests = [];
- 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 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")
- ];
- $this->populateAllTests($base_test_nvp);
- $this->populateLiveTests($base_test_nvp);
- if($this->filter) {
- $exclude_location_flags = [];
- $tests = $this->allTests;
- if(preg_match("/current$/", $this->filter)) {
- $tests = $this->liveTests;
- }
- switch($this->filter) {
- case "untested-current":
- case "untested-all":
- foreach($tests as $test) {
- $exclude_location_flags[$test->location_id] = true;
- }
- break;
- case "failed-current":
- case "failed-all":
- foreach($tests as $test) {
- if(!$test->isFail() || $test->isVoid()) {
- $exclude_location_flags[$test->location_id] = true;
- }
- }
- foreach($locations as $location) {
- if(!isset($tests[$location->id])) {
- $exclude_location_flags[$location->id] = true;
- }
- }
- break;
- }
- $locations = array_filter($locations, static function(Location $location) use ($exclude_location_flags) {
- return empty($exclude_location_flags[$location->id]);
- });
- }
- return $locations;
- }
- /**
- * @param array $nvp
- * @return Test[]
- * @throws \Exception
- */
- public function populateAllTests(array $nvp): array {
- $all_tests = Test::query($nvp, ["sort" => "dts_start", "order" => "asc"]);
- foreach($all_tests as $test) {
- $this->allTests[$test->location_id] = $test;
- }
- return $this->allTests;
- }
- /**
- * @param array $nvp
- * @return Test[]
- * @throws \Exception
- */
- public function populateLiveTests(array $nvp): array {
- $nvp["dts_start"] = $this->getDateTimeConstraint();
- $this->liveTests = Test::query($nvp);
- return $this->liveTests;
- }
- public function getAllTests(): array {
- return $this->allTests;
- }
- private function getDateTimeConstraint(): Constraint {
- if($this->minTestDate && $this->maxTestDate) {
- $constraint = Constraint::between($this->minTestDate, $this->maxTestDate);
- }
- else {
- $constraint = $this->customer->getYearCycleConstraint(TIMEZONE);
- }
- return $constraint;
- }
- }
|