Update report

Report now has a 'total' column next to the 'inventory' column.

Also, for items that are not empty yet we use the current time instead of the most recent sale to estimate time until empty.
This commit is contained in:
Paul Brinkmeier 2023-08-18 00:14:11 +02:00
parent 53eb6a9fba
commit 1a34486a84
2 changed files with 45 additions and 31 deletions

View File

@ -5,33 +5,45 @@ most_recent_sales AS (
FROM garfield.snack_sales_log
ORDER BY inventory_line ASC, snack_sales_log_timestamp DESC
),
enhanced_overview AS (
enhanced_overview1 AS (
SELECT
inventory_items.item_id,
inventory_items.item_barcode,
inventory_items.name,
units_left,
inventory_items.sales_units,
correction_delta,
location_name,
location,
CASE
WHEN snack_sales_log_id IS NULL THEN 0
ELSE sales / (EXTRACT(EPOCH FROM most_recent_sale) - EXTRACT(EPOCH FROM bought)) * 24 * 3600
ELSE sales / (EXTRACT(EPOCH FROM (
CASE
WHEN units_left <= 0 THEN most_recent_sale
ELSE NOW()
END
)) - EXTRACT(EPOCH FROM bought)) * 24 * 3600
END AS per_day
FROM garfield.inventory_item_overview
LEFT JOIN garfield.inventory_items USING (item_id)
LEFT JOIN most_recent_sales ON item_id = inventory_line
)
),
enhanced_overview2 AS (
SELECT
*,
CASE
WHEN per_day = 0 THEN NULL
ELSE GREATEST(0, units_left / per_day)
END AS days_left,
END AS days_left
FROM enhanced_overview1
)
SELECT
*,
CASE
WHEN per_day = 0 THEN NULL
ELSE GREATEST(0, (60 - GREATEST(0, units_left / per_day)) * per_day)
WHEN days_left IS NULL THEN NULL
ELSE GREATEST(0, (60 - days_left) * per_day)
END AS for_two_months
FROM enhanced_overview
FROM enhanced_overview2
WHERE (%(location_id)s IS NULL OR location = %(location_id)s)
ORDER BY days_left ASC, per_day DESC

View File

@ -7,6 +7,7 @@
<th>Barcode</th>
<th>Name</th>
<th>Inventar</th>
<th>Gesamt</th>
<th>Raum</th>
<th>Verbrauch [1/d]</th>
<th>ETUE [d]</th>
@ -17,7 +18,8 @@
<td><a href="/inventory/item/{{ item.item_id }}">{{ item.item_id }}</a></td>
<td><code>{{ item.item_barcode }}</code></td>
<td>{{ item.name }}</td>
<td class="--align-right">{{ item.units_left }}</td>
<td class="--align-right">{{ item.units_left + item.correction_delta }}</td>
<td class="--align-right">{{ item.sales_units }}</td>
<td>{{ item.location_name }}</td>
<td class="--align-right">{{ item.per_day|round(2) }}</td>
<td class="--align-right">{% if item.days_left != None %}{{ item.days_left|round(1) }}{% endif %}</td>