Handle all bank loading gracefully.

This commit is contained in:
Dominik Pantůček 2023-04-04 22:34:41 +02:00
parent cc4cfccae1
commit e25a75ab27
5 changed files with 51 additions and 29 deletions

View file

@ -213,7 +213,7 @@ bank-fio.import.scm: $(BANK-FIO-SOURCES)
MEMBERS-PAYMENTS-SOURCES=members-payments.scm bank-account.import.scm \ MEMBERS-PAYMENTS-SOURCES=members-payments.scm bank-account.import.scm \
dictionary.import.scm member-fees.import.scm \ dictionary.import.scm member-fees.import.scm \
period.import.scm configuration.import.scm period.import.scm configuration.import.scm utils.import.scm
members-payments.o: members-payments.import.scm members-payments.o: members-payments.import.scm
members-payments.import.scm: $(MEMBERS-PAYMENTS-SOURCES) members-payments.import.scm: $(MEMBERS-PAYMENTS-SOURCES)

View file

@ -61,17 +61,20 @@
;; Loads Fio bank accound statement. ;; Loads Fio bank accound statement.
(define (bank-fio-parse fn) (define (bank-fio-parse fn)
(with-progress% (let ((csv (with-progress% #t fn (csv-parse fn))))
#t fn (if csv
(let* ((csv (csv-parse fn)) (let* ((head+body (csv-split-header csv))
(head+body (csv-split-header csv)) (head (car head+body))
(head (car head+body)) (body (cadr head+body))
(body (cadr head+body)) (numrow (assoc "accountId" head))
(numrow (assoc "accountId" head)) (num (if numrow (cadr numrow) "ERROR"))
(num (if numrow (cadr numrow) "ERROR")) (bankrow (assoc "bankId" head))
(bankrow (assoc "bankId" head)) (bank (if bankrow (cadr bankrow) "ERROR")))
(bank (if bankrow (cadr bankrow) "ERROR"))) (make-bank-account num bank
(make-bank-account num bank (map make-fio-transaction body)))
(map make-fio-transaction body))))) (let ()
(print "Fio: cannot load account " fn)
#f
))))
) )

View file

@ -38,6 +38,7 @@
(chicken keyword) (chicken keyword)
(chicken io) (chicken io)
(chicken irregex) (chicken irregex)
(chicken condition)
testing testing
progress) progress)
@ -103,8 +104,14 @@
;; Loads given CSV file and parses its lines into lists ;; Loads given CSV file and parses its lines into lists
(define (csv-parse fn . args) (define (csv-parse fn . args)
(let ((lines (read-lines (open-input-file fn)))) (call/cc
(apply csv-parse-lines lines args))) (lambda (ret)
(with-exception-handler
(lambda (ex)
(ret #f))
(lambda ()
(let ((lines (read-lines (open-input-file fn))))
(apply csv-parse-lines lines args)))))))
;; Splits CSV into header and body based on the first empty row. ;; Splits CSV into header and body based on the first empty row.
(define (csv-split-header csv) (define (csv-split-header csv)

View file

@ -43,8 +43,8 @@
(let* ((edvar (get-environment-variable "EDITOR")) (let* ((edvar (get-environment-variable "EDITOR"))
(editor (or edvar "editor")) (editor (or edvar "editor"))
(pid (process-run editor (list file-path)))) (pid (process-run editor (list file-path))))
(process-wait pid) (let-values (((a b c) (process-wait pid)))
(clrscr) (clrscr)
(flush-output))) (flush-output))))
) )

View file

@ -42,6 +42,7 @@
(chicken sort) (chicken sort)
(chicken process-context) (chicken process-context)
(chicken pathname) (chicken pathname)
(chicken condition)
bank-account bank-account
member-record member-record
members-base members-base
@ -49,7 +50,8 @@
dictionary dictionary
member-fees member-fees
period period
configuration) configuration
utils)
;; Exchange rates ;; Exchange rates
(define exchange-rates-lookup-table (define exchange-rates-lookup-table
@ -103,9 +105,15 @@
;; Reads the payments ;; Reads the payments
(define (load-accounts-list apikeys) (define (load-accounts-list apikeys)
(map (compose car string-split) (call/cc
(read-lines (lambda (ret)
(open-input-file apikeys)))) (with-exception-handler
(lambda (ex)
(ret #f))
(lambda ()
(map (compose car string-split)
(read-lines
(open-input-file apikeys))))))))
;; Loads all accounts - it expects .csv files in given directory. ;; Loads all accounts - it expects .csv files in given directory.
(define (load-accounts accounts-list dir) (define (load-accounts accounts-list dir)
@ -117,13 +125,17 @@
;; accounts and processes transactions. ;; accounts and processes transactions.
(define (members-payments-process mb apikeys-file dir) (define (members-payments-process mb apikeys-file dir)
(if apikeys-file (if apikeys-file
(let* ((accounts (load-accounts (let* ((acc-list (load-accounts-list apikeys-file))
(load-accounts-list apikeys-file) (accounts (if acc-list (load-accounts acc-list dir) #f)))
dir))) (if accounts
(map member-sort-payments (map member-sort-payments
(foldl members-payments-process-bank (foldl members-payments-process-bank
mb mb
accounts))) (filter identity
accounts)))
(let ()
(print "Warning: no accounts loaded!")
mb)))
mb)) mb))
;; Adds all balances - payments are converted to CZK in member-payments-total ;; Adds all balances - payments are converted to CZK in member-payments-total