feat(manager): expenses, the supplier book and tabs on v2 (platform-v2 step 6.13a)
- Finance -> Expenses: still owed, late, filters, new/edit (supplier picked or added there), pay in parts, details with payments, delete - CRM -> Contacts: the supplier book with what each one is owed - Finance -> Tabs: open (who owes most first) / settled, charges and payments, pay (cash/card/other), close, forgive - backend: expenses.manage / tabs.manage (the list also reports.view, a customer's tabs also crm.manage, put-on-tab needs tabs.manage); payment <= due (expense.over_due), total >= paid (expense.below_paid), categories/kinds/methods checked, null clears a field; live events expenses.changed / tabs.changed; GET /api/expenses ?from=&to= venue dates (the report uses it); tab_status=settled - fix: an expense with a due date failed to save (naive date refused by D-11) - @xenia/i18n/number subpath; tests: test_finance.py (6), finance.test.mjs (5), e2e manager.mjs finance flow Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,13 @@
|
||||
"""Customer tabs: pay later (module `tabs`; manager page in plan step 6.13a).
|
||||
|
||||
A customer has at most one open tab. Lines of an order go on it from the order
|
||||
(POST /api/orders/{id}/items/{item}/tab: the line becomes "tabbed", the tab opens by itself);
|
||||
the customer pays it off in one or more payments (cash, card or other), and it is closed when
|
||||
nothing is owed, or forgiven (what is owed is written off, with a reason).
|
||||
Every route needs `tabs.manage`; a customer's tabs can also be read with `crm.manage` (their
|
||||
card on the customers page). Changes send the live event `tabs.changed`.
|
||||
"""
|
||||
from core.timeutil import utcnow
|
||||
from datetime import datetime, timezone
|
||||
from fastapi import APIRouter, Depends, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List, Optional
|
||||
@@ -10,13 +18,19 @@ from models.customers import Customer
|
||||
from models.order import Order, OrderItem
|
||||
from models.user import User
|
||||
from schemas.tabs import TabOut, TabEntryOut, TabPaymentOut, TabPayRequest, TabForgiveRequest, TabCloseRequest
|
||||
from routers.deps import require_manager
|
||||
from core.access.deps import require, require_any
|
||||
from core.errors import XeniaError
|
||||
from services.ws_bus import broadcast_sync
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
MANAGE = Depends(require("tabs.manage"))
|
||||
METHODS = ("cash", "card", "other")
|
||||
|
||||
|
||||
def changed():
|
||||
broadcast_sync("tabs.changed", {})
|
||||
|
||||
|
||||
def _tab_out(tab: Tab) -> dict:
|
||||
c = tab.customer
|
||||
@@ -75,10 +89,12 @@ def list_tabs(
|
||||
tab_status: Optional[str] = None,
|
||||
customer_id: Optional[int] = None,
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(require_manager),
|
||||
user: User = MANAGE,
|
||||
):
|
||||
q = db.query(Tab)
|
||||
if tab_status:
|
||||
if tab_status == "settled":
|
||||
q = q.filter(Tab.status.in_(("closed", "forgiven")))
|
||||
elif tab_status:
|
||||
q = q.filter(Tab.status == tab_status)
|
||||
else:
|
||||
q = q.filter(Tab.status == "open") # default: only open tabs
|
||||
@@ -86,13 +102,13 @@ def list_tabs(
|
||||
q = q.filter(Tab.customer_id == customer_id)
|
||||
tabs = q.order_by(Tab.opened_at.desc()).all()
|
||||
result = [_tab_out(t) for t in tabs]
|
||||
# Sort open tabs by balance descending
|
||||
result.sort(key=lambda x: x["balance"], reverse=True)
|
||||
if tab_status in (None, "open"):
|
||||
result.sort(key=lambda x: x["balance"], reverse=True) # who owes most first
|
||||
return result
|
||||
|
||||
|
||||
@router.get("/{tab_id}", response_model=TabOut)
|
||||
def get_tab(tab_id: int, db: Session = Depends(get_db), user: User = Depends(require_manager)):
|
||||
def get_tab(tab_id: int, db: Session = Depends(get_db), user: User = MANAGE):
|
||||
tab = db.query(Tab).filter(Tab.id == tab_id).first()
|
||||
if not tab:
|
||||
raise XeniaError("tab.not_found")
|
||||
@@ -100,7 +116,7 @@ def get_tab(tab_id: int, db: Session = Depends(get_db), user: User = Depends(req
|
||||
|
||||
|
||||
@router.post("/", response_model=TabOut, status_code=status.HTTP_201_CREATED)
|
||||
def open_tab(customer_id: int, db: Session = Depends(get_db), user: User = Depends(require_manager)):
|
||||
def open_tab(customer_id: int, db: Session = Depends(get_db), user: User = MANAGE):
|
||||
customer = db.query(Customer).filter(Customer.id == customer_id, Customer.is_active == True).first()
|
||||
if not customer:
|
||||
raise XeniaError("customer.not_found")
|
||||
@@ -111,6 +127,7 @@ def open_tab(customer_id: int, db: Session = Depends(get_db), user: User = Depen
|
||||
db.add(tab)
|
||||
db.commit()
|
||||
db.refresh(tab)
|
||||
changed()
|
||||
return _tab_out(tab)
|
||||
|
||||
|
||||
@@ -122,7 +139,7 @@ def add_tab_entry(
|
||||
amount: float = 0.0,
|
||||
description: Optional[str] = None,
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(require_manager),
|
||||
user: User = MANAGE,
|
||||
):
|
||||
tab = db.query(Tab).filter(Tab.id == tab_id, Tab.status == "open").first()
|
||||
if not tab:
|
||||
@@ -136,6 +153,7 @@ def add_tab_entry(
|
||||
db.add(entry)
|
||||
db.commit()
|
||||
db.refresh(tab)
|
||||
changed()
|
||||
return _tab_out(tab)
|
||||
|
||||
|
||||
@@ -144,7 +162,7 @@ def pay_tab(
|
||||
tab_id: int,
|
||||
body: TabPayRequest,
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(require_manager),
|
||||
user: User = MANAGE,
|
||||
):
|
||||
tab = db.query(Tab).filter(Tab.id == tab_id, Tab.status == "open").first()
|
||||
if not tab:
|
||||
@@ -156,14 +174,18 @@ def pay_tab(
|
||||
balance = total_charged - total_paid
|
||||
if body.amount > balance + 0.005:
|
||||
raise XeniaError("tab.over_balance", balance=f'{balance:.2f}')
|
||||
method = body.payment_method or "cash"
|
||||
if method not in METHODS:
|
||||
raise XeniaError("validation.choice", field="payment_method", value=method)
|
||||
payment = TabPayment(
|
||||
tab_id=tab_id, amount=body.amount,
|
||||
payment_method=body.payment_method, notes=body.notes,
|
||||
payment_method=method, notes=body.notes,
|
||||
received_by_id=user.id,
|
||||
)
|
||||
db.add(payment)
|
||||
db.commit()
|
||||
db.refresh(tab)
|
||||
changed()
|
||||
return _tab_out(tab)
|
||||
|
||||
|
||||
@@ -172,7 +194,7 @@ def close_tab(
|
||||
tab_id: int,
|
||||
body: TabCloseRequest,
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(require_manager),
|
||||
user: User = MANAGE,
|
||||
):
|
||||
tab = db.query(Tab).filter(Tab.id == tab_id, Tab.status == "open").first()
|
||||
if not tab:
|
||||
@@ -189,6 +211,7 @@ def close_tab(
|
||||
tab.notes = body.notes
|
||||
db.commit()
|
||||
db.refresh(tab)
|
||||
changed()
|
||||
return _tab_out(tab)
|
||||
|
||||
|
||||
@@ -197,7 +220,7 @@ def forgive_tab(
|
||||
tab_id: int,
|
||||
body: TabForgiveRequest,
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(require_manager),
|
||||
user: User = MANAGE,
|
||||
):
|
||||
tab = db.query(Tab).filter(Tab.id == tab_id, Tab.status == "open").first()
|
||||
if not tab:
|
||||
@@ -209,13 +232,15 @@ def forgive_tab(
|
||||
tab.notes = body.reason
|
||||
db.commit()
|
||||
db.refresh(tab)
|
||||
changed()
|
||||
return _tab_out(tab)
|
||||
|
||||
|
||||
# ── Customer tabs ──────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/customer/{customer_id}", response_model=List[TabOut])
|
||||
def customer_tabs(customer_id: int, db: Session = Depends(get_db), user: User = Depends(require_manager)):
|
||||
def customer_tabs(customer_id: int, db: Session = Depends(get_db),
|
||||
user: User = Depends(require_any("tabs.manage", "crm.manage"))):
|
||||
tabs = db.query(Tab).filter(Tab.customer_id == customer_id).order_by(
|
||||
Tab.status.asc(), # open first
|
||||
Tab.opened_at.desc(),
|
||||
|
||||
Reference in New Issue
Block a user