From b47f04539ef20e409b7516a5886c1b6c459df874 Mon Sep 17 00:00:00 2001 From: Peter Broadbery Date: Thu, 12 Nov 2015 20:26:18 +0000 Subject: [PATCH] Move account & split.scm to engine-utilities --- src/engine/engine-utilities.scm | 67 +++++++++++++++++++ src/engine/engine.scm | 13 ++++ src/report/report-system/Makefile.am | 4 +- src/report/report-system/account.scm | 57 ---------------- src/report/report-system/split.scm | 13 ---- .../report-system/test/test-account.scm | 2 +- src/report/report-system/test/test-split.scm | 2 +- src/report/standard-reports/budget.scm | 3 +- src/report/standard-reports/cash-flow.scm | 3 +- 9 files changed, 85 insertions(+), 79 deletions(-) diff --git a/src/engine/engine-utilities.scm b/src/engine/engine-utilities.scm index 6838f66d8e..1310ff9605 100644 --- a/src/engine/engine-utilities.scm +++ b/src/engine/engine-utilities.scm @@ -20,6 +20,12 @@ ;; Boston, MA 02110-1301, USA gnu@gnu.org ;; Copyright 2000 Rob Browning +(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-nameloaded-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