From 240af5586d025d0730c93f90a0779a5fc6516724 Mon Sep 17 00:00:00 2001 From: fomichev Date: Wed, 22 Jan 2025 13:22:41 +0300 Subject: [PATCH] =?utf8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?utf8?q?=D0=B8=D0=B5=20softDelete=20=D0=B2=20=D0=B2=D0=BE=D1=80=D0=BA?= =?utf8?q?=D0=B1=D0=BE=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/api3/core/services/TimetableService.php | 2 +- .../modules/v1/models/timetable/Timetable.php | 14 +++++++++++++- erp24/records/TimetablePlan.php | 6 +++--- erp24/records/TimetableV3.php | 18 +++++++++++++++++- erp24/traits/SoftDeleteTrait.php | 5 +++-- 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/erp24/api3/core/services/TimetableService.php b/erp24/api3/core/services/TimetableService.php index 7ce83008..48d4167c 100644 --- a/erp24/api3/core/services/TimetableService.php +++ b/erp24/api3/core/services/TimetableService.php @@ -268,7 +268,7 @@ class TimetableService $timetableWorkbot->save(); - return (bool)$timetable->softDelete(); + return (bool)$timetable->softDelete($removed_by); } return false; } diff --git a/erp24/api3/modules/v1/models/timetable/Timetable.php b/erp24/api3/modules/v1/models/timetable/Timetable.php index 8c483411..3777f3aa 100644 --- a/erp24/api3/modules/v1/models/timetable/Timetable.php +++ b/erp24/api3/modules/v1/models/timetable/Timetable.php @@ -194,4 +194,16 @@ class Timetable extends \yii_app\records\TimetableV3 public function getCheckInCount() { return $this->hasMany(AdminCheckin::class, ['plan_id' => 'id'])->count(); } -} \ No newline at end of file + + public function softDelete($deleted_by = null) + { + $existingFact = TimetableFactModel::findOne(['plan_id' => $this->id]); + + if ($existingFact) { + return false; + } + + return parent::softDelete($deleted_by); + } + +} diff --git a/erp24/records/TimetablePlan.php b/erp24/records/TimetablePlan.php index b40c9ca1..763696cf 100755 --- a/erp24/records/TimetablePlan.php +++ b/erp24/records/TimetablePlan.php @@ -146,15 +146,15 @@ class TimetablePlan extends Timetable return $this->fact->getDateTimeEnd() < $this->getDateTimeEnd(); } - public function softDelete() + public function softDelete($deleted_by = null) { $existingFact = TimetableFactModel::findOne(['plan_id' => $this->id]); -//var_dump($existingFact); die(); + if ($existingFact) { Yii::$app->session->setFlash('error', 'Невозможно удалить запись плановой смены для которой существует факт смены.'); return false; } - return parent::softDelete(); + return parent::softDelete($deleted_by); } } diff --git a/erp24/records/TimetableV3.php b/erp24/records/TimetableV3.php index 8bb65150..cf08782a 100755 --- a/erp24/records/TimetableV3.php +++ b/erp24/records/TimetableV3.php @@ -5,6 +5,7 @@ namespace yii_app\records; use yii\db\ActiveQuery; use yii\db\ActiveRecord; +use yii_app\traits\SoftDeleteTrait; /** * Табель сотрудников @@ -36,6 +37,7 @@ use yii\db\ActiveRecord; */ class TimetableV3 extends ActiveRecord { + use SoftDeleteTrait; const STATUS_PENDING = 0; const STATUS_VERIFIED = 1; @@ -331,4 +333,18 @@ class TimetableV3 extends ActiveRecord { return $this->hasOne(Shift::class, ['id' => 'shift_id']); } -} \ No newline at end of file + + public static function hasSoftDelete(): bool + { + return true; + } + + public static function find() + { + $query = parent::find(); + if (static::hasSoftDelete()) { + $query->andWhere(['timetable.active' => 1]); + } + return $query; + } +} diff --git a/erp24/traits/SoftDeleteTrait.php b/erp24/traits/SoftDeleteTrait.php index 6fd45fed..9a44b885 100644 --- a/erp24/traits/SoftDeleteTrait.php +++ b/erp24/traits/SoftDeleteTrait.php @@ -9,11 +9,12 @@ trait SoftDeleteTrait /** * Удаление записи (soft delete). */ - public function softDelete() + public function softDelete($deleted_by = null) { $admin = Yii::$app->user->identity; + $adminId = $deleted_by ?? $admin->id; $this->active = 0; - $this->deleted_by = $admin->id; + $this->deleted_by = $adminId; $this->deleted_at = date('Y-m-d H:i:s'); return $this->save(false, ['active', 'deleted_by', 'deleted_at']); } -- 2.39.5