From: Vladimir Fomichev Date: Wed, 1 Oct 2025 14:07:11 +0000 (+0300) Subject: Загрузка из файла с логами X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=71bf7a2fb5f4d0157e894fbe8d659f31cb0182a9;p=erp24_rep%2Fyii-erp24%2F.git Загрузка из файла с логами --- diff --git a/erp24/controllers/MarketplacePricesController.php b/erp24/controllers/MarketplacePricesController.php index adf30c6d..5d6d84b5 100644 --- a/erp24/controllers/MarketplacePricesController.php +++ b/erp24/controllers/MarketplacePricesController.php @@ -7,13 +7,13 @@ use Yii; use yii\base\DynamicModel; use yii\web\UploadedFile; use yii_app\records\MarketplacePrices; +use yii_app\records\MarketplacePricesLog; 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. @@ -74,22 +74,36 @@ class MarketplacePricesController extends Controller } $mpPrice = $mpPrices[$matrixProduct->id] ?? null; - + $action = 2; 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; + $action = 1; } - + $beforePrice = $mpPrice->price ?? null; + $beforeOldPrice = $mpPrice->old_price ?? null; $mpPrice->price = $price; $mpPrice->old_price = $oldPrice; if (!$mpPrice->save()) { Yii::error("Ошибка создания цены: " . json_encode($mpPrice->getErrors()), __METHOD__); + } else { + $mpPriceLog = new MarketplacePricesLog(); + $mpPriceLog->marketplace_prices_id = $mpPrice->id; + $mpPriceLog->price_after = $mpPrice->price; + $mpPriceLog->price_before = $beforePrice; + $mpPriceLog->old_price_after = $mpPrice->old_price; + $mpPriceLog->old_price_before = $beforeOldPrice; + $mpPriceLog->changed_at = date('Y-m-d H:i:s'); + $mpPriceLog->changed_by = Yii::$app->user->id; + $mpPriceLog->action = $action; + if (!$mpPriceLog->save()) { + Yii::error("Ошибка создания лога цены: " . json_encode($mpPriceLog->getErrors()), __METHOD__); + } } - } Yii::$app->session->setFlash('success', 'Импорт завершён: записи добавлены или обновлены.'); diff --git a/erp24/records/MarketplacePrices.php b/erp24/records/MarketplacePrices.php index e118d0a5..113a68d4 100644 --- a/erp24/records/MarketplacePrices.php +++ b/erp24/records/MarketplacePrices.php @@ -28,6 +28,18 @@ class MarketplacePrices extends \yii\db\ActiveRecord return 'marketplace_prices'; } + public function behaviors() + { + return [ + 'ts' => [ + 'class' => \yii\behaviors\TimestampBehavior::class, + 'createdAtAttribute' => 'created_at', + 'updatedAtAttribute' => 'updated_at', + 'value' => new \yii\db\Expression('NOW()'), + ], + ]; + } + /** * {@inheritdoc} */ diff --git a/erp24/records/MarketplacePricesLog.php b/erp24/records/MarketplacePricesLog.php index dadb38f6..ebcf3209 100644 --- a/erp24/records/MarketplacePricesLog.php +++ b/erp24/records/MarketplacePricesLog.php @@ -29,6 +29,23 @@ class MarketplacePricesLog extends \yii\db\ActiveRecord return 'marketplace_prices_log'; } + public function behaviors() + { + return [ + 'ts' => [ + 'class' => \yii\behaviors\TimestampBehavior::class, + 'createdAtAttribute' => 'changed_at', + 'updatedAtAttribute' => 'changed_at', + 'value' => new \yii\db\Expression('NOW()'), + ], + 'blame' => [ + 'class' => \yii\behaviors\BlameableBehavior::class, + 'createdByAttribute' => 'changed_by', + 'updatedByAttribute' => 'changed_by', + ], + ]; + } + /** * {@inheritdoc} */