# -*- coding: utf-8 -*- """ Batch views - READ ONLY - IncomingBatch (Партии поступлений) - StockBatch (Партии товара на складе) """ from django.views.generic import ListView, DetailView from django.contrib.auth.mixins import LoginRequiredMixin from ..models import IncomingBatch, Incoming, StockBatch, SaleBatchAllocation, WriteOff class IncomingBatchListView(LoginRequiredMixin, ListView): """Список всех партий поступлений товара""" model = IncomingBatch template_name = 'inventory/incoming_batch/batch_list.html' context_object_name = 'batches' paginate_by = 30 def get_queryset(self): return IncomingBatch.objects.all().select_related('warehouse').order_by('-created_at') def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) # Добавляем количество товаров в каждую партию for batch in context['batches']: batch.items_count = batch.items.count() batch.total_quantity = sum(item.quantity for item in batch.items.all()) return context class IncomingBatchDetailView(LoginRequiredMixin, DetailView): """Детальная информация по партии поступления""" model = IncomingBatch template_name = 'inventory/incoming_batch/batch_detail.html' context_object_name = 'batch' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) batch = self.get_object() # Товары в этой партии context['items'] = batch.items.all().select_related('product', 'stock_batch') return context class StockBatchListView(LoginRequiredMixin, ListView): """Список всех партий товара на складах""" model = StockBatch template_name = 'inventory/batch/batch_list.html' context_object_name = 'batches' paginate_by = 30 def get_queryset(self): return StockBatch.objects.filter( is_active=True ).select_related('product', 'warehouse').order_by('-created_at') class StockBatchDetailView(LoginRequiredMixin, DetailView): """ Детальная информация по партии товара. Показывает историю операций с данной партией (продажи, списания, перемещения). """ model = StockBatch template_name = 'inventory/batch/batch_detail.html' context_object_name = 'batch' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) batch = self.get_object() # История продаж из этой партии context['sales'] = SaleBatchAllocation.objects.filter( batch=batch ).select_related('sale', 'sale__product') # История списаний из этой партии context['writeoffs'] = WriteOff.objects.filter( batch=batch ).order_by('-date') return context