]>
Dogcows Code - chaz/homebank/blob - src/hb-import.c
7520c03a77a76cb6c78947f906f5dcb554d1b08a
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2019 Maxime DOYEN
4 * This file is part of HomeBank.
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * HomeBank is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "hb-import.h"
24 /****************************************************************************/
26 /****************************************************************************/
35 /* our global datas */
36 extern struct HomeBank
*GLOBALS
;
37 extern struct Preferences
*PREFS
;
40 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
42 da_import_context_gen_txn_destroy(ImportContext
*context
)
46 DB( g_print("\n[import] free gen txn list\n") );
47 list
= g_list_first(context
->gen_lst_txn
);
50 GenTxn
*gentxn
= list
->data
;
51 da_gen_txn_free(gentxn
);
52 list
= g_list_next(list
);
54 g_list_free(context
->gen_lst_txn
);
55 context
->gen_lst_txn
= NULL
;
60 da_import_context_gen_acc_destroy(ImportContext
*context
)
64 DB( g_print("\n[import] free gen acc list\n") );
65 list
= g_list_first(context
->gen_lst_acc
);
68 GenAcc
*genacc
= list
->data
;
69 da_gen_acc_free(genacc
);
70 list
= g_list_next(list
);
72 g_list_free(context
->gen_lst_acc
);
73 context
->gen_lst_acc
= NULL
;
79 da_import_context_clear(ImportContext
*context
)
81 DB( g_print("\n[import] context clear\n") );
83 da_import_context_gen_txn_destroy(context
);
84 da_import_context_gen_acc_destroy(context
);
85 context
->gen_next_acckey
= 1;
91 da_import_context_destroy(ImportContext
*context
)
95 DB( g_print("\n[import] context destroy\n") );
97 da_import_context_gen_txn_destroy(context
);
98 da_import_context_gen_acc_destroy(context
);
100 DB( g_print(" free gen file list\n") );
101 list
= g_list_first(context
->gen_lst_file
);
104 GenFile
*genfile
= list
->data
;
105 da_gen_file_free(genfile
);
106 list
= g_list_next(list
);
108 g_list_free(context
->gen_lst_file
);
109 context
->gen_lst_file
= NULL
;
114 da_import_context_new(ImportContext
*context
)
116 context
->gen_lst_file
= NULL
;
118 context
->gen_lst_acc
= NULL
;
119 context
->gen_lst_txn
= NULL
;
120 context
->gen_next_acckey
= 1;
124 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
127 da_gen_file_malloc(void)
129 return g_malloc0(sizeof(GenFile
));
133 da_gen_file_free(GenFile
*genfile
)
137 if(genfile
->filepath
!= NULL
)
138 g_free(genfile
->filepath
);
146 da_gen_file_get(GList
*lst_file
, guint32 key
)
148 GenFile
*existfile
= NULL
;
151 list
= g_list_first(lst_file
);
154 GenFile
*genfile
= list
->data
;
156 if( key
== genfile
->key
)
161 list
= g_list_next(list
);
168 da_gen_file_get_by_name(GList
*lst_file
, gchar
*filepath
)
170 GenFile
*existfile
= NULL
;
173 DB( g_print("da_gen_file_get_by_name\n") );
175 list
= g_list_first(lst_file
);
178 GenFile
*genfile
= list
->data
;
180 DB( g_print(" strcasecmp '%s' '%s'\n", filepath
, genfile
->filepath
) );
182 if(!strcasecmp(filepath
, genfile
->filepath
))
185 DB( g_print(" found\n") );
188 list
= g_list_next(list
);
196 da_gen_file_append_from_filename(ImportContext
*ictx
, gchar
*filename
)
198 GenFile
*genfile
= NULL
;
201 //todo: should check if its a file !!
203 filetype
= homebank_alienfile_recognize(filename
);
205 DB( g_print(" - filename '%s', type is %d\n", filename
, filetype
) );
207 // we keep everything here
208 //if( (filetype == FILETYPE_OFX) || (filetype == FILETYPE_QIF) || (filetype == FILETYPE_CSV_HB) )
210 GenFile
*existgenfile
;
212 existgenfile
= da_gen_file_get_by_name(ictx
->gen_lst_file
, filename
);
213 if(existgenfile
== NULL
)
215 genfile
= da_gen_file_malloc();
216 genfile
->filepath
= g_strdup(filename
);
217 genfile
->filetype
= filetype
;
220 genfile
->key
= g_list_length (ictx
->gen_lst_file
) + 1;
221 ictx
->gen_lst_file
= g_list_append(ictx
->gen_lst_file
, genfile
);
230 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
234 da_gen_acc_malloc(void)
236 return g_malloc0(sizeof(GenAcc
));
240 da_gen_acc_free(GenAcc
*genacc
)
244 if(genacc
->name
!= NULL
)
245 g_free(genacc
->name
);
246 if(genacc
->number
!= NULL
)
247 g_free(genacc
->number
);
255 da_gen_acc_get_by_key(GList
*lst_acc
, guint32 key
)
257 GenAcc
*existacc
= NULL
;
260 list
= g_list_first(lst_acc
);
263 GenAcc
*genacc
= list
->data
;
265 if( key
== genacc
->key
)
270 list
= g_list_next(list
);
277 da_gen_acc_get_by_name(GList
*lst_acc
, gchar
*name
)
279 GenAcc
*existacc
= NULL
;
282 //DB( g_print("da_gen_acc_get_by_name\n") );
284 list
= g_list_first(lst_acc
);
287 GenAcc
*genacc
= list
->data
;
289 //DB( g_print(" strcasecmp '%s' '%s'\n", name, genacc->name) );
291 if(!strcasecmp(name
, genacc
->name
))
294 //DB( g_print(" found\n") );
297 list
= g_list_next(list
);
305 hb_import_acc_find_existing(gchar
*name
, gchar
*number
)
307 Account
*retacc
= NULL
;
310 DB( g_print("\n[import] acc_find_existing\n") );
312 DB( g_print(" - search number '%s'\n", number
) );
313 lacc
= list
= g_hash_table_get_values(GLOBALS
->h_acc
);
316 Account
*acc
= list
->data
;
318 //DB( g_print(" - eval acc '%s' or '%s'\n", acc->name, acc->number) );
319 if(number
!= NULL
&& acc
->number
&& strlen(acc
->number
) )
321 //prefer identifying with number & search number into acc->number
322 if(g_strstr_len(number
, -1, acc
->number
) != NULL
)
324 DB( g_print(" - match number '%s'\n", acc
->number
) );
329 list
= g_list_next(list
);
332 //# 1815964 only test name if all number test failed
333 //if not found try with name
336 DB( g_print(" - search name '%s'\n", name
) );
337 list
= g_list_first(lacc
);
340 Account
*acc
= list
->data
;
342 //DB( g_print(" - eval acc '%s' or '%s'\n", acc->name, acc->number) );
343 if(retacc
== NULL
&& name
!= NULL
)
345 if(g_strstr_len(name
, -1, acc
->name
) != NULL
)
347 DB( g_print(" - match name '%s'\n", acc
->name
) );
352 list
= g_list_next(list
);
363 hb_import_gen_acc_get_next(ImportContext
*ictx
, gint filetype
, gchar
*name
, gchar
*number
)
367 DB( g_print("\n[import] acc_get_next\n") );
369 DB( g_print(" - type='%d', name='%s', number='%s'\n", filetype
, name
, number
) );
371 // try to find a same name account
374 newacc
= da_gen_acc_get_by_name(ictx
->gen_lst_acc
, name
);
377 DB( g_print(" - found existing '%s'\n", name
) );
382 newacc
= da_gen_acc_malloc();
385 newacc
->kfile
= ictx
->curr_kfile
;
386 newacc
->key
= ictx
->gen_next_acckey
++;
387 newacc
->kacc
= DST_ACC_GLOBAL
;
391 newacc
->is_unamed
= FALSE
;
392 newacc
->name
= g_strdup(name
);
399 newacc
->is_unamed
= TRUE
;
401 genfile
= da_gen_file_get (ictx
->gen_lst_file
, newacc
->kfile
);
402 basename
= g_path_get_basename(genfile
->filepath
);
404 newacc
->name
= g_strdup_printf("%s %d", basename
, newacc
->key
);
409 newacc
->number
= g_strdup(number
);
411 ictx
->gen_lst_acc
= g_list_append(ictx
->gen_lst_acc
, newacc
);
414 DB( g_print(" - create new '%s'\n", newacc
->name
) );
417 newacc
->filetype
= filetype
;
418 ictx
->curr_kacc
= newacc
->key
;
424 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
428 da_gen_txn_malloc(void)
430 return g_malloc0(sizeof(GenTxn
));
435 da_gen_txn_free(GenTxn
*gentxn
)
441 if(gentxn
->account
!= NULL
)
442 g_free(gentxn
->account
);
444 if(gentxn
->rawinfo
!= NULL
)
445 g_free(gentxn
->rawinfo
);
446 if(gentxn
->rawpayee
!= NULL
)
447 g_free(gentxn
->rawpayee
);
448 if(gentxn
->rawmemo
!= NULL
)
449 g_free(gentxn
->rawmemo
);
451 if(gentxn
->date
!= NULL
)
452 g_free(gentxn
->date
);
453 if(gentxn
->info
!= NULL
)
454 g_free(gentxn
->info
);
455 if(gentxn
->payee
!= NULL
)
456 g_free(gentxn
->payee
);
457 if(gentxn
->memo
!= NULL
)
458 g_free(gentxn
->memo
);
459 if(gentxn
->category
!= NULL
)
460 g_free(gentxn
->category
);
461 if(gentxn
->tags
!= NULL
)
462 g_free(gentxn
->tags
);
464 for(i
=0;i
<TXN_MAX_SPLIT
;i
++)
466 GenSplit
*s
= &gentxn
->splits
[i
];
470 if(s
->category
!= NULL
)
474 if(gentxn
->lst_existing
!= NULL
)
476 g_list_free(gentxn
->lst_existing
);
477 gentxn
->lst_existing
= NULL
;
485 da_gen_txn_compare_func(GenTxn
*a
, GenTxn
*b
)
487 gint retval
= (gint
)(a
->julian
- b
->julian
);
490 retval
= (ABS(a
->amount
) - ABS(b
->amount
)) > 0 ? 1 : -1;
496 da_gen_txn_sort(GList
*list
)
498 return( g_list_sort(list
, (GCompareFunc
)da_gen_txn_compare_func
));
503 da_gen_txn_move(GenTxn
*sgentxn
, GenTxn
*dgentxn
)
505 if(sgentxn
!= NULL
&& dgentxn
!= NULL
)
507 memcpy(dgentxn
, sgentxn
, sizeof(GenTxn
));
508 memset(sgentxn
, 0, sizeof(GenTxn
));
514 da_gen_txn_append(ImportContext
*ctx
, GenTxn
*gentxn
)
516 gentxn
->kfile
= ctx
->curr_kfile
;
517 gentxn
->kacc
= ctx
->curr_kacc
;
518 gentxn
->to_import
= TRUE
;
519 ctx
->gen_lst_txn
= g_list_append(ctx
->gen_lst_txn
, gentxn
);
523 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
526 static void _string_utf8_ucfirst(gchar
**str
)
534 str_len
= strlen(*str
);
538 first
= g_utf8_strup(*str
, 1);
539 lc
= g_utf8_strdown( g_utf8_next_char(*str
), -1 );
541 *str
= g_strjoin(NULL
, first
, lc
, NULL
);
548 _string_concat(gchar
*str
, gchar
*addon
)
552 DB( g_print(" - concat '%s' + '%s'\n", str
, addon
) );
555 retval
= g_strdup(addon
);
558 retval
= g_strjoin(" ", str
, addon
, NULL
);
562 DB( g_print(" - retval='%s'\n", retval
) );
567 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
569 gchar
*hb_import_filetype_char_get(GenAcc
*genacc
)
573 switch(genacc
->filetype
)
584 case FILETYPE_CSV_HB
:
594 hb_import_load_all(ImportContext
*ictx
)
598 DB( g_print("\n[import] load all\n") );
600 da_import_context_clear (ictx
);
602 list
= g_list_first(ictx
->gen_lst_file
);
605 GenFile
*genfile
= list
->data
;
607 if(genfile
->filetype
!= FILETYPE_UNKNOWN
)
609 //todo: move this to alien analysis
610 genfile
->encoding
= homebank_file_getencoding(genfile
->filepath
);
612 ictx
->curr_kfile
= genfile
->key
;
614 DB( g_print(" -> key = '%d'\n", genfile
->key
) );
615 DB( g_print(" -> filepath = '%s'\n", genfile
->filepath
) );
616 DB( g_print(" -> encoding = '%s'\n", genfile
->encoding
) );
618 genfile
->loaded
= FALSE
;
619 genfile
->invaliddatefmt
= FALSE
;
621 switch(genfile
->filetype
)
625 homebank_ofx_import(ictx
, genfile
);
629 homebank_qif_import(ictx
, genfile
);
632 case FILETYPE_CSV_HB
:
633 homebank_csv_import(ictx
, genfile
);
637 genfile
->loaded
= TRUE
;
640 list
= g_list_next(list
);
644 ictx
->gen_lst_txn
= da_gen_txn_sort(ictx
->gen_lst_txn
);
650 hb_import_gen_acc_count_txn(ImportContext
*ictx
, GenAcc
*genacc
)
655 DB( g_print("\n[import] gen_acc_count_txn\n") );
657 genacc
->n_txnall
= 0;
658 genacc
->n_txnimp
= 0;
660 list
= g_list_first(ictx
->gen_lst_txn
);
663 GenTxn
*gentxn
= list
->data
;
665 if(gentxn
->kacc
== genacc
->key
)
670 DB( g_print(" count %03d: gentxn in=%d dup=%d '%s'\n", count
, gentxn
->to_import
, gentxn
->is_dst_similar
, gentxn
->memo
) );
672 if(gentxn
->to_import
)
675 list
= g_list_next(list
);
682 * uncheck duplicate within the import context files
685 hb_import_gen_txn_check_duplicate(ImportContext
*ictx
, GenAcc
*genacc
)
687 GList
*list1
, *list2
;
690 DB( g_print("\n[import] gen_txn_check_duplicate\n") );
693 list1
= g_list_first(ictx
->gen_lst_txn
);
694 while (list1
!= NULL
)
696 GenTxn
*gentxn1
= list1
->data
;
698 if( (genacc
->key
== gentxn1
->kacc
) && (gentxn1
->julian
!= 0) ) //same account, valid date
700 list2
= g_list_next(list1
);
701 while (list2
!= NULL
)
703 GenTxn
*gentxn2
= list2
->data
;
705 if( (gentxn2
->julian
> gentxn1
->julian
) )
708 //todo: maybe reinforce controls here
709 if( (gentxn2
->kacc
== gentxn1
->kacc
)
710 && (gentxn2
->julian
== gentxn1
->julian
)
711 && (gentxn2
->amount
== gentxn1
->amount
)
712 && (hb_string_compare(gentxn2
->memo
, gentxn1
->memo
) == 0)
713 && (hb_string_compare(gentxn2
->payee
, gentxn1
->payee
) == 0)
716 gentxn1
->to_import
= FALSE
;
717 gentxn1
->is_imp_similar
= TRUE
;
720 DB( g_print(" found import dup %d=%d %.2f %.2f in=%d dup=%d\n", gentxn1
->julian
, gentxn2
->julian
, gentxn2
->amount
, gentxn1
->amount
, gentxn1
->to_import
, gentxn1
->is_imp_similar
) );
723 list2
= g_list_next(list2
);
726 list1
= g_list_next(list1
);
733 * uncheck existing txn into target account
737 hb_import_gen_txn_check_target_similar(ImportContext
*ictx
, GenAcc
*genacc
)
739 GList
*list1
, *list2
;
742 DB( g_print("\n[import] gen_txn_check_target_similar\n") );
744 list1
= g_list_first(ictx
->gen_lst_txn
);
745 while (list1
!= NULL
)
747 GenTxn
*gentxn
= list1
->data
;
749 if(genacc
->key
== gentxn
->kacc
)
751 gentxn
->to_import
= TRUE
;
752 gentxn
->is_dst_similar
= FALSE
;
754 if(genacc
->kacc
== DST_ACC_SKIP
)
756 gentxn
->to_import
= FALSE
;
760 Account
*acc
= da_acc_get(genacc
->kacc
);
764 //clear previous existing
765 if(gentxn
->lst_existing
!= NULL
)
767 g_list_free(gentxn
->lst_existing
);
768 gentxn
->lst_existing
= NULL
;
771 // try to find existing transaction
772 list2
= g_queue_peek_tail_link(acc
->txn_queue
);
773 while (list2
!= NULL
)
775 Transaction
*txn
= list2
->data
;
777 //break if the date goes below the gentxn date + gap
778 if( txn
->date
< (gentxn
->julian
- ictx
->opt_daygap
) )
781 //#1586211 add of date tolerance
782 //todo: maybe reinforce controls here
783 if( ( txn
->kacc
== genacc
->kacc
)
784 && ( gentxn
->julian
<= (txn
->date
+ ictx
->opt_daygap
) )
785 && ( gentxn
->julian
>= (txn
->date
- ictx
->opt_daygap
) )
786 && ( txn
->amount
== gentxn
->amount
)
789 gentxn
->lst_existing
= g_list_append(gentxn
->lst_existing
, txn
);
790 gentxn
->to_import
= FALSE
;
791 gentxn
->is_dst_similar
= TRUE
;
794 DB( g_print(" found dst acc dup %d %.2f '%s' in=%d, dup=%d\n", gentxn
->julian
, gentxn
->amount
, gentxn
->memo
, gentxn
->to_import
, gentxn
->is_dst_similar
) );
797 list2
= g_list_previous(list2
);
804 list1
= g_list_next(list1
);
812 * try to indentify xfer for OFX
816 hb_import_gen_xfer_eval(ImportContext
*ictx
, GList
*list
)
818 GList
*root
, *list1
, *list2
;
822 DB( g_print("\n[import] gen xfer eval\n") );
824 root
= list1
= g_list_first(list
);
825 while (list1
!= NULL
)
827 Transaction
*txn1
= list1
->data
;
830 acc
= da_gen_acc_get_by_key(ictx
->gen_lst_acc
, txn1
->kacc
);
832 DB( g_print(" src: kacc:%d dat:%d amt:%.2f %s kfxacc:%d\n", txn1
->kacc
, txn1
->date
, txn1
->amount
, txn1
->memo
, txn1
->kxferacc
) );
834 if( (acc
!= NULL
) && (acc
->filetype
== FILETYPE_OFX
) )
838 list2
= g_list_next(root
);
839 while (list2
!= NULL
)
841 Transaction
*txn2
= list2
->data
;
843 //DB( g_print(" -- chk: kacc:%d dat:%d amt:%.2f %s\n", txn2->kacc, txn2->date, txn2->amount, txn2->memo) );
844 if( (txn2
->date
> txn1
->date
) )
847 if( (txn2
== txn1
) || (txn2
->paymode
== PAYMODE_INTXFER
) )
850 //todo: maybe reinforce controls here
851 if( (txn2
->kacc
!= txn1
->kacc
)
852 && (txn2
->date
== txn1
->date
)
853 && (txn2
->amount
== -txn1
->amount
)
854 && (hb_string_compare(txn2
->memo
, txn1
->memo
) == 0)
857 DB( g_print(" match: kacc:%d dat:%d amt:%.2f %s kfxacc:%d\n", txn2
->kacc
, txn2
->date
, txn2
->amount
, txn2
->memo
, txn2
->kxferacc
) );
858 match
= g_list_append(match
, txn2
);
862 list2
= g_list_next(list2
);
865 if(count
== 1) //we found a single potential xfer, transform it
869 DB( g_print(" single found => convert both\n") );
871 list2
= g_list_first(match
);
875 txn1
->paymode
= PAYMODE_INTXFER
;
876 transaction_xfer_change_to_child(txn1
, txn2
);
878 /*list2 = g_list_first(match);
881 txn1->paymode = PAYMODE_INTXFER;
882 txn1->kxferacc = txn2->kacc;
884 txn2->paymode = PAYMODE_INTXFER;
885 txn2->kxferacc = txn1->kacc;
888 // if more than one, we cannot be sure
892 list1
= g_list_next(list1
);
900 * apply the user option: date format, payee/memo/info mapping
904 hb_import_option_apply(ImportContext
*ictx
, GenAcc
*genacc
)
908 DB( g_print("\n[import] option apply\n") );
910 DB( g_print(" - type=%d\n", genacc
->filetype
) );
912 genacc
->n_txnbaddate
= 0;
914 list
= g_list_first(ictx
->gen_lst_txn
);
917 GenTxn
*gentxn
= list
->data
;
919 if(gentxn
->kacc
== genacc
->key
)
921 if(genacc
->filetype
!= FILETYPE_OFX
)
923 gentxn
->julian
= hb_date_get_julian(gentxn
->date
, ictx
->opt_dateorder
);
924 if( gentxn
->julian
== 0 )
926 genacc
->n_txnbaddate
++;
930 if(genacc
->filetype
== FILETYPE_OFX
)
932 DB( g_print(" - ofx option apply\n") );
934 g_free(gentxn
->payee
);
935 g_free(gentxn
->memo
);
936 g_free(gentxn
->info
);
937 gentxn
->payee
= NULL
;
942 gentxn
->info
= g_strdup(gentxn
->rawinfo
);
944 //#1791482 map name to info (concat only)
945 switch(ictx
->opt_ofxname
)
947 //ofxname is stored into rawpayee
949 gentxn
->memo
= g_strdup(gentxn
->rawpayee
);
952 gentxn
->payee
= g_strdup(gentxn
->rawpayee
);
955 g_free(gentxn
->info
);
956 gentxn
->info
= _string_concat(gentxn
->rawinfo
, gentxn
->rawpayee
);
960 if(gentxn
->rawmemo
!= NULL
)
962 switch(ictx
->opt_ofxmemo
)
965 case 1: //add to info
966 gentxn
->info
= _string_concat(gentxn
->info
, gentxn
->rawmemo
);
969 case 2: //add to memo
970 gentxn
->memo
= _string_concat(gentxn
->memo
, gentxn
->rawmemo
);
973 case 3: //add to payee
974 gentxn
->payee
= _string_concat(gentxn
->payee
, gentxn
->rawmemo
);
979 DB( g_print(" - payee is '%s'\n", gentxn
->payee
) );
980 DB( g_print(" - memo is '%s'\n", gentxn
->memo
) );
981 DB( g_print(" - info is '%s'\n", gentxn
->info
) );
986 if(genacc
->filetype
== FILETYPE_QIF
)
988 DB( g_print(" - qif option apply\n") );
990 g_free(gentxn
->payee
);
991 g_free(gentxn
->memo
);
992 gentxn
->payee
= NULL
;
995 if(!ictx
->opt_qifswap
)
997 gentxn
->payee
= g_strdup(gentxn
->rawpayee
);
998 if(ictx
->opt_qifmemo
)
999 gentxn
->memo
= g_strdup(gentxn
->rawmemo
);
1003 gentxn
->payee
= g_strdup(gentxn
->rawmemo
);
1004 if(ictx
->opt_qifmemo
)
1005 gentxn
->memo
= g_strdup(gentxn
->rawpayee
);
1008 DB( g_print(" - payee is '%s'\n", gentxn
->payee
) );
1009 DB( g_print(" - memo is '%s'\n", gentxn
->memo
) );
1013 if(genacc
->filetype
== FILETYPE_CSV_HB
)
1015 DB( g_print(" - csv option apply\n") );
1017 //#1791656 missing: info, payee and tagsg_freg_free(gentxn->payee);
1018 g_free(gentxn
->payee
);
1019 g_free(gentxn
->memo
);
1020 g_free(gentxn
->info
);
1022 gentxn
->payee
= g_strdup(gentxn
->rawpayee
);
1023 gentxn
->memo
= g_strdup(gentxn
->rawmemo
);
1024 gentxn
->info
= g_strdup(gentxn
->rawinfo
);
1027 //at last do ucfirst
1028 if( (ictx
->opt_ucfirst
== TRUE
) )
1030 _string_utf8_ucfirst(&gentxn
->memo
);
1031 _string_utf8_ucfirst(&gentxn
->payee
);
1036 list
= g_list_next(list
);
1039 DB( g_print(" - nb_err=%d\n", genacc
->n_txnbaddate
) );
1041 return genacc
->n_txnbaddate
== 0 ? TRUE
: FALSE
;
1046 * convert a GenTxn to a Transaction
1050 hb_import_convert_txn(GenAcc
*genacc
, GenTxn
*gentxn
)
1052 Transaction
*newope
;
1058 DB( g_print("\n[import] convert txn\n") );
1062 DB( g_print(" - gentxt %s %s %s\n", gentxn
->account
, gentxn
->date
, gentxn
->memo
) );
1063 DB( g_print(" - genacc '%s' '%p'\n", gentxn
->account
, genacc
) );
1067 newope
= da_transaction_malloc();
1069 newope
->kacc
= genacc
->kacc
;
1070 newope
->date
= gentxn
->julian
;
1071 newope
->paymode
= gentxn
->paymode
;
1072 newope
->info
= g_strdup(gentxn
->info
);
1073 newope
->memo
= g_strdup(gentxn
->memo
);
1074 newope
->amount
= gentxn
->amount
;
1076 if(newope
->amount
> 0)
1077 newope
->flags
|= OF_INCOME
;
1079 //#773282 invert amount for ccard accounts
1080 //todo: manage this (qif), it is not set to true anywhere
1081 //if(ictx->is_ccard)
1082 // gentxn->amount *= -1;
1085 if( gentxn
->payee
!= NULL
)
1087 payitem
= da_pay_get_by_name(gentxn
->payee
);
1090 //DB( g_print(" -> append pay: '%s'\n", item->payee ) );
1092 payitem
= da_pay_malloc();
1093 payitem
->name
= g_strdup(gentxn
->payee
);
1094 //payitem->imported = TRUE;
1095 da_pay_append(payitem
);
1097 //ictx->cnt_new_pay += 1;
1099 newope
->kpay
= payitem
->key
;
1102 // LCategory of transaction
1103 // L[Transfer account name]
1104 // LCategory of transaction/Class of transaction
1105 // L[Transfer account]/Class of transaction
1106 if( gentxn
->category
!= NULL
)
1108 if(g_str_has_prefix(gentxn
->category
, "[")) // this is a transfer account name
1112 //DB ( g_print(" -> transfer to: '%s'\n", item->category) );
1115 accname
= hb_strdup_nobrackets(gentxn
->category
);
1117 // search target account + append if not exixts
1118 accitem
= da_acc_get_by_name(accname
);
1121 DB( g_print(" -> append int xfer dest acc: '%s'\n", accname
) );
1123 accitem
= da_acc_malloc();
1124 accitem
->name
= g_strdup(accname
);
1125 //accitem->imported = TRUE;
1126 //accitem->imp_name = g_strdup(accname);
1127 da_acc_append(accitem
);
1130 newope
->kxferacc
= accitem
->key
;
1131 newope
->paymode
= PAYMODE_INTXFER
;
1137 //DB ( g_print(" -> append cat: '%s'\n", item->category) );
1139 catitem
= da_cat_append_ifnew_by_fullname(gentxn
->category
);
1140 if( catitem
!= NULL
)
1142 //ictx->cnt_new_cat += 1;
1143 newope
->kcat
= catitem
->key
;
1148 //#1791656 miss tags also...
1149 if( gentxn
->tags
!= NULL
)
1151 g_free(newope
->tags
);
1152 newope
->tags
= tags_parse(gentxn
->tags
);
1155 // splits, if not a xfer
1156 if( gentxn
->paymode
!= PAYMODE_INTXFER
)
1158 if( gentxn
->nb_splits
> 0 )
1160 newope
->splits
= da_split_new();
1161 for(nsplit
=0;nsplit
<gentxn
->nb_splits
;nsplit
++)
1163 GenSplit
*s
= &gentxn
->splits
[nsplit
];
1167 DB( g_print(" -> append split %d: '%s' '%.2f' '%s'\n", nsplit
, s
->category
, s
->amount
, s
->memo
) );
1169 if( s
->category
!= NULL
)
1171 catitem
= da_cat_append_ifnew_by_fullname(s
->category
);
1172 if( catitem
!= NULL
)
1174 kcat
= catitem
->key
;
1178 //todo: remove this when no more use ||
1179 hb_string_replace_char('|', s
->memo
);
1180 hbs
= da_split_malloc ();
1182 hbs
->memo
= g_strdup(s
->memo
);
1183 hbs
->amount
= s
->amount
;
1184 da_splits_append(newope
->splits
, hbs
);
1190 newope
->flags
|= OF_ADDED
;
1191 if( newope
->amount
> 0 )
1192 newope
->flags
|= OF_INCOME
;
1194 if( gentxn
->reconciled
)
1195 newope
->status
= TXN_STATUS_RECONCILED
;
1197 if( gentxn
->cleared
)
1198 newope
->status
= TXN_STATUS_CLEARED
;
1205 hb_import_apply(ImportContext
*ictx
)
1209 guint32 kcommon
= 0;
1212 DB( g_print("\n[import] apply\n") );
1215 list
= g_list_first(ictx
->gen_lst_acc
);
1216 while (list
!= NULL
)
1218 GenAcc
*genacc
= list
->data
;
1220 DB( g_print(" #1 genacc: %d %s %s => %d\n", genacc
->key
, genacc
->name
, genacc
->number
, genacc
->kacc
) );
1222 //we do create the common account once
1223 if( (genacc
->kacc
== DST_ACC_GLOBAL
) )
1227 Account
*acc
= da_acc_malloc ();
1229 acc
->name
= g_strdup(_("imported account"));
1230 if( da_acc_append(acc
) )
1237 genacc
->kacc
= kcommon
;
1240 if( (genacc
->kacc
== DST_ACC_NEW
) )
1242 Account
*acc
= da_acc_malloc ();
1244 acc
->name
= g_strdup(genacc
->name
);
1245 if( da_acc_append(acc
) )
1247 acc
->number
= g_strdup(genacc
->number
);
1248 acc
->initial
= genacc
->initial
;
1250 //store the target acc key
1251 genacc
->kacc
= acc
->key
;
1256 list
= g_list_next(list
);
1259 // insert every transactions into a temporary list
1260 // we do this to keep a finished real txn list for detect xfer below
1261 DB( g_print(" #2 insert txn\n") );
1264 lacc
= g_list_first(ictx
->gen_lst_acc
);
1265 while (lacc
!= NULL
)
1267 GenAcc
*genacc
= lacc
->data
;
1269 if(genacc
->kacc
!= DST_ACC_SKIP
)
1271 list
= g_list_first(ictx
->gen_lst_txn
);
1272 while (list
!= NULL
)
1274 GenTxn
*gentxn
= list
->data
;
1276 if(gentxn
->kacc
== genacc
->key
&& gentxn
->to_import
== TRUE
)
1278 Transaction
*txn
, *dtxn
;
1280 txn
= hb_import_convert_txn(genacc
, gentxn
);
1283 dtxn
= transaction_add(NULL
, txn
);
1284 txnlist
= g_list_append(txnlist
, dtxn
);
1285 da_transaction_free(txn
);
1286 //#1820618 forgot to report changes count
1290 list
= g_list_next(list
);
1293 lacc
= g_list_next(lacc
);
1297 DB( g_print(" call auto assign\n") );
1298 transaction_auto_assign(txnlist
, 0);
1300 //check for ofx internal xfer
1301 DB( g_print(" call hb_import_gen_xfer_eval\n") );
1302 hb_import_gen_xfer_eval(ictx
, txnlist
);
1304 g_list_free(txnlist
);
1306 GLOBALS
->changes_count
+= changes
;
1312 void _import_context_debug_file_list(ImportContext
*ctx
)
1316 g_print("\n--debug-- file list %d\n", g_list_length(ctx
->gen_lst_file
) );
1318 list
= g_list_first(ctx
->gen_lst_file
);
1319 while (list
!= NULL
)
1321 GenFile
*item
= list
->data
;
1323 g_print(" genfile: %d '%s' '%s'\ndf=%d invalid=%d\n", item
->key
, item
->filepath
, item
->encoding
, item
->datefmt
, item
->invaliddatefmt
);
1325 list
= g_list_next(list
);
1330 void _import_context_debug_acc_list(ImportContext
*ctx
)
1334 g_print("\n--debug-- acc list %d\n", g_list_length(ctx
->gen_lst_acc
) );
1336 list
= g_list_first(ctx
->gen_lst_acc
);
1337 while (list
!= NULL
)
1339 GenAcc
*item
= list
->data
;
1341 g_print(" genacc: %d %s %s => %d\n", item
->key
, item
->name
, item
->number
, item
->kacc
);
1343 list
= g_list_next(list
);
1349 void _import_context_debug_txn_list(ImportContext
*ctx
)
1353 g_print("\n--debug-- txn list %d\n", g_list_length(ctx
->gen_lst_txn
) );
1355 list
= g_list_first(ctx
->gen_lst_txn
);
1356 while (list
!= NULL
)
1358 GenTxn
*item
= list
->data
;
1360 g_print(" gentxn: (%d) %s %s (%d) %s %.2f\n", item
->kfile
, item
->account
, item
->date
, item
->julian
, item
->memo
, item
->amount
);
1362 list
= g_list_next(list
);
This page took 0.091205 seconds and 4 git commands to generate.