Fix margin consistency: use MetricasDiarias for sumatorio cost

Both the monthly summary and the daily table now derive coste
from MetricasDiarias entries, so totals match exactly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Jose Manuel 2026-05-18 12:46:07 +02:00
parent 624f5e484d
commit a489a08785

View File

@ -91,11 +91,15 @@ def build_and_send(collected: list, dry_run: bool, prev_month_metricas: dict = N
margen_leads_ppl = 0.0 margen_leads_ppl = 0.0
pct_leads_ppl = 0.0 pct_leads_ppl = 0.0
else: else:
ing_sumatorio = round(sum( ing_sumatorio = 0.0
sum(d.get("ingreso", 0) for d in _parse_metricas(item["campaign"].get("metricas_diarias", "{}")).values()) coste_sumatorio = 0.0
for item in fco for item in fco:
), 2) for d in _parse_metricas(item["campaign"].get("metricas_diarias", "{}")).values():
margen_sumatorio = round(ing_sumatorio - inv_total, 2) ing_sumatorio += d.get("ingreso", 0)
coste_sumatorio += d.get("coste", 0)
ing_sumatorio = round(ing_sumatorio, 2)
coste_sumatorio = round(coste_sumatorio, 2)
margen_sumatorio = round(ing_sumatorio - coste_sumatorio, 2)
margen_leads_ppl = round(ing_leads_ppl - inv_total, 2) margen_leads_ppl = round(ing_leads_ppl - inv_total, 2)
pct_sumatorio = round(margen_sumatorio / ing_sumatorio * 100, 1) if ing_sumatorio > 0 else 0.0 pct_sumatorio = round(margen_sumatorio / ing_sumatorio * 100, 1) if ing_sumatorio > 0 else 0.0
pct_leads_ppl = round(margen_leads_ppl / ing_leads_ppl * 100, 1) if ing_leads_ppl > 0 else 0.0 pct_leads_ppl = round(margen_leads_ppl / ing_leads_ppl * 100, 1) if ing_leads_ppl > 0 else 0.0
@ -168,13 +172,24 @@ def build_and_send(collected: list, dry_run: bool, prev_month_metricas: dict = N
margin_table_block = None margin_table_block = None
if daily_totals and not primer_dia_mes: if daily_totals and not primer_dia_mes:
rows = ["Día Margen"] rows = ["Día Margen € %"]
total_coste = total_ing = 0.0
for d in sorted(daily_totals): for d in sorted(daily_totals):
coste = daily_totals[d]["coste"] coste = daily_totals[d]["coste"]
ing_sum = daily_totals[d]["ingreso_sum"] ing_sum = daily_totals[d]["ingreso_sum"]
pct_sum = round((ing_sum - coste) / ing_sum * 100, 1) if ing_sum > 0 else 0.0 margen = ing_sum - coste
s_sum = ("+" if pct_sum >= 0 else "") + f"{pct_sum:.1f}%" pct = round(margen / ing_sum * 100, 1) if ing_sum > 0 else 0.0
rows.append(f"{d:02d} {s_sum:>7}") total_coste += coste
total_ing += ing_sum
s_eur = ("+" if margen >= 0 else "") + f"{margen:,.0f}".replace(",", ".")
s_pct = ("+" if pct >= 0 else "") + f"{pct:.1f}%"
rows.append(f"{d:02d} {s_eur:>9} {s_pct:>7}")
total_margen = total_ing - total_coste
total_pct = round(total_margen / total_ing * 100, 1) if total_ing > 0 else 0.0
s_tot_eur = ("+" if total_margen >= 0 else "") + f"{total_margen:,.0f}".replace(",", ".")
s_tot_pct = ("+" if total_pct >= 0 else "") + f"{total_pct:.1f}%"
rows.append("" * 24)
rows.append(f"TOT {s_tot_eur:>9} {s_tot_pct:>7}")
margin_table_block = { margin_table_block = {
"type": "section", "type": "section",
"text": { "text": {