Compare commits
29
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ab53b31ec0 | ||
|
|
a201ce00b6 | ||
|
|
630a0435a2 | ||
|
|
61cf7696a5 | ||
|
|
3d9ad49020 | ||
|
|
1b6d0c40f3 | ||
|
|
235ef32efd | ||
|
|
85598adaab | ||
|
|
70934a2f85 | ||
|
|
5cec28cad1 | ||
|
|
d57e66c033 | ||
|
|
d9114d21c7 | ||
|
|
2108ef2418 | ||
|
|
a2d1d42362 | ||
|
|
32bcb1b18a | ||
|
|
1e4aadee06 | ||
|
|
a82e75999b | ||
|
|
7b95b69c7b | ||
|
|
e0aef726bd | ||
|
|
7707a9da6d | ||
|
|
ca2e2e2e4a | ||
|
|
208cc54e53 | ||
|
|
f7dd572fe1 | ||
|
|
69dba202d0 | ||
|
|
3d6bd7bac5 | ||
|
|
0f9be13e31 | ||
|
|
be86248cce | ||
|
|
01d060cf5f | ||
|
|
8c5b34073d |
@@ -4,6 +4,11 @@
|
||||
|
||||
## Setup
|
||||
|
||||
`jon` is a Python WSGI application written using Flask.
|
||||
This means you'll have to install a bunch of Python packages to get up and running.
|
||||
|
||||
### Dependencies
|
||||
|
||||
```
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
@@ -11,6 +16,14 @@ pip install -r requirements.txt
|
||||
You should probably use a virtualenv for that.
|
||||
I develop `jon` using Python 3.10 but it should work with older versions as well.
|
||||
|
||||
#### Arch Linux
|
||||
|
||||
If you're on Arch, these packages are required for running the server:
|
||||
|
||||
```
|
||||
python python-flask python-flask-login python-psycopg2
|
||||
```
|
||||
|
||||
### Building Frontend JS
|
||||
|
||||
Most of jon works without JS but there are some features that require it.
|
||||
@@ -46,9 +59,13 @@ ssh -nNTvL 5432:fsmi-db.fsmi.org:5432 fsmi-login.fsmi.uni-karlsruhe.de
|
||||
- [ ] etc.
|
||||
- [ ] Make it print nicely
|
||||
- [ ] Make it possible to edit entries
|
||||
- [ ] Fix unsafe client-side sessions, either:
|
||||
- [ ] Use `flask-session` for file-backed sessions
|
||||
- [ ] Use `flask-login` with a single user stored in memory
|
||||
- [ ] Improve project structure
|
||||
- [ ] Use `flask.flash` for error messages
|
||||
- [x] Implement item and snack entry as Elm application
|
||||
- [x] Figure out/Add documentation about building `entry.js`
|
||||
- [ ] Clean up the code a little and add some comments
|
||||
- [ ] Needs good documentation for maintainability
|
||||
- [ ] Use cool new function for deactivating items
|
||||
|
||||
+16
-3
@@ -9,7 +9,15 @@ import Select
|
||||
|
||||
type Tax = Net | Gross
|
||||
|
||||
ctShow ct = case ct of
|
||||
-- Duplicated from Entry.elm but too lazy to sandwich this out
|
||||
type alias TaxGroup =
|
||||
{ id : Int
|
||||
, description : String
|
||||
, percentage : Float
|
||||
}
|
||||
|
||||
showTax : Tax -> String
|
||||
showTax tax = case tax of
|
||||
Gross -> "Brutto"
|
||||
Net -> "Netto"
|
||||
|
||||
@@ -19,11 +27,13 @@ type alias Model =
|
||||
, bundleSize : NumberInput.Model Int
|
||||
}
|
||||
|
||||
init : Float -> Model
|
||||
init bundlePrice = Model
|
||||
(Select.init ctShow ctShow Net [Net, Gross])
|
||||
(Select.init showTax showTax Net [Net, Gross])
|
||||
(NumberInput.fromFloat bundlePrice)
|
||||
(NumberInput.fromInt 1)
|
||||
|
||||
getResult : Model -> TaxGroup -> Maybe Float
|
||||
getResult model taxGroup =
|
||||
case (NumberInput.get model.bundlePrice, NumberInput.get model.bundleSize) of
|
||||
(Just bundlePrice, Just bundleSize) ->
|
||||
@@ -41,6 +51,7 @@ type Msg
|
||||
| SetBundlePrice String
|
||||
| SetBundleSize String
|
||||
|
||||
update : Msg -> Model -> Model
|
||||
update msg model = case msg of
|
||||
SetTax key ->
|
||||
{ model | tax = Select.update key model.tax }
|
||||
@@ -49,6 +60,7 @@ update msg model = case msg of
|
||||
SetBundleSize str ->
|
||||
{ model | bundleSize = NumberInput.update str model.bundleSize }
|
||||
|
||||
view : Model -> TaxGroup -> Html Msg
|
||||
view model taxGroup =
|
||||
let
|
||||
mainPart =
|
||||
@@ -112,4 +124,5 @@ view model taxGroup =
|
||||
]
|
||||
]
|
||||
|
||||
roundTo places x = toFloat (round <| x * 10 ^ places) / 10 ^ places
|
||||
roundTo : Int -> Float -> Float
|
||||
roundTo places x = toFloat (round <| x * 10 ^ toFloat places) / 10 ^ toFloat places
|
||||
|
||||
@@ -14,6 +14,50 @@ import Calculator
|
||||
import NumberInput
|
||||
import Select
|
||||
|
||||
{-
|
||||
Elm forces us to use the Elm architecture:
|
||||
|
||||
┌──────┐
|
||||
┌─────Model──► view ├──Html───────┐
|
||||
│ └──────┘ │
|
||||
│ │
|
||||
┌┴─────────────────────────────────▼┐
|
||||
│ Elm runtime │
|
||||
└▲─────────────────────────────────┬┘
|
||||
│ │
|
||||
│ ┌──────┐ │
|
||||
└─Model+Cmd──┤update◄──Msg+Model──┘
|
||||
└──────┘
|
||||
|
||||
This architecture is similar to what React does but its implementation
|
||||
in Elm is a bit special since it's purely functional and side effects
|
||||
are isolated into the runtime system.
|
||||
|
||||
An Elm component is usually centered around two types, Model and Msg.
|
||||
Model contains all data the application is concerned with, including the state
|
||||
of UI elements. Msg encodes all updates to Model that the application supports.
|
||||
|
||||
In addition to Msg and Model, we have to provide two functions, view and update.
|
||||
|
||||
view : Model -> Html Msg
|
||||
update : Msg -> Model -> (Model, Cmd Msg)
|
||||
|
||||
view maps a Model to a DOM tree. Events in this DOM tree create Msg values.
|
||||
update maps a Msg and a Model to a new Model. In addition, update can create
|
||||
a command. Commands are used to make the runtime do side effects, which in
|
||||
turn create new Msg values.
|
||||
|
||||
For example, we have a SetSearchTerm message which simply updates the searchTerm
|
||||
property in the model. This message is triggered every time the search box input
|
||||
is changed. Submitting the search box form triggers a SubmitSearch event.
|
||||
This event leaves the model unchanged but issues a command that sends the search
|
||||
term to a JSON endpoint. When the request successfully resolves, the runtime
|
||||
triggers a ReceiveSearchResults messages which updates the list of search results
|
||||
in the model.
|
||||
|
||||
See Calculator.elm for a simpler example of this architecture.
|
||||
-}
|
||||
|
||||
main = Browser.element
|
||||
{ init = \globals ->
|
||||
( Context globals <| ItemSearch { searchTerm = "", searchResults = [] }
|
||||
|
||||
@@ -58,3 +58,56 @@ FROM garfield.inventory_items
|
||||
) m USING (item_id)
|
||||
ORDER BY inventory_items.name;
|
||||
|
||||
-- How many *other* active inventory lines exist with the same barcode in the same location that are newer?
|
||||
CREATE TEMPORARY VIEW more_recent_inventory_lines_with_same_barcode AS
|
||||
SELECT
|
||||
a.item_id,
|
||||
-- It's important not to count(*) here because item_id is NULL
|
||||
-- when no other inventory lines exist.
|
||||
count(b.item_id) AS other_lines_count
|
||||
FROM garfield.inventory_items AS a
|
||||
LEFT JOIN garfield.inventory_items AS b
|
||||
ON a.item_barcode = b.item_barcode
|
||||
AND a.item_id != b.item_id
|
||||
AND a.location = b.location
|
||||
AND b.available
|
||||
AND b.bought > a.bought
|
||||
GROUP BY a.item_id;
|
||||
|
||||
-- We need to create this table so that we can put an index on it
|
||||
-- Otherwise the join in the consumption graph query becomes much slower
|
||||
-- Perhaps it would be nicer to use a materialized view instead
|
||||
DROP TABLE IF EXISTS last_n_days;
|
||||
CREATE TEMPORARY TABLE last_n_days AS (
|
||||
SELECT
|
||||
generate_series(now() - interval '14 days', now(), interval '1 day')::date AS sale_date
|
||||
);
|
||||
CREATE UNIQUE INDEX last_n_days_sale_date ON last_n_days (sale_date);
|
||||
|
||||
-- Get an array of how often items were sold over the last 14 days
|
||||
CREATE TEMPORARY VIEW inventory_last_n_days_sales AS
|
||||
WITH
|
||||
sales_by_date AS (
|
||||
SELECT
|
||||
inventory_line AS item_id,
|
||||
date_trunc('day', snack_sales_log_timestamp AT TIME ZONE 'UTC+1') AS sale_date,
|
||||
count(*)::int AS sales
|
||||
FROM garfield.snack_sales_log
|
||||
GROUP BY item_id, sale_date
|
||||
),
|
||||
beeg AS (
|
||||
SELECT item_id, sale_date, count(snack_sales_log_timestamp)::int AS sales
|
||||
FROM garfield.inventory_items
|
||||
CROSS JOIN last_n_days
|
||||
LEFT JOIN garfield.snack_sales_log
|
||||
ON inventory_line = item_id
|
||||
-- snack_sales_log has an index on snack_sales_log_timestamp to speed up this query
|
||||
-- If that index doesn't exist the query takes much longer.
|
||||
AND sale_date = date_trunc('day', snack_sales_log_timestamp AT TIME ZONE 'UTC+1')
|
||||
WHERE available
|
||||
GROUP BY item_id, sale_date
|
||||
ORDER BY item_id, sale_date
|
||||
)
|
||||
SELECT item_id, array_agg(sales) AS last_n_days_sales
|
||||
FROM beeg
|
||||
GROUP BY item_id
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
SELECT
|
||||
*
|
||||
FROM all_inventory_item_overview
|
||||
LEFT JOIN more_recent_inventory_lines_with_same_barcode USING (item_id)
|
||||
LEFT JOIN inventory_last_n_days_sales USING (item_id)
|
||||
WHERE (%(location_id)s IS NULL OR location = %(location_id)s)
|
||||
AND available
|
||||
ORDER BY
|
||||
|
||||
@@ -74,3 +74,7 @@ th {
|
||||
details {
|
||||
font-size: 0.8em;
|
||||
}
|
||||
.consumption-graph {
|
||||
display: block;
|
||||
height: 1em;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,34 @@
|
||||
{% macro consumption_graph_svg(values) -%}
|
||||
{% set stroke_width = 8 %}
|
||||
{% set width = 300 %}
|
||||
{% set height = 100 %}
|
||||
{% set padding = 4 %}
|
||||
{% set dx = (width - 2 * padding) / ((values | length) + 1) %}
|
||||
{% set dy = (height - 2 * padding) / ((values + [1]) | max) %}
|
||||
<svg viewBox="0 0 {{ width }} {{ height }}" role="img" class="consumption-graph">
|
||||
<polyline
|
||||
points="
|
||||
{% for value in values %}
|
||||
{{ padding + loop.index * dx }}, {{ height - padding - value * dy }}
|
||||
{% endfor %}
|
||||
"
|
||||
stroke="green"
|
||||
stroke-width="{{ stroke_width }}"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
fill="none"
|
||||
>
|
||||
</svg>
|
||||
{% endmacro -%}
|
||||
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<table>
|
||||
<tr>
|
||||
<th>Stat</th>
|
||||
<th>ID</th>
|
||||
<th>Graph</th>
|
||||
<th>Barcode</th>
|
||||
<th>Name</th>
|
||||
<th>Preis (Netto)</th>
|
||||
@@ -17,7 +42,12 @@
|
||||
</tr>
|
||||
{% for item in items %}
|
||||
<tr>
|
||||
<td>
|
||||
{% if item.units_left == 0 %}<span title="Leerer aktiver Inventareintrag">🅾️</span>{% endif %}
|
||||
{% if item.other_lines_count != 0 %}<span title="{% if item.other_lines_count == 1 %}Neuerer Eintrag mit demselben Barcode ist aktiv{% else %}{{ item.other_lines_count }} Einträge mit demselben Barcode sind aktiv{% endif %}">🔄</span>{% endif %}
|
||||
</td>
|
||||
<td><a href="/inventory/item/{{ item.item_id }}">{{ item.item_id }}</a></td>
|
||||
<td>{{ consumption_graph_svg(item.last_n_days_sales) }}</td>
|
||||
<td><code>{{ item.item_barcode }}</code></td>
|
||||
<td>{{ item.name }}</td>
|
||||
<td class="--align-right">{{ format_currency(item.unit_price) }}</td>
|
||||
|
||||
Reference in New Issue
Block a user