feat(pos): refunds at the counter on a fiscal driver layer (platform-v2 3.3e)

- services/fiscal/: one driver interface (prepare_receipt / prepare_credit_note -> send,
  credit_notes flag, status); the dTEC100extra folder bridge moved into txt_file.py as the
  first driver, same command files. fiscal_service.py removed, callers moved.
- Refunds: models/refund.py + Alembic 0016 (refunds, refund_items; ordering.refunds for
  existing owners/managers). services/refunds.py: today's business day, the cashier's open
  shift, partial quantities, items from another shift need ordering.refunds or a manager's
  approval (check()/authorize() return the override row). Paid items are never edited.
- POS: GET /api/pos/receipts, GET /api/pos/receipts/{id}, POST /api/pos/orders/{id}/refund
  (cash opens the drawer; fiscal "manual" when the driver can't issue credit notes).
- Shift totals subtract refunds and now count discounts; day close and business-day
  reports carry refunds.
- Terminal: Receipts mode (ReceiptsView, ReceiptPanel, logic/refunds.js), en/el texts.
- Tests: test_pos_refunds.py (12), terminal unit +1, terminal.mjs +7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 09:44:17 +02:00
co-authored by Claude Opus 5.5
parent d542cf7d34
commit f2182dcb37
32 changed files with 1486 additions and 169 deletions
+29 -10
View File
@@ -30,15 +30,15 @@ def _get_setting(db: Session, key: str, default: str = "true") -> str:
def compute_shift_total(shift_id: int, db: Session) -> float:
items = db.query(OrderItem).filter(
OrderItem.paid_in_shift_id == shift_id,
OrderItem.status == "paid",
).all()
return round(sum(i.unit_price * i.quantity for i in items), 2)
"""What the shift took, net of its refunds (3.3e)."""
split = compute_shift_payment_split(shift_id, db)
return round(split["cash_sales"] + split["card_sales"] - split["cash_refunds"] - split["card_refunds"], 2)
def compute_shift_payment_split(shift_id: int, db: Session) -> dict:
"""Return cash_sales and card_sales for a shift (items paid in this shift)."""
"""cash_sales / card_sales (items paid in this shift, with their price adjustments, i.e.
discounts) and cash_refunds / card_refunds (money this shift gave back, 3.3e)."""
from services.refunds import item_value, shift_refunds
items = db.query(OrderItem).filter(
OrderItem.paid_in_shift_id == shift_id,
OrderItem.status == "paid",
@@ -46,13 +46,15 @@ def compute_shift_payment_split(shift_id: int, db: Session) -> dict:
cash = 0.0
card = 0.0
for i in items:
amount = float(i.unit_price) * i.quantity
amount = item_value(i)
method = (i.payment_method or "cash").lower()
if method == "card":
card += amount
else:
cash += amount
return {"cash_sales": round(cash, 2), "card_sales": round(card, 2)}
refunds = shift_refunds(db, shift_id)
return {"cash_sales": round(cash, 2), "card_sales": round(card, 2),
"cash_refunds": refunds["cash"], "card_refunds": refunds["card"]}
def compute_shift_pay(shift: WaiterShift) -> dict:
@@ -119,7 +121,9 @@ def _enrich_shift(shift: WaiterShift, db: Session) -> dict:
"total_collected": total,
"cash_sales": split["cash_sales"],
"card_sales": split["card_sales"],
"net_to_deliver": round(split["cash_sales"] + (shift.starting_cash or 0.0), 2),
"cash_refunds": split["cash_refunds"],
"card_refunds": split["card_refunds"],
"net_to_deliver": round(split["cash_sales"] - split["cash_refunds"] + (shift.starting_cash or 0.0), 2),
"is_active": shift.ended_at is None,
"notes": shift.notes,
"hourly_rate_snapshot": shift.hourly_rate_snapshot,
@@ -131,7 +135,7 @@ def _enrich_shift(shift: WaiterShift, db: Session) -> dict:
# Phase 2E — discrepancy computed live against cash-only expected (starting_cash + cash_sales)
"counted_cash_end": shift.counted_cash_end,
"cash_discrepancy": round(
shift.counted_cash_end - (split["cash_sales"] + (shift.starting_cash or 0.0)), 2
shift.counted_cash_end - (split["cash_sales"] - split["cash_refunds"] + (shift.starting_cash or 0.0)), 2
) if shift.counted_cash_end is not None else None,
"breaks": [
{"id": b.id, "shift_id": b.shift_id, "started_at": _dt(b.started_at), "ended_at": _dt(b.ended_at)}
@@ -578,4 +582,19 @@ def get_shift_summary(
enriched = _enrich_shift(shift, db)
enriched["orders"] = populated_orders
enriched["duration_minutes"] = duration_minutes
from models.refund import Refund
refunds = db.query(Refund).filter(Refund.shift_id == shift_id).order_by(Refund.created_at).all()
for r in refunds:
all_waiter_ids.update(x for x in (r.refunded_by, r.approved_by) if x)
missing = [w for w in all_waiter_ids if w not in waiters_map]
if missing:
for w in db.query(User).filter(User.id.in_(missing)).all():
waiters_map[w.id] = w.full_name or w.username
enriched["refunds"] = [{
"id": r.id, "order_id": r.order_id, "created_at": _dt(r.created_at), "method": r.method, "amount": r.amount,
"reason": r.reason, "refunded_by_name": _wname(r.refunded_by), "approved_by_name": _wname(r.approved_by),
"fiscal_status": r.fiscal_status,
"items": [{"order_item_id": ri.order_item_id, "name": ri.order_item.product_name if ri.order_item else None,
"quantity": ri.quantity, "amount": ri.amount} for ri in r.items],
} for r in refunds]
return enriched