Przeglądaj źródła

Replace same-day discount clearing with a -15% surcharge hack

The same_day rule used to strip any discount on same-day/near-same-day
dates. Until real surcharge support lands, instead force a -15%
"discount" on those dates, which price_calendar's net calculation
turns into an actual 15% price increase.
ZiB 12 godzin temu
rodzic
commit
5f21697295

+ 20 - 10
room_cal/rules/discount/same_day.py

@@ -1,17 +1,25 @@
-"""Strip same-day (and near-same-day) discounts from the accumulated discount list.
-
-Transformer rule: removes any date that falls in the current same-day window — see
-`_same_day_dates` — from the accumulated DiscountList, regardless of which earlier rule
-in the pipeline generated it. Centralizes the same-day exclusion so individual generator
-rules (short_notice, fill_month, etc.) don't each need to reimplement this time-of-day logic.
+"""Force a same-day surcharge over any discount in the accumulated discount list.
+
+Transformer rule: for every date that falls in the current same-day window — see
+`_same_day_dates` — overrides whatever discount an earlier rule in the pipeline produced
+(or lack thereof) with a flat -15% "discount" (i.e. a 15% surcharge, since price_calendar's
+`net = base - discount_value * base / 100` makes a negative percent raise the price).
+Centralizes the same-day handling so individual generator rules (short_notice, fill_month,
+etc.) don't each need to reimplement this time-of-day logic.
+
+HACK: this is a stand-in for a real surcharge mechanism (tracked in the discount-framework
+plan) — DiscountData has no dedicated surcharge type, so a negative PERCENT value is reused
+here to get the pricing effect without one.
 """
 from datetime import date, datetime, timedelta
 
 from room_cal._utils import get_config, local_now
-from room_cal.discount import DiscountList
+from room_cal.discount import DiscountData, DiscountList, DiscountType
 
 RULE_NAME = 'same_day'
 
+SURCHARGE_PERCENT = -15
+
 
 def _same_day_dates(now: datetime) -> set[date]:
   """Dates too close to check-in to be eligible for any rule-based discount.
@@ -34,7 +42,9 @@ def _same_day_dates(now: datetime) -> set[date]:
 
 
 async def run(start_date: date, end_date: date, data: DiscountList) -> DiscountList:
-  if not data:
-    return {}
   same_day = _same_day_dates(local_now())
-  return {d: rec for d, rec in data.items() if d not in same_day}
+  result = dict(data)
+  for d in same_day:
+    if start_date <= d <= end_date:
+      result[d] = DiscountData(type=DiscountType.PERCENT, value=SURCHARGE_PERCENT)
+  return result

+ 1 - 1
room_cal/rules/discount/short_notice.py

@@ -5,7 +5,7 @@ Discount tiers for each qualifying open date:
   30% — day before is open (day after is not)
   20% — day after is open (day before is not)
 
-Same-day exclusion lives in the separate `same_day` rule, not here — see that module.
+Same-day handling (surcharge override) lives in the separate `same_day` rule, not here — see that module.
 """
 from datetime import date, timedelta
 

+ 29 - 11
tests/test_same_day_rule.py

@@ -4,7 +4,7 @@ from types import SimpleNamespace
 from zoneinfo import ZoneInfo
 
 from room_cal.discount import DiscountData, DiscountType
-from room_cal.rules.discount.same_day import RULE_NAME, _same_day_dates, run
+from room_cal.rules.discount.same_day import RULE_NAME, SURCHARGE_PERCENT, _same_day_dates, run
 
 _TZ = ZoneInfo('America/Los_Angeles')
 
@@ -49,11 +49,18 @@ class TestSameDayDates:
 class TestRun:
 
   @pytest.mark.asyncio
-  async def test_empty_input_returns_empty(self, monkeypatch):
-    assert await run(date(2026, 1, 1), date(2026, 1, 1), {}) == {}
+  async def test_empty_input_adds_surcharge_dates(self, monkeypatch):
+    today = date(2026, 1, 1)
+    now = datetime(2026, 1, 1, 10, 0, tzinfo=_TZ)  # before checkin_hour -> same_day = {today}
+    _stub_config(monkeypatch)
+    _stub_now(monkeypatch, now)
+
+    result = await run(today, today, {})
+
+    assert result == {today: DiscountData(type=DiscountType.PERCENT, value=SURCHARGE_PERCENT)}
 
   @pytest.mark.asyncio
-  async def test_strips_same_day_dates_morning(self, monkeypatch):
+  async def test_overrides_same_day_dates_morning(self, monkeypatch):
     today = date(2026, 7, 10)
     now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ)  # before checkin_hour -> same_day = {today}
     _stub_config(monkeypatch)
@@ -66,12 +73,12 @@ class TestRun:
 
     result = await run(today, today + timedelta(days=2), data)
 
-    assert today not in result
-    assert today + timedelta(days=1) in result  # tomorrow is NOT same-day in the morning window
-    assert today + timedelta(days=2) in result
+    assert result[today] == DiscountData(type=DiscountType.PERCENT, value=SURCHARGE_PERCENT)
+    assert result[today + timedelta(days=1)].value == 30  # tomorrow is NOT same-day in the morning window
+    assert result[today + timedelta(days=2)].value == 20
 
   @pytest.mark.asyncio
-  async def test_strips_same_day_dates_afternoon(self, monkeypatch):
+  async def test_overrides_same_day_dates_afternoon(self, monkeypatch):
     today = date(2026, 7, 10)
     now = datetime(2026, 7, 10, 16, 0, tzinfo=_TZ)  # after checkin_hour -> same_day = {today, +1}
     _stub_config(monkeypatch)
@@ -84,11 +91,22 @@ class TestRun:
 
     result = await run(today, today + timedelta(days=2), data)
 
-    assert today not in result
-    assert today + timedelta(days=1) not in result
-    assert today + timedelta(days=2) in result
+    assert result[today] == DiscountData(type=DiscountType.PERCENT, value=SURCHARGE_PERCENT)
+    assert result[today + timedelta(days=1)] == DiscountData(type=DiscountType.PERCENT, value=SURCHARGE_PERCENT)
     assert result[today + timedelta(days=2)].value == 20
 
+  @pytest.mark.asyncio
+  async def test_same_day_outside_range_not_added(self, monkeypatch):
+    # start_date/end_date excludes today -> the surcharge must not be written outside the window
+    today = date(2026, 7, 10)
+    now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ)
+    _stub_config(monkeypatch)
+    _stub_now(monkeypatch, now)
+
+    result = await run(today + timedelta(days=1), today + timedelta(days=5), {})
+
+    assert today not in result
+
   @pytest.mark.asyncio
   async def test_untouched_dates_pass_through_unchanged(self, monkeypatch):
     today = date(2026, 7, 10)