# model.py def score_opportunity(data): # Stage weights for scoring stage_weight = { "Prospecting": 10, "Qualified": 20, "Proposal": 30, "Proposal/Price Quote": 35, "Negotiation": 40, "Closed Won": 50, "Closed Lost": 0 } lead_score = data.get("lead_score", 0) email_score = min(10, data.get("emails_last_7_days", 0)) * 2 # up to 20 meeting_score = min(5, data.get("meetings_last_30_days", 0)) * 5 # up to 25 amount_score = min(data.get("amount", 0) / 1000, 25) # up to 25 stage_score = stage_weight.get(data.get("stage"), 0) # Total Score Calculation total_score = lead_score * 0.25 + email_score + meeting_score + amount_score + stage_score total_score = round(min(total_score, 100)) # Confidence (0.0 to 1.0) confidence = round( min(1.0, ( (lead_score / 100) * 0.5 + min(1, data.get("emails_last_7_days", 0) / 10) * 0.25 + min(1, data.get("meetings_last_30_days", 0) / 5) * 0.25 )), 2 ) # Risk level and AI recommendation if total_score >= 80: risk = "Low" recommendation = "🔥 Strong lead. Schedule final meeting or send proposal." elif total_score >= 60: risk = "Medium" recommendation = "🗓️ Schedule another meeting before sending proposal." elif total_score >= 40: risk = "High" recommendation = "📞 Reconnect with lead. Increase engagement." else: risk = "Very High" recommendation = "⚠️ Low potential. Reassess or de-prioritize." return { "score": total_score, "confidence": confidence, "risk": risk, "recommendation": recommendation }