From 993cebad48795a371c9eaf58f7388ad94a6022f7 Mon Sep 17 00:00:00 2001
From: fomichev
Date: Fri, 15 Nov 2024 18:07:19 +0300
Subject: [PATCH] =?utf8?q?=D0=9B=D0=BE=D0=B3=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?=
=?utf8?q?=D0=BD=D0=B8=D0=B5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit
---
.../crud/Product1cReplacementController.php | 118 ++++++++++++++++--
.../product1c-replacement/log-history.php | 44 +++++++
.../crud/product1c-replacement/multy-form.php | 12 +-
.../views/crud/product1c-replacement/view.php | 2 +-
4 files changed, 158 insertions(+), 18 deletions(-)
create mode 100644 erp24/views/crud/product1c-replacement/log-history.php
diff --git a/erp24/controllers/crud/Product1cReplacementController.php b/erp24/controllers/crud/Product1cReplacementController.php
index 5a0e7faa..ba073cce 100644
--- a/erp24/controllers/crud/Product1cReplacementController.php
+++ b/erp24/controllers/crud/Product1cReplacementController.php
@@ -6,6 +6,7 @@ use Yii;
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;
@@ -126,9 +127,14 @@ class Product1cReplacementController extends Controller
'guid_replacement' => $replacementGuid,
]);
if (!$replacementModel->save()) {
-
throw new \Exception('ÐÑибка ÑоÑ
ÑÐ°Ð½ÐµÐ½Ð¸Ñ Ð·Ð°Ð¼ÐµÐ½Ñ.');
}
+ // ÐогиÑование ÑозданиÑ
+ $this->logReplacementAction(
+ $replacementModel->id,
+ 'ÐапиÑÑ Ñоздана',
+ $replacementGuid
+ );
}
}
$transaction->commit();
@@ -185,14 +191,24 @@ class Product1cReplacementController extends Controller
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 {
@@ -200,7 +216,14 @@ class Product1cReplacementController extends Controller
'guid' => $model->guid,
'guid_replacement' => $replacementGuid,
]);
- $replacementModel->save();
+ if ($replacementModel->save()) {
+ // ÐогиÑование ÑозданиÑ
+ $this->logReplacementAction(
+ $replacementModel->id,
+ 'ÐапиÑÑ Ñоздана',
+ $replacementGuid
+ );
+ }
}
}
@@ -218,6 +241,45 @@ class Product1cReplacementController extends Controller
'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.
@@ -230,8 +292,17 @@ class Product1cReplacementController extends Controller
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' => 'ÐÑибка пÑи Ñдалении запиÑи.'];
}
@@ -259,4 +330,37 @@ class Product1cReplacementController extends Controller
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('ÐÑибка запиÑи в лог.');
+ }
+ }
}
diff --git a/erp24/views/crud/product1c-replacement/log-history.php b/erp24/views/crud/product1c-replacement/log-history.php
new file mode 100644
index 00000000..cfa0f525
--- /dev/null
+++ b/erp24/views/crud/product1c-replacement/log-history.php
@@ -0,0 +1,44 @@
+title = 'ÐÑÑоÑÐ¸Ñ Ð»Ð¾Ð³Ð¾Ð² Ð´Ð»Ñ GUI: ';
+$this->params['breadcrumbs'][] = $this->title;
+?>
+
+
= Html::encode($this->title) ?> = $name ?> ( = $guid ?> )
+
+ 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 : 'ÐеизвеÑÑно';
+ },
+ ],
+
+
+ ],
+ ]); ?>
+
+
ÐÑÑоÑÐ¸Ñ Ð»Ð¾Ð³Ð¾Ð² не найдена Ð´Ð»Ñ Ñказанного GUID.
+
+
\ No newline at end of file
diff --git a/erp24/views/crud/product1c-replacement/multy-form.php b/erp24/views/crud/product1c-replacement/multy-form.php
index ec2bed4c..380cb593 100644
--- a/erp24/views/crud/product1c-replacement/multy-form.php
+++ b/erp24/views/crud/product1c-replacement/multy-form.php
@@ -82,6 +82,7 @@ $this->registerJsFile('/js/crud/product1cReplacement/multy-form.js', ['position'
]) ?>
'pjax-container']); ?>
--
2.39.5