namespace app\controllers;
use Yii;
+use yii\db\Query;
+use yii_app\api3\modules\v1\models\Sales;
use yii_app\records\Products1cAdditionalCharacteristics;
use yii_app\records\Products1cNomenclature;
use yii_app\records\Products1cNomenclatureActuality;
]);
}
+ public function actionAddActivity()
+ {
+ $request = Yii::$app->request;
+
+ if (!$request->get('historyDays') || !$request->get('intervalMonths')) {
+ return $this->render('add-activity', [
+ 'historyDays' => $historyDays ?? 14,
+ 'intervalMonths' => $intervalMonths ?? 4,
+ ]);
+ }
+
+ $historyDays = (int)$request->get('historyDays');
+ $intervalMonths = (int)$request->get('intervalMonths');
+
+ $endDate = date('Y-m-d');
+ $startDate = date('Y-m-d', strtotime("-{$historyDays} days", strtotime($endDate)));
+
+ $storeIds = array_map(fn($s) => $s->id, $this->getVisibleStores());
+ $countStores = count($storeIds);
+
+ $productIds = (new Query())
+ ->select('sp.product_id')
+ ->from(['s' => 'sales'])
+ ->innerJoin(['sp' => 'sales_products'], 's.id = sp.check_id')
+ ->andWhere(['s.store_id' => $storeIds])
+ ->andWhere(['between', 's.date', "{$startDate} 00:00:00", "{$endDate} 23:59:59"])
+ ->groupBy('sp.product_id')
+ ->having(['=', new \yii\db\Expression('COUNT(DISTINCT s.store_id)'), $countStores])
+ ->column();
+
+ if (empty($productIds)) {
+ Yii::$app->session->setFlash('info', 'Нет товаров, удовлетворяющих условиям.');
+ return $this->render('add-activity', [
+ 'historyDays' => $historyDays,
+ 'intervalMonths' => $intervalMonths,
+ ]);
+ }
+
+ $now = new \DateTime();
+ $from = (clone $now)->modify("-{$intervalMonths} months")
+ ->modify('first day of this month')->setTime(0,0,0)
+ ->format('Y-m-d H:i:s');
+ $to = (clone $now)->modify("+{$intervalMonths} months")
+ ->modify('last day of this month')->setTime(23,59,59)
+ ->format('Y-m-d H:i:s');
+
+ $userId = Yii::$app->user->id;
+ $createdAt = date('Y-m-d H:i:s');
+ $rows = [];
+ foreach ($productIds as $pid) {
+ $rows[] = [
+ 'guid' => $pid,
+ 'date_from' => $from,
+ 'date_end' => $to,
+ 'active' => 1,
+ 'created_at' => $createdAt,
+ 'created_by' => $userId,
+ ];
+ }
+
+ Yii::$app->db->createCommand()
+ ->batchInsert(
+ Products1cNomenclatureActuality::tableName(),
+ ['guid','date_from','date_end','active','created_at','created_by'],
+ $rows
+ )
+ ->execute();
+
+ Yii::$app->session->setFlash('success', 'Таблица актуальности заполнена.');
+ return $this->render('add-activity', [
+ 'historyDays' => $historyDays,
+ 'intervalMonths' => $intervalMonths,
+ ]);
+ }
+
/**
--- /dev/null
+<?php
+// Вид: views/products1c-nomenclature-actuality/add-activity.php
+/** @var yii\web\View $this */
+/** @var int $historyDays */
+/** @var int $intervalMonths */
+use yii\helpers\Html;
+use kartik\form\ActiveForm;
+
+$this->title = 'Заполнить актуальность';
+?>
+<div class="add-activity p-4">
+<h1><?= Html::encode($this->title) ?></h1>
+
+<?php $form = ActiveForm::begin([
+ 'method' => 'get',
+ 'action' => ['add-activity'],
+ 'options' => ['class' => 'form-inline'],
+]); ?>
+
+<div class="row g-3 align-items-end">
+ <div class="col-auto">
+ <?= $form->field(new \yii\base\DynamicModel(['historyDays'=>$historyDays]), 'historyDays')
+ ->textInput(['type'=>'number','min'=>1,'value'=>$historyDays])
+ ->label('История (дней)') ?>
+ </div>
+ <div class="col-auto">
+ <?= $form->field(new \yii\base\DynamicModel(['intervalMonths'=>$intervalMonths]), 'intervalMonths')
+ ->textInput(['type'=>'number','min'=>1,'value'=>$intervalMonths])
+ ->label('Интервал (месяцы)') ?>
+ </div>
+ <div class="col-auto">
+ <?= Html::submitButton('Запустить', ['class'=>'btn btn-success']) ?>
+ </div>
+ <?php ActiveForm::end(); ?>
+</div>
\ No newline at end of file