From a34bf202ca146de32b3e2145fe4f319aa13833a9 Mon Sep 17 00:00:00 2001 From: Vladimir Fomichev Date: Mon, 10 Nov 2025 18:15:40 +0300 Subject: [PATCH] =?utf8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D1=80?= =?utf8?q?=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- ...monthly_and_daily_in_employee_position.php | 78 ++++++++++------- ...ployee_position_id_to_employee_payment.php | 86 +++++++++++-------- erp24/records/Admin.php | 18 +++- erp24/records/EmployeePayment.php | 2 +- erp24/records/EmployeePosition.php | 15 +++- erp24/services/SalarySyncService.php | 6 +- 6 files changed, 128 insertions(+), 77 deletions(-) diff --git a/erp24/migrations/m251201_120000_replace_salary_with_monthly_and_daily_in_employee_position.php b/erp24/migrations/m251201_120000_replace_salary_with_monthly_and_daily_in_employee_position.php index 8a812f8a..74b9f1c2 100644 --- a/erp24/migrations/m251201_120000_replace_salary_with_monthly_and_daily_in_employee_position.php +++ b/erp24/migrations/m251201_120000_replace_salary_with_monthly_and_daily_in_employee_position.php @@ -11,24 +11,32 @@ class m251201_120000_replace_salary_with_monthly_and_daily_in_employee_position */ public function safeUp() { - $tableSchema = $this->db->getTableSchema(self::TABLE_NAME); + $transaction = $this->db->beginTransaction(); + try { + $tableSchema = $this->db->getTableSchema(self::TABLE_NAME); - // Добавляем поле monthly_salary - if (isset($tableSchema) && !isset($tableSchema->columns['monthly_salary'])) { - $this->addColumn(self::TABLE_NAME, 'monthly_salary', $this->decimal(10, 2)->null()->comment('Месячный оклад')); - } + // Добавляем поле monthly_salary + if (isset($tableSchema) && !isset($tableSchema->columns['monthly_salary'])) { + $this->addColumn(self::TABLE_NAME, 'monthly_salary', $this->decimal(10, 2)->null()->comment('Месячный оклад')); + } - // Добавляем поле daily_payment - if (isset($tableSchema) && !isset($tableSchema->columns['daily_payment'])) { - $this->addColumn(self::TABLE_NAME, 'daily_payment', $this->decimal(10, 2)->null()->comment('Подневная оплата')); - } + // Добавляем поле daily_payment + if (isset($tableSchema) && !isset($tableSchema->columns['daily_payment'])) { + $this->addColumn(self::TABLE_NAME, 'daily_payment', $this->decimal(10, 2)->null()->comment('Подневная оплата')); + } - // Переносим данные из salary в monthly_salary, если salary существует - if (isset($tableSchema) && isset($tableSchema->columns['salary'])) { - $this->execute("UPDATE " . self::TABLE_NAME . " SET monthly_salary = salary WHERE salary IS NOT NULL AND monthly_salary IS NULL"); - - // Удаляем старое поле salary - $this->dropColumn(self::TABLE_NAME, 'salary'); + // Переносим данные из salary в monthly_salary, если salary существует + if (isset($tableSchema) && isset($tableSchema->columns['salary'])) { + $this->execute("UPDATE " . self::TABLE_NAME . " SET monthly_salary = salary WHERE salary IS NOT NULL AND monthly_salary IS NULL"); + + // Удаляем старое поле salary + $this->dropColumn(self::TABLE_NAME, 'salary'); + } + + $transaction->commit(); + } catch (\Exception $e) { + $transaction->rollBack(); + throw $e; } } @@ -37,24 +45,32 @@ class m251201_120000_replace_salary_with_monthly_and_daily_in_employee_position */ public function safeDown() { - $tableSchema = $this->db->getTableSchema(self::TABLE_NAME); - - // Восстанавливаем поле salary - if (isset($tableSchema) && !isset($tableSchema->columns['salary'])) { - $this->addColumn(self::TABLE_NAME, 'salary', $this->integer()->null()->comment('Зарплата')); - - // Переносим данные обратно из monthly_salary в salary - $this->execute("UPDATE " . self::TABLE_NAME . " SET salary = CAST(monthly_salary AS INTEGER) WHERE monthly_salary IS NOT NULL"); - } + $transaction = $this->db->beginTransaction(); + try { + $tableSchema = $this->db->getTableSchema(self::TABLE_NAME); - // Удаляем поле daily_payment - if (isset($tableSchema) && isset($tableSchema->columns['daily_payment'])) { - $this->dropColumn(self::TABLE_NAME, 'daily_payment'); - } + // Восстанавливаем поле salary + if (isset($tableSchema) && !isset($tableSchema->columns['salary'])) { + $this->addColumn(self::TABLE_NAME, 'salary', $this->integer()->null()->comment('Зарплата')); + + // Переносим данные обратно из monthly_salary в salary + $this->execute("UPDATE " . self::TABLE_NAME . " SET salary = CAST(monthly_salary AS INTEGER) WHERE monthly_salary IS NOT NULL"); + } + + // Удаляем поле daily_payment + if (isset($tableSchema) && isset($tableSchema->columns['daily_payment'])) { + $this->dropColumn(self::TABLE_NAME, 'daily_payment'); + } + + // Удаляем поле monthly_salary + if (isset($tableSchema) && isset($tableSchema->columns['monthly_salary'])) { + $this->dropColumn(self::TABLE_NAME, 'monthly_salary'); + } - // Удаляем поле monthly_salary - if (isset($tableSchema) && isset($tableSchema->columns['monthly_salary'])) { - $this->dropColumn(self::TABLE_NAME, 'monthly_salary'); + $transaction->commit(); + } catch (\Exception $e) { + $transaction->rollBack(); + throw $e; } } } diff --git a/erp24/migrations/m251201_120001_add_employee_position_id_to_employee_payment.php b/erp24/migrations/m251201_120001_add_employee_position_id_to_employee_payment.php index 9191d175..d352407f 100644 --- a/erp24/migrations/m251201_120001_add_employee_position_id_to_employee_payment.php +++ b/erp24/migrations/m251201_120001_add_employee_position_id_to_employee_payment.php @@ -11,29 +11,37 @@ class m251201_120001_add_employee_position_id_to_employee_payment extends Migrat */ public function safeUp() { - $tableSchema = $this->db->getTableSchema(self::TABLE_NAME); - - // Добавляем поле employee_position_id - if (isset($tableSchema) && !isset($tableSchema->columns['employee_position_id'])) { - $this->addColumn(self::TABLE_NAME, 'employee_position_id', $this->integer()->null()->comment('ID должности из employee_position')); - - // Добавляем внешний ключ - $this->addForeignKey( - 'fk-employee_payment-employee_position_id', - self::TABLE_NAME, - 'employee_position_id', - 'erp24.employee_position', - 'id', - 'SET NULL', - 'CASCADE' - ); - - // Добавляем индекс для улучшения производительности - $this->createIndex( - 'idx-employee_payment-employee_position_id', - self::TABLE_NAME, - 'employee_position_id' - ); + $transaction = $this->db->beginTransaction(); + try { + $tableSchema = $this->db->getTableSchema(self::TABLE_NAME); + + // Добавляем поле employee_position_id + if (isset($tableSchema) && !isset($tableSchema->columns['employee_position_id'])) { + $this->addColumn(self::TABLE_NAME, 'employee_position_id', $this->integer()->null()->comment('ID должности из employee_position')); + + // Добавляем внешний ключ + $this->addForeignKey( + 'fk-employee_payment-employee_position_id', + self::TABLE_NAME, + 'employee_position_id', + 'erp24.employee_position', + 'id', + 'SET NULL', + 'CASCADE' + ); + + // Добавляем индекс для улучшения производительности + $this->createIndex( + 'idx-employee_payment-employee_position_id', + self::TABLE_NAME, + 'employee_position_id' + ); + } + + $transaction->commit(); + } catch (\Exception $e) { + $transaction->rollBack(); + throw $e; } } @@ -42,21 +50,29 @@ class m251201_120001_add_employee_position_id_to_employee_payment extends Migrat */ public function safeDown() { - $tableSchema = $this->db->getTableSchema(self::TABLE_NAME); + $transaction = $this->db->beginTransaction(); + try { + $tableSchema = $this->db->getTableSchema(self::TABLE_NAME); - // Удаляем индекс - if (isset($tableSchema) && isset($tableSchema->columns['employee_position_id'])) { - $this->dropIndex('idx-employee_payment-employee_position_id', self::TABLE_NAME); - } + // Удаляем индекс + if (isset($tableSchema) && isset($tableSchema->columns['employee_position_id'])) { + $this->dropIndex('idx-employee_payment-employee_position_id', self::TABLE_NAME); + } - // Удаляем внешний ключ - if (isset($tableSchema) && isset($tableSchema->columns['employee_position_id'])) { - $this->dropForeignKey('fk-employee_payment-employee_position_id', self::TABLE_NAME); - } + // Удаляем внешний ключ + if (isset($tableSchema) && isset($tableSchema->columns['employee_position_id'])) { + $this->dropForeignKey('fk-employee_payment-employee_position_id', self::TABLE_NAME); + } + + // Удаляем поле employee_position_id + if (isset($tableSchema) && isset($tableSchema->columns['employee_position_id'])) { + $this->dropColumn(self::TABLE_NAME, 'employee_position_id'); + } - // Удаляем поле employee_position_id - if (isset($tableSchema) && isset($tableSchema->columns['employee_position_id'])) { - $this->dropColumn(self::TABLE_NAME, 'employee_position_id'); + $transaction->commit(); + } catch (\Exception $e) { + $transaction->rollBack(); + throw $e; } } } diff --git a/erp24/records/Admin.php b/erp24/records/Admin.php index d493b086..65f295a0 100755 --- a/erp24/records/Admin.php +++ b/erp24/records/Admin.php @@ -4,6 +4,7 @@ declare(strict_types = 1); namespace yii_app\records; use svay\FaceDetector; +use Yii; use yii\base\NotSupportedException; use yii\db\ActiveQueryInterface; use yii\db\ActiveRecord; @@ -778,10 +779,19 @@ class Admin extends ActiveRecord implements IdentityInterface $oldPositionId = $changedAttributes['employee_position_id'] ?? null; $newPositionId = $this->employee_position_id; - // Если должность была добавлена или изменена - if ($newPositionId && ($oldPositionId != $newPositionId || $insert)) { - $syncService = new SalarySyncService(); - $syncService->createPaymentFromPosition($this->id); + // Если должность была добавлена или изменена, и сотрудник не уволен + if ($newPositionId && ($oldPositionId != $newPositionId || $insert) && $this->group_id != AdminGroup::GROUP_FIRED) { + try { + $syncService = new SalarySyncService(); + $result = $syncService->createPaymentFromPosition($this->id); + + if ($result) { + Yii::info("Автоматически создана запись EmployeePayment для admin_id={$this->id}, position_id={$newPositionId}", 'salary-sync'); + } + } catch (\Exception $e) { + Yii::error("Ошибка автоматической синхронизации оклада для admin_id={$this->id}: " . $e->getMessage(), 'salary-sync'); + // Не бросаем исключение, чтобы не прервать сохранение Admin + } } } } diff --git a/erp24/records/EmployeePayment.php b/erp24/records/EmployeePayment.php index 04c57f9a..17817732 100755 --- a/erp24/records/EmployeePayment.php +++ b/erp24/records/EmployeePayment.php @@ -94,7 +94,7 @@ class EmployeePayment extends \yii\db\ActiveRecord public function beforeValidate() { if (get_called_class() === self::class) { - $this->creator_id = $_SESSION['admin_id'] ?? null; + $this->creator_id = Yii::$app->user->id ?? null; if ($this->admin) { $this->admin_group_id = $this->admin->group_id; // Автоматически заполняем employee_position_id из Admin, если не заполнено diff --git a/erp24/records/EmployeePosition.php b/erp24/records/EmployeePosition.php index 3697ab15..dc29f537 100755 --- a/erp24/records/EmployeePosition.php +++ b/erp24/records/EmployeePosition.php @@ -155,9 +155,18 @@ class EmployeePosition extends \yii\db\ActiveRecord // Если оклады изменились и они заполнены, синхронизируем для всех сотрудников if ($salaryChanged && $this->monthly_salary !== null && $this->daily_payment !== null) { - $syncService = new SalarySyncService(); - $creatorId = $this->updated_by ?? $_SESSION['admin_id'] ?? null; - $syncService->syncEmployeesByPosition($this->id, $creatorId); + try { + $syncService = new SalarySyncService(); + $creatorId = $this->updated_by ?? Yii::$app->user->id ?? null; + $result = $syncService->syncEmployeesByPosition($this->id, $creatorId); + + if ($result['success']) { + Yii::info("Автоматическая синхронизация окладов для должности ID={$this->id}: {$result['message']}", 'salary-sync'); + } + } catch (\Exception $e) { + Yii::error("Ошибка автоматической синхронизации окладов для должности ID={$this->id}: " . $e->getMessage(), 'salary-sync'); + // Не бросаем исключение, чтобы не прервать сохранение EmployeePosition + } } } } diff --git a/erp24/services/SalarySyncService.php b/erp24/services/SalarySyncService.php index ab3a5b3c..6512ec6c 100644 --- a/erp24/services/SalarySyncService.php +++ b/erp24/services/SalarySyncService.php @@ -23,7 +23,7 @@ class SalarySyncService public function syncAllEmployeesFromPositions($creatorId = null): array { if ($creatorId === null) { - $creatorId = $_SESSION['admin_id'] ?? null; + $creatorId = Yii::$app->user->id ?? null; } if (!$creatorId) { @@ -121,7 +121,7 @@ class SalarySyncService public function createPaymentFromPosition($adminId, $creatorId = null, $date = null): ?EmployeePayment { if ($creatorId === null) { - $creatorId = $_SESSION['admin_id'] ?? null; + $creatorId = Yii::$app->user->id ?? null; } if (!$creatorId) { @@ -178,7 +178,7 @@ class SalarySyncService public function syncEmployeesByPosition($positionId, $creatorId = null): array { if ($creatorId === null) { - $creatorId = $_SESSION['admin_id'] ?? null; + $creatorId = Yii::$app->user->id ?? null; } if (!$creatorId) { -- 2.39.5