# Generated by Django 5.2.19 on 2026-11-29 04:37

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', '0004_account_account_class_account_allow_direct_posting_and_more'),
        ('projects', '0001_initial'),
        ('tenancy', '0002_company_multi_currency_enabled'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.AddField(
            model_name='voucherline',
            name='against_voucher',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='referenced_by_lines', to='accounting.voucher'),
        ),
        migrations.AddField(
            model_name='voucherline',
            name='expense_scope',
            field=models.CharField(blank=True, default='', max_length=16),
        ),
        migrations.AlterField(
            model_name='account',
            name='control_role',
            field=models.CharField(choices=[('NONE', 'Regular Ledger Account'), ('SUNDRY_DEBTORS', 'Sundry Debtors (Customer Control)'), ('SUNDRY_CREDITORS', 'Sundry Creditors (Supplier Control)'), ('SUBCONTRACTOR_PAYABLE', 'Subcontractor Payable Control'), ('RETENTION_RECEIVABLE', 'Retention Receivable'), ('RETENTION_PAYABLE', 'Retention Payable'), ('INVENTORY', 'Inventory / Stock Control'), ('PROJECT_WIP', 'Project Work-in-Progress (WIP)'), ('CASH', 'Cash in Hand'), ('BANK', 'Bank Account'), ('INPUT_VAT', 'Input VAT Recoverable'), ('OUTPUT_VAT', 'Output VAT Payable'), ('TDS_PAYABLE', 'TDS Withholding Payable'), ('TDS_RECEIVABLE', 'TDS Advance Receivable'), ('CUSTOMER_ADVANCE', 'Customer / Mobilization Advance Liability'), ('SUPPLIER_ADVANCE', 'Supplier Advance Asset'), ('EMPLOYEE_ADVANCE', 'Employee / Staff Advance'), ('EMPLOYEE_PAYABLE', 'Employee Payables'), ('WAGES_PAYABLE', 'Wages Payable'), ('EMPLOYEE_REIMBURSEMENT', 'Employee Reimbursement Payable'), ('STAFF_LOAN_RECEIVABLE', 'Staff Loan Receivable'), ('OFFICE_EXPENSES', 'Office & Administrative Expenses'), ('DIRECT_EXPENSES', 'Direct Construction Expenses'), ('OUTSTANDING_EXPENSES', 'Outstanding / Accrued Expenses'), ('OTHER_INCOME', 'Other Income & Gains'), ('LOSS_WRITE_OFF', 'Losses & Write-offs'), ('FX_GAIN', 'Foreign Exchange Gain'), ('FX_LOSS', 'Foreign Exchange Loss'), ('DEPRECIATION_EXP', 'Depreciation Expense'), ('ACCUM_DEPRECIATION', 'Accumulated Depreciation (Contra Asset)')], db_index=True, default='NONE', max_length=28),
        ),
        migrations.CreateModel(
            name='AdvanceRecord',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('advance_no', models.CharField(max_length=40)),
                ('advance_type', models.CharField(choices=[('EMPLOYEE_ADVANCE', 'Employee Advance'), ('SUPPLIER_ADVANCE', 'Supplier Advance'), ('CUSTOMER_ADVANCE', 'Customer Advance'), ('MOBILIZATION_ADVANCE', 'Contractor Mobilization Advance'), ('SITE_ADVANCE', 'Site Operational Advance'), ('PROJECT_ADVANCE', 'Project-Specific Advance'), ('STAFF_LOAN', 'Staff Loan'), ('TRAVEL_ADVANCE', 'Travel Advance'), ('PETTY_CASH_ADVANCE', 'Petty Cash Advance')], db_index=True, max_length=28)),
                ('issue_date', models.DateField()),
                ('bs_date', models.CharField(blank=True, max_length=16)),
                ('original_amount', models.DecimalField(decimal_places=2, max_digits=18)),
                ('adjusted_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('status', models.CharField(choices=[('OPEN', 'Open'), ('PARTIALLY_ADJUSTED', 'Partially Adjusted'), ('SETTLED', 'Settled'), ('CANCELLED', 'Cancelled')], db_index=True, default='OPEN', max_length=24)),
                ('remarks', models.CharField(blank=True, max_length=255)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='advance_records', to='tenancy.company')),
                ('control_account', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='advance_records', to='accounting.account')),
                ('party', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='advance_records', to='accounting.party')),
                ('project', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='advance_records', to='projects.project')),
                ('source_voucher', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='issued_advances', to='accounting.voucher')),
            ],
            options={
                'db_table': 'employee_advances',
                'ordering': ['issue_date', 'advance_no'],
            },
        ),
        migrations.AddField(
            model_name='voucherline',
            name='advance_record',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='voucher_lines', to='accounting.advancerecord'),
        ),
        migrations.CreateModel(
            name='EmployeeProfile',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('employee_code', models.CharField(max_length=32)),
                ('full_name', models.CharField(max_length=180)),
                ('designation', models.CharField(blank=True, max_length=120)),
                ('basic_salary', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=14)),
                ('phone', models.CharField(blank=True, max_length=40)),
                ('email', models.EmailField(blank=True, max_length=254)),
                ('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='employees', to='tenancy.company')),
                ('department', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='employees', to='accounting.department')),
                ('party', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='employee_profile', to='accounting.party')),
                ('project', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='employees', to='projects.project')),
            ],
            options={
                'db_table': 'employee_ledgers',
                'ordering': ['employee_code'],
                'unique_together': {('company', 'employee_code')},
            },
        ),
        migrations.AddField(
            model_name='advancerecord',
            name='employee',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='advance_records', to='accounting.employeeprofile'),
        ),
        migrations.CreateModel(
            name='PayrollLiability',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('payroll_ref', models.CharField(max_length=48)),
                ('period_label', models.CharField(max_length=40)),
                ('gross_wages', models.DecimalField(decimal_places=2, max_digits=18)),
                ('settled_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('status', models.CharField(choices=[('UNPAID', 'Unpaid'), ('PARTIALLY_PAID', 'Partially Paid'), ('SETTLED', 'Settled')], default='UNPAID', max_length=20)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='payroll_liabilities', to='tenancy.company')),
                ('control_account', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='payroll_liabilities', to='accounting.account')),
                ('employee', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='payroll_liabilities', to='accounting.employeeprofile')),
                ('project', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='projects.project')),
                ('source_voucher', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='payroll_liabilities', to='accounting.voucher')),
            ],
            options={
                'db_table': 'payroll_liabilities',
                'ordering': ['-created_at'],
                'unique_together': {('company', 'payroll_ref', 'employee')},
            },
        ),
        migrations.AddField(
            model_name='voucherline',
            name='payroll_liability',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='voucher_lines', to='accounting.payrollliability'),
        ),
        migrations.CreateModel(
            name='StaffLoan',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('loan_no', models.CharField(max_length=40)),
                ('disbursement_date', models.DateField()),
                ('principal_amount', models.DecimalField(decimal_places=2, max_digits=18)),
                ('repaid_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=18)),
                ('interest_rate_pct', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=5)),
                ('monthly_installment', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=14)),
                ('remaining_installments', models.PositiveSmallIntegerField(default=12)),
                ('status', models.CharField(choices=[('ACTIVE', 'Active'), ('SETTLED', 'Settled')], default='ACTIVE', max_length=16)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='staff_loans', to='tenancy.company')),
                ('control_account', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='staff_loans', to='accounting.account')),
                ('disbursement_voucher', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='staff_loans', to='accounting.voucher')),
                ('employee', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='staff_loans', to='accounting.employeeprofile')),
            ],
            options={
                'db_table': 'staff_loans',
                'ordering': ['disbursement_date'],
                'unique_together': {('company', 'loan_no')},
            },
        ),
        migrations.AddField(
            model_name='voucherline',
            name='staff_loan',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='voucher_lines', to='accounting.staffloan'),
        ),
        migrations.CreateModel(
            name='VoucherLogicSnapshot',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('stage', models.CharField(default='POSTED', max_length=24)),
                ('is_balanced', models.BooleanField(default=False)),
                ('is_valid', models.BooleanField(default=False)),
                ('snapshot_payload', models.JSONField(default=dict, encoder=django.core.serializers.json.DjangoJSONEncoder)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='tenancy.company')),
                ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
                ('voucher', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='logic_snapshots', to='accounting.voucher')),
            ],
            options={
                'db_table': 'voucher_logic_snapshots',
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='VoucherReference',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('reference_kind', models.CharField(choices=[('SUPPLIER_INVOICE', 'Supplier Invoice'), ('CUSTOMER_INVOICE', 'Customer Invoice'), ('EMPLOYEE_ADVANCE', 'Employee Advance'), ('SUPPLIER_ADVANCE', 'Supplier Advance'), ('CUSTOMER_ADVANCE', 'Customer Advance'), ('MOBILIZATION_ADVANCE', 'Mobilization Advance'), ('PAYROLL_LIABILITY', 'Payroll / Wages Payable'), ('STAFF_LOAN', 'Staff Loan')], max_length=28)),
                ('allocated_amount', models.DecimalField(decimal_places=2, max_digits=18)),
                ('notes', models.CharField(blank=True, max_length=255)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('advance_record', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='voucher_references', to='accounting.advancerecord')),
                ('against_voucher', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='allocated_references', to='accounting.voucher')),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='voucher_references', to='tenancy.company')),
                ('party', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='accounting.party')),
                ('payroll_liability', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='voucher_references', to='accounting.payrollliability')),
                ('staff_loan', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='voucher_references', to='accounting.staffloan')),
                ('voucher', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='references', to='accounting.voucher')),
                ('voucher_line', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='references', to='accounting.voucherline')),
            ],
            options={
                'db_table': 'voucher_references',
                'ordering': ['created_at'],
            },
        ),
        migrations.CreateModel(
            name='AccountRelationship',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('target_kind', models.CharField(choices=[('SUPPLIER_CONTRACTOR', 'Suppliers & Contractors (Creditors)'), ('CUSTOMER_CLIENT', 'Customers & Clients (Debtors)'), ('CHILD_EXPENSE', 'Child Expense Ledgers'), ('DIRECT_EXPENSE', 'Direct Construction Expense Ledgers'), ('EMPLOYEE', 'Employees & Staff Ledgers'), ('EMPLOYEE_ADVANCE', 'Employees with Outstanding Advances'), ('SUPPLIER_ADVANCE', 'Suppliers with Outstanding Advances'), ('CUSTOMER_ADVANCE', 'Customers with Outstanding Advances'), ('STAFF_LOAN', 'Staff Loans Receivable'), ('PROJECT_WIP', 'Projects, Sites, BOQ & Cost Codes'), ('BANK_ACCOUNT', 'Registered Bank Accounts'), ('CASH_ACCOUNT', 'Cash & Petty Cash Accounts'), ('PURCHASE_SUB', 'Purchase Subaccounts'), ('SALES_SUB', 'Sales & Revenue Accounts'), ('INCOME_SUB', 'Other Income & Gains'), ('LOSS_SUB', 'Loss & Write-off Accounts')], max_length=28)),
                ('extra_selector', models.CharField(blank=True, default='', max_length=40)),
                ('require_project', models.BooleanField(default=False)),
                ('require_subledger', models.BooleanField(default=False)),
                ('require_reason_on_journal', models.BooleanField(default=False)),
                ('allow_direct_journal_adjustment', models.BooleanField(default=True)),
                ('notes', models.CharField(blank=True, max_length=255)),
                ('is_active', models.BooleanField(default=True)),
                ('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='relationships', to='accounting.account')),
                ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='account_relationships', to='tenancy.company')),
                ('suggested_counter_account', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='counter_for_relationships', to='accounting.account')),
            ],
            options={
                'db_table': 'account_relationships',
                'ordering': ['account__code'],
                'unique_together': {('company', 'account', 'target_kind')},
            },
        ),
        migrations.CreateModel(
            name='AdvanceAdjustment',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('adjusted_amount', models.DecimalField(decimal_places=2, max_digits=18)),
                ('supporting_bill_ref', models.CharField(blank=True, max_length=80)),
                ('notes', models.CharField(blank=True, max_length=255)),
                ('adjusted_at', models.DateTimeField(auto_now_add=True)),
                ('adjusted_by', 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='advance_adjustments', to='tenancy.company')),
                ('voucher', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='advance_adjustments', to='accounting.voucher')),
                ('advance', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='adjustments', to='accounting.advancerecord')),
            ],
            options={
                'db_table': 'advance_adjustments',
                'ordering': ['adjusted_at'],
                'unique_together': {('advance', 'voucher')},
            },
        ),
        migrations.AlterUniqueTogether(
            name='advancerecord',
            unique_together={('company', 'advance_no')},
        ),
    ]
