diff --git a/myproject/inventory/admin.py b/myproject/inventory/admin.py index f677ffd..9b7fc1b 100644 --- a/myproject/inventory/admin.py +++ b/myproject/inventory/admin.py @@ -94,13 +94,13 @@ class StockBatchAdmin(admin.ModelAdmin): # ===== INCOMING BATCH ===== @admin.register(IncomingBatch) class IncomingBatchAdmin(admin.ModelAdmin): - list_display = ('document_number', 'warehouse', 'supplier_name', 'items_count', 'created_at') - list_filter = ('warehouse', 'created_at') + list_display = ('document_number', 'warehouse', 'receipt_type_display', 'supplier_name', 'items_count', 'created_at') + list_filter = ('warehouse', 'receipt_type', 'created_at') search_fields = ('document_number', 'supplier_name') date_hierarchy = 'created_at' fieldsets = ( ('Партия поступления', { - 'fields': ('document_number', 'warehouse', 'supplier_name', 'notes') + 'fields': ('document_number', 'warehouse', 'receipt_type', 'supplier_name', 'notes') }), ('Даты', { 'fields': ('created_at', 'updated_at'), @@ -113,6 +113,20 @@ class IncomingBatchAdmin(admin.ModelAdmin): return obj.items.count() items_count.short_description = 'Товаров' + def receipt_type_display(self, obj): + colors = { + 'supplier': '#0d6efd', # primary (синий) + 'inventory': '#0dcaf0', # info (голубой) + 'adjustment': '#198754', # success (зеленый) + } + color = colors.get(obj.receipt_type, '#6c757d') + return format_html( + '{}', + color, + obj.get_receipt_type_display() + ) + receipt_type_display.short_description = 'Тип поступления' + # ===== INCOMING ===== @admin.register(Incoming) diff --git a/myproject/inventory/forms.py b/myproject/inventory/forms.py index 40b8c1e..2b6ac4b 100644 --- a/myproject/inventory/forms.py +++ b/myproject/inventory/forms.py @@ -272,6 +272,13 @@ class IncomingForm(forms.Form): required=False ) + receipt_type = forms.CharField( + max_length=20, + widget=forms.HiddenInput(), + initial='supplier', + required=False + ) + supplier_name = forms.CharField( max_length=200, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'ООО Поставщик'}), diff --git a/myproject/inventory/migrations/0013_add_receipt_type_to_incomingbatch.py b/myproject/inventory/migrations/0013_add_receipt_type_to_incomingbatch.py new file mode 100644 index 0000000..ef2cf24 --- /dev/null +++ b/myproject/inventory/migrations/0013_add_receipt_type_to_incomingbatch.py @@ -0,0 +1,22 @@ +# Generated by Django 5.0.10 on 2025-12-20 20:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('inventory', '0012_change_sold_order_item_to_fk'), + ] + + operations = [ + migrations.AddField( + model_name='incomingbatch', + name='receipt_type', + field=models.CharField(choices=[('supplier', 'Поступление от поставщика'), ('inventory', 'Оприходование при инвентаризации'), ('adjustment', 'Оприходование без инвентаризации')], db_index=True, default='supplier', max_length=20, verbose_name='Тип поступления'), + ), + migrations.AddIndex( + model_name='incomingbatch', + index=models.Index(fields=['receipt_type'], name='inventory_i_receipt_ce70c1_idx'), + ), + ] diff --git a/myproject/inventory/models.py b/myproject/inventory/models.py index 7ce7ffe..c7d6bed 100644 --- a/myproject/inventory/models.py +++ b/myproject/inventory/models.py @@ -105,10 +105,23 @@ class IncomingBatch(models.Model): Партия поступления товара (один номер документа = одна партия). Содержит один номер документа и может включать несколько товаров. """ + RECEIPT_TYPE_CHOICES = [ + ('supplier', 'Поступление от поставщика'), + ('inventory', 'Оприходование при инвентаризации'), + ('adjustment', 'Оприходование без инвентаризации'), + ] + warehouse = models.ForeignKey(Warehouse, on_delete=models.CASCADE, related_name='incoming_batches', verbose_name="Склад") document_number = models.CharField(max_length=100, unique=True, db_index=True, verbose_name="Номер документа") + receipt_type = models.CharField( + max_length=20, + choices=RECEIPT_TYPE_CHOICES, + default='supplier', + db_index=True, + verbose_name="Тип поступления" + ) supplier_name = models.CharField(max_length=200, blank=True, null=True, verbose_name="Наименование поставщика") notes = models.TextField(blank=True, null=True, verbose_name="Примечания") @@ -122,6 +135,7 @@ class IncomingBatch(models.Model): indexes = [ models.Index(fields=['document_number']), models.Index(fields=['warehouse']), + models.Index(fields=['receipt_type']), models.Index(fields=['-created_at']), ] diff --git a/myproject/inventory/services/inventory_processor.py b/myproject/inventory/services/inventory_processor.py index 067309c..4c84f40 100644 --- a/myproject/inventory/services/inventory_processor.py +++ b/myproject/inventory/services/inventory_processor.py @@ -11,10 +11,11 @@ from django.db import transaction from django.utils import timezone from inventory.models import ( - Inventory, InventoryLine, WriteOff, Incoming, + Inventory, InventoryLine, WriteOff, Incoming, IncomingBatch, StockBatch, Stock ) from inventory.services.batch_manager import StockBatchManager +from inventory.utils import generate_incoming_document_number class InventoryProcessor: @@ -142,21 +143,24 @@ class InventoryProcessor: inventory.warehouse ) - # Создаем новую партию - batch = StockBatchManager.create_batch( - line.product, - inventory.warehouse, - quantity_surplus, - cost_price + # Генерируем номер документа для поступления + document_number = generate_incoming_document_number() + + # Создаем IncomingBatch с типом 'inventory' + incoming_batch = IncomingBatch.objects.create( + warehouse=inventory.warehouse, + document_number=document_number, + receipt_type='inventory', + notes=f'Оприходование при инвентаризации {inventory.id}, строка {line.id}' ) # Создаем документ Incoming + # Сигнал create_stock_batch_on_incoming автоматически создаст StockBatch Incoming.objects.create( + batch=incoming_batch, product=line.product, - warehouse=inventory.warehouse, quantity=quantity_surplus, cost_price=cost_price, - batch=batch, notes=f'Инвентаризация {inventory.id}, строка {line.id}' ) diff --git a/myproject/inventory/templates/inventory/base_inventory_minimal.html b/myproject/inventory/templates/inventory/base_inventory_minimal.html index 1a0c8a3..9b79564 100644 --- a/myproject/inventory/templates/inventory/base_inventory_minimal.html +++ b/myproject/inventory/templates/inventory/base_inventory_minimal.html @@ -25,6 +25,7 @@