fix: Улучшения системы ценообразования комплектов
Исправлены 4 проблемы: 1. Расчёт цены первого товара - улучшена валидация в getProductPrice и calculateFinalPrice 2. Отображение actual_price в Select2 вместо обычной цены 3. Количество по умолчанию = 1 для новых форм компонентов 4. Auto-select текста при клике на поле количества для удобства редактирования Изменённые файлы: - products/forms.py: добавлен __init__ в KitItemForm для quantity.initial = 1 - products/templates/includes/select2-product-init.html: обновлена formatSelectResult - products/templates/productkit_create.html: добавлен focus handler для auto-select - products/templates/productkit_edit.html: добавлен focus handler для auto-select 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -108,10 +108,13 @@ class ProductKitCreateView(LoginRequiredMixin, PermissionRequiredMixin, CreateVi
|
||||
text = product.name
|
||||
if product.sku:
|
||||
text += f" ({product.sku})"
|
||||
# Получаем actual_price: приоритет sale_price > price
|
||||
actual_price = product.sale_price if product.sale_price else product.price
|
||||
selected_products[key] = {
|
||||
'id': product.id,
|
||||
'text': text,
|
||||
'price': str(product.sale_price) if product.sale_price else None
|
||||
'price': str(product.price) if product.price else None,
|
||||
'actual_price': str(actual_price) if actual_price else '0'
|
||||
}
|
||||
except Product.DoesNotExist:
|
||||
pass
|
||||
@@ -137,7 +140,7 @@ class ProductKitCreateView(LoginRequiredMixin, PermissionRequiredMixin, CreateVi
|
||||
# Получаем формсет из POST с правильным префиксом
|
||||
kititem_formset = KitItemFormSetCreate(self.request.POST, prefix='kititem')
|
||||
|
||||
# Проверяем валидность основной формы и формсета
|
||||
# Проверяем валидность основной формы
|
||||
if not form.is_valid():
|
||||
messages.error(self.request, 'Пожалуйста, исправьте ошибки в основной форме комплекта.')
|
||||
return self.form_invalid(form)
|
||||
@@ -150,7 +153,7 @@ class ProductKitCreateView(LoginRequiredMixin, PermissionRequiredMixin, CreateVi
|
||||
try:
|
||||
with transaction.atomic():
|
||||
# Сохраняем основную форму (комплект)
|
||||
self.object = form.save(commit=True) # Явно сохраняем в БД
|
||||
self.object = form.save(commit=True)
|
||||
|
||||
# Убеждаемся что объект в БД
|
||||
if not self.object.pk:
|
||||
@@ -160,6 +163,15 @@ class ProductKitCreateView(LoginRequiredMixin, PermissionRequiredMixin, CreateVi
|
||||
kititem_formset.instance = self.object
|
||||
saved_items = kititem_formset.save()
|
||||
|
||||
# ТЕПЕРЬ (после сохранения комплекта) проверяем валидность ценообразования
|
||||
from ..validators.kit_validators import KitValidator
|
||||
is_method_valid, pricing_warning = KitValidator.validate_pricing_method_availability(self.object)
|
||||
|
||||
if not is_method_valid and pricing_warning:
|
||||
# Метод был переключен - сохраняем изменения
|
||||
self.object.save()
|
||||
messages.warning(self.request, pricing_warning)
|
||||
|
||||
# Обработка фотографий
|
||||
handle_photos(self.request, self.object, ProductKitPhoto, 'kit')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user