# Generated by Django 5.2.17 on 2026-09-02 14:58

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


class Migration(migrations.Migration):

    dependencies = [
        ('billing', '0001_initial'),
        ('customers', '0002_internetpackage_description_country_city_area_and_more'),
        ('tenancy', '0003_organization_address_organization_email_and_more'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='PromiseToPay',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('promise_number', models.CharField(max_length=50)),
                ('outstanding_amount', models.DecimalField(decimal_places=2, max_digits=12)),
                ('promised_amount', models.DecimalField(decimal_places=2, max_digits=12)),
                ('promise_date', models.DateField()),
                ('deadline', models.DateField()),
                ('status', models.CharField(choices=[('PENDING', 'Pending Approval'), ('ACTIVE', 'Active Promise'), ('FULFILLED', 'Fulfilled'), ('BROKEN', 'Broken'), ('EXPIRED', 'Expired'), ('CANCELLED', 'Cancelled')], default='PENDING', max_length=30)),
                ('notes', models.TextField(blank=True)),
                ('failure_reason', models.TextField(blank=True)),
                ('completed_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_promises', to=settings.AUTH_USER_MODEL)),
                ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_promises', to=settings.AUTH_USER_MODEL)),
                ('customer', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='promises_to_pay', to='customers.customer')),
                ('invoice', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='promises_to_pay', to='billing.invoice')),
                ('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='promises_to_pay', to='customers.serviceaccount')),
            ],
            options={
                'db_table': 'billing_promise_to_pay',
                'ordering': ['-created_at'],
                'indexes': [models.Index(fields=['organization', 'status'], name='ptp_org_status_idx'), models.Index(fields=['organization', 'service_account'], name='ptp_org_service_idx'), models.Index(fields=['organization', 'deadline'], name='ptp_org_deadline_idx')],
                'constraints': [models.UniqueConstraint(fields=('organization', 'promise_number'), name='unique_promise_number_per_org'), models.CheckConstraint(condition=models.Q(('promised_amount__gt', 0)), name='ptp_promised_amount_gt_zero'), models.CheckConstraint(condition=models.Q(('deadline__gte', models.F('promise_date'))), name='ptp_deadline_gte_promise_date')],
            },
        ),
    ]
