-
Notifications
You must be signed in to change notification settings - Fork 762
/
import-backend.c
1317 lines (1154 loc) · 45.3 KB
/
import-backend.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
/********************************************************************\
* 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 *
\********************************************************************/
/** @addtogroup Import_Export
@{ */
/** @internal
@file import-backend.c
@brief import-backend.c: Generic importer backend implementation (duplicate matching algorithm, action handling, etc.)
@author Copyright (C) 2002 Benoit Grégoire
@author Christian Stimming
@author Copyright (c) 2006 David Hampton <hampton@employees.org>
*/
#include <config.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <stdlib.h>
#include <math.h>
#include <errno.h>
#include "import-backend.h"
#include "import-utilities.h"
#include "Account.h"
#include "Query.h"
#include "gnc-engine.h"
#include "engine-helpers.h"
#include "gnc-prefs.h"
#include "gnc-ui-util.h"
#define GNCIMPORT_DESC "desc"
#define GNCIMPORT_MEMO "memo"
#define GNCIMPORT_PAYEE "payee"
/********************************************************************\
* Constants *
\********************************************************************/
static QofLogModule log_module = GNC_MOD_IMPORT;
/********************************************************************\
* Constants, should ideally be defined a user preference dialog *
\********************************************************************/
static const int MATCH_DATE_THRESHOLD = 4; /*within 4 days*/
static const int MATCH_DATE_NOT_THRESHOLD = 14;
/********************************************************************\
* Forward declared prototypes *
\********************************************************************/
static void
matchmap_store_destination (GncImportMatchMap *matchmap,
GNCImportTransInfo *trans_info,
gboolean use_match);
/********************************************************************\
* Structures passed between the functions *
\********************************************************************/
struct _selected_match_info
{
GNCImportMatchInfo *selected_match;
gboolean selected_manually;
};
struct _transactioninfo
{
Transaction * trans;
Split * first_split;
/* GList of GNCImportMatchInfo's, one for each possible duplicate match. */
GList * match_list;
GNCImportSelectedMatchInfo selected_match_info;
GNCImportAction action;
GNCImportAction previous_action;
/* A list of tokenized strings to use for bayesian matching purposes */
GList * match_tokens;
/* In case of a single destination account it is stored here. */
Account *dest_acc;
gboolean dest_acc_selected_manually;
/* Reference id to link gnc transaction to external object. E.g. aqbanking job id. */
guint32 ref_id;
};
/* Some simple getters and setters for the above data types. */
GList *
gnc_import_TransInfo_get_match_list (const GNCImportTransInfo *info)
{
g_assert (info);
return info->match_list;
}
Transaction *
gnc_import_TransInfo_get_trans (const GNCImportTransInfo *info)
{
g_assert (info);
return info->trans;
}
gboolean
gnc_import_TransInfo_is_balanced (const GNCImportTransInfo *info)
{
g_assert (info);
/* Assume that the importer won't create a transaction that involves two or more
currencies and no non-currency commodity. In that case can use the simpler
value imbalance check. */
if (gnc_numeric_zero_p(xaccTransGetImbalanceValue(gnc_import_TransInfo_get_trans(info))))
{
return TRUE;
}
else
{
return FALSE;
}
}
Split *
gnc_import_TransInfo_get_fsplit (const GNCImportTransInfo *info)
{
g_assert (info);
return info->first_split;
}
GNCImportMatchInfo *
gnc_import_TransInfo_get_selected_match (const GNCImportTransInfo *info)
{
g_assert (info);
return info->selected_match_info.selected_match;
}
void
gnc_import_TransInfo_set_selected_match_info (GNCImportTransInfo *info,
GNCImportMatchInfo *match,
gboolean selected_manually)
{
g_assert (info);
info->selected_match_info.selected_match = match;
info->selected_match_info.selected_manually = selected_manually;
}
gboolean
gnc_import_TransInfo_get_match_selected_manually (const GNCImportTransInfo *info)
{
g_assert (info);
return info->selected_match_info.selected_manually;
}
GNCImportAction
gnc_import_TransInfo_get_action (const GNCImportTransInfo *info)
{
g_assert (info);
return info->action;
}
void
gnc_import_TransInfo_set_action (GNCImportTransInfo *info,
GNCImportAction action)
{
g_assert (info);
if (action != info->action)
{
info->previous_action = info->action;
info->action = action;
}
}
Account *
gnc_import_TransInfo_get_destacc (const GNCImportTransInfo *info)
{
g_assert (info);
return info->dest_acc;
}
void gnc_import_TransInfo_set_destacc (GNCImportTransInfo *info,
Account *acc,
gboolean selected_manually)
{
g_assert (info);
info->dest_acc = acc;
info->dest_acc_selected_manually = selected_manually;
/* Store the mapping to the other account in the MatchMap. */
if (selected_manually)
{
matchmap_store_destination (NULL, info, FALSE);
}
}
gboolean
gnc_import_TransInfo_get_destacc_selected_manually (const GNCImportTransInfo *info)
{
g_assert (info);
return info->dest_acc_selected_manually;
}
guint32
gnc_import_TransInfo_get_ref_id (const GNCImportTransInfo *info)
{
g_assert (info);
return info->ref_id;
}
void
gnc_import_TransInfo_set_ref_id (GNCImportTransInfo *info,
guint32 ref_id)
{
g_assert (info);
info->ref_id = ref_id;
}
Split *
gnc_import_MatchInfo_get_split (const GNCImportMatchInfo * info)
{
g_assert (info);
return info->split;
}
gint
gnc_import_MatchInfo_get_probability (const GNCImportMatchInfo * info)
{
if (info)
{
return info->probability;
}
else
{
return 0;
}
}
void gnc_import_TransInfo_delete (GNCImportTransInfo *info)
{
if (info)
{
g_list_free (info->match_list);
/*If the transaction exists and is still open, it must be destroyed*/
if (info->trans && xaccTransIsOpen(info->trans))
{
xaccTransDestroy(info->trans);
xaccTransCommitEdit(info->trans);
}
if (info->match_tokens)
{
GList *node;
for (node = info->match_tokens; node; node = node->next)
g_free (node->data);
g_list_free (info->match_tokens);
}
g_free(info);
}
}
GdkPixbuf* gen_probability_pixbuf(gint score_original, GNCImportSettings *settings, GtkWidget * widget)
{
GdkPixbuf* retval = NULL;
gint i, j;
gint score;
const gint height = 15;
const gint width_each_bar = 7;
gchar * green_bar = ("bggggb ");
gchar * yellow_bar = ("byyyyb ");
gchar * red_bar = ("brrrrb ");
gchar * black_bar = ("bbbbbb ");
const gint width_first_bar = 1;
gchar * black_first_bar = ("b");
const gint num_colors = 5;
gchar * size_str;
gchar * none_color_str = g_strdup_printf(" c None");
gchar * green_color_str = g_strdup_printf("g c green");
gchar * yellow_color_str = g_strdup_printf("y c yellow");
gchar * red_color_str = g_strdup_printf("r c red");
gchar * black_color_str = g_strdup_printf("b c black");
gchar * xpm[2+num_colors+height];
gint add_threshold, clear_threshold;
g_assert(settings);
g_assert(widget);
if (score_original < 0)
{
score = 0;
}
else
{
score = score_original;
}
size_str = g_strdup_printf("%d%s%d%s%d%s", (width_each_bar * score) + width_first_bar/*width*/, " ", height, " ", num_colors, " 1"/*characters per pixel*/);
/*DEBUG("Begin");*/
xpm[0] = size_str;
xpm[1] = none_color_str;
xpm[2] = green_color_str;
xpm[3] = yellow_color_str;
xpm[4] = red_color_str;
xpm[5] = black_color_str;
add_threshold = gnc_import_Settings_get_add_threshold(settings);
clear_threshold = gnc_import_Settings_get_clear_threshold(settings);
for (i = 0; i < height; i++)
{
xpm[num_colors+1+i] = g_new0(char, (width_each_bar * score) + width_first_bar + 1);
for (j = 0; j <= score; j++)
{
if (i == 0 || i == height - 1)
{
if (j == 0)
{
strcat(xpm[num_colors+1+i], black_first_bar);
}
else
{
strcat(xpm[num_colors+1+i], black_bar);
}
}
else
{
if (j == 0)
{
strcat(xpm[num_colors+1+i], black_first_bar);
}
else if (j <= add_threshold)
{
strcat(xpm[num_colors+1+i], red_bar);
}
else if (j >= clear_threshold)
{
strcat(xpm[num_colors+1+i], green_bar);
}
else
{
strcat(xpm[num_colors+1+i], yellow_bar);
}
}
}
}
retval = gdk_pixbuf_new_from_xpm_data((const gchar **)xpm);
for (i = 0; i <= num_colors + height; i++)
{
/*DEBUG("free_loop i=%d%s%s",i,": ",xpm[i]);*/
g_free(xpm[i]);
}
return retval;
}
/*************************************************************************
* MatchMap- related functions (storing and retrieving)
*/
/* Tokenize a string and append to an existing GList(or an empty GList)
* the tokens
*/
static GList*
tokenize_string(GList* existing_tokens, const char *string)
{
char **tokenized_strings; /* array of strings returned by g_strsplit() */
char **stringpos;
tokenized_strings = g_strsplit(string, " ", 0);
stringpos = tokenized_strings;
/* add each token to the token GList */
while (stringpos && *stringpos)
{
if (strlen(*stringpos) > 0)
{
/* check for duplicated tokens */
gboolean duplicated = FALSE;
for (GList* token = existing_tokens; token != NULL; token = token->next)
{
if (g_strcmp0(token->data, *stringpos) == 0)
{
duplicated = TRUE;
break;
}
}
if (duplicated == FALSE)
{
/* prepend the char* to the token GList */
existing_tokens = g_list_prepend(existing_tokens, g_strdup(*stringpos));
}
}
/* then move to the next string */
stringpos++;
}
/* free up the strings that g_strsplit() created */
g_strfreev(tokenized_strings);
return existing_tokens;
}
/* create and return a list of tokens for a given transaction info. */
static GList*
TransactionGetTokens(GNCImportTransInfo *info)
{
Transaction* transaction;
GList* tokens;
const char* text;
time64 transtime;
struct tm *tm_struct;
char local_day_of_week[16];
Split* split;
int split_index;
g_return_val_if_fail (info, NULL);
if (info->match_tokens) return info->match_tokens;
transaction = gnc_import_TransInfo_get_trans(info);
g_assert(transaction);
tokens = 0; /* start off with an empty list */
/* make tokens from the transaction description */
text = xaccTransGetDescription(transaction);
tokens = tokenize_string(tokens, text);
/* The day of week the transaction occurred is a good indicator of
* what account this transaction belongs in. Get the date and covert
* it to day of week as a token
*/
transtime = xaccTransGetDate(transaction);
tm_struct = gnc_gmtime(&transtime);
if (!qof_strftime(local_day_of_week, sizeof(local_day_of_week), "%A", tm_struct))
{
PERR("TransactionGetTokens: error, strftime failed\n");
}
gnc_tm_free (tm_struct);
/* we cannot add a locally allocated string to this array, dup it so
* it frees the same way the rest do
*/
tokens = g_list_prepend(tokens, g_strdup(local_day_of_week));
/* make tokens from the memo of each split of this transaction */
split_index = 0;
while ((split = xaccTransGetSplit(transaction, split_index)))
{
text = xaccSplitGetMemo(split);
tokens = tokenize_string(tokens, text);
split_index++; /* next split */
}
/* remember the list of tokens for later.. */
info->match_tokens = tokens;
/* return the pointer to the GList */
return tokens;
}
/* Destroy an import map. But all stored entries will still continue
* to exist in the underlying kvp frame of the account.
*/
static void
gnc_imap_destroy (GncImportMatchMap *imap)
{
if (!imap) return;
g_free (imap);
}
/* searches using the GNCImportTransInfo through all existing transactions
* if there is an exact match of the description and memo
*/
static Account *
matchmap_find_destination (GncImportMatchMap *matchmap, GNCImportTransInfo *info)
{
GncImportMatchMap *tmp_map;
Account *result;
GList* tokens;
gboolean useBayes;
g_assert (info);
tmp_map = ((matchmap != NULL) ? matchmap :
gnc_account_imap_create_imap
(xaccSplitGetAccount
(gnc_import_TransInfo_get_fsplit (info))));
useBayes = gnc_prefs_get_bool (GNC_PREFS_GROUP_IMPORT, GNC_PREF_USE_BAYES);
if (useBayes)
{
/* get the tokens for this transaction* */
tokens = TransactionGetTokens(info);
/* try to find the destination account for this transaction from its tokens */
result = gnc_account_imap_find_account_bayes(tmp_map, tokens);
}
else
{
/* old system of transaction to account matching */
result = gnc_account_imap_find_account
(tmp_map, GNCIMPORT_DESC,
xaccTransGetDescription (gnc_import_TransInfo_get_trans (info)));
}
/* Disable matching by memo, until bayesian filtering is implemented.
* It's currently unlikely to help, and has adverse effects,
* causing false positives, since very often the type of the
* transaction is stored there.
if (result == NULL)
result = gnc_account_imap_find_account
(tmp_map, GNCIMPORT_MEMO,
xaccSplitGetMemo (gnc_import_TransInfo_get_fsplit (info)));
*/
if (matchmap == NULL)
gnc_imap_destroy (tmp_map);
return result;
}
/** Store the destination account from trans_info in the matchmap. If
'use_match' is true, the destination account of the selected
matching/duplicate transaction is used; otherwise, the stored
destination_acc pointer is used. */
static void
matchmap_store_destination (GncImportMatchMap *matchmap,
GNCImportTransInfo *trans_info,
gboolean use_match)
{
GncImportMatchMap *tmp_matchmap = NULL;
Account *dest;
const char *descr, *memo;
GList *tokens;
gboolean useBayes;
g_assert (trans_info);
/* This will store the destination account of the selected match if
the reconcile match selected has only two splits. Good idea
Christian! */
dest = ((use_match) ?
xaccSplitGetAccount
(xaccSplitGetOtherSplit
(gnc_import_MatchInfo_get_split
(gnc_import_TransInfo_get_selected_match (trans_info)))) :
gnc_import_TransInfo_get_destacc (trans_info));
if (dest == NULL)
return;
tmp_matchmap = ((matchmap != NULL) ?
matchmap :
gnc_account_imap_create_imap
(xaccSplitGetAccount
(gnc_import_TransInfo_get_fsplit (trans_info))));
/* see what matching system we are currently using */
useBayes = gnc_prefs_get_bool (GNC_PREFS_GROUP_IMPORT, GNC_PREF_USE_BAYES);
if (useBayes)
{
/* tokenize this transaction */
tokens = TransactionGetTokens(trans_info);
/* add the tokens to the imap with the given destination account */
gnc_account_imap_add_account_bayes(tmp_matchmap, tokens, dest);
}
else
{
/* old matching system */
descr = xaccTransGetDescription
(gnc_import_TransInfo_get_trans (trans_info));
if (descr && (strlen (descr) > 0))
gnc_account_imap_add_account (tmp_matchmap,
GNCIMPORT_DESC,
descr,
dest);
memo = xaccSplitGetMemo
(gnc_import_TransInfo_get_fsplit (trans_info));
if (memo && (strlen (memo) > 0))
gnc_account_imap_add_account (tmp_matchmap,
GNCIMPORT_MEMO,
memo,
dest);
} /* if(useBayes) */
if (matchmap == NULL)
gnc_imap_destroy (tmp_matchmap);
}
/** @brief The transaction matching heuristics are here.
*/
static void split_find_match (GNCImportTransInfo * trans_info,
Split * split,
gint display_threshold,
double fuzzy_amount_difference)
{
/* DEBUG("Begin"); */
/*Ignore the split if the transaction is open for edit, meaning it
was just downloaded. */
if (xaccTransIsOpen(xaccSplitGetParent(split)) == FALSE)
{
GNCImportMatchInfo * match_info;
gint prob = 0;
gboolean update_proposed;
double downloaded_split_amount, match_split_amount;
time64 match_time, download_time;
int datediff_day;
Transaction *new_trans = gnc_import_TransInfo_get_trans (trans_info);
Split *new_trans_fsplit = gnc_import_TransInfo_get_fsplit (trans_info);
// Do not consider transactions that have been previously matched.
if (gnc_import_split_has_online_id (split))
return;
/* Matching heuristics */
/* Amount heuristics */
downloaded_split_amount =
gnc_numeric_to_double (xaccSplitGetAmount(new_trans_fsplit));
/*DEBUG(" downloaded_split_amount=%f", downloaded_split_amount);*/
match_split_amount = gnc_numeric_to_double(xaccSplitGetAmount(split));
/*DEBUG(" match_split_amount=%f", match_split_amount);*/
if (fabs(downloaded_split_amount - match_split_amount) < 1e-6)
/* bug#347791: Double type shouldn't be compared for exact
equality, so we're using fabs() instead. */
/*if (gnc_numeric_equal(xaccSplitGetAmount
(new_trans_fsplit),
xaccSplitGetAmount(split)))
-- gnc_numeric_equal is an expensive function call */
{
prob = prob + 3;
/*DEBUG("heuristics: probability + 3 (amount)");*/
}
else if (fabs (downloaded_split_amount - match_split_amount) <=
fuzzy_amount_difference)
{
/* ATM fees are sometimes added directly in the transaction.
So you withdraw 100$ and get charged 101,25$ in the same
transaction */
prob = prob + 2;
/*DEBUG("heuristics: probability + 2 (amount)");*/
}
else
{
/* If a transaction's amount doesn't match within the
threshold, it's very unlikely to be the same transaction
so we give it an extra -5 penalty */
prob = prob - 5;
/* DEBUG("heuristics: probability - 1 (amount)"); */
}
/* Date heuristics */
match_time = xaccTransGetDate (xaccSplitGetParent (split));
download_time = xaccTransGetDate (new_trans);
datediff_day = llabs(match_time - download_time) / 86400;
/* Sorry, there are not really functions around at all that
provide for less hacky calculation of days of date
differences. Whatever. On the other hand, the difference
calculation itself will work regardless of month/year
turnarounds. */
/*DEBUG("diff day %d", datediff_day);*/
if (datediff_day == 0)
{
prob = prob + 3;
/*DEBUG("heuristics: probability + 3 (date)");*/
}
else if (datediff_day <= MATCH_DATE_THRESHOLD)
{
prob = prob + 2;
/*DEBUG("heuristics: probability + 2 (date)");*/
}
else if (datediff_day > MATCH_DATE_NOT_THRESHOLD)
{
/* Extra penalty if that split lies awfully far away from
the given one. */
prob = prob - 5;
/*DEBUG("heuristics: probability - 5 (date)"); */
/* Changed 2005-02-21: Revert the hard-limiting behaviour
back to the previous large penalty. (Changed 2004-11-27:
The penalty is so high that we can forget about this
split anyway and skip the rest of the tests.) */
}
/* Check if date and amount are identical */
update_proposed = (prob < 6);
/* Check number heuristics */
{
const char *new_trans_str = gnc_get_num_action(new_trans, new_trans_fsplit);
if (new_trans_str && strlen(new_trans_str) != 0)
{
long new_trans_number, split_number;
const gchar *split_str;
char *endptr;
gboolean conversion_ok = TRUE;
/* To distinguish success/failure after strtol call */
errno = 0;
new_trans_number = strtol(new_trans_str, &endptr, 10);
/* Possible addressed problems: over/underflow, only non
numbers on string and string empty */
if (errno || endptr == new_trans_str)
conversion_ok = FALSE;
split_str = gnc_get_num_action (xaccSplitGetParent (split), split);
errno = 0;
split_number = strtol(split_str, &endptr, 10);
if (errno || endptr == split_str)
conversion_ok = FALSE;
if ( (conversion_ok && (split_number == new_trans_number)) ||
(g_strcmp0(new_trans_str, split_str) == 0) )
{
/* An exact match of the Check number gives a +4 */
prob += 4;
/*DEBUG("heuristics: probability + 4 (Check number)");*/
}
else if (strlen(new_trans_str) > 0 && strlen(split_str) > 0)
{
/* If both number are not empty yet do not match, add a
little extra penalty */
prob -= 2;
}
}
}
/* Memo heuristics */
{
const char *memo = xaccSplitGetMemo(new_trans_fsplit);
if (memo && strlen(memo) != 0)
{
if (safe_strcasecmp(memo, xaccSplitGetMemo(split)) == 0)
{
/* An exact match of memo gives a +2 */
prob = prob + 2;
/* DEBUG("heuristics: probability + 2 (memo)"); */
}
else if ((strncasecmp(memo, xaccSplitGetMemo(split),
strlen(xaccSplitGetMemo(split)) / 2)
== 0))
{
/* Very primitive fuzzy match worth +1. This matches the
first 50% of the strings to skip annoying transaction
number some banks seem to include in the memo but someone
should write something more sophisticated */
prob = prob + 1;
/*DEBUG("heuristics: probability + 1 (memo)"); */
}
}
}
/* Description heuristics */
{
const char *descr = xaccTransGetDescription(new_trans);
if (descr && strlen(descr) != 0)
{
if (safe_strcasecmp(descr,
xaccTransGetDescription(xaccSplitGetParent(split)))
== 0)
{
/*An exact match of Description gives a +2 */
prob = prob + 2;
/*DEBUG("heuristics: probability + 2 (description)");*/
}
else if ((strncasecmp(descr,
xaccTransGetDescription (xaccSplitGetParent(split)),
strlen(xaccTransGetDescription (new_trans)) / 2)
== 0))
{
/* Very primitive fuzzy match worth +1. This matches the
first 50% of the strings to skip annoying transaction
number some banks seem to include in the memo but someone
should write something more sophisticated */
prob = prob + 1;
/*DEBUG("heuristics: probability + 1 (description)"); */
}
}
}
/* Is the probability high enough? Otherwise do nothing and return. */
if (prob < display_threshold)
{
return;
}
/* The probability is high enough, so allocate an object
here. Allocating it only when it's actually being used is
probably quite some performance gain. */
match_info = g_new0(GNCImportMatchInfo, 1);
match_info->probability = prob;
match_info->update_proposed = update_proposed;
match_info->split = split;
match_info->trans = xaccSplitGetParent(split);
/* Append that to the list. Do not use g_list_append because
it is slow. The list is sorted afterwards anyway. */
trans_info->match_list =
g_list_prepend(trans_info->match_list,
match_info);
}
}/* end split_find_match */
/** /brief Iterate through all splits of the originating account of the given
transaction, and find all matching splits there. */
void gnc_import_find_split_matches(GNCImportTransInfo *trans_info,
gint process_threshold,
double fuzzy_amount_difference,
gint match_date_hardlimit)
{
GList * list_element;
Query *query = qof_query_create_for(GNC_ID_SPLIT);
g_assert (trans_info);
/* Get list of splits of the originating account. */
{
/* We used to traverse *all* splits of the account by using
xaccAccountGetSplitList, which is a bad idea because 90% of these
splits are outside the date range that is interesting. We should
rather use a query according to the date region, which is
implemented here.
*/
Account *importaccount =
xaccSplitGetAccount (gnc_import_TransInfo_get_fsplit (trans_info));
time64 download_time = xaccTransGetDate (gnc_import_TransInfo_get_trans (trans_info));
qof_query_set_book (query, gnc_get_current_book());
xaccQueryAddSingleAccountMatch (query, importaccount,
QOF_QUERY_AND);
xaccQueryAddDateMatchTT (query,
TRUE, download_time - match_date_hardlimit * 86400,
TRUE, download_time + match_date_hardlimit * 86400,
QOF_QUERY_AND);
list_element = qof_query_run (query);
/* Sigh. Doesn't help too much. We still create and run one query
for each imported transaction. Maybe it would improve
performance further if there is one single (master-)query at
the beginning, matching the full date range and all accounts in
question. However, this doesn't quite work because this function
here is called from each gnc_gen_trans_list_add_trans(), which
is called one at a time. Therefore the whole importer would
have to change its behaviour: Accept the imported txns via
gnc_gen_trans_list_add_trans(), and only when
gnc_gen_trans_list_run() is called, then calculate all the
different match candidates. That's too much work for now.
*/
}
/* Traverse that list, calling split_find_match on each one. Note
that xaccAccountForEachSplit is declared in Account.h but
implemented nowhere :-( */
while (list_element != NULL)
{
split_find_match (trans_info, list_element->data,
process_threshold, fuzzy_amount_difference);
list_element = g_list_next (list_element);
}
qof_query_destroy (query);
}
/***********************************************************************
*/
/** /brief -- Processes one match
according to its selected action. */
gboolean
gnc_import_process_trans_item (GncImportMatchMap *matchmap,
GNCImportTransInfo *trans_info)
{
Split * other_split;
gnc_numeric imbalance_value;
/* DEBUG("Begin"); */
g_assert (trans_info);
/*DEBUG("Iteration %d, action %d, split %s", i,
trans_info->action,
xaccTransGetDescription (gnc_import_TransInfo_get_trans
(trans_info)))*/
switch (gnc_import_TransInfo_get_action (trans_info))
{
case GNCImport_SKIP:
return FALSE;
case GNCImport_ADD:
/* Transaction gets imported. */
/* Is the transaction not balanced and there is a non-NULL destination account? */
if (gnc_import_TransInfo_is_balanced(trans_info) == FALSE
&& gnc_import_TransInfo_get_destacc(trans_info) != NULL)
{
/* Create the 'other' split. */
Split *split =
xaccMallocSplit
(gnc_account_get_book
(gnc_import_TransInfo_get_destacc (trans_info)));
xaccTransAppendSplit
(gnc_import_TransInfo_get_trans (trans_info), split);
xaccAccountInsertSplit
(gnc_import_TransInfo_get_destacc (trans_info), split);
/*xaccSplitSetBaseValue
(split,
gnc_numeric_neg(xaccTransGetImbalance
(gnc_import_TransInfo_get_trans (trans_info))),
xaccTransGetCurrency
(gnc_import_TransInfo_get_trans (trans_info)));*/
{
/* This is a quick workaround for the bug described in
http://lists.gnucash.org/pipermail/gnucash-devel/2003-August/009982.html
Assume that importers won't create transactions involving two or more
currencies so we can use xaccTransGetImbalanceValue. */
imbalance_value =
gnc_numeric_neg (xaccTransGetImbalanceValue
(gnc_import_TransInfo_get_trans (trans_info)));
xaccSplitSetValue (split, imbalance_value);
xaccSplitSetAmount (split, imbalance_value);
}
/*xaccSplitSetMemo (split, _("Auto-Balance split"));
-- disabled due to popular request */
}
xaccSplitSetReconcile(gnc_import_TransInfo_get_fsplit (trans_info), CREC);
/*Set reconcile date to today*/
xaccSplitSetDateReconciledSecs(gnc_import_TransInfo_get_fsplit (trans_info),
gnc_time (NULL));
/* Done editing. */
xaccTransCommitEdit(gnc_import_TransInfo_get_trans (trans_info));
return TRUE;
case GNCImport_UPDATE:
{
GNCImportMatchInfo *selected_match =
gnc_import_TransInfo_get_selected_match(trans_info);
/* If there is no selection, ignore this transaction. */
if (!selected_match)
{
PWARN("No matching translaction to be cleared was chosen. Imported transaction will be ignored.");
break;
}
/* Transaction gets not imported but the matching one gets
updated and reconciled. */
if (gnc_import_MatchInfo_get_split(selected_match) == NULL)
{
PERR("The split I am trying to update and reconcile is NULL, shouldn't happen!");
}
else
{
/* Update and reconcile the matching transaction */
/*DEBUG("BeginEdit selected_match")*/
xaccTransBeginEdit(selected_match->trans);
xaccTransSetDatePostedSecsNormalized(selected_match->trans,
xaccTransGetDate(xaccSplitGetParent(
gnc_import_TransInfo_get_fsplit(trans_info))));
xaccSplitSetAmount(selected_match->split,
xaccSplitGetAmount(
gnc_import_TransInfo_get_fsplit(trans_info)));
xaccSplitSetValue(selected_match->split,
xaccSplitGetValue(
gnc_import_TransInfo_get_fsplit(trans_info)));
imbalance_value = xaccTransGetImbalanceValue(
gnc_import_TransInfo_get_trans(trans_info));
other_split = xaccSplitGetOtherSplit(selected_match->split);
if (!gnc_numeric_zero_p(imbalance_value) && other_split)
{
if (xaccSplitGetReconcile(other_split) == NREC)
{
imbalance_value = gnc_numeric_neg(imbalance_value);
xaccSplitSetValue(other_split, imbalance_value);
xaccSplitSetAmount(other_split, imbalance_value);
}
/* else GC will automatically insert a split to equity
to balance the transaction */
}