andWhere(['or', ['and', ['in', 'card.number', [$number]], "trim(coalesce(card.number, '')) <>'' "], ['and', ['in', 'card.rfid_key', [$number]], "trim(coalesce(card.rfid_key, '')) <>'' "], ]); $card = $query->one(); if (!isset($card)) { throw new NotFoundHttpException("Kártya nem található"); } $customer = $card->customer; if (!isset($customer)) { throw new NotFoundHttpException("Vendég nem található"); } if (isset($lastXDays)) { if (!is_numeric($lastXDays)) { throw new BadRequestHttpException("lastXDays paraméter hibás"); } if ($lastXDays > 6 || $lastXDays < 1) { throw new BadRequestHttpException("lastXDays paraméter érték hibás"); } } // check if has valid ticket today /** @var \common\models\Card $card */ $tickets = Ticket::readActive($card); $hasValidTicket = count($tickets) > 0; if (isset($lastXDays)) { // try to find any valid ticket in the lastXDays $minusDay = 1; while (!$hasValidTicket && $minusDay <= $lastXDays) { /** @var integer $minusDay */ $day = $this->getDateMinusDays($minusDay); $tickets = Ticket::readActive($card, $day); $hasValidTicket = count($tickets) > 0; $minusDay = $minusDay + 1; } } $result = [ 'discount' => $hasValidTicket ]; if (isset($customer)) { $result['card_number'] = $card->number; $result['name'] = $customer->name; } return $result; } /** * @param $minusDays * @return \DateTime * @throws \Exception */ private function getDateMinusDays($minusDays) { $date = new \DateTime('now'); $date->sub(new \DateInterval('P' . $minusDays . 'D')); $date->setTime(0, 0, 0); return $date; } }