GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-ui-balances.c
1 /********************************************************************\
2  * gnc-ui-balances.c -- utility functions for calculating *
3  * account and owner balances used in the *
4  * the GnuCash UI *
5  * Copyright (C) 2000 Dave Peticolas <[email protected]> *
6  * Copyright (C) 2011 Geert Janssens <[email protected]> *
7  * *
8  * This program is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU General Public License as *
10  * published by the Free Software Foundation; either version 2 of *
11  * the License, or (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License*
19  * along with this program; if not, contact: *
20  * *
21  * Free Software Foundation Voice: +1-617-542-5942 *
22  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
23  * Boston, MA 02110-1301, USA [email protected] *
24 \********************************************************************/
25 
26 #include "config.h"
27 
28 #include "gnc-ui-balances.h"
29 #include "gnc-ui-util.h"
30 
31 #include <glib.h>
32 
33 #include "Account.h"
34 #include "gncOwner.h"
35 #include "qof.h"
36 
37 G_GNUC_UNUSED static QofLogModule log_module = GNC_MOD_GUI;
38 
39 /********************************************************************
40  * Balance calculations related to accounts
41  ********************************************************************/
42 
43 /*
44  * This is a wrapper routine around an xaccGetBalanceInCurrency
45  * function that handles additional needs of the gui.
46  *
47  * @param fn The underlying function in Account.c to call to retrieve
48  * a specific balance from the account.
49  * @param account The account to retrieve data about.
50  * @param recurse Include all sub-accounts of this account.
51  * @param negative An indication of whether or not the returned value
52  * is negative. This can be used by the caller to
53  * easily decode whether or not to color the output.
54  * @param commodity The commodity in which the account balance should
55  * be returned. If NULL, the value will be returned in
56  * the commodity of the account. This is normally used
57  * to specify a currency, which forces the conversion
58  * of things like stock account values from share
59  * values to an amount the requested currency.
60  */
62 gnc_ui_account_get_balance_full (xaccGetBalanceInCurrencyFn fn,
63  const Account *account,
64  gboolean recurse,
65  gboolean *negative,
66  const gnc_commodity *commodity)
67 {
68  gnc_numeric balance;
69 
70  balance = fn(account, commodity, recurse);
71 
72  /* reverse sign if needed */
73  if (gnc_reverse_balance (account))
74  balance = gnc_numeric_neg (balance);
75 
76  /* Record whether the balance is negative. */
77  if (negative)
78  *negative = gnc_numeric_negative_p(balance);
79 
80  return balance;
81 }
82 
83 /*
84  * This routine retrieves the total balance in an account, possibly
85  * including all sub-accounts under the specified account.
86  */
88 gnc_ui_account_get_balance (const Account *account, gboolean recurse)
89 {
90  return gnc_ui_account_get_balance_full (xaccAccountGetBalanceInCurrency,
91  account, recurse, NULL, NULL);
92 }
93 
94 /*
95  * This routine retrieves the total balance in an account converted to
96  * a given currency, possibly including all sub-accounts under the
97  * specified account.
98  *
99 gnc_numeric
100 gnc_ui_account_get_balance_in_currency (const Account *account,
101  const gnc_commodity *currency,
102  gboolean recurse)
103 {
104  return gnc_ui_account_get_balance_full (xaccAccountGetBalanceInCurrency,
105  account, recurse, NULL, currency);
106 }
107 */
108 /*
109  * This routine retrieves the reconciled balance in an account,
110  * possibly including all sub-accounts under the specified account.
111  */
113 gnc_ui_account_get_reconciled_balance (const Account *account,
114  gboolean recurse)
115 {
116  return gnc_ui_account_get_balance_full (xaccAccountGetReconciledBalanceInCurrency,
117  account, recurse, NULL, NULL);
118 }
119 
120 
135 gchar *
136 gnc_ui_account_get_print_balance (xaccGetBalanceInCurrencyFn fn,
137  const Account *account,
138  gboolean recurse,
139  gboolean *negative)
140 {
141  GNCPrintAmountInfo print_info;
142  gnc_numeric balance;
143 
144  balance = gnc_ui_account_get_balance_full(fn, account, recurse,
145  negative, NULL);
146  print_info = gnc_account_print_info(account, TRUE);
147  return g_strdup(xaccPrintAmount(balance, print_info));
148 }
149 
150 
165 gchar *
166 gnc_ui_account_get_print_report_balance (xaccGetBalanceInCurrencyFn fn,
167  const Account *account,
168  gboolean recurse,
169  gboolean *negative)
170 {
171  GNCPrintAmountInfo print_info;
172  gnc_numeric balance;
173  gnc_commodity *report_commodity;
174 
175  report_commodity = gnc_default_report_currency();
176  balance = gnc_ui_account_get_balance_full(fn, account, recurse,
177  negative, report_commodity);
178  print_info = gnc_commodity_print_info(report_commodity, TRUE);
179  return g_strdup(xaccPrintAmount(balance, print_info));
180 }
181 
182 
184 gnc_ui_account_get_balance_as_of_date (Account *account,
185  time64 date,
186  gboolean include_children)
187 {
188  gnc_numeric balance;
189  gnc_commodity *currency;
190 
191  if (account == NULL)
192  return gnc_numeric_zero ();
193 
194  currency = xaccAccountGetCommodity (account);
195  balance = xaccAccountGetBalanceAsOfDate (account, date);
196 
197  if (include_children)
198  {
199  GList *children, *node;
200 
201  children = gnc_account_get_descendants(account);
202 
203  for (node = children; node; node = node->next)
204  {
205  Account *child;
206  gnc_commodity *child_currency;
207  gnc_numeric child_balance;
208 
209  child = node->data;
210  child_currency = xaccAccountGetCommodity (child);
211  child_balance = xaccAccountGetBalanceAsOfDate (child, date);
212  child_balance = xaccAccountConvertBalanceToCurrency (child,
213  child_balance, child_currency, currency);
214  balance = gnc_numeric_add_fixed (balance, child_balance);
215  }
216 
217  g_list_free(children);
218  }
219 
220  /* reverse sign if needed */
221  if (gnc_reverse_balance (account))
222  balance = gnc_numeric_neg (balance);
223 
224  return balance;
225 }
226 
227 
228 /********************************************************************
229  * Balance calculations related to owners
230  ********************************************************************/
231 
232 /*
233  * This is a wrapper routine around an gncOwnerGetBalanceInCurrency
234  * function that handles additional needs of the gui.
235  *
236  * @param owner The owner to retrieve data about.
237  * @param negative An indication of whether or not the returned value
238  * is negative. This can be used by the caller to
239  * easily decode whether or not to color the output.
240  * @param commodity The commodity in which the account balance should
241  * be returned. If NULL, the value will be returned in
242  * the commodity of the owner. This is normally used
243  * to specify a currency, which forces the conversion
244  * of things like stock account values from share
245  * values to an amount the requested currency.
246  */
248 gnc_ui_owner_get_balance_full (GncOwner *owner,
249  gboolean *negative,
250  const gnc_commodity *commodity)
251 {
252  gnc_numeric balance;
253 
254  if (!owner)
255  return gnc_numeric_zero ();
256 
257  balance = gncOwnerGetBalanceInCurrency (owner, commodity);
258 
259  /* reverse sign if needed */
260  if ((gncOwnerGetType (owner) != GNC_OWNER_CUSTOMER))
261  balance = gnc_numeric_neg (balance);
262 
263  /* Record whether the balance is negative. */
264  if (negative)
265  *negative = gnc_numeric_negative_p (balance);
266 
267  return balance;
268 }
269 
270 
282 gchar *
283 gnc_ui_owner_get_print_balance (GncOwner *owner,
284  gboolean *negative)
285 {
286  gnc_numeric balance;
287  GNCPrintAmountInfo print_info;
288 
289  balance = gnc_ui_owner_get_balance_full (owner, negative, NULL);
290  print_info = gnc_commodity_print_info (gncOwnerGetCurrency (owner), TRUE);
291  return g_strdup (xaccPrintAmount (balance, print_info));
292 }
293 
305 gchar *
306 gnc_ui_owner_get_print_report_balance (GncOwner *owner,
307  gboolean *negative)
308 {
309  GNCPrintAmountInfo print_info;
310  gnc_numeric balance;
311  gnc_commodity *report_commodity;
312 
313  report_commodity = gnc_default_report_currency ();
314  balance = gnc_ui_owner_get_balance_full (owner, negative,
315  report_commodity);
316  print_info = gnc_commodity_print_info (report_commodity, TRUE);
317  return g_strdup (xaccPrintAmount (balance, print_info));
318 }
319 
Business Interface: Object OWNERs.
utility functions for the GnuCash UI
gnc_numeric gnc_numeric_neg(gnc_numeric a)
gnc_commodity * gnc_default_report_currency(void)
Definition: gnc-ui-util.c:972
gboolean gnc_numeric_negative_p(gnc_numeric a)
Account handling public routines.
gnc_numeric gncOwnerGetBalanceInCurrency(const GncOwner *owner, const gnc_commodity *report_currency)
Definition: gncOwner.c:1417
gnc_numeric xaccAccountGetBalanceAsOfDate(Account *acc, time64 date)
Definition: Account.c:3288
GncOwnerType gncOwnerGetType(const GncOwner *owner)
Definition: gncOwner.c:201
GList * gnc_account_get_descendants(const Account *account)
Definition: Account.c:2755
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Definition: Account.c:3148
gint64 time64
Definition: gnc-date.h:83
const gchar * QofLogModule
Definition: qofid.h:89