]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Загрузка из файла
authorVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 1 Oct 2025 13:42:26 +0000 (16:42 +0300)
committerVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 1 Oct 2025 13:42:26 +0000 (16:42 +0300)
erp24/controllers/MarketplacePricesController.php
erp24/controllers/MarketplacePricesLogController.php
erp24/records/MarketplacePricesLogSearch.php [new file with mode: 0644]
erp24/records/MarketplacePricesSearch.php [new file with mode: 0644]
erp24/views/marketplace-prices-log/_search.php [new file with mode: 0644]
erp24/views/marketplace-prices-log/index.php
erp24/views/marketplace-prices/_search.php [new file with mode: 0644]
erp24/views/marketplace-prices/create.php
erp24/views/marketplace-prices/index.php
erp24/views/marketplace-prices/update.php
erp24/views/marketplace-prices/view.php

index 1fafc4699a44a84c170f2e33334eac3ed14d6f18..adf30c6d0e2439ab9bcbae178f926bff63991989 100644 (file)
@@ -2,11 +2,18 @@
 
 namespace app\controllers;
 
+use PhpOffice\PhpSpreadsheet\IOFactory;
+use Yii;
+use yii\base\DynamicModel;
+use yii\web\UploadedFile;
 use yii_app\records\MarketplacePrices;
-use yii\data\ActiveDataProvider;
+use yii_app\records\MarketplacePricesSearch;
 use yii\web\Controller;
 use yii\web\NotFoundHttpException;
 use yii\filters\VerbFilter;
+use yii_app\records\MatrixBouquetForecast;
+use yii_app\records\MatrixErp;
+use yii_app\records\Products1c;
 
 /**
  * MarketplacePricesController implements the CRUD actions for MarketplacePrices model.
@@ -38,21 +45,62 @@ class MarketplacePricesController extends Controller
      */
     public function actionIndex()
     {
-        $dataProvider = new ActiveDataProvider([
-            'query' => MarketplacePrices::find(),
-            /*
-            'pagination' => [
-                'pageSize' => 50
-            ],
-            'sort' => [
-                'defaultOrder' => [
-                    'id' => SORT_DESC,
-                ]
-            ],
-            */
-        ]);
+        $model = new DynamicModel(['excelFile']);
+        $model->addRule('excelFile', 'file', ['extensions' => ['xls', 'xlsx'], 'skipOnEmpty' => false]);
+
+        if (Yii::$app->request->isPost) {
+            $model->excelFile = UploadedFile::getInstance($model, 'excelFile');
+
+            if ($model->validate()) {
+                $filePath = Yii::getAlias('@runtime') . '/import_' . uniqid() . '.' . $model->excelFile->extension;
+                $model->excelFile->saveAs($filePath);
+
+                $spreadsheet = IOFactory::load($filePath);
+                $rows = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
+                $mpPrices = MarketplacePrices::find()
+                    ->indexBy('matrix_erp_id')
+                    ->all();
+                $mpMap = ['YM' => 1, 'FW' => 2];
+
+                foreach ($rows as $row) {
+                    $mpAlias   = strtoupper(trim($row['A']));
+                    $articule  = trim($row['B']);
+                    $price     = (float) trim($row['C']);
+                    $oldPrice  = ($row['D'] !== '' ? (float) trim($row['D']) : null);
+
+                    $matrixProduct = MatrixErp::find()->where(['articule' => $articule])->one();
+                    if (!$matrixProduct) {
+                        continue;
+                    }
+
+                    $mpPrice = $mpPrices[$matrixProduct->id] ?? null;
+
+                    if ($mpPrice === null) {
+                        $mpPrice = new MarketplacePrices();
+                        $mpPrice->matrix_erp_id     = $matrixProduct->id;
+                        $mpPrice->marketplace_alias = $mpAlias;
+                        $mpPrice->marketplace_id    = $mpMap[$mpAlias] ?? null;
+                        $mpPrices[$matrixProduct->id] = $mpPrice;
+                    }
+
+                    $mpPrice->price     = $price;
+                    $mpPrice->old_price = $oldPrice;
+
+                    if (!$mpPrice->save()) {
+                        Yii::error("Ошибка создания цены: " . json_encode($mpPrice->getErrors()), __METHOD__);
+                    }
+
+                }
+
+                Yii::$app->session->setFlash('success', 'Импорт завершён: записи добавлены или обновлены.');
+            }
+        }
+        $searchModel = new MarketplacePricesSearch();
+        $dataProvider = $searchModel->search($this->request->queryParams);
 
         return $this->render('index', [
+            'searchModel' => $searchModel,
+            'model' => $model,
             'dataProvider' => $dataProvider,
         ]);
     }
