feat(terminal): quick sale at the counter: cash/card/split, change, cash drawer, fiscal (platform-v2 3.3b)

- POST /api/pos/sales: all checks first (shift, lines, payment split, cash given,
  discount → ordering.prices.modify or manager approval, fiscal VAT groups), then the
  existing open_order / add_items / pay_items functions, so tickets, KDS, price events,
  shift totals and the fiscal receipt work as for any order.
- Cash drawer: printer_service.open_cash_drawer (ESC @ + ESC p 0 25 250) to the screen's
  receipt printer on cash, logged and audited (DRAWER_OPENED / DRAWER_FAILED).
- orders._can_access_order: the people assigned to an order may act on it (a cashier's
  own takeaway order); pos_cart prices a batch with the lines before it, as add_items does.
- Terminal: Charge → payment dialog (exact cash pre-selected, quick amounts, change,
  card, split by line), per-line discounts, sale summary and "Same again".
- Tests: backend +6 (socket printer receives the drawer kick, fiscal file in the bridge
  folder), terminal unit +1, terminal.mjs +9.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 00:28:33 +02:00
co-authored by Claude Opus 5.5
parent 17bee66df1
commit bff82a332a
15 changed files with 632 additions and 14 deletions
+17 -3
View File
@@ -20,7 +20,9 @@ Rules (the same as the waiter's options dialog, waiter_pwa ItemOptionsModal.jsx)
- quantities: per-choice qty only on multi-select sets, per-add-on qty only on allow_multiple.
Anything else → XeniaError("pos.line_invalid", line=n, reason=...), reason one of
product_unavailable | unknown_quick | unknown_option | unknown_choice | unknown_sub |
choice_required | sub_required | unknown_ingredient | quantity.
choice_required | sub_required | unknown_ingredient | quantity | discount.
A line may carry `discount_pct` (0–100, whole percent): the quote shows it; selling it needs
ordering.prices.modify or a manager's approval (routers/pos.py).
"""
from __future__ import annotations
@@ -61,6 +63,7 @@ class Line:
entries: list = field(default_factory=list) # selected_options entries
removed: list = field(default_factory=list) # ingredient names
notes: Optional[str] = None
discount_pct: int = 0 # 3.3b: needs ordering.prices.modify (or approval)
@property
def extra(self) -> float:
@@ -96,6 +99,10 @@ def resolve_line(db, raw: dict, n: int) -> Line:
if isinstance(quantity, bool) or not isinstance(quantity, (int, float)) or quantity <= 0 or quantity > 999:
raise _bad(n, "quantity")
line = Line(product=product, quantity=quantity, notes=(raw.get("notes") or "").strip() or None)
pct = raw.get("discount_pct", 0) or 0
if isinstance(pct, bool) or not isinstance(pct, int) or not 0 <= pct <= 100:
raise _bad(n, "discount")
line.discount_pct = pct
quick = {q.id: q for q in product.quick_options}
for sel in raw.get("quick") or []:
@@ -170,8 +177,8 @@ def resolve_line(db, raw: dict, n: int) -> Line:
def price_lines(db, lines: list[Line], order_type: str, existing_cart: list | None = None):
"""[(unit_price, [PriceEvent])] for each line, with the same context POST
/api/orders/{id}/items builds: the order's existing items plus this line (the other new
lines of the same batch don't count, as there)."""
/api/orders/{id}/items builds: the order's existing items, the lines before it in the
same batch (at their resolved price), and this line."""
from core.timeutil import utcnow
from services.pricing import OrderContext, build_active_price_group_ids, resolve_price
now = utcnow()
@@ -186,4 +193,11 @@ def price_lines(db, lines: list[Line], order_type: str, existing_cart: list | No
ctx = OrderContext(channel=channel, now=now, cart_items=cart + [here], active_price_group_ids=active)
unit, events = resolve_price(p, line.quantity, line.extra, ctx, db)
out.append((unit, events))
cart.append({**here, "unit_price": unit})
return out
def discount_adjustment(unit_price: float, pct) -> float:
"""A line's discount (percent of its unit price) as the order item's price_adjustment
(negative, rounded to cents)."""
return -round(unit_price * pct / 100.0, 2) if pct else 0.0
+21
View File
@@ -324,6 +324,27 @@ def send_test_beep(ip: str, port: int, n1: int, n2: int, n3: int, codepage_n: in
return False, str(e)
DRAWER_KICK = bytes([0x1b, 0x70, 0x00, 0x19, 0xfa]) # ESC p m t1 t2: pin 2, 50 ms on, 500 ms off
def open_cash_drawer(printer, db: Session, reason: str = "") -> Tuple[bool, str]:
"""Pulse the cash drawer wired to `printer` (counter POS cash payments, plan step 3.3b).
A job of its own: ESC @ then ESC p 0 25 250, nothing printed. Logs one line either way.
Spoof printing drops it like any print. -> (ok, error)"""
if _is_spoof_mode(db):
logger.info("Cash drawer: spoof printing ON, not sent to %s (%s)", printer.name, reason)
return True, ""
try:
p = Network(printer.ip_address, printer.port, timeout=PRINTER_TIMEOUT)
p._raw(bytes([0x1b, 0x40]) + DRAWER_KICK)
p.close()
logger.info("Cash drawer: opened on %s (%s:%s) %s", printer.name, printer.ip_address, printer.port, reason)
return True, ""
except Exception as e:
logger.warning("Cash drawer: FAILED on %s (%s:%s) %s: %s", printer.name, printer.ip_address, printer.port, reason, e)
return False, str(e)
def send_test_print(ip: str, port: int, name: str, codepage_n: int = 29) -> Tuple[bool, str]:
if is_spoof_mode():
logger.info("Spoof printing ON — dropping test print for %s", name)