--- /dev/null
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Handles the creation of table `store_staffing` and `store_staffing_log`
+ * for storing store staffing information and logs of changes
+ */
+class m251211_100000_create_store_staffing_tables extends Migration
+{
+ const TABLE_STAFFING = 'erp24.store_staffing';
+ const TABLE_LOG = 'erp24.store_staffing_log';
+
+ /**
+ * {@inheritdoc}
+ */
+ public function safeUp()
+ {
+ $transaction = $this->db->beginTransaction();
+ try {
+ // Создаем таблицу store_staffing (штатное расписание)
+ $staffingSchema = $this->db->getTableSchema(self::TABLE_STAFFING);
+ if (!isset($staffingSchema)) {
+ $this->createTable(self::TABLE_STAFFING, [
+ 'id' => $this->primaryKey(),
+ 'store_id' => $this->integer()->notNull()->comment('ID магазина'),
+ 'employee_position_id' => $this->integer()->notNull()->comment('ID должности'),
+ 'count' => $this->decimal(4, 2)->notNull()->comment('Количество сотрудников (может быть дробным - пол смены)'),
+ 'created_at' => $this->timestamp()->defaultExpression('CURRENT_TIMESTAMP'),
+ 'updated_at' => $this->timestamp()->defaultExpression('CURRENT_TIMESTAMP'),
+ ]);
+ }
+
+ // Создаем таблицу store_staffing_log (логирование изменений)
+ $logSchema = $this->db->getTableSchema(self::TABLE_LOG);
+ if (!isset($logSchema)) {
+ $this->createTable(self::TABLE_LOG, [
+ 'id' => $this->primaryKey(),
+ 'store_staffing_id' => $this->integer()->null()->comment('ID записи штатного расписания (NULL для удалений)'),
+ 'store_id' => $this->integer()->notNull()->comment('ID магазина'),
+ 'employee_position_id' => $this->integer()->notNull()->comment('ID должности'),
+ 'action' => $this->string(10)->notNull()->comment('Действие: create, update, delete'),
+ 'old_count' => $this->integer()->null()->comment('Старое количество'),
+ 'new_count' => $this->integer()->null()->comment('Новое количество'),
+ 'changed_by' => $this->integer()->null()->comment('ID администратора, внесшего изменение'),
+ 'created_at' => $this->timestamp()->defaultExpression('CURRENT_TIMESTAMP'),
+ ]);
+ }
+
+ $transaction->commit();
+ } catch (\Exception $e) {
+ $transaction->rollBack();
+ throw $e;
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function safeDown()
+ {
+ $transaction = $this->db->beginTransaction();
+ try {
+ // Удаляем таблицу store_staffing_log
+ $logSchema = $this->db->getTableSchema(self::TABLE_LOG);
+ if (isset($logSchema)) {
+ $this->dropTable(self::TABLE_LOG);
+ }
+
+ // Удаляем таблицу store_staffing
+ $staffingSchema = $this->db->getTableSchema(self::TABLE_STAFFING);
+ if (isset($staffingSchema)) {
+ $this->dropTable(self::TABLE_STAFFING);
+ }
+
+ $transaction->commit();
+ } catch (\Exception $e) {
+ $transaction->rollBack();
+ throw $e;
+ }
+ }
+}
+