# Generated by Django 5.2.15 on 2026-07-05 13:03

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('customers', '0001_initial'),
        ('tenancy', '0002_auditlog'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Invoice',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('invoice_number', models.CharField(max_length=100)),
                ('billing_period_start', models.DateField()),
                ('billing_period_end', models.DateField()),
                ('issue_date', models.DateField()),
                ('due_date', models.DateField()),
                ('status', models.CharField(choices=[('UNPAID', 'Unpaid'), ('PARTIALLY_PAID', 'Partially Paid'), ('PAID', 'Paid')], default='UNPAID', max_length=30)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('billing_profile', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='invoices', to='customers.billingprofile')),
                ('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_records', to='tenancy.organization')),
                ('service_account', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='invoices', to='customers.serviceaccount')),
            ],
            options={
                'db_table': 'billing_invoice',
                'ordering': ['-issue_date', '-created_at'],
            },
        ),
        migrations.CreateModel(
            name='InvoiceLine',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('description', models.CharField(max_length=255)),
                ('amount', models.DecimalField(decimal_places=2, max_digits=12)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('invoice', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='lines', to='billing.invoice')),
                ('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_records', to='tenancy.organization')),
            ],
            options={
                'db_table': 'billing_invoice_line',
                'ordering': ['created_at'],
            },
        ),
        migrations.CreateModel(
            name='Payment',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('payment_number', models.CharField(max_length=100)),
                ('amount', models.DecimalField(decimal_places=2, max_digits=12)),
                ('payment_method', models.CharField(choices=[('CASH', 'Cash'), ('BANK_TRANSFER', 'Bank Transfer'), ('CARD', 'Card'), ('MOBILE_WALLET', 'Mobile Wallet'), ('OTHER', 'Other')], max_length=30)),
                ('reference', models.CharField(blank=True, max_length=150)),
                ('notes', models.TextField(blank=True)),
                ('paid_at', models.DateTimeField()),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_records', to='tenancy.organization')),
                ('received_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='billing_payments_received', to=settings.AUTH_USER_MODEL)),
                ('service_account', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='payments', to='customers.serviceaccount')),
            ],
            options={
                'db_table': 'billing_payment',
                'ordering': ['-paid_at', '-created_at'],
            },
        ),
        migrations.CreateModel(
            name='PaymentAllocation',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('amount', models.DecimalField(decimal_places=2, max_digits=12)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('invoice', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='allocations', to='billing.invoice')),
                ('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_records', to='tenancy.organization')),
                ('payment', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='allocations', to='billing.payment')),
            ],
            options={
                'db_table': 'billing_payment_allocation',
                'ordering': ['created_at'],
            },
        ),
        migrations.AddIndex(
            model_name='invoice',
            index=models.Index(fields=['organization', 'status'], name='invoice_org_status_idx'),
        ),
        migrations.AddIndex(
            model_name='invoice',
            index=models.Index(fields=['organization', 'service_account'], name='invoice_org_service_idx'),
        ),
        migrations.AddIndex(
            model_name='invoice',
            index=models.Index(fields=['organization', 'due_date'], name='invoice_org_due_idx'),
        ),
        migrations.AddConstraint(
            model_name='invoice',
            constraint=models.UniqueConstraint(fields=('organization', 'invoice_number'), name='unique_invoice_number_per_org'),
        ),
        migrations.AddConstraint(
            model_name='invoice',
            constraint=models.UniqueConstraint(fields=('organization', 'service_account', 'billing_period_start', 'billing_period_end'), name='unique_service_billing_period'),
        ),
        migrations.AddConstraint(
            model_name='invoice',
            constraint=models.CheckConstraint(condition=models.Q(('billing_period_end__gte', models.F('billing_period_start'))), name='invoice_period_end_gte_start'),
        ),
        migrations.AddConstraint(
            model_name='invoice',
            constraint=models.CheckConstraint(condition=models.Q(('due_date__gte', models.F('issue_date'))), name='invoice_due_date_gte_issue'),
        ),
        migrations.AddIndex(
            model_name='invoiceline',
            index=models.Index(fields=['organization', 'invoice'], name='invoice_line_org_invoice_idx'),
        ),
        migrations.AddConstraint(
            model_name='invoiceline',
            constraint=models.CheckConstraint(condition=models.Q(('amount__gt', 0)), name='invoice_line_amount_gt_zero'),
        ),
        migrations.AddIndex(
            model_name='payment',
            index=models.Index(fields=['organization', 'service_account'], name='payment_org_service_idx'),
        ),
        migrations.AddIndex(
            model_name='payment',
            index=models.Index(fields=['organization', 'paid_at'], name='payment_org_paid_idx'),
        ),
        migrations.AddConstraint(
            model_name='payment',
            constraint=models.UniqueConstraint(fields=('organization', 'payment_number'), name='unique_payment_number_per_org'),
        ),
        migrations.AddConstraint(
            model_name='payment',
            constraint=models.CheckConstraint(condition=models.Q(('amount__gt', 0)), name='payment_amount_gt_zero'),
        ),
        migrations.AddIndex(
            model_name='paymentallocation',
            index=models.Index(fields=['organization', 'payment'], name='allocation_org_payment_idx'),
        ),
        migrations.AddIndex(
            model_name='paymentallocation',
            index=models.Index(fields=['organization', 'invoice'], name='allocation_org_invoice_idx'),
        ),
        migrations.AddConstraint(
            model_name='paymentallocation',
            constraint=models.CheckConstraint(condition=models.Q(('amount__gt', 0)), name='payment_allocation_amount_gt_zero'),
        ),
    ]