index 63ae3c88e98160bcc8baa597fd0f1e71968c52f3..60967eb53215b1f786ab575e53481dc4160a1c8e 100644 (file)
@@ -3,7 +3,7 @@
 namespace app\controllers;
 
 use yii_app\records\MarketplacePricesLog;
-use yii\data\ActiveDataProvider;
+use yii_app\records\MarketplacePricesLogSearch;
 use yii\web\Controller;
 use yii\web\NotFoundHttpException;
 use yii\filters\VerbFilter;
@@ -38,21 +38,11 @@ class MarketplacePricesLogController extends Controller
      */
     public function actionIndex()
     {
-        $dataProvider = new ActiveDataProvider([
-            'query' => MarketplacePricesLog::find(),
-            /*
-            'pagination' => [
-                'pageSize' => 50
-            ],
-            'sort' => [
-                'defaultOrder' => [
-                    'id' => SORT_DESC,
-                ]
-            ],
-            */
-        ]);
+        $searchModel = new MarketplacePricesLogSearch();
+        $dataProvider = $searchModel->search($this->request->queryParams);
 
         return $this->render('index', [
+            'searchModel' => $searchModel,
             'dataProvider' => $dataProvider,
         ]);
     }
diff --git a/erp24/records/MarketplacePricesLogSearch.php b/erp24/records/MarketplacePricesLogSearch.php
new file mode 100644 (file)
index 0000000..c3f7575
--- /dev/null
@@ -0,0 +1,76 @@
+<?php
+
+namespace yii_app\records;
+
+use yii\base\Model;
+use yii\data\ActiveDataProvider;
+use yii_app\records\MarketplacePricesLog;
+
+/**
+ * MarketplacePricesLogSearch represents the model behind the search form of `yii_app\records\MarketplacePricesLog`.
+ */
+class MarketplacePricesLogSearch extends MarketplacePricesLog
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id', 'marketplace_prices_id', 'action', 'changed_by'], 'integer'],
+            [['changed_at'], 'safe'],
+            [['price_before', 'price_after', 'old_price_before', 'old_price_after'], 'number'],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function scenarios()
+    {
+        // bypass scenarios() implementation in the parent class
+        return Model::scenarios();
+    }
+
+    /**
+     * Creates data provider instance with search query applied
+     *
+     * @param array $params
+     * @param string|null $formName Form name to be used into `->load()` method.
+     *
+     * @return ActiveDataProvider
+     */
+    public function search($params, $formName = null)
+    {
+        $query = MarketplacePricesLog::find();
+
+        // add conditions that should always apply here
+
+        $dataProvider = new ActiveDataProvider([
+            'query' => $query,
+        ]);
+
+        $this->load($params, $formName);
+
+        if (!$this->validate()) {
+            // uncomment the following line if you do not want to return any records when validation fails
+            // $query->where('0=1');
+            return $dataProvider;
+        }
+
+        // grid filtering conditions
+        $query->andFilterWhere([
+            'id' => $this->id,
+            'marketplace_prices_id' => $this->marketplace_prices_id,
+            'action' => $this->action,
+            'changed_at' => $this->changed_at,
+            'changed_by' => $this->changed_by,
+            'price_before' => $this->price_before,
+            'price_after' => $this->price_after,
+            'old_price_before' => $this->old_price_before,
+            'old_price_after' => $this->old_price_after,
+        ]);
+
+        return $dataProvider;
+    }
+}
diff --git a/erp24/records/MarketplacePricesSearch.php b/erp24/records/MarketplacePricesSearch.php
new file mode 100644 (file)
index 0000000..6aeeeb0
--- /dev/null
@@ -0,0 +1,76 @@
+<?php
+
+namespace yii_app\records;
+
+use yii\base\Model;
+use yii\data\ActiveDataProvider;
+use yii_app\records\MarketplacePrices;
+
+/**
+ * MarketplacePricesSearch represents the model behind the search form of `yii_app\records\MarketplacePrices`.
+ */
+class MarketplacePricesSearch extends MarketplacePrices
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id', 'matrix_erp_id', 'marketplace_id'], 'integer'],
+            [['marketplace_alias', 'created_at', 'updated_at'], 'safe'],
+            [['price', 'old_price'], 'number'],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function scenarios()
+    {
+        // bypass scenarios() implementation in the parent class
+        return Model::scenarios();
+    }
+
+    /**
+     * Creates data provider instance with search query applied
+     *
+     * @param array $params
+     * @param string|null $formName Form name to be used into `->load()` method.
+     *
+     * @return ActiveDataProvider
+     */
+    public function search($params, $formName = null)
+    {
+        $query = MarketplacePrices::find();
+
+        // add conditions that should always apply here
+
+        $dataProvider = new ActiveDataProvider([
+            'query' => $query,
+        ]);
+
+        $this->load($params, $formName);
+
+        if (!$this->validate()) {
+            // uncomment the following line if you do not want to return any records when validation fails
+            // $query->where('0=1');
+            return $dataProvider;
+        }
+
+        // grid filtering conditions
+        $query->andFilterWhere([
+            'id' => $this->id,
+            'matrix_erp_id' => $this->matrix_erp_id,
+            'marketplace_id' => $this->marketplace_id,
+            'price' => $this->price,
+            'old_price' => $this->old_price,
+            'created_at' => $this->created_at,
+            'updated_at' => $this->updated_at,
+        ]);
+
+        $query->andFilterWhere(['ilike', 'marketplace_alias', $this->marketplace_alias]);
+
+        return $dataProvider;
+    }
+}
diff --git a/erp24/views/marketplace-prices-log/_search.php b/erp24/views/marketplace-prices-log/_search.php
new file mode 100644 (file)
index 0000000..678829c
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\MarketplacePricesLogSearch $model */
+/** @var yii\widgets\ActiveForm $form */
+?>
+
+<div class="marketplace-prices-log-search">
+
+    <?php $form = ActiveForm::begin([
+        'action' => ['index'],
+        'method' => 'get',
+    ]); ?>
+
+    <?= $form->field($model, 'id') ?>
+
+    <?= $form->field($model, 'marketplace_prices_id') ?>
+
+    <?= $form->field($model, 'action') ?>
+
+    <?= $form->field($model, 'changed_at') ?>
+
+    <?= $form->field($model, 'changed_by') ?>
+
+    <?php // echo $form->field($model, 'price_before') ?>
+
+    <?php // echo $form->field($model, 'price_after') ?>
+
+    <?php // echo $form->field($model, 'old_price_before') ?>
+
+    <?php // echo $form->field($model, 'old_price_after') ?>
+
+    <div class="form-group">
+        <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
+        <?= Html::resetButton('Reset', ['class' => 'btn btn-outline-secondary']) ?>
+    </div>
+
+    <?php ActiveForm::end(); ?>
+
+</div>
index 89fc713e5b8b279087498f0df872b4266acbc5a7..0221a0a56205d6950706758be95aeea251b76b3d 100644 (file)
@@ -7,40 +7,33 @@ use yii\grid\ActionColumn;
 use yii\grid\GridView;
 
 /** @var yii\web\View $this */
