# Generated by Django 5.2.17 on 2026-09-03 17:55

import django.db.models.deletion
import uuid
from decimal import Decimal
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('accounting', '0002_alter_journalentry_reference_type'),
        ('customers', '0004_notificationpreference_email_enabled_and_more'),
        ('inventory', '0001_initial'),
        ('tenancy', '0004_staffprofile'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.AlterField(
            model_name='inventorydevice',
            name='status',
            field=models.CharField(choices=[('AVAILABLE', 'Available'), ('ASSIGNED', 'Assigned'), ('FAULTY', 'Faulty'), ('IN_REPAIR', 'In Repair'), ('RETIRED', 'Retired'), ('SOLD', 'Sold')], default='AVAILABLE', max_length=30),
        ),
        migrations.CreateModel(
            name='InventoryItem',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=200)),
                ('code', models.CharField(max_length=100)),
                ('category', models.CharField(choices=[('CABLES_CONNECTORS', 'Cables & Connectors'), ('OPTICAL_SPLITTERS', 'Optical Splitters'), ('ROUTERS_AP', 'Routers & Access Points'), ('ONU_ONT', 'ONU / ONT Devices'), ('POWER_ADAPTERS', 'Power Adapters'), ('ACCESSORIES', 'Hardware & Accessories'), ('TOOLS_EQUIPMENT', 'Tools & Field Equipment'), ('OTHER', 'Other')], default='ACCESSORIES', max_length=40)),
                ('unit_of_measure', models.CharField(choices=[('PIECES', 'Pieces (Pcs)'), ('METERS', 'Meters (m)'), ('ROLLS', 'Rolls'), ('PACKS', 'Packs'), ('BOXES', 'Boxes')], default='PIECES', max_length=30)),
                ('unit_cost_price', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=12)),
                ('unit_selling_price', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=12)),
                ('quantity_on_hand', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=12)),
                ('quantity_damaged', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=12)),
                ('reorder_threshold', models.PositiveIntegerField(default=5)),
                ('is_serialized', models.BooleanField(default=False)),
                ('is_active', models.BooleanField(default=True)),
                ('notes', models.TextField(blank=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_records', to='tenancy.organization')),
            ],
            options={
                'db_table': 'inventory_item',
                'ordering': ['name'],
            },
        ),
        migrations.CreateModel(
            name='PosSale',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('sale_number', models.CharField(max_length=100)),
                ('walk_in_customer_name', models.CharField(blank=True, max_length=200)),
                ('walk_in_customer_phone', models.CharField(blank=True, max_length=50)),
                ('sale_date', models.DateField()),
                ('subtotal_amount', models.DecimalField(decimal_places=2, max_digits=12)),
                ('discount_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=12)),
                ('tax_amount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=12)),
                ('total_amount', models.DecimalField(decimal_places=2, max_digits=12)),
                ('paid_amount', models.DecimalField(decimal_places=2, max_digits=12)),
                ('payment_method', models.CharField(choices=[('CASH', 'Cash'), ('BANK_TRANSFER', 'Bank Transfer'), ('CARD', 'Card'), ('MOBILE_WALLET', 'Mobile Wallet')], default='CASH', max_length=30)),
                ('payment_reference', models.CharField(blank=True, max_length=150)),
                ('status', models.CharField(choices=[('COMPLETED', 'Completed'), ('CANCELLED', 'Cancelled')], default='COMPLETED', max_length=30)),
                ('cancellation_reason', models.TextField(blank=True)),
                ('cancelled_at', models.DateTimeField(blank=True, null=True)),
                ('notes', models.TextField(blank=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('cancelled_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='cancelled_pos_sales', to=settings.AUTH_USER_MODEL)),
                ('customer', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='pos_sales', to='customers.customer')),
                ('journal_entry', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='pos_sale_records', to='accounting.journalentry')),
                ('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_records', to='tenancy.organization')),
                ('sold_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='recorded_pos_sales', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'inventory_pos_sale',
                'ordering': ['-sale_date', '-created_at'],
            },
        ),
        migrations.CreateModel(
            name='PosSaleItem',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('quantity', models.DecimalField(decimal_places=2, default=Decimal('1.00'), max_digits=12)),
                ('unit_price', models.DecimalField(decimal_places=2, max_digits=12)),
                ('unit_cost', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=12)),
                ('line_discount', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=12)),
                ('line_total', models.DecimalField(decimal_places=2, max_digits=12)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('device', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='sold_via_pos', to='inventory.inventorydevice')),
                ('item', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='sale_items', to='inventory.inventoryitem')),
                ('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_records', to='tenancy.organization')),
                ('sale', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='items', to='inventory.possale')),
            ],
            options={
                'db_table': 'inventory_pos_sale_item',
                'ordering': ['created_at'],
            },
        ),
        migrations.CreateModel(
            name='StockMovement',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('movement_type', models.CharField(choices=[('PURCHASE_RESTOCK', 'Purchase / Restock'), ('SALE_DEDUCTION', 'POS Sale Deduction'), ('SERVICE_ISSUANCE', 'Service / Field Issuance'), ('SERVICE_RETURN', 'Service Return'), ('DAMAGE_TRANSFER', 'Transfer to Damaged'), ('DAMAGE_DISPOSAL', 'Disposal of Damaged Stock'), ('MANUAL_ADJUSTMENT', 'Manual Stock Adjustment')], max_length=40)),
                ('quantity', models.DecimalField(decimal_places=2, max_digits=12)),
                ('previous_quantity', models.DecimalField(decimal_places=2, max_digits=12)),
                ('new_quantity', models.DecimalField(decimal_places=2, max_digits=12)),
                ('unit_cost', models.DecimalField(decimal_places=2, default=Decimal('0.00'), max_digits=12)),
                ('reference_type', models.CharField(blank=True, max_length=50)),
                ('reference_id', models.CharField(blank=True, max_length=255)),
                ('notes', models.TextField(blank=True)),
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='inventory_stock_movements', to=settings.AUTH_USER_MODEL)),
                ('item', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='movements', to='inventory.inventoryitem')),
                ('organization', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_records', to='tenancy.organization')),
            ],
            options={
                'db_table': 'inventory_stock_movement',
                'ordering': ['-created_at'],
            },
        ),
        migrations.AddIndex(
            model_name='inventoryitem',
            index=models.Index(fields=['organization', 'category'], name='inv_item_org_cat_idx'),
        ),
        migrations.AddIndex(
            model_name='inventoryitem',
            index=models.Index(fields=['organization', 'is_active'], name='inv_item_org_active_idx'),
        ),
        migrations.AddConstraint(
            model_name='inventoryitem',
            constraint=models.UniqueConstraint(fields=('organization', 'code'), name='unique_item_code_per_org'),
        ),
        migrations.AddConstraint(
            model_name='inventoryitem',
            constraint=models.CheckConstraint(condition=models.Q(('quantity_on_hand__gte', 0)), name='item_quantity_on_hand_non_negative'),
        ),
        migrations.AddConstraint(
            model_name='inventoryitem',
            constraint=models.CheckConstraint(condition=models.Q(('quantity_damaged__gte', 0)), name='item_quantity_damaged_non_negative'),
        ),
        migrations.AddConstraint(
            model_name='inventoryitem',
            constraint=models.CheckConstraint(condition=models.Q(('unit_cost_price__gte', 0)), name='item_unit_cost_non_negative'),
        ),
        migrations.AddConstraint(
            model_name='inventoryitem',
            constraint=models.CheckConstraint(condition=models.Q(('unit_selling_price__gte', 0)), name='item_unit_sell_non_negative'),
        ),
        migrations.AddIndex(
            model_name='possale',
            index=models.Index(fields=['organization', 'sale_date'], name='pos_sale_org_date_idx'),
        ),
        migrations.AddIndex(
            model_name='possale',
            index=models.Index(fields=['organization', 'status'], name='pos_sale_org_status_idx'),
        ),
        migrations.AddIndex(
            model_name='possale',
            index=models.Index(fields=['organization', 'customer'], name='pos_sale_org_cust_idx'),
        ),
        migrations.AddConstraint(
            model_name='possale',
            constraint=models.UniqueConstraint(fields=('organization', 'sale_number'), name='unique_pos_sale_number_per_org'),
        ),
        migrations.AddConstraint(
            model_name='possale',
            constraint=models.CheckConstraint(condition=models.Q(('subtotal_amount__gte', 0)), name='pos_subtotal_non_negative'),
        ),
        migrations.AddConstraint(
            model_name='possale',
            constraint=models.CheckConstraint(condition=models.Q(('total_amount__gte', 0)), name='pos_total_non_negative'),
        ),
        migrations.AddConstraint(
            model_name='possale',
            constraint=models.CheckConstraint(condition=models.Q(('paid_amount__gte', 0)), name='pos_paid_non_negative'),
        ),
        migrations.AddIndex(
            model_name='possaleitem',
            index=models.Index(fields=['organization', 'sale'], name='pos_item_org_sale_idx'),
        ),
        migrations.AddIndex(
            model_name='possaleitem',
            index=models.Index(fields=['organization', 'item'], name='pos_item_org_item_idx'),
        ),
        migrations.AddConstraint(
            model_name='possaleitem',
            constraint=models.CheckConstraint(condition=models.Q(('quantity__gt', 0)), name='pos_item_qty_gt_zero'),
        ),
        migrations.AddConstraint(
            model_name='possaleitem',
            constraint=models.CheckConstraint(condition=models.Q(('unit_price__gte', 0)), name='pos_item_price_non_negative'),
        ),
        migrations.AddConstraint(
            model_name='possaleitem',
            constraint=models.CheckConstraint(condition=models.Q(('line_total__gte', 0)), name='pos_item_total_non_negative'),
        ),
        migrations.AddIndex(
            model_name='stockmovement',
            index=models.Index(fields=['organization', 'item', 'created_at'], name='inv_mvmt_org_item_idx'),
        ),
        migrations.AddIndex(
            model_name='stockmovement',
            index=models.Index(fields=['organization', 'movement_type'], name='inv_mvmt_org_type_idx'),
        ),
    ]
