Move account & split.scm to engine-utilities

This commit is contained in:
Peter Broadbery 2015-11-12 20:26:18 +00:00
parent 8dfea02da7
commit b47f04539e
9 changed files with 85 additions and 79 deletions

View File

@ -20,6 +20,12 @@
;; Boston, MA 02110-1301, USA gnu@gnu.org
;; Copyright 2000 Rob Browning <rlb@cs.utexas.edu>
(use-modules (gnucash gnc-module))
(gnc:module-begin-syntax (gnc:module-load "gnucash/engine" 0))
(use-modules (srfi srfi-1)
(srfi srfi-13))
(define (gnc:account-map-descendants thunk account)
(let ((descendants (or (gnc-account-get-descendants-sorted account) '())))
@ -28,3 +34,64 @@
(define (gnc:account-map-children thunk account)
(let ((children (or (gnc-account-get-children-sorted account) '())))
(map thunk children)))
;; account related functions
;; is account in list of accounts?
(define (account-same? a1 a2)
(or (eq? a1 a2)
(string=? (gncAccountGetGUID a1) (gncAccountGetGUID a2))))
(define account-in-list?
(lambda (account accounts)
(cond
((null? accounts) #f)
((account-same? (car accounts) account) #t)
(else (account-in-list? account (cdr accounts))))))
;; Optimized version of accout-in-list if we know
;; the list in advance.
(define (account-in-list-pred accounts)
(define (my-assoc str alist)
(find (lambda (pair) (account-same? str (car pair))) alist))
(define (my-hash acc size)
(remainder (string-hash (gncAccountGetGUID acc)) size))
(let ((hash-table (make-hash-table)))
(for-each (lambda (acc) (hashx-set! my-hash my-assoc hash-table acc #t))
accounts)
(lambda (account)
(hashx-ref my-hash my-assoc hash-table account))))
(define account-in-alist
(lambda (account alist)
(cond
((null? alist) #f)
((account-same? (caar alist) account) (car alist))
(else (account-in-alist account (cdr alist))))))
;; helper for sorting of account list
(define (account-full-name<? a b)
(string<? (gnc-account-get-full-name a) (gnc-account-get-full-name b)))
;; return maximum depth over accounts and their children, if any
(define (accounts-get-children-depth accounts)
(apply max
(map (lambda (acct)
(let ((acct-depth (gnc-account-get-current-depth acct)))
(+ acct-depth (- (gnc-account-get-tree-depth acct) 1))))
accounts)))
;; Splits
(export split-same?)
(export split-in-list?)
(define (split-same? s1 s2)
(or (eq? s1 s2)
(string=? (gncSplitGetGUID s1) (gncSplitGetGUID s2))))
(define split-in-list?
(lambda (split splits)
(cond
((null? splits) #f)
((split-same? (car splits) split) #t)
(else (split-in-list? split (cdr splits))))))

View File

@ -67,9 +67,22 @@
(export GNC_COMMODITY_NS_MUTUAL)
(export gnc:url->loaded-session)
;; engine-utilities.scm
(export gnc:account-map-descendants)
(export gnc:account-map-children)
(export account-same?)
(export account-in-list?)
(export account-in-list-pred)
(export account-in-alist)
(export account-full-name<?)
(export account-list-predicate)
(export accounts-get-children-depth)
(export split-same?)
(export split-in-list?)
(export gnc:split-structure)
(export gnc:make-split-scm)
(export gnc:split-scm?)

View File

@ -69,11 +69,9 @@ gncscm_DATA = \
gncmodscmdir = ${GNC_SCM_INSTALL_DIR}/gnucash/report/report-system
gncmodscm_DATA = \
account.scm \
collectors.scm \
list-extras.scm \
report-collectors.scm \
split.scm
report-collectors.scm
gncscmmoddir = ${GNC_SCM_INSTALL_DIR}/gnucash/report/
gncscmmod_DATA = \

View File

@ -1,62 +1,5 @@
(define-module (gnucash report report-system account))
(use-modules (gnucash gnc-module))
(use-modules (gnucash gnc-module))
(use-modules (srfi srfi-1)
(srfi srfi-13))
(gnc:module-begin-syntax (gnc:module-load "gnucash/engine" 0))
(export account-same?)
(export account-in-list?)
(export account-in-list-pred)
(export account-in-alist)
(export account-full-name<?)
(export account-list-predicate)
(export accounts-get-children-depth)
;; is account in list of accounts?
(define (account-same? a1 a2)
(string=? (gncAccountGetGUID a1) (gncAccountGetGUID a2)))
(define account-in-list?
(lambda (account accounts)
(cond
((null? accounts) #f)
((account-same? (car accounts) account) #t)
(else (account-in-list? account (cdr accounts))))))
;; Optimized version of accout-in-list if we know
;; the list in advance.
(define (account-in-list-pred accounts)
(define (my-assoc str alist)
(find (lambda (pair) (account-same? str (car pair))) alist))
(define (my-hash acc size)
(remainder (string-hash (gncAccountGetGUID acc)) size))
(let ((hash-table (make-hash-table)))
(for-each (lambda (acc) (hashx-set! my-hash my-assoc hash-table acc #t))
accounts)
(lambda (account)
(hashx-ref my-hash my-assoc hash-table account))))
(define account-in-alist
(lambda (account alist)
(cond
((null? alist) #f)
((account-same? (caar alist) account) (car alist))
(else (account-in-alist account (cdr alist))))))
;; helper for sorting of account list
(define (account-full-name<? a b)
(string<? (gnc-account-get-full-name a) (gnc-account-get-full-name b)))
;; return maximum depth over accounts and their children, if any
(define (accounts-get-children-depth accounts)
(apply max
(map (lambda (acct)
(let ((acct-depth (gnc-account-get-current-depth acct)))
(+ acct-depth (- (gnc-account-get-tree-depth acct) 1))))
accounts)))

View File

@ -5,16 +5,3 @@
(use-modules (sw_engine))
(export split-same?)
(export split-in-list?)
(define (split-same? s1 s2)
(string=? (gncSplitGetGUID s1) (gncSplitGetGUID s2)))
(define split-in-list?
(lambda (split splits)
(cond
((null? splits) #f)
((split-same? (car splits) split) #t)
(else (split-in-list? split (cdr splits))))))

View File

@ -1,4 +1,4 @@
(use-modules (gnucash report report-system account))
(use-modules (gnucash engine))
(use-modules (gnucash report report-system test test-extras))
(use-modules (sw_engine))

View File

@ -3,7 +3,7 @@
(gnc:module-begin-syntax (gnc:module-load "gnucash/app-utils" 0))
(use-modules (gnucash report report-system split))
(use-modules (gnucash engine))
(use-modules (gnucash report report-system test test-extras))
(use-modules (gnucash report report-system))

View File

@ -31,8 +31,7 @@
(use-modules (gnucash gettext))
(use-modules (gnucash printf))
(use-modules (gnucash report report-system account))
(use-modules (gnucash report report-system split))
(use-modules (gnucash engine))
(gnc:module-load "gnucash/report/report-system" 0)
(gnc:module-load "gnucash/gnome-utils" 0) ;for gnc-build-url

View File

@ -31,8 +31,7 @@
(use-modules (gnucash main)) ;; FIXME: delete after we finish modularizing.
(use-modules (gnucash gnc-module))
(use-modules (gnucash gettext))
(use-modules (gnucash report report-system account))
(use-modules (gnucash report report-system split))
(use-modules (gnucash engine))
(use-modules (gnucash printf))