/
gnc-ab-utils.c
1126 lines (975 loc) · 34.2 KB
/
gnc-ab-utils.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-ab-utils.c --
*
* 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
*/
/**
* @internal
* @file gnc-ab-utils.c
* @brief AqBanking utility functions
* @author Copyright (C) 2002 Christian Stimming <stimming@tuhh.de>
* @author Copyright (C) 2008 Andreas Koehler <andi5.py@gmx.net>
*/
#include "config.h"
#include <glib/gi18n.h>
#include <gwenhywfar/gwenhywfar.h>
#include <aqbanking/banking.h>
#include "window-reconcile.h"
#include "Transaction.h"
#include "dialog-ab-trans.h"
#include "gnc-ab-kvp.h"
#include "gnc-ab-utils.h"
#include "gnc-glib-utils.h"
#include "gnc-gwen-gui.h"
#include "gnc-prefs.h"
#include "gnc-ui.h"
#include "import-account-matcher.h"
#include "import-main-matcher.h"
#include "import-utilities.h"
#include "qof.h"
#include "engine-helpers.h"
#ifdef AQBANKING_VERSION_5_PLUS
# include <aqbanking/abgui.h>
#endif /* AQBANKING_VERSION_5_PLUS */
/* This static indicates the debugging module that this .o belongs to. */
G_GNUC_UNUSED static QofLogModule log_module = G_LOG_DOMAIN;
/* Global variables for AB_BANKING caching. */
static AB_BANKING *gnc_AB_BANKING = NULL;
static gint gnc_AB_BANKING_refcount = 0;
static gpointer join_ab_strings_cb(const gchar *str, gpointer user_data);
static Account *gnc_ab_accinfo_to_gnc_acc(
AB_IMEXPORTER_ACCOUNTINFO *account_info);
static const AB_TRANSACTION *txn_transaction_cb(
const AB_TRANSACTION *element, gpointer user_data);
static AB_IMEXPORTER_ACCOUNTINFO *txn_accountinfo_cb(
AB_IMEXPORTER_ACCOUNTINFO *element, gpointer user_data);
static AB_IMEXPORTER_ACCOUNTINFO *bal_accountinfo_cb(
AB_IMEXPORTER_ACCOUNTINFO *element, gpointer user_data);
struct _GncABImExContextImport
{
guint awaiting;
gboolean txn_found;
Account *gnc_acc;
AB_ACCOUNT *ab_acc;
gboolean execute_txns;
AB_BANKING *api;
GtkWidget *parent;
AB_JOB_LIST2 *job_list;
GNCImportMainMatcher *generic_importer;
GData *tmp_job_list;
};
void
gnc_GWEN_Init(void)
{
gchar* gwen_logging = g_strdup(g_getenv("GWEN_LOGLEVEL"));
gchar* aqb_logging = g_strdup(g_getenv("AQBANKING_LOGLEVEL"));
/* Initialize gwen library */
GWEN_Init();
/* Initialize gwen logging */
if (gnc_prefs_get_bool(GNC_PREFS_GROUP_AQBANKING, GNC_PREF_VERBOSE_DEBUG))
{
if (!gwen_logging)
{
GWEN_Logger_SetLevel(NULL, GWEN_LoggerLevel_Info);
GWEN_Logger_SetLevel(GWEN_LOGDOMAIN, GWEN_LoggerLevel_Info);
}
if (!aqb_logging)
GWEN_Logger_SetLevel(AQBANKING_LOGDOMAIN, GWEN_LoggerLevel_Debug);
}
else
{
if (!gwen_logging)
{
GWEN_Logger_SetLevel(NULL, GWEN_LoggerLevel_Error);
GWEN_Logger_SetLevel(GWEN_LOGDOMAIN, GWEN_LoggerLevel_Error);
}
if (!aqb_logging)
GWEN_Logger_SetLevel(AQBANKING_LOGDOMAIN, GWEN_LoggerLevel_Warning);
}
g_free(gwen_logging);
g_free(aqb_logging);
gnc_GWEN_Gui_log_init();
}
void
gnc_GWEN_Fini(void)
{
/* Shutdown the GWEN_GUIs */
gnc_GWEN_Gui_shutdown();
GWEN_Logger_SetLevel(NULL, GWEN_LoggerLevel_Error);
GWEN_Logger_SetLevel(GWEN_LOGDOMAIN, GWEN_LoggerLevel_Warning);
GWEN_Logger_SetLevel(AQBANKING_LOGDOMAIN, GWEN_LoggerLevel_Warning);
/* Finalize gwen library */
GWEN_Fini();
}
static GWEN_GUI *gnc_gwengui_extended_by_ABBanking;
AB_BANKING *
gnc_AB_BANKING_new(void)
{
AB_BANKING *api;
if (gnc_AB_BANKING)
{
/* API cached. */
api = gnc_AB_BANKING;
/* Init the API again. */
if (gnc_AB_BANKING_refcount == 0)
g_return_val_if_fail(AB_Banking_Init(api) == 0, NULL);
}
else
{
api = AB_Banking_new("gnucash", NULL, 0);
g_return_val_if_fail(api, NULL);
#ifdef AQBANKING_VERSION_4_PLUS
/* Check for config migration */
if (AB_Banking_HasConf4(api
# ifndef AQBANKING_VERSION_5_PLUS
, 0
# endif
) != 0)
{
if (AB_Banking_HasConf3(api
# ifndef AQBANKING_VERSION_5_PLUS
, 0
# endif
) == 0)
{
g_message("gnc_AB_BANKING_new: importing aqbanking3 configuration\n");
if (AB_Banking_ImportConf3(api
# ifndef AQBANKING_VERSION_5_PLUS
, 0
# endif
) < 0)
{
g_message("gnc_AB_BANKING_new: unable to import aqbanking3 configuration\n");
}
}
else if (AB_Banking_HasConf2(api
# ifndef AQBANKING_VERSION_5_PLUS
, 0
# endif
) == 0)
{
g_message("gnc_AB_BANKING_new: importing aqbanking2 configuration\n");
if (AB_Banking_ImportConf2(api
# ifndef AQBANKING_VERSION_5_PLUS
, 0
# endif
) < 0)
{
g_message("gnc_AB_BANKING_new: unable to import aqbanking2 configuration\n");
}
}
}
#endif /* AQBANKING_VERSION_4_PLUS */
/* Init the API */
g_return_val_if_fail(AB_Banking_Init(api) == 0, NULL);
#ifdef AQBANKING_VERSION_5_PLUS
gnc_gwengui_extended_by_ABBanking = GWEN_Gui_GetGui();
AB_Gui_Extend(gnc_gwengui_extended_by_ABBanking, api);
#endif /* AQBANKING_VERSION_5_PLUS */
/* Cache it */
gnc_AB_BANKING = api;
gnc_AB_BANKING_refcount = 0;
}
gnc_AB_BANKING_refcount++;
return api;
}
void
gnc_AB_BANKING_delete(AB_BANKING *api)
{
if (!api)
api = gnc_AB_BANKING;
if (api)
{
if (api == gnc_AB_BANKING)
{
gnc_AB_BANKING = NULL;
gnc_AB_BANKING_fini(api);
}
AB_Banking_free(api);
}
}
gint
gnc_AB_BANKING_fini(AB_BANKING *api)
{
if (api == gnc_AB_BANKING)
{
if (--gnc_AB_BANKING_refcount == 0)
{
#ifdef AQBANKING_VERSION_5_PLUS
if (gnc_gwengui_extended_by_ABBanking)
AB_Gui_Unextend(gnc_gwengui_extended_by_ABBanking);
gnc_gwengui_extended_by_ABBanking = NULL;
#endif /* AQBANKING_VERSION_5_PLUS */
return AB_Banking_Fini(api);
}
}
else
{
#ifdef AQBANKING_VERSION_5_PLUS
if (gnc_gwengui_extended_by_ABBanking)
AB_Gui_Unextend(gnc_gwengui_extended_by_ABBanking);
gnc_gwengui_extended_by_ABBanking = NULL;
#endif /* AQBANKING_VERSION_5_PLUS */
return AB_Banking_Fini(api);
}
return 0;
}
AB_ACCOUNT *
gnc_ab_get_ab_account(const AB_BANKING *api, Account *gnc_acc)
{
AB_ACCOUNT *ab_account = NULL;
const gchar *bankcode = NULL;
const gchar *accountid = NULL;
guint32 account_uid = 0;
bankcode = gnc_ab_get_account_bankcode(gnc_acc);
accountid = gnc_ab_get_account_accountid(gnc_acc);
account_uid = gnc_ab_get_account_uid (gnc_acc);
if (account_uid > 0)
{
ab_account = AB_Banking_GetAccount(api, account_uid);
if (!ab_account && bankcode && *bankcode && accountid && *accountid)
{
g_message("gnc_ab_get_ab_account: No AB_ACCOUNT found for UID %d, "
"trying bank code\n", account_uid);
ab_account = AB_Banking_GetAccountByCodeAndNumber(api, bankcode,
accountid);
}
return ab_account;
}
else if (bankcode && *bankcode && accountid && *accountid)
{
ab_account = AB_Banking_GetAccountByCodeAndNumber(api, bankcode,
accountid);
return ab_account;
}
return NULL;
}
gchar *
gnc_AB_VALUE_to_readable_string(const AB_VALUE *value)
{
if (value)
return g_strdup_printf("%.2f %s",
AB_Value_GetValueAsDouble(value),
AB_Value_GetCurrency(value));
else
return g_strdup_printf("%.2f", 0.0);
}
/**
* Take a string from a GWEN_STRINGLIST, strip invalid utf8 and join it
* to the rest.
*/
static gpointer
join_ab_strings_cb(const gchar *str, gpointer user_data)
{
gchar **acc = user_data;
gchar *tmp;
if (!str || !*str)
return NULL;
tmp = g_strdup(str);
g_strstrip(tmp);
gnc_utf8_strip_invalid(tmp);
if (*acc)
{
gchar *join = g_strjoin(" ", *acc, tmp, (gchar*) NULL);
g_free(*acc);
g_free(tmp);
*acc = join;
}
else
{
*acc = tmp;
}
return NULL;
}
gchar *
gnc_ab_get_remote_name(const AB_TRANSACTION *ab_trans)
{
const GWEN_STRINGLIST *ab_remote_name;
gchar *gnc_other_name = NULL;
g_return_val_if_fail(ab_trans, NULL);
ab_remote_name = AB_Transaction_GetRemoteName(ab_trans);
if (ab_remote_name)
GWEN_StringList_ForEach(ab_remote_name, join_ab_strings_cb,
&gnc_other_name);
if (!gnc_other_name || !*gnc_other_name)
{
g_free(gnc_other_name);
gnc_other_name = NULL;
}
return gnc_other_name;
}
gchar *
gnc_ab_get_purpose(const AB_TRANSACTION *ab_trans)
{
const GWEN_STRINGLIST *ab_purpose;
const char *ab_transactionText = NULL;
gchar *gnc_description = NULL;
g_return_val_if_fail(ab_trans, g_strdup(""));
if (gnc_prefs_get_bool(GNC_PREFS_GROUP_AQBANKING, GNC_PREF_USE_TRANSACTION_TXT)) {
/* According to AqBanking, some of the non-swift lines have a special
* meaning. Some banks place valuable text into the transaction text,
* hence we put this text in front of the purpose. */
ab_transactionText = AB_Transaction_GetTransactionText(ab_trans);
if (ab_transactionText)
gnc_description = g_strdup(ab_transactionText);
}
ab_purpose = AB_Transaction_GetPurpose(ab_trans);
if (ab_purpose)
GWEN_StringList_ForEach(ab_purpose, join_ab_strings_cb,
&gnc_description);
if (!gnc_description)
gnc_description = g_strdup("");
return gnc_description;
}
gchar *
gnc_ab_description_to_gnc(const AB_TRANSACTION *ab_trans)
{
/* Description */
gchar *description = gnc_ab_get_purpose(ab_trans);
gchar *other_name = gnc_ab_get_remote_name(ab_trans);
gchar *retval;
if (other_name)
{
if (description && *description)
{
retval = g_strdup_printf("%s; %s", description, other_name);
}
else
{
retval = g_strdup(other_name);
}
}
else
{
if (description && *description)
{
retval = g_strdup(description);
}
else
{
retval = g_strdup(_("Unspecified"));
}
}
g_free(description);
g_free(other_name);
return retval;
}
gchar *
gnc_ab_memo_to_gnc(const AB_TRANSACTION *ab_trans)
{
const gchar *ab_remote_accountnumber =
AB_Transaction_GetRemoteAccountNumber(ab_trans);
const gchar *ab_remote_bankcode =
AB_Transaction_GetRemoteBankCode(ab_trans);
gchar *ab_other_accountid;
gchar *ab_other_bankcode;
gboolean have_accountid;
gboolean have_bankcode;
gchar *retval;
// For SEPA transactions, we need to ask for something different here
if (!ab_remote_accountnumber)
ab_remote_accountnumber = AB_Transaction_GetRemoteIban(ab_trans);
if (!ab_remote_bankcode)
ab_remote_bankcode = AB_Transaction_GetRemoteBic(ab_trans);
ab_other_accountid = g_strdup(ab_remote_accountnumber ? ab_remote_accountnumber : "");
ab_other_bankcode = g_strdup(ab_remote_bankcode ? ab_remote_bankcode : "");
/* Ensure string is in utf8 */
gnc_utf8_strip_invalid(ab_other_accountid);
gnc_utf8_strip_invalid(ab_other_bankcode);
/* and -then- trim it */
g_strstrip(ab_other_accountid);
g_strstrip(ab_other_bankcode);
have_accountid = ab_other_accountid && *ab_other_accountid;
have_bankcode = ab_other_bankcode && *ab_other_bankcode;
if ( have_accountid || have_bankcode )
{
retval = g_strdup_printf("%s %s %s %s",
have_accountid ? _("Account") : "",
have_accountid ? ab_other_accountid : "",
have_bankcode ? _("Bank") : "",
have_bankcode ? ab_other_bankcode : ""
);
g_strstrip(retval);
}
else
{
retval = g_strdup("");
}
g_free(ab_other_accountid);
g_free(ab_other_bankcode);
return retval;
}
Transaction *
gnc_ab_trans_to_gnc(const AB_TRANSACTION *ab_trans, Account *gnc_acc)
{
QofBook *book;
Transaction *gnc_trans;
const gchar *fitid;
const GWEN_TIME *valuta_date;
time64 current_time;
const char *custref;
gchar *description;
Split *split;
gchar *memo;
g_return_val_if_fail(ab_trans && gnc_acc, NULL);
/* Create new GnuCash transaction for the given AqBanking one */
book = gnc_account_get_book(gnc_acc);
gnc_trans = xaccMallocTransaction(book);
xaccTransBeginEdit(gnc_trans);
/* Date / Time */
valuta_date = AB_Transaction_GetValutaDate(ab_trans);
if (!valuta_date)
{
const GWEN_TIME *normal_date = AB_Transaction_GetDate(ab_trans);
if (normal_date)
valuta_date = normal_date;
}
if (valuta_date)
xaccTransSetDatePostedSecsNormalized(gnc_trans, GWEN_Time_toTime_t(valuta_date));
else
g_warning("transaction_cb: Oops, date 'valuta_date' was NULL");
xaccTransSetDateEnteredSecs(gnc_trans, gnc_time_utc (NULL));
/* Currency. We take simply the default currency of the gnucash account */
xaccTransSetCurrency(gnc_trans, xaccAccountGetCommodity(gnc_acc));
/* Trans-Num or Split-Action set with gnc_set_num_action below per book
* option */
/* Description */
description = gnc_ab_description_to_gnc(ab_trans);
xaccTransSetDescription(gnc_trans, description);
g_free(description);
/* Notes. */
/* xaccTransSetNotes(gnc_trans, g_notes); */
/* But Nobody ever uses the Notes field? */
/* Add one split */
split = xaccMallocSplit(book);
xaccSplitSetParent(split, gnc_trans);
xaccSplitSetAccount(split, gnc_acc);
/* Set the transaction number or split action field based on book option.
* We use the "customer reference", if there is one. */
custref = AB_Transaction_GetCustomerReference(ab_trans);
if (custref && *custref
&& g_ascii_strncasecmp(custref, "NONREF", 6) != 0)
gnc_set_num_action (gnc_trans, split, custref, NULL);
/* Set OFX unique transaction ID */
fitid = AB_Transaction_GetFiId(ab_trans);
if (fitid && *fitid)
gnc_import_set_split_online_id(split, fitid);
{
/* Amount into the split */
const AB_VALUE *ab_value = AB_Transaction_GetValue(ab_trans);
double d_value = ab_value ? AB_Value_GetValueAsDouble (ab_value) : 0.0;
AB_TRANSACTION_TYPE ab_type = AB_Transaction_GetType (ab_trans);
gnc_numeric gnc_amount;
/*printf("Transaction with value %f has type %d\n", d_value, ab_type);*/
/* If the value is positive, but the transaction type says the
money is transferred away from our account (Transfer instead of
DebitNote), we switch the value to negative. */
if (d_value > 0.0 && ab_type == AB_Transaction_TypeTransfer)
d_value = -d_value;
gnc_amount = double_to_gnc_numeric(
d_value,
xaccAccountGetCommoditySCU(gnc_acc),
GNC_HOW_RND_ROUND_HALF_UP);
if (!ab_value)
g_warning("transaction_cb: Oops, value was NULL. Using 0");
xaccSplitSetBaseValue(split, gnc_amount, xaccAccountGetCommodity(gnc_acc));
}
/* Memo in the Split. */
memo = gnc_ab_memo_to_gnc(ab_trans);
xaccSplitSetMemo(split, memo);
g_free(memo);
return gnc_trans;
}
/**
* Call gnc_import_select_account() on the online id constructed using
* the information in @a acc_info.
*
* @param acc_info AB_IMEXPORTER_ACCOUNTINFO
* @return A GnuCash account, or NULL otherwise
*/
static Account *
gnc_ab_accinfo_to_gnc_acc(AB_IMEXPORTER_ACCOUNTINFO *acc_info)
{
const gchar *bankcode, *accountnumber;
gchar *online_id;
Account *gnc_acc;
g_return_val_if_fail(acc_info, NULL);
bankcode = AB_ImExporterAccountInfo_GetBankCode(acc_info);
accountnumber = AB_ImExporterAccountInfo_GetAccountNumber(acc_info);
online_id = g_strconcat(bankcode ? bankcode : "",
accountnumber ? accountnumber : "",
(gchar*)NULL);
gnc_acc = gnc_import_select_account(
NULL, online_id, 1, AB_ImExporterAccountInfo_GetAccountName(acc_info),
NULL, ACCT_TYPE_NONE, NULL, NULL);
if (!gnc_acc)
{
g_warning("gnc_ab_accinfo_to_gnc_acc: Could not determine source account"
" for online_id %s", online_id);
}
g_free(online_id);
return gnc_acc;
}
static const AB_TRANSACTION *
txn_transaction_cb(const AB_TRANSACTION *element, gpointer user_data)
{
GncABImExContextImport *data = user_data;
Transaction *gnc_trans;
GncABTransType trans_type;
g_return_val_if_fail(element && data, NULL);
/* Create a GnuCash transaction from ab_trans */
gnc_trans = gnc_ab_trans_to_gnc(element, data->gnc_acc);
if (data->execute_txns && data->ab_acc)
{
AB_TRANSACTION *ab_trans = AB_Transaction_dup(element);
AB_JOB *job;
/* NEW: The imported transaction has been imported into gnucash.
* Now also add it as a job to aqbanking */
AB_Transaction_SetLocalBankCode(
ab_trans, AB_Account_GetBankCode(data->ab_acc));
AB_Transaction_SetLocalAccountNumber(
ab_trans, AB_Account_GetAccountNumber(data->ab_acc));
AB_Transaction_SetLocalCountry(ab_trans, "DE");
switch (AB_Transaction_GetType(ab_trans))
{
case AB_Transaction_TypeDebitNote:
trans_type = SINGLE_DEBITNOTE;
break;
case AB_Transaction_TypeEuTransfer:
trans_type = SEPA_TRANSFER;
break;
case AB_Transaction_TypeTransaction:
/* trans_type = SINGLE_INTERNAL_TRANSFER;
* break; */
case AB_Transaction_TypeTransfer:
default:
trans_type = SINGLE_TRANSFER;
} /* switch */
job = gnc_ab_get_trans_job(data->ab_acc, ab_trans, trans_type);
/* Check whether we really got a job */
if (!job || AB_Job_CheckAvailability(job
#ifndef AQBANKING_VERSION_5_PLUS
, 0
#endif
))
{
/* Oops, no job, probably not supported by bank */
if (gnc_verify_dialog(
NULL, FALSE, "%s",
_("The backend found an error during the preparation "
"of the job. It is not possible to execute this job. \n"
"\n"
"Most probably the bank does not support your chosen "
"job or your Online Banking account does not have the permission "
"to execute this job. More error messages might be "
"visible on your console log.\n"
"\n"
"Do you want to enter the job again?")))
{
gnc_error_dialog(NULL, "Sorry, not implemented yet. Please check the console or trace file logs to see which job was rejected.");
}
}
else
{
gnc_gen_trans_list_add_trans_with_ref_id(data->generic_importer, gnc_trans, AB_Job_GetJobId(job));
/* AB_Job_List2_PushBack(data->job_list, job); -> delayed until trans is successfully imported */
g_datalist_set_data(&data->tmp_job_list, gnc_AB_JOB_to_readable_string(job), job);
}
AB_Transaction_free(ab_trans);
}
else
{
/* Instead of xaccTransCommitEdit(gnc_trans) */
gnc_gen_trans_list_add_trans(data->generic_importer, gnc_trans);
}
return NULL;
}
static void gnc_ab_trans_processed_cb(GNCImportTransInfo *trans_info,
gboolean imported,
gpointer user_data)
{
GncABImExContextImport *data = user_data;
gchar *jobname = gnc_AB_JOB_ID_to_string(gnc_import_TransInfo_get_ref_id(trans_info));
AB_JOB *job = g_datalist_get_data(&data->tmp_job_list, jobname);
if (imported)
{
AB_Job_List2_PushBack(data->job_list, job);
}
else
{
AB_Job_free(job);
}
g_datalist_remove_data(&data->tmp_job_list, jobname);
}
gchar *
gnc_AB_JOB_to_readable_string(const AB_JOB *job)
{
if (job)
{
return gnc_AB_JOB_ID_to_string(AB_Job_GetJobId(job));
}
else
{
return gnc_AB_JOB_ID_to_string(0);
}
}
gchar *
gnc_AB_JOB_ID_to_string(gulong job_id)
{
return g_strdup_printf("job_%lu", job_id);
}
static AB_IMEXPORTER_ACCOUNTINFO *
txn_accountinfo_cb(AB_IMEXPORTER_ACCOUNTINFO *element, gpointer user_data)
{
GncABImExContextImport *data = user_data;
Account *gnc_acc;
g_return_val_if_fail(element && data, NULL);
if (data->awaiting & IGNORE_TRANSACTIONS)
/* Ignore them */
return NULL;
if (!AB_ImExporterAccountInfo_GetFirstTransaction(element))
/* No transaction found */
return NULL;
else
data->awaiting |= FOUND_TRANSACTIONS;
if (!(data->awaiting & AWAIT_TRANSACTIONS))
{
if (gnc_verify_dialog(data->parent, TRUE, "%s",
_("The bank has sent transaction information "
"in its response."
"\n"
"Do you want to import it?")))
{
data->awaiting |= AWAIT_TRANSACTIONS;
}
else
{
data->awaiting |= IGNORE_TRANSACTIONS;
return NULL;
}
}
/* Lookup the corresponding gnucash account */
gnc_acc = gnc_ab_accinfo_to_gnc_acc(element);
if (!gnc_acc) return NULL;
data->gnc_acc = gnc_acc;
if (data->execute_txns)
{
/* Retrieve the aqbanking account that belongs to this gnucash
* account */
data->ab_acc = gnc_ab_get_ab_account(data->api, gnc_acc);
if (!data->ab_acc)
{
gnc_error_dialog(NULL, "%s",
_("No Online Banking account found for this "
"gnucash account. These transactions will "
"not be executed by Online Banking."));
}
}
else
{
data->ab_acc = NULL;
}
if (!data->generic_importer)
{
data->generic_importer = gnc_gen_trans_list_new(data->parent, NULL,
TRUE, 14);
if (data->execute_txns)
{
gnc_gen_trans_list_add_tp_cb(data->generic_importer,
gnc_ab_trans_processed_cb, data);
}
}
/* Iterate through all transactions */
AB_ImExporterAccountInfo_TransactionsForEach(element, txn_transaction_cb,
data);
return NULL;
}
static AB_IMEXPORTER_ACCOUNTINFO *
bal_accountinfo_cb(AB_IMEXPORTER_ACCOUNTINFO *element, gpointer user_data)
{
GncABImExContextImport *data = user_data;
Account *gnc_acc;
AB_ACCOUNT_STATUS *item, *best = NULL;
const GWEN_TIME *best_time = NULL;
const AB_BALANCE *booked_bal, *noted_bal;
const AB_VALUE *booked_val = NULL, *noted_val = NULL;
gdouble booked_value, noted_value;
gnc_numeric value;
time64 booked_tt = 0;
GtkWidget *dialog;
gboolean show_recn_window = FALSE;
g_return_val_if_fail(element && data, NULL);
if (data->awaiting & IGNORE_BALANCES)
/* Ignore them */
return NULL;
if (!AB_ImExporterAccountInfo_GetFirstAccountStatus(element))
/* No balance found */
return NULL;
else
data->awaiting |= FOUND_BALANCES;
/* Lookup the most recent ACCOUNT_STATUS available */
item = AB_ImExporterAccountInfo_GetFirstAccountStatus(element);
while (item)
{
const GWEN_TIME *item_time = AB_AccountStatus_GetTime(item);
if (!best || GWEN_Time_Diff(best_time, item_time) < 0.0)
{
best = item;
best_time = item_time;
}
item = AB_ImExporterAccountInfo_GetNextAccountStatus(element);
}
booked_bal = AB_AccountStatus_GetBookedBalance(best);
if (!(data->awaiting & AWAIT_BALANCES))
{
/* Ignore zero balances if we don't await a balance */
if (!booked_bal || AB_Value_IsZero(AB_Balance_GetValue(booked_bal)))
return NULL;
/* Ask the user whether to import unawaited non-zero balance */
if (gnc_verify_dialog(data->parent, TRUE, "%s",
_("The bank has sent balance information "
"in its response."
"\n"
"Do you want to import it?")))
{
data->awaiting |= AWAIT_BALANCES;
}
else
{
data->awaiting |= IGNORE_BALANCES;
return NULL;
}
}
/* Lookup the corresponding gnucash account */
gnc_acc = gnc_ab_accinfo_to_gnc_acc(element);
if (!gnc_acc) return NULL;
data->gnc_acc = gnc_acc;
/* Lookup booked balance and time */
if (booked_bal)
{
const GWEN_TIME *ti = AB_Balance_GetTime(booked_bal);
if (ti)
{
booked_tt = GWEN_Time_toTime_t(ti);
}
else
{
/* No time found? Use today because the HBCI query asked for today's
* balance. */
booked_tt = gnc_time64_get_day_start(gnc_time(NULL));
}
booked_val = AB_Balance_GetValue(booked_bal);
if (booked_val)
{
booked_value = AB_Value_GetValueAsDouble(booked_val);
}
else
{
g_warning("bal_accountinfo_cb: booked_val == NULL. Assuming 0");
booked_value = 0.0;
}
}
else
{
g_warning("bal_accountinfo_cb: booked_bal == NULL. Assuming 0");
booked_tt = 0;
booked_value = 0.0;
}
/* Lookup noted balance */
noted_bal = AB_AccountStatus_GetNotedBalance(best);
if (noted_bal)
{
noted_val = AB_Balance_GetValue(noted_bal);
if (noted_val)
noted_value = AB_Value_GetValueAsDouble(noted_val);
else
{
g_warning("bal_accountinfo_cb: noted_val == NULL. Assuming 0");
noted_value = 0.0;
}
}
else
{
g_warning("bal_accountinfo_cb: noted_bal == NULL. Assuming 0");
noted_value = 0.0;
}
value = double_to_gnc_numeric(booked_value,
xaccAccountGetCommoditySCU(gnc_acc),
GNC_HOW_RND_ROUND_HALF_UP);
if (noted_value == 0.0 && booked_value == 0.0)
{
dialog = gtk_message_dialog_new(
GTK_WINDOW(data->parent),
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_OK,
"%s",
/* Translators: Strings from this file are needed only in
* countries that have one of aqbanking's Online Banking
* techniques available. This is 'OFX DirectConnect'
* (U.S. and others), 'HBCI' (in Germany), or 'YellowNet'
* (Switzerland). If none of these techniques are available
* in your country, you may safely ignore strings from the
* import-export/hbci subdirectory. */
_("The downloaded Online Banking Balance was zero.\n\n"
"Either this is the correct balance, or your bank does not "
"support Balance download in this Online Banking version. "
"In the latter case you should choose a different "
"Online Banking version number in the Online Banking "
"(AqBanking or HBCI) Setup. After that, try again to "
"download the Online Banking Balance."));
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
else
{
gnc_numeric reconc_balance = xaccAccountGetReconciledBalance(gnc_acc);
gchar *booked_str = gnc_AB_VALUE_to_readable_string(booked_val);
gchar *message1 = g_strdup_printf(
_("Result of Online Banking job: \n"
"Account booked balance is %s"),
booked_str);
gchar *message2 =
(noted_value == 0.0) ?
g_strdup("") :
g_strdup_printf(_("For your information: This account also "
"has a noted balance of %s\n"),
gnc_AB_VALUE_to_readable_string(noted_val));
if (gnc_numeric_equal(value, reconc_balance))
{
const gchar *message3 =
_("The booked balance is identical to the current "
"reconciled balance of the account.");
dialog = gtk_message_dialog_new(
GTK_WINDOW(data->parent),
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_OK,
"%s\n%s\n%s",
message1, message2, message3);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(GTK_WIDGET(dialog));
}
else
{