+/** @var yii_app\records\MarketplacePricesLogSearch $searchModel */
 /** @var yii\data\ActiveDataProvider $dataProvider */
 
-$this->title = 'Marketplace Prices Logs';
+$this->title = 'История цен для маркетплейсов';
 $this->params['breadcrumbs'][] = $this->title;
 ?>
-<div class="marketplace-prices-log-index">
-
+<div class="marketplace-prices-log-index px-4">
+    <?= Html::a('к ценам', ['/marketplace-prices/index'], ['class' => 'btn btn-primary my-4']) ?>
     <h1><?= Html::encode($this->title) ?></h1>
 
-    <p>
-        <?= Html::a('Create Marketplace Prices Log', ['create'], ['class' => 'btn btn-success']) ?>
-    </p>
-
+    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
 
     <?= GridView::widget([
         'dataProvider' => $dataProvider,
+        'filterModel' => $searchModel,
         'columns' => [
             ['class' => 'yii\grid\SerialColumn'],
 
             'id',
             'marketplace_prices_id',
             'action',
+            'price_before',
+            'price_after',
+            'old_price_before',
+            'old_price_after',
             'changed_at',
             'changed_by',
-            //'price_before',
-            //'price_after',
-            //'old_price_before',
-            //'old_price_after',
-            [
-                'class' => ActionColumn::className(),
-                'urlCreator' => function ($action, MarketplacePricesLog $model, $key, $index, $column) {
-                    return Url::toRoute([$action, 'id' => $model->id]);
-                 }
-            ],
         ],
     ]); ?>
 
