feat(backend): UTC time model - venue zone, business date, aware-only datetimes
Platform v2 step 1.1 (APPS_PLATFORM D-11, §7, D-24). - core/timeutil.py: utcnow, venue_tz (venue.timezone, cached, invalidated on change - no restart), to_venue/venue_date/venue_strftime, parse_api_datetime (offset -> UTC; naive = venue-local; date = venue midnight), business_date_of /business_date_window (rollover hour, default 06:00), LocalClock + is_within, UTCDateTime column type (naive writes raise, reads are aware UTC), iso_z - every model column is UTCDateTime; Pydantic UTCDatetime emits "...Z" - business_days.business_date set at open; revision 0002 adds it (backfilled) and makes sync_events.created_at a UTCDateTime (AUTOINCREMENT restated) - tz.py deleted; system.timezone setting replaced by venue.timezone (validated) + business_day.rollover_hour; manager timezone picker writes venue.timezone - fixes: price-group schedules/time_range used the container's zone (happy hour bug); traffic report bucketed by UTC hour; reports' "date" meant the UTC calendar day, now the business date; reservations stored venue-local naive times (no-show check 2-3 h off) and listed by func.date; schedule week used UTC midnights; fiscal file names used container-local time - seed.py/demo_seed.py create store_manager with role permissions (regression from step 1.0: v1's startup ALTER list used to rename/grant role 'manager') - tzdata dependency; guard tests/guards/test_no_naive_time.py (AST scan); tests/test_timeutil.py covers the §7.3 DST cases and the API checks Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from core.timeutil import parse_api_datetime, venue_strftime, utcnow
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, BackgroundTasks
|
||||
from tz import local_strftime
|
||||
|
||||
|
||||
def _iso(dt) -> str | None:
|
||||
@@ -487,7 +487,7 @@ def add_items(
|
||||
pending_price_events = [] # (order_item_id, [PriceEvent]) — flushed after commit
|
||||
|
||||
# Build pricing context once for the whole batch
|
||||
now = datetime.now(timezone.utc)
|
||||
now = utcnow()
|
||||
active_pg_ids = build_active_price_group_ids(db, now)
|
||||
# Determine order channel
|
||||
_channel_map = {"online": "online", "takeaway": "takeaway", "delivery": "takeaway"}
|
||||
@@ -760,7 +760,7 @@ def cancel_item(
|
||||
if item.status == "paid":
|
||||
raise HTTPException(status_code=400, detail="Το αντικείμενο έχει ήδη πληρωθεί")
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
now = utcnow()
|
||||
item.status = "cancelled"
|
||||
item.cancelled_by = user.id
|
||||
item.cancelled_at = now
|
||||
@@ -817,7 +817,7 @@ def pay_items(order_id: int, body: PayItemsRequest, db: Session = Depends(get_db
|
||||
detail=f"Fiscal printing is ON but the following products have no VAT group assigned: {names}"
|
||||
)
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
now = utcnow()
|
||||
active_shift = db.query(WaiterShift).filter(
|
||||
WaiterShift.waiter_id == user.id,
|
||||
WaiterShift.ended_at == None,
|
||||
@@ -940,7 +940,7 @@ def close_order(order_id: int, db: Session = Depends(get_db), user: User = Depen
|
||||
if order.status not in ("paid", "open", "partially_paid"):
|
||||
raise HTTPException(status_code=400, detail="Cannot close order in current status")
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
now = utcnow()
|
||||
|
||||
# Mark all still-active items as 'closed' — unpaid, closed by manager
|
||||
active_items = db.query(OrderItem).filter(
|
||||
@@ -1009,9 +1009,9 @@ def pay_items_offline(
|
||||
|
||||
# Use the client-recorded offline timestamp as paid_at so audit reflects real payment time
|
||||
try:
|
||||
paid_at = datetime.fromisoformat(body.offline_at.replace("Z", "+00:00")) if body.offline_at else datetime.now(timezone.utc)
|
||||
except (ValueError, AttributeError):
|
||||
paid_at = datetime.now(timezone.utc)
|
||||
paid_at = parse_api_datetime(body.offline_at) or utcnow()
|
||||
except (ValueError, TypeError, AttributeError):
|
||||
paid_at = utcnow()
|
||||
|
||||
active_shift = db.query(WaiterShift).filter(
|
||||
WaiterShift.waiter_id == user.id,
|
||||
@@ -1085,7 +1085,7 @@ def cancel_order(
|
||||
if not is_manager and not _can_access_order(order, user, db):
|
||||
raise HTTPException(status_code=403, detail="Access denied")
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
now = utcnow()
|
||||
|
||||
active_items = db.query(OrderItem).filter(
|
||||
OrderItem.order_id == order_id,
|
||||
@@ -1142,7 +1142,7 @@ def cancel_items_bulk(
|
||||
OrderItem.order_id == order_id,
|
||||
).all()
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
now = utcnow()
|
||||
cancelled_ids = []
|
||||
skipped_paid = []
|
||||
for item in items:
|
||||
@@ -1273,8 +1273,8 @@ def print_order(
|
||||
"order_id": order.id,
|
||||
"table_name": table_name,
|
||||
"waiter_name": waiter_name,
|
||||
"opened_at": local_strftime(order.opened_at, "%d/%m/%Y %H:%M"),
|
||||
"closed_at": local_strftime(order.closed_at, "%d/%m/%Y %H:%M") if order.closed_at else None,
|
||||
"opened_at": venue_strftime(order.opened_at, "%d/%m/%Y %H:%M"),
|
||||
"closed_at": venue_strftime(order.closed_at, "%d/%m/%Y %H:%M") if order.closed_at else None,
|
||||
"status": order.status,
|
||||
"items": items_data,
|
||||
"total": grand_total,
|
||||
@@ -1385,7 +1385,7 @@ def merge_order(
|
||||
|
||||
# Cancel source order
|
||||
source.status = "cancelled"
|
||||
source.closed_at = datetime.now(timezone.utc)
|
||||
source.closed_at = utcnow()
|
||||
source.closed_by = user.id
|
||||
|
||||
_audit(db, source.id, "ORDER_CANCELLED", waiter_id=user.id,
|
||||
@@ -1676,7 +1676,7 @@ def cancel_print_job(
|
||||
|
||||
from datetime import datetime, timezone
|
||||
pj.status = "cancelled"
|
||||
pj.cancelled_at = datetime.now(timezone.utc)
|
||||
pj.cancelled_at = utcnow()
|
||||
pj.cancel_reason = "staff_cancelled"
|
||||
|
||||
# Mark affected items as printed so the UI warning goes away
|
||||
@@ -1834,7 +1834,7 @@ def print_synopsis(
|
||||
"order_id": order.id,
|
||||
"table_name": table_name,
|
||||
"waiter_name": waiter_name,
|
||||
"opened_at": local_strftime(order.opened_at, "%d/%m/%Y %H:%M"),
|
||||
"opened_at": venue_strftime(order.opened_at, "%d/%m/%Y %H:%M"),
|
||||
"items": items_data,
|
||||
"total": total,
|
||||
"paid_total": paid_total,
|
||||
|
||||
Reference in New Issue
Block a user