# Generated by Django 5.0.7 on 2026-09-01 10:08

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", "0082_inventory_core"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="StockAdjustment",
            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)),
                (
                    "adjustment_number",
                    models.CharField(blank=True, max_length=50, null=True, unique=True),
                ),
                (
                    "adjustment_date",
                    models.DateField(default=django.utils.timezone.localdate),
                ),
                ("reason", models.CharField(max_length=255)),
                ("notes", models.TextField(blank=True)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("DRAFT", "Draft"),
                            ("POSTED", "Posted"),
                            ("VOID", "Void"),
                        ],
                        default="DRAFT",
                        max_length=10,
                    ),
                ),
                ("posted_at", models.DateTimeField(blank=True, null=True)),
                ("voided_at", models.DateTimeField(blank=True, null=True)),
                ("void_reason", 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,
                    ),
                ),
                (
                    "posted_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="stock_adjustments_posted",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "voided_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="stock_adjustments_voided",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "warehouse",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="stock_adjustments",
                        to="shop_management.warehouse",
                    ),
                ),
            ],
            options={
                "ordering": ["-adjustment_date", "-id"],
            },
        ),
        migrations.CreateModel(
            name="StockAdjustmentItem",
            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)),
                (
                    "direction",
                    models.CharField(
                        choices=[("IN", "Increase"), ("OUT", "Decrease")], max_length=3
                    ),
                ),
                ("quantity", models.DecimalField(decimal_places=4, max_digits=18)),
                (
                    "unit_cost",
                    models.DecimalField(
                        blank=True,
                        decimal_places=6,
                        help_text="Required for incoming adjustments; outgoing adjustments use current average cost.",
                        max_digits=18,
                        null=True,
                    ),
                ),
                ("remarks", models.TextField(blank=True)),
                (
                    "adjustment",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="items",
                        to="shop_management.stockadjustment",
                    ),
                ),
                (
                    "batch",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="stock_adjustment_items",
                        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_adjustment_items",
                        to="shop_management.currency",
                    ),
                ),
                (
                    "product",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="stock_adjustment_items",
                        to="shop_management.product",
                    ),
                ),
            ],
            options={
                "ordering": ["id"],
            },
        ),
        migrations.AddConstraint(
            model_name="stockadjustmentitem",
            constraint=models.UniqueConstraint(
                fields=("adjustment", "product", "batch"),
                name="uniq_adjustment_product_batch",
            ),
        ),
        migrations.AddConstraint(
            model_name="stockadjustmentitem",
            constraint=models.CheckConstraint(
                check=models.Q(("quantity__gt", 0)),
                name="adjustment_item_quantity_positive",
            ),
        ),
        migrations.AddConstraint(
            model_name="stockadjustmentitem",
            constraint=models.CheckConstraint(
                check=models.Q(
                    ("unit_cost__isnull", True), ("unit_cost__gte", 0), _connector="OR"
                ),
                name="adjustment_item_cost_nonnegative",
            ),
        ),
    ]
