# Generated by Django 3.2.16 on 2023-01-21 16:57

import re

import django.core.validators
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ("NEMO_billing", "0002_version_1_2_0"),
        ("rates", "0003_version_1_17_0"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("NEMO", "0042_version_4_3_0"),
    ]

    operations = [
        migrations.CreateModel(
            name="CAPDiscountConfiguration",
            fields=[
                ("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                (
                    "amount",
                    models.DecimalField(
                        decimal_places=2,
                        help_text="Amount after which the discount will be applied",
                        max_digits=14,
                        validators=[django.core.validators.MinValueValidator(1)],
                    ),
                ),
                (
                    "discount",
                    models.DecimalField(
                        decimal_places=3,
                        help_text="Discount in percent. Ex 20.5%",
                        max_digits=6,
                        validators=[
                            django.core.validators.MinValueValidator(1),
                            django.core.validators.MaxValueValidator(100),
                        ],
                    ),
                ),
                (
                    "charge_types",
                    models.CharField(
                        help_text="List of charge type this CAP applies to",
                        max_length=100,
                        validators=[
                            django.core.validators.RegexValidator(
                                re.compile("^\\d+(?:,\\d+)*\\Z"),
                                code="invalid",
                                message="Enter only digits separated by commas.",
                            )
                        ],
                    ),
                ),
                (
                    "start_on_first_charge",
                    models.BooleanField(
                        default=True,
                        help_text="Automatically set CAP start date to the month of the first eligible charge",
                    ),
                ),
                (
                    "start_month",
                    models.PositiveIntegerField(
                        blank=True,
                        choices=[
                            (1, "JANUARY"),
                            (2, "FEBRUARY"),
                            (3, "MARCH"),
                            (4, "APRIL"),
                            (5, "MAY"),
                            (6, "JUNE"),
                            (7, "JULY"),
                            (8, "AUGUST"),
                            (9, "SEPTEMBER"),
                            (10, "OCTOBER"),
                            (11, "NOVEMBER"),
                            (12, "DECEMBER"),
                        ],
                        help_text="Start month of the CAP discount",
                        null=True,
                    ),
                ),
                (
                    "start_year",
                    models.PositiveIntegerField(
                        blank=True,
                        help_text="Start year of the CAP discount",
                        null=True,
                        validators=[
                            django.core.validators.MinValueValidator(1900),
                            django.core.validators.MaxValueValidator(9999),
                        ],
                    ),
                ),
                (
                    "reset_interval",
                    models.PositiveIntegerField(
                        default=1,
                        help_text="Recurring interval, i.e. every 5 days.",
                        validators=[django.core.validators.MinValueValidator(1)],
                    ),
                ),
                (
                    "reset_frequency",
                    models.PositiveIntegerField(
                        choices=[(5, "Month(s)"), (6, "Year(s)")], help_text="The CAP reset frequency."
                    ),
                ),
                (
                    "core_facility",
                    models.ForeignKey(
                        blank=True,
                        help_text="The core facility this CAP applies to",
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        to="NEMO_billing.corefacility",
                    ),
                ),
                (
                    "rate_category",
                    models.ForeignKey(
                        help_text="The rate category this CAP applies to",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="rates.ratecategory",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="CAPDiscount",
            fields=[
                ("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                (
                    "amount",
                    models.DecimalField(
                        decimal_places=2,
                        help_text="Amount after which the discount will be applied",
                        max_digits=14,
                        validators=[django.core.validators.MinValueValidator(1)],
                    ),
                ),
                (
                    "discount",
                    models.DecimalField(
                        decimal_places=3,
                        help_text="Discount in percent. Ex 20.5%",
                        max_digits=6,
                        validators=[
                            django.core.validators.MinValueValidator(1),
                            django.core.validators.MaxValueValidator(100),
                        ],
                    ),
                ),
                (
                    "charge_types",
                    models.CharField(
                        help_text="List of charge type this CAP applies to",
                        max_length=100,
                        validators=[
                            django.core.validators.RegexValidator(
                                re.compile("^\\d+(?:,\\d+)*\\Z"),
                                code="invalid",
                                message="Enter only digits separated by commas.",
                            )
                        ],
                    ),
                ),
                (
                    "start_month",
                    models.PositiveIntegerField(
                        blank=True,
                        choices=[
                            (1, "JANUARY"),
                            (2, "FEBRUARY"),
                            (3, "MARCH"),
                            (4, "APRIL"),
                            (5, "MAY"),
                            (6, "JUNE"),
                            (7, "JULY"),
                            (8, "AUGUST"),
                            (9, "SEPTEMBER"),
                            (10, "OCTOBER"),
                            (11, "NOVEMBER"),
                            (12, "DECEMBER"),
                        ],
                        help_text="Start month of the CAP discount",
                        null=True,
                    ),
                ),
                (
                    "start_year",
                    models.PositiveIntegerField(
                        blank=True,
                        help_text="Start year of the CAP discount",
                        null=True,
                        validators=[
                            django.core.validators.MinValueValidator(1900),
                            django.core.validators.MaxValueValidator(9999),
                        ],
                    ),
                ),
                (
                    "next_reset_override_month",
                    models.PositiveIntegerField(
                        blank=True,
                        choices=[
                            (1, "JANUARY"),
                            (2, "FEBRUARY"),
                            (3, "MARCH"),
                            (4, "APRIL"),
                            (5, "MAY"),
                            (6, "JUNE"),
                            (7, "JULY"),
                            (8, "AUGUST"),
                            (9, "SEPTEMBER"),
                            (10, "OCTOBER"),
                            (11, "NOVEMBER"),
                            (12, "DECEMBER"),
                        ],
                        help_text="Temporary one-time reset override month",
                        null=True,
                    ),
                ),
                (
                    "next_reset_override_year",
                    models.PositiveIntegerField(
                        blank=True,
                        help_text="Temporary one-time reset override year",
                        null=True,
                        validators=[
                            django.core.validators.MinValueValidator(1900),
                            django.core.validators.MaxValueValidator(9999),
                        ],
                    ),
                ),
                (
                    "reset_interval",
                    models.PositiveIntegerField(
                        default=1,
                        help_text="Recurring interval, i.e. every 5 days.",
                        validators=[django.core.validators.MinValueValidator(1)],
                    ),
                ),
                (
                    "reset_frequency",
                    models.PositiveIntegerField(
                        choices=[(5, "Month(s)"), (6, "Year(s)")], help_text="The CAP reset frequency."
                    ),
                ),
                (
                    "account",
                    models.ForeignKey(
                        help_text="The account this CAP applies to",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="NEMO.account",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        help_text="The user this CAP applies to",
                        on_delete=django.db.models.deletion.CASCADE,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "configuration",
                    models.ForeignKey(
                        help_text="The configuration for this CAP",
                        on_delete=django.db.models.deletion.CASCADE,
                        to="cap_discount.capdiscountconfiguration",
                    ),
                ),
            ],
            options={
                "unique_together": {("configuration", "user", "account")},
            },
        ),
        migrations.CreateModel(
            name="CAPDiscountAmount",
            fields=[
                ("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                (
                    "month",
                    models.PositiveIntegerField(
                        choices=[
                            (1, "JANUARY"),
                            (2, "FEBRUARY"),
                            (3, "MARCH"),
                            (4, "APRIL"),
                            (5, "MAY"),
                            (6, "JUNE"),
                            (7, "JULY"),
                            (8, "AUGUST"),
                            (9, "SEPTEMBER"),
                            (10, "OCTOBER"),
                            (11, "NOVEMBER"),
                            (12, "DECEMBER"),
                        ]
                    ),
                ),
                (
                    "year",
                    models.PositiveIntegerField(
                        validators=[
                            django.core.validators.MinValueValidator(1),
                            django.core.validators.MaxValueValidator(9999),
                        ]
                    ),
                ),
                ("start", models.DecimalField(decimal_places=2, max_digits=14)),
                ("end", models.DecimalField(decimal_places=2, max_digits=14)),
                (
                    "cap_discount",
                    models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="cap_discount.capdiscount"),
                ),
            ],
            options={
                "ordering": ["-year", "-month"],
                "unique_together": {("cap_discount", "month", "year")},
            },
        ),
        migrations.AddConstraint(
            model_name="capdiscountconfiguration",
            constraint=models.UniqueConstraint(
                fields=("rate_category", "core_facility"), name="capdiscountconfiguration_unique_rate_category_facility"
            ),
        ),
        migrations.AddConstraint(
            model_name="capdiscountconfiguration",
            constraint=models.UniqueConstraint(
                condition=models.Q(("core_facility", None)),
                fields=("rate_category",),
                name="capdiscountconfiguration_unique_rate_category_null_facility",
            ),
        ),
    ]
