# Generated by Django 5.2.1 on 2026-10-10 06:33

import django.db.models.deletion
from decimal import Decimal
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("shop_management", "0107_sale_previous_balance_details"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="CustomerCreditApplication",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True, null=True)),
                ("updated_at", models.DateTimeField(auto_now=True, null=True)),
                ("source_amount", models.DecimalField(decimal_places=2, max_digits=18)),
                ("sale_amount", models.DecimalField(decimal_places=2, max_digits=18)),
                (
                    "exchange_rate",
                    models.DecimalField(
                        decimal_places=6, default=Decimal("1"), max_digits=20
                    ),
                ),
                (
                    "balance_payment",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="credit_applications",
                        to="shop_management.customerbalancepayment",
                    ),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="%(class)s_created",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "customer",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="credit_applications",
                        to="shop_management.customer",
                    ),
                ),
                (
                    "customer_payment",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="credit_applications",
                        to="shop_management.customerpayment",
                    ),
                ),
                (
                    "sale",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="credit_applications",
                        to="shop_management.sale",
                    ),
                ),
                (
                    "sale_currency",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="customer_credit_applications_to",
                        to="shop_management.currency",
                    ),
                ),
                (
                    "source_currency",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="customer_credit_applications_from",
                        to="shop_management.currency",
                    ),
                ),
            ],
            options={
                "ordering": ["created_at", "id"],
                "constraints": [
                    models.CheckConstraint(
                        check=models.Q(
                            models.Q(
                                ("balance_payment__isnull", True),
                                ("customer_payment__isnull", False),
                            ),
                            models.Q(
                                ("balance_payment__isnull", False),
                                ("customer_payment__isnull", True),
                            ),
                            _connector="OR",
                        ),
                        name="customer_credit_application_one_source",
                    ),
                    models.CheckConstraint(
                        check=models.Q(
                            ("exchange_rate__gt", 0),
                            ("sale_amount__gt", 0),
                            ("source_amount__gt", 0),
                        ),
                        name="customer_credit_application_positive_amounts",
                    ),
                ],
            },
        ),
    ]
