Stats module reads the full sales history from Storage and renders summary cards and a top-products table on the statistics view. You can filter the data by the current week, the current month, or all time using the period filter buttons.
Properties
| Property | Type | Description |
|---|---|---|
currentPeriod | string | The active time filter. One of 'week', 'month', or 'all'. Defaults to 'week'. |
salesData | array | The full unfiltered sales array loaded from Storage during the last render() call. |
Methods
Stats.init()
Binds the period filter buttons. Called once by App.init(). Note: render() is not called here — it is triggered when the stats view is first activated via App.refreshView('stats').
Stats.bindEvents()
Attaches click listeners to all .filter-btn elements. Each listener reads element.dataset.period and calls setPeriod(period).
Stats.setPeriod(period)
Updates currentPeriod, toggles the active class on the corresponding .filter-btn, and calls render().
The time period to filter by. One of
'week', 'month', or 'all'.Stats.render()
Loads all sales from Storage.getSales(), stores them in salesData, and filters them with filterByPeriod(). If the filtered set is empty, renders an empty state message. Otherwise calculates and renders:
- Total ventas — count of sales in the period.
- Ingresos — total revenue.
- Efectivo — revenue from cash sales (
tipo === 'efectivo'). - Fiado — revenue from credit sales (
tipo === 'fiado'). - Productos más vendidos — a table of the top 10 products by unit quantity, via
getTopProducts().
Stats.filterByPeriod(sales)
Filters a sales array based on currentPeriod.
| Period | Filter rule |
|---|---|
'week' | Sales with fecha >= start of the current week (Sunday at midnight). |
'month' | Sales with fecha >= first day of the current month at midnight. |
'all' | No filter applied — returns the full array. |
The full array of sale objects to filter.
A new array containing only the sales that fall within the current period.
Stats.getTopProducts(sales)
Aggregates product data across all provided sales. Groups by product name (nombre), summing cantidad (units sold) and ingresos (revenue = precio × cantidad) for each product. Returns the top 10 products sorted by cantidad descending.
The filtered sales array to aggregate.
Array of up to 10 objects, each with:
nombre(string) — product name.cantidad(number) — total units sold.ingresos(number) — total revenue for that product.
Stats.escapeHtml(text)
Sanitizes a string to prevent XSS when injecting user-provided content into HTML. Creates a temporary <div> element, assigns the text as textContent, and returns innerHTML.
The raw string to sanitize.
The HTML-escaped string safe for insertion into the DOM.