feat(backend,waiter,manager): prices changed by hand are on the record
Owner 2026-11-23 (plan 6F.3). services/price_changes.py: the venue's switch orders.price_change (off | before_sending | anytime, default anytime as before), orders.price_change_reason and its quick reasons; keys ordering.prices.modify (the cart), new modify_sent (a sent or served line) and view_changes (who sees them). Every by-hand price (waiter cart, a sent line, the counter's % discount) writes a manual_price PriceEventLog row (before, after, quantity, who, approving manager, stage, reason) plus a PRICE_CHANGED audit row; before, the sent-line change overwrote the price with no record. Alembic 0047: event columns; modify_sent for everyone holding modify (and the venues' role overrides), view_changes for owners and store managers. Waiter: the reason picker when the venue asks, a sent line's price on modify_sent and the venue's switch, the line's price history. Manager: Settings -> Operation 'Prices changed by hand', the changes on the order's lines and history, Reports -> Cancellations, discounts and price changes (+ export). Counter: no discount when the venue turned price changes off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -584,20 +584,27 @@ def add_items(order_id: int, body: AddLinesRequest, request: Request, db: Sessio
|
||||
raise XeniaError("order.not_yours")
|
||||
lines, manual = _resolve_lines(db, body.lines, of_order(order))
|
||||
approval = None
|
||||
reasons = [None] * len(lines)
|
||||
if any(manual) or any(line.discount_pct for line in lines):
|
||||
from services import price_changes # the venue's switch and the reason first (6F.3)
|
||||
reasons = [price_changes.check(db, sent=False, reason=(raw or {}).get("price_reason") if isinstance(raw, dict) else None,
|
||||
by_hand=bool(hand)) if hand or line.discount_pct else None
|
||||
for raw, line, hand in zip(body.lines, lines, manual)]
|
||||
approval = _need(request, db, user, "ordering.prices.modify", target=f"order {order_id} new items")
|
||||
discounts = [0.0] * len(lines)
|
||||
if any(line.discount_pct for line in lines):
|
||||
priced = pos_cart.price_lines(db, lines, order.order_type or "here", pos_cart.order_cart(order), point=of_order(order))
|
||||
discounts = [pos_cart.discount_adjustment(unit, line.discount_pct) for line, (unit, _) in zip(lines, priced)]
|
||||
items = []
|
||||
for raw, line, hand, disc in zip(body.lines, lines, manual, discounts):
|
||||
for raw, line, hand, disc, reason in zip(body.lines, lines, manual, discounts, reasons):
|
||||
adj = round(hand + disc, 2)
|
||||
items.append(OrderItemInput(
|
||||
product_id=line.product.id, quantity=line.quantity,
|
||||
selected_options=[SelectedOptionInput(**e) for e in line.entries] or None,
|
||||
removed_ingredients=line.removed or None, notes=line.notes, price_adjustment=adj or None,
|
||||
is_service_item=raw.get("is_service_item") is True, course_id=raw.get("course_id"),
|
||||
price_reason=reason if adj else None,
|
||||
price_approved_by=approval.approved_by if approval is not None and adj else None,
|
||||
))
|
||||
result = add_priced_items(order_id, AddItemsRequest(items=items, order_note=body.order_note,
|
||||
customer_count=body.customer_count), db, user)
|
||||
@@ -708,6 +715,11 @@ def add_priced_items(order_id: int, body: AddItemsRequest, db: Session, user: Us
|
||||
db.add(item)
|
||||
db.flush()
|
||||
new_item_ids.append(item.id)
|
||||
if item.price_adjustment: # a price changed by hand in the cart (6F.3)
|
||||
from services import price_changes
|
||||
price_changes.record(db, item=item, before=unit_price, after=unit_price + item.price_adjustment,
|
||||
user_id=user.id, stage="cart", reason=item_in.price_reason,
|
||||
approved_by=item_in.price_approved_by)
|
||||
|
||||
# Stamp order_id on events and queue them — written after commit
|
||||
if price_events:
|
||||
@@ -1627,6 +1639,7 @@ def split_item(
|
||||
|
||||
class PriceAdjustRequest(BaseModel):
|
||||
price_adjustment: float # delta from unit_price (can be negative)
|
||||
reason: Optional[str] = Field(default=None, max_length=200) # 6F.3: needed when the venue asks for one
|
||||
|
||||
|
||||
@router.patch("/{order_id}/items/{item_id}/price-adjust", response_model=OrderItemOut)
|
||||
@@ -1638,8 +1651,10 @@ def adjust_item_price(
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Apply an on-the-fly price adjustment to an active order item. Without
|
||||
ordering.prices.modify a manager can approve it once (X-Xenia-Override, D-10)."""
|
||||
"""Change a sent line's price by hand (6F.3): the venue must allow it after sending
|
||||
(orders.price_change = anytime), it needs ordering.prices.modify_sent (a manager can approve it
|
||||
once, X-Xenia-Override, D-10) and, when the venue asks, a reason. Recorded as a manual_price
|
||||
event (before → after, who, the approver, the stage, the reason)."""
|
||||
item = db.query(OrderItem).filter(
|
||||
OrderItem.id == item_id, OrderItem.order_id == order_id
|
||||
).first()
|
||||
@@ -1651,15 +1666,37 @@ def adjust_item_price(
|
||||
order = db.query(Order).filter(Order.id == order_id).first()
|
||||
if not _can_access_order(order, user, db):
|
||||
raise XeniaError("order.not_yours")
|
||||
_need(request, db, user, "ordering.prices.modify", target=f"order:{order_id} item:{item_id}")
|
||||
from services import price_changes
|
||||
reason = price_changes.check(db, sent=True, reason=body.reason)
|
||||
approval = _need(request, db, user, "ordering.prices.modify_sent", target=f"order:{order_id} item:{item_id}")
|
||||
if not -10000 <= body.price_adjustment <= 10000 or item.unit_price + body.price_adjustment < 0:
|
||||
raise XeniaError("order.line.price", line=1, name=item.product_name)
|
||||
|
||||
item.price_adjustment = body.price_adjustment
|
||||
before = item.unit_price + (item.price_adjustment or 0.0)
|
||||
item.price_adjustment = round(body.price_adjustment, 2)
|
||||
price_changes.record(db, item=item, before=before, after=item.unit_price + item.price_adjustment, user_id=user.id,
|
||||
stage=price_changes.stage_of(item), reason=reason,
|
||||
approved_by=approval.approved_by if approval is not None else None)
|
||||
db.commit()
|
||||
db.refresh(item)
|
||||
broadcast_sync("order_updated", {"order_id": order_id, "table_id": order.table_id, "status": order.status, "action": "item_price_adjusted"})
|
||||
return item
|
||||
|
||||
|
||||
@router.get("/{order_id}/price-changes")
|
||||
def order_price_changes(order_id: int, db: Session = Depends(get_db),
|
||||
user: User = Depends(require("ordering.prices.view_changes"))):
|
||||
"""The prices changed by hand on an order (6F.3), oldest first, for the waiter app and the
|
||||
counter: [{id, item_id, product_name, before, after, delta, quantity, stage, reason, by, approved_by, at}]."""
|
||||
from models.pricing import PriceEventLog
|
||||
from services import price_changes
|
||||
order = db.get(Order, order_id)
|
||||
if order is None:
|
||||
raise XeniaError("order.not_found", order_id=order_id)
|
||||
return [price_changes.event_out(ev, db) for ev in db.query(PriceEventLog).filter(
|
||||
PriceEventLog.order_id == order_id, PriceEventLog.event_type == price_changes.EVENT).order_by(PriceEventLog.id)]
|
||||
|
||||
|
||||
# ─── Waiter-applied manual discount ─────────────────────────────────────────
|
||||
|
||||
from schemas.pricing import ApplyDiscountRequest, ApplyDiscountResponse
|
||||
|
||||
Reference in New Issue
Block a user