"""payment methods receivables and payment links

Revision ID: 145ce6700b8c
Revises: 75583021dc7e
Create Date: 2026-05-04 16:24:19.590947

"""
from alembic import op
import sqlalchemy as sa


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


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('payment_methods',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=80), nullable=False),
    sa.Column('code', sa.String(length=40), nullable=False),
    sa.Column('adjustment_percent', sa.Numeric(precision=5, scale=2), nullable=False),
    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('payment_methods', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_payment_methods_code'), ['code'], unique=True)
        batch_op.create_index(batch_op.f('ix_payment_methods_is_active'), ['is_active'], unique=False)
        batch_op.create_index(batch_op.f('ix_payment_methods_name'), ['name'], unique=True)

    op.create_table('accounts_receivable',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('customer_id', sa.Integer(), nullable=False),
    sa.Column('sale_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='receivablestatus', 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(['customer_id'], ['customers.id'], ),
    sa.ForeignKeyConstraint(['sale_id'], ['sales.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('accounts_receivable', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_accounts_receivable_created_at'), ['created_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_accounts_receivable_customer_id'), ['customer_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_accounts_receivable_due_date'), ['due_date'], unique=False)
        batch_op.create_index(batch_op.f('ix_accounts_receivable_sale_id'), ['sale_id'], unique=True)
        batch_op.create_index(batch_op.f('ix_accounts_receivable_status'), ['status'], unique=False)

    with op.batch_alter_table('payments', schema=None) as batch_op:
        batch_op.add_column(sa.Column('account_receivable_id', sa.Integer(), nullable=True))
        batch_op.add_column(sa.Column('payment_method_id', sa.Integer(), nullable=True))
        batch_op.create_index(batch_op.f('ix_payments_account_receivable_id'), ['account_receivable_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_payments_payment_method_id'), ['payment_method_id'], unique=False)
        batch_op.create_foreign_key(None, 'accounts_receivable', ['account_receivable_id'], ['id'])
        batch_op.create_foreign_key(None, 'payment_methods', ['payment_method_id'], ['id'])

    # ### 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_constraint(None, type_='foreignkey')
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_index(batch_op.f('ix_payments_payment_method_id'))
        batch_op.drop_index(batch_op.f('ix_payments_account_receivable_id'))
        batch_op.drop_column('payment_method_id')
        batch_op.drop_column('account_receivable_id')

    with op.batch_alter_table('accounts_receivable', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_accounts_receivable_status'))
        batch_op.drop_index(batch_op.f('ix_accounts_receivable_sale_id'))
        batch_op.drop_index(batch_op.f('ix_accounts_receivable_due_date'))
        batch_op.drop_index(batch_op.f('ix_accounts_receivable_customer_id'))
        batch_op.drop_index(batch_op.f('ix_accounts_receivable_created_at'))

    op.drop_table('accounts_receivable')
    with op.batch_alter_table('payment_methods', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_payment_methods_name'))
        batch_op.drop_index(batch_op.f('ix_payment_methods_is_active'))
        batch_op.drop_index(batch_op.f('ix_payment_methods_code'))

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