diff --git a/src/report/report-system/report-system.scm b/src/report/report-system/report-system.scm index 19c835de9c..3043314c19 100644 --- a/src/report/report-system/report-system.scm +++ b/src/report/report-system/report-system.scm @@ -663,7 +663,9 @@ (export gnc:account-get-balance-interval) (export gnc:account-get-comm-balance-interval) (export gnc:accountlist-get-comm-balance-interval) +(export gnc:accountlist-get-comm-balance-interval-with-closing) (export gnc:accountlist-get-comm-balance-at-date) +(export gnc:accountlist-get-comm-balance-at-date-with-closing) (export gnc:query-set-match-non-voids-only!) (export gnc:query-set-match-voids-only!) (export gnc:split-voided?) @@ -674,6 +676,7 @@ (export gnc:accounts-count-splits) (export gnc-commodity-collector-allzero?) (export gnc:account-get-trans-type-balance-interval) +(export gnc:account-get-trans-type-balance-interval-with-closing) (export gnc:account-get-pos-trans-total-interval) (export gnc:account-get-trans-type-splits-interval) (export gnc:double-col) diff --git a/src/report/report-system/report-utilities.scm b/src/report/report-system/report-utilities.scm index 3281cd4eb6..d38e354b64 100644 --- a/src/report/report-system/report-utilities.scm +++ b/src/report/report-system/report-utilities.scm @@ -643,9 +643,15 @@ (define (gnc:accountlist-get-comm-balance-interval accountlist from to) (gnc:account-get-trans-type-balance-interval accountlist #f from to)) +(define (gnc:accountlist-get-comm-balance-interval-with-closing accountlist from to) + (gnc:account-get-trans-type-balance-interval-with-closing accountlist #f from to)) + (define (gnc:accountlist-get-comm-balance-at-date accountlist date) (gnc:account-get-trans-type-balance-interval accountlist #f #f date)) +(define (gnc:accountlist-get-comm-balance-at-date-with-closing accountlist date) + (gnc:account-get-trans-type-balance-interval-with-closing accountlist #f #f date)) + ;; utility function - ensure that a query matches only non-voids. Destructive. (define (gnc:query-set-match-non-voids-only! query book) (let ((temp-query (qof-query-create-for-splits))) @@ -708,7 +714,7 @@ ;; Sums up any splits of a certain type affecting a set of accounts. ;; the type is an alist '((str "match me") (cased #f) (regexp #f)) -;; If type is #f, sums all splits in the interval +;; If type is #f, sums all non-closing splits in the interval (define (gnc:account-get-trans-type-balance-interval account-list type start-date-tp end-date-tp) (let* ((total (gnc:make-commodity-collector))) @@ -716,9 +722,13 @@ (let* ((shares (xaccSplitGetAmount split)) (acct-comm (xaccAccountGetCommodity (xaccSplitGetAccount split))) + (txn (xaccSplitGetParent split)) ) - (gnc-commodity-collector-add total acct-comm shares) - ) + (if type + (gnc-commodity-collector-add total acct-comm shares) + (if (not (xaccTransGetIsClosingTxn txn)) + (gnc-commodity-collector-add total acct-comm shares) + ))) ) (gnc:account-get-trans-type-splits-interval account-list type start-date-tp end-date-tp) @@ -727,6 +737,26 @@ ) ) +;; Sums up any splits of a certain type affecting a set of accounts. +;; the type is an alist '((str "match me") (cased #f) (regexp #f)) +;; If type is #f, sums all splits in the interval (even closing splits) +(define (gnc:account-get-trans-type-balance-interval-with-closing + account-list type start-date-tp end-date-tp) + (let* ((total (gnc:make-commodity-collector))) + (map (lambda (split) + (let* ((shares (xaccSplitGetAmount split)) + (acct-comm (xaccAccountGetCommodity + (xaccSplitGetAccount split))) + ) + (gnc-commodity-collector-add total acct-comm shares) + ) + ) + (gnc:account-get-trans-type-splits-interval + account-list type start-date-tp end-date-tp) + ) + total + ) + ) ;; similar, but only counts transactions with non-negative shares and ;; *ignores* any closing entries (define (gnc:account-get-pos-trans-total-interval diff --git a/src/report/standard-reports/equity-statement.scm b/src/report/standard-reports/equity-statement.scm index 78301ba3a8..e34ea03936 100644 --- a/src/report/standard-reports/equity-statement.scm +++ b/src/report/standard-reports/equity-statement.scm @@ -444,10 +444,10 @@ ;; start and end retained earnings (income - expenses) (set! neg-pre-start-retained-earnings - (gnc:accountlist-get-comm-balance-at-date + (gnc:accountlist-get-comm-balance-at-date-with-closing income-expense-accounts start-date-tp)) ; OK (set! neg-pre-end-retained-earnings - (gnc:accountlist-get-comm-balance-at-date + (gnc:accountlist-get-comm-balance-at-date-with-closing income-expense-accounts end-date-tp)) ; OK ;; neg-pre-end-retained-earnings is not used to calculate ;; profit but is used to calculate unrealized gains @@ -455,13 +455,13 @@ ;; calculate net income ;; first, ask out how much profit/loss was closed (set! income-expense-closing - (gnc:account-get-trans-type-balance-interval + (gnc:account-get-trans-type-balance-interval-with-closing income-expense-accounts closing-pattern start-date-tp end-date-tp) ) ;; find retained earnings for the period (set! neg-net-income - (gnc:accountlist-get-comm-balance-interval + (gnc:accountlist-get-comm-balance-interval-with-closing income-expense-accounts start-date-tp end-date-tp)) ; OK ;; revert the income/expense to its pre-closing balance @@ -532,7 +532,7 @@ ;; (set! equity-closing - (gnc:account-get-trans-type-balance-interval + (gnc:account-get-trans-type-balance-interval-with-closing equity-accounts closing-pattern start-date-tp end-date-tp) ) diff --git a/src/report/standard-reports/income-statement.scm b/src/report/standard-reports/income-statement.scm index 9adeb1a6fb..23f0a917c5 100644 --- a/src/report/standard-reports/income-statement.scm +++ b/src/report/standard-reports/income-statement.scm @@ -516,29 +516,29 @@ ;; sum revenues and expenses (set! revenue-closing - (gnc:account-get-trans-type-balance-interval + (gnc:account-get-trans-type-balance-interval-with-closing revenue-accounts closing-pattern start-date-tp end-date-tp) ) ;; this is norm positive (debit) (set! expense-closing - (gnc:account-get-trans-type-balance-interval + (gnc:account-get-trans-type-balance-interval-with-closing expense-accounts closing-pattern start-date-tp end-date-tp) ) ;; this is norm negative (credit) (set! expense-total - (gnc:accountlist-get-comm-balance-interval + (gnc:accountlist-get-comm-balance-interval-with-closing expense-accounts start-date-tp end-date-tp)) (expense-total 'minusmerge expense-closing #f) (set! neg-revenue-total - (gnc:accountlist-get-comm-balance-interval + (gnc:accountlist-get-comm-balance-interval-with-closing revenue-accounts start-date-tp end-date-tp)) (neg-revenue-total 'minusmerge revenue-closing #f) (set! revenue-total (gnc:make-commodity-collector)) (revenue-total 'minusmerge neg-revenue-total #f) (set! trading-total - (gnc:accountlist-get-comm-balance-interval + (gnc:accountlist-get-comm-balance-interval-with-closing trading-accounts start-date-tp end-date-tp)) ;; calculate net income