diff --git a/erp24/views/marketplace-prices/_search.php b/erp24/views/marketplace-prices/_search.php
new file mode 100644 (file)
index 0000000..8ee8b97
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+use yii\helpers\Html;
+use yii\widgets\ActiveForm;
+
+/** @var yii\web\View $this */
+/** @var yii_app\records\MarketplacePricesSearch $model */
+/** @var yii\widgets\ActiveForm $form */
+?>
+
+<div class="marketplace-prices-search">
+
+    <?php $form = ActiveForm::begin([
+        'action' => ['index'],
+        'method' => 'get',
+    ]); ?>
+
+    <?= $form->field($model, 'id') ?>
+
+    <?= $form->field($model, 'matrix_erp_id') ?>
+
+    <?= $form->field($model, 'marketplace_id') ?>
+
+    <?= $form->field($model, 'marketplace_alias') ?>
+
+    <?= $form->field($model, 'price') ?>
+
+    <?php // echo $form->field($model, 'old_price') ?>
+
+    <?php // echo $form->field($model, 'created_at') ?>
+
+    <?php // echo $form->field($model, 'updated_at') ?>
+
+    <div class="form-group">
+        <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
+        <?= Html::resetButton('Reset', ['class' => 'btn btn-outline-secondary']) ?>
+    </div>
+
+    <?php ActiveForm::end(); ?>
+
+</div>
index 22a8b2e8b159f66e070b17af9338c0946aa1da94..ed8884135aeb48beb5d80809a93acc9701f51150 100644 (file)
@@ -5,12 +5,12 @@ use yii\helpers\Html;
 /** @var yii\web\View $this */
 /** @var yii_app\records\MarketplacePrices $model */
 
-$this->title = 'Create Marketplace Prices';
+$this->title = 'Создать цену';
 $this->params['breadcrumbs'][] = ['label' => 'Marketplace Prices', 'url' => ['index']];
 $this->params['breadcrumbs'][] = $this->title;
 ?>
