osvc

scripts to save some time with self-employed stuff
git clone git://gtms.dev/osvc.git
Log | Files | Refs

commit 7a2a51067543ac8c7cec47f50832d5a136566534
Author: Tomas Nemec <nemi@skaut.cz>
Date:   Fri, 24 Sep 2021 22:19:56 +0200

feat: fin-check-invoices

Script for check paid invoices from fio bank account.

Diffstat:
Afin-check-invoices | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amakefile | 13+++++++++++++
2 files changed, 92 insertions(+), 0 deletions(-)

diff --git a/fin-check-invoices b/fin-check-invoices @@ -0,0 +1,79 @@ +#!/usr/bin/env zsh +# +# Script that will check your fio bank account and fakturoid overdue invoices +# and find same VS. +# +# For each pair (fio transaction and fakturoid invoice) will run script, that +# will compare money paid and requested, mark as paid in fakturoid and generate +# RSS feed. +# +# Deps: +# - fio +# - fak +# - jq +# - rssg + +tempdir=`mktemp -d` +fi=$tempdir/fio +fa=$tempdir/fak + +[ -d $tempdir ] || mkdir $tempdir + +# Fetch transactions from fio and invoices from fakturoid +fio -a work t p -f `date -Idate --date="1 month ago"` > $fi +fak i l -s "overdue" > $fa + +# Find pairs +found=$( +comm -12 \ + <(jq ".[].vs" -r $fi | sort) \ + <(jq ".[].variable_symbol" -r $fa | sort) +) + +for vs in ${(f)found}; do + # Fio Data + fio_data=$(jq ".[] | select(.vs == \"$vs\")" $fi) + fio_money=$(jq ".money | tonumber" <<< $fio_data) + fio_date=$(jq ".date" -r <<< $fio_data | cut -d+ -f1) + fio_from=$(jq "if .beneficiary_name == \"\" then .beneficiary_comment else .beneficiary_name end" -r <<< $fio_data) + fio_currency=$(jq ".currency" -r <<< $fio_data) + # Fakturoid Data + fak_data=$(jq ".[] | select(.variable_symbol == \"$vs\")" $fa) + fak_money=$(jq ".subtotal | tonumber" <<< $fak_data) + fak_id=$(jq ".id" <<< $fak_data) + fak_currency=$(jq ".currency" -r <<< $fak_data) + + print "Invoice $vs has been paid.\n" + # Money check [fak.total == fio.money] + mcheck="OK" + if [[ $fio_money != $fak_money ]]; then + mcheck="Failed: $(( $fio_money - $fak_money ))" + print "Money check failed: FIO:$fio_money != FAK:$fak_money\n" >&2 + fi + + # Mark as paid in fakturoid [invoiceID] + fak i a \ + -i "$fak_id" \ + -e "pay" --paid-at="`date -Iseconds --date="$fio_date"`" \ + --paid-amount="$fio_money" + + # Generate rss page + from="" + if [ -n "$fio_from" ]; then + from="[$fio_from]" + fi + print "# Paid invoice $from ($mcheck) +VS: $vs + +TO PAY: $fak_money +PAID: $fak_currency + +$mcheck" \ + > "$CACHE/osvc/$fak_id.md" + + # Add to rss + print "<a href=\"$fak_id.md\" title=\"$fio_date\">$fak_id</a>\n" >> "$CACHE/osvc/rss.html" +done + +rssg $CACHE/osvc/rss.html "Invoices" > $CACHE/osvc/rss.xml +rm -r $tempdir diff --git a/makefile b/makefile @@ -0,0 +1,13 @@ +PREFIX = /usr/local +BINDIR = $(PREFIX)/bin + +all: + @echo "Nothing to do, try \"make install\" instead." + +install: + @install -v -d "$(BINDIR)/" && install -m 0755 -v "./fin-check-invoices" "$(BINDIR)/fin-check-invoices" + +uninstall: trun.lua + @rm -vrf "$(BINDIR)/fin-check-invoices" + +.PHONY: all install uninstall