"""suppliers purchases payables payments

Revision ID: 75583021dc7e
Revises: 32affdb9c7fb
Create Date: 2026-05-04 13:25:43.288565

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '75583021dc7e'
down_revision = '32affdb9c7fb'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('suppliers',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=160), nullable=False),
    sa.Column('document', sa.String(length=40), nullable=True),
    sa.Column('contact_name', sa.String(length=120), nullable=True),
    sa.Column('phone', sa.String(length=40), nullable=True),
    sa.Column('email', sa.String(length=190), nullable=True),
    sa.Column('address', sa.String(length=300), nullable=True),
    sa.Column('notes', sa.Text(), nullable=True),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('suppliers', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_suppliers_document'), ['document'], unique=False)
        batch_op.create_index(batch_op.f('ix_suppliers_is_active'), ['is_active'], unique=False)
        batch_op.create_index(batch_op.f('ix_suppliers_name'), ['name'], unique=True)

    op.create_table('purchases',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('number', sa.String(length=20), nullable=False),
    sa.Column('supplier_id', sa.Integer(), nullable=False),
    sa.Column('status', sa.Enum('DRAFT', 'RECEIVED', 'CANCELLED', name='purchasestatus', native_enum=False, length=20), nullable=False),
    sa.Column('subtotal', sa.Numeric(precision=12, scale=2), nullable=False),
    sa.Column('discount', sa.Numeric(precision=12, scale=2), nullable=False),
    sa.Column('total', sa.Numeric(precision=12, scale=2), nullable=False),
    sa.Column('currency', sa.String(length=8), nullable=False),
    sa.Column('notes', sa.Text(), nullable=True),
    sa.Column('user_id', sa.Integer(), 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(['supplier_id'], ['suppliers.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('purchases', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_purchases_created_at'), ['created_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_purchases_number'), ['number'], unique=True)
        batch_op.create_index(batch_op.f('ix_purchases_received_at'), ['received_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_purchases_status'), ['status'], unique=False)
        batch_op.create_index(batch_op.f('ix_purchases_supplier_id'), ['supplier_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_purchases_user_id'), ['user_id'], unique=False)

    op.create_table('accounts_payable',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('supplier_id', sa.Integer(), nullable=False),
    sa.Column('purchase_id', sa.Integer(), nullable=True),
    sa.Column('total', sa.Numeric(precision=12, scale=2), nullable=False),
    sa.Column('paid', sa.Numeric(precision=12, scale=2), nullable=False),
    sa.Column('currency', sa.String(length=8), nullable=False),
    sa.Column('due_date', sa.Date(), nullable=True),
    sa.Column('status', sa.Enum('PENDING', 'PARTIAL', 'PAID', 'CANCELLED', name='payablestatus', native_enum=False, length=20), nullable=False),
    sa.Column('notes', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['purchase_id'], ['purchases.id'], ),
    sa.ForeignKeyConstraint(['supplier_id'], ['suppliers.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('accounts_payable', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_accounts_payable_created_at'), ['created_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_accounts_payable_due_date'), ['due_date'], unique=False)
        batch_op.create_index(batch_op.f('ix_accounts_payable_purchase_id'), ['purchase_id'], unique=True)
        batch_op.create_index(batch_op.f('ix_accounts_payable_status'), ['status'], unique=False)
        batch_op.create_index(batch_op.f('ix_accounts_payable_supplier_id'), ['supplier_id'], unique=False)

    op.create_table('purchase_items',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('purchase_id', sa.Integer(), nullable=False),
    sa.Column('product_id', sa.Integer(), nullable=False),
    sa.Column('quantity', sa.Integer(), nullable=False),
    sa.Column('unit_cost', sa.Numeric(precision=12, scale=2), nullable=False),
    sa.Column('line_total', sa.Numeric(precision=12, scale=2), nullable=False),
    sa.ForeignKeyConstraint(['product_id'], ['products.id'], ),
    sa.ForeignKeyConstraint(['purchase_id'], ['purchases.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('purchase_items', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_purchase_items_product_id'), ['product_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_purchase_items_purchase_id'), ['purchase_id'], unique=False)

    op.create_table('payments',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('account_payable_id', sa.Integer(), nullable=True),
    sa.Column('amount', sa.Numeric(precision=12, scale=2), nullable=False),
    sa.Column('currency', sa.String(length=8), nullable=False),
    sa.Column('method', sa.String(length=40), nullable=True),
    sa.Column('reference', sa.String(length=120), nullable=True),
    sa.Column('paid_at', sa.DateTime(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('notes', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['account_payable_id'], ['accounts_payable.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('payments', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_payments_account_payable_id'), ['account_payable_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_payments_paid_at'), ['paid_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_payments_user_id'), ['user_id'], unique=False)

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('payments', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_payments_user_id'))
        batch_op.drop_index(batch_op.f('ix_payments_paid_at'))
        batch_op.drop_index(batch_op.f('ix_payments_account_payable_id'))

    op.drop_table('payments')
    with op.batch_alter_table('purchase_items', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_purchase_items_purchase_id'))
        batch_op.drop_index(batch_op.f('ix_purchase_items_product_id'))

    op.drop_table('purchase_items')
    with op.batch_alter_table('accounts_payable', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_accounts_payable_supplier_id'))
        batch_op.drop_index(batch_op.f('ix_accounts_payable_status'))
        batch_op.drop_index(batch_op.f('ix_accounts_payable_purchase_id'))
        batch_op.drop_index(batch_op.f('ix_accounts_payable_due_date'))
        batch_op.drop_index(batch_op.f('ix_accounts_payable_created_at'))

    op.drop_table('accounts_payable')
    with op.batch_alter_table('purchases', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_purchases_user_id'))
        batch_op.drop_index(batch_op.f('ix_purchases_supplier_id'))
        batch_op.drop_index(batch_op.f('ix_purchases_status'))
        batch_op.drop_index(batch_op.f('ix_purchases_received_at'))
        batch_op.drop_index(batch_op.f('ix_purchases_number'))
        batch_op.drop_index(batch_op.f('ix_purchases_created_at'))

    op.drop_table('purchases')
    with op.batch_alter_table('suppliers', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_suppliers_name'))
        batch_op.drop_index(batch_op.f('ix_suppliers_is_active'))
        batch_op.drop_index(batch_op.f('ix_suppliers_document'))

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