Fix product reservation system for demo orders

PROBLEM ANALYSIS:
- SQL script created orders bypassing Django ORM
- Django signals (post_save) didn't trigger
- No reservations were created automatically
- Found 51 orders with 102 items and 0 reservations

SOLUTION IMPLEMENTED:

1. Updated create_demo_orders command:
   - Added clear documentation about ORM usage
   - Already uses ORM (.save()) which triggers signals
   - Added informative messages about automatic reservations

2. Created fix_missing_reservations command:
   - Finds OrderItems without reservations
   - Creates missing Reservation records
   - Supports --dry-run mode for safety
   - Handles missing warehouses gracefully

3. Created SQL fix script:
   - Direct SQL approach for existing data
   - Creates reservations for all 102 items
   - Status: 'reserved'
   - Verified: All items now have reservations

4. Added verification scripts:
   - check_orders.py: Shows orders/items/reservations count
   - run_fix_reservations.py: Executes SQL fix

RESULTS:
- ✓ 102 reservations created for existing orders
- ✓ Future orders will use ORM and create reservations automatically
- ✓ System now works correctly

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-08 00:04:55 +03:00
parent e3bab2252e
commit fcc7f2263d
5 changed files with 291 additions and 5 deletions

53
myproject/check_orders.py Normal file
View File

@@ -0,0 +1,53 @@
"""
Проверка созданных заказов и резервов
"""
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()
from django.db import connection
with connection.cursor() as cursor:
cursor.execute("SET search_path TO grach")
# Считаем заказы
cursor.execute("SELECT COUNT(*) FROM grach.orders_order")
orders_count = cursor.fetchone()[0]
print(f"Заказов: {orders_count}")
# Считаем позиции заказов
cursor.execute("SELECT COUNT(*) FROM grach.orders_orderitem")
items_count = cursor.fetchone()[0]
print(f"Позиций в заказах: {items_count}")
# Считаем резервы
cursor.execute("SELECT COUNT(*) FROM grach.inventory_reservation")
reservations_count = cursor.fetchone()[0]
print(f"Резервов: {reservations_count}")
# Детали по заказам без резервов
print("\nПервые 10 позиций без резервов:")
cursor.execute("""
SELECT
o.order_number,
oi.id as item_id,
p.name as product_name,
oi.quantity,
COUNT(r.id) as reservations_count
FROM grach.orders_order o
JOIN grach.orders_orderitem oi ON oi.order_id = o.id
LEFT JOIN grach.products_product p ON p.id = oi.product_id
LEFT JOIN grach.inventory_reservation r ON r.order_item_id = oi.id
GROUP BY o.order_number, oi.id, p.name, oi.quantity
HAVING COUNT(r.id) = 0
ORDER BY o.order_number
LIMIT 10
""")
rows = cursor.fetchall()
if rows:
for row in rows:
print(f" Заказ {row[0]}: ItemID={row[1]}, Товар=\"{row[2]}\", Кол-во={row[3]}, Резервов={row[4]}")
else:
print(" Все позиции имеют резервы!")