fin-check-invoices (2352B)
1 #!/usr/bin/env zsh 2 # 3 # Script that will check your fio bank account and fakturoid overdue invoices 4 # and find same VS. 5 # 6 # For each pair (fio transaction and fakturoid invoice) will run script, that 7 # will compare money paid and requested, mark as paid in fakturoid and generate 8 # RSS feed. 9 # 10 # Deps: 11 # - fio 12 # - fak 13 # - jq 14 # - rssg 15 16 tempdir=`mktemp -d` 17 fi=$tempdir/fio 18 fa=$tempdir/fak 19 20 [ -d $tempdir ] || mkdir $tempdir 21 22 # Fetch transactions from fio and invoices from fakturoid 23 fio -a work t p -f `date -Idate --date="1 month ago"` > $fi 24 fak i l -s "overdue" > $tempdir/fakod 25 fak i l -s "open" > $tempdir/fakop 26 jq -s '[.[][]]' $tempdir/fakod $tempdir/fakop > $fa 27 28 # Find pairs 29 found=$( 30 comm -12 \ 31 <(jq ".[].vs" -r $fi | sort) \ 32 <(jq ".[].variable_symbol" -r $fa | sort) 33 ) 34 35 for vs in ${(f)found}; do 36 # Fio Data 37 local fio_data=$(jq ".[] | select(.vs == \"$vs\")" $fi) 38 local -F fio_money=$(jq ".money | tonumber" <<< $fio_data) 39 local fio_date=$(jq ".date" -r <<< $fio_data | cut -d+ -f1) 40 local fio_from=$(jq "if .beneficiary_name == \"\" then .beneficiary_comment else .beneficiary_name end" -r <<< $fio_data) 41 local fio_currency=$(jq ".currency" -r <<< $fio_data) 42 # Fakturoid Data 43 local fak_data=$(jq ".[] | select(.variable_symbol == \"$vs\")" $fa) 44 local -F fak_money=$(jq ".subtotal | tonumber" <<< $fak_data) 45 local fak_id=$(jq ".id" <<< $fak_data) 46 local fak_currency=$(jq ".currency" -r <<< $fak_data) 47 48 print "Invoice $vs has been paid.\n" 49 50 # TODO: check currency 51 52 # Money check [fak.total == fio.money] 53 local mcheck="OK" 54 if [[ $fio_money -ne $fak_money ]]; then 55 mcheck="Failed: $(( $fio_money - $fak_money ))" 56 print "Money check failed: FIO:$fio_money != FAK:$fak_money\n" >&2 57 fi 58 59 # Mark as paid in fakturoid [invoiceID] 60 fak i a \ 61 -i "$fak_id" \ 62 -e "pay" --paid-at="`date -Iseconds --date="$fio_date"`" \ 63 --paid-amount="$fio_money" 64 65 # Generate rss page 66 local from="" 67 if [[ -n "$fio_from" ]]; then 68 from="[$fio_from]" 69 fi 70 print "# Paid invoice $from ($mcheck) 71 VS: $vs 72 73 TO PAY: $fak_money 74 PAID: $fak_currency 75 76 $mcheck" \ 77 > "$XDG_CACHE_HOME/osvc/$fak_id.md" 78 79 # Add to rss 80 print "<a href=\"$fak_id.md\" title=\"$fio_date\">$fak_id</a>\n" >> "$XDG_CACHE_HOME/osvc/rss.html" 81 done 82 83 rssg $XDG_CACHE_HOME/osvc/rss.html "Invoices" > $XDG_CACHE_HOME/osvc/rss.xml 84 rm -r $tempdir