From: fomichev Date: Mon, 13 Jan 2025 13:02:17 +0000 (+0300) Subject: Добавление мягкого удаления X-Git-Tag: 1.7~83^2~5 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=596f5404499f919ef25ac2b47edaab05503afac4;p=erp24_rep%2Fyii-erp24%2F.git Добавление мягкого удаления --- 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