Add /inventory/report route
This commit is contained in:
parent
7c1e13e32e
commit
3ad649a402
37
py/jon/db/get_inventory_report.sql
Normal file
37
py/jon/db/get_inventory_report.sql
Normal file
@ -0,0 +1,37 @@
|
||||
WITH
|
||||
most_recent_sales AS (
|
||||
SELECT DISTINCT ON (inventory_line)
|
||||
inventory_line, snack_sales_log_id, snack_sales_log_timestamp AS most_recent_sale
|
||||
FROM garfield.snack_sales_log
|
||||
ORDER BY inventory_line ASC, snack_sales_log_timestamp DESC
|
||||
),
|
||||
enhanced_overview AS (
|
||||
SELECT
|
||||
inventory_items.item_id,
|
||||
inventory_items.item_barcode,
|
||||
inventory_items.name,
|
||||
units_left,
|
||||
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
|
||||
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
|
||||
)
|
||||
|
||||
SELECT
|
||||
*,
|
||||
CASE
|
||||
WHEN per_day = 0 THEN NULL
|
||||
ELSE GREATEST(0, units_left / per_day)
|
||||
END AS days_left,
|
||||
CASE
|
||||
WHEN per_day = 0 THEN NULL
|
||||
ELSE GREATEST(0, (60 - GREATEST(0, units_left / per_day)) * per_day)
|
||||
END AS for_two_months
|
||||
FROM enhanced_overview
|
||||
WHERE (%(location_id)s IS NULL OR location = %(location_id)s)
|
||||
ORDER BY days_left ASC, per_day DESC
|
@ -6,7 +6,7 @@ from . import db
|
||||
bp = Blueprint("inventory", __name__, url_prefix="/inventory")
|
||||
|
||||
|
||||
@bp.route("/")
|
||||
@bp.get("/")
|
||||
def index():
|
||||
location = session.get("location", None)
|
||||
items = db.run_query("get_inventory_overview.sql", {
|
||||
@ -18,6 +18,18 @@ def index():
|
||||
})
|
||||
|
||||
|
||||
@bp.get("/report")
|
||||
def read_report():
|
||||
location = session.get("location", None)
|
||||
items = db.run_query("get_inventory_report.sql", {
|
||||
"location_id": None if location is None else location["location_id"]
|
||||
}).fetchall()
|
||||
|
||||
return render_template("inventory/read_report.html", **{
|
||||
"items": items
|
||||
})
|
||||
|
||||
|
||||
@bp.get("/item/<item_id>")
|
||||
def read_item(item_id: int):
|
||||
item = db.run_query("get_item_by_id.sql", {
|
||||
|
28
py/jon/templates/inventory/read_report.html
Normal file
28
py/jon/templates/inventory/read_report.html
Normal file
@ -0,0 +1,28 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<table>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Barcode</th>
|
||||
<th>Name</th>
|
||||
<th>Inventar</th>
|
||||
<th>Raum</th>
|
||||
<th>Verbrauch [1/d]</th>
|
||||
<th>ETUE [d]</th>
|
||||
<th>Für 2m</th>
|
||||
</tr>
|
||||
{% for item in items %}
|
||||
<tr>
|
||||
<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>{{ 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>
|
||||
<td class="--align-right">{% if item.for_two_months %}{{ item.for_two_months|round(1) }}{% endif %}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
{% endblock %}
|
Loading…
x
Reference in New Issue
Block a user