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.
*/
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,
]);
}
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;
*/
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,
]);
}
--- /dev/null
+<?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;
+ }
+}
--- /dev/null
+<?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;
+ }
+}
--- /dev/null
+<?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>
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]);
- }
- ],
],
]); ?>
--- /dev/null
+<?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>
/** @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', [
<?php
+use yii\widgets\ActiveForm;
use yii_app\records\MarketplacePrices;
use yii\helpers\Html;
use yii\helpers\Url;
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'],
'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]);
}
/** @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', [
\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([