r18394 - gnucash/trunk/src - Bug #593070: Budget Report: Omit accounts with no budget and zero balance

Christian Stimming cstim at code.gnucash.org
Mon Oct 26 16:15:18 EDT 2009


Author: cstim
Date: 2009-10-26 16:15:18 -0400 (Mon, 26 Oct 2009)
New Revision: 18394
Trac: http://svn.gnucash.org/trac/changeset/18394

Modified:
   gnucash/trunk/src/engine/gnc-budget.c
   gnucash/trunk/src/engine/gnc-budget.h
   gnucash/trunk/src/report/report-system/html-acct-table.scm
   gnucash/trunk/src/report/report-system/report-system.scm
   gnucash/trunk/src/report/report-system/report-utilities.scm
   gnucash/trunk/src/report/standard-reports/budget.scm
Log:
Bug #593070: Budget Report: Omit accounts with no budget and zero balance

Patch contributed by Luke Duncan:

I would like to have an option for the budget report that allows it to omit
accounts that have zero balance and no budget value specified over the entire
budget term. This avoids showing entire rows of blank cells without the user
having to manually select which accounts have a budget value or balance for the
term of the budget. The user can select all expense and income accounts and it
only shows the accounts that have actual (nonzero) data to display.

Modified: gnucash/trunk/src/engine/gnc-budget.c
===================================================================
--- gnucash/trunk/src/engine/gnc-budget.c	2009-10-26 19:30:32 UTC (rev 18393)
+++ gnucash/trunk/src/engine/gnc-budget.c	2009-10-26 20:15:18 UTC (rev 18394)
@@ -545,6 +545,15 @@
     return ts;
 }
 
+Timespec
+gnc_budget_get_period_end_date(GncBudget *budget, guint period_num)
+{
+    Timespec ts;
+    timespecFromTime_t(
+        &ts,  recurrenceGetPeriodTime(&GET_PRIVATE(budget)->recurrence, period_num, TRUE));
+    return ts;
+}
+
 gnc_numeric
 gnc_budget_get_account_period_actual_value(
     GncBudget *budget, Account *acc, guint period_num)

Modified: gnucash/trunk/src/engine/gnc-budget.h
===================================================================
--- gnucash/trunk/src/engine/gnc-budget.h	2009-10-26 19:30:32 UTC (rev 18393)
+++ gnucash/trunk/src/engine/gnc-budget.h	2009-10-26 20:15:18 UTC (rev 18394)
@@ -122,9 +122,12 @@
 void gnc_budget_set_recurrence(GncBudget *budget, const Recurrence *r);
 /*@ dependent @*/ const Recurrence * gnc_budget_get_recurrence(GncBudget *budget);
 
-/** Set/Get the starting date of the Budget */
+/** Get the starting date of the Budget period*/
 Timespec gnc_budget_get_period_start_date(GncBudget* budget, guint period_num);
 
+/** Get the ending date of the Budget period*/
+Timespec gnc_budget_get_period_end_date(GncBudget* budget, guint period_num);
+
 /* Period indices are zero-based. */
 void gnc_budget_set_account_period_value(
     GncBudget* budget, Account* account, guint period_num, gnc_numeric val);

Modified: gnucash/trunk/src/report/report-system/html-acct-table.scm
===================================================================
--- gnucash/trunk/src/report/report-system/html-acct-table.scm	2009-10-26 19:30:32 UTC (rev 18393)
+++ gnucash/trunk/src/report/report-system/html-acct-table.scm	2009-10-26 20:15:18 UTC (rev 18394)
@@ -247,6 +247,11 @@
 ;;          gnc:account-get-trans-type-balance-interval, matching
 ;;          closing transactions to be ignored when balance-mode is
 ;;          'pre-closing.
+;;
+;;     report-budget: budget
+;;
+;;	    (optional) a budget used to ignore accounts with zero 
+;;	    budget or balance (if zb-balance-mode is set to omit).
 ;; 
 ;;     account-type: unimplemented
 ;;     account-class: unimplemented
@@ -628,6 +633,7 @@
 				 (list 'regexp #f)
 				 )
 				))
