Skip to content

Commit

Permalink
Fix CartManager.apply_voucher to handle all_bundles_included
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelm committed Jan 22, 2024
1 parent 6474240 commit 4cd2381
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/pretix/base/services/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1299,10 +1299,23 @@ def _perform_operations(self):

op.position.price_after_voucher = op.price_after_voucher
op.position.voucher = op.voucher
# op.posiiton.price will be set in recompute_final_prices_and_taxes
# op.position.price will be set in recompute_final_prices_and_taxes
op.position.save(update_fields=['price_after_voucher', 'voucher'])
vouchers_ok[op.voucher] -= 1

if op.voucher.all_bundles_included or op.voucher.all_addons_included:
for a in op.position.addons.all():
if a.is_bundled and op.voucher.all_bundles_included and a.price:
a.listed_price = Decimal("0.00")
a.price_after_voucher = Decimal("0.00")
# a.price will be set in recompute_final_prices_and_taxes
a.save(update_fields=['listed_price', 'price_after_voucher'])
elif not a.is_bundled and op.voucher.all_addons_included and a.price and not a.custom_price_input:
a.listed_price = Decimal("0.00")
a.price_after_voucher = Decimal("0.00")
# op.positon.price will be set in recompute_final_prices_and_taxes
a.save(update_fields=['listed_price', 'price_after_voucher'])

for p in new_cart_positions:
if getattr(p, '_answers', None):
if not p.pk: # We stored some to the database already before
Expand Down
56 changes: 56 additions & 0 deletions src/tests/presale/test_cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -3643,6 +3643,62 @@ def test_voucher_apply_multiple_reduce_beyond_designated_price(self):
assert cp.price == Decimal('0.00')
assert b.price == Decimal('1.50')

@classscope(attr='orga')
def test_voucher_apply_affect_bundled(self):
cp = CartPosition.objects.create(
event=self.event, cart_id=self.session_key, item=self.ticket,
price=21.5, expires=now() + timedelta(minutes=10)
)
a = CartPosition.objects.create(
event=self.event, cart_id=self.session_key, item=self.trans, addon_to=cp,
price=2.5, expires=now() + timedelta(minutes=10), is_bundled=False
)
b = CartPosition.objects.create(
event=self.event, cart_id=self.session_key, item=self.trans, addon_to=cp,
price=1.5, expires=now() + timedelta(minutes=10), is_bundled=True
)
v = Voucher.objects.create(
event=self.event, price_mode='set', value=Decimal('0.00'), max_usages=100,
all_bundles_included=True, all_addons_included=False, item=self.ticket,
)

self.cm.apply_voucher(v.code)
self.cm.commit()
cp.refresh_from_db()
b.refresh_from_db()
a.refresh_from_db()
assert cp.price == Decimal('0.00')
assert a.price == Decimal('2.50')
assert b.price == Decimal('0.00')

@classscope(attr='orga')
def test_voucher_apply_affect_addons(self):
cp = CartPosition.objects.create(
event=self.event, cart_id=self.session_key, item=self.ticket,
price=21.5, expires=now() + timedelta(minutes=10)
)
a = CartPosition.objects.create(
event=self.event, cart_id=self.session_key, item=self.trans, addon_to=cp,
price=1.5, expires=now() + timedelta(minutes=10), is_bundled=False
)
b = CartPosition.objects.create(
event=self.event, cart_id=self.session_key, item=self.trans, addon_to=cp,
price=1.5, expires=now() + timedelta(minutes=10), is_bundled=True
)
v = Voucher.objects.create(
event=self.event, price_mode='set', value=Decimal('0.00'), max_usages=100,
all_addons_included=True, all_bundles_included=False,
)

self.cm.apply_voucher(v.code)
self.cm.commit()
cp.refresh_from_db()
b.refresh_from_db()
a.refresh_from_db()
assert cp.price == Decimal('0.00')
assert a.price == Decimal('0.00')
assert b.price == Decimal('1.50')

@classscope(attr='orga')
def test_extend_base_price_changed(self):
cp = CartPosition.objects.create(
Expand Down

0 comments on commit 4cd2381

Please sign in to comment.