# Generated by Django 5.2.19 on 2026-11-29 02:41

import django.db.models.deletion
import uuid
from decimal import Decimal
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('accounting', '0003_remove_voucher_uniq_company_voucher_no_and_more'),
        ('banking', '0001_initial'),
        ('projects', '0001_initial'),
        ('tenancy', '0002_company_multi_currency_enabled'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='BankFacility',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('facility_code', models.CharField(max_length=40)),
                ('facility_name', models.CharField(max_length=200)),
                ('facility_type', models.CharField(choices=[('TERM_LOAN', 'Term Loan'), ('OVERDRAFT', 'Overdraft (OD)'), ('CASH_CREDIT', 'Cash Credit (CC)'), ('SHORT_TERM_WC', 'Short-Term Working Capital'), ('PERMANENT_WC_TERM', 'Permanent Working Capital Term Loan'), ('DEMAND_LOAN', 'Demand Loan'), ('CONTRACT_LOAN', 'Contract Loan'), ('PROJECT_LOAN', 'Project Loan'), ('EQUIPMENT_LOAN', 'Equipment & Machinery Loan'), ('IMPORT_TR_LOAN', 'Import / Trust Receipt (TR) Loan'), ('EXPORT_FINANCE', 'Export Finance'), ('BANK_GUARANTEE', 'Bank Guarantee Line'), ('LETTER_OF_CREDIT', 'Letter of Credit (LC)'), ('CONSORTIUM', 'Consortium Financing'), ('OTHER', 'Other Credit Facility')], max_length=24)),
                ('sanction_ref', models.CharField(blank=True, max_length=80)),
                ('sanction_date', models.DateField()),
                ('maturity_date', models.DateField()),
                ('renewal_date', models.DateField(blank=True, null=True)),
                ('currency', models.CharField(default='NPR', max_length=3)),
                ('sanctioned_limit', models.DecimalField(decimal_places=2, max_digits=18)),
                ('interest_mode', models.CharField(choices=[('FIXED', 'Fixed Rate'), ('FLOATING', 'Floating (Base Rate + Premium)')], default='FLOATING', max_length=12)),
                ('base_rate', models.DecimalField(decimal_places=2, default=Decimal('8.50'), max_digits=5)),
                ('premium_rate', models.DecimalField(decimal_places=2, default=Decimal('2.00'), max_digits=5)),
                ('penal_rate', models.DecimalField(decimal_places=2, default=Decimal('2.00'), max_digits=5)),
                ('schedule_method', models.CharField(choices=[('EMI', 'Equal Monthly/Periodic Installment (EMI)'), ('EQUAL_PRINCIPAL', 'Equal Principal + Reducing Interest'), ('REDUCING_BALANCE', 'Reducing Balance'), ('BULLET', 'Bullet Repayment')], default='EMI', max_length=20)),
                ('repayment_frequency', models.CharField(choices=[('MONTHLY', 'Monthly'), ('QUARTERLY', 'Quarterly'), ('HALF_YEARLY', 'Half-Yearly'), ('ANNUAL', 'Annual'), ('BULLET', 'Bullet at Maturity')], default='MONTHLY', max_length=16)),
                ('tenure_months', models.PositiveIntegerField(default=36)),
                ('moratorium_months', models.PositiveSmallIntegerField(default=0)),
                ('status', models.CharField(choices=[('APPLIED', 'Applied'), ('SANCTIONED', 'Sanctioned'), ('ACTIVE', 'Active'), ('RESTRUCTURED', 'Restructured'), ('CLOSED', 'Closed / Settled')], default='ACTIVE', max_length=16)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('bank_account', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='facilities', to='banking.bankaccount')),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bank_facilities', to='tenancy.company')),
                ('linked_interest_account', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='interest_facilities', to='accounting.account')),
                ('linked_liability_account', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='loan_facilities', to='accounting.account')),
                ('project', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='facilities', to='projects.project')),
            ],
            options={
                'ordering': ['facility_code'],
                'unique_together': {('company', 'facility_code')},
            },
        ),
        migrations.CreateModel(
            name='DrawingPowerAssessment',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('assessment_date', models.DateField()),
                ('eligible_stock', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('eligible_receivables', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('stock_haircut_pct', models.DecimalField(decimal_places=2, max_digits=5)),
                ('receivable_haircut_pct', models.DecimalField(decimal_places=2, max_digits=5)),
                ('computed_drawing_power', models.DecimalField(decimal_places=2, max_digits=18)),
                ('effective_limit', models.DecimalField(decimal_places=2, max_digits=18)),
                ('facility', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='dp_assessments', to='loans.bankfacility')),
            ],
        ),
        migrations.CreateModel(
            name='LoanCollateral',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('security_type', models.CharField(choices=[('LAND_BUILDING', 'Land & Building'), ('EQUIPMENT_VEHICLE', 'Equipment & Vehicles'), ('FIXED_DEPOSIT', 'Fixed Deposit Lien'), ('INVENTORY_RECEIVABLES', 'Inventory & Receivables'), ('PERSONAL_GUARANTEE', 'Personal Guarantee'), ('CORPORATE_GUARANTEE', 'Corporate Guarantee')], max_length=28)),
                ('description', models.CharField(max_length=255)),
                ('owner_name', models.CharField(blank=True, max_length=150)),
                ('assessed_value', models.DecimalField(decimal_places=2, max_digits=18)),
                ('lien_mortgage_ref', models.CharField(blank=True, max_length=120)),
                ('insurance_policy_no', models.CharField(blank=True, max_length=80)),
                ('insurance_expiry', models.DateField(blank=True, null=True)),
                ('is_released', models.BooleanField(default=False)),
                ('released_at', models.DateField(blank=True, null=True)),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='tenancy.company')),
                ('facility', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='collaterals', to='loans.bankfacility')),
            ],
        ),
        migrations.CreateModel(
            name='LoanDisbursement',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('disbursement_date', models.DateField()),
                ('amount', models.DecimalField(decimal_places=2, max_digits=18)),
                ('reference_no', models.CharField(blank=True, max_length=64)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
                ('facility', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='disbursements', to='loans.bankfacility')),
                ('voucher', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='accounting.voucher')),
            ],
        ),
        migrations.CreateModel(
            name='LoanDocument',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('doc_type', models.CharField(max_length=48)),
                ('title', models.CharField(max_length=200)),
                ('document_ref', models.CharField(blank=True, max_length=120)),
                ('uploaded_at', models.DateTimeField(auto_now_add=True)),
                ('facility', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='documents', to='loans.bankfacility')),
            ],
        ),
        migrations.CreateModel(
            name='LoanRateRevision',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('effective_date', models.DateField()),
                ('base_rate', models.DecimalField(decimal_places=2, max_digits=5)),
                ('premium_rate', models.DecimalField(decimal_places=2, max_digits=5)),
                ('reason', models.CharField(blank=True, max_length=200)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
                ('facility', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rate_revisions', to='loans.bankfacility')),
            ],
        ),
        migrations.CreateModel(
            name='LoanScheduleInstallment',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('installment_no', models.PositiveSmallIntegerField()),
                ('due_date', models.DateField(db_index=True)),
                ('opening_principal', models.DecimalField(decimal_places=2, max_digits=18)),
                ('principal_due', models.DecimalField(decimal_places=2, max_digits=18)),
                ('interest_due', models.DecimalField(decimal_places=2, max_digits=18)),
                ('penal_due', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('principal_paid', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('interest_paid', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('closing_principal', models.DecimalField(decimal_places=2, max_digits=18)),
                ('status', models.CharField(choices=[('PENDING', 'Pending'), ('PARTIAL', 'Partially Paid'), ('PAID', 'Paid'), ('OVERDUE', 'Overdue')], default='PENDING', max_length=12)),
                ('facility', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='schedule', to='loans.bankfacility')),
            ],
            options={
                'ordering': ['installment_no'],
                'unique_together': {('facility', 'installment_no')},
            },
        ),
        migrations.CreateModel(
            name='LoanRepayment',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('payment_date', models.DateField()),
                ('principal_amount', models.DecimalField(decimal_places=2, max_digits=18)),
                ('interest_amount', models.DecimalField(decimal_places=2, max_digits=18)),
                ('penal_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('is_prepayment', models.BooleanField(default=False)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
                ('facility', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='repayments', to='loans.bankfacility')),
                ('voucher', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='accounting.voucher')),
                ('installment', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='loans.loanscheduleinstallment')),
            ],
        ),
        migrations.CreateModel(
            name='BankGuarantee',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('bg_number', models.CharField(max_length=64)),
                ('bg_type', models.CharField(choices=[('BID_BOND', 'Bid Bond'), ('PERFORMANCE', 'Performance Guarantee'), ('ADVANCE_PAYMENT', 'Advance Payment Guarantee'), ('RETENTION_MONEY', 'Retention Money Guarantee'), ('CUSTOMS', 'Customs Guarantee'), ('OTHER', 'Other Guarantee')], max_length=24)),
                ('beneficiary', models.CharField(max_length=200)),
                ('currency', models.CharField(default='NPR', max_length=3)),
                ('amount', models.DecimalField(decimal_places=2, max_digits=18)),
                ('margin_percent', models.DecimalField(decimal_places=2, default=Decimal('10.00'), max_digits=5)),
                ('margin_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('commission_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('issue_date', models.DateField()),
                ('expiry_date', models.DateField()),
                ('claim_expiry_date', models.DateField(blank=True, null=True)),
                ('status', models.CharField(choices=[('ACTIVE', 'Active (Contingent Exposure)'), ('EXTENDED', 'Extended'), ('RELEASED', 'Released'), ('CLAIMED', 'Claim Received'), ('INVOKED', 'Invoked (Funded Liability)')], default='ACTIVE', max_length=16)),
                ('notes', models.TextField(blank=True)),
                ('bank_account', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='guarantees', to='banking.bankaccount')),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bank_guarantees', to='tenancy.company')),
                ('facility', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='guarantees', to='loans.bankfacility')),
                ('invocation_voucher', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='bg_invocations', to='accounting.voucher')),
                ('margin_voucher', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='bg_margins', to='accounting.voucher')),
                ('project', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='guarantees', to='projects.project')),
            ],
            options={
                'ordering': ['-issue_date'],
                'unique_together': {('company', 'bg_number')},
            },
        ),
        migrations.CreateModel(
            name='DailyFacilityBalance',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('balance_date', models.DateField()),
                ('utilized_amount', models.DecimalField(decimal_places=2, max_digits=18)),
                ('applicable_rate', models.DecimalField(decimal_places=2, max_digits=5)),
                ('daily_interest', models.DecimalField(decimal_places=2, max_digits=18)),
                ('is_excess_drawing', models.BooleanField(default=False)),
                ('facility', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='daily_balances', to='loans.bankfacility')),
                ('interest_voucher', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='accounting.voucher')),
            ],
            options={
                'ordering': ['-balance_date'],
                'unique_together': {('facility', 'balance_date')},
            },
        ),
        migrations.CreateModel(
            name='NrbRegulatoryConfig',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('directive_code', models.CharField(max_length=40)),
                ('title', models.CharField(max_length=200)),
                ('effective_from', models.DateField(db_index=True)),
                ('effective_to', models.DateField(blank=True, null=True)),
                ('working_capital_cap_pct', models.DecimalField(decimal_places=2, default=Decimal('25.00'), max_digits=5)),
                ('od_max_limit_npr', models.DecimalField(decimal_places=2, default=Decimal('5000000.00'), max_digits=18)),
                ('cc_default_haircut_pct', models.DecimalField(decimal_places=2, default=Decimal('25.00'), max_digits=5)),
                ('penal_interest_cap_pct', models.DecimalField(decimal_places=2, default=Decimal('2.00'), max_digits=5)),
                ('published_base_rate', models.DecimalField(decimal_places=2, default=Decimal('8.50'), max_digits=5)),
                ('notes', models.TextField(blank=True)),
                ('is_active', models.BooleanField(default=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='nrb_configs', to='tenancy.company')),
            ],
            options={
                'ordering': ['-effective_from'],
                'unique_together': {('company', 'directive_code')},
            },
        ),
        migrations.CreateModel(
            name='RevolvingProductConfig',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('product_kind', models.CharField(choices=[('OVERDRAFT', 'Overdraft (OD)'), ('CASH_CREDIT', 'Cash Credit (CC)')], max_length=16)),
                ('requires_drawing_power', models.BooleanField(default=False)),
                ('stock_haircut_pct', models.DecimalField(decimal_places=2, default=Decimal('25.00'), max_digits=5)),
                ('receivable_haircut_pct', models.DecimalField(decimal_places=2, default=Decimal('30.00'), max_digits=5)),
                ('max_tenure_days', models.PositiveIntegerField(default=365)),
                ('allow_clean_overdraft', models.BooleanField(default=False)),
                ('eligibility_notes', models.TextField(blank=True)),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='tenancy.company')),
            ],
            options={
                'unique_together': {('company', 'product_kind')},
            },
        ),
    ]
