}
if ($filter->date_from || $filter->date_to) {
- $hasDf = !empty($filter->date_from);
- $hasDt = !empty($filter->date_to);
+ $hasDateFrom = !empty($filter->date_from);
+ $hasDateTo = !empty($filter->date_to);
- $df = null;
- $dt = null;
+ $dateFrom = null;
+ $dateTo = null;
- if ($hasDf) {
- $df = (new \DateTime("{$filter->date_from}-01"))
+ if ($hasDateFrom) {
+ $dateFrom = (new \DateTime("{$filter->date_from}-01"))
->setTime(0, 0, 0)->format('Y-m-d H:i:s');
}
- if ($hasDt) {
- $dt = (new \DateTime("{$filter->date_to}-01"))
+ if ($hasDateTo) {
+ $dateTo = (new \DateTime("{$filter->date_to}-01"))
->modify('last day of this month')->setTime(23, 59, 59)
->format('Y-m-d H:i:s');
}
->alias('a')
->where('a.guid = n.id');
- if ($hasDf && !$hasDt) {
- $dateExists//->andWhere(['a.date_from' => $df])
- ->andWhere(['>=', 'a.date_to', $df]);;
- } elseif (!$hasDf && $hasDt) {
- $dateExists//->andWhere(['a.date_to' => $dt])
- ->andWhere(['<=', 'a.date_from', $dt]);
+ if ($hasDateFrom && !$hasDateTo) {
+ $dateExists//->andWhere(['a.date_from' => $dateFrom])
+ ->andWhere(['>=', 'a.date_to', $dateFrom]);;
+ } elseif (!$hasDateFrom && $hasDateTo) {
+ $dateExists//->andWhere(['a.date_to' => $dateTo])
+ ->andWhere(['<=', 'a.date_from', $dateTo]);
} else {
$dateExists
- ->andWhere(['>=', 'a.date_to', $df])
- ->andWhere(['<=', 'a.date_from', $dt]);
+ ->andWhere(['>=', 'a.date_to', $dateFrom])
+ ->andWhere(['<=', 'a.date_from', $dateTo]);
}
if (!empty($filter->onlyInactive)) {
$query->andWhere(['exists', $dateExists->select(new \yii\db\Expression('1'))]);
}
- $query->with(['actualities' => function ($q) use ($hasDf, $hasDt, $df, $dt) {
- if ($hasDf && !$hasDt) {
- $q//->andWhere(['date_from' => $df])
- ->andWhere(['>=', 'date_to', $df]);
- } elseif (!$hasDf && $hasDt) {
- $q//->andWhere(['date_to' => $dt])
- ->andWhere(['<=', 'date_from', $dt]);
+ $query->with(['actualities' => function ($subQuery) use ($hasDateFrom, $hasDateTo, $dateFrom, $dateTo) {
+ if ($hasDateFrom && !$hasDateTo) {
+ $subQuery//->andWhere(['date_from' => $dateFrom])
+ ->andWhere(['>=', 'date_to', $dateFrom]);
+ } elseif (!$hasDateFrom && $hasDateTo) {
+ $subQuery//->andWhere(['date_to' => $dateTo])
+ ->andWhere(['<=', 'date_from', $dateTo]);
} else {
- $q->andWhere(['>=', 'date_to', $df])
- ->andWhere(['<=', 'date_from', $dt]);
+ $subQuery->andWhere(['>=', 'date_to', $dateFrom])
+ ->andWhere(['<=', 'date_from', $dateTo]);
}
- $q->orderBy(['date_from' => SORT_ASC]);
+ $subQuery->orderBy(['date_from' => SORT_ASC]);
}]);
} else {
$query->with(['actualities' => function ($q) {
continue;
}
- $fromDt = \DateTime::createFromFormat('Y-m', $row['from']);
- $toDt = \DateTime::createFromFormat('Y-m', $row['to']);
- if (!$fromDt || !$toDt) {
+ $fromDate = \DateTime::createFromFormat('Y-m', $row['from']);
+ $toDate = \DateTime::createFromFormat('Y-m', $row['to']);
+ if (!$fromDate || !$toDate) {
continue;
}
- $fromDt->setDate((int)$fromDt->format('Y'), (int)$fromDt->format('m'), 1)
+ $fromDate->setDate((int)$fromDate->format('Y'), (int)$fromDate->format('m'), 1)
->setTime(0, 0, 0);
- $toDt->modify('last day of this month')
+ $toDate->modify('last day of this month')
->setTime(23, 59, 59);
- $from = $fromDt->format('Y-m-d H:i:s');
- $to = $toDt->format('Y-m-d H:i:s');
+ $from = $fromDate->format('Y-m-d H:i:s');
+ $to = $toDate->format('Y-m-d H:i:s');
if ($from > $to) {
Yii::warning("GUID {$row['guid']}: пропускаем — from > to");
$guid = $row['guid'];
- $fromAdj = (clone $fromDt)->modify('-1 second')->format('Y-m-d H:i:s');
- $toAdj = (clone $toDt)->modify('+1 second')->format('Y-m-d H:i:s');
+ $fromAdj = (clone $fromDate)->modify('-1 second')->format('Y-m-d H:i:s');
+ $toAdj = (clone $toDate)->modify('+1 second')->format('Y-m-d H:i:s');
/** @var Products1cNomenclatureActuality[] $hits */
$hits = Products1cNomenclatureActuality::find()