"""initial accounting models

Revision ID: 6c8249a2ba7e
Revises: 
Create Date: 2026-04-24 10:45:12.789931

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '6c8249a2ba7e'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('currency',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('currency_code', sa.String(length=10), nullable=False),
    sa.Column('currency_name', sa.String(length=50), nullable=False),
    sa.Column('symbol', sa.String(length=10), nullable=True),
    sa.Column('decimal_places', sa.Integer(), nullable=False),
    sa.Column('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('currency', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_currency_currency_code'), ['currency_code'], unique=True)

    op.create_table('feature_module',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('module_code', sa.String(length=50), nullable=False),
    sa.Column('module_name', sa.String(length=100), nullable=False),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('is_core', sa.Boolean(), nullable=False),
    sa.Column('sort_order', sa.Integer(), nullable=False),
    sa.Column('active', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('module_code', name='uq_feature_module_code')
    )
    op.create_table('permission',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('module_name', sa.String(length=100), nullable=False),
    sa.Column('action_name', sa.String(length=100), nullable=False),
    sa.Column('permission_key', sa.String(length=150), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('permission_key', name='uq_permission_key')
    )
    with op.batch_alter_table('permission', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_permission_module_name'), ['module_name'], unique=False)

    op.create_table('company',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_code', sa.String(length=20), nullable=False),
    sa.Column('company_name', sa.String(length=150), nullable=False),
    sa.Column('legal_name', sa.String(length=200), nullable=True),
    sa.Column('gstin', sa.String(length=20), nullable=True),
    sa.Column('tax_id', sa.String(length=30), nullable=True),
    sa.Column('pan', sa.String(length=20), nullable=True),
    sa.Column('registration_no', sa.String(length=50), nullable=True),
    sa.Column('email', sa.String(length=120), nullable=True),
    sa.Column('phone', sa.String(length=30), nullable=True),
    sa.Column('address_line_1', sa.String(length=255), nullable=True),
    sa.Column('address_line_2', sa.String(length=255), nullable=True),
    sa.Column('city', sa.String(length=100), nullable=True),
    sa.Column('state', sa.String(length=100), nullable=True),
    sa.Column('country', sa.String(length=100), nullable=True),
    sa.Column('pincode', sa.String(length=20), nullable=True),
    sa.Column('logo_path', sa.String(length=255), nullable=True),
    sa.Column('base_currency_id', sa.Integer(), nullable=True),
    sa.Column('active', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['base_currency_id'], ['currency.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('company', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_company_company_code'), ['company_code'], unique=True)

    op.create_table('account_group',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('group_name', sa.String(length=120), nullable=False),
    sa.Column('parent_group_id', sa.Integer(), nullable=True),
    sa.Column('nature', sa.String(length=30), nullable=False),
    sa.Column('affects_gross_profit', sa.Boolean(), nullable=False),
    sa.Column('sort_order', sa.Integer(), nullable=False),
    sa.Column('active', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.Column('is_deleted', sa.Boolean(), nullable=False),
    sa.Column('deleted_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['company_id'], ['company.id'], ),
    sa.ForeignKeyConstraint(['parent_group_id'], ['account_group.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('company_id', 'group_name', name='uq_account_group_company_name')
    )
    with op.batch_alter_table('account_group', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_account_group_company_id'), ['company_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_account_group_nature'), ['nature'], unique=False)

    op.create_table('branch',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('branch_code', sa.String(length=20), nullable=False),
    sa.Column('branch_name', sa.String(length=150), nullable=False),
    sa.Column('address', sa.String(length=255), nullable=True),
    sa.Column('city', sa.String(length=100), nullable=True),
    sa.Column('state', sa.String(length=100), nullable=True),
    sa.Column('country', sa.String(length=100), nullable=True),
    sa.Column('pincode', sa.String(length=20), nullable=True),
    sa.Column('phone', sa.String(length=30), nullable=True),
    sa.Column('email', sa.String(length=120), nullable=True),
    sa.Column('active', sa.Boolean(), 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.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('company_id', 'branch_code', name='uq_branch_company_code')
    )
    with op.batch_alter_table('branch', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_branch_company_id'), ['company_id'], unique=False)

    op.create_table('company_module_config',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('module_id', sa.Integer(), nullable=False),
    sa.Column('is_enabled', sa.Boolean(), nullable=False),
    sa.Column('enabled_on', sa.DateTime(), nullable=True),
    sa.Column('settings_json', sa.JSON(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['company_id'], ['company.id'], ),
    sa.ForeignKeyConstraint(['module_id'], ['feature_module.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('company_id', 'module_id', name='uq_company_module_config')
    )
    with op.batch_alter_table('company_module_config', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_company_module_config_company_id'), ['company_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_company_module_config_module_id'), ['module_id'], unique=False)

    op.create_table('cost_center',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('center_code', sa.String(length=20), nullable=False),
    sa.Column('center_name', sa.String(length=150), nullable=False),
    sa.Column('parent_id', sa.Integer(), nullable=True),
    sa.Column('active', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.Column('is_deleted', sa.Boolean(), nullable=False),
    sa.Column('deleted_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['company_id'], ['company.id'], ),
    sa.ForeignKeyConstraint(['parent_id'], ['cost_center.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('company_id', 'center_code', name='uq_cost_center_company_code')
    )
    with op.batch_alter_table('cost_center', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_cost_center_company_id'), ['company_id'], unique=False)

    op.create_table('financial_year',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('fy_name', sa.String(length=20), nullable=False),
    sa.Column('start_date', sa.Date(), nullable=False),
    sa.Column('end_date', sa.Date(), nullable=False),
    sa.Column('is_current', sa.Boolean(), nullable=False),
    sa.Column('is_closed', sa.Boolean(), nullable=False),
    sa.Column('lock_date', sa.Date(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.CheckConstraint('start_date <= end_date', name='ck_financial_year_dates'),
    sa.ForeignKeyConstraint(['company_id'], ['company.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('company_id', 'fy_name', name='uq_financial_year_company_name')
    )
    with op.batch_alter_table('financial_year', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_financial_year_company_id'), ['company_id'], unique=False)

    op.create_table('role',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('role_name', sa.String(length=100), nullable=False),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('active', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.Column('is_deleted', sa.Boolean(), nullable=False),
    sa.Column('deleted_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['company_id'], ['company.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('company_id', 'role_name', name='uq_role_company_name')
    )
    with op.batch_alter_table('role', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_role_company_id'), ['company_id'], unique=False)

    op.create_table('users',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('username', sa.String(length=80), nullable=False),
    sa.Column('full_name', sa.String(length=150), nullable=False),
    sa.Column('email', sa.String(length=120), nullable=False),
    sa.Column('password_hash', sa.String(length=255), nullable=False),
    sa.Column('mobile', sa.String(length=20), nullable=True),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.Column('last_login', sa.DateTime(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.Column('is_deleted', sa.Boolean(), nullable=False),
    sa.Column('deleted_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['company_id'], ['company.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('company_id', 'email', name='uq_user_company_email'),
    sa.UniqueConstraint('company_id', 'username', name='uq_user_company_username')
    )
    with op.batch_alter_table('users', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_users_company_id'), ['company_id'], unique=False)

    op.create_table('voucher_type',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('type_code', sa.String(length=20), nullable=False),
    sa.Column('type_name', sa.String(length=100), nullable=False),
    sa.Column('numbering_method', sa.String(length=20), nullable=False),
    sa.Column('prefix', sa.String(length=20), nullable=True),
    sa.Column('current_number', sa.Integer(), nullable=False),
    sa.Column('requires_approval', sa.Boolean(), nullable=False),
    sa.Column('active', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.Column('is_deleted', sa.Boolean(), nullable=False),
    sa.Column('deleted_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['company_id'], ['company.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('company_id', 'type_code', name='uq_voucher_type_company_code'),
    sa.UniqueConstraint('company_id', 'type_name', name='uq_voucher_type_company_name')
    )
    with op.batch_alter_table('voucher_type', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_voucher_type_company_id'), ['company_id'], unique=False)

    op.create_table('account_ledger',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('ledger_code', sa.String(length=30), nullable=False),
    sa.Column('ledger_name', sa.String(length=150), nullable=False),
    sa.Column('group_id', sa.Integer(), nullable=False),
    sa.Column('ledger_type', sa.String(length=30), nullable=False),
    sa.Column('opening_balance', sa.Numeric(precision=18, scale=2), nullable=False),
    sa.Column('opening_balance_type', sa.String(length=10), nullable=False),
    sa.Column('gst_applicable', sa.Boolean(), nullable=False),
    sa.Column('tds_applicable', sa.Boolean(), nullable=False),
    sa.Column('bill_wise_enabled', sa.Boolean(), nullable=False),
    sa.Column('cost_center_enabled', sa.Boolean(), nullable=False),
    sa.Column('branch_wise_enabled', sa.Boolean(), nullable=False),
    sa.Column('active', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.Column('is_deleted', sa.Boolean(), nullable=False),
    sa.Column('deleted_at', sa.DateTime(), nullable=True),
    sa.CheckConstraint('opening_balance >= 0', name='ck_ledger_opening_balance_positive'),
    sa.ForeignKeyConstraint(['company_id'], ['company.id'], ),
    sa.ForeignKeyConstraint(['group_id'], ['account_group.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('company_id', 'ledger_code', name='uq_ledger_company_code'),
    sa.UniqueConstraint('company_id', 'ledger_name', name='uq_ledger_company_name')
    )
    with op.batch_alter_table('account_ledger', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_account_ledger_company_id'), ['company_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_account_ledger_group_id'), ['group_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_account_ledger_ledger_type'), ['ledger_type'], unique=False)

    op.create_table('audit_log',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('module_name', sa.String(length=100), nullable=False),
    sa.Column('entity_name', sa.String(length=100), nullable=False),
    sa.Column('entity_id', sa.String(length=50), nullable=False),
    sa.Column('action', sa.String(length=50), nullable=False),
    sa.Column('old_value_json', sa.JSON(), nullable=True),
    sa.Column('new_value_json', sa.JSON(), nullable=True),
    sa.Column('ip_address', sa.String(length=64), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['company_id'], ['company.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('audit_log', schema=None) as batch_op:
        batch_op.create_index('ix_audit_company_module_entity', ['company_id', 'module_name', 'entity_name', 'entity_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_audit_log_company_id'), ['company_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_audit_log_created_at'), ['created_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_audit_log_user_id'), ['user_id'], unique=False)

    op.create_table('role_permission',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('role_id', sa.Integer(), nullable=False),
    sa.Column('permission_id', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['permission_id'], ['permission.id'], ),
    sa.ForeignKeyConstraint(['role_id'], ['role.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('role_id', 'permission_id', name='uq_role_permission')
    )
    with op.batch_alter_table('role_permission', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_role_permission_permission_id'), ['permission_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_role_permission_role_id'), ['role_id'], unique=False)

    op.create_table('user_role',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('role_id', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['role_id'], ['role.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('user_id', 'role_id', name='uq_user_role')
    )
    with op.batch_alter_table('user_role', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_user_role_role_id'), ['role_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_user_role_user_id'), ['user_id'], unique=False)

    op.create_table('voucher',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('branch_id', sa.Integer(), nullable=True),
    sa.Column('financial_year_id', sa.Integer(), nullable=False),
    sa.Column('voucher_type_id', sa.Integer(), nullable=False),
    sa.Column('voucher_no', sa.String(length=50), nullable=False),
    sa.Column('voucher_date', sa.Date(), nullable=False),
    sa.Column('reference_no', sa.String(length=100), nullable=True),
    sa.Column('narration', sa.Text(), nullable=True),
    sa.Column('source_module', sa.String(length=50), nullable=True),
    sa.Column('source_doc_type', sa.String(length=50), nullable=True),
    sa.Column('source_doc_id', sa.Integer(), nullable=True),
    sa.Column('external_ref_no', sa.String(length=100), nullable=True),
    sa.Column('status', sa.String(length=20), nullable=False),
    sa.Column('is_posted', sa.Boolean(), nullable=False),
    sa.Column('posted_at', sa.DateTime(), nullable=True),
    sa.Column('approved_by', sa.Integer(), nullable=True),
    sa.Column('created_by', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.Column('is_deleted', sa.Boolean(), nullable=False),
    sa.Column('deleted_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['approved_by'], ['users.id'], ),
    sa.ForeignKeyConstraint(['branch_id'], ['branch.id'], ),
    sa.ForeignKeyConstraint(['company_id'], ['company.id'], ),
    sa.ForeignKeyConstraint(['created_by'], ['users.id'], ),
    sa.ForeignKeyConstraint(['financial_year_id'], ['financial_year.id'], ),
    sa.ForeignKeyConstraint(['voucher_type_id'], ['voucher_type.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('company_id', 'financial_year_id', 'voucher_no', name='uq_voucher_company_fy_no')
    )
    with op.batch_alter_table('voucher', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_voucher_branch_id'), ['branch_id'], unique=False)
        batch_op.create_index('ix_voucher_company_date_status', ['company_id', 'voucher_date', 'status'], unique=False)
        batch_op.create_index(batch_op.f('ix_voucher_company_id'), ['company_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_voucher_external_ref_no'), ['external_ref_no'], unique=False)
        batch_op.create_index(batch_op.f('ix_voucher_financial_year_id'), ['financial_year_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_voucher_status'), ['status'], unique=False)
        batch_op.create_index(batch_op.f('ix_voucher_voucher_date'), ['voucher_date'], unique=False)
        batch_op.create_index(batch_op.f('ix_voucher_voucher_type_id'), ['voucher_type_id'], unique=False)

    op.create_table('document_attachment',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('module_name', sa.String(length=100), nullable=False),
    sa.Column('entity_type', sa.String(length=100), nullable=False),
    sa.Column('entity_id', sa.Integer(), nullable=False),
    sa.Column('file_name', sa.String(length=255), nullable=False),
    sa.Column('file_path', sa.String(length=255), nullable=False),
    sa.Column('uploaded_by', sa.Integer(), nullable=True),
    sa.Column('uploaded_at', sa.DateTime(), nullable=False),
    sa.Column('voucher_id', sa.Integer(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['company_id'], ['company.id'], ),
    sa.ForeignKeyConstraint(['uploaded_by'], ['users.id'], ),
    sa.ForeignKeyConstraint(['voucher_id'], ['voucher.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('document_attachment', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_document_attachment_company_id'), ['company_id'], unique=False)
        batch_op.create_index('ix_document_attachment_module_entity', ['company_id', 'module_name', 'entity_type', 'entity_id'], unique=False)

    op.create_table('ledger_balance_daily',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('company_id', sa.Integer(), nullable=False),
    sa.Column('ledger_id', sa.Integer(), nullable=False),
    sa.Column('balance_date', sa.Date(), nullable=False),
    sa.Column('debit_total', sa.Numeric(precision=18, scale=2), nullable=False),
    sa.Column('credit_total', sa.Numeric(precision=18, scale=2), nullable=False),
    sa.Column('closing_balance', sa.Numeric(precision=18, scale=2), nullable=False),
    sa.Column('closing_type', sa.String(length=10), nullable=False),
    sa.ForeignKeyConstraint(['company_id'], ['company.id'], ),
    sa.ForeignKeyConstraint(['ledger_id'], ['account_ledger.id'], ),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('company_id', 'ledger_id', 'balance_date', name='uq_ledger_balance_daily')
    )
    with op.batch_alter_table('ledger_balance_daily', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_ledger_balance_daily_balance_date'), ['balance_date'], unique=False)
        batch_op.create_index(batch_op.f('ix_ledger_balance_daily_company_id'), ['company_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_ledger_balance_daily_ledger_id'), ['ledger_id'], unique=False)

    op.create_table('voucher_entry',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('voucher_id', sa.Integer(), nullable=False),
    sa.Column('ledger_id', sa.Integer(), nullable=False),
    sa.Column('cost_center_id', sa.Integer(), nullable=True),
    sa.Column('branch_id', sa.Integer(), nullable=True),
    sa.Column('debit_amount', sa.Numeric(precision=18, scale=2), nullable=False),
    sa.Column('credit_amount', sa.Numeric(precision=18, scale=2), nullable=False),
    sa.Column('line_narration', sa.String(length=255), nullable=True),
    sa.Column('party_type', sa.String(length=30), nullable=True),
    sa.Column('party_id', sa.Integer(), nullable=True),
    sa.Column('bill_ref_no', sa.String(length=50), nullable=True),
    sa.Column('bill_ref_type', sa.String(length=30), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.CheckConstraint('(debit_amount > 0 AND credit_amount = 0) OR (credit_amount > 0 AND debit_amount = 0)', name='ck_voucher_entry_single_side'),
    sa.CheckConstraint('credit_amount >= 0', name='ck_voucher_entry_credit_positive'),
    sa.CheckConstraint('debit_amount >= 0', name='ck_voucher_entry_debit_positive'),
    sa.ForeignKeyConstraint(['branch_id'], ['branch.id'], ),
    sa.ForeignKeyConstraint(['cost_center_id'], ['cost_center.id'], ),
    sa.ForeignKeyConstraint(['ledger_id'], ['account_ledger.id'], ),
    sa.ForeignKeyConstraint(['voucher_id'], ['voucher.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('voucher_entry', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_voucher_entry_branch_id'), ['branch_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_voucher_entry_cost_center_id'), ['cost_center_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_voucher_entry_ledger_id'), ['ledger_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_voucher_entry_voucher_id'), ['voucher_id'], unique=False)

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('voucher_entry', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_voucher_entry_voucher_id'))
        batch_op.drop_index(batch_op.f('ix_voucher_entry_ledger_id'))
        batch_op.drop_index(batch_op.f('ix_voucher_entry_cost_center_id'))
        batch_op.drop_index(batch_op.f('ix_voucher_entry_branch_id'))

    op.drop_table('voucher_entry')
    with op.batch_alter_table('ledger_balance_daily', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_ledger_balance_daily_ledger_id'))
        batch_op.drop_index(batch_op.f('ix_ledger_balance_daily_company_id'))
        batch_op.drop_index(batch_op.f('ix_ledger_balance_daily_balance_date'))

    op.drop_table('ledger_balance_daily')
    with op.batch_alter_table('document_attachment', schema=None) as batch_op:
        batch_op.drop_index('ix_document_attachment_module_entity')
        batch_op.drop_index(batch_op.f('ix_document_attachment_company_id'))

    op.drop_table('document_attachment')
    with op.batch_alter_table('voucher', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_voucher_voucher_type_id'))
        batch_op.drop_index(batch_op.f('ix_voucher_voucher_date'))
        batch_op.drop_index(batch_op.f('ix_voucher_status'))
        batch_op.drop_index(batch_op.f('ix_voucher_financial_year_id'))
        batch_op.drop_index(batch_op.f('ix_voucher_external_ref_no'))
        batch_op.drop_index(batch_op.f('ix_voucher_company_id'))
        batch_op.drop_index('ix_voucher_company_date_status')
        batch_op.drop_index(batch_op.f('ix_voucher_branch_id'))

    op.drop_table('voucher')
    with op.batch_alter_table('user_role', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_user_role_user_id'))
        batch_op.drop_index(batch_op.f('ix_user_role_role_id'))

    op.drop_table('user_role')
    with op.batch_alter_table('role_permission', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_role_permission_role_id'))
        batch_op.drop_index(batch_op.f('ix_role_permission_permission_id'))

    op.drop_table('role_permission')
    with op.batch_alter_table('audit_log', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_audit_log_user_id'))
        batch_op.drop_index(batch_op.f('ix_audit_log_created_at'))
        batch_op.drop_index(batch_op.f('ix_audit_log_company_id'))
        batch_op.drop_index('ix_audit_company_module_entity')

    op.drop_table('audit_log')
    with op.batch_alter_table('account_ledger', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_account_ledger_ledger_type'))
        batch_op.drop_index(batch_op.f('ix_account_ledger_group_id'))
        batch_op.drop_index(batch_op.f('ix_account_ledger_company_id'))

    op.drop_table('account_ledger')
    with op.batch_alter_table('voucher_type', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_voucher_type_company_id'))

    op.drop_table('voucher_type')
    with op.batch_alter_table('users', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_users_company_id'))

    op.drop_table('users')
    with op.batch_alter_table('role', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_role_company_id'))

    op.drop_table('role')
    with op.batch_alter_table('financial_year', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_financial_year_company_id'))

    op.drop_table('financial_year')
    with op.batch_alter_table('cost_center', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_cost_center_company_id'))

    op.drop_table('cost_center')
    with op.batch_alter_table('company_module_config', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_company_module_config_module_id'))
        batch_op.drop_index(batch_op.f('ix_company_module_config_company_id'))

    op.drop_table('company_module_config')
    with op.batch_alter_table('branch', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_branch_company_id'))

    op.drop_table('branch')
    with op.batch_alter_table('account_group', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_account_group_nature'))
        batch_op.drop_index(batch_op.f('ix_account_group_company_id'))

    op.drop_table('account_group')
    with op.batch_alter_table('company', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_company_company_code'))

    op.drop_table('company')
    with op.batch_alter_table('permission', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_permission_module_name'))

    op.drop_table('permission')
    op.drop_table('feature_module')
    with op.batch_alter_table('currency', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_currency_currency_code'))

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