"""refunds + refund_items: money given back for paid items; key ordering.refunds for owners and managers (plan step 3.3e) Revision ID: 0016_refunds Revises: 0015_item_claims Create Date: 2026-11-15 18:00:00 """ from alembic import op import sqlalchemy as sa from core.timeutil import UTCDateTime revision = '0016_refunds' down_revision = '0015_item_claims' branch_labels = None depends_on = None def upgrade() -> None: op.create_table('refunds', sa.Column('id', sa.Integer(), nullable=False), sa.Column('order_id', sa.Integer(), nullable=False), sa.Column('business_day_id', sa.Integer(), nullable=True), sa.Column('shift_id', sa.Integer(), nullable=True), sa.Column('refunded_by', sa.Integer(), nullable=False), sa.Column('approved_by', sa.Integer(), nullable=True), sa.Column('device_id', sa.Integer(), nullable=True), sa.Column('method', sa.String(), nullable=False), sa.Column('amount', sa.Float(), nullable=False), sa.Column('reason', sa.Text(), nullable=True), sa.Column('fiscal_status', sa.String(), nullable=True), sa.Column('fiscal_reference', sa.String(), nullable=True), sa.Column('created_at', UTCDateTime(), nullable=False), sa.ForeignKeyConstraint(['order_id'], ['orders.id']), sa.ForeignKeyConstraint(['business_day_id'], ['business_days.id']), sa.ForeignKeyConstraint(['shift_id'], ['waiter_shifts.id']), sa.ForeignKeyConstraint(['refunded_by'], ['users.id']), sa.ForeignKeyConstraint(['approved_by'], ['users.id']), sa.ForeignKeyConstraint(['device_id'], ['devices.id']), sa.PrimaryKeyConstraint('id'), ) with op.batch_alter_table('refunds', schema=None) as batch_op: batch_op.create_index(batch_op.f('ix_refunds_id'), ['id'], unique=False) batch_op.create_index(batch_op.f('ix_refunds_order_id'), ['order_id'], unique=False) batch_op.create_index(batch_op.f('ix_refunds_business_day_id'), ['business_day_id'], unique=False) batch_op.create_index(batch_op.f('ix_refunds_shift_id'), ['shift_id'], unique=False) op.create_table('refund_items', sa.Column('id', sa.Integer(), nullable=False), sa.Column('refund_id', sa.Integer(), nullable=False), sa.Column('order_item_id', sa.Integer(), nullable=False), sa.Column('quantity', sa.Float(), nullable=False), sa.Column('amount', sa.Float(), nullable=False), sa.ForeignKeyConstraint(['refund_id'], ['refunds.id'], ondelete='CASCADE'), sa.ForeignKeyConstraint(['order_item_id'], ['order_items.id']), sa.PrimaryKeyConstraint('id'), ) with op.batch_alter_table('refund_items', schema=None) as batch_op: batch_op.create_index(batch_op.f('ix_refund_items_id'), ['id'], unique=False) batch_op.create_index(batch_op.f('ix_refund_items_refund_id'), ['refund_id'], unique=False) batch_op.create_index(batch_op.f('ix_refund_items_order_item_id'), ['order_item_id'], unique=False) # the new key `ordering.refunds` for the roles whose template has it (new users get it from # the template; existing owners and managers here) op.get_bind().execute(sa.text("insert or ignore into user_permissions (user_id, key) " "select id, 'ordering.refunds' from users where role in ('owner', 'store_manager')")) def downgrade() -> None: op.get_bind().execute(sa.text("delete from user_permissions where key = 'ordering.refunds'")) op.drop_table('refund_items') op.drop_table('refunds')