/
gnc-ui-balances.c
452 lines (376 loc) · 16.3 KB
/
gnc-ui-balances.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/********************************************************************\
* gnc-ui-balances.c -- utility functions for calculating *
* account and owner balances used in the *
* the GnuCash UI *
* Copyright (C) 2000 Dave Peticolas <dave@krondo.com> *
* Copyright (C) 2011 Geert Janssens <geert@kobaltwit.be> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
\********************************************************************/
#include <config.h>
#include "gnc-ui-balances.h"
#include "gnc-ui-util.h"
#include <glib.h>
#include <glib/gi18n.h>
#include "Account.h"
#include "Split.h"
#include "gncOwner.h"
#include "qof.h"
G_GNUC_UNUSED static QofLogModule log_module = GNC_MOD_GUI;
/********************************************************************
* Balance calculations related to accounts
********************************************************************/
/*
* This is a wrapper routine around an xaccGetBalanceInCurrency
* function that handles additional needs of the gui.
*
* @param fn The underlying function in Account.c to call to retrieve
* a specific balance from the account.
* @param account The account to retrieve data about.
* @param recurse Include all sub-accounts of this account.
* @param negative An indication of whether or not the returned value
* is negative. This can be used by the caller to
* easily decode whether or not to color the output.
* @param commodity The commodity in which the account balance should
* be returned. If NULL, the value will be returned in
* the commodity of the account. This is normally used
* to specify a currency, which forces the conversion
* of things like stock account values from share
* values to an amount the requested currency.
*/
gnc_numeric
gnc_ui_account_get_balance_full (xaccGetBalanceInCurrencyFn fn,
const Account *account,
gboolean recurse,
gboolean *negative,
const gnc_commodity *commodity)
{
gnc_numeric balance;
balance = fn(account, commodity, recurse);
/* reverse sign if needed */
if (gnc_reverse_balance (account))
balance = gnc_numeric_neg (balance);
/* Record whether the balance is negative. */
if (negative)
*negative = gnc_numeric_negative_p(balance);
return balance;
}
/*
* This routine retrieves the total balance in an account, possibly
* including all sub-accounts under the specified account.
*/
gnc_numeric
gnc_ui_account_get_balance (const Account *account, gboolean recurse)
{
return gnc_ui_account_get_balance_full (xaccAccountGetBalanceInCurrency,
account, recurse, NULL, NULL);
}
/*
* This routine retrieves the total balance in an account converted to
* a given currency, possibly including all sub-accounts under the
* specified account.
*
gnc_numeric
gnc_ui_account_get_balance_in_currency (const Account *account,
const gnc_commodity *currency,
gboolean recurse)
{
return gnc_ui_account_get_balance_full (xaccAccountGetBalanceInCurrency,
account, recurse, NULL, currency);
}
*/
/*
* This routine retrieves the reconciled balance in an account,
* possibly including all sub-accounts under the specified account.
*/
gnc_numeric
gnc_ui_account_get_reconciled_balance (const Account *account,
gboolean recurse)
{
return gnc_ui_account_get_balance_full (xaccAccountGetReconciledBalanceInCurrency,
account, recurse, NULL, NULL);
}
/**
* Wrapper around gnc_ui_account_get_balance_full that converts
* the resulting number to a character string. The number is
* formatted according to the specification of the account currency.
* The caller is responsible for g_free'ing the returned memory.
*
* @param fn The underlying function in Account.c to call to retrieve
* a specific balance from the account.
* @param account The account to retrieve data about.
* @param recurse Include all sub-accounts of this account.
* @param negative An indication of whether or not the returned value
* is negative. This can be used by the caller to
* easily decode whether or not to color the output.
*/
gchar *
gnc_ui_account_get_print_balance (xaccGetBalanceInCurrencyFn fn,
const Account *account,
gboolean recurse,
gboolean *negative)
{
GNCPrintAmountInfo print_info;
gnc_numeric balance;
balance = gnc_ui_account_get_balance_full(fn, account, recurse,
negative, NULL);
print_info = gnc_account_print_info(account, TRUE);
return g_strdup (gnc_print_amount_with_bidi_ltr_isolate (balance, print_info));
}
/**
* Wrapper around gnc_ui_account_get_balance_full that converts
* the resulting number to a character string. The number is
* formatted according to the specification of the default reporting
* currency.
*
* @param fn The underlying function in Account.c to call to retrieve
* a specific balance from the account.
* @param account The account to retrieve data about.
* @param recurse Include all sub-accounts of this account.
* @param negative An indication of whether or not the returned value
* is negative. This can be used by the caller to
* easily decode whether or not to color the output.
*/
gchar *
gnc_ui_account_get_print_report_balance (xaccGetBalanceInCurrencyFn fn,
const Account *account,
gboolean recurse,
gboolean *negative)
{
GNCPrintAmountInfo print_info;
gnc_numeric balance;
gnc_commodity *report_commodity;
report_commodity = gnc_default_report_currency();
balance = gnc_ui_account_get_balance_full(fn, account, recurse,
negative, report_commodity);
print_info = gnc_commodity_print_info(report_commodity, TRUE);
return g_strdup (gnc_print_amount_with_bidi_ltr_isolate (balance, print_info));
}
static gnc_numeric
account_get_balance_as_of_date (Account *account,
time64 date,
gboolean include_children,
xaccGetBalanceAsOfDateFn fn)
{
QofBook *book = gnc_account_get_book (account);
GNCPriceDB *pdb = gnc_pricedb_get_db (book);
gnc_numeric balance;
gnc_commodity *currency;
if (account == NULL)
return gnc_numeric_zero ();
currency = xaccAccountGetCommodity (account);
balance = fn (account, date);
if (include_children)
{
GList *children, *node;
children = gnc_account_get_descendants(account);
for (node = children; node; node = node->next)
{
Account *child;
gnc_commodity *child_currency;
gnc_numeric child_balance;
child = node->data;
child_currency = xaccAccountGetCommodity (child);
child_balance = fn (child, date);
child_balance =
gnc_pricedb_convert_balance_latest_price (pdb, child_balance,
child_currency,
currency);
balance = gnc_numeric_add_fixed (balance, child_balance);
}
g_list_free(children);
}
/* reverse sign if needed */
if (gnc_reverse_balance (account))
balance = gnc_numeric_neg (balance);
return balance;
}
gnc_numeric
gnc_ui_account_get_balance_as_of_date (Account *account,
time64 date,
gboolean include_children)
{
return account_get_balance_as_of_date (account, date, include_children,
xaccAccountGetBalanceAsOfDate);
}
gnc_numeric
gnc_ui_account_get_reconciled_balance_as_of_date (Account *account,
time64 date,
gboolean include_children)
{
return account_get_balance_as_of_date (account, date, include_children,
xaccAccountGetReconciledBalanceAsOfDate);
}
static gint
account_balance_limit_reached (const Account *account, gnc_numeric balance_limit)
{
gboolean inc_sub = xaccAccountGetIncludeSubAccountBalances (account);
// we use today's date because account may have future dated splits
gnc_numeric balance = gnc_ui_account_get_balance_as_of_date (
(Account*)account,
gnc_time64_get_day_end (gnc_time (NULL)),
inc_sub);
if (gnc_numeric_zero_p (balance))
return 0;
if (gnc_reverse_balance (account))
balance_limit = gnc_numeric_neg (balance_limit);
// Returns 1 if a>b, -1 if b>a, 0 if a == b
return gnc_numeric_compare (balance, balance_limit);
}
gboolean
gnc_ui_account_is_higher_balance_limit_reached (const Account *account,
gboolean *is_zero)
{
gnc_numeric balance_limit;
gboolean limit_valid = FALSE;
gboolean retval = FALSE;
gint compare_result;
g_return_val_if_fail (GNC_IS_ACCOUNT(account), FALSE);
if (gnc_reverse_balance (account))
limit_valid = xaccAccountGetLowerBalanceLimit (account, &balance_limit);
else
limit_valid = xaccAccountGetHigherBalanceLimit (account, &balance_limit);
if (!limit_valid)
return retval;
if (gnc_numeric_zero_p (balance_limit))
*is_zero = TRUE;
if (account_balance_limit_reached (account, balance_limit) == 1)
retval = TRUE;
return retval;
}
gboolean
gnc_ui_account_is_lower_balance_limit_reached (const Account *account,
gboolean *is_zero)
{
gnc_numeric balance_limit;
gboolean limit_valid = FALSE;
gboolean retval = FALSE;
gint compare_result;
g_return_val_if_fail (GNC_IS_ACCOUNT(account), FALSE);
if (gnc_reverse_balance (account))
limit_valid = xaccAccountGetHigherBalanceLimit (account, &balance_limit);
else
limit_valid = xaccAccountGetLowerBalanceLimit (account, &balance_limit);
if (!limit_valid)
return retval;
if (gnc_numeric_zero_p (balance_limit))
*is_zero = TRUE;
if (account_balance_limit_reached (account, balance_limit) == -1)
retval = TRUE;
return retval;
}
gchar *
gnc_ui_account_get_balance_limit_icon_name (const Account *account)
{
gboolean lower_limit_reached, higher_limit_reached;
gboolean lower_is_zero = FALSE;
gboolean higher_is_zero = FALSE;
g_return_val_if_fail (GNC_IS_ACCOUNT(account), g_strdup (""));
higher_limit_reached = gnc_ui_account_is_higher_balance_limit_reached (account, &higher_is_zero);
// assume the higher value will be set mostly so test that first
if (higher_limit_reached && !higher_is_zero)
return g_strdup ("go-top");
lower_limit_reached = gnc_ui_account_is_lower_balance_limit_reached (account, &lower_is_zero);
if (lower_limit_reached && (!lower_is_zero || !higher_is_zero))
return g_strdup ("go-bottom");
if (higher_limit_reached && !lower_is_zero)
return g_strdup ("go-top");
if ((lower_limit_reached || higher_limit_reached ) && lower_is_zero && higher_is_zero)
return g_strdup ("dialog-warning");
return g_strdup ("");
}
/********************************************************************
* Balance calculations related to owners
********************************************************************/
/*
* This is a wrapper routine around an gncOwnerGetBalanceInCurrency
* function that handles additional needs of the gui.
*
* @param owner The owner to retrieve data about.
* @param negative An indication of whether or not the returned value
* is negative. This can be used by the caller to
* easily decode whether or not to color the output.
* @param commodity The commodity in which the account balance should
* be returned. If NULL, the value will be returned in
* the commodity of the owner. This is normally used
* to specify a currency, which forces the conversion
* of things like stock account values from share
* values to an amount the requested currency.
*/
gnc_numeric
gnc_ui_owner_get_balance_full (GncOwner *owner,
gboolean *negative,
const gnc_commodity *commodity)
{
gnc_numeric balance;
if (!owner)
return gnc_numeric_zero ();
balance = gncOwnerGetBalanceInCurrency (owner, commodity);
/* reverse sign if needed */
if ((gncOwnerGetType (owner) != GNC_OWNER_CUSTOMER))
balance = gnc_numeric_neg (balance);
/* Record whether the balance is negative. */
if (negative)
*negative = gnc_numeric_negative_p (balance);
return balance;
}
/**
* Wrapper around gnc_ui_owner_get_balance_full that converts
* the resulting number to a character string. The number is
* formatted according to the specification of the owner currency.
* The caller is responsible for g_free'ing the returned memory.
*
* @param owner The owner to retrieve data about.
* @param negative An indication of whether or not the returned value
* is negative. This can be used by the caller to
* easily decode whether or not to color the output.
*/
gchar *
gnc_ui_owner_get_print_balance (GncOwner *owner,
gboolean *negative)
{
gnc_numeric balance;
GNCPrintAmountInfo print_info;
balance = gnc_ui_owner_get_balance_full (owner, negative, NULL);
print_info = gnc_commodity_print_info (gncOwnerGetCurrency (owner), TRUE);
return g_strdup (gnc_print_amount_with_bidi_ltr_isolate (balance, print_info));
}
/**
* Wrapper around gnc_ui_owner_get_balance_full that converts
* the resulting number to a character string. The number is
* formatted according to the specification of the default reporting
* currency.
*
* @param account The owner to retrieve data about.
* @param negative An indication of whether or not the returned value
* is negative. This can be used by the caller to
* easily decode whether or not to color the output.
*/
gchar *
gnc_ui_owner_get_print_report_balance (GncOwner *owner,
gboolean *negative)
{
GNCPrintAmountInfo print_info;
gnc_numeric balance;
gnc_commodity *report_commodity;
report_commodity = gnc_default_report_currency ();
balance = gnc_ui_owner_get_balance_full (owner, negative,
report_commodity);
print_info = gnc_commodity_print_info (report_commodity, TRUE);
return g_strdup (gnc_print_amount_with_bidi_ltr_isolate (balance, print_info));
}