diff --git a/routes_admin.py b/routes_admin.py index 8b83d46..0434099 100644 --- a/routes_admin.py +++ b/routes_admin.py @@ -516,4 +516,83 @@ def register_admin_routes(app): dias_en_periodo=dias_en_periodo, data_por_dia=data_por_dia, totales_mes=totales_mes, - dias_activos=dias_activos) \ No newline at end of file + dias_activos=dias_activos) + + @app.route('/admin/reportes/modulo//comisiones') + @admin_required + def report_modulo_comisiones(modulo_id): + mes_actual = date.today().month + anio_actual = date.today().year + + conn = get_db_connection() + c = conn.cursor() + + c.execute("SELECT name FROM modulos WHERE id = ?", (modulo_id,)) + modulo_info = c.fetchone() + if not modulo_info: + conn.close() + flash("Módulo no encontrado.", "danger") + return redirect(url_for('admin_reportes_index')) + modulo_name = modulo_info[0] + + # Fetch rendiciones with commission calculations for this module and month + c.execute(''' + SELECT r.id, strftime('%d', r.fecha) as dia, + w.id, w.name, w.tipo, r.worker_comision, + cw.id, cw.name, cw.tipo, r.companion_comision, + COALESCE((SELECT SUM(cantidad * comision_historica) FROM rendicion_items WHERE rendicion_id = r.id), 0) as total_comision + FROM rendiciones r + JOIN workers w ON r.worker_id = w.id + LEFT JOIN workers cw ON r.companion_id = cw.id + WHERE r.modulo_id = ? AND strftime('%m', r.fecha) = ? AND strftime('%Y', r.fecha) = ? + ORDER BY r.fecha ASC + ''', (modulo_id, f'{mes_actual:02}', str(anio_actual))) + + rendiciones = c.fetchall() + conn.close() + + workers_data = {} + + for row in rendiciones: + r_id, dia, w_id, w_name, w_tipo, w_com, c_id, c_name, c_tipo, c_com, total_com = row + + w_share = 0 + c_share = 0 + + # Split logic + if w_com and c_com: + w_share = total_com / 2 + c_share = total_com / 2 + elif w_com: + w_share = total_com + elif c_com: + c_share = total_com + + # Process Titular Worker + if w_id not in workers_data: + workers_data[w_id] = {'name': w_name, 'tipo': w_tipo, 'dias': {}, 'total': 0, 'enabled': bool(w_com)} + else: + if w_com: workers_data[w_id]['enabled'] = True + + workers_data[w_id]['dias'][dia] = workers_data[w_id]['dias'].get(dia, 0) + w_share + workers_data[w_id]['total'] += w_share + + # Process Companion (if any) + if c_id: + if c_id not in workers_data: + workers_data[c_id] = {'name': c_name, 'tipo': c_tipo, 'dias': {}, 'total': 0, 'enabled': bool(c_com)} + else: + if c_com: workers_data[c_id]['enabled'] = True + + workers_data[c_id]['dias'][dia] = workers_data[c_id]['dias'].get(dia, 0) + c_share + workers_data[c_id]['total'] += c_share + + # Sort alphabetically so the table doesn't shuffle randomly + workers_data = dict(sorted(workers_data.items(), key=lambda item: item[1]['name'])) + dias_en_periodo = [f'{d:02}' for d in range(1, 32)] + + return render_template('admin_report_comisiones.html', + modulo_name=modulo_name, + mes_nombre=f'{mes_actual:02}/{anio_actual}', + workers_data=workers_data, + dias_en_periodo=dias_en_periodo) \ No newline at end of file diff --git a/templates/admin_report_comisiones.html b/templates/admin_report_comisiones.html new file mode 100644 index 0000000..4090268 --- /dev/null +++ b/templates/admin_report_comisiones.html @@ -0,0 +1,85 @@ +{% extends "macros/base.html" %} + +{% block title %}Reporte: Comisiones - {{ modulo_name }}{% endblock %} + +{% block styles %} + +{% endblock %} + +{% block content %} +
+
+ + Volver al Menú + +

Reporte de Comisiones

+
+
+
{{ modulo_name }}
+
Período: {{ mes_nombre }}
+
+
+ +
+
+ {% if workers_data %} +
+ + + + + {% for w_id, data in workers_data.items() %} + + {% endfor %} + + + + {% for dia in dias_en_periodo %} + + + {% for w_id, data in workers_data.items() %} + {% set comision = data.dias.get(dia, 0) %} + + {% endfor %} + + {% endfor %} + + + + + {% for w_id, data in workers_data.items() %} + + {% endfor %} + + +
Día + {{ data.name }}
+ {{ data.tipo }} +
+ + {% if data.enabled %}(Comisión Activa){% else %}(Sin Comisión){% endif %} + +
{{ dia }} + {{ ("$" ~ "{:,.0f}".format(comision).replace(',', '.')) if comision > 0 else "-" }} +
TOTAL + ${{ "{:,.0f}".format(data.total).replace(',', '.') }} +
+
+ {% else %} +
No hay trabajadores con rendiciones registradas para este módulo en el período actual.
+ {% endif %} +
+
+{% endblock %} \ No newline at end of file diff --git a/templates/macros/modals.html b/templates/macros/modals.html index 118b682..79ce5f2 100644 --- a/templates/macros/modals.html +++ b/templates/macros/modals.html @@ -482,7 +482,7 @@
Comisiones

Cálculo de comisiones generadas por los trabajadores en este módulo.

- + Generar Reporte