Files
octopus/myproject/check_order_101.py
Andrey Smakotin 920dbf4273 Добавлена защита от повторного списания + команда исправления дубликатов
Проблема: Сигнал 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>
2025-11-30 22:15:33 +03:00

75 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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()