feat(manager): customers, throwaways, stock and notes on v2 (platform-v2 step 6.13b)
- CRM -> Customers: search ignoring case and Greek accents (and part of the phone), visits / spent / last visit, the card (contact, notes, open tab, every visit with what was on a tab), new/edit/remove - Inventory -> Throwaways: log (product, quantity with a comma, reason, note) with its cost, today's list (delete today only), a period's totals by reason and product, every entry; Inventory -> Stock: an honest 'not available yet' - Notes: notes (pinned first, edit in place) and to-dos (urgent first, done folded away, who did it kept) - backend: crm.manage / inventory.manage / notes.use (the customer search also with the dashboard key, for orders); spending counted by services/sales.py (refunds off, tab lines in); null clears a field; names/notes not blank, priorities checked; waste ?date_from=&date_to= venue dates; the Greek reason labels left the API; live customers.changed / waste.changed / notes.changed - the overview's notes card needs notes.use and refreshes live - tests: test_customers_waste_notes.py (4), crm.test.mjs (3), e2e manager.mjs (customers, throwaways, notes, stock, Greek) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,13 @@
|
||||
from core.timeutil import parse_api_datetime
|
||||
"""Throwaways (module `inventory`; manager page Inventory → Throwaways, plan step 6.13b).
|
||||
|
||||
What was thrown away (a product, how many, why: a kitchen mistake, dropped, expired, other)
|
||||
with its cost at that moment (the product's cost, when it has one) and who logged it, in the
|
||||
open business day. An entry can be deleted only during the business day it was logged in.
|
||||
Everything needs `inventory.manage`; changes send `waste.changed`.
|
||||
"""
|
||||
from datetime import date
|
||||
|
||||
from core.timeutil import parse_api_datetime, venue_day_window
|
||||
import json
|
||||
from fastapi import APIRouter, Depends, status, Query
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -9,12 +18,28 @@ from models.waste import WasteLog
|
||||
from models.product import Product
|
||||
from models.business_day import BusinessDay
|
||||
from models.user import User
|
||||
from schemas.waste import WasteLogCreate, WasteLogOut, VALID_REASONS, REASON_LABELS
|
||||
from routers.deps import require_manager
|
||||
from schemas.waste import WasteLogCreate, VALID_REASONS
|
||||
from core.access.deps import require
|
||||
from core.errors import XeniaError
|
||||
from services.ws_bus import broadcast_sync
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
MANAGE = Depends(require("inventory.manage"))
|
||||
|
||||
|
||||
def _period(q, business_day_id, from_dt, to_dt, date_from, date_to):
|
||||
"""business_day_id, or `from` / `to` times, or `date_from` / `date_to` venue dates (inclusive)."""
|
||||
if business_day_id:
|
||||
return q.filter(WasteLog.business_day_id == business_day_id)
|
||||
if from_dt and to_dt:
|
||||
return q.filter(WasteLog.logged_at >= parse_api_datetime(from_dt), WasteLog.logged_at <= parse_api_datetime(to_dt))
|
||||
if date_from:
|
||||
q = q.filter(WasteLog.logged_at >= venue_day_window(date_from)[0])
|
||||
if date_to:
|
||||
q = q.filter(WasteLog.logged_at < venue_day_window(date_to)[1])
|
||||
return q
|
||||
|
||||
|
||||
def _effective_cost(product: Product) -> Optional[float]:
|
||||
"""Return product's current effective cost (breakdown sum or simple), or None."""
|
||||
@@ -44,7 +69,6 @@ def _waste_out(w: WasteLog) -> dict:
|
||||
"unit_cost_snapshot": w.unit_cost_snapshot,
|
||||
"total_cost": w.total_cost,
|
||||
"reason": w.reason,
|
||||
"reason_label": REASON_LABELS.get(w.reason, w.reason),
|
||||
"reason_notes": w.reason_notes,
|
||||
"logged_by_id": w.logged_by_id,
|
||||
"logged_by_name": logger_name,
|
||||
@@ -58,18 +82,12 @@ def list_waste(
|
||||
business_day_id: Optional[int] = None,
|
||||
from_dt: Optional[str] = Query(default=None, alias="from"),
|
||||
to_dt: Optional[str] = Query(default=None, alias="to"),
|
||||
date_from: Optional[date] = None,
|
||||
date_to: Optional[date] = None,
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(require_manager),
|
||||
user: User = MANAGE,
|
||||
):
|
||||
from datetime import datetime
|
||||
q = db.query(WasteLog).order_by(WasteLog.logged_at.desc())
|
||||
if business_day_id:
|
||||
q = q.filter(WasteLog.business_day_id == business_day_id)
|
||||
elif from_dt and to_dt:
|
||||
q = q.filter(
|
||||
WasteLog.logged_at >= parse_api_datetime(from_dt),
|
||||
WasteLog.logged_at <= parse_api_datetime(to_dt),
|
||||
)
|
||||
q = _period(db.query(WasteLog).order_by(WasteLog.logged_at.desc()), business_day_id, from_dt, to_dt, date_from, date_to)
|
||||
entries = q.all()
|
||||
return {"entries": [_waste_out(w) for w in entries]}
|
||||
|
||||
@@ -78,7 +96,7 @@ def list_waste(
|
||||
def log_waste(
|
||||
body: WasteLogCreate,
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(require_manager),
|
||||
user: User = MANAGE,
|
||||
):
|
||||
if body.reason not in VALID_REASONS:
|
||||
raise XeniaError("validation.choice", field='reason', value=body.reason)
|
||||
@@ -109,6 +127,7 @@ def log_waste(
|
||||
db.add(entry)
|
||||
db.commit()
|
||||
db.refresh(entry)
|
||||
broadcast_sync("waste.changed", {})
|
||||
return _waste_out(entry)
|
||||
|
||||
|
||||
@@ -116,7 +135,7 @@ def log_waste(
|
||||
def delete_waste(
|
||||
entry_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(require_manager),
|
||||
user: User = MANAGE,
|
||||
):
|
||||
entry = db.query(WasteLog).filter(WasteLog.id == entry_id).first()
|
||||
if not entry:
|
||||
@@ -129,6 +148,7 @@ def delete_waste(
|
||||
|
||||
db.delete(entry)
|
||||
db.commit()
|
||||
broadcast_sync("waste.changed", {})
|
||||
|
||||
|
||||
@router.get("/summary")
|
||||
@@ -136,19 +156,12 @@ def waste_summary(
|
||||
business_day_id: Optional[int] = None,
|
||||
from_dt: Optional[str] = Query(default=None, alias="from"),
|
||||
to_dt: Optional[str] = Query(default=None, alias="to"),
|
||||
date_from: Optional[date] = None,
|
||||
date_to: Optional[date] = None,
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(require_manager),
|
||||
user: User = MANAGE,
|
||||
):
|
||||
from datetime import datetime
|
||||
q = db.query(WasteLog)
|
||||
if business_day_id:
|
||||
q = q.filter(WasteLog.business_day_id == business_day_id)
|
||||
elif from_dt and to_dt:
|
||||
q = q.filter(
|
||||
WasteLog.logged_at >= parse_api_datetime(from_dt),
|
||||
WasteLog.logged_at <= parse_api_datetime(to_dt),
|
||||
)
|
||||
entries = q.all()
|
||||
entries = _period(db.query(WasteLog), business_day_id, from_dt, to_dt, date_from, date_to).all()
|
||||
|
||||
total_items = len(entries)
|
||||
total_cost = round(sum(e.total_cost for e in entries if e.total_cost is not None), 2)
|
||||
@@ -159,7 +172,7 @@ def waste_summary(
|
||||
for e in entries:
|
||||
r = e.reason
|
||||
if r not in by_reason:
|
||||
by_reason[r] = {"reason": r, "label": REASON_LABELS.get(r, r), "count": 0, "total_cost": 0.0}
|
||||
by_reason[r] = {"reason": r, "count": 0, "total_cost": 0.0}
|
||||
by_reason[r]["count"] += 1
|
||||
by_reason[r]["total_cost"] += e.total_cost or 0.0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user