"""Material list generator from estimates.""" import json, sys, os sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) from lib.db import get_db, row_to_dict, rows_to_list def generate(estimate_id): conn = get_db() est = row_to_dict(conn.execute("SELECT * FROM estimates WHERE id=?", (estimate_id,)).fetchone()) if not est: conn.close(); return {"error": "estimate not found"} items = rows_to_list(conn.execute("SELECT * FROM line_items WHERE estimate_id=? AND category='materials'", (estimate_id,)).fetchall()) job = row_to_dict(conn.execute("SELECT * FROM jobs WHERE id=?", (est["job_id"],)).fetchone()) conn.close() materials = [] total = 0 for item in items: materials.append({ "item": item["description"], "quantity": item["quantity"], "unit": item["unit"], "est_cost": round(item["total"], 2) }) total += item["total"] return { "job": job.get("description", "") if job else "", "materials": materials, "total_materials_cost": round(total, 2), "items_count": len(materials) } if __name__ == "__main__": from lib.db import init_db; init_db() if len(sys.argv) < 2: print(json.dumps({"error": "usage: material_list.py "})) else: print(json.dumps(generate(sys.argv[1]), indent=2))