Fix: Implement proper deletion of order items with confirmation dialog
Fixes deletion functionality for order items across frontend and backend: - Remove restriction preventing deletion of last item - Add confirmation dialog before deletion - Properly track and send deleted item IDs to backend via autosave - Update backend to handle item deletion by ID instead of index - Fix visual feedback: deleted items are hidden immediately - Auto-recalculate total sum after deletion Technical changes: - order_form.html: Add confirmation dialog, trigger autosave on delete - autosave.js: Collect deleted item IDs, send to backend - draft_service.py: Process deleted_item_ids, update items by ID 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -208,6 +208,13 @@ class DraftOrderService:
|
||||
|
||||
setattr(order, field, value)
|
||||
|
||||
# Обрабатываем удаление позиций заказа
|
||||
if 'deleted_item_ids' in data:
|
||||
deleted_ids = data['deleted_item_ids']
|
||||
if deleted_ids:
|
||||
from ..models import OrderItem
|
||||
OrderItem.objects.filter(id__in=deleted_ids, order=order).delete()
|
||||
|
||||
# Обрабатываем позиции заказа (items)
|
||||
if 'items' in data:
|
||||
# Импортируем модели
|
||||
@@ -216,18 +223,9 @@ class DraftOrderService:
|
||||
|
||||
items_data = data['items']
|
||||
|
||||
# Получаем существующие позиции
|
||||
existing_items = list(order.items.all())
|
||||
|
||||
# Удаляем все существующие позиции, которых нет в новых данных
|
||||
items_to_keep_count = len(items_data)
|
||||
for i, existing_item in enumerate(existing_items):
|
||||
if i >= items_to_keep_count:
|
||||
# Удаляем лишние позиции
|
||||
existing_item.delete()
|
||||
|
||||
# Обновляем или создаём позиции
|
||||
for index, item_data in enumerate(items_data):
|
||||
# Обрабатываем каждую позицию
|
||||
for item_data in items_data:
|
||||
item_id = item_data.get('id') # ID существующей позиции (если есть)
|
||||
product_id = item_data.get('product_id')
|
||||
product_kit_id = item_data.get('product_kit_id')
|
||||
quantity = item_data.get('quantity', 1)
|
||||
@@ -275,17 +273,28 @@ class DraftOrderService:
|
||||
is_custom_price = False
|
||||
|
||||
# Обновляем существующую позицию или создаём новую
|
||||
if index < len(existing_items):
|
||||
# Обновляем существующую
|
||||
item = existing_items[index]
|
||||
item.product = product
|
||||
item.product_kit = product_kit
|
||||
item.quantity = quantity
|
||||
item.price = price
|
||||
item.is_custom_price = is_custom_price
|
||||
item.save()
|
||||
if item_id:
|
||||
# Обновляем существующую позицию
|
||||
try:
|
||||
item = OrderItem.objects.get(id=item_id, order=order)
|
||||
item.product = product
|
||||
item.product_kit = product_kit
|
||||
item.quantity = quantity
|
||||
item.price = price
|
||||
item.is_custom_price = is_custom_price
|
||||
item.save()
|
||||
except OrderItem.DoesNotExist:
|
||||
# Если позиция не найдена, создаём новую
|
||||
OrderItem.objects.create(
|
||||
order=order,
|
||||
product=product,
|
||||
product_kit=product_kit,
|
||||
quantity=quantity,
|
||||
price=price,
|
||||
is_custom_price=is_custom_price
|
||||
)
|
||||
else:
|
||||
# Создаём новую
|
||||
# Создаём новую позицию
|
||||
OrderItem.objects.create(
|
||||
order=order,
|
||||
product=product,
|
||||
|
||||
Reference in New Issue
Block a user