# Generated by Django 5.2.17 on 2026-09-28 08:22

import django.db.models.deletion
import uuid
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('commercial', '0004_invoice_history_integrity'),
        ('organization', '0002_userorganizationassignment'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='ReceiptSequence',
            fields=[
                ('service_center', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, primary_key=True, serialize=False, to='organization.servicecenter')),
                ('next_value', models.PositiveBigIntegerField(default=1)),
            ],
            options={
                'constraints': [models.CheckConstraint(condition=models.Q(('next_value__gte', 1)), name='payment_sequence_positive')],
            },
        ),
        migrations.CreateModel(
            name='ServicePayment',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('amount', models.DecimalField(decimal_places=2, default=0, max_digits=14)),
                ('currency', models.CharField(max_length=3)),
                ('method', models.CharField(choices=[('CASH', 'Cash'), ('CARD', 'Card'), ('BANK_TRANSFER', 'Bank Transfer'), ('MOBILE_FINANCIAL_SERVICE', 'Mobile Financial Service'), ('OTHER', 'Other')], max_length=24)),
                ('reference', models.CharField(blank=True, help_text='References only. Never record card numbers, CVV, banking credentials, wallet secrets or OTPs.', max_length=200)),
                ('status', models.CharField(choices=[('POSTED', 'Posted'), ('VOIDED', 'Voided')], default='POSTED', max_length=6)),
                ('received_at', models.DateTimeField()),
                ('note', models.TextField(blank=True, help_text='References only. Never record card numbers, CVV, banking credentials, wallet secrets or OTPs.', max_length=2000)),
                ('invoice', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='payments', to='commercial.serviceinvoice')),
                ('received_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
                ('service_center', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='organization.servicecenter')),
            ],
            options={
                'ordering': ('created_at', 'pk'),
                'permissions': [('receive_servicepayment', 'Receive and allocate service payment'), ('reverse_servicepayment', 'Reverse service payment')],
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='PaymentReversal',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('amount', models.DecimalField(decimal_places=2, default=0, max_digits=14)),
                ('reversed_at', models.DateTimeField()),
                ('reason', models.TextField(help_text='References only. Never record card numbers, CVV, banking credentials, wallet secrets or OTPs.', max_length=2000)),
                ('reversed_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
                ('payment', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='reversal', to='commercial.servicepayment')),
            ],
            options={
                'ordering': ('created_at', 'pk'),
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='PaymentAllocation',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('amount', models.DecimalField(decimal_places=2, default=0, max_digits=14)),
                ('allocated_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
                ('invoice', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='payment_allocations', to='commercial.serviceinvoice')),
                ('payment', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='allocation', to='commercial.servicepayment')),
            ],
            options={
                'ordering': ('created_at', 'pk'),
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='ServicePaymentReceipt',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('number', models.CharField(max_length=22)),
                ('amount', models.DecimalField(decimal_places=2, default=0, max_digits=14)),
                ('currency', models.CharField(max_length=3)),
                ('method', models.CharField(choices=[('CASH', 'Cash'), ('CARD', 'Card'), ('BANK_TRANSFER', 'Bank Transfer'), ('MOBILE_FINANCIAL_SERVICE', 'Mobile Financial Service'), ('OTHER', 'Other')], max_length=24)),
                ('reference', models.CharField(blank=True, max_length=200)),
                ('customer_snapshot', models.JSONField()),
                ('center_snapshot', models.JSONField()),
                ('invoice_number', models.CharField(max_length=22)),
                ('issued_at', models.DateTimeField()),
                ('issued_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
                ('payment', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='receipt', to='commercial.servicepayment')),
                ('service_center', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='organization.servicecenter')),
            ],
            options={
                'ordering': ('created_at', 'pk'),
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='ServiceFinancialRelease',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('outstanding_amount', models.DecimalField(decimal_places=2, default=0, max_digits=14)),
                ('authorized_at', models.DateTimeField()),
                ('reason', models.TextField(help_text='References only. Never record card numbers, CVV, banking credentials, wallet secrets or OTPs.', max_length=2000)),
                ('reference', models.CharField(blank=True, help_text='References only. Never record card numbers, CVV, banking credentials, wallet secrets or OTPs.', max_length=200)),
                ('authorized_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
                ('invoice', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='financial_releases', to='commercial.serviceinvoice')),
            ],
            options={
                'ordering': ('created_at', 'pk'),
                'permissions': [('authorize_due_release', 'Authorize delivery with outstanding customer balance')],
                'abstract': False,
                'constraints': [models.CheckConstraint(condition=models.Q(('outstanding_amount__gt', 0), models.Q(('reason', ''), _negated=True)), name='payment_release_valid')],
            },
        ),
        migrations.AddConstraint(
            model_name='servicepayment',
            constraint=models.CheckConstraint(condition=models.Q(('amount__gt', 0), ('currency__regex', '^[A-Z]{3}$'), ('method__in', ['CASH', 'CARD', 'BANK_TRANSFER', 'MOBILE_FINANCIAL_SERVICE', 'OTHER']), ('status__in', ['POSTED', 'VOIDED'])), name='payment_values_valid'),
        ),
        migrations.AddConstraint(
            model_name='servicepayment',
            constraint=models.CheckConstraint(condition=models.Q(('method', 'CASH'), models.Q(('reference', ''), _negated=True), _connector='OR'), name='payment_external_reference'),
        ),
        migrations.AddConstraint(
            model_name='servicepayment',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('method', 'OTHER'), _negated=True), models.Q(('note', ''), _negated=True), _connector='OR'), name='payment_other_explanation'),
        ),
        migrations.AddConstraint(
            model_name='servicepayment',
            constraint=models.UniqueConstraint(condition=models.Q(('reference', ''), _negated=True), fields=('service_center', 'method', 'reference'), name='payment_reference_unique'),
        ),
        migrations.AddConstraint(
            model_name='paymentreversal',
            constraint=models.CheckConstraint(condition=models.Q(('amount__gt', 0), models.Q(('reason', ''), _negated=True)), name='payment_reversal_valid'),
        ),
        migrations.AddConstraint(
            model_name='paymentallocation',
            constraint=models.CheckConstraint(condition=models.Q(('amount__gt', 0)), name='payment_allocation_positive'),
        ),
        migrations.AddConstraint(
            model_name='servicepaymentreceipt',
            constraint=models.UniqueConstraint(fields=('service_center', 'number'), name='payment_receipt_number_unique'),
        ),
        migrations.AddConstraint(
            model_name='servicepaymentreceipt',
            constraint=models.CheckConstraint(condition=models.Q(('amount__gt', 0), ('currency__regex', '^[A-Z]{3}$'), ('method__in', ['CASH', 'CARD', 'BANK_TRANSFER', 'MOBILE_FINANCIAL_SERVICE', 'OTHER']), ('number__regex', '^RCT-[0-9]{8,18}$')), name='payment_receipt_valid'),
        ),
    ]
