feat(backend,manager): the business day opens and closes by itself (opening hours, a time, or only an alert)
- services/day_auto.py: a minute loop (venue time, offline) opens the day when the opening
hours start (or at business_day.auto_open_time) and closes it when they end (or at
auto_close_time); 'alert' only tells the managers. Offsets before / after, one day across
the break or one per span, spans past midnight belong to the day they start, a day closed
by hand isn't reopened in the same span, a restart catches up the open (until the business
date changes for 'time') and alerts a missed close instead of doing it late.
- The close runs only when no order of the day is unclosed (Q-10): otherwise a sticky alert
and a new try every 15 minutes. A locked / expired license turns the open into an alert.
- Sticky notifications: notifications.resolved_at + core.notifications.resolve(); the
alerts resolve when the day opens / closes (by hand or by itself).
- services/business_days.py: the one open / close path (the routes and the loop); days opened
or closed by the system have no opener / closer and opened_auto / closed_auto (Alembic 0052).
- Manager: Settings -> Operation "Opening and closing the day"; the bell's action buttons
("Open now", "Go to the overview"); Reports -> Days marks days opened / closed by itself.
Plan platform-v2 6F.8.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
"""Opening and closing the business day: the one way, used by the manager's buttons
|
||||
(routers/business_day.py) and by the automatic open / close (services/day_auto.py, plan 6F.8).
|
||||
|
||||
- open_day(db, user_id=None, auto=True): a day opened by the system has no opener
|
||||
(opened_by_id NULL) and opened_auto set. The license gates apply to both.
|
||||
- close_day(db, user_id=None, auto=True): the same close as a manager's: unclosed orders are
|
||||
closed (the automatic close only runs when there are none, Q-10), open shifts end with
|
||||
their totals, flags and staff messages are cleared, a lock the cloud asked for while the
|
||||
day was open takes effect, the license is re-evaluated.
|
||||
- unclosed_orders(db, day): what "a table is still open" means for the automatic close: every
|
||||
order of the day that isn't closed or cancelled (paid but not closed counts too).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from core.errors import XeniaError
|
||||
from core.timeutil import business_date_of, utcnow
|
||||
|
||||
|
||||
def current(db):
|
||||
from models.business_day import BusinessDay
|
||||
return db.query(BusinessDay).filter(BusinessDay.status == "open").first()
|
||||
|
||||
|
||||
def license_blocked() -> str | None:
|
||||
"""The error code that stops a day from opening now, or None."""
|
||||
from services.license_state import license_state
|
||||
if license_state.get("locked") or license_state.get("lock_pending"):
|
||||
return "license.locked"
|
||||
if not license_state.get("licensed", True):
|
||||
return "license.expired"
|
||||
return None
|
||||
|
||||
|
||||
def open_day(db, *, user_id: int | None = None, notes: str | None = None, auto: bool = False, at=None):
|
||||
from models.business_day import BusinessDay
|
||||
if current(db) is not None:
|
||||
raise XeniaError("business_day.already_open")
|
||||
blocked = license_blocked()
|
||||
if blocked:
|
||||
raise XeniaError(blocked)
|
||||
opened_at = at or utcnow() # `at`: the automatic open's clock (tests drive it)
|
||||
day = BusinessDay(opened_by_id=user_id, notes=notes, opened_at=opened_at,
|
||||
business_date=business_date_of(opened_at), opened_auto=auto)
|
||||
db.add(day)
|
||||
db.commit()
|
||||
db.refresh(day)
|
||||
from services import day_auto
|
||||
day_auto.resolve_alerts(db)
|
||||
return day
|
||||
|
||||
|
||||
def unclosed_orders(db, day):
|
||||
from models.order import Order
|
||||
return db.query(Order).filter(Order.business_day_id == day.id,
|
||||
Order.status.in_(["open", "partially_paid", "paid"])).all()
|
||||
|
||||
|
||||
def close_day(db, *, user_id: int | None = None, force: bool = False, notes: str | None = None,
|
||||
store_closing_cash: float | None = None, auto: bool = False, at=None):
|
||||
from models.business_day import BusinessDay # noqa: F401
|
||||
from models.flag import TableFlagAssignment
|
||||
from models.message import StaffMessage, StaffMessageAck
|
||||
from models.order import Order, OrderAuditLog, OrderItem
|
||||
from models.shift import WaiterShift
|
||||
from services.license_state import license_state
|
||||
|
||||
day = current(db)
|
||||
if not day:
|
||||
raise XeniaError("business_day.none_open")
|
||||
|
||||
open_orders = db.query(Order).filter(
|
||||
Order.business_day_id == day.id,
|
||||
Order.status.in_(["open", "partially_paid"]),
|
||||
).all()
|
||||
if open_orders and not force:
|
||||
# Count orders that have at least one active (unpaid) item — covers both
|
||||
# "open" (fully unpaid) and "partially_paid" (partially unpaid) orders.
|
||||
with_pending = sum(
|
||||
1 for o in open_orders
|
||||
if db.query(OrderItem).filter(OrderItem.order_id == o.id, OrderItem.status == "active").first() is not None
|
||||
)
|
||||
raise XeniaError("business_day.orders_open", open_orders=len(open_orders), partially_paid=with_pending)
|
||||
|
||||
now = at or utcnow()
|
||||
# Close all non-terminal orders for this business day (open, partially_paid, paid)
|
||||
for order in unclosed_orders(db, day):
|
||||
was_unpaid = order.status in ("open", "partially_paid")
|
||||
order.status = "closed"
|
||||
order.closed_at = now
|
||||
order.closed_by = user_id
|
||||
if was_unpaid:
|
||||
db.add(OrderAuditLog(order_id=order.id, event_type="ORDER_CLOSED", waiter_id=user_id,
|
||||
note="Force-closed at end of business day"))
|
||||
|
||||
from routers.shifts import compute_shift_total
|
||||
for shift in db.query(WaiterShift).filter(WaiterShift.business_day_id == day.id, WaiterShift.ended_at == None).all(): # noqa: E711
|
||||
shift.total_collected = compute_shift_total(shift.id, db)
|
||||
shift.ended_at = now
|
||||
|
||||
# Clear all table flags and staff messages — fresh slate for the next day
|
||||
db.query(TableFlagAssignment).delete(synchronize_session=False)
|
||||
db.query(StaffMessageAck).delete(synchronize_session=False)
|
||||
db.query(StaffMessage).delete(synchronize_session=False)
|
||||
|
||||
day.status = "closed"
|
||||
day.closed_at = now
|
||||
day.closed_by_id = user_id
|
||||
day.closed_auto = auto
|
||||
if notes:
|
||||
day.notes = notes
|
||||
|
||||
# Phase 2E: store-level cash reconciliation
|
||||
if store_closing_cash is not None:
|
||||
day.store_closing_cash = store_closing_cash
|
||||
all_shifts = db.query(WaiterShift).filter(WaiterShift.business_day_id == day.id).all()
|
||||
expected_closing = (day.store_opening_cash or 0.0) + sum(s.total_collected or 0.0 for s in all_shifts)
|
||||
day.store_cash_discrepancy = round(store_closing_cash - expected_closing, 2)
|
||||
|
||||
db.commit()
|
||||
db.refresh(day)
|
||||
|
||||
# Deferred lock: if cloud requested a lock while the workday was open, enforce it now.
|
||||
if license_state.get("lock_pending"):
|
||||
license_state["lock_pending"] = False
|
||||
license_state["locked"] = True
|
||||
# Re-evaluate the license now that no workday is open (an expiry whose grace
|
||||
# ended mid-service takes effect here) — also persists the state
|
||||
from services.cloud_sync import apply_license
|
||||
apply_license()
|
||||
from services import day_auto
|
||||
day_auto.resolve_alerts(db)
|
||||
return day
|
||||
Reference in New Issue
Block a user