Allow to round taxes on order-level (#5019)

* Allow to round taxes on order-level

* Rename get_cart_total

* Persist rounding mode with order

* Add general docs

* Order creation API

* Update fee algorithm

* Rounding on payment method change

* Round when splitting order

* Fix failing tests

* Add settings page

* Add tests

* Replace algorithm

* Add test case for currency rounding

* Improve order change

* Update flowchart

* Update discount logic (more hypothetical, we don't store rounding on cart positions atm)

* Rename internal method

* Fix typo

* Update help text

* Apply suggestions from code review

Co-authored-by: luelista <weller@rami.io>

* Order rounding refactor (#5571)

* Add RoundingCorrectionMixin providing before-rounding-values as properties

* Use gross_price_before_rounding in more places

* Update doc/development/algorithms/pricing.rst

Co-authored-by: Martin Gross <gross@rami.io>

* Allow to override on perform_order

* Rebase migration

* Fix event cancellation

---------

Co-authored-by: luelista <weller@rami.io>
Co-authored-by: Martin Gross <gross@rami.io>
This commit is contained in:
Raphael Michel
2025-10-30 11:49:31 +01:00
committed by GitHub
parent cdeb1e86bd
commit 3e972eddbf
37 changed files with 1923 additions and 319 deletions

View File

@@ -91,9 +91,7 @@ from pretix.presale.signals import (
question_form_fields_overrides,
)
from pretix.presale.utils import customer_login
from pretix.presale.views import (
CartMixin, get_cart, get_cart_is_free, get_cart_total,
)
from pretix.presale.views import CartMixin, get_cart, get_cart_is_free
from pretix.presale.views.cart import (
_items_from_post_data, cart_session, create_empty_cart_id,
get_or_create_cart_id,
@@ -1262,18 +1260,16 @@ class PaymentStep(CartMixin, TemplateFlowStep):
@cached_property
def _total_order_value(self):
cart = get_cart(self.request)
total = get_cart_total(self.request)
try:
total += sum([
f.value for f in get_fees(
self.request.event, self.request, total, self.invoice_address,
[p for p in self.cart_session.get('payments', []) if p.get('multi_use_supported')],
cart,
)
])
fees = get_fees(
event=self.request.event, request=self.request, invoice_address=self.invoice_address,
payments=[p for p in self.cart_session.get('payments', []) if p.get('multi_use_supported')],
positions=cart,
)
except TaxRule.SaleNotAllowed:
# ignore for now, will fail on order creation
pass
fees = []
total = sum([c.price for c in cart]) + sum([f.value for f in fees])
return Decimal(total)
@cached_property
@@ -1399,7 +1395,13 @@ class PaymentStep(CartMixin, TemplateFlowStep):
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['current_payments'] = [p for p in self.current_selected_payments(self._total_order_value) if p.get('multi_use_supported')]
ctx['cart'] = self.get_cart()
ctx['current_payments'] = [
p for p in self.current_selected_payments(
ctx['cart']['raw'], ctx['cart']['fees'], ctx['cart']['invoice_address'],
)
if p.get('multi_use_supported')
]
ctx['remaining'] = self._total_order_value - sum(p['payment_amount'] for p in ctx['current_payments']) + sum(p['fee'] for p in ctx['current_payments'])
ctx['providers'] = self.provider_forms
ctx['show_fees'] = any(p['fee'] for p in self.provider_forms)
@@ -1412,7 +1414,6 @@ class PaymentStep(CartMixin, TemplateFlowStep):
ctx['selected'] = self.single_use_payment['provider']
else:
ctx['selected'] = ''
ctx['cart'] = self.get_cart()
return ctx
def _is_allowed(self, prov, request):
@@ -1425,14 +1426,20 @@ class PaymentStep(CartMixin, TemplateFlowStep):
return False
cart = get_cart(self.request)
total = get_cart_total(self.request)
try:
total += sum([f.value for f in get_fees(self.request.event, self.request, total, self.invoice_address,
self.cart_session.get('payments', []), cart)])
fees = get_fees(
event=self.request.event,
request=self.request,
invoice_address=self.invoice_address,
payments=self.cart_session.get('payments', []),
positions=cart
)
except TaxRule.SaleNotAllowed:
# ignore for now, will fail on order creation
pass
selected = self.current_selected_payments(total, warn=warn, total_includes_payment_fees=True)
fees = []
total = sum([c.price for c in cart]) + sum([f.value for f in fees])
selected = self.current_selected_payments(cart, fees, self.invoice_address, warn=warn)
if sum(p['payment_amount'] for p in selected) != total:
if warn:
messages.error(request, _('Please select a payment method to proceed.'))
@@ -1516,7 +1523,11 @@ class ConfirmStep(CartMixin, AsyncAction, TemplateFlowStep):
ctx = super().get_context_data(**kwargs)
ctx['cart'] = self.get_cart(answers=True)
selected_payments = self.current_selected_payments(ctx['cart']['total'], total_includes_payment_fees=True)
selected_payments = self.current_selected_payments(
ctx['cart']['raw'],
ctx['cart']['fees'],
ctx['cart']['invoice_address'],
)
ctx['payments'] = []
for p in selected_payments:
if p['provider'] == 'free':