serviceadvisor / car_advisor /cost_estimator.py
viswanani's picture
Upload 22 files
1c7bc31 verified
import yaml
from typing import Dict, Any
def estimate_costs(fused: Dict[str, Any], parts_yaml_path: str, top_k: int = 4) -> Dict[str, Any]:
cfg = yaml.safe_load(open(parts_yaml_path, "r", encoding="utf-8"))
labor_rate = float(cfg.get("labor_rate_per_hour", 1200.0))
diagnostic_fee = float(cfg.get("diagnostic_fee", 500.0))
parts_cfg = cfg.get("parts", {})
items = []
total = 0.0
for i, (label, rec) in enumerate(fused.items()):
if i >= top_k:
break
part_info = parts_cfg.get(label, {})
hours = float(part_info.get("hours", 1.0))
parts_list = part_info.get("parts_list", [])
parts_cost = sum(float(p.get("cost", 0.0)) for p in parts_list)
labor_cost = hours * labor_rate
line_total = parts_cost + labor_cost
items.append({
"issue": label,
"probability": round(float(rec["prob"]), 3),
"severity": int(rec.get("severity", 3)),
"labor_hours": hours,
"labor_cost": round(labor_cost, 2),
"parts": parts_list,
"parts_cost": round(parts_cost, 2),
"line_total": round(line_total, 2)
})
total += line_total
if not items:
items.append({
"issue": "diagnostic_only",
"probability": 0.3,
"severity": 1,
"labor_hours": 0.0,
"labor_cost": 0.0,
"parts": [],
"parts_cost": 0.0,
"line_total": diagnostic_fee
})
total += diagnostic_fee
tax = round(0.18 * total, 2)
grand = round(total + tax, 2)
return {"items": items, "subtotal": round(total, 2), "tax": tax, "grand_total": grand}