From: Vladimir Fomichev Date: Wed, 1 Oct 2025 13:42:26 +0000 (+0300) Subject: Загрузка из файла X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=bb587e8c4cbfd4f0085bc43e4d4e35a1d965bb86;p=erp24_rep%2Fyii-erp24%2F.git Загрузка из файла --- diff --git a/erp24/controllers/MarketplacePricesController.php b/erp24/controllers/MarketplacePricesController.php index 1fafc469..adf30c6d 100644 --- a/erp24/controllers/MarketplacePricesController.php +++ b/erp24/controllers/MarketplacePricesController.php @@ -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, ]); } diff --git a/erp24/controllers/MarketplacePricesLogController.php b/erp24/controllers/MarketplacePricesLogController.php index 63ae3c88..60967eb5 100644 --- a/erp24/controllers/MarketplacePricesLogController.php +++ b/erp24/controllers/MarketplacePricesLogController.php @@ -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 index 00000000..c3f7575c --- /dev/null +++ b/erp24/records/MarketplacePricesLogSearch.php @@ -0,0 +1,76 @@ +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 index 00000000..6aeeeb02 --- /dev/null +++ b/erp24/records/MarketplacePricesSearch.php @@ -0,0 +1,76 @@ +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 index 00000000..678829c2 --- /dev/null +++ b/erp24/views/marketplace-prices-log/_search.php @@ -0,0 +1,43 @@ + + + diff --git a/erp24/views/marketplace-prices-log/index.php b/erp24/views/marketplace-prices-log/index.php index 89fc713e..0221a0a5 100644 --- a/erp24/views/marketplace-prices-log/index.php +++ b/erp24/views/marketplace-prices-log/index.php @@ -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; ?> -
- +
+ 'btn btn-primary my-4']) ?>

title) ?>

-

- 'btn btn-success']) ?> -

- + render('_search', ['model' => $searchModel]); ?> $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 index 00000000..8ee8b978 --- /dev/null +++ b/erp24/views/marketplace-prices/_search.php @@ -0,0 +1,41 @@ + + + diff --git a/erp24/views/marketplace-prices/create.php b/erp24/views/marketplace-prices/create.php index 22a8b2e8..ed888413 100644 --- a/erp24/views/marketplace-prices/create.php +++ b/erp24/views/marketplace-prices/create.php @@ -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; ?> -
- +
+ 'btn btn-primary my-4']) ?>

title) ?>

render('_form', [ diff --git a/erp24/views/marketplace-prices/index.php b/erp24/views/marketplace-prices/index.php index f97103e1..29c5c3cd 100644 --- a/erp24/views/marketplace-prices/index.php +++ b/erp24/views/marketplace-prices/index.php @@ -1,5 +1,6 @@ title = 'Marketplace Prices'; +/** @var \yii\base\DynamicModel $model */ +$this->title = 'Цены для маркетплейсов'; $this->params['breadcrumbs'][] = $this->title; ?> -
+

title) ?>

- 'btn btn-success']) ?> + 'btn btn-success']) ?> + ['enctype' => 'multipart/form-data']]); ?> + field($model, 'excelFile')->fileInput() ?> +

+ 'btn btn-success']) ?> +
+

+ render('_search', ['model' => $searchModel]); ?> $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]); } diff --git a/erp24/views/marketplace-prices/update.php b/erp24/views/marketplace-prices/update.php index a33c1555..51ddea3c 100644 --- a/erp24/views/marketplace-prices/update.php +++ b/erp24/views/marketplace-prices/update.php @@ -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'; ?> -
- +
+ 'btn btn-primary my-4']) ?>

title) ?>

render('_form', [ diff --git a/erp24/views/marketplace-prices/view.php b/erp24/views/marketplace-prices/view.php index 9fefbd78..865b3f37 100644 --- a/erp24/views/marketplace-prices/view.php +++ b/erp24/views/marketplace-prices/view.php @@ -12,18 +12,12 @@ $this->params['breadcrumbs'][] = $this->title; \yii\web\YiiAsset::register($this); ?>
- + 'btn btn-primary my-4']) ?>

title) ?>

- $model->id], ['class' => 'btn btn-primary']) ?> - $model->id], [ - 'class' => 'btn btn-danger', - 'data' => [ - 'confirm' => 'Are you sure you want to delete this item?', - 'method' => 'post', - ], - ]) ?> + $model->id], ['class' => 'btn btn-primary']) ?> +