"""add invoice allocation tables

Revision ID: a1077ce64b5a
Revises: 597eb9bbbf0d
Create Date: 2026-04-25 22:28:27.950291

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'a1077ce64b5a'
down_revision = '597eb9bbbf0d'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('purchase_payment_allocation',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('vendor_id', sa.Integer(), nullable=False),
    sa.Column('purchase_invoice_id', sa.Integer(), nullable=False),
    sa.Column('voucher_id', sa.Integer(), nullable=False),
    sa.Column('allocated_amount', sa.Numeric(precision=18, scale=2), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['company_id'], ['company.id'], ),
    sa.ForeignKeyConstraint(['purchase_invoice_id'], ['purchase_invoice.id'], ),
    sa.ForeignKeyConstraint(['vendor_id'], ['vendor.id'], ),
    sa.ForeignKeyConstraint(['voucher_id'], ['voucher.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('voucher_id', 'purchase_invoice_id', name='uq_purchase_payment_invoice_alloc')
    )
    with op.batch_alter_table('purchase_payment_allocation', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_purchase_payment_allocation_company_id'), ['company_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_purchase_payment_allocation_purchase_invoice_id'), ['purchase_invoice_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_purchase_payment_allocation_vendor_id'), ['vendor_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_purchase_payment_allocation_voucher_id'), ['voucher_id'], unique=False)

    op.create_table('sales_receipt_allocation',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('customer_id', sa.Integer(), nullable=False),
    sa.Column('sales_invoice_id', sa.Integer(), nullable=False),
    sa.Column('voucher_id', sa.Integer(), nullable=False),
    sa.Column('allocated_amount', sa.Numeric(precision=18, scale=2), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['company_id'], ['company.id'], ),
    sa.ForeignKeyConstraint(['customer_id'], ['customer.id'], ),
    sa.ForeignKeyConstraint(['sales_invoice_id'], ['sales_invoice.id'], ),
    sa.ForeignKeyConstraint(['voucher_id'], ['voucher.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('voucher_id', 'sales_invoice_id', name='uq_sales_receipt_invoice_alloc')
    )
    with op.batch_alter_table('sales_receipt_allocation', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_sales_receipt_allocation_company_id'), ['company_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_sales_receipt_allocation_customer_id'), ['customer_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_sales_receipt_allocation_sales_invoice_id'), ['sales_invoice_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_sales_receipt_allocation_voucher_id'), ['voucher_id'], unique=False)

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('sales_receipt_allocation', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_sales_receipt_allocation_voucher_id'))
        batch_op.drop_index(batch_op.f('ix_sales_receipt_allocation_sales_invoice_id'))
        batch_op.drop_index(batch_op.f('ix_sales_receipt_allocation_customer_id'))
        batch_op.drop_index(batch_op.f('ix_sales_receipt_allocation_company_id'))

    op.drop_table('sales_receipt_allocation')
    with op.batch_alter_table('purchase_payment_allocation', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_purchase_payment_allocation_voucher_id'))
        batch_op.drop_index(batch_op.f('ix_purchase_payment_allocation_vendor_id'))
        batch_op.drop_index(batch_op.f('ix_purchase_payment_allocation_purchase_invoice_id'))
        batch_op.drop_index(batch_op.f('ix_purchase_payment_allocation_company_id'))

    op.drop_table('purchase_payment_allocation')
    # ### end Alembic commands ###
