# Generated by Django 5.2.17 on 2026-09-27 19:39

import django.core.validators
import django.db.models.deletion
import uuid
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('inventory', '0002_ledger_integrity'),
        ('organization', '0002_userorganizationassignment'),
        ('parts', '0001_initial'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='GoodsReceipt',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('number', models.CharField(max_length=22)),
                ('external_reference', models.CharField(blank=True, max_length=128)),
                ('note', models.TextField(blank=True, validators=[django.core.validators.MaxLengthValidator(2000)])),
                ('status', models.CharField(choices=[('DRAFT', 'Draft'), ('POSTED', 'Posted'), ('CANCELLED', 'Cancelled')], default='DRAFT', max_length=12)),
                ('received_at', models.DateTimeField(blank=True, null=True)),
                ('cancelled_at', models.DateTimeField(blank=True, null=True)),
                ('cancellation_reason', models.CharField(blank=True, max_length=500)),
            ],
            options={
                'ordering': ['company_id', 'number'],
            },
        ),
        migrations.CreateModel(
            name='GoodsReceiptIdentifier',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('identifier', models.CharField(max_length=128)),
                ('is_active', models.BooleanField(default=True)),
            ],
        ),
        migrations.CreateModel(
            name='GoodsReceiptLine',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('quantity', models.PositiveIntegerField()),
                ('is_active', models.BooleanField(default=True)),
            ],
            options={
                'ordering': ['spare_part_id', 'pk'],
            },
        ),
        migrations.CreateModel(
            name='InventoryDocumentSequence',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('kind', models.CharField(choices=[('RECEIPT', 'Receipt'), ('TRANSFER', 'Transfer')], max_length=8)),
                ('next_value', models.PositiveBigIntegerField(default=1)),
            ],
        ),
        migrations.CreateModel(
            name='StockTransfer',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('number', models.CharField(max_length=22)),
                ('note', models.TextField(blank=True, validators=[django.core.validators.MaxLengthValidator(2000)])),
                ('status', models.CharField(choices=[('DRAFT', 'Draft'), ('DISPATCHED', 'Dispatched'), ('RECEIVED', 'Received'), ('CANCELLED', 'Cancelled')], default='DRAFT', max_length=12)),
                ('dispatched_at', models.DateTimeField(blank=True, null=True)),
                ('received_at', models.DateTimeField(blank=True, null=True)),
                ('cancelled_at', models.DateTimeField(blank=True, null=True)),
                ('cancellation_reason', models.CharField(blank=True, max_length=500)),
            ],
            options={
                'ordering': ['company_id', 'number'],
            },
        ),
        migrations.CreateModel(
            name='StockTransferLine',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('quantity', models.PositiveIntegerField()),
                ('is_active', models.BooleanField(default=True)),
            ],
            options={
                'ordering': ['spare_part_id', 'pk'],
            },
        ),
        migrations.CreateModel(
            name='StockTransferUnit',
            fields=[
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('is_active', models.BooleanField(default=True)),
            ],
        ),
        migrations.RemoveConstraint(
            model_name='inventorylocation',
            name='inventory_location_type_valid',
        ),
        migrations.RemoveConstraint(
            model_name='serializedstockunit',
            name='inventory_unit_state_location',
        ),
        migrations.AlterField(
            model_name='inventorylocation',
            name='location_type',
            field=models.CharField(choices=[('WAREHOUSE', 'Warehouse'), ('STORE', 'Service-center store'), ('QUARANTINE', 'Quarantine'), ('DEFECTIVE', 'Defective'), ('TRANSIT', 'In transit (managed by transfer)')], max_length=16),
        ),
        migrations.AlterField(
            model_name='serializedstockunit',
            name='state',
            field=models.CharField(choices=[('REGISTERED', 'Registered, not yet received'), ('IN_STOCK', 'In stock'), ('IN_TRANSIT', 'In transit')], default='REGISTERED', max_length=16),
        ),
        migrations.AddConstraint(
            model_name='inventorylocation',
            constraint=models.CheckConstraint(condition=models.Q(('location_type__in', ['WAREHOUSE', 'STORE', 'QUARANTINE', 'DEFECTIVE', 'TRANSIT'])), name='inventory_location_type_valid'),
        ),
        migrations.AddConstraint(
            model_name='serializedstockunit',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('current_location__isnull', True), ('current_movement__isnull', True), ('state', 'REGISTERED')), models.Q(('current_location__isnull', False), ('current_movement__isnull', False), ('state__in', ['IN_STOCK', 'IN_TRANSIT'])), _connector='OR'), name='inventory_unit_state_location'),
        ),
        migrations.AddField(
            model_name='goodsreceipt',
            name='cancelled_by',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='cancelled_goods_receipts', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='goodsreceipt',
            name='company',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='organization.company'),
        ),
        migrations.AddField(
            model_name='goodsreceipt',
            name='created_by',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='created_goods_receipts', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='goodsreceipt',
            name='destination',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='inventory.inventorylocation'),
        ),
        migrations.AddField(
            model_name='goodsreceipt',
            name='received_by',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='posted_goods_receipts', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='goodsreceiptidentifier',
            name='unit',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='inventory.serializedstockunit'),
        ),
        migrations.AddField(
            model_name='goodsreceiptline',
            name='movement',
            field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='receipt_line', to='inventory.stockmovement'),
        ),
        migrations.AddField(
            model_name='goodsreceiptline',
            name='receipt',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='lines', to='inventory.goodsreceipt'),
        ),
        migrations.AddField(
            model_name='goodsreceiptline',
            name='spare_part',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='parts.sparepart'),
        ),
        migrations.AddField(
            model_name='goodsreceiptidentifier',
            name='line',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='identifiers', to='inventory.goodsreceiptline'),
        ),
        migrations.AddField(
            model_name='inventorydocumentsequence',
            name='company',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='organization.company'),
        ),
        migrations.AddField(
            model_name='stocktransfer',
            name='cancelled_by',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='cancelled_stock_transfers', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='stocktransfer',
            name='company',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='organization.company'),
        ),
        migrations.AddField(
            model_name='stocktransfer',
            name='created_by',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='created_stock_transfers', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='stocktransfer',
            name='destination',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='transfer_destinations', to='inventory.inventorylocation'),
        ),
        migrations.AddField(
            model_name='stocktransfer',
            name='dispatched_by',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='dispatched_stock_transfers', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='stocktransfer',
            name='received_by',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='received_stock_transfers', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='stocktransfer',
            name='source',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='transfer_sources', to='inventory.inventorylocation'),
        ),
        migrations.AddField(
            model_name='stocktransfer',
            name='transit_location',
            field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='managed_transfer', to='inventory.inventorylocation'),
        ),
        migrations.AddField(
            model_name='stocktransferline',
            name='dispatch_movement',
            field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='transfer_dispatch_line', to='inventory.stockmovement'),
        ),
        migrations.AddField(
            model_name='stocktransferline',
            name='receive_movement',
            field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='transfer_receive_line', to='inventory.stockmovement'),
        ),
        migrations.AddField(
            model_name='stocktransferline',
            name='spare_part',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='parts.sparepart'),
        ),
        migrations.AddField(
            model_name='stocktransferline',
            name='transfer',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='lines', to='inventory.stocktransfer'),
        ),
        migrations.AddField(
            model_name='stocktransferunit',
            name='line',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='units', to='inventory.stocktransferline'),
        ),
        migrations.AddField(
            model_name='stocktransferunit',
            name='unit',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='inventory.serializedstockunit'),
        ),
        migrations.AddConstraint(
            model_name='goodsreceipt',
            constraint=models.UniqueConstraint(fields=('company', 'number'), name='inventory_receipt_number_unique'),
        ),
        migrations.AddConstraint(
            model_name='goodsreceipt',
            constraint=models.CheckConstraint(condition=models.Q(('number__regex', '^GRN-[0-9]{8,18}$')), name='inventory_receipt_number_format'),
        ),
        migrations.AddConstraint(
            model_name='goodsreceipt',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('cancellation_reason', ''), ('cancelled_at__isnull', True), ('cancelled_by__isnull', True), ('received_at__isnull', True), ('received_by__isnull', True), ('status', 'DRAFT')), models.Q(('cancellation_reason', ''), ('cancelled_at__isnull', True), ('cancelled_by__isnull', True), ('received_at__isnull', False), ('received_by__isnull', False), ('status', 'POSTED')), models.Q(('cancelled_at__isnull', False), ('cancelled_by__isnull', False), ('received_at__isnull', True), ('received_by__isnull', True), ('status', 'CANCELLED'), models.Q(('cancellation_reason', ''), _negated=True)), _connector='OR'), name='inventory_receipt_status_evidence'),
        ),
        migrations.AddConstraint(
            model_name='goodsreceiptline',
            constraint=models.UniqueConstraint(fields=('receipt', 'spare_part'), name='inventory_receipt_part_unique'),
        ),
        migrations.AddConstraint(
            model_name='goodsreceiptline',
            constraint=models.CheckConstraint(condition=models.Q(('quantity__gte', 1), ('quantity__lte', 1000000000)), name='inventory_receipt_line_quantity'),
        ),
        migrations.AddConstraint(
            model_name='goodsreceiptidentifier',
            constraint=models.UniqueConstraint(fields=('line', 'identifier'), name='inventory_receipt_identifier_unique'),
        ),
        migrations.AddConstraint(
            model_name='goodsreceiptidentifier',
            constraint=models.CheckConstraint(condition=models.Q(('identifier', ''), _negated=True), name='inventory_receipt_identifier_nonempty'),
        ),
        migrations.AddConstraint(
            model_name='inventorydocumentsequence',
            constraint=models.UniqueConstraint(fields=('company', 'kind'), name='inventory_sequence_company_kind_unique'),
        ),
        migrations.AddConstraint(
            model_name='inventorydocumentsequence',
            constraint=models.CheckConstraint(condition=models.Q(('next_value__gte', 1), ('next_value__lte', 1000000000000000000)), name='inventory_sequence_positive'),
        ),
        migrations.AddConstraint(
            model_name='inventorydocumentsequence',
            constraint=models.CheckConstraint(condition=models.Q(('kind__in', ['RECEIPT', 'TRANSFER'])), name='inventory_sequence_kind_valid'),
        ),
        migrations.AddConstraint(
            model_name='stocktransfer',
            constraint=models.UniqueConstraint(fields=('company', 'number'), name='inventory_transfer_number_unique'),
        ),
        migrations.AddConstraint(
            model_name='stocktransfer',
            constraint=models.CheckConstraint(condition=models.Q(('number__regex', '^TRF-[0-9]{8,18}$')), name='inventory_transfer_number_format'),
        ),
        migrations.AddConstraint(
            model_name='stocktransfer',
            constraint=models.CheckConstraint(condition=models.Q(('source', models.F('destination')), _negated=True), name='inventory_transfer_distinct_locations'),
        ),
        migrations.AddConstraint(
            model_name='stocktransfer',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('cancellation_reason', ''), ('cancelled_at__isnull', True), ('cancelled_by__isnull', True), ('dispatched_at__isnull', True), ('dispatched_by__isnull', True), ('received_at__isnull', True), ('received_by__isnull', True), ('status', 'DRAFT'), ('transit_location__isnull', True)), models.Q(('cancellation_reason', ''), ('cancelled_at__isnull', True), ('cancelled_by__isnull', True), ('dispatched_at__isnull', False), ('dispatched_by__isnull', False), ('received_at__isnull', True), ('received_by__isnull', True), ('status', 'DISPATCHED'), ('transit_location__isnull', False)), models.Q(('cancellation_reason', ''), ('cancelled_at__isnull', True), ('cancelled_by__isnull', True), ('dispatched_at__isnull', False), ('dispatched_by__isnull', False), ('received_at__isnull', False), ('received_by__isnull', False), ('status', 'RECEIVED'), ('transit_location__isnull', False)), models.Q(('cancelled_at__isnull', False), ('cancelled_by__isnull', False), ('dispatched_at__isnull', True), ('dispatched_by__isnull', True), ('received_at__isnull', True), ('received_by__isnull', True), ('status', 'CANCELLED'), ('transit_location__isnull', True), models.Q(('cancellation_reason', ''), _negated=True)), _connector='OR'), name='inventory_transfer_status_evidence'),
        ),
        migrations.AddConstraint(
            model_name='stocktransfer',
            constraint=models.CheckConstraint(condition=models.Q(('received_at__isnull', True), ('received_at__gte', models.F('dispatched_at')), _connector='OR'), name='inventory_transfer_time_order'),
        ),
        migrations.AddConstraint(
            model_name='stocktransferline',
            constraint=models.UniqueConstraint(fields=('transfer', 'spare_part'), name='inventory_transfer_part_unique'),
        ),
        migrations.AddConstraint(
            model_name='stocktransferline',
            constraint=models.CheckConstraint(condition=models.Q(('quantity__gte', 1), ('quantity__lte', 1000000000)), name='inventory_transfer_line_quantity'),
        ),
        migrations.AddConstraint(
            model_name='stocktransferunit',
            constraint=models.UniqueConstraint(fields=('line', 'unit'), name='inventory_transfer_unit_unique'),
        ),
    ]
