Просмотр исходного кода

Fix fetch-prices inflating base price on booked days via discount reversal

On booked days the platform price is the locked-in booking rate; reversing
the current local discount to recover a base inflated the stored price on
every fetch cycle. Now booked days are written with no discount attached.
Also scope the fetch-prices call in sync_reservations to booked_only=True,
matching the periodic price-fetch jobs and preventing open-day price imports
from the every-5-minute reservation sync.
ZiB 4 месяцев назад
Родитель
Сommit
c9c8720952
2 измененных файлов с 8 добавлено и 6 удалено
  1. 7 5
      room_cal/actions/fetch_prices.py
  2. 1 1
      room_cal/workflows/reservations.py

+ 7 - 5
room_cal/actions/fetch_prices.py

@@ -79,15 +79,17 @@ async def fetch_prices(
       continue
     d = date.fromisoformat(ds)
     discount_rec = discount_by_date.get(d)
-    # Reverse the local discount to recover the true base before calling CalPrice.set,
-    # which will re-apply the discount. Without this, each fetch→push cycle compounds
-    # the discount and ratchets the base price down.
+    # On booked days the platform price is the locked-in booking rate; the current
+    # discount is irrelevant, so store net as-is with no discount attached.
+    # On open days, reverse the local discount so CalPrice.set can re-apply it
+    # consistently without drifting the base on each fetch→push cycle.
     base = net
-    if discount_rec:
+    if discount_rec and not booked_svc:
       if discount_rec.type == DiscountType.FLAT:
         base = net + int(discount_rec.value)
       else:
         base = round(net / (1 - discount_rec.value / 100))
+    effective_discount = None if booked_svc else discount_rec
     existing = existing_by_date.get(d)
     if existing is None:
       changed = True
@@ -95,7 +97,7 @@ async def fetch_prices(
       xd = safe_json_loads(existing.x_data)
       if (xd or {}).get('net') != net:
         changed = True
-    await cal.set(d, base, existing=existing, discount_rec=discount_rec)
+    await cal.set(d, base, existing=existing, discount_rec=effective_discount)
     results.append({'date': d, 'net': net})
 
   return FetchResult(data=results, changed=changed)

+ 1 - 1
room_cal/workflows/reservations.py

@@ -33,7 +33,7 @@ async def sync_reservations(
   else:
     eff_start, eff_end = start_date, end_date
   await _push(push_closed(eff_start, eff_end, api=api), schedule)
-  await _push(fetch_prices(eff_start, eff_end, api=api), schedule)
+  await _push(fetch_prices(eff_start, eff_end, api=api, booked_only=True), schedule)
   await _push(local_sync(eff_start, eff_end), schedule)