fix(inventory): учитывать скидки на позицию и заказ при расчёте цены продажи

Расширяем логику расчёта цены продажи для учёта как скидок на отдельные позиции,
так и скидок на весь заказ. Скидка на заказ распределяется пропорционально доле
каждой позиции в общей сумме заказа.

Изменения внесены в SaleProcessor и сигнал создания продажи при завершении заказа.
This commit is contained in:
2026-01-20 23:40:27 +03:00
parent 0938878e67
commit 6b327fa7e0
2 changed files with 38 additions and 8 deletions

View File

@@ -480,10 +480,25 @@ def create_sale_on_order_completion(sender, instance, created, **kwargs):
f"Используем quantity_in_base_units: {sale_quantity}"
)
# Цена за единицу с учётом скидки
if item.discount_amount and item.quantity > 0:
subtotal = Decimal(str(item.price)) * Decimal(str(item.quantity))
price_with_discount = (subtotal - Decimal(str(item.discount_amount))) / Decimal(str(item.quantity))
# Цена за единицу с учётом всех скидок (позиция + заказ)
item_subtotal = Decimal(str(item.price)) * Decimal(str(item.quantity))
# Скидка на позицию
item_discount = Decimal(str(item.discount_amount)) if item.discount_amount else Decimal('0')
# Скидка на заказ (распределяется пропорционально доле позиции в заказе)
# subtotal - сумма позиций С учётом скидок на позиции (без скидки на заказ)
order_total = instance.subtotal if hasattr(instance, 'subtotal') else Decimal('0')
if order_total > 0 and instance.discount_amount:
order_discount = Decimal(str(instance.discount_amount))
# Пропорциональная часть скидки заказа для этой позиции
item_order_discount = order_discount * (item_subtotal / order_total)
else:
item_order_discount = Decimal('0')
total_discount = item_discount + item_order_discount
if total_discount and item.quantity > 0:
price_with_discount = (item_subtotal - total_discount) / Decimal(str(item.quantity))
else:
price_with_discount = Decimal(str(item.price))