Files
Hotel-Booking/Backend/alembic/versions/add_inventory_management_tables.py
Iliyan Angelov 3d634b4fce updates
2025-12-04 01:07:34 +02:00

172 lines
10 KiB
Python

"""add_inventory_management_tables
Revision ID: inventory_management_001
Revises: add_photos_to_housekeeping_tasks
Create Date: 2025-01-02 12:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
# revision identifiers, used by Alembic.
revision = 'inventory_management_001'
down_revision = 'add_photos_housekeeping'
branch_labels = None
depends_on = None
def upgrade() -> None:
# Create inventory_items table
op.create_table(
'inventory_items',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(255), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('category', sa.Enum(
'cleaning_supplies', 'linens', 'toiletries', 'amenities',
'maintenance', 'food_beverage', 'other',
name='inventorycategory'
), nullable=False),
sa.Column('unit', sa.Enum(
'piece', 'box', 'bottle', 'roll', 'pack', 'liter',
'kilogram', 'meter', 'other',
name='inventoryunit'
), nullable=False),
sa.Column('current_quantity', sa.Numeric(precision=10, scale=2), nullable=False, server_default='0'),
sa.Column('minimum_quantity', sa.Numeric(precision=10, scale=2), nullable=False, server_default='0'),
sa.Column('maximum_quantity', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('reorder_quantity', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('unit_cost', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('supplier', sa.String(255), nullable=True),
sa.Column('supplier_contact', sa.Text(), nullable=True),
sa.Column('storage_location', sa.String(255), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=False, server_default='1'),
sa.Column('is_tracked', sa.Boolean(), nullable=False, server_default='1'),
sa.Column('barcode', sa.String(100), nullable=True),
sa.Column('sku', sa.String(100), nullable=True),
sa.Column('notes', sa.Text(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.Column('created_by', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['created_by'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_inventory_items_id'), 'inventory_items', ['id'], unique=False)
op.create_index(op.f('ix_inventory_items_name'), 'inventory_items', ['name'], unique=False)
op.create_index(op.f('ix_inventory_items_barcode'), 'inventory_items', ['barcode'], unique=True)
op.create_index(op.f('ix_inventory_items_sku'), 'inventory_items', ['sku'], unique=True)
# Create inventory_transactions table
op.create_table(
'inventory_transactions',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('item_id', sa.Integer(), nullable=False),
sa.Column('transaction_type', sa.Enum(
'consumption', 'adjustment', 'received', 'transfer',
'damaged', 'returned',
name='transactiontype'
), nullable=False),
sa.Column('quantity', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('quantity_before', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('quantity_after', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('reference_type', sa.String(50), nullable=True),
sa.Column('reference_id', sa.Integer(), nullable=True),
sa.Column('notes', sa.Text(), nullable=True),
sa.Column('cost', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('performed_by', sa.Integer(), nullable=True),
sa.Column('transaction_date', sa.DateTime(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['item_id'], ['inventory_items.id'], ),
sa.ForeignKeyConstraint(['performed_by'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_inventory_transactions_id'), 'inventory_transactions', ['id'], unique=False)
op.create_index(op.f('ix_inventory_transactions_item_id'), 'inventory_transactions', ['item_id'], unique=False)
op.create_index(op.f('ix_inventory_transactions_reference_id'), 'inventory_transactions', ['reference_id'], unique=False)
op.create_index(op.f('ix_inventory_transactions_transaction_date'), 'inventory_transactions', ['transaction_date'], unique=False)
# Create inventory_reorder_requests table
op.create_table(
'inventory_reorder_requests',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('item_id', sa.Integer(), nullable=False),
sa.Column('requested_quantity', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('current_quantity', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('minimum_quantity', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('status', sa.Enum(
'pending', 'approved', 'ordered', 'received', 'cancelled',
name='reorderstatus'
), nullable=False, server_default='pending'),
sa.Column('priority', sa.String(20), nullable=False, server_default='normal'),
sa.Column('requested_by', sa.Integer(), nullable=False),
sa.Column('requested_at', sa.DateTime(), nullable=False),
sa.Column('notes', sa.Text(), nullable=True),
sa.Column('approved_by', sa.Integer(), nullable=True),
sa.Column('approved_at', sa.DateTime(), nullable=True),
sa.Column('approval_notes', sa.Text(), nullable=True),
sa.Column('order_number', sa.String(100), nullable=True),
sa.Column('expected_delivery_date', sa.DateTime(), nullable=True),
sa.Column('received_quantity', sa.Numeric(precision=10, scale=2), nullable=True),
sa.Column('received_at', sa.DateTime(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['item_id'], ['inventory_items.id'], ),
sa.ForeignKeyConstraint(['requested_by'], ['users.id'], ),
sa.ForeignKeyConstraint(['approved_by'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_inventory_reorder_requests_id'), 'inventory_reorder_requests', ['id'], unique=False)
op.create_index(op.f('ix_inventory_reorder_requests_item_id'), 'inventory_reorder_requests', ['item_id'], unique=False)
op.create_index(op.f('ix_inventory_reorder_requests_order_number'), 'inventory_reorder_requests', ['order_number'], unique=False)
op.create_index(op.f('ix_inventory_reorder_requests_requested_at'), 'inventory_reorder_requests', ['requested_at'], unique=False)
# Create inventory_task_consumptions table
op.create_table(
'inventory_task_consumptions',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('task_id', sa.Integer(), nullable=False),
sa.Column('item_id', sa.Integer(), nullable=False),
sa.Column('quantity', sa.Numeric(precision=10, scale=2), nullable=False),
sa.Column('notes', sa.Text(), nullable=True),
sa.Column('recorded_by', sa.Integer(), nullable=True),
sa.Column('recorded_at', sa.DateTime(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['task_id'], ['housekeeping_tasks.id'], ),
sa.ForeignKeyConstraint(['item_id'], ['inventory_items.id'], ),
sa.ForeignKeyConstraint(['recorded_by'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_inventory_task_consumptions_id'), 'inventory_task_consumptions', ['id'], unique=False)
op.create_index(op.f('ix_inventory_task_consumptions_task_id'), 'inventory_task_consumptions', ['task_id'], unique=False)
op.create_index(op.f('ix_inventory_task_consumptions_item_id'), 'inventory_task_consumptions', ['item_id'], unique=False)
op.create_index(op.f('ix_inventory_task_consumptions_recorded_at'), 'inventory_task_consumptions', ['recorded_at'], unique=False)
def downgrade() -> None:
op.drop_index(op.f('ix_inventory_task_consumptions_recorded_at'), table_name='inventory_task_consumptions')
op.drop_index(op.f('ix_inventory_task_consumptions_item_id'), table_name='inventory_task_consumptions')
op.drop_index(op.f('ix_inventory_task_consumptions_task_id'), table_name='inventory_task_consumptions')
op.drop_index(op.f('ix_inventory_task_consumptions_id'), table_name='inventory_task_consumptions')
op.drop_table('inventory_task_consumptions')
op.drop_index(op.f('ix_inventory_reorder_requests_requested_at'), table_name='inventory_reorder_requests')
op.drop_index(op.f('ix_inventory_reorder_requests_order_number'), table_name='inventory_reorder_requests')
op.drop_index(op.f('ix_inventory_reorder_requests_item_id'), table_name='inventory_reorder_requests')
op.drop_index(op.f('ix_inventory_reorder_requests_id'), table_name='inventory_reorder_requests')
op.drop_table('inventory_reorder_requests')
op.drop_index(op.f('ix_inventory_transactions_transaction_date'), table_name='inventory_transactions')
op.drop_index(op.f('ix_inventory_transactions_reference_id'), table_name='inventory_transactions')
op.drop_index(op.f('ix_inventory_transactions_item_id'), table_name='inventory_transactions')
op.drop_index(op.f('ix_inventory_transactions_id'), table_name='inventory_transactions')
op.drop_table('inventory_transactions')
op.drop_index(op.f('ix_inventory_items_sku'), table_name='inventory_items')
op.drop_index(op.f('ix_inventory_items_barcode'), table_name='inventory_items')
op.drop_index(op.f('ix_inventory_items_name'), table_name='inventory_items')
op.drop_index(op.f('ix_inventory_items_id'), table_name='inventory_items')
op.drop_table('inventory_items')