# Generated by Django 5.2.11 on 2026-02-27 22:00

import InvenTree.fields
import InvenTree.models
import django.core.validators
import django.db.models.deletion
import generic.states.fields
import generic.states.states
import generic.states.transition
import generic.states.validators
import order.status_codes
import order.validators
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("common", "0041_auto_20251203_1244"),
        ("company", "0077_delete_manufacturerpartparameter"),
        ("order", "0117_purchaseorderextraline_line_int_and_more"),
        ("part", "0146_auto_20251203_1241"),
        ("stock", "0116_alter_stockitem_link"),
        ("users", "0005_owner_model"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="TransferOrder",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "metadata",
                    models.JSONField(
                        blank=True,
                        help_text="JSON metadata field, for use by external plugins",
                        null=True,
                        verbose_name="Plugin Metadata",
                    ),
                ),
                ("reference_int", models.BigIntegerField(default=0)),
                (
                    "notes",
                    InvenTree.fields.InvenTreeNotesField(
                        blank=True,
                        help_text="Markdown notes (optional)",
                        max_length=50000,
                        null=True,
                        verbose_name="Notes",
                    ),
                ),
                (
                    "barcode_data",
                    models.CharField(
                        blank=True,
                        help_text="Third party barcode data",
                        max_length=500,
                        verbose_name="Barcode Data",
                    ),
                ),
                (
                    "barcode_hash",
                    models.CharField(
                        blank=True,
                        help_text="Unique hash of barcode data",
                        max_length=128,
                        verbose_name="Barcode Hash",
                    ),
                ),
                (
                    "description",
                    models.CharField(
                        blank=True,
                        help_text="Order description (optional)",
                        max_length=250,
                        verbose_name="Description",
                    ),
                ),
                (
                    "link",
                    InvenTree.fields.InvenTreeURLField(
                        blank=True,
                        help_text="Link to external page",
                        max_length=2000,
                        verbose_name="Link",
                    ),
                ),
                (
                    "start_date",
                    models.DateField(
                        blank=True,
                        help_text="Scheduled start date for this order",
                        null=True,
                        verbose_name="Start date",
                    ),
                ),
                (
                    "target_date",
                    models.DateField(
                        blank=True,
                        help_text="Expected date for order delivery. Order will be overdue after this date.",
                        null=True,
                        verbose_name="Target Date",
                    ),
                ),
                (
                    "creation_date",
                    models.DateField(
                        blank=True, null=True, verbose_name="Creation Date"
                    ),
                ),
                (
                    "issue_date",
                    models.DateField(
                        blank=True,
                        help_text="Date order was issued",
                        null=True,
                        verbose_name="Issue Date",
                    ),
                ),
                (
                    "reference",
                    models.CharField(
                        default=order.validators.generate_next_transfer_order_reference,
                        help_text="Transfer Order Reference",
                        max_length=64,
                        unique=True,
                        validators=[order.validators.validate_transfer_order_reference],
                        verbose_name="Reference",
                    ),
                ),
                (
                    "consume",
                    models.BooleanField(
                        default=False,
                        help_text='Rather than transfer the stock to the destination, "consume" it, by removing transferred quantity from the allocated stock item',
                        verbose_name="Consume Stock",
                    ),
                ),
                (
                    "complete_date",
                    models.DateField(
                        blank=True,
                        help_text="Date order was completed",
                        null=True,
                        verbose_name="Completion Date",
                    ),
                ),
                (
                    "status_custom_key",
                    generic.states.fields.ExtraInvenTreeCustomStatusModelField(
                        blank=True,
                        default=None,
                        help_text="Additional status information for this item",
                        null=True,
                        validators=[
                            generic.states.validators.CustomStatusCodeValidator(
                                status_class=order.status_codes.TransferOrderStatus
                            )
                        ],
                        verbose_name="Custom status key",
                    ),
                ),
                (
                    "status",
                    generic.states.fields.InvenTreeCustomStatusModelField(
                        choices=[
                            (10, "Pending"),
                            (20, "Issued"),
                            (25, "On Hold"),
                            (30, "Complete"),
                            (40, "Cancelled"),
                        ],
                        default=10,
                        help_text="Transfer order status",
                        validators=[
                            generic.states.validators.CustomStatusCodeValidator(
                                status_class=order.status_codes.TransferOrderStatus
                            )
                        ],
                        verbose_name="Status",
                    ),
                ),
                (
                    "address",
                    models.ForeignKey(
                        blank=True,
                        help_text="Company address for this order",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to="company.address",
                        verbose_name="Address",
                    ),
                ),
                (
                    "contact",
                    models.ForeignKey(
                        blank=True,
                        help_text="Point of contact for this order",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to="company.contact",
                        verbose_name="Contact",
                    ),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Created By",
                    ),
                ),
                (
                    "destination",
                    models.ForeignKey(
                        blank=True,
                        help_text="Destination for transferred items",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="incoming_transfers",
                        to="stock.stocklocation",
                        verbose_name="Destination Location",
                    ),
                ),
                (
                    "project_code",
                    models.ForeignKey(
                        blank=True,
                        help_text="Select project code for this order",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to="common.projectcode",
                        verbose_name="Project Code",
                    ),
                ),
                (
                    "responsible",
                    models.ForeignKey(
                        blank=True,
                        help_text="User or group responsible for this order",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to="users.owner",
                        verbose_name="Responsible",
                    ),
                ),
                (
                    "take_from",
                    models.ForeignKey(
                        blank=True,
                        help_text="Source for transferred items",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="sourcing_transfers",
                        to="stock.stocklocation",
                        verbose_name="Source Location",
                    ),
                ),
                (
                    "updated_at",
                    models.DateTimeField(
                        blank=True,
                        help_text="Timestamp of last update",
                        null=True,
                        verbose_name="Updated At",
                    ),
                )
            ],
            options={
                "verbose_name": "Transfer Order",
            },
            bases=(
                generic.states.states.StatusCodeMixin,
                generic.states.transition.StateTransitionMixin,
                InvenTree.models.InvenTreeAttachmentMixin,
                InvenTree.models.InvenTreePermissionCheckMixin,
                InvenTree.models.ContentTypeMixin,
                InvenTree.models.PluginValidationMixin,
                models.Model,
            ),
        ),
        migrations.CreateModel(
            name="TransferOrderLineItem",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "metadata",
                    models.JSONField(
                        blank=True,
                        help_text="JSON metadata field, for use by external plugins",
                        null=True,
                        verbose_name="Plugin Metadata",
                    ),
                ),
                (
                    "quantity",
                    InvenTree.fields.RoundingDecimalField(
                        decimal_places=5,
                        default=1,
                        help_text="Item quantity",
                        max_digits=15,
                        validators=[django.core.validators.MinValueValidator(0)],
                        verbose_name="Quantity",
                    ),
                ),
                (
                    "reference",
                    models.CharField(
                        blank=True,
                        help_text="Line item reference",
                        max_length=100,
                        verbose_name="Reference",
                    ),
                ),
                (
                    "notes",
                    models.CharField(
                        blank=True,
                        help_text="Line item notes",
                        max_length=500,
                        verbose_name="Notes",
                    ),
                ),
                (
                    "link",
                    InvenTree.fields.InvenTreeURLField(
                        blank=True,
                        help_text="Link to external page",
                        max_length=2000,
                        verbose_name="Link",
                    ),
                ),
                (
                    "target_date",
                    models.DateField(
                        blank=True,
                        help_text="Target date for this line item (leave blank to use the target date from the order)",
                        null=True,
                        verbose_name="Target Date",
                    ),
                ),
                (
                    "order",
                    models.ForeignKey(
                        help_text="Transfer Order",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="lines",
                        to="order.transferorder",
                        verbose_name="Order",
                    ),
                ),
                (
                    "part",
                    models.ForeignKey(
                        help_text="Part",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="transfer_order_line_items",
                        to="part.part",
                        verbose_name="Part",
                    ),
                ),
                (
                    "project_code",
                    models.ForeignKey(
                        blank=True,
                        help_text="Select project code for this order",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to="common.projectcode",
                        verbose_name="Project Code",
                    ),
                ),
                (
                    "transferred",
                    InvenTree.fields.RoundingDecimalField(
                        decimal_places=5,
                        default=0,
                        help_text="transferred quantity",
                        max_digits=15,
                        validators=[django.core.validators.MinValueValidator(0)],
                        verbose_name="transferred",
                    ),
                ),
                (
                    "line",
                    models.CharField(
                        blank=True,
                        default="",
                        help_text="Line number for this item (optional)",
                        max_length=20,
                        verbose_name="Line Number",
                    ),
                )
            ],
            options={
                "verbose_name": "Transfer Order Line Item",
            },
            bases=(
                InvenTree.models.ContentTypeMixin,
                InvenTree.models.PluginValidationMixin,
                models.Model,
            ),
        ),
        migrations.CreateModel(
            name="TransferOrderAllocation",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "quantity",
                    InvenTree.fields.RoundingDecimalField(
                        decimal_places=5,
                        default=1,
                        help_text="Enter stock allocation quantity",
                        max_digits=15,
                        validators=[django.core.validators.MinValueValidator(0)],
                        verbose_name="Quantity",
                    ),
                ),
                (
                    "item",
                    models.ForeignKey(
                        help_text="Select stock item to allocate",
                        limit_choices_to={
                            "belongs_to": None,
                            "part__virtual": False,
                            "sales_order": None,
                        },
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="transfer_order_allocations",
                        to="stock.stockitem",
                        verbose_name="Item",
                    ),
                ),
                (
                    "line",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="allocations",
                        to="order.transferorderlineitem",
                        verbose_name="Line",
                    ),
                ),
            ],
            options={
                "verbose_name": "Transfer Order Allocation",
            },
        ),
    ]
