| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- <?php
- namespace Searches;
- use Constraint;
- use Customer;
- use DB;
- use Location;
- use Test;
- use User;
- class LocationSearch {
- private Customer $customer;
- private ?User $user = null;
- private ?int $id = null;
- private ?array $ids = null;
- private ?string $internalId = null;
- private ?string $name = null;
- private ?string $legalEntityName = null;
- private ?string $brandName = null;
- private ?string $regionName = null;
- private ?string $areaManagerName = null;
- private ?string $operationsManagerName = null;
- private ?string $gamingManagerName = null;
- private ?string $seniorGamingManagerName = 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 = [];
- private int $filterCount = 0;
- public function __construct(Customer $customer, ?User $user) {
- $this->customer = $customer;
- $this->user = $user;
- }
- 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;
- $this->filterCount++;
- }
- if($this->ids) {
- $nvp["id"] = $this->ids;
- $this->filterCount++;
- }
- if($this->internalId) {
- $nvp["internal_id"] = Constraint::contains($this->internalId);
- $this->filterCount++;
- }
- if($this->name) {
- $nvp["name"] = Constraint::contains($this->name);
- $this->filterCount++;
- }
- if($this->legalEntityName) {
- $nvp["legal_entity_name"] = Constraint::contains($this->legalEntityName);
- $this->filterCount++;
- }
- if($this->includeArchived) {
- unset($nvp["archived"]);
- $this->filterCount++;
- }
- if($this->includeClosed) {
- unset($nvp["closed"]);
- $this->filterCount++;
- }
- if($this->brandName) {
- $nvp["brand"] = $this->brandName;
- $this->filterCount++;
- }
- if($this->regionName) {
- $nvp["region"] = $this->regionName;
- $this->filterCount++;
- }
- if($this->areaManagerName) {
- $nvp["area_manager"] = $this->areaManagerName;
- $this->filterCount++;
- }
- if($this->operationsManagerName) {
- $nvp["operations_manager_name"] = $this->operationsManagerName;
- $this->filterCount++;
- }
- if($this->gamingManagerName) {
- $nvp["gaming_manager_name"] = $this->gamingManagerName;
- $this->filterCount++;
- }
- if($this->seniorGamingManagerName) {
- $nvp["senior_gaming_manager_name"] = $this->seniorGamingManagerName;
- $this->filterCount++;
- }
- $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->user && $this->user->level <= User::LEVEL_CUSTOMER) {
- $base_test_nvp["status"] = Test::STATUS_COMPLETED;
- }
- $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;
- }
- public function getBrandName(): ?string {
- return $this->brandName;
- }
- public function getRegionName(): ?string {
- return $this->regionName;
- }
- public function getAreaManagerName(): ?string {
- return $this->areaManagerName;
- }
- public function getOperationsManagerName(): ?string {
- return $this->operationsManagerName;
- }
- public function getGamingManagerName(): ?string {
- return $this->gamingManagerName;
- }
- public function getSeniorGamingManagerName(): ?string {
- return $this->seniorGamingManagerName;
- }
- public function setBrandName(?string $brandName): void {
- $this->brandName = $brandName;
- }
- public function setRegionName(?string $regionName): void {
- $this->regionName = $regionName;
- }
- public function setAreaManagerName(?string $areaManagerName): void {
- $this->areaManagerName = $areaManagerName;
- }
- public function setOperationsManagerName(?string $operationsManagerName): void {
- $this->operationsManagerName = $operationsManagerName;
- }
- public function setSeniorGamingManagerName(?string $seniorGamingManagerName): void {
- $this->seniorGamingManagerName = $seniorGamingManagerName;
- }
- public function setGamingManagerName(?string $gamingManagerName): void {
- $this->gamingManagerName = $gamingManagerName;
- }
- public function getFilterCount(): int {
- return $this->filterCount;
- }
- }
|