27#include <glib/gi18n.h>
35#include "ScrubBudget.h"
37static QofLogModule log_module =
"gnc.engine.scrub";
42 HEURISTICS_CREDIT_ACC,
54 gint asset,liability,equity,income,expense;
60process_heuristics_acct (
Account * account, gpointer user_data)
65 gnc_numeric total = gnc_numeric_zero(), val;
69 for (gint i = 0; i < heuristics->num_periods; ++i)
71 if (!gnc_budget_is_account_period_value_set (heuristics->budget, account, i))
73 val = gnc_budget_get_account_period_value (heuristics->budget, account, i);
74 total = gnc_numeric_add_fixed (total, val);
79 PINFO (
"acct=%s, total=%s, sign=%d",
86 heuristics->asset += sign;
89 heuristics->liability += sign;
92 heuristics->expense += sign;
95 heuristics->income += sign;
98 heuristics->equity += sign;
106heuristics_on_budget (GncBudget * budget,
Account *root)
108 ProcessData heuristics = {0, 0, 0, 0, 0, gnc_budget_get_num_periods (budget),
110 SignReversals result;
112 gnc_account_foreach_descendant (root, process_heuristics_acct, &heuristics);
115 heuristics.expense < 0 ? HEURISTICS_INC_EXP :
116 heuristics.income < 0 ? HEURISTICS_NONE :
117 HEURISTICS_CREDIT_ACC;
119 LEAVE (
"heuristics_on_budget %s: A(%d) L(%d) Inc(%d) Exp(%d) Eq(%d) = %d",
120 gnc_budget_get_name (budget),
121 heuristics.asset, heuristics.liability, heuristics.income,
122 heuristics.expense, heuristics.equity, result);
128fix_budget_acc_sign (
Account *acc, gpointer user_data)
131 GncBudget* budget = reversal->budget;
132 guint numperiods = gnc_budget_get_num_periods (budget);
137 switch (reversal->policy)
139 case HEURISTICS_INC_EXP:
140 if ((type != ACCT_TYPE_INCOME) && (type != ACCT_TYPE_EXPENSE))
142 PINFO (
"budget account [%s] is inc/exp. reverse!",
145 case HEURISTICS_CREDIT_ACC:
146 if ((type != ACCT_TYPE_LIABILITY) &&
147 (type != ACCT_TYPE_EQUITY) &&
148 (type != ACCT_TYPE_INCOME))
150 PINFO (
"budget account [%s] is credit-account. reverse!",
158 for (guint i=0; i < numperiods; ++i)
161 if (!gnc_budget_is_account_period_value_set (budget, acc, i))
164 amt = gnc_budget_get_account_period_value (budget, acc, i);
166 gnc_budget_set_account_period_value (budget, acc, i, amt);
173maybe_scrub_budget (
QofInstance* data, gpointer user_data)
175 GncBudget* budget = GNC_BUDGET(data);
179 reversal.policy = heuristics_on_budget (budget, root);
180 if (reversal.policy == HEURISTICS_NONE)
182 PWARN (
"budget [%s] doesn't need reversing", gnc_budget_get_name (budget));
186 reversal.budget = budget;
188 ENTER (
"processing budget [%s] for reversal", gnc_budget_get_name (budget));
189 gnc_account_foreach_descendant (root, fix_budget_acc_sign, &reversal);
190 LEAVE (
"completed budget [%s] for reversal", gnc_budget_get_name (budget));
194gnc_maybe_scrub_all_budget_signs (
QofBook *book)
198 gboolean featured = gnc_features_check_used (book, GNC_FEATURE_BUDGET_UNREVERSED);
201 if (has_no_budgets && featured)
204 PWARN (
"There are no budgets, removing feature BUDGET_UNREVERSED");
207 if (has_no_budgets || featured)
212 qof_collection_foreach (collection, maybe_scrub_budget,
213 gnc_book_get_root_account (book));
Utility functions for file access.
Generic api to store and retrieve preferences.
const char * xaccAccountGetName(const Account *acc)
Get the account's name.
GNCAccountType xaccAccountGetType(const Account *acc)
Returns the account's account type.
@ ACCT_TYPE_INCOME
Income accounts are used to denote income.
@ ACCT_TYPE_EXPENSE
Expense accounts are used to denote expenses.
@ ACCT_TYPE_ASSET
asset (and liability) accounts indicate generic, generalized accounts that are none of the above.
@ ACCT_TYPE_LIABILITY
liability (and asset) accounts indicate generic, generalized accounts that are none of the above.
@ ACCT_TYPE_EQUITY
Equity account is used to balance the balance sheet.
QofCollection * qof_book_get_collection(const QofBook *book, QofIdType entity_type)
Return The table of entities of the given type.
GNCAccountType xaccAccountTypeGetFundamental(GNCAccountType t)
Convenience function to return the fundamental type asset/liability/income/expense/equity given an ac...
guint qof_collection_count(const QofCollection *col)
return the number of entities in the collection.
#define PINFO(format, args...)
Print an informational note.
#define LEAVE(format, args...)
Print a function exit debugging message.
#define PWARN(format, args...)
Log a warning.
#define ENTER(format, args...)
Print a function entry debugging message.
gchar * gnc_numeric_to_string(gnc_numeric n)
Convert to string.
int gnc_numeric_compare(gnc_numeric a, gnc_numeric b)
Returns 1 if a>b, -1 if b>a, 0 if a == b
gnc_numeric gnc_numeric_neg(gnc_numeric a)
Returns a newly created gnc_numeric that is the negative of the given gnc_numeric value.
void gnc_features_set_unused(QofBook *book, const gchar *feature)
Indicate that the current book does not use the given feature.
void gnc_features_set_used(QofBook *book, const gchar *feature)
Indicate that the current book uses the given feature.