feat: 商业核算+个人库存+认证优化 #23
@@ -1242,17 +1242,32 @@ def list_projects():
|
||||
conn = get_db()
|
||||
rows = conn.execute("SELECT * FROM profit_projects ORDER BY id DESC").fetchall()
|
||||
conn.close()
|
||||
return [{ **dict(r), "ingredients": json.loads(r["ingredients"]) } for r in rows]
|
||||
result = []
|
||||
for r in rows:
|
||||
d = dict(r)
|
||||
d["ingredients"] = json.loads(r["ingredients"])
|
||||
try:
|
||||
extra = json.loads(r["pricing"]) if r["pricing"] else {}
|
||||
if isinstance(extra, dict):
|
||||
d.update(extra)
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
pass
|
||||
result.append(d)
|
||||
return result
|
||||
|
||||
|
||||
@app.post("/api/projects", status_code=201)
|
||||
def create_project(body: dict, user=Depends(require_role("admin", "senior_editor"))):
|
||||
conn = get_db()
|
||||
c = conn.cursor()
|
||||
extra = {}
|
||||
for k in ("selling_price", "packaging_cost", "labor_cost", "other_cost", "quantity"):
|
||||
if k in body:
|
||||
extra[k] = body[k]
|
||||
c.execute(
|
||||
"INSERT INTO profit_projects (name, ingredients, pricing, note, created_by) VALUES (?, ?, ?, ?, ?)",
|
||||
(body["name"], json.dumps(body.get("ingredients", []), ensure_ascii=False),
|
||||
body.get("pricing", 0), body.get("note", ""), user["id"])
|
||||
json.dumps(extra) if extra else '{}', body.get("note", ""), user["id"])
|
||||
)
|
||||
conn.commit()
|
||||
pid = c.lastrowid
|
||||
@@ -1272,6 +1287,14 @@ def update_project(pid: int, body: dict, user=Depends(require_role("admin", "sen
|
||||
conn.execute("UPDATE profit_projects SET pricing = ? WHERE id = ?", (body["pricing"], pid))
|
||||
if "note" in body:
|
||||
conn.execute("UPDATE profit_projects SET note = ? WHERE id = ?", (body["note"], pid))
|
||||
# Store extra cost fields in pricing as JSON
|
||||
extra = {}
|
||||
for k in ("selling_price", "packaging_cost", "labor_cost", "other_cost", "quantity"):
|
||||
if k in body:
|
||||
extra[k] = body[k]
|
||||
if extra:
|
||||
conn.execute("UPDATE profit_projects SET pricing = ? WHERE id = ?",
|
||||
(json.dumps(extra, ensure_ascii=False), pid))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return {"ok": True}
|
||||
|
||||
Reference in New Issue
Block a user