From be53deb9ce7acfe98f6027f689c9101b3fbc18a3 Mon Sep 17 00:00:00 2001
From: Paul Brinkmeier <hallo@pbrinkmeier.de>
Date: Sun, 20 Aug 2023 19:54:49 +0200
Subject: [PATCH] Implement adding orders to the cart and viewing it

---
 frontend/Entry.elm                 |  8 ++---
 jon/entry.py                       | 55 ++++++++++++++++++++++++++----
 jon/static/entry.js                | 32 +++++++----------
 jon/templates/entry/index.html     | 38 ++++++++++++++-------
 jon/templates/entry/new-order.html | 16 +++++++++
 5 files changed, 107 insertions(+), 42 deletions(-)
 create mode 100644 jon/templates/entry/new-order.html

diff --git a/frontend/Entry.elm b/frontend/Entry.elm
index b512f26..503fe4e 100644
--- a/frontend/Entry.elm
+++ b/frontend/Entry.elm
@@ -210,19 +210,19 @@ view { globals, state } = case state of
         ]
       ]
   ItemEditor model ->
-    Html.form [ method "POST", action "/entry/add-to-cart" ]
+    Html.form [ method "POST" ]
       [ fieldset []
         [ legend [] [ text "Neuer Inventareintrag" ]
         , div [ class "form-input" ]
           [ label [ for "barcode" ] [ text "Barcode" ]
-          , input [ onInput SetBarcode, value model.barcode, disabled True, name "barcode", id "barcode" ] []
+          , input [ onInput SetBarcode, value model.barcode, name "barcode", id "barcode" ] []
           ]
         , div [ class "form-input" ]
           [ label [ for "name" ] [ text "Name" ]
           , input [ onInput SetName, value model.name, name "name", id "name" ] []
           ]
         , div [ class "form-input" ]
-          [ label [ for "sales-units" ] [ text "Stückzahl" ]
+          [ label [ for "sales-units" ] [ text "Eingekauft" ]
           , input [ onInput SetSalesUnits, value <| NumberInput.show model.salesUnits, name "sales-units", id "sales-units", type_ "number" ] []
           ]
         , div [ class "form-input" ]
@@ -264,7 +264,7 @@ view { globals, state } = case state of
             []
           , viewSetSuggestedPriceButton model
           ]
-        , button [] [ text "In den Warenkorb" ]
+        , button [] [ text "Auftrag anlegen" ]
         ]
       ]
 
diff --git a/jon/entry.py b/jon/entry.py
index 4ab298b..c3b6a8d 100644
--- a/jon/entry.py
+++ b/jon/entry.py
@@ -7,8 +7,45 @@ from . import db
 bp = Blueprint("entry", __name__, url_prefix="/entry")
 
 
-@bp.get("/")
+@bp.route("/", methods=["GET", "POST"])
 def index():
+    cart = session["cart"]
+    return render_template(
+        "entry/index.html",
+        cart=cart
+    )
+
+
+@bp.route("/new-order", methods=["GET", "POST"])
+def new_order():
+    if request.method == "POST":
+        try:
+            barcode = request.form["barcode"]
+            name = request.form["name"]
+            sales_units = int(request.form["sales-units"])
+            group_id = int(request.form["group"])
+            location_id = int(request.form["location"])
+            tax_group_id = int(request.form["tax-group"])
+            net_unit_price = float(request.form["net-unit-price"])
+            gross_unit_price = float(request.form["gross-unit-price"])
+        except Exception as e:
+            return f"Incomplete or mistyped form", 400
+        
+        cart = session.get("cart", default=[])
+        print(cart)
+        cart.append({
+            "barcode": barcode,
+            "name": name,
+            "sales_units": sales_units,
+            "group_id": group_id,
+            "location_id": location_id,
+            "tax_group_id": tax_group_id,
+            "net_unit_price": net_unit_price,
+            "gross_unit_price": gross_unit_price
+        })
+        session["cart"] = cart
+        return redirect("/entry")
+
     with db.run_query("entry/get_groups.sql") as cursor:
         groups = cursor.fetchall()
 
@@ -18,11 +55,17 @@ def index():
     with db.run_query("entry/get_tax_groups.sql") as cursor:
         tax_groups = cursor.fetchall()
 
-    return render_template("entry/index.html", **{
-        "locations": locations,
-        "groups": groups,
-        "tax_groups": tax_groups
-    })
+    return render_template(
+        "entry/new-order.html",
+        groups=groups,
+        locations=locations,
+        tax_groups=tax_groups
+    )
+
+
+
+# API routes for interactive JS stuff
+
 
 @bp.get("/api/search-items")
 def api_search_items():
diff --git a/jon/static/entry.js b/jon/static/entry.js
index f42209a..f5720f3 100644
--- a/jon/static/entry.js
+++ b/jon/static/entry.js
@@ -11458,21 +11458,6 @@ var $author$project$Entry$SetTaxGroup = function (a) {
 	return {$: 'SetTaxGroup', a: a};
 };
 var $author$project$Entry$SubmitSearch = {$: 'SubmitSearch'};
