*/
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;
}
}
*/
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;
}
}
}
*/
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;
}
}
*/
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;
}
}
}
namespace yii_app\records;
use svay\FaceDetector;
+use Yii;
use yii\base\NotSupportedException;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveRecord;
$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
+ }
}
}
}
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, если не заполнено
// Если оклады изменились и они заполнены, синхронизируем для всех сотрудников
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
+ }
}
}
}
public function syncAllEmployeesFromPositions($creatorId = null): array
{
if ($creatorId === null) {
- $creatorId = $_SESSION['admin_id'] ?? null;
+ $creatorId = Yii::$app->user->id ?? null;
}
if (!$creatorId) {
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) {
public function syncEmployeesByPosition($positionId, $creatorId = null): array
{
if ($creatorId === null) {
- $creatorId = $_SESSION['admin_id'] ?? null;
+ $creatorId = Yii::$app->user->id ?? null;
}
if (!$creatorId) {