fix(pos): сохранение изменённой цены товара при создании витринного комплекта

При изменении цены товара в корзине через модалку, витринный комплект
теперь сохраняется с правильной ценой, а не с ценой из каталога:

- terminal.js: передача unit_price при создании витринного комплекта
- views.py: обработка и сохранение unit_price из корзины в KitItem
- kits.py: использование item.unit_price в ProductKit.save() при расчёте base_price
This commit is contained in:
2026-01-27 07:51:34 +03:00
parent 9a7c0728f0
commit 2dc397b3ce
3 changed files with 22 additions and 10 deletions

View File

@@ -2379,7 +2379,8 @@ document.getElementById('confirmCreateTempKit').onclick = async () => {
if (item.type === 'product') { if (item.type === 'product') {
items.push({ items.push({
product_id: item.id, product_id: item.id,
quantity: item.qty quantity: item.qty,
unit_price: item.price // Передаём изменённую цену из корзины
}); });
} }
}); });

View File

@@ -1163,15 +1163,20 @@ def create_temp_kit_to_showcase(request):
}, status=400) }, status=400)
# Агрегируем дубликаты (если один товар добавлен несколько раз) # Агрегируем дубликаты (если один товар добавлен несколько раз)
# Сохраняем также цену из корзины (unit_price)
aggregated_items = {} aggregated_items = {}
for item in items: for item in items:
product_id = item['product_id'] product_id = item['product_id']
quantity = Decimal(str(item['quantity'])) quantity = Decimal(str(item['quantity']))
unit_price = item.get('unit_price') # Цена из корзины (может быть изменена пользователем)
if product_id in aggregated_items: if product_id in aggregated_items:
aggregated_items[product_id] += quantity aggregated_items[product_id]['quantity'] += quantity
else: else:
aggregated_items[product_id] = quantity aggregated_items[product_id] = {
'quantity': quantity,
'unit_price': Decimal(str(unit_price)) if unit_price is not None else None
}
# Создаём временный комплект и резервируем на витрину # Создаём временный комплект и резервируем на витрину
with transaction.atomic(): with transaction.atomic():
@@ -1189,13 +1194,15 @@ def create_temp_kit_to_showcase(request):
) )
# 2. Создаём KitItem для каждого товара из корзины # 2. Создаём KitItem для каждого товара из корзины
for product_id, quantity in aggregated_items.items(): for product_id, item_data in aggregated_items.items():
product = products[product_id] product = products[product_id]
# Используем цену из корзины, если передана, иначе из каталога
final_price = item_data['unit_price'] if item_data['unit_price'] is not None else product.actual_price
KitItem.objects.create( KitItem.objects.create(
kit=kit, kit=kit,
product=product, product=product,
quantity=quantity, quantity=item_data['quantity'],
unit_price=product.actual_price # Фиксируем цену для временного комплекта unit_price=final_price # Фиксируем цену из корзины (с учётом изменений пользователя)
) )
# 3. Пересчитываем цену комплекта # 3. Пересчитываем цену комплекта
@@ -1264,7 +1271,7 @@ def create_temp_kit_to_showcase(request):
f' Название: {request.POST.get("kit_name")}\n' f' Название: {request.POST.get("kit_name")}\n'
f' Витрина ID: {request.POST.get("showcase_id")}\n' f' Витрина ID: {request.POST.get("showcase_id")}\n'
f' Товары: {request.POST.get("items")}\n' f' Товары: {request.POST.get("items")}\n'
f' Пользователь: {request.user.username}\n' f' Пользователь: {str(request.user)}\n'
f' Ошибка: {str(e)}', f' Ошибка: {str(e)}',
exc_info=True exc_info=True
) )

View File

@@ -230,6 +230,10 @@ class ProductKit(BaseProductEntity):
qty = item.quantity or Decimal('1') qty = item.quantity or Decimal('1')
total += actual_price * qty total += actual_price * qty
elif item.product: elif item.product:
# Используем зафиксированную цену (unit_price) если задана, иначе актуальную цену товара
if item.unit_price is not None:
actual_price = item.unit_price
else:
actual_price = item.product.actual_price or Decimal('0') actual_price = item.product.actual_price or Decimal('0')
qty = item.quantity or Decimal('1') qty = item.quantity or Decimal('1')
total += actual_price * qty total += actual_price * qty