+	 (report-budget (or (get-val env 'report-budget) #f))
 	 ;; local variables
 	 (toplvl-accts
 	  (gnc-account-get-children-sorted (gnc-get-current-root-account)))
@@ -828,10 +834,15 @@
 	     (set! acct-depth-reached (max acct-depth-reached acct-depth))
 	     (set! logi-depth-reached (max logi-depth-reached logi-depth))
 	     (set! disp-depth-reached (max disp-depth-reached disp-depth))
+
 	     (or (not (use-acct? acct))
 		 ;; ok, so we'll consider parent accounts with zero
 		 ;; recursive-bal to be zero balance leaf accounts
 		 (and (gnc-commodity-collector-allzero? recursive-bal)
+		      (or (not report-budget)
+             		  (gnc-numeric-zero-p
+				(gnc:budget-account-get-rolledup-net 
+					report-budget account #f #f)))
 		      (equal? zero-mode 'omit-leaf-acct))
 		 (begin
 		   (set! row-env

Modified: gnucash/trunk/src/report/report-system/report-system.scm
===================================================================
--- gnucash/trunk/src/report/report-system/report-system.scm	2009-10-26 19:30:32 UTC (rev 18393)
+++ gnucash/trunk/src/report/report-system/report-system.scm	2009-10-26 20:15:18 UTC (rev 18394)
@@ -672,10 +672,14 @@
 (export gnc:account-get-trans-type-splits-interval)
 (export gnc:double-col)
 (export gnc:budget-get-start-date)
+(export gnc:budget-get-end-date)
 (export gnc:budget-account-get-net)
 (export gnc:budget-accountlist-get-net)
 (export gnc:budget-account-get-initial-balance)
 (export gnc:budget-accountlist-get-initial-balance)
+(export budget-account-sum budget)
+(export gnc:get-account-period-rolledup-budget-value)
+(export gnc:budget-account-get-rolledup-net)
 (export gnc:get-assoc-account-balances)
 (export gnc:select-assoc-account-balance)
 (export gnc:get-assoc-account-balances-total)

Modified: gnucash/trunk/src/report/report-system/report-utilities.scm
===================================================================
--- gnucash/trunk/src/report/report-system/report-utilities.scm	2009-10-26 19:30:32 UTC (rev 18393)
+++ gnucash/trunk/src/report/report-system/report-utilities.scm	2009-10-26 20:15:18 UTC (rev 18394)
@@ -865,6 +865,11 @@
 (define (gnc:budget-get-start-date budget)
   (gnc-budget-get-period-start-date budget 0))
 
+;; Returns the end date of the last period of the budget.
+(define (gnc:budget-get-end-date budget)
+  (gnc-budget-get-period-end-date budget (- (gnc-budget-get-num-periods budget) 1)))
+
+
 (define (gnc:budget-accountlist-helper accountlist get-fn)
   (let
     (
@@ -932,6 +937,77 @@
   (gnc:budget-accountlist-helper accountlist (lambda (account)
     (gnc:budget-account-get-initial-balance budget account))))
 
+;; Calculate the sum of all budgets of all children of an account for a specific period
+;;
+;; Parameters:
+;;   budget - budget to use
+;;   children - list of children
+;;   period - budget period to use
+;;
+;; Return value:
+;;   budget value to use for account for specified period.
+(define (budget-account-sum budget children period)
+  (let* ((sum
+           (cond
+             ((null? children) (gnc-numeric-zero))
+             (else
+               (gnc-numeric-add
+                 (gnc:get-account-period-rolledup-budget-value budget (car children) period)
+                 (budget-account-sum budget (cdr children) period)
+                 GNC-DENOM-AUTO GNC-RND-ROUND))
+               )
+        ))
+  sum)
+)
+
+;; Calculate the value to use for the budget of an account for a specific period.
+;; - If the account has a budget value set for the period, use it
+;; - If the account has children, use the sum of budget values for the children
+;; - Otherwise, use 0.
+;;
+;; Parameters:
+;;   budget - budget to use
+;;   acct - account
+;;   period - budget period to use
+;;
+;; Return value:
+;;   sum of all budgets for list of children for specified period.
+(define (gnc:get-account-period-rolledup-budget-value budget acct period)
+  (let* ((bgt-set? (gnc-budget-is-account-period-value-set budget acct period))
+        (children (gnc-account-get-children acct))
+        (amount (cond
+                  (bgt-set? (gnc-budget-get-account-period-value budget acct period))
+          ((not (null? children)) (budget-account-sum budget children period))
+          (else (gnc-numeric-zero)))
+        ))
+  amount)
+)
+
+;; Sums rolled-up budget values for a single account from start-period (inclusive) to
+;; end-period (exclusive).
+;; - If the account has a budget value set for the period, use it
+;; - If the account has children, use the sum of budget values for the children
+;; - Otherwise, use 0.
+;;
+;; start-period may be #f to specify the start of the budget
+;; end-period may be #f to specify the end of the budget
+;;
+;; Returns a gnc-numeric value
+(define (gnc:budget-account-get-rolledup-net budget account start-period end-period)
+  (if (not start-period) (set! start-period 0))
+  (if (not end-period) (set! end-period (gnc-budget-get-num-periods budget)))
+  (let*
+    (
+      (period start-period)
+      (net (gnc-numeric-zero))
+      (acct-comm (xaccAccountGetCommodity account)))
+    (while (< period end-period)
+      (set! net (gnc-numeric-add net
+          (gnc:get-account-period-rolledup-budget-value budget account period)
+	  GNC-DENOM-AUTO GNC-RND-ROUND))
+      (set! period (+ period 1)))
+    net))
+
 (define (gnc:get-assoc-account-balances accounts get-balance-fn)
   (let*
     (

Modified: gnucash/trunk/src/report/standard-reports/budget.scm
===================================================================
--- gnucash/trunk/src/report/standard-reports/budget.scm	2009-10-26 19:30:32 UTC (rev 18393)
+++ gnucash/trunk/src/report/standard-reports/budget.scm	2009-10-26 20:15:18 UTC (rev 18394)
@@ -60,6 +60,8 @@
 (define opthelp-show-totalcol (N_ "Display a column with the row totals"))
 (define optname-rollup-budget (N_ "Roll up budget amounts to parent"))
 (define opthelp-rollup-budget (N_ "If parent account does not have its own budget value, use the sum of the child account budget values"))
+(define optname-show-zb-accounts (N_ "Include accounts with zero total balances and budget values"))
+(define opthelp-show-zb-accounts (N_ "Include accounts with zero total (recursive) balances and budget values in this report"))
 (define optname-compress-periods (N_ "Compress prior/later periods"))
 (define opthelp-compress-periods (N_ "Accumulate columns for periods before and after the current period to allow focus on the current period."))
 (define optname-bottom-behavior (N_ "Flatten list to depth limit"))
@@ -138,6 +140,10 @@
      (gnc:make-simple-boolean-option
       gnc:pagename-display optname-rollup-budget
       "s4" opthelp-rollup-budget #f))
+    (add-option
+     (gnc:make-simple-boolean-option
+      gnc:pagename-display optname-show-zb-accounts
+      "s5" opthelp-show-zb-accounts #t))
 
       ;; Set the general page as default option tab
     (gnc:options-set-default-section options gnc:pagename-general)
@@ -175,53 +181,7 @@
          (colnum (quotient numcolumns 2))
 
      )
-  
-  ;; Calculate the sum of all budgets of all children of an account for a specific period
-  ;;
-  ;; Parameters:
-  ;;   budget - budget to use
-  ;;   children - list of children
-  ;;   period - budget period to use
-  ;;
-  ;; Return value:
-  ;;   budget value to use for account for specified period.
-  (define (budget-account-sum budget children period)
-    (let* ((sum
-             (cond
-               ((null? children) (gnc-numeric-zero))
-               (else
-                 (gnc-numeric-add
-                   (gnc:get-account-period-rolledup-budget-value budget (car children) period)
-                   (budget-account-sum budget (cdr children) period)
-                   GNC-DENOM-AUTO GNC-RND-ROUND))
-                 )
-          ))
-    sum)
-  )
 
-  ;; Calculate the value to use for the budget of an account for a specific period.
-  ;; - If the account has a budget value set for the period, use it
-  ;; - If the account has children, use the sum of budget values for the children
-  ;; - Otherwise, use 0.
-  ;;
-  ;; Parameters:
-  ;;   budget - budget to use
-  ;;   acct - account
-  ;;   period - budget period to use
-  ;;
-  ;; Return value:
-  ;;   sum of all budgets for list of children for specified period.
-  (define (gnc:get-account-period-rolledup-budget-value budget acct period)
-    (let* ((bgt-set? (gnc-budget-is-account-period-value-set budget acct period))
-          (children (gnc-account-get-children acct))
-          (amount (cond
-                    (bgt-set? (gnc-budget-get-account-period-value budget acct period))
-            ((not (null? children)) (budget-account-sum budget children period))
-            (else (gnc-numeric-zero)))
-          ))
-    amount)
-  )
-
   (define (negative-numeric-p x)
     (if (gnc-numeric-p x) (gnc-numeric-negative-p x) #f))
   (define (number-cell-tag x)
@@ -583,6 +543,8 @@
          (bottom-behavior (get-option gnc:pagename-accounts optname-bottom-behavior))
          (rollup-budget? (get-option gnc:pagename-display
                                      optname-rollup-budget))
+	 (show-zb-accts? (get-option gnc:pagename-display
+                                     optname-show-zb-accounts))
          (row-num 0) ;; ???
          (work-done 0)
          (work-to-do 0)
@@ -664,12 +626,15 @@
                                display-depth))
                ;;(account-disp-list '())
 
-               ;; Things seem to crash if I don't set 'end-date to
-               ;; _something_ but the actual value isn't used.
-               (env (list (list 'end-date (gnc:get-today))
-                          (list 'display-tree-depth tree-depth)
+               (env (list  
+			   (list 'start-date (gnc:budget-get-start-date budget))
+			   (list 'end-date (gnc:budget-get-end-date budget))
+                           (list 'display-tree-depth tree-depth)
                            (list 'depth-limit-behavior
-                                     (if bottom-behavior 'flatten 'summarize))
+                                (if bottom-behavior 'flatten 'summarize))
+			   (list 'zero-balance-mode 
+				(if show-zb-accts? 'show-leaf-acct 'omit-leaf-acct))
+			   (list 'report-budget budget)
                           ))
                (acct-table #f)
                (html-table (gnc:make-html-table))



More information about the gnucash-changes mailing list