# Generated by Django 5.0.7 on 2026-09-01 10:03

import django.db.models.deletion
import django.utils.timezone
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("shop_management", "0081_product_master_and_prices"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="InventoryBatch",
            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)),
                ("batch_number", models.CharField(max_length=100)),
                ("manufacture_date", models.DateField(blank=True, null=True)),
                ("expiry_date", models.DateField(blank=True, db_index=True, null=True)),
                ("is_blocked", models.BooleanField(default=False)),
                ("notes", models.TextField(blank=True)),
                (
                    "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,
                    ),
                ),
                (
                    "product",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="batches",
                        to="shop_management.product",
                    ),
                ),
            ],
            options={
                "ordering": ["product_id", "expiry_date", "id"],
            },
        ),
        migrations.CreateModel(
            name="StockBalance",
            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)),
                (
                    "quantity",
                    models.DecimalField(decimal_places=4, default=0, max_digits=18),
                ),
                (
                    "average_cost",
                    models.DecimalField(decimal_places=6, default=0, max_digits=18),
                ),
                (
                    "batch",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="stock_balances",
                        to="shop_management.inventorybatch",
                    ),
                ),
                (
                    "cost_currency",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="stock_balances",
                        to="shop_management.currency",
                    ),
                ),
                (
                    "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,
                    ),
                ),
                (
                    "product",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="stock_balances",
                        to="shop_management.product",
                    ),
                ),
                (
                    "warehouse",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="stock_balances",
                        to="shop_management.warehouse",
                    ),
                ),
            ],
            options={
                "ordering": ["warehouse_id", "product_id", "batch__expiry_date", "id"],
            },
        ),
        migrations.CreateModel(
            name="StockMovement",
            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)),
                (
                    "movement_type",
                    models.CharField(
                        choices=[
                            ("OPENING", "Opening"),
                            ("PURCHASE", "Purchase"),
                            ("PURCHASE_RETURN", "Purchase Return"),
                            ("SALE", "Sale"),
                            ("SALE_RETURN", "Sale Return"),
                            ("TRANSFER_OUT", "Transfer Out"),
                            ("TRANSFER_IN", "Transfer In"),
                            ("ADJUSTMENT_IN", "Adjustment In"),
                            ("ADJUSTMENT_OUT", "Adjustment Out"),
                            ("EXPIRY_WRITE_OFF", "Expiry Write-off"),
                            ("VOID_REVERSAL", "Void Reversal"),
                        ],
                        max_length=30,
                    ),
                ),
                (
                    "quantity_in",
                    models.DecimalField(decimal_places=4, default=0, max_digits=18),
                ),
                (
                    "quantity_out",
                    models.DecimalField(decimal_places=4, default=0, max_digits=18),
                ),
                ("unit_cost", models.DecimalField(decimal_places=6, max_digits=18)),
                (
                    "movement_date",
                    models.DateTimeField(
                        db_index=True, default=django.utils.timezone.now
                    ),
                ),
                (
                    "reference_type",
                    models.CharField(blank=True, db_index=True, max_length=100),
                ),
                (
                    "reference_id",
                    models.CharField(blank=True, db_index=True, max_length=100),
                ),
                ("remarks", models.TextField(blank=True)),
                (
                    "batch",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="stock_movements",
                        to="shop_management.inventorybatch",
                    ),
                ),
                (
                    "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,
                    ),
                ),
                (
                    "currency",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="stock_movements",
                        to="shop_management.currency",
                    ),
                ),
                (
                    "product",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="stock_movements",
                        to="shop_management.product",
                    ),
                ),
                (
                    "warehouse",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="stock_movements",
                        to="shop_management.warehouse",
                    ),
                ),
            ],
            options={
                "ordering": ["-movement_date", "-id"],
            },
        ),
        migrations.AddConstraint(
            model_name="inventorybatch",
            constraint=models.UniqueConstraint(
                fields=("product", "batch_number"), name="uniq_batch_per_product"
            ),
        ),
        migrations.AddConstraint(
            model_name="stockbalance",
            constraint=models.UniqueConstraint(
                fields=("warehouse", "product", "batch"), name="uniq_stock_balance"
            ),
        ),
        migrations.AddConstraint(
            model_name="stockbalance",
            constraint=models.CheckConstraint(
                check=models.Q(("quantity__gte", 0)), name="stock_quantity_nonnegative"
            ),
        ),
        migrations.AddConstraint(
            model_name="stockbalance",
            constraint=models.CheckConstraint(
                check=models.Q(("average_cost__gte", 0)),
                name="stock_average_cost_nonnegative",
            ),
        ),
        migrations.AddConstraint(
            model_name="stockmovement",
            constraint=models.CheckConstraint(
                check=models.Q(("quantity_in__gte", 0)),
                name="movement_quantity_in_nonnegative",
            ),
        ),
        migrations.AddConstraint(
            model_name="stockmovement",
            constraint=models.CheckConstraint(
                check=models.Q(("quantity_out__gte", 0)),
                name="movement_quantity_out_nonnegative",
            ),
        ),
        migrations.AddConstraint(
            model_name="stockmovement",
            constraint=models.CheckConstraint(
                check=models.Q(("unit_cost__gte", 0)),
                name="movement_unit_cost_nonnegative",
            ),
        ),
        migrations.AddConstraint(
            model_name="stockmovement",
            constraint=models.CheckConstraint(
                check=models.Q(
                    models.Q(("quantity_in__gt", 0), ("quantity_out", 0)),
                    models.Q(("quantity_in", 0), ("quantity_out__gt", 0)),
                    _connector="OR",
                ),
                name="movement_has_one_direction",
            ),
        ),
    ]
