feat(backend,waiter,terminal,manager): +1 on a sent line and changing a line's quantity
Owner 2026-11-23 (plan 6F.5). Product flag one_more_enabled (off by default; product editor).
POST /api/orders/{id}/items/{item}/again: a new line like the old one (choices, removed, note,
course), priced now, sent to the kitchen like any new line. Waiter app and the counter's table
panel: +1 beside such lines; the first tap arms it, more taps count up, 'Send +N' sends, 4 s idle
disarms (no stray sends).
POST /api/orders/{id}/items/{item}/quantity: venue setting orders.quantity_change (off | add_only |
add_and_remove, default off), key ordering.items.change_quantity (owner, store manager; Alembic 0049
grants it); up only changes the line, down cuts a cancelled part ('quantity lowered', split_from_id)
and prints a cancellation only when asked (the dialog presets yes while the kitchen hasn't finished).
Waiter: 'Change quantity' in a line's actions (now -> new, -/+). cut_part() now shared with split.
GET /api/orders/{id} items carry the product's one_more_enabled / unit_type, deal ids, split_from_id,
cancel_reason.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+136
-21
@@ -343,7 +343,8 @@ def get_order(order_id: int, db: Session = Depends(get_db), user: User = Depends
|
||||
"id": i.id,
|
||||
"order_id": i.order_id,
|
||||
"product_id": i.product_id,
|
||||
"product": {"id": i.product.id, "name": i.product.name} if i.product else None,
|
||||
"product": {"id": i.product.id, "name": i.product.name, "unit_type": i.product.unit_type or "piece",
|
||||
"one_more_enabled": bool(i.product.one_more_enabled)} if i.product else None, # 6F.5: +1
|
||||
"product_name": i.product_name, "category_id": i.category_id, "category_name": i.category_name,
|
||||
"added_by": i.added_by,
|
||||
"added_by_name": name_map.get(i.added_by),
|
||||
@@ -366,6 +367,9 @@ def get_order(order_id: int, db: Session = Depends(get_db), user: User = Depends
|
||||
"system_key": i.system_key, # the delivery fee line (phase 3S, S.5)
|
||||
"source": i.source, # waiter | counter | qr | online (4.8)
|
||||
"course_id": i.course_id,
|
||||
"deal_id": i.deal_id, "linked_item_id": i.linked_item_id,
|
||||
"split_from_id": i.split_from_id, # 6F.4: the line it was cut from
|
||||
"cancel_reason": i.cancel_reason,
|
||||
}
|
||||
|
||||
def fmt_log(l):
|
||||
@@ -1618,26 +1622,7 @@ def split_item(
|
||||
|
||||
# Reduce original item
|
||||
item.quantity -= body.quantity
|
||||
|
||||
# Create split-off item
|
||||
new_item = OrderItem(
|
||||
source=item.source,
|
||||
order_id=order_id,
|
||||
product_id=item.product_id,
|
||||
added_by=item.added_by,
|
||||
quantity=body.quantity,
|
||||
unit_price=item.unit_price,
|
||||
selected_options=item.selected_options,
|
||||
removed_ingredients=item.removed_ingredients,
|
||||
notes=item.notes,
|
||||
status="active",
|
||||
printed=item.printed,
|
||||
split_from_id=item.id, # 6F.4: the moves the line had come with this part
|
||||
# the rest of what the line was (kitchen, course, price, names, cost) goes with the part
|
||||
kds_status=item.kds_status, course_id=item.course_id, price_adjustment=item.price_adjustment,
|
||||
unit_cost=item.unit_cost, product_name=item.product_name, category_id=item.category_id,
|
||||
category_name=item.category_name,
|
||||
)
|
||||
new_item = cut_part(item, body.quantity)
|
||||
db.add(new_item)
|
||||
db.commit()
|
||||
db.refresh(item)
|
||||
@@ -1645,6 +1630,136 @@ def split_item(
|
||||
return [item, new_item]
|
||||
|
||||
|
||||
def cut_part(item, quantity, **changes):
|
||||
"""A new line cut from `item` (a split, a lowered quantity, 6F.4/6F.5): everything the line was
|
||||
(product, price and its change, choices, note, kitchen state, course, cost, names, source) for
|
||||
`quantity`, linked by split_from_id so its moves follow it. Not added to the session."""
|
||||
fields = dict(
|
||||
source=item.source, order_id=item.order_id, product_id=item.product_id, added_by=item.added_by,
|
||||
quantity=quantity, unit_price=item.unit_price, selected_options=item.selected_options,
|
||||
removed_ingredients=item.removed_ingredients, notes=item.notes, status="active", printed=item.printed,
|
||||
split_from_id=item.id, kds_status=item.kds_status, course_id=item.course_id,
|
||||
price_adjustment=item.price_adjustment, unit_cost=item.unit_cost, product_name=item.product_name,
|
||||
category_id=item.category_id, category_name=item.category_name,
|
||||
) # added_at = now: the moment it was cut (services/moves.py reads it)
|
||||
fields.update(changes)
|
||||
return OrderItem(**fields)
|
||||
|
||||
|
||||
# ─── One more of a line, and a line's quantity (6F.5) ────────────────────────
|
||||
|
||||
class AgainRequest(BaseModel):
|
||||
quantity: int = Field(default=1, ge=1, le=50)
|
||||
|
||||
|
||||
@router.post("/{order_id}/items/{item_id}/again", response_model=AddItemsResponse)
|
||||
def order_again(order_id: int, item_id: int, body: AgainRequest, request: Request, db: Session = Depends(get_db),
|
||||
user: User = Depends(get_current_user)):
|
||||
""""One more" (6F.5): a new line just like this one (product, choices, removed ingredients, note,
|
||||
course) for `quantity`, priced by the server now and sent like any new line (kitchen ticket,
|
||||
KDS). Only for products with one_more_enabled, counted in pieces, from an active, paid or
|
||||
tabbed line that isn't a deal's or the system's. Needs ordering.orders.open."""
|
||||
from core.access.context import current_device
|
||||
from schemas.order import OrderItemInput, SelectedOptionInput
|
||||
_need(request, db, user, "ordering.orders.open", target=f"order:{order_id}")
|
||||
order = db.get(Order, order_id)
|
||||
if order is None:
|
||||
raise XeniaError("order.not_found", order_id=order_id)
|
||||
if not _can_access_order(order, user, db):
|
||||
raise XeniaError("order.not_yours")
|
||||
item = db.query(OrderItem).filter(OrderItem.id == item_id, OrderItem.order_id == order_id).first()
|
||||
if item is None:
|
||||
raise XeniaError("order.item_not_found")
|
||||
product = item.product
|
||||
if product is None or not product.one_more_enabled:
|
||||
raise XeniaError("order.one_more_off")
|
||||
if item.status not in ("active", "paid", "tabbed") or item.deal_id or item.linked_item_id \
|
||||
or product.system_key or (product.unit_type or "piece") != "piece":
|
||||
raise XeniaError("order.one_more_line")
|
||||
try:
|
||||
options = [SelectedOptionInput(**o) for o in json.loads(item.selected_options or "[]") if isinstance(o, dict)]
|
||||
removed = [str(x) for x in json.loads(item.removed_ingredients or "[]")]
|
||||
except (ValueError, TypeError):
|
||||
options, removed = [], []
|
||||
device = current_device(request, db)
|
||||
source = "counter" if device is not None and device.app_key == "pos_counter" else "waiter"
|
||||
result = add_priced_items(order_id, AddItemsRequest(items=[OrderItemInput(
|
||||
product_id=item.product_id, quantity=body.quantity, selected_options=options or None,
|
||||
removed_ingredients=removed or None, notes=item.notes, course_id=item.course_id, source=source,
|
||||
)]), db, user)
|
||||
_audit(db, order_id, "ITEM_REORDERED", waiter_id=user.id, item_ids=[item.id],
|
||||
note=f"{body.quantity:g} more of #{item.id}")
|
||||
db.commit()
|
||||
return result
|
||||
|
||||
|
||||
class QuantityRequest(BaseModel):
|
||||
quantity: int = Field(ge=1, le=999)
|
||||
print_cancellation: bool = False # lowering: send a cancellation to the kitchen (the app asks)
|
||||
|
||||
|
||||
@router.post("/{order_id}/items/{item_id}/quantity")
|
||||
def change_quantity(order_id: int, item_id: int, body: QuantityRequest, request: Request, background_tasks: BackgroundTasks,
|
||||
db: Session = Depends(get_db), user: User = Depends(get_current_user)):
|
||||
"""Change a sent line's quantity without sending it again (6F.5). The venue
|
||||
(orders.quantity_change: off | add_only | add_and_remove) and ordering.items.change_quantity
|
||||
decide; lowering also needs ordering.orders.cancel (or a manager's PIN each). Up: the line
|
||||
grows, nothing is printed. Down: the removed part becomes its own cancelled line ("quantity
|
||||
lowered", split_from_id = the line) and, when asked, a cancellation ticket goes to the kitchen.
|
||||
Only unpaid lines counted in pieces, not a deal's or the system's."""
|
||||
from models.settings import PosSettings
|
||||
order = db.get(Order, order_id)
|
||||
if order is None:
|
||||
raise XeniaError("order.not_found", order_id=order_id)
|
||||
if not _can_access_order(order, user, db):
|
||||
raise XeniaError("order.not_yours")
|
||||
row = db.query(PosSettings).filter(PosSettings.key == "orders.quantity_change").first()
|
||||
mode = row.value if row is not None and row.value in ("off", "add_only", "add_and_remove") else "off"
|
||||
if mode == "off":
|
||||
raise XeniaError("order.quantity_change_off")
|
||||
item = db.query(OrderItem).filter(OrderItem.id == item_id, OrderItem.order_id == order_id).first()
|
||||
if item is None:
|
||||
raise XeniaError("order.item_not_found")
|
||||
product = item.product
|
||||
if item.status != "active" or item.deal_id or item.linked_item_id or (product is not None and product.system_key) \
|
||||
or (item.unit_type or "piece") != "piece" or item.quantity != int(item.quantity):
|
||||
raise XeniaError("order.quantity_line")
|
||||
old = int(item.quantity)
|
||||
if body.quantity == old:
|
||||
return {"item_id": item.id, "quantity": old, "cancelled_item_id": None}
|
||||
lowering = body.quantity < old
|
||||
if lowering and mode != "add_and_remove":
|
||||
raise XeniaError("order.quantity_remove_off")
|
||||
approval = _need(request, db, user, "ordering.items.change_quantity", target=f"order:{order_id} item:{item_id}")
|
||||
cancel_approval = None
|
||||
if lowering and not holds(user, "core.dashboard.access") and not _waiter_can_cancel(user, db):
|
||||
cancel_approval = require_override(request, db, "ordering.orders.cancel", target=f"order:{order_id}")
|
||||
note = f"{old} → {body.quantity}" + (f" · {_approval_note(approval)}" if approval is not None else "")
|
||||
part = None
|
||||
if lowering:
|
||||
now = utcnow()
|
||||
part = cut_part(item, old - body.quantity, status="cancelled", cancelled_by=user.id, cancelled_at=now,
|
||||
cancel_reason="quantity lowered")
|
||||
db.add(part)
|
||||
item.quantity = body.quantity
|
||||
db.flush()
|
||||
_audit(db, order_id, "ITEM_CANCELLED", waiter_id=user.id, item_ids=[part.id],
|
||||
note=("quantity lowered" + (f" · {_approval_note(cancel_approval)}" if cancel_approval is not None else "")
|
||||
+ ("" if body.print_cancellation else " · no kitchen ticket")))
|
||||
else:
|
||||
item.quantity = body.quantity
|
||||
_audit(db, order_id, "QUANTITY_CHANGED", waiter_id=user.id, item_ids=[item.id], note=note)
|
||||
db.commit()
|
||||
broadcast_sync("order_updated", {"order_id": order_id, "table_id": order.table_id, "status": order.status,
|
||||
"action": "quantity_changed", "item_ids": [item.id]})
|
||||
if part is not None:
|
||||
broadcast_sync("order_updated", {"order_id": order_id, "table_id": order.table_id, "status": order.status,
|
||||
"action": "items_cancelled", "item_ids": [part.id]})
|
||||
if body.print_cancellation:
|
||||
background_tasks.add_task(print_cancellation_ticket, order_id, [part.id])
|
||||
return {"item_id": item.id, "quantity": item.quantity, "cancelled_item_id": part.id if part is not None else None}
|
||||
|
||||
|
||||
# ─── Adjust price on an existing item ────────────────────────────────────────
|
||||
|
||||
class PriceAdjustRequest(BaseModel):
|
||||
|
||||
Reference in New Issue
Block a user