Plan phase 3S, step S.99.
- GET /api/connect/fees returns `links` {order, menu}: the ordering app and QR menu
addresses, from the cloud's apps_url (heartbeat) and the site id.
- Manager: "Your ordering link" card (OrderingLink.jsx) on Online ordering: the address, its
QR code, copy / open / print, to share or put on the website and window.
- Pack README: routers/fees.py row.
- Tests: the link (backend), the card (manager e2e).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
179 lines
11 KiB
Python
179 lines
11 KiB
Python
"""The site side of online orders (plan phase 3S, step S.5): a pulled order keeps the customer's
|
|
time and area, its delivery fee becomes a line of its own (a system product: paid with the
|
|
items, counted in the shift, on the fiscal receipt, never in the kitchen), and the site checks
|
|
the cloud's prices with its own engine (a difference is kept and shown, the customer pays what
|
|
they were charged)."""
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from tests.test_live import err, owner_h, pair, terminal_login # noqa: F401 (fixtures)
|
|
from tests.test_pos import drawer_printer, menu, till # noqa: F401 (fixtures)
|
|
from tests.test_pos_online import board, cloud, counter, kds_ids # noqa: F401 (fixtures)
|
|
|
|
|
|
def pull(db, menu, cloud_id=601, *, burger_price=10.0, fee=2.5, area_id=None, area="Centre", **extra):
|
|
"""An online delivery order as the cloud hands it over (S.4 shape)."""
|
|
from services.cloud_sync import store_online_orders
|
|
order = {
|
|
"id": cloud_id, "public_ref": f"ORD-{cloud_id:04d}", "order_type": "delivery", "sales_point": "online_delivery",
|
|
"customer_name": "Maria", "customer_phone": "6911111111", "customer_address": "Harbour 7",
|
|
"status": "pending_acceptance", "delivery_area": area, "delivery_area_id": area_id,
|
|
"requested_for": "2026-11-16T19:30:00+00:00", "created_at": "2026-11-16T17:00:00+00:00",
|
|
"items_json": json.dumps([
|
|
{"product_id": menu["burger"].id, "name": "Burger", "quantity": 2, "unit_price": burger_price + 1.0,
|
|
"options": [{"id": menu["burger"].quick_options[0].id, "name": "Extra cheese", "price": 1.0}]},
|
|
{"product_id": menu["coffee"].id, "name": "Freddo", "quantity": 1, "unit_price": 3.0, "options": []},
|
|
]),
|
|
"subtotal": round(2 * (burger_price + 1.0) + 3.0, 2), "delivery_fee": fee,
|
|
"total": round(2 * (burger_price + 1.0) + 3.0 + fee, 2), **extra,
|
|
}
|
|
return store_online_orders(db, [order])[cloud_id]
|
|
|
|
|
|
@pytest.fixture
|
|
def fees_set(db):
|
|
"""The venue's own fee rules: €2.50 to the Centre (a fixed fee)."""
|
|
from models.fees import DeliveryArea, FeeRule
|
|
centre = DeliveryArea(name="Centre", postcodes=json.dumps(["85100"]), is_active=1, sort_order=0)
|
|
db.add(centre)
|
|
db.flush()
|
|
db.add(FeeRule(name="Centre", is_active=1, sort_order=0, sales_points=json.dumps(["online_delivery"]),
|
|
area_ids=json.dumps([centre.id]), action="set", value=2.5, stop=1))
|
|
db.commit()
|
|
return centre
|
|
|
|
|
|
def test_the_fee_is_its_own_line_and_never_reaches_the_kitchen(client, db, counter, menu, owner_h, cloud, fees_set):
|
|
from models.order import Order
|
|
from models.product import Product
|
|
oid = pull(db, menu, area_id=fees_set.id)
|
|
order = db.get(Order, oid)
|
|
assert order.requested_for.isoformat() == "2026-11-16T19:30:00+00:00"
|
|
assert order.online_delivery_area == "Centre"
|
|
assert order.online_price_diff is None # the cloud's prices are the venue's
|
|
fee = [i for i in order.items if i.system_key == "delivery_fee"]
|
|
assert [(i.product_name, i.quantity, i.unit_price, i.printed, i.kds_status) for i in fee] == [("Delivery fee", 1, 2.5, True, "served")]
|
|
burger = next(i for i in order.items if i.product_id == menu["burger"].id)
|
|
assert json.loads(burger.selected_options) == [{"id": menu["burger"].quick_options[0].id, "name": "Extra cheese",
|
|
"price_delta": 1.0, "type": "quick"}]
|
|
assert burger.unit_cost == 3.5 # the cost snapshot, as for any order
|
|
# the system product: never on a menu or in the catalogue, never added by hand
|
|
product = db.query(Product).filter(Product.system_key == "delivery_fee").one()
|
|
assert product.lifecycle_status == "system"
|
|
assert product.id not in [p["id"] for p in client.get("/api/products/?all=true", headers=owner_h).json()]
|
|
assert product.id not in [p["id"] for p in client.get("/api/pos/menu", headers=counter["h"]).json()["products"]]
|
|
assert client.put(f"/api/products/{product.id}", headers=owner_h, json={"name": "x"}).status_code == 404
|
|
assert err(client.post(f"/api/orders/{oid}/items", headers=owner_h,
|
|
json={"items": [{"product_id": product.id, "quantity": 1}]})) == "order.not_sold_here"
|
|
# the board: the time, the area, the fee
|
|
o = board(client, counter)["incoming"][0]
|
|
assert (o["requested_for"], o["area"], o["fee"], o["total"], o["price_diff"]) == ("2026-11-16T19:30:00+00:00", "Centre", 2.5, 27.5, None)
|
|
assert [i["system_key"] for i in o["items"]] == [None, None, "delivery_fee"]
|
|
# accepted: the kitchen gets the food, not the fee
|
|
client.post(f"/api/connect/orders/{oid}/accept", headers=counter["h"])
|
|
assert cloud["printed"] == [(oid, sorted(i.id for i in order.items if i.system_key is None))]
|
|
kds = next(x for x in client.get("/api/kds/orders", headers=owner_h).json()["orders"] if x["id"] == oid)
|
|
assert [i["product_name"] for i in kds["items"]] == ["Burger", "Freddo"]
|
|
assert kds["online_ref"] == "ORD-0601" and kds["requested_for"] == "2026-11-16T19:30:00+00:00"
|
|
assert kds["kds_status"] == "pending" # the served fee line doesn't count
|
|
|
|
|
|
def test_paid_at_the_counter_in_full_with_the_shift_and_the_fiscal_receipt(client, db, counter, menu, cloud, fees_set,
|
|
monkeypatch, owner_h):
|
|
from models.settings import PosSettings
|
|
from services import fiscal
|
|
issued = []
|
|
monkeypatch.setattr(fiscal, "send_fiscal_receipt_background",
|
|
lambda items, method, total, db, order_id, db_factory, **kw: issued.append((items, method, total)))
|
|
for key, value in (("fiscal.enabled", "true"), ("fiscal.vat_groups", json.dumps([{"machine_id": 2, "friendly_name": "13%"}]))):
|
|
row = db.get(PosSettings, key)
|
|
if row is None:
|
|
db.add(PosSettings(key=key, value=value))
|
|
else:
|
|
row.value = value
|
|
menu["burger"].fiscal_vat_group_id = menu["coffee"].fiscal_vat_group_id = 2
|
|
db.commit()
|
|
oid = pull(db, menu, area_id=fees_set.id)
|
|
h = counter["h"]
|
|
client.post(f"/api/connect/orders/{oid}/accept", headers=h)
|
|
for s in ("preparing", "out_for_delivery"):
|
|
client.post(f"/api/connect/orders/{oid}/status", headers=h, json={"status": s})
|
|
o = board(client, counter)["in_progress"][0]
|
|
assert o["unpaid"] == 27.5
|
|
paid = client.post(f"/api/pos/orders/{oid}/pay", headers=h,
|
|
json={"payments": [{"method": "cash", "item_ids": [i["id"] for i in o["items"]]}], "cash_given": 30})
|
|
assert paid.status_code == 200, paid.text
|
|
assert paid.json()["total"] == 27.5 and paid.json()["change"] == 2.5
|
|
# the fee is on the receipt, with the VAT group most of the venue's products use
|
|
(items, method, total), = issued
|
|
assert method == "cash" and total == 27.5
|
|
assert {"name": "Delivery fee", "qty": 1, "unit_price": 2.5, "vat_group_id": 2} in items
|
|
# and in the shift
|
|
from routers.shifts import compute_shift_total
|
|
assert compute_shift_total(counter["shift"].id, db) == 27.5
|
|
|
|
|
|
def test_the_site_flags_prices_that_differ(client, db, counter, menu, fees_set):
|
|
from models.order import Order
|
|
# the cloud charged an old burger price and a fee the venue no longer has for that area
|
|
oid = pull(db, menu, cloud_id=602, burger_price=9.0, fee=4.0, area_id=fees_set.id)
|
|
diff = json.loads(db.get(Order, oid).online_price_diff)
|
|
assert diff["lines"] == [{"product_id": menu["burger"].id, "name": "Burger", "quantity": 2, "cloud": 10.0, "site": 11.0, "reason": "price"}]
|
|
assert diff["fee"] == {"cloud": 4.0, "site": 2.5, "reason": "price"}
|
|
assert (diff["cloud_total"], diff["site_total"]) == (27.0, 27.5)
|
|
# the customer pays what they were charged: the order keeps the cloud's prices
|
|
o = next(x for x in board(client, counter)["incoming"] if x["id"] == oid)
|
|
assert o["total"] == 27.0 and o["fee"] == 4.0 and o["price_diff"]["fee"]["site"] == 2.5
|
|
# an area the venue doesn't serve (any more), a product no longer sold online
|
|
menu["coffee"].sold_at = json.dumps(["in_store"])
|
|
db.commit()
|
|
oid = pull(db, menu, cloud_id=603, area_id=999)
|
|
diff = json.loads(db.get(Order, oid).online_price_diff)
|
|
assert [(x["name"], x["reason"]) for x in diff["lines"]] == [("Freddo", "not_sold")]
|
|
assert diff["fee"] == {"cloud": 2.5, "site": None, "reason": "area"}
|
|
|
|
|
|
def test_no_fee_no_line_and_asap(db, menu, counter):
|
|
from models.order import Order
|
|
oid = pull(db, menu, cloud_id=604, fee=0.0, requested_for=None, sales_point="online_takeaway", order_type="takeaway",
|
|
delivery_area=None)
|
|
order = db.get(Order, oid)
|
|
assert order.requested_for is None and order.online_delivery_area is None
|
|
assert all(i.system_key is None for i in order.items) and len(order.items) == 2
|
|
|
|
|
|
def test_the_fee_line_name_and_vat_group(client, db, owner_h):
|
|
from models.settings import PosSettings
|
|
r = client.get("/api/connect/fees", headers=owner_h)
|
|
assert r.status_code == 200, r.text
|
|
fp = r.json()["fee_product"]
|
|
assert fp["key"] == "delivery_fee" and fp["name"] == "Delivery fee" and fp["vat_groups"] == []
|
|
r = client.put("/api/connect/fees/fee-product", headers=owner_h, json={"name": "Μεταφορικά", "vat_group_id": 3})
|
|
assert r.status_code == 200 and r.json()["name"] == "Μεταφορικά" and r.json()["vat_group_id"] == 3
|
|
db.add(PosSettings(key="fiscal.vat_groups", value=json.dumps([{"machine_id": 1, "friendly_name": "24%"}])))
|
|
db.commit()
|
|
assert err(client.put("/api/connect/fees/fee-product", headers=owner_h, json={"name": "Fee", "vat_group_id": 3})) == "fees.vat_group_unknown"
|
|
assert client.put("/api/connect/fees/fee-product", headers=owner_h, json={"name": " ", "vat_group_id": 1}).status_code == 422
|
|
assert client.get("/api/connect/fees", headers=owner_h).json()["fee_product"]["vat_groups"] == [{"machine_id": 1, "friendly_name": "24%"}]
|
|
|
|
|
|
def test_kitchen_ticket_shows_the_reference_and_the_time(db, menu):
|
|
from models.order import Order
|
|
from services.printer_service import _online_meta, _place
|
|
order = db.get(Order, pull(db, menu, cloud_id=605))
|
|
meta = _online_meta(order)
|
|
assert meta[0] == ("Online:", "ORD-0605 - Delivery")
|
|
assert meta[1][0] == "For:" and meta[1][1].endswith("21:30") # 19:30 UTC at the venue (Athens, winter)
|
|
assert _place(order, "None") == "ORD-0605"
|
|
order.requested_for = None
|
|
assert _online_meta(order)[1] == ("For:", "As soon as possible")
|
|
|
|
|
|
def test_the_venues_ordering_link(client, owner_h, monkeypatch):
|
|
from services import cloud_sync
|
|
monkeypatch.setitem(cloud_sync.license_state, "apps_url", "https://menu.example.com")
|
|
monkeypatch.setattr(cloud_sync.settings, "SITE_ID", "taverna-1")
|
|
links = client.get("/api/connect/fees", headers=owner_h).json()["links"]
|
|
assert links == {"order": "https://menu.example.com/order/taverna-1", "menu": "https://menu.example.com/menu/taverna-1"}
|