# Generated by Django 5.2.19 on 2026-12-01 09:06

import django.core.serializers.json
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):

    dependencies = [
        ('accounting', '0010_voucherregisterviewconfig_and_more'),
        ('banking', '0004_chequeleaf_reservation_expires_at_and_more'),
        ('construction_ops', '0001_initial'),
        ('party_hub', '0001_initial'),
        ('projects', '0002_dailysitereport_projectactivity_projectadvance_and_more'),
        ('tenancy', '0003_taxconfig_calculation_rule_taxconfig_category_and_more'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.AddField(
            model_name='customeraddress',
            name='country',
            field=models.CharField(default='Nepal', max_length=80),
        ),
        migrations.AddField(
            model_name='customeraddress',
            name='district',
            field=models.CharField(blank=True, max_length=80),
        ),
        migrations.AddField(
            model_name='customeraddress',
            name='local_level',
            field=models.CharField(blank=True, max_length=120),
        ),
        migrations.AddField(
            model_name='customeraddress',
            name='province',
            field=models.CharField(blank=True, max_length=80),
        ),
        migrations.AddField(
            model_name='customeraddress',
            name='ward',
            field=models.CharField(blank=True, max_length=16),
        ),
        migrations.AddField(
            model_name='customerprofile',
            name='collection_officer',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='assigned_debtors', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='customerprofile',
            name='contract_required',
            field=models.BooleanField(default=False),
        ),
        migrations.AddField(
            model_name='customerprofile',
            name='default_currency',
            field=models.CharField(default='NPR', max_length=8),
        ),
        migrations.AddField(
            model_name='customerprofile',
            name='default_retention_rate',
            field=models.DecimalField(decimal_places=2, default=Decimal('5.00'), max_digits=5),
        ),
        migrations.AddField(
            model_name='customerprofile',
            name='default_tds_rate',
            field=models.DecimalField(decimal_places=2, default=Decimal('1.50'), max_digits=5),
        ),
        migrations.AddField(
            model_name='customerprofile',
            name='duplicate_override_reason',
            field=models.CharField(blank=True, max_length=255),
        ),
        migrations.AddField(
            model_name='customerprofile',
            name='govt_contract_exempt_credit',
            field=models.BooleanField(default=False),
        ),
        migrations.AddField(
            model_name='customerprofile',
            name='legal_name',
            field=models.CharField(blank=True, max_length=200),
        ),
        migrations.AddField(
            model_name='customerprofile',
            name='notes',
            field=models.TextField(blank=True),
        ),
        migrations.AddField(
            model_name='customerprofile',
            name='parent_customer',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='subsidiaries', to='party_hub.customerprofile'),
        ),
        migrations.AddField(
            model_name='customerprofile',
            name='required_approval_level',
            field=models.CharField(default='FINANCE_MANAGER', max_length=40),
        ),
        migrations.AddField(
            model_name='customerprofile',
            name='risk_classification',
            field=models.CharField(choices=[('LOW', 'Low Credit Risk'), ('MODERATE', 'Moderate Credit Risk'), ('HIGH', 'High Credit Risk'), ('CRITICAL', 'Critical / Restricted')], default='LOW', max_length=16),
        ),
        migrations.AddField(
            model_name='customerprofile',
            name='supporting_docs_required',
            field=models.BooleanField(default=False),
        ),
        migrations.AddField(
            model_name='customerprofile',
            name='tds_applicable',
            field=models.BooleanField(default=True),
        ),
        migrations.AddField(
            model_name='customerprofile',
            name='website',
            field=models.URLField(blank=True),
        ),
        migrations.CreateModel(
            name='ARAuditEvent',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('entity_type', models.CharField(db_index=True, max_length=40)),
                ('entity_id', models.CharField(db_index=True, max_length=64)),
                ('action', models.CharField(max_length=64)),
                ('details', models.JSONField(default=dict, encoder=django.core.serializers.json.DjangoJSONEncoder)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('actor', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ar_audit_events', to='tenancy.company')),
            ],
            options={
                'db_table': 'ar_audit_events',
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='ARControlPolicy',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('default_credit_days', models.PositiveSmallIntegerField(default=30)),
                ('default_retention_pct', models.DecimalField(decimal_places=2, default=Decimal('5.00'), max_digits=5)),
                ('default_advance_recovery_pct', models.DecimalField(decimal_places=2, default=Decimal('10.00'), max_digits=5)),
                ('dso_method', models.CharField(choices=[('AVG_RECEIVABLES', 'Average Trade Receivables / Credit Sales × Period Days'), ('CLOSING_AR', 'Closing Trade Receivables / Credit Sales × Period Days')], default='AVG_RECEIVABLES', max_length=24)),
                ('aging_buckets_json', models.JSONField(default=list, encoder=django.core.serializers.json.DjangoJSONEncoder, help_text='Ordered bucket boundaries, e.g. [0, 30, 60, 90, 180, 365]')),
                ('enforce_maker_checker_ra', models.BooleanField(default=True)),
                ('block_over_credit_limit', models.BooleanField(default=True)),
                ('include_unbilled_in_credit_exposure', models.BooleanField(default=False)),
                ('auto_reminders_enabled', models.BooleanField(default=False)),
                ('require_client_certification_before_posting', models.BooleanField(default=True)),
                ('small_writeoff_threshold', models.DecimalField(decimal_places=2, default=Decimal('5000.00'), max_digits=18)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('company', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='ar_control_policy', to='tenancy.company')),
                ('default_advance_account', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='ar_policy_advances', to='accounting.account')),
                ('default_control_account', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='ar_policy_controls', to='accounting.account')),
                ('default_retention_account', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='ar_policy_retentions', to='accounting.account')),
                ('default_revenue_account', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='ar_policy_revenue', to='accounting.account')),
                ('default_tds_receivable_account', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='ar_policy_tds', to='accounting.account')),
                ('default_vat_output_account', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='ar_policy_vat', to='accounting.account')),
                ('updated_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'ar_control_policies',
            },
        ),
        migrations.CreateModel(
            name='CustomerAdvanceContractRule',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('recovery_method', models.CharField(choices=[('PCT_CERTIFIED', 'Percentage of Certified RA Gross'), ('FIXED_PER_BILL', 'Fixed Amount per RA Bill'), ('MILESTONE', 'Milestone Based'), ('MANUAL', 'Manual Adjustment')], default='PCT_CERTIFIED', max_length=20)),
                ('recovery_rate_pct', models.DecimalField(decimal_places=2, default=Decimal('10.00'), max_digits=6)),
                ('fixed_recovery_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('min_recovery_per_bill', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('max_recovery_per_bill', models.DecimalField(blank=True, decimal_places=2, max_digits=18, null=True)),
                ('start_recovery_date', models.DateField(blank=True, null=True)),
                ('agreement_ref', models.CharField(blank=True, max_length=120)),
                ('bank_guarantee_ref', models.CharField(blank=True, max_length=120)),
                ('advance_record', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='contract_rule', to='accounting.advancerecord')),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='advance_contract_rules', to='tenancy.company')),
                ('contract', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='advance_recovery_rules', to='projects.projectcontract')),
            ],
            options={
                'db_table': 'ar_customer_advance_rules',
            },
        ),
        migrations.CreateModel(
            name='CustomerCreditHoldLog',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('action', models.CharField(choices=[('HOLD_PLACED', 'Credit Hold Placed'), ('HOLD_RELEASED', 'Credit Hold Released'), ('LIMIT_UPDATED', 'Credit Limit Updated'), ('OVERRIDE_GRANTED', 'Temporary Credit Override Granted')], max_length=24)),
                ('previous_value', models.CharField(blank=True, max_length=120)),
                ('new_value', models.CharField(blank=True, max_length=120)),
                ('reason', models.CharField(max_length=255)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('actor', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='credit_hold_logs', to='tenancy.company')),
                ('customer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='credit_hold_logs', to='party_hub.customerprofile')),
            ],
            options={
                'db_table': 'ar_customer_credit_hold_logs',
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='CustomerCreditOverride',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('override_limit', models.DecimalField(decimal_places=2, max_digits=18)),
                ('reason', models.CharField(max_length=255)),
                ('valid_until', models.DateField(blank=True, null=True)),
                ('is_active', models.BooleanField(default=True)),
                ('approved_at', models.DateTimeField(auto_now_add=True)),
                ('approved_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='approved_credit_overrides', to=settings.AUTH_USER_MODEL)),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='credit_overrides', to='tenancy.company')),
                ('customer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='credit_overrides', to='party_hub.customerprofile')),
            ],
            options={
                'db_table': 'ar_customer_credit_overrides',
                'ordering': ['-approved_at'],
            },
        ),
        migrations.CreateModel(
            name='PaymentCommitment',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('promised_date', models.DateField(db_index=True)),
                ('bs_promised_date', models.CharField(blank=True, max_length=16)),
                ('promised_amount', models.DecimalField(decimal_places=2, max_digits=18)),
                ('fulfilled_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('contact_person', models.CharField(blank=True, max_length=120)),
                ('remarks', models.CharField(blank=True, max_length=255)),
                ('status', models.CharField(choices=[('PENDING', 'Pending Due Date'), ('FULFILLED', 'Fulfilled / Paid'), ('PARTIAL', 'Partially Fulfilled'), ('BROKEN', 'Broken / Missed'), ('CANCELLED', 'Cancelled')], db_index=True, default='PENDING', max_length=16)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('collection_activity', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='commitments', to='party_hub.collectionactivity')),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ar_payment_commitments', to='tenancy.company')),
                ('open_item', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='commitments', to='party_hub.openitem')),
                ('party', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ar_payment_commitments', to='accounting.party')),
                ('recorded_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'ar_payment_commitments',
                'ordering': ['promised_date', '-created_at'],
            },
        ),
        migrations.CreateModel(
            name='RABill',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('bill_category', models.CharField(choices=[('PROGRESS_RA', 'Running Account (RA) Progress Bill'), ('INTERIM_VALUATION', 'Interim Payment Certificate (IPC)'), ('VARIATION_CLAIM', 'Variation / Extra Work Claim'), ('FINAL_BILL', 'Final Contract Settlement Bill')], default='PROGRESS_RA', max_length=24)),
                ('ra_number', models.CharField(max_length=48)),
                ('ra_sequence_no', models.PositiveIntegerField(default=1)),
                ('bill_date', models.DateField(db_index=True)),
                ('bs_date', models.CharField(blank=True, max_length=16)),
                ('period_from', models.DateField(blank=True, null=True)),
                ('period_to', models.DateField(blank=True, null=True)),
                ('due_date', models.DateField(db_index=True)),
                ('currency_code', models.CharField(default='NPR', max_length=8)),
                ('exchange_rate', models.DecimalField(decimal_places=6, default=Decimal('1.000000'), max_digits=14)),
                ('previous_cumulative_gross', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('current_gross_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('cumulative_gross_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('vat_rate_pct', models.DecimalField(decimal_places=2, default=Decimal('13.00'), max_digits=5)),
                ('vat_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('gross_with_vat', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('total_deductions', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('retention_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('advance_recovery_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('tds_deduction_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('other_deductions_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('net_receivable_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('certified_gross_amount', models.DecimalField(blank=True, decimal_places=2, max_digits=18, null=True)),
                ('certified_net_amount', models.DecimalField(blank=True, decimal_places=2, max_digits=18, null=True)),
                ('status', models.CharField(choices=[('DRAFT', 'Draft'), ('MEASUREMENT_VERIFIED', 'Measurement Verified'), ('ENGINEER_APPROVED', 'Site Engineer Approved'), ('PM_REVIEWED', 'Project Manager Reviewed'), ('ACCOUNTS_REVIEWED', 'Accounts Reviewed'), ('AUTHORIZED', 'Authorized for Submission'), ('SUBMITTED_TO_CLIENT', 'Submitted to Client (Pending Certification)'), ('PARTIALLY_CERTIFIED', 'Partially Certified by Client'), ('CLIENT_CERTIFIED', 'Client Certified (Ready to Post)'), ('POSTED', 'Posted to AR & GL'), ('PARTIALLY_RECEIVED', 'Partially Received'), ('SETTLED', 'Settled / Closed'), ('DISPUTED', 'Disputed'), ('RETURNED', 'Returned for Revision'), ('REJECTED', 'Rejected'), ('SUPERSEDED', 'Superseded'), ('CANCELLED', 'Cancelled')], db_index=True, default='DRAFT', max_length=24)),
                ('remarks', models.TextField(blank=True)),
                ('return_reason', models.CharField(blank=True, max_length=255)),
                ('version', models.PositiveIntegerField(default=1)),
                ('posted_at', models.DateTimeField(blank=True, null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('accounts_reviewed_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='accounts_ra_bills', to=settings.AUTH_USER_MODEL)),
                ('authorized_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='authorized_ra_bills', to=settings.AUTH_USER_MODEL)),
                ('boq_header', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='ra_bills', to='projects.projectboqheader')),
                ('branch', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='tenancy.branch')),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ra_bills', to='tenancy.company')),
                ('contract', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='ra_bills', to='projects.projectcontract')),
                ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='created_ra_bills', to=settings.AUTH_USER_MODEL)),
                ('engineer_approved_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='engineer_ra_bills', to=settings.AUTH_USER_MODEL)),
                ('fiscal_year', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='tenancy.fiscalyear')),
                ('measurement_book', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='linked_ra_bills', to='construction_ops.operationaldocument')),
                ('open_item', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='ra_bill', to='party_hub.openitem')),
                ('party', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='ra_bills', to='accounting.party')),
                ('pm_reviewed_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='pm_ra_bills', to=settings.AUTH_USER_MODEL)),
                ('posted_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='posted_ra_bills', to=settings.AUTH_USER_MODEL)),
                ('previous_ra_bill', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='next_ra_bills', to='party_hub.rabill')),
                ('project', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='ra_bills', to='projects.project')),
                ('verified_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='verified_ra_bills', to=settings.AUTH_USER_MODEL)),
                ('voucher', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='ra_bill', to='accounting.voucher')),
            ],
            options={
                'db_table': 'ar_ra_bills',
                'ordering': ['-bill_date', '-ra_sequence_no'],
                'unique_together': {('company', 'ra_number')},
            },
        ),
        migrations.CreateModel(
            name='RABillCertification',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('certificate_no', models.CharField(max_length=64)),
                ('certified_date', models.DateField()),
                ('bs_certified_date', models.CharField(blank=True, max_length=16)),
                ('certified_gross_amount', models.DecimalField(decimal_places=2, max_digits=18)),
                ('certified_deductions_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('certified_net_amount', models.DecimalField(decimal_places=2, max_digits=18)),
                ('certified_by_client_officer', models.CharField(blank=True, max_length=120)),
                ('remarks', models.TextField(blank=True)),
                ('supporting_doc_ref', models.CharField(blank=True, max_length=255)),
                ('recorded_at', models.DateTimeField(auto_now_add=True)),
                ('ra_bill', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='certifications', to='party_hub.rabill')),
                ('recorded_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'ar_ra_bill_certifications',
                'ordering': ['-certified_date', '-recorded_at'],
            },
        ),
        migrations.CreateModel(
            name='RABillDeduction',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('deduction_type', models.CharField(choices=[('ADVANCE_RECOVERY', 'Mobilization Advance Recovery'), ('RETENTION', 'Contract Retention Deduction'), ('CLIENT_TDS', 'Client Withholding Tax (TDS)'), ('MATERIAL_RECOVERY', 'Client-Supplied Material Recovery'), ('PENALTY', 'Delay Penalty'), ('LIQUIDATED_DAMAGES', 'Liquidated Damages (LD)'), ('PERFORMANCE_DEDUCTION', 'Performance / Quality Deduction'), ('OTHER', 'Other Contractual Deduction')], max_length=28)),
                ('description', models.CharField(blank=True, max_length=180)),
                ('base_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('rate_pct', models.DecimalField(decimal_places=3, default=Decimal('0.000'), max_digits=6)),
                ('amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('advance_record', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='accounting.advancerecord')),
                ('gl_account', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='accounting.account')),
                ('ra_bill', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='deductions', to='party_hub.rabill')),
            ],
            options={
                'db_table': 'ar_ra_bill_deductions',
            },
        ),
        migrations.CreateModel(
            name='RABillLine',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('line_no', models.PositiveSmallIntegerField(default=1)),
                ('item_code', models.CharField(max_length=40)),
                ('description', models.CharField(max_length=255)),
                ('unit', models.CharField(default='Cum', max_length=24)),
                ('contract_qty', models.DecimalField(decimal_places=3, default=Decimal('0.000'), max_digits=18)),
                ('contract_rate', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('previous_cumulative_qty', models.DecimalField(decimal_places=3, default=Decimal('0.000'), max_digits=18)),
                ('current_qty', models.DecimalField(decimal_places=3, default=Decimal('0.000'), max_digits=18)),
                ('cumulative_qty', models.DecimalField(decimal_places=3, default=Decimal('0.000'), max_digits=18)),
                ('remaining_qty', models.DecimalField(decimal_places=3, default=Decimal('0.000'), max_digits=18)),
                ('previous_value', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('current_value', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('cumulative_value', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('balance_value', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('is_variation_excess', models.BooleanField(default=False)),
                ('measurement_ref', models.CharField(blank=True, max_length=120)),
                ('boq_line', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='ra_bill_lines', to='projects.projectboqline')),
                ('ra_bill', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='lines', to='party_hub.rabill')),
                ('variation_order', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='ra_bill_lines', to='projects.projectvariationorder')),
            ],
            options={
                'db_table': 'ar_ra_bill_lines',
                'ordering': ['line_no', 'item_code'],
            },
        ),
        migrations.CreateModel(
            name='ReceiptAllocationReversal',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('allocation_id_ref', models.UUIDField(db_index=True)),
                ('reversed_amount', models.DecimalField(decimal_places=2, max_digits=18)),
                ('reason', models.CharField(max_length=255)),
                ('reversed_at', models.DateTimeField(auto_now_add=True)),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ar_allocation_reversals', to='tenancy.company')),
                ('invoice_voucher', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='reversed_ar_invoice_allocations', to='accounting.voucher')),
                ('party', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='ar_allocation_reversals', to='accounting.party')),
                ('receipt_voucher', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='reversed_ar_receipt_allocations', to='accounting.voucher')),
                ('reversed_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'ar_receipt_allocation_reversals',
                'ordering': ['-reversed_at'],
            },
        ),
        migrations.CreateModel(
            name='SalesInvoice',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('doc_type', models.CharField(choices=[('TAX_INVOICE', 'Tax Invoice (VAT)'), ('COMMERCIAL_INVOICE', 'Commercial / General Sales Invoice'), ('SERVICE_INVOICE', 'Service / Consultancy Invoice'), ('MILESTONE_INVOICE', 'Contract Milestone Invoice'), ('DEBIT_NOTE', 'Customer Debit Note'), ('CREDIT_NOTE', 'Customer Credit Note')], db_index=True, default='TAX_INVOICE', max_length=24)),
                ('invoice_no', models.CharField(max_length=48)),
                ('invoice_date', models.DateField(db_index=True)),
                ('bs_date', models.CharField(blank=True, max_length=16)),
                ('due_date', models.DateField(db_index=True)),
                ('currency_code', models.CharField(default='NPR', max_length=8)),
                ('exchange_rate', models.DecimalField(decimal_places=6, default=Decimal('1.000000'), max_digits=14)),
                ('subtotal_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('discount_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('taxable_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('vat_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('tds_withholding_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('total_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('status', models.CharField(choices=[('DRAFT', 'Draft'), ('SUBMITTED', 'Submitted for Approval'), ('APPROVED', 'Approved (Ready to Post)'), ('POSTED', 'Posted to GL & AR'), ('PARTIAL', 'Partially Paid'), ('SETTLED', 'Settled / Closed'), ('DISPUTED', 'Disputed'), ('CANCELLED', 'Cancelled'), ('REVERSED', 'Reversed')], db_index=True, default='DRAFT', max_length=16)),
                ('narration', models.TextField(blank=True)),
                ('version', models.PositiveIntegerField(default=1)),
                ('posted_at', models.DateTimeField(blank=True, null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('approved_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='approved_ar_invoices', to=settings.AUTH_USER_MODEL)),
                ('branch', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='tenancy.branch')),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ar_sales_invoices', to='tenancy.company')),
                ('contract', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='ar_sales_invoices', to='projects.projectcontract')),
                ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='created_ar_invoices', to=settings.AUTH_USER_MODEL)),
                ('fiscal_year', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='tenancy.fiscalyear')),
                ('open_item', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='ar_sales_invoice', to='party_hub.openitem')),
                ('party', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='ar_sales_invoices', to='accounting.party')),
                ('posted_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='posted_ar_invoices', to=settings.AUTH_USER_MODEL)),
                ('project', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='ar_sales_invoices', to='projects.project')),
                ('reference_invoice', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='adjustment_notes', to='party_hub.salesinvoice')),
                ('revenue_account', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='ar_invoice_revenues', to='accounting.account')),
                ('voucher', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='ar_sales_invoice', to='accounting.voucher')),
            ],
            options={
                'db_table': 'ar_sales_invoices',
                'ordering': ['-invoice_date', '-created_at'],
                'unique_together': {('company', 'invoice_no')},
            },
        ),
        migrations.CreateModel(
            name='SalesInvoiceLine',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('line_no', models.PositiveSmallIntegerField(default=1)),
                ('item_code', models.CharField(blank=True, max_length=40)),
                ('description', models.CharField(max_length=255)),
                ('unit', models.CharField(default='Unit', max_length=24)),
                ('quantity', models.DecimalField(decimal_places=3, default=Decimal('1.000'), max_digits=16)),
                ('unit_rate', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('discount_pct', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=5)),
                ('tax_rate_pct', models.DecimalField(decimal_places=2, default=Decimal('13.00'), max_digits=5)),
                ('line_subtotal', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('vat_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('line_total', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('cost_center', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='accounting.costcenter')),
                ('invoice', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='lines', to='party_hub.salesinvoice')),
                ('revenue_account', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='accounting.account')),
            ],
            options={
                'db_table': 'ar_sales_invoice_lines',
                'ordering': ['line_no'],
            },
        ),
        migrations.CreateModel(
            name='StatementReconciliation',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('recon_no', models.CharField(max_length=48)),
                ('statement_from', models.DateField(blank=True, null=True)),
                ('statement_to', models.DateField()),
                ('source_filename', models.CharField(blank=True, max_length=255)),
                ('company_closing_balance', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('customer_closing_balance', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('variance_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('status', models.CharField(choices=[('IMPORTED', 'Imported'), ('MATCHED', 'Matching Completed'), ('EXCEPTIONS_PENDING', 'Exceptions Require Action'), ('SUBMITTED', 'Submitted for Approval'), ('CONFIRMED', 'Reconciled & Confirmed')], db_index=True, default='IMPORTED', max_length=24)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('approved_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='approved_ar_recons', to=settings.AUTH_USER_MODEL)),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ar_statement_recons', to='tenancy.company')),
                ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='created_ar_recons', to=settings.AUTH_USER_MODEL)),
                ('party', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='ar_statement_recons', to='accounting.party')),
            ],
            options={
                'db_table': 'ar_statement_reconciliations',
                'ordering': ['-created_at'],
                'unique_together': {('company', 'recon_no')},
            },
        ),
        migrations.CreateModel(
            name='StatementReconciliationLine',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('line_no', models.PositiveIntegerField(default=1)),
                ('ext_date', models.DateField(blank=True, null=True)),
                ('ext_reference', models.CharField(blank=True, max_length=80)),
                ('ext_description', models.CharField(blank=True, max_length=255)),
                ('ext_debit', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('ext_credit', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('ext_net_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('match_state', models.CharField(choices=[('MATCHED', 'Exact Match'), ('MISSING_IN_COMPANY', 'Missing in Company Books'), ('MISSING_IN_CUSTOMER', 'Missing in Customer Statement'), ('AMOUNT_MISMATCH', 'Amount Mismatch'), ('DATE_MISMATCH', 'Date Mismatch'), ('UNRECOGNIZED_DEDUCTION', 'Unrecognized Client Deduction'), ('DISPUTED', 'Flagged as Disputed')], db_index=True, default='MISSING_IN_COMPANY', max_length=28)),
                ('variance_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('resolution_action', models.CharField(blank=True, max_length=64)),
                ('resolution_notes', models.CharField(blank=True, max_length=255)),
                ('matched_open_item', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='party_hub.openitem')),
                ('matched_voucher', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='accounting.voucher')),
                ('reconciliation', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='lines', to='party_hub.statementreconciliation')),
            ],
            options={
                'db_table': 'ar_statement_reconciliation_lines',
                'ordering': ['line_no'],
            },
        ),
        migrations.CreateModel(
            name='CustomerReceipt',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('receipt_no', models.CharField(max_length=48)),
                ('receipt_date', models.DateField(db_index=True)),
                ('bs_date', models.CharField(blank=True, max_length=16)),
                ('receipt_mode', models.CharField(choices=[('CASH', 'Cash Receipt'), ('BANK_TRANSFER', 'Bank Transfer / IPS / ConnectIPS'), ('CHEQUE', 'Customer Cheque (Two-Step Clearing)'), ('ONLINE', 'Electronic / Gateway Receipt'), ('TDS_CERTIFICATE', 'Withholding Tax (TDS) Certificate'), ('ADVANCE', 'Customer Mobilization / Advance Receipt'), ('RETENTION_RELEASE', 'Retention Release Receipt')], db_index=True, default='BANK_TRANSFER', max_length=24)),
                ('cheque_number', models.CharField(blank=True, max_length=40)),
                ('cheque_date', models.DateField(blank=True, null=True)),
                ('drawee_bank', models.CharField(blank=True, max_length=120)),
                ('tds_certificate_no', models.CharField(blank=True, max_length=64)),
                ('tds_tax_period', models.CharField(blank=True, max_length=32)),
                ('currency_code', models.CharField(default='NPR', max_length=8)),
                ('exchange_rate', models.DecimalField(decimal_places=6, default=Decimal('1.000000'), max_digits=14)),
                ('amount', models.DecimalField(decimal_places=2, max_digits=18)),
                ('bank_charges_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('reference_no', models.CharField(blank=True, max_length=80)),
                ('narration', models.CharField(blank=True, max_length=255)),
                ('status', models.CharField(choices=[('DRAFT', 'Draft'), ('POSTED', 'Posted (Open / Unallocated)'), ('PARTIALLY_ALLOCATED', 'Partially Allocated'), ('FULLY_ALLOCATED', 'Fully Allocated'), ('BOUNCED', 'Cheque Bounced / Returned'), ('REVERSED', 'Reversed')], db_index=True, default='POSTED', max_length=24)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('advance_record', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='ar_receipts', to='accounting.advancerecord')),
                ('bank_account', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='banking.bankaccount')),
                ('branch', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='tenancy.branch')),
                ('cheque_leaf', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='ar_receipts', to='banking.chequeleaf')),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ar_customer_receipts', to='tenancy.company')),
                ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='created_ar_receipts', to=settings.AUTH_USER_MODEL)),
                ('deposit_account', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='ar_receipt_deposits', to='accounting.account')),
                ('open_item', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='ar_customer_receipt', to='party_hub.openitem')),
                ('party', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='ar_receipts', to='accounting.party')),
                ('project', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='ar_receipts', to='projects.project')),
                ('voucher', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='ar_customer_receipt', to='accounting.voucher')),
            ],
            options={
                'db_table': 'ar_customer_receipts',
                'ordering': ['-receipt_date', '-created_at'],
                'unique_together': {('company', 'receipt_no')},
            },
        ),
        migrations.CreateModel(
            name='MeasurementReference',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('mb_page_no', models.CharField(blank=True, max_length=40)),
                ('verified_qty', models.DecimalField(decimal_places=3, default=Decimal('0.000'), max_digits=18)),
                ('notes', models.CharField(blank=True, max_length=255)),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ar_measurement_refs', to='tenancy.company')),
                ('measurement_doc', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='ra_bill_references', to='construction_ops.operationaldocument')),
                ('ra_bill', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='measurement_references', to='party_hub.rabill')),
            ],
            options={
                'db_table': 'ar_measurement_references',
                'unique_together': {('ra_bill', 'measurement_doc')},
            },
        ),
    ]
