Проблема: Сигнал post_save срабатывает несколько раз, создавая дубликаты Sale для одного заказа. Решения: 1. Добавлена проверка Sale.objects.filter(order=instance).exists() перед созданием продаж (inventory/signals.py:74-75) 2. Создана management команда fix_duplicate_sales для исправления существующих дубликатов 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
75 lines
2.9 KiB
Python
75 lines
2.9 KiB
Python
from django.db import connection
|
||
from orders.models import Order
|
||
from inventory.models import Reservation, Sale, Stock, StockBatch, Warehouse
|
||
|
||
# Устанавливаем схему тенанта
|
||
connection.set_schema('buba')
|
||
|
||
# Получаем заказ
|
||
try:
|
||
order = Order.objects.get(order_number='101')
|
||
|
||
print("=" * 60)
|
||
print(f"ЗАКАЗ: {order.order_number}")
|
||
print("=" * 60)
|
||
print(f"ID: {order.id}")
|
||
print(f"Статус: '{order.status}'")
|
||
print(f"Склад самовывоза: {order.pickup_warehouse}")
|
||
|
||
print("\n" + "=" * 60)
|
||
print("ТОВАРЫ В ЗАКАЗЕ")
|
||
print("=" * 60)
|
||
items = order.items.all()
|
||
print(f"Количество товаров: {items.count()}")
|
||
for item in items:
|
||
product = item.product or item.product_kit
|
||
print(f" - {product.name if product else 'НЕТ!'}: {item.quantity} шт, цена: {item.price}")
|
||
|
||
print("\n" + "=" * 60)
|
||
print("РЕЗЕРВЫ")
|
||
print("=" * 60)
|
||
reservations = Reservation.objects.filter(order_item__order=order)
|
||
print(f"Количество резервов: {reservations.count()}")
|
||
for res in reservations:
|
||
print(f" - {res.product.name}: {res.quantity} шт, статус: '{res.status}'")
|
||
|
||
print("\n" + "=" * 60)
|
||
print("ПРОДАЖИ (Sale)")
|
||
print("=" * 60)
|
||
sales = Sale.objects.filter(order=order)
|
||
print(f"Количество продаж: {sales.count()}")
|
||
if sales:
|
||
for sale in sales:
|
||
print(f" - {sale.product.name}: {sale.quantity} шт, обработано: {sale.processed}")
|
||
else:
|
||
print(" ⚠️ ПРОДАЖ НЕТ!")
|
||
|
||
print("\n" + "=" * 60)
|
||
print("ДИАГНОСТИКА")
|
||
print("=" * 60)
|
||
|
||
warehouse = order.pickup_warehouse or Warehouse.objects.filter(is_active=True).first()
|
||
|
||
print(f"Статус заказа: '{order.status}' (тип: {type(order.status).__name__})")
|
||
print(f"Условие: order.status != 'completed' = {order.status != 'completed'}")
|
||
print(f"Склад: {warehouse.name if warehouse else 'НЕ НАЙДЕН!'}")
|
||
|
||
# Проверяем наличие товара
|
||
if warehouse and items:
|
||
print("\nОстатки на складе:")
|
||
for item in items:
|
||
product = item.product or item.product_kit
|
||
if product:
|
||
batches = StockBatch.objects.filter(product=product, warehouse=warehouse, is_active=True)
|
||
total = sum(b.quantity for b in batches)
|
||
print(f" - {product.name}: {total} доступно (нужно {item.quantity})")
|
||
if total < item.quantity:
|
||
print(f" ⚠️ НЕДОСТАТОЧНО! (не хватает {item.quantity - total})")
|
||
|
||
except Order.DoesNotExist:
|
||
print("⚠️ ЗАКАЗ 101 НЕ НАЙДЕН В ТЕНАНТЕ 'buba'!")
|
||
except Exception as e:
|
||
print(f"❌ ОШИБКА: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|