-var $elm$html$Html$Attributes$action = function (uri) {
-	return A2(
-		$elm$html$Html$Attributes$stringProperty,
-		'action',
-		_VirtualDom_noJavaScriptUri(uri));
-};
-var $elm$json$Json$Encode$bool = _Json_wrap;
-var $elm$html$Html$Attributes$boolProperty = F2(
-	function (key, bool) {
-		return A2(
-			_VirtualDom_property,
-			key,
-			$elm$json$Json$Encode$bool(bool));
-	});
-var $elm$html$Html$Attributes$disabled = $elm$html$Html$Attributes$boolProperty('disabled');
 var $elm$html$Html$fieldset = _VirtualDom_node('fieldset');
 var $elm$html$Html$Attributes$for = $elm$html$Html$Attributes$stringProperty('htmlFor');
 var $elm$html$Html$form = _VirtualDom_node('form');
@@ -11586,6 +11571,15 @@ var $author$project$Calculator$SetBundleSize = function (a) {
 var $author$project$Calculator$SetTax = function (a) {
 	return {$: 'SetTax', a: a};
 };
+var $elm$json$Json$Encode$bool = _Json_wrap;
+var $elm$html$Html$Attributes$boolProperty = F2(
+	function (key, bool) {
+		return A2(
+			_VirtualDom_property,
+			key,
+			$elm$json$Json$Encode$bool(bool));
+	});
+var $elm$html$Html$Attributes$disabled = $elm$html$Html$Attributes$boolProperty('disabled');
 var $author$project$NumberInput$get = function ($) {
 	return $.value;
 };
@@ -12009,8 +12003,7 @@ var $author$project$Entry$view = function (_v0) {
 			$elm$html$Html$form,
 			_List_fromArray(
 				[
-					$elm$html$Html$Attributes$method('POST'),
-					$elm$html$Html$Attributes$action('/entry/add-to-cart')
+					$elm$html$Html$Attributes$method('POST')
 				]),
 			_List_fromArray(
 				[
@@ -12050,7 +12043,6 @@ var $author$project$Entry$view = function (_v0) {
 										[
 											$elm$html$Html$Events$onInput($author$project$Entry$SetBarcode),
 											$elm$html$Html$Attributes$value(model.barcode),
-											$elm$html$Html$Attributes$disabled(true),
 											$elm$html$Html$Attributes$name('barcode'),
 											$elm$html$Html$Attributes$id('barcode')
 										]),
@@ -12101,7 +12093,7 @@ var $author$project$Entry$view = function (_v0) {
 										]),
 									_List_fromArray(
 										[
-											$elm$html$Html$text('Stückzahl')
+											$elm$html$Html$text('Eingekauft')
 										])),
 									A2(
 									$elm$html$Html$input,
@@ -12278,7 +12270,7 @@ var $author$project$Entry$view = function (_v0) {
 							_List_Nil,
 							_List_fromArray(
 								[
-									$elm$html$Html$text('In den Warenkorb')
+									$elm$html$Html$text('Auftrag anlegen')
 								]))
 						]))
 				]));
diff --git a/jon/templates/entry/index.html b/jon/templates/entry/index.html
index b986866..6e9de2f 100644
--- a/jon/templates/entry/index.html
+++ b/jon/templates/entry/index.html
@@ -1,16 +1,30 @@
 {% extends "base.html" %}
 
 {% block content %}
-<div class="entry-app"></div>
-<script src="{{ url_for('static', filename='entry.js') }}"></script>
-<script>
-Elm.Entry.init({
-  node: document.querySelector('.entry-app'),
-  flags: {
-    locations: {{ to_json(locations) | safe }},
-    groups: {{ to_json(groups) | safe }},
-    taxGroups: {{ to_json(tax_groups) | safe }}
-  }
-});
-</script>
+<a href="/entry/new-order">Neuer Auftrag</a>
+<h2>Aufträge</h2>
+<table>
+  <tr>
+    <th>Barcode</th>
+    <th>Name</th>
+    <th>Eingekauft</th>
+    <th>Gruppen-ID</th>
+    <th>Raum-ID</th>
+    <th>Steuergruppen-ID</th>
+    <th>EK-Preis (Netto)</th>
+    <th>VK-Preis (Brutto)</th>
+  </tr>
+  {% for cart_item in cart %}
+  <tr>
+    <td><code>{{ cart_item.barcode }}</code></td>
+    <td>{{ cart_item.name }}</td>
+    <td class="--align-right">{{ cart_item.sales_units }}</td>
+    <td class="--align-right">{{ cart_item.group_id }}</td>
+    <td class="--align-right">{{ cart_item.location_id }}</td>
+    <td class="--align-right">{{ cart_item.tax_group_id }}</td>
+    <td class="--align-right">{{ format_currency(cart_item.net_unit_price) }}</td>
+    <td class="--align-right">{{ format_currency(cart_item.gross_unit_price) }}</td>
+  </tr>
+  {% endfor %}
+</table>
 {% endblock %}
diff --git a/jon/templates/entry/new-order.html b/jon/templates/entry/new-order.html
new file mode 100644
index 0000000..b986866
--- /dev/null
+++ b/jon/templates/entry/new-order.html
@@ -0,0 +1,16 @@
+{% extends "base.html" %}
+
+{% block content %}
+<div class="entry-app"></div>
+<script src="{{ url_for('static', filename='entry.js') }}"></script>
+<script>
+Elm.Entry.init({
+  node: document.querySelector('.entry-app'),
+  flags: {
+    locations: {{ to_json(locations) | safe }},
+    groups: {{ to_json(groups) | safe }},
+    taxGroups: {{ to_json(tax_groups) | safe }}
+  }
+});
+</script>
+{% endblock %}