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

- Пересчитывать цену в базовые единицы: price * conversion_factor
- Вычислять скидку как разницу между subtotal и total_amount
- Распределять скидку пропорционально долям позиций
- Использовать refresh_from_db() для актуального total_amount

Пример: 20 ед. (коэфф. 5) по 7₽ со скидкой 10% → Sale: 4 шт. по 31.5₽

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-21 00:35:43 +03:00
parent 6b327fa7e0
commit 1071f3cacc
2 changed files with 27 additions and 24 deletions

View File

@@ -43,16 +43,17 @@ class SaleProcessor:
item_discount = Decimal(str(item.discount_amount)) if item.discount_amount else Decimal('0')
# Скидка на заказ (распределяется пропорционально доле позиции в заказе)
# subtotal - сумма позиций С учётом скидок на позиции (без скидки на заказ)
# Вычисляем как разницу между subtotal и total_amount (так как discount_amount может быть 0)
order_total = order.subtotal if hasattr(order, 'subtotal') else Decimal('0')
if order_total > 0 and order.discount_amount:
order_discount = Decimal(str(order.discount_amount))
item_order_discount = order_discount * (item_subtotal / order_total)
else:
item_order_discount = Decimal('0')
# Скидка = subtotal - (total_amount - delivery) (вычитаем доставку, если есть)
delivery_cost = Decimal(str(order.delivery.cost)) if hasattr(order, 'delivery') and order.delivery else Decimal('0')
order_discount = (order_total - (Decimal(str(order.total_amount)) - delivery_cost)) if order_total > 0 else Decimal('0')
total_discount = item_discount + item_order_discount
total_discount = item_discount + order_discount
if total_discount and item.quantity > 0:
# Распределяем общую скидку пропорционально доле позиции
item_order_discount = order_discount * (item_subtotal / order_total) if order_total > 0 else Decimal('0')
total_discount = item_discount + item_order_discount
price_with_discount = (item_subtotal - total_discount) / Decimal(str(item.quantity))
else:
price_with_discount = Decimal(str(item.price))