Procházet zdrojové kódy

Fix min_price dropping the same_day surcharge

min_price's final "value > 0" filter was meant to drop discounts
clamped away to nothing, but it also silently dropped same_day's
negative-value surcharge records, since min_price runs after
same_day in the this_month pipeline. Pass negative values through
untouched instead — the floor check doesn't apply to a surcharge.
ZiB před 12 hodinami
rodič
revize
32ed7a20bd

+ 7 - 0
room_cal/rules/discount/min_price.py

@@ -6,6 +6,9 @@ any discount whose resulting net price would fall below rules.min_price.min_net,
 using each date's current base price. Dates whose base price is already at or
 below the floor lose their discount entirely, since no discount can apply there
 without breaching the floor.
+
+Negative-value records (the same_day rule's surcharge hack) pass through untouched —
+they raise the net price, so the floor they're being checked against is irrelevant.
 """
 import dataclasses
 from datetime import date
@@ -27,6 +30,10 @@ async def run(start_date: date, end_date: date, data: DiscountList) -> DiscountL
 
   result: DiscountList = {}
   for d, rec in data.items():
+    if rec.value < 0:
+      result[d] = rec
+      continue
+
     base = prices[str(d)].base
     headroom = base - min_net
     if headroom <= 0:

+ 24 - 0
tests/test_min_price_rule.py

@@ -67,3 +67,27 @@ async def test_base_at_or_below_floor_drops_discount(monkeypatch):
   data = {d: DiscountData(type=DiscountType.PERCENT, value=10)}
   result = await run(d, d, data)
   assert d not in result
+
+
+@pytest.mark.asyncio
+async def test_negative_value_surcharge_passes_through_unclamped(monkeypatch):
+  # same_day's -15% surcharge hack must survive min_price untouched, even though
+  # it would otherwise be mistaken for a discount clamped away to nothing.
+  d = date(2026, 1, 10)
+  _stub_prices(monkeypatch, {d: 100})
+  _stub_config(monkeypatch, 52)
+  data = {d: DiscountData(type=DiscountType.PERCENT, value=-15)}
+  result = await run(d, d, data)
+  assert result[d].value == -15
+
+
+@pytest.mark.asyncio
+async def test_negative_value_surcharge_passes_through_below_floor(monkeypatch):
+  # Even when base is at/below the floor (which drops a normal discount entirely),
+  # a surcharge has nothing to do with the floor and must still pass through.
+  d = date(2026, 1, 10)
+  _stub_prices(monkeypatch, {d: 50})
+  _stub_config(monkeypatch, 52)
+  data = {d: DiscountData(type=DiscountType.PERCENT, value=-15)}
+  result = await run(d, d, data)
+  assert result[d].value == -15