From 596f5404499f919ef25ac2b47edaab05503afac4 Mon Sep 17 00:00:00 2001 From: fomichev Date: Mon, 13 Jan 2025 16:02:17 +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=20=D0=BC=D1=8F=D0=B3=D0=BA=D0=BE=D0=B3=D0=BE=20?= =?utf8?q?=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/actions/timetable/EditPlanAction.php | 2 +- ...214_add_deleted_at_column_to_timetable.php | 38 +++++++++++++++++++ erp24/records/Timetable.php | 17 +++++++++ erp24/records/TimetableFact.php | 5 +++ erp24/traits/SoftDeleteTrait.php | 33 ++++++++++++++++ 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 erp24/migrations/m250113_104214_add_deleted_at_column_to_timetable.php create mode 100644 erp24/traits/SoftDeleteTrait.php diff --git a/erp24/actions/timetable/EditPlanAction.php b/erp24/actions/timetable/EditPlanAction.php index a6db9656..b6d556a1 100755 --- a/erp24/actions/timetable/EditPlanAction.php +++ b/erp24/actions/timetable/EditPlanAction.php @@ -30,7 +30,7 @@ class EditPlanAction extends Action $fact->delete(); } // $slot->validate() && $slot->delete(); - $slot->delete(); + $slot->softDelete(); } return $this->controller->redirect($this->controller->request->getReferrer() ?: ['timetable/plan']); } diff --git a/erp24/migrations/m250113_104214_add_deleted_at_column_to_timetable.php b/erp24/migrations/m250113_104214_add_deleted_at_column_to_timetable.php new file mode 100644 index 00000000..85870adf --- /dev/null +++ b/erp24/migrations/m250113_104214_add_deleted_at_column_to_timetable.php @@ -0,0 +1,38 @@ +db->schema->getTableSchema(self::TABLE_NAME) === null) { + return; + } + + if ($this->db->schema->getTableSchema(self::TABLE_NAME)->getColumn('deleted_at') === null) { + $this->addColumn(self::TABLE_NAME, 'deleted_at', $this->timestamp()->null()->defaultValue(null)->comment('Дата мягкого удаления')); + } + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + if ($this->db->schema->getTableSchema(self::TABLE_NAME) === null) { + return; + } + + if ($this->db->schema->getTableSchema(self::TABLE_NAME)->getColumn('deleted_at') !== null) { + $this->dropColumn(self::TABLE_NAME, 'deleted_at'); + } + } +} diff --git a/erp24/records/Timetable.php b/erp24/records/Timetable.php index e3ba83c8..2d4f2709 100755 --- a/erp24/records/Timetable.php +++ b/erp24/records/Timetable.php @@ -6,6 +6,7 @@ namespace yii_app\records; use yii\db\ActiveQuery; use yii\db\ActiveRecord; use yii\helpers\ArrayHelper; +use yii_app\traits\SoftDeleteTrait; /** * Табель сотрудников @@ -38,6 +39,8 @@ use yii\helpers\ArrayHelper; */ class Timetable extends ActiveRecord { + use SoftDeleteTrait; + const STATUS_PENDING = 0; const STATUS_VERIFIED = 1; @@ -478,4 +481,18 @@ class Timetable extends ActiveRecord 'dateAdmins' => $adminsByDate, ]; } + + public static function hasSoftDelete(): bool + { + return true; + } + + public static function find() + { + $query = parent::find(); + if (static::hasSoftDelete()) { + $query->andWhere(['deleted_at' => null]); + } + return $query; + } } \ No newline at end of file diff --git a/erp24/records/TimetableFact.php b/erp24/records/TimetableFact.php index 00af40ee..bc18839b 100755 --- a/erp24/records/TimetableFact.php +++ b/erp24/records/TimetableFact.php @@ -38,6 +38,11 @@ class TimetableFact extends Timetable ); } + public static function hasSoftDelete(): bool + { + return false; + } + public static function find() { return parent::find()->andWhere(['tabel' => self::TABLE_FACT]); diff --git a/erp24/traits/SoftDeleteTrait.php b/erp24/traits/SoftDeleteTrait.php new file mode 100644 index 00000000..22fed05d --- /dev/null +++ b/erp24/traits/SoftDeleteTrait.php @@ -0,0 +1,33 @@ +deleted_at = date('Y-m-d H:i:s'); + return $this->save(false, ['deleted_at']); + } + + /** + * Восстановление удаленной записи. + */ + public function restore() + { + $this->deleted_at = null; + return $this->save(false, ['deleted_at']); + } + + /** + * Проверка, удалена ли запись. + */ + public function isDeleted(): bool + { + return $this->deleted_at !== null; + } +} \ No newline at end of file -- 2.39.5