feat(inventory): dynamic tab badges with real tenant data

- Add /pos/api/inventory/stats endpoint returning counts per tab
- Replace hardcoded badge numbers (4,817, 14, 3, 23) with dynamic values
- Frontend auto-fetches stats on page load and updates badges
This commit is contained in:
2026-05-18 04:31:00 +00:00
parent 9ad624d26c
commit 24cdd71262
2 changed files with 82 additions and 7 deletions

View File

@@ -809,6 +809,52 @@ def physical_count_approve():
})
# ─── Stats Summary ─────────────────────────────
@inventory_bp.route('/stats', methods=['GET'])
@require_auth('inventory.view')
def api_inventory_stats():
"""Get inventory summary counts for dashboard badges."""
conn = get_tenant_conn(g.tenant_id)
cur = conn.cursor()
branch_id = getattr(g, 'branch_id', None)
# Stock count
cur.execute("SELECT COUNT(*) FROM inventory WHERE is_active = true AND (branch_id = %s OR %s IS NULL)", (branch_id, branch_id))
stock = cur.fetchone()[0]
# Operations counts by type
cur.execute("""
SELECT operation_type, COUNT(*)
FROM inventory_operations
WHERE (branch_id = %s OR %s IS NULL)
GROUP BY operation_type
""", (branch_id, branch_id))
op_counts = {row[0]: row[1] for row in cur.fetchall()}
# Physical counts
cur.execute("SELECT COUNT(*) FROM physical_counts WHERE (branch_id = %s OR %s IS NULL)", (branch_id, branch_id))
physical = cur.fetchone()[0]
cur.close()
conn.close()
# Alerts (reuse existing function)
conn2 = get_tenant_conn(g.tenant_id)
alerts_list = get_alerts(conn2, branch_id)
conn2.close()
return jsonify({
'stock': stock,
'entradas': op_counts.get('PURCHASE', 0),
'salidas': op_counts.get('SALE', 0),
'traspasos': op_counts.get('TRANSFER', 0),
'ajustes': op_counts.get('ADJUST', 0),
'conteos': physical,
'alertas': len(alerts_list)
})
# ─── Alerts and History ────────────────────────
@inventory_bp.route('/alerts', methods=['GET'])