-<div class="marketplace-prices-create">
-
+<div class="marketplace-prices-create px-4">
+    <?= Html::a('Назад', ['index'], ['class' => 'btn btn-primary my-4']) ?>
     <h1><?= Html::encode($this->title) ?></h1>
 
     <?= $this->render('_form', [
index f97103e162ea8fc0284c8ceaccdc594adff3e093..29c5c3cdb83e76567507c31187319f0d563b7589 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 
+use yii\widgets\ActiveForm;
 use yii_app\records\MarketplacePrices;
 use yii\helpers\Html;
 use yii\helpers\Url;
@@ -7,22 +8,31 @@ use yii\grid\ActionColumn;
 use yii\grid\GridView;
 
 /** @var yii\web\View $this */
+/** @var yii_app\records\MarketplacePricesSearch $searchModel */
 /** @var yii\data\ActiveDataProvider $dataProvider */
-
-$this->title = 'Marketplace Prices';
+/** @var \yii\base\DynamicModel $model */
+$this->title = 'Цены для маркетплейсов';
 $this->params['breadcrumbs'][] = $this->title;
 ?>
-<div class="marketplace-prices-index">
+<div class="marketplace-prices-index px-4">
 
     <h1><?= Html::encode($this->title) ?></h1>
 
     <p>
-        <?= Html::a('Create Marketplace Prices', ['create'], ['class' => 'btn btn-success']) ?>
+        <?= Html::a('Добавить цены', ['create'], ['class' => 'btn btn-success']) ?>
+        <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
+        <?= $form->field($model, 'excelFile')->fileInput() ?>
+    <div class="form-group">
+        <?= Html::submitButton('Загрузить', ['class' => 'btn btn-success']) ?>
+    </div>
+    <?php ActiveForm::end(); ?>
     </p>
 
+    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
 
     <?= GridView::widget([
         'dataProvider' => $dataProvider,
+        'filterModel' => $searchModel,
         'columns' => [
             ['class' => 'yii\grid\SerialColumn'],
 
@@ -31,11 +41,12 @@ $this->params['breadcrumbs'][] = $this->title;
             'marketplace_id',
             'marketplace_alias',
             'price',
-            //'old_price',
-            //'created_at',
-            //'updated_at',
+            'old_price',
+            'created_at',
+            'updated_at',
             [
-                'class' => ActionColumn::className(),
+                'template' => '{view} {update}',
+                'class' => ActionColumn::class,
                 'urlCreator' => function ($action, MarketplacePrices $model, $key, $index, $column) {
                     return Url::toRoute([$action, 'id' => $model->id]);
                  }
index a33c15559b10896f75265d39fffb2724250f94cb..51ddea3cf473e391fe743ce76b5a0db4da15a8d7 100644 (file)
@@ -5,13 +5,13 @@ use yii\helpers\Html;
 /** @var yii\web\View $this */
 /** @var yii_app\records\MarketplacePrices $model */
 
-$this->title = 'Update Marketplace Prices: ' . $model->id;
+$this->title = 'Изменить цену: ' . $model->id;
 $this->params['breadcrumbs'][] = ['label' => 'Marketplace Prices', 'url' => ['index']];
 $this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]];
 $this->params['breadcrumbs'][] = 'Update';
 ?>
-<div class="marketplace-prices-update">
-
+<div class="marketplace-prices-update px-4">
+    <?= Html::a('Назад', ['index'], ['class' => 'btn btn-primary my-4']) ?>
     <h1><?= Html::encode($this->title) ?></h1>
 
     <?= $this->render('_form', [
index 9fefbd78974d50f0a08a0c0b4c45988222ca533c..865b3f378ee5a0b6c66a158c67d8cc0f71e6fe61 100644 (file)
@@ -12,18 +12,12 @@ $this->params['breadcrumbs'][] = $this->title;
 \yii\web\YiiAsset::register($this);
 ?>
 <div class="marketplace-prices-view">
-
+    <?= Html::a('Назад', ['index'], ['class' => 'btn btn-primary my-4']) ?>
     <h1><?= Html::encode($this->title) ?></h1>
 
     <p>
-        <?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
-        <?= Html::a('Delete', ['delete', 'id' => $model->id], [
-            'class' => 'btn btn-danger',
-            'data' => [
-                'confirm' => 'Are you sure you want to delete this item?',
-                'method' => 'post',
-            ],
-        ]) ?>
+        <?= Html::a('Изменить', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
+
     </p>
 
     <?= DetailView::widget([