from . import models from .models import StockOperation def register_incoming_stock(product, quantity, warehouse, expiration_date=None): operation = StockOperation.objects.create( product=product, quantity=quantity, warehouse=warehouse, operation_type='Incoming', expiration_date=expiration_date ) # Распределение по местам хранения if product.storage_temperature: storage_location = "Холодильник" if product.storage_temperature == "+4C" else "Полка" operation.storage_location = storage_location operation.save() return operation def transfer_stock(product, quantity, from_warehouse, to_location): total_stock = StockOperation.objects.filter( product=product, warehouse=from_warehouse, operation_type='Incoming' ).aggregate(total=models.Sum('quantity'))['total'] or 0 if total_stock < quantity: raise ValueError("Недостаточно товара на складе для перемещения.") transfer_operation = StockOperation.objects.create( product=product, quantity=quantity, warehouse=from_warehouse, storage_location=to_location, operation_type='Transfer' ) return transfer_operation def write_off_stock(product, quantity, warehouse, reason): total_stock = StockOperation.objects.filter( product=product, warehouse=warehouse, operation_type='Incoming' ).aggregate(total=models.Sum('quantity'))['total'] or 0 if total_stock < quantity: raise ValueError("Недостаточно товара на складе для списания.") write_off_operation = StockOperation.objects.create( product=product, quantity=quantity, warehouse=warehouse, operation_type='WriteOff', reason=reason ) return write_off_operation