use yii\behaviors\TimestampBehavior;
use yii\data\ActiveDataProvider;
use yii_app\records\Product1cReplacement;
+use yii_app\records\Product1cReplacementLog;
use yii_app\records\Product1cReplacementSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
'guid_replacement' => $replacementGuid,
]);
if (!$replacementModel->save()) {
-
throw new \Exception('Ошибка сохранения замены.');
}
+ // Логирование создания
+ $this->logReplacementAction(
+ $replacementModel->id,
+ 'Запись создана',
+ $replacementGuid
+ );
}
}
$transaction->commit();
try {
$replacementData = $this->request->post('Product1cReplacement')['guid_replacement'] ?? [];
$replacementIds = $this->request->post('Product1cReplacement')['replacement_ids'] ?? [];
-
+ //var_dump($replacementData); die();
foreach ($replacementData as $index => $replacementGuid) {
if (!empty($replacementIds[$index])) {
$replacementModel = Product1cReplacement::findOne($replacementIds[$index]);
if ($replacementModel) {
- $replacementModel->guid_replacement = $replacementGuid;
- $replacementModel->save();
+ $stateBefore = $replacementModel->guid_replacement;
+ if ($replacementModel->guid_replacement !== $replacementGuid) {
+ $replacementModel->guid_replacement = $replacementGuid;
+ if ($replacementModel->save()) {
+ // Логирование изменения только при изменении данных
+ $this->logReplacementAction(
+ $replacementModel->id,
+ $stateBefore,
+ $replacementGuid
+ );
+ }
+ }
}
} else {
'guid' => $model->guid,
'guid_replacement' => $replacementGuid,
]);
- $replacementModel->save();
+ if ($replacementModel->save()) {
+ // Логирование создания
+ $this->logReplacementAction(
+ $replacementModel->id,
+ 'Запись создана',
+ $replacementGuid
+ );
+ }
}
}
'replacements' => $replacementsQuery,
]);
}
+
+
+ public function actionLogHistory($guid)
+ {
+
+ $replacements = Product1cReplacement::find()
+ ->select('id')
+ ->where(['guid' => $guid])
+ ->column();
+
+
+ if (empty($replacements)) {
+ Yii::$app->session->setFlash('warning', 'История логов не найдена для указанного GUID.');
+ return $this->render('log-history', ['dataProvider' => null]);
+ }
+ $productName = Products1c::find()
+ ->select('name')
+ ->where(['id' => $guid])
+ ->scalar();
+
+ $logQuery = Product1cReplacementLog::find()
+ ->where(['replacement_id' => $replacements])
+ ->orderBy(['date' => SORT_DESC]);
+
+ $dataProvider = new \yii\data\ActiveDataProvider([
+ 'query' => $logQuery,
+ 'pagination' => [
+ 'pageSize' => 20,
+ ],
+ ]);
+
+ return $this->render('log-history', [
+ 'dataProvider' => $dataProvider,
+ 'guid' => $guid,
+ 'name' => $productName,
+ ]);
+ }
+
+
/**
* Deletes an existing Product1CReplacement model.
* If deletion is successful, the browser will be redirected to the 'index' page.
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$model = $this->findModel($id);
- if ($model && $model->softDelete()) {
- return ['success' => true];
+ if ($model) {
+ $stateBefore = $model->guid_replacement;
+ $this->logReplacementAction(
+ $model->id,
+ $stateBefore,
+ 'Запись удалена'
+ );
+ if ($model->softDelete()) {
+
+ return ['success' => true];
+ }
}
return ['success' => false, 'message' => 'Ошибка при удалении записи.'];
}
throw new NotFoundHttpException('The requested page does not exist.');
}
+
+ protected function logReplacementAction($replacementId, $stateBefore, $stateAfter)
+ {
+
+ $existingLog = Product1cReplacementLog::find()
+ ->where([
+ 'replacement_id' => $replacementId,
+ 'state_before' => $stateBefore,
+ 'state_after' => $stateAfter,
+ 'admin_id' => Yii::$app->user->id,
+ ])
+ ->exists();
+
+ if ($existingLog) {
+
+ return;
+ }
+
+
+ $log = new Product1cReplacementLog([
+ 'replacement_id' => $replacementId,
+ 'state_before' => $stateBefore,
+ 'state_after' => $stateAfter,
+ 'date' => date('Y-m-d H:i:s'),
+ 'admin_id' => Yii::$app->user->id,
+ ]);
+
+ if (!$log->save()) {
+
+ Yii::error('Ошибка записи в лог: ' . json_encode($log->errors), __METHOD__);
+ throw new \Exception('Ошибка записи в лог.');
+ }
+ }
}
--- /dev/null
+<?php
+
+use yii\grid\GridView;
+use yii\helpers\Html;
+use yii_app\records\Admin;
+
+/* @var $this yii\web\View */
+/* @var $dataProvider yii\data\ActiveDataProvider */
+/* @var $guid string */
+/* @var $name string */
+
+$this->title = 'История логов для GUI: ';
+$this->params['breadcrumbs'][] = $this->title;
+?>
+<div class="log-history-index p-4">
+ <h1><?= Html::encode($this->title) ?> <?= $name ?> ( <?= $guid ?> ) </h1>
+
+ <?php if ($dataProvider && $dataProvider->getTotalCount() > 0): ?>
+ <?= GridView::widget([
+ 'dataProvider' => $dataProvider,
+ 'columns' => [
+ ['class' => 'yii\grid\SerialColumn'],
+
+ 'replacement_id',
+ 'state_before',
+ 'state_after',
+ 'date',
+ [
+ 'attribute' => 'admin_id',
+ 'label' => 'Имя пользователя',
+ 'value' => function ($model) {
+ // Получаем имя пользователя по admin_id
+ $admin = Admin::findOne($model->admin_id);
+ return $admin ? $admin->name : 'Неизвестно';
+ },
+ ],
+
+
+ ],
+ ]); ?>
+ <?php else: ?>
+ <p>История логов не найдена для указанного GUID.</p>
+ <?php endif; ?>
+</div>
\ No newline at end of file