From 3f0575483388756ab1a6af9200cac6bf47e22e7d Mon Sep 17 00:00:00 2001 From: Jakub Czajka Date: Sun, 31 Dec 2023 13:27:45 +0100 Subject: [PATCH] Add scripts for recording a new purchase. --- payment.sh | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++ payments.sh | 96 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 payment.sh create mode 100644 payments.sh diff --git a/payment.sh b/payment.sh new file mode 100644 index 0000000..5c36394 --- /dev/null +++ b/payment.sh @@ -0,0 +1,113 @@ +#!/bin/sh +# Copyright (c) 2023 Jakub Czajka +# License: GPL-3.0 or later. + +. /etc/environment + +request_body=$(echo "${REQUEST_BODY}" | /usr/bin/python3 -c "\ +import sys; +from urllib.parse import unquote_plus; +print(unquote_plus(sys.stdin.read()));") + +get_query_param() { + echo "${request_body}" | sed "s/^.*${1}=// ; s/\&.*$//" +} + +query_db() { + /usr/bin/psql --user=payments_tracker --dbname="${PAYMENTS_DB}" \ + --tuples-only --no-align --command="${1}" +} + +insert_into() { + query_db "\ + WITH new_id AS ( + INSERT INTO ${1} (name) + VALUES ('${2}') + ON CONFLICT DO NOTHING + RETURNING id + ) + SELECT * + FROM new_id + UNION + SELECT id + FROM ${1} + WHERE name='${2}'" +} + +shop=$(get_query_param "shop") +shop_id=$(insert_into "shops" "${shop}") + +product=$(get_query_param "product") +product_id=$(insert_into "products" "${product}") + +purchase_date=$(get_query_param "date") + +price=$(get_query_param "price") + +currency=$(get_query_param "currency") +currency_id=$(insert_into "currencies" "${currency}") + +unit=$(get_query_param "unit") +unit_id=$(insert_into "units" "${unit}") + +payment_method=$(get_query_param "payment_method") +payment_method_id=$(insert_into "payment_methods" "${payment_method}") + +if [ "$(get_query_param "discount")" = "on" ] +then + discount="TRUE" +else + discount="FALSE" +fi + +purchase_id=$(query_db "\ +WITH new_purchase_id AS ( + INSERT INTO purchases ( + shop_id, + product_id, + payment_method_id, + date, + price, + currency_id, + unit_id, + discount + ) + VALUES ( + ${shop_id}, + ${product_id}, + ${payment_method_id}, + '${purchase_date}', + ${price}, + ${currency_id}, + ${unit_id}, + ${discount} + ) + RETURNING id +) +SELECT * +FROM new_purchase_id;") + +PAGE=" + + + + + Purchase + + + +Recorded new purchase (id=${purchase_id}) of ${product} \ +(id=${product_id}) on ${purchase_date} in ${shop} (id=${shop_id}). Payed \ +${price} ${currency} (id=${currency_id}) for ${unit} (id=${unit_id}) with \ +${payment_method} (id=${payment_method_id}) (discount=${discount}). + +" + +echo "HTTP/1.1 200 OK" +echo "Content-Type: text/html" +echo "${PAGE}" | sed 's/^[ \t]*//g' diff --git a/payments.sh b/payments.sh new file mode 100644 index 0000000..ff9d6c0 --- /dev/null +++ b/payments.sh @@ -0,0 +1,96 @@ +#!/bin/sh +# Copyright (c) 2023 Jakub Czajka +# License: GPL-3.0 or later. + +. /etc/environment + +query_db() { + /usr/bin/psql --user=payments_tracker --dbname="${PAYMENTS_DB}" \ + --tuples-only --no-align --command="${1}" +} + +as_options() { + xargs --replace={} echo "" +} + +PAGE=" + + + + + Payments + + + +
+
+ Payment + + + + $(query_db "SELECT name FROM shops" | as_options) + + + + + $(query_db "SELECT name FROM products" | as_options) + + + + + + + + + $(query_db "SELECT name FROM currencies" | as_options) + + + + + $(query_db "SELECT name FROM units" | as_options) + + + + + $(query_db "SELECT name FROM payment_methods" | as_options) + + + + +
+
+ +" + +echo "HTTP/1.1 200 OK" +echo "Content-Type: text/html" +echo "${PAGE}" | sed 's/^[ \t]*//g' -- 2.39.5