diff --git a/myproject/inventory/signals.py b/myproject/inventory/signals.py index a309016..b729bb9 100644 --- a/myproject/inventory/signals.py +++ b/myproject/inventory/signals.py @@ -365,6 +365,28 @@ def create_sale_on_order_completion(sender, instance, created, **kwargs): product_kit=kit ).exclude(status='converted_to_sale') + # Если резервы не найдены по order_item (например, при переходе из cancelled), + # ищем витринные резервы через product_kit + if not kit_reservations.exists() and kit.is_temporary and kit.showcase: + logger.info( + f"🔄 Витринный комплект '{kit.name}': резервы не найдены по order_item. " + f"Ищем через product_kit..." + ) + + # Ищем резервы для этого комплекта (могут быть без order_item при переходе из cancelled) + kit_reservations = Reservation.objects.filter( + product_kit=kit, + showcase__isnull=False, + status='reserved' + ) + + # Привязываем резервы к order_item перед созданием Sale + if kit_reservations.exists(): + updated_count = kit_reservations.update(order_item=item) + logger.info( + f" ✅ Привязано {updated_count} резервов к OrderItem #{item.id}" + ) + if not kit_reservations.exists(): # Проверяем, может быть витринный комплект уже продан через ShowcaseManager? already_sold = Reservation.objects.filter(