Bug726674-BudgetReverseSignSupport

This commit is contained in:
Carsten Rinke 2014-03-26 21:34:35 +01:00 committed by Geert Janssens
parent 8e56cc88c0
commit 247f086703
5 changed files with 145 additions and 33 deletions

View File

@ -35,6 +35,8 @@
#include "gnc-budget.h"
#include "gnc-commodity.h"
#include "gnc-gdate-utils.h"
#include "gnc-prefs.h"
/*#include "app-utils/gnc-ui-util.h" for gnc_reverse_balance */
static QofLogModule log_module = GNC_MOD_ENGINE;
@ -496,6 +498,52 @@ gnc_budget_unset_account_period_value(GncBudget *budget, const Account *account,
}
/******************************************************\
* this helper function has been placed here, because *
* the "original" function gnc_reverse_balance is not *
* reachable from here with the current build setup *
\******************************************************/
gboolean
gnc_is_account_reverse_balance (const Account *account)
{
int type;
type = xaccAccountGetType (account);
if (gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, "reversed-accounts-incomeexpense"))
{
switch (type) {
case ACCT_TYPE_INCOME: return TRUE;
break;
case ACCT_TYPE_EXPENSE: return TRUE;
break;
default: return FALSE;
break;
}
}
else if (gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, "reversed-accounts-credit"))
{
switch (type) {
case ACCT_TYPE_LIABILITY: return TRUE;
break;
case ACCT_TYPE_PAYABLE: return TRUE;
break;
case ACCT_TYPE_EQUITY: return TRUE;
break;
case ACCT_TYPE_INCOME: return TRUE;
break;
case ACCT_TYPE_CREDIT: return TRUE;
break;
default: return FALSE;
break;
}
}
return FALSE;
}
/* period_num is zero-based */
/* What happens when account is deleted, after we have an entry for it? */
void
@ -524,7 +572,12 @@ gnc_budget_set_account_period_value(GncBudget *budget, const Account *account,
if (gnc_numeric_check(val))
kvp_frame_set_value(frame, path, NULL);
else
{
if (gnc_is_account_reverse_balance (account))
val = gnc_numeric_neg (val);
kvp_frame_set_numeric(frame, path, val);
}
qof_instance_set_dirty(&budget->inst);
gnc_budget_commit_edit(budget);
@ -570,6 +623,10 @@ gnc_budget_get_account_period_value(const GncBudget *budget, const Account *acco
g_sprintf(bufend, "/%d", period_num);
numeric = kvp_frame_get_numeric(frame, path);
if (gnc_is_account_reverse_balance (account))
numeric = gnc_numeric_neg (numeric);
/* This still returns zero if unset, but callers can check for that. */
return numeric;
}

View File

@ -136,6 +136,9 @@ Timespec gnc_budget_get_period_start_date(const GncBudget* budget, guint period_
/** Get the ending date of the Budget period*/
Timespec gnc_budget_get_period_end_date(const GncBudget* budget, guint period_num);
/* local helper to check what preferences have been set for account reverse */
gboolean gnc_is_account_reverse_balance (const Account *account);
/* Period indices are zero-based. */
void gnc_budget_set_account_period_value(
GncBudget* budget, const Account* account, guint period_num, gnc_numeric val);

View File

@ -869,10 +869,6 @@ estimate_budget_helper(GtkTreeModel *model, GtkTreePath *path,
num = recurrenceGetAccountPeriodValue(&priv->r, acct, i);
if (!gnc_numeric_check(num))
{
if (gnc_reverse_balance (acct))
num = gnc_numeric_neg (num);
num = gnc_numeric_convert(num, GNC_DENOM_AUTO,
GNC_HOW_DENOM_SIGFIGS(priv->sigFigs) | GNC_HOW_RND_ROUND_HALF_UP);
gnc_budget_set_account_period_value(

View File

@ -120,36 +120,51 @@
;; Loop though periods
(while (< period num-periods)
(let
(
(budget-period-value
(gnc-budget-get-account-period-value budget acct period)
)
(actual-period-value
(gnc-budget-get-account-period-actual-value budget acct period)
)
)
;; Add calc new running sum and add to list
(if running-sum
(set! bgt-sum (+ bgt-sum
(gnc-numeric-to-double
(gnc-budget-get-account-period-value budget acct period))))
;; take care of the reverse account preference
;; only needed for the actual value
;; for the budget value this is already included
(if (gnc-reverse-balance acct)
(set! actual-period-value
(gnc-numeric-neg actual-period-value)
)
)
(set! bgt-sum
(gnc-numeric-to-double
(gnc-budget-get-account-period-value budget acct period)))
)
(set! bgt-vals (append bgt-vals (list bgt-sum)))
;; do the conversion
(set! budget-period-value (gnc-numeric-to-double budget-period-value))
(set! actual-period-value (gnc-numeric-to-double actual-period-value))
(if running-sum
(set! act-sum (+ act-sum
(gnc-numeric-to-double
(gnc-budget-get-account-period-actual-value budget acct period))))
;; Add calc new running sum and add to list
(if running-sum
(set! bgt-sum (+ bgt-sum budget-period-value))
;; else
(set! bgt-sum budget-period-value)
)
(set! bgt-vals (append bgt-vals (list bgt-sum)))
(set! act-sum
(gnc-numeric-to-double
(gnc-budget-get-account-period-actual-value budget acct period)))
)
(set! act-vals (append act-vals (list act-sum)))
(if running-sum
(set! act-sum (+ act-sum actual-period-value))
;; else
(set! act-sum actual-period-value)
)
(set! act-vals (append act-vals (list act-sum)))
;; Add period to date list
(set! date (gnc-budget-get-period-start-date budget period))
(set! date-list (append date-list (list (gnc-print-date date))))
;; Add period to date list
(set! date (gnc-budget-get-period-start-date budget period))
(set! date-list (append date-list (list (gnc-print-date date))))
(set! period (+ period 1))
)
(set! period (+ period 1))
);; end of let
);; end of while
;; Add data to chart
(gnc:html-barchart-append-column! chart bgt-vals)

View File

@ -570,11 +570,32 @@
;; Total expenses.
(set! expense-total
(gnc:get-assoc-account-balances-total expense-account-balances))
(if (gnc-reverse-balance (car expense-accounts))
(set! expense-total
(gnc:commodity-collector-get-negated expense-total)
)
)
;; Function to get individual expense account total.
(set! expense-get-balance-fn
(lambda (account start-date end-date)
(gnc:select-assoc-account-balance expense-account-balances account)))
(let
(
(individual-balance
(gnc:select-assoc-account-balance
expense-account-balances
account
)
)
)
(if (gnc-reverse-balance account)
(gnc:commodity-collector-get-negated individual-balance)
;; else
individual-balance
)
)
)
)
(gnc:report-percent-done 10)
@ -592,13 +613,33 @@
;; Total revenue.
(set! revenue-total
(gnc:get-assoc-account-balances-total revenue-account-balances))
(if (not (gnc-reverse-balance (car revenue-accounts)))
(set! revenue-total
(gnc:commodity-collector-get-negated revenue-total)
)
)
;; Function to get individual revenue account total.
;; Budget revenue is always positive, so this must be negated.
(set! revenue-get-balance-fn
(lambda (account start-date end-date)
(gnc:commodity-collector-get-negated
(gnc:select-assoc-account-balance revenue-account-balances account))))
(let
(
(individual-balance
(gnc:select-assoc-account-balance
revenue-account-balances
account
)
)
)
(if (gnc-reverse-balance account)
(gnc:commodity-collector-get-negated individual-balance)
;; else
individual-balance
)
)
)
)
(gnc:report-percent-done 20)