-
Notifications
You must be signed in to change notification settings - Fork 760
/
gnc-ui-util.c
2376 lines (2027 loc) · 71.7 KB
/
gnc-ui-util.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************\
* gnc-ui-util.c -- utility functions for the GnuCash UI *
* Copyright (C) 2000 Dave Peticolas <dave@krondo.com> *
* *
* 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>
#ifdef __MINGW32__
#define __USE_MINGW_ANSI_STDIO 1
#endif
#include "gnc-ui-util.h"
#include <glib.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <locale.h>
#include <math.h>
#if defined(G_OS_WIN32) && !defined(_MSC_VER)
# include <pow.h>
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "qof.h"
#include "gnc-prefs.h"
#include "Account.h"
#include "Transaction.h"
#include "gnc-engine.h"
#include "gnc-features.h"
#include "gnc-hooks.h"
#include "gnc-session.h"
#include "engine-helpers.h"
#include "gnc-locale-utils.h"
#define GNC_PREF_CURRENCY_CHOICE_LOCALE "currency-choice-locale"
#define GNC_PREF_CURRENCY_CHOICE_OTHER "currency-choice-other"
#define GNC_PREF_CURRENCY_OTHER "currency-other"
#define GNC_PREF_REVERSED_ACCTS_NONE "reversed-accounts-none"
#define GNC_PREF_REVERSED_ACCTS_CREDIT "reversed-accounts-credit"
#define GNC_PREF_REVERSED_ACCTS_INC_EXP "reversed-accounts-incomeexpense"
#define GNC_PREF_PRICES_FORCE_DECIMAL "force-price-decimal"
static QofLogModule log_module = GNC_MOD_GUI;
static gboolean auto_decimal_enabled = FALSE;
static int auto_decimal_places = 2; /* default, can be changed */
static gboolean reverse_balance_inited = FALSE;
static gboolean reverse_type[NUM_ACCOUNT_TYPES];
/* Cache currency ISO codes and only look them up in the settings when
* absolutely necessary. Can't cache a pointer to the data structure
* as that will change any time the book changes. */
static gchar *user_default_currency = NULL;
static gchar *user_report_currency = NULL;
const static int maximum_decimals = 15;
static const gint64 pow_10[] = {1, 10, 100, 1000, 10000, 100000, 1000000,
10000000, 100000000, 1000000000, 10000000000,
100000000000, 1000000000000, 10000000000000,
100000000000000, 1000000000000000};
gchar *gnc_normalize_account_separator (const gchar* separator)
{
gchar *new_sep=NULL;
if (!separator || !*separator || g_strcmp0(separator, "colon") == 0)
new_sep = g_strdup (":");
else if (g_strcmp0(separator, "slash") == 0)
new_sep = g_strdup ("/");
else if (g_strcmp0(separator, "backslash") == 0)
new_sep = g_strdup ("\\");
else if (g_strcmp0(separator, "dash") == 0)
new_sep = g_strdup ("-");
else if (g_strcmp0(separator, "period") == 0)
new_sep = g_strdup (".");
else
new_sep = g_strdup (separator);
return new_sep;
}
/********************************************************************\
* gnc_configure_account_separator *
* updates the current account separator character *
* *
* Args: none *
\*******************************************************************/
static void
gnc_configure_account_separator (void)
{
gchar *separator;
char *string;
string = gnc_prefs_get_string(GNC_PREFS_GROUP_GENERAL, GNC_PREF_ACCOUNT_SEPARATOR);
separator = gnc_normalize_account_separator (string);
gnc_set_account_separator(separator);
g_free(string);
g_free(separator);
}
static void
gnc_configure_reverse_balance (void)
{
gint i;
for (i = 0; i < NUM_ACCOUNT_TYPES; i++)
reverse_type[i] = FALSE;
if (gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_REVERSED_ACCTS_INC_EXP))
{
reverse_type[ACCT_TYPE_INCOME] = TRUE;
reverse_type[ACCT_TYPE_EXPENSE] = TRUE;
}
else if (gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_REVERSED_ACCTS_CREDIT))
{
reverse_type[ACCT_TYPE_LIABILITY] = TRUE;
reverse_type[ACCT_TYPE_PAYABLE] = TRUE;
reverse_type[ACCT_TYPE_EQUITY] = TRUE;
reverse_type[ACCT_TYPE_INCOME] = TRUE;
reverse_type[ACCT_TYPE_CREDIT] = TRUE;
}
else if (!gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_REVERSED_ACCTS_NONE))
PWARN("no reversed account preference set, using none");
}
static void
gnc_reverse_balance_init (void)
{
gnc_configure_reverse_balance ();
reverse_balance_inited = TRUE;
}
gboolean
gnc_reverse_balance (const Account *account)
{
int type;
if (account == NULL)
return FALSE;
type = xaccAccountGetType (account);
if ((type < 0) || (type >= NUM_ACCOUNT_TYPES))
return FALSE;
if (!reverse_balance_inited)
gnc_reverse_balance_init ();
return reverse_type[type];
}
gboolean gnc_using_equity_type_opening_balance_account (QofBook* book)
{
return gnc_features_check_used (book, GNC_FEATURE_EQUITY_TYPE_OPENING_BALANCE);
}
void gnc_set_use_equity_type_opening_balance_account (QofBook* book)
{
gnc_features_set_used (book, GNC_FEATURE_EQUITY_TYPE_OPENING_BALANCE);
}
gchar *
gnc_get_default_directory (const gchar *section)
{
gchar *dir;
dir = gnc_prefs_get_string (section, GNC_PREF_LAST_PATH);
if (!(dir && *dir))
{
g_free (dir); // if it's ""
#ifdef G_OS_WIN32
dir = g_strdup (g_get_user_data_dir ()); /* equivalent of "My Documents" */
#else
dir = g_strdup (g_get_home_dir ());
#endif
}
return dir;
}
void
gnc_set_default_directory (const gchar *section, const gchar *directory)
{
gnc_prefs_set_string(section, GNC_PREF_LAST_PATH, directory);
}
QofBook *
gnc_get_current_book (void)
{
return qof_session_get_book (gnc_get_current_session ());
}
/* If there is no current session, there is no book and we must be dealing
* with a new book. When gnucash is started with --nofile, there is
* initially no session (and no book), but by the time we check, one
* could have been created (for example, if an empty account tree tab is
* opened, a session is created which creates a new, but empty, book).
* A session is created and a book is loaded from a backend when gnucash is
* started with a file, but selecting 'new file' keeps a session open. So we
* need to check as well for a book with no accounts (root with no children). */
gboolean
gnc_is_new_book (void)
{
return ((!gnc_current_session_exist() ||
(gnc_current_session_exist() &&
!gnc_account_get_children(
gnc_book_get_root_account(
gnc_get_current_book()))))
? TRUE : FALSE);
}
#define OPTION_TAXUS_NAME "tax_US/name"
#define OPTION_TAXUS_TYPE "tax_US/type"
#define OLD_OPTION_TAXUS_NAME "book/tax_US/name"
#define OLD_OPTION_TAXUS_TYPE "book/tax_US/type"
void
gnc_set_current_book_tax_name_type (gboolean name_changed, const gchar *tax_name,
gboolean type_changed, const gchar *tax_type)
{
if (name_changed)
{
if (type_changed)
{
QofBook* book = gnc_get_current_book();
if ((g_strcmp0 (tax_name, "") == 0) ||
(tax_name == NULL))
{ /* change to no name */
if ((g_strcmp0 (tax_type, "Other") == 0) ||
(g_strcmp0 (tax_type, "") == 0) ||
(tax_type == NULL))
{ /* need to delete both name and type and the "tax_US" frame */
qof_book_set_string_option(book, OPTION_TAXUS_NAME, NULL);
qof_book_set_string_option(book, OPTION_TAXUS_TYPE, NULL);
qof_book_option_frame_delete(book, "tax_US");
}
else
{ /* delete the name & change the type; keep the "tax_US" frame */
qof_book_set_string_option(book, OPTION_TAXUS_NAME, NULL);
qof_book_set_string_option(book, OPTION_TAXUS_TYPE, tax_type);
}
}
else /* new name */
{
if ((g_strcmp0 (tax_type, "Other") == 0) ||
(g_strcmp0 (tax_type, "") == 0) ||
(tax_type == NULL))
{ /* delete the type & change the name; keep the "tax_US" frame */
qof_book_set_string_option(book, OPTION_TAXUS_TYPE, NULL);
qof_book_set_string_option(book, OPTION_TAXUS_NAME, tax_name);
}
else /* and new type */
{ /* change the name & change the type */
qof_book_set_string_option(book, OPTION_TAXUS_NAME, tax_name);
qof_book_set_string_option(book, OPTION_TAXUS_TYPE, tax_type);
}
}
}
else /* no type change but name changed */
{
QofBook* book = gnc_get_current_book();
if ((g_strcmp0 (tax_name, "") == 0) ||
(tax_name == NULL))
{ /* change to no name */
if ((g_strcmp0 (tax_type, "Other") == 0) ||
(g_strcmp0 (tax_type, "") == 0) ||
(tax_type == NULL))
{ /* delete the name; there is no type; deleted the "tax_US" frame */
qof_book_set_string_option(book, OPTION_TAXUS_NAME, NULL);
qof_book_option_frame_delete(book, "tax_US");
}
else
{ /* need to delete the name and keep "tax_US" frame */
qof_book_set_string_option(book, OPTION_TAXUS_NAME, NULL);
}
}
else
{ /* change the name & keep "tax_US" frame */
qof_book_set_string_option(book, OPTION_TAXUS_NAME, tax_name);
}
}
}
else /* no name change */
{
if (type_changed)
{
QofBook* book = gnc_get_current_book();
if ((g_strcmp0 (tax_type, "Other") == 0) ||
(g_strcmp0 (tax_type, "") == 0) ||
(tax_type == NULL))
{
if ((g_strcmp0 (tax_name, "") == 0) ||
(tax_name == NULL))
{/* delete the type; there is no name; delete the "tax_US" frame */
qof_book_set_string_option(book, OPTION_TAXUS_TYPE, NULL);
qof_book_option_frame_delete(book, "tax_US");
}
else
{ /* need to delete the type and keep "tax_US" frame */
qof_book_set_string_option(book, OPTION_TAXUS_TYPE, NULL);
}
}
else
{ /* change the type & keep "tax_US" frame */
qof_book_set_string_option(book, OPTION_TAXUS_TYPE, tax_type);
}
} /*else no name and no type change - do nothing */
}
}
const gchar *
gnc_get_current_book_tax_name (void)
{
QofBook* book = gnc_get_current_book();
const char* tax_name =
qof_book_get_string_option(book, OPTION_TAXUS_NAME);
if (tax_name)
{
return tax_name;
}
else
{
const char* old_option_taxus_name =
qof_book_get_string_option(book, OLD_OPTION_TAXUS_NAME);
if (old_option_taxus_name)
{
char* taxus_name = g_strdup(old_option_taxus_name);
const char* old_option_taxus_type =
qof_book_get_string_option(book, OLD_OPTION_TAXUS_TYPE);
if (old_option_taxus_type)
{ /* switch both name and type and remove unused frames */
char* taxus_type = g_strdup(old_option_taxus_type);
qof_book_set_string_option(book, OPTION_TAXUS_NAME, taxus_name);
qof_book_set_string_option(book, OLD_OPTION_TAXUS_NAME, NULL);
qof_book_set_string_option(book, OPTION_TAXUS_TYPE, taxus_type);
qof_book_set_string_option(book, OLD_OPTION_TAXUS_TYPE, NULL);
qof_book_option_frame_delete(book, "book/tax_US");
qof_book_option_frame_delete(book, "book");
g_free (taxus_type);
}
else
{ /* switch just name and remove unused frames */
qof_book_set_string_option(book, OPTION_TAXUS_NAME, taxus_name);
qof_book_set_string_option(book, OLD_OPTION_TAXUS_NAME, NULL);
qof_book_option_frame_delete(book, "book/tax_US");
qof_book_option_frame_delete(book, "book");
}
g_free (taxus_name);
return qof_book_get_string_option(book, OPTION_TAXUS_NAME);
}
return NULL;
}
}
const gchar *
gnc_get_current_book_tax_type (void)
{
QofBook* book = gnc_get_current_book();
const char* tax_type =
qof_book_get_string_option(book, OPTION_TAXUS_TYPE);
if (tax_type)
{
return tax_type;
}
else
{
const char* old_option_taxus_type =
qof_book_get_string_option(book, OLD_OPTION_TAXUS_TYPE);
if (old_option_taxus_type)
{
char* taxus_type = g_strdup(old_option_taxus_type);
const char* old_option_taxus_name =
qof_book_get_string_option(book, OLD_OPTION_TAXUS_NAME);
if (old_option_taxus_name)
{ /* switch both name and type and remove unused frames */
char* taxus_name = g_strdup(old_option_taxus_name);
qof_book_set_string_option(book, OPTION_TAXUS_NAME, taxus_name);
qof_book_set_string_option(book, OLD_OPTION_TAXUS_NAME, NULL);
qof_book_set_string_option(book, OPTION_TAXUS_TYPE, taxus_type);
qof_book_set_string_option(book, OLD_OPTION_TAXUS_TYPE, NULL);
qof_book_option_frame_delete(book, "book/tax_US");
qof_book_option_frame_delete(book, "book");
g_free (taxus_name);
}
else
{ /* switch just type and remove unused frames */
qof_book_set_string_option(book, OPTION_TAXUS_TYPE, taxus_type);
qof_book_set_string_option(book, OLD_OPTION_TAXUS_TYPE, NULL);
qof_book_option_frame_delete(book, "book/tax_US");
qof_book_option_frame_delete(book, "book");
}
g_free (taxus_type);
return qof_book_get_string_option(book, OPTION_TAXUS_TYPE);
}
return NULL;
}
}
Account *
gnc_get_current_root_account (void)
{
return gnc_book_get_root_account (gnc_get_current_book ());
}
gnc_commodity_table *
gnc_get_current_commodities (void)
{
if (gnc_current_session_exist())
return gnc_commodity_table_get_table (gnc_get_current_book ());
return NULL;
}
gchar *
gnc_get_account_name_for_split_register(const Account *account, gboolean show_leaf_accounts)
{
if (show_leaf_accounts)
return g_strdup (xaccAccountGetName (account));
else
return gnc_account_get_full_name (account);
}
gchar *
gnc_get_account_name_for_register(const Account *account)
{
gboolean show_leaf_accounts;
show_leaf_accounts = gnc_prefs_get_bool(GNC_PREFS_GROUP_GENERAL_REGISTER,
GNC_PREF_SHOW_LEAF_ACCT_NAMES);
return gnc_get_account_name_for_split_register(account, show_leaf_accounts);
}
Account *
gnc_account_lookup_for_register(const Account *base_account, const char *name)
{
gboolean show_leaf_accounts;
show_leaf_accounts = gnc_prefs_get_bool(GNC_PREFS_GROUP_GENERAL_REGISTER,
GNC_PREF_SHOW_LEAF_ACCT_NAMES);
if (show_leaf_accounts)
return gnc_account_lookup_by_name (base_account, name);
else
return gnc_account_lookup_by_full_name (base_account, name);
}
/********************************************************************\
* gnc_get_reconcile_str *
* return the i18n'd string for the given reconciled flag *
* *
* Args: reconciled_flag - the flag to convert into a string *
* Returns: the i18n'd reconciled string *
\********************************************************************/
const char *
gnc_get_reconcile_str (char reconciled_flag)
{
switch (reconciled_flag)
{
case NREC:
return C_("Reconciled flag 'not cleared'", "n");
case CREC:
return C_("Reconciled flag 'cleared'", "c");
case YREC:
return C_("Reconciled flag 'reconciled'", "y");
case FREC:
return C_("Reconciled flag 'frozen'", "f");
case VREC:
return C_("Reconciled flag 'void'", "v");
default:
PERR("Bad reconciled flag\n");
return NULL;
}
}
/********************************************************************\
* gnc_get_reconcile_valid_flags *
* return a string containing the list of reconciled flags *
* *
* Returns: the i18n'd reconciled flags string *
\********************************************************************/
const char *
gnc_get_reconcile_valid_flags (void)
{
static const char flags[] = { NREC, CREC, YREC, FREC, VREC, 0 };
return flags;
}
/********************************************************************\
* gnc_get_reconcile_flag_order *
* return a string containing the reconciled-flag change order *
* *
* Args: reconciled_flag - the flag to convert into a string *
* Returns: the i18n'd reconciled string *
\********************************************************************/
const char *
gnc_get_reconcile_flag_order (void)
{
static const char flags[] = { NREC, CREC, 0 };
return flags;
}
const char *
gnc_get_doclink_str (char link_flag)
{
switch (link_flag)
{
case WLINK:
return C_("Document Link flag for 'web'", "w");
case FLINK:
return C_("Document Link flag for 'file'", "f");
case ' ':
return " ";
default:
PERR("Bad link flag");
return NULL;
}
}
const char *
gnc_get_doclink_valid_flags (void)
{
static const char flags[] = { FLINK, WLINK, ' ', 0 };
return flags;
}
const char *
gnc_get_doclink_flag_order (void)
{
static const char flags[] = { FLINK, WLINK, ' ', 0 };
return flags;
}
static const char *
equity_base_name (GNCEquityType equity_type)
{
switch (equity_type)
{
case EQUITY_OPENING_BALANCE:
return N_("Opening Balances");
case EQUITY_RETAINED_EARNINGS:
return N_("Retained Earnings");
default:
return NULL;
}
}
Account *
gnc_find_or_create_equity_account (Account *root,
GNCEquityType equity_type,
gnc_commodity *currency)
{
Account *parent;
Account *account = NULL;
gboolean name_exists;
gboolean base_name_exists;
const char *base_name;
char *name;
gboolean use_eq_op_feature;
g_return_val_if_fail (equity_type >= 0, NULL);
g_return_val_if_fail (equity_type < NUM_EQUITY_TYPES, NULL);
g_return_val_if_fail (currency != NULL, NULL);
g_return_val_if_fail (root != NULL, NULL);
g_return_val_if_fail (gnc_commodity_is_currency(currency), NULL);
use_eq_op_feature = equity_type == EQUITY_OPENING_BALANCE && gnc_using_equity_type_opening_balance_account (gnc_get_current_book ());
if (use_eq_op_feature)
{
account = gnc_account_lookup_by_opening_balance (root, currency);
if (account)
return account;
}
base_name = equity_base_name (equity_type);
account = gnc_account_lookup_by_name(root, base_name);
if (account && xaccAccountGetType (account) != ACCT_TYPE_EQUITY)
account = NULL;
if (!account)
{
base_name = base_name && *base_name ? _(base_name) : "";
account = gnc_account_lookup_by_name(root, base_name);
if (account && xaccAccountGetType (account) != ACCT_TYPE_EQUITY)
account = NULL;
}
base_name_exists = (account != NULL);
if (account &&
gnc_commodity_equiv (currency, xaccAccountGetCommodity (account)))
{
if (use_eq_op_feature)
xaccAccountSetIsOpeningBalance (account, TRUE);
return account;
}
name = g_strconcat (base_name, " - ",
gnc_commodity_get_mnemonic (currency), NULL);
account = gnc_account_lookup_by_name(root, name);
if (account && xaccAccountGetType (account) != ACCT_TYPE_EQUITY)
account = NULL;
name_exists = (account != NULL);
if (account &&
gnc_commodity_equiv (currency, xaccAccountGetCommodity (account)))
{
if (use_eq_op_feature)
xaccAccountSetIsOpeningBalance (account, TRUE);
return account;
}
/* Couldn't find one, so create it */
if (name_exists && base_name_exists)
{
PWARN ("equity account with unexpected currency");
g_free (name);
return NULL;
}
if (!base_name_exists &&
gnc_commodity_equiv (currency, gnc_default_currency ()))
{
g_free (name);
name = g_strdup (base_name);
}
parent = gnc_account_lookup_by_name(root, _("Equity"));
if (!parent || xaccAccountGetType (parent) != ACCT_TYPE_EQUITY)
parent = root;
g_assert(parent);
account = xaccMallocAccount (gnc_account_get_book(root));
xaccAccountBeginEdit (account);
xaccAccountSetName (account, name);
xaccAccountSetType (account, ACCT_TYPE_EQUITY);
xaccAccountSetCommodity (account, currency);
if (use_eq_op_feature)
xaccAccountSetIsOpeningBalance (account, TRUE);
xaccAccountBeginEdit (parent);
gnc_account_append_child (parent, account);
xaccAccountCommitEdit (parent);
xaccAccountCommitEdit (account);
g_free (name);
return account;
}
gboolean
gnc_account_create_opening_balance (Account *account,
gnc_numeric balance,
time64 date,
QofBook *book)
{
Account *equity_account;
Transaction *trans;
Split *split;
gnc_commodity *commodity;
if (gnc_numeric_zero_p (balance))
return TRUE;
g_return_val_if_fail (account != NULL, FALSE);
commodity = xaccAccountGetCommodity (account);
g_return_val_if_fail (gnc_commodity_is_currency (commodity), FALSE);
equity_account =
gnc_find_or_create_equity_account (gnc_account_get_root(account),
EQUITY_OPENING_BALANCE,
commodity);
if (!equity_account)
return FALSE;
xaccAccountBeginEdit (account);
xaccAccountBeginEdit (equity_account);
trans = xaccMallocTransaction (book);
xaccTransBeginEdit (trans);
xaccTransSetCurrency (trans, gnc_account_or_default_currency (account, NULL));
xaccTransSetDatePostedSecsNormalized (trans, date);
xaccTransSetDescription (trans, _("Opening Balance"));
split = xaccMallocSplit (book);
xaccTransAppendSplit (trans, split);
xaccAccountInsertSplit (account, split);
xaccSplitSetAmount (split, balance);
xaccSplitSetValue (split, balance);
balance = gnc_numeric_neg (balance);
split = xaccMallocSplit (book);
xaccTransAppendSplit (trans, split);
xaccAccountInsertSplit (equity_account, split);
xaccSplitSetAmount (split, balance);
xaccSplitSetValue (split, balance);
xaccTransCommitEdit (trans);
xaccAccountCommitEdit (equity_account);
xaccAccountCommitEdit (account);
return TRUE;
}
gnc_commodity *
gnc_locale_default_currency_nodefault (void)
{
gnc_commodity * currency;
gnc_commodity_table *table;
const char *code;
table = gnc_get_current_commodities ();
code = gnc_locale_default_iso_currency_code ();
currency = gnc_commodity_table_lookup (table, GNC_COMMODITY_NS_CURRENCY, code);
return (currency ? currency : NULL);
}
gnc_commodity *
gnc_locale_default_currency (void)
{
gnc_commodity * currency = gnc_locale_default_currency_nodefault ();
return (currency ? currency :
gnc_commodity_table_lookup (gnc_get_current_commodities (),
GNC_COMMODITY_NS_CURRENCY, "USD"));
}
static gnc_commodity *
gnc_default_currency_common (gchar *requested_currency,
const gchar *section)
{
gnc_commodity *currency = NULL;
gchar *mnemonic;
if (requested_currency)
return gnc_commodity_table_lookup(gnc_get_current_commodities(),
GNC_COMMODITY_NS_CURRENCY,
requested_currency);
if (gnc_current_session_exist() &&
gnc_prefs_get_bool (section, GNC_PREF_CURRENCY_CHOICE_OTHER))
{
mnemonic = gnc_prefs_get_string(section, GNC_PREF_CURRENCY_OTHER);
currency = gnc_commodity_table_lookup(gnc_get_current_commodities(),
GNC_COMMODITY_NS_CURRENCY, mnemonic);
DEBUG("mnemonic %s, result %p",
mnemonic && *mnemonic ? mnemonic : "(null)", currency);
g_free(mnemonic);
}
if (!currency)
currency = gnc_locale_default_currency ();
if (currency)
{
mnemonic = requested_currency;
g_free(mnemonic);
}
return currency;
}
gnc_commodity *
gnc_default_currency (void)
{
return gnc_default_currency_common (user_default_currency, GNC_PREFS_GROUP_GENERAL);
}
gnc_commodity * gnc_account_or_default_currency(const Account* account, gboolean * currency_from_account_found)
{
gnc_commodity *currency;
if (!account)
{
if (currency_from_account_found)
*currency_from_account_found = FALSE;
return gnc_default_currency();
}
currency = gnc_account_get_currency_or_parent(account);
if (currency)
{
if (currency_from_account_found)
*currency_from_account_found = TRUE;
}
else
{
if (currency_from_account_found)
*currency_from_account_found = FALSE;
currency = gnc_default_currency();
}
return currency;
}
gnc_commodity *
gnc_default_report_currency (void)
{
return gnc_default_currency_common (user_report_currency, GNC_PREFS_GROUP_GENERAL_REPORT);
}
static void
gnc_currency_changed_cb (GSettings *settings, gchar *key, gpointer user_data)
{
user_default_currency = NULL;
user_report_currency = NULL;
gnc_hook_run(HOOK_CURRENCY_CHANGED, NULL);
}
GNCPrintAmountInfo
gnc_default_print_info (gboolean use_symbol)
{
static GNCPrintAmountInfo info;
static gboolean got_it = FALSE;
struct lconv *lc;
/* These must be updated each time. */
info.use_symbol = use_symbol ? 1 : 0;
info.commodity = gnc_default_currency ();
if (got_it)
return info;
lc = gnc_localeconv ();
info.max_decimal_places = lc->frac_digits;
info.min_decimal_places = lc->frac_digits;
info.use_separators = 1;
info.use_locale = 1;
info.monetary = 1;
info.force_fit = 0;
info.round = 0;
got_it = TRUE;
return info;
}
static gboolean
is_decimal_fraction (int fraction, guint8 *max_decimal_places_p)
{
guint8 max_decimal_places = 0;
if (fraction <= 0)
return FALSE;
while (fraction != 1)
{
if (fraction % 10 != 0)
return FALSE;
fraction = fraction / 10;
max_decimal_places += 1;
}
if (max_decimal_places_p)
*max_decimal_places_p = max_decimal_places;
return TRUE;
}
GNCPrintAmountInfo
gnc_commodity_print_info (const gnc_commodity *commodity,
gboolean use_symbol)
{
GNCPrintAmountInfo info;
gboolean is_iso;
if (commodity == NULL)
return gnc_default_print_info (use_symbol);
info.commodity = commodity;
is_iso = gnc_commodity_is_iso (commodity);
if (is_decimal_fraction (gnc_commodity_get_fraction (commodity),
&info.max_decimal_places))
{
if (is_iso)
info.min_decimal_places = info.max_decimal_places;
else
info.min_decimal_places = 0;
}
else
info.max_decimal_places = info.min_decimal_places = 0;
info.use_separators = 1;
info.use_symbol = use_symbol ? 1 : 0;
info.use_locale = is_iso ? 1 : 0;
info.monetary = 1;
info.force_fit = 0;
info.round = 0;
return info;
}
static GNCPrintAmountInfo
gnc_account_print_info_helper(const Account *account, gboolean use_symbol,
gnc_commodity * (*efffunc)(const Account *),
int (*scufunc)(const Account*))
{
GNCPrintAmountInfo info;
gboolean is_iso;
int scu;
if (account == NULL)
return gnc_default_print_info (use_symbol);
info.commodity = efffunc (account);
is_iso = gnc_commodity_is_iso (info.commodity);
scu = scufunc (account);
if (is_decimal_fraction (scu, &info.max_decimal_places))
{
if (is_iso)
info.min_decimal_places = info.max_decimal_places;
else
info.min_decimal_places = 0;
}
else
info.max_decimal_places = info.min_decimal_places = 0;
info.use_separators = 1;
info.use_symbol = use_symbol ? 1 : 0;
info.use_locale = is_iso ? 1 : 0;
info.monetary = 1;
info.force_fit = 0;
info.round = 0;
return info;
}
GNCPrintAmountInfo
gnc_account_print_info (const Account *account, gboolean use_symbol)
{
return gnc_account_print_info_helper(account, use_symbol,
xaccAccountGetCommodity,
xaccAccountGetCommoditySCU);
}
GNCPrintAmountInfo
gnc_split_amount_print_info (Split *split, gboolean use_symbol)
{
if (!split)
{
GNCPrintAmountInfo info = gnc_default_share_print_info ();
info.use_symbol = use_symbol;
return info;
}
return gnc_account_print_info (xaccSplitGetAccount (split), use_symbol);
}
static GNCPrintAmountInfo
gnc_default_print_info_helper (int decplaces)