diff --git a/slack_reporter.py b/slack_reporter.py index 5d84306..4956b4a 100644 --- a/slack_reporter.py +++ b/slack_reporter.py @@ -91,11 +91,15 @@ def build_and_send(collected: list, dry_run: bool, prev_month_metricas: dict = N margen_leads_ppl = 0.0 pct_leads_ppl = 0.0 else: - ing_sumatorio = round(sum( - sum(d.get("ingreso", 0) for d in _parse_metricas(item["campaign"].get("metricas_diarias", "{}")).values()) - for item in fco - ), 2) - margen_sumatorio = round(ing_sumatorio - inv_total, 2) + ing_sumatorio = 0.0 + coste_sumatorio = 0.0 + for item in fco: + for d in _parse_metricas(item["campaign"].get("metricas_diarias", "{}")).values(): + 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) 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 @@ -168,13 +172,24 @@ def build_and_send(collected: list, dry_run: bool, prev_month_metricas: dict = N margin_table_block = None 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): coste = daily_totals[d]["coste"] ing_sum = daily_totals[d]["ingreso_sum"] - pct_sum = round((ing_sum - coste) / ing_sum * 100, 1) if ing_sum > 0 else 0.0 - s_sum = ("+" if pct_sum >= 0 else "") + f"{pct_sum:.1f}%" - rows.append(f"{d:02d} {s_sum:>7}") + margen = ing_sum - coste + pct = round(margen / ing_sum * 100, 1) if ing_sum > 0 else 0.0 + 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 = { "type": "section", "text": {