X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fhomebank;a=blobdiff_plain;f=src%2Fhb-transaction.c;h=0368946f9fbb4267bb383d7ace7200f70f03a6a2;hp=8ab4306a937e3fc013d92e0c6f6306416091fa7e;hb=a6c6b0df5492c2160ed97e3a376bdb2fe7c5ebc4;hpb=b84403141a4c3a32a594800eb3fcabdc856461f8 diff --git a/src/hb-transaction.c b/src/hb-transaction.c index 8ab4306..0368946 100644 --- a/src/hb-transaction.c +++ b/src/hb-transaction.c @@ -1,5 +1,5 @@ /* HomeBank -- Free, easy, personal accounting for everyone. - * Copyright (C) 1995-2017 Maxime DOYEN + * Copyright (C) 1995-2019 Maxime DOYEN * * This file is part of HomeBank. * @@ -24,7 +24,7 @@ #include "hb-split.h" /****************************************************************************/ -/* Debug macros */ +/* Debug macro */ /****************************************************************************/ #define MYDEBUG 0 @@ -46,10 +46,10 @@ da_transaction_clean(Transaction *item) { if(item != NULL) { - if(item->wording != NULL) + if(item->memo != NULL) { - g_free(item->wording); - item->wording = NULL; + g_free(item->memo); + item->memo = NULL; } if(item->info != NULL) { @@ -63,8 +63,12 @@ da_transaction_clean(Transaction *item) item->tags = NULL; } - da_splits_free(item->splits); - item->flags &= ~(OF_SPLIT); //Flag that Splits are cleared + if(item->splits != NULL) + { + da_split_destroy(item->splits); + item->splits = NULL; + item->flags &= ~(OF_SPLIT); //Flag that Splits are cleared + } if(item->same != NULL) { @@ -75,7 +79,6 @@ da_transaction_clean(Transaction *item) } - void da_transaction_free(Transaction *item) { @@ -94,30 +97,10 @@ da_transaction_malloc(void) } -Transaction *da_transaction_copy(Transaction *src_txn, Transaction *dst_txn) -{ - DB( g_print("da_transaction_copy\n") ); - - da_transaction_clean (dst_txn); - - memmove(dst_txn, src_txn, sizeof(Transaction)); - - //duplicate the string - dst_txn->wording = g_strdup(src_txn->wording); - dst_txn->info = g_strdup(src_txn->info); - - //duplicate tags - transaction_tags_clone(src_txn, dst_txn); - - if (da_splits_clone(src_txn->splits, dst_txn->splits) > 0) - dst_txn->flags |= OF_SPLIT; //Flag that Splits are active - - return dst_txn; -} - - Transaction *da_transaction_init_from_template(Transaction *txn, Archive *arc) { + DB( g_print("da_transaction_init_from_template\n") ); + //txn->date = 0; txn->amount = arc->amount; //#1258344 keep the current account if tpl is empty @@ -129,15 +112,44 @@ Transaction *da_transaction_init_from_template(Transaction *txn, Archive *arc) txn->kpay = arc->kpay; txn->kcat = arc->kcat; txn->kxferacc = arc->kxferacc; - txn->wording = g_strdup(arc->wording); + txn->memo = g_strdup(arc->memo); txn->info = NULL; - if( da_splits_clone(arc->splits, txn->splits) > 0) + + //copy tags (with free previous here) + g_free(txn->tags); + txn->tags = tags_clone(arc->tags); + + da_split_destroy (txn->splits); + txn->splits = da_splits_clone(arc->splits); + if( da_splits_length (txn->splits) > 0 ) txn->flags |= OF_SPLIT; //Flag that Splits are active return txn; } +Transaction *da_transaction_set_default_template(Transaction *txn) +{ +Account *acc; +Archive *arc; + + DB( g_print("da_transaction_set_default_template\n") ); + + acc = da_acc_get(txn->kacc); + if(acc != NULL && acc->karc > 0) + { + arc = da_archive_get(acc->karc); + if( arc ) + { + DB( g_print(" - init with default template\n") ); + da_transaction_init_from_template(txn, arc); + } + } + + return txn; +} + + Transaction *da_transaction_clone(Transaction *src_item) { Transaction *new_item = g_memdup(src_item, sizeof(Transaction)); @@ -147,13 +159,15 @@ Transaction *new_item = g_memdup(src_item, sizeof(Transaction)); if(new_item) { //duplicate the string - new_item->wording = g_strdup(src_item->wording); + new_item->memo = g_strdup(src_item->memo); new_item->info = g_strdup(src_item->info); - //duplicate tags - transaction_tags_clone(src_item, new_item); + //duplicate tags/splits + //no g_free here to avoid free the src tags (memdup copied the ptr) + new_item->tags = tags_clone(src_item->tags); - if( da_splits_clone(src_item->splits, new_item->splits) > 0) + new_item->splits = da_splits_clone(src_item->splits); + if( da_splits_length (new_item->splits) > 0 ) new_item->flags |= OF_SPLIT; //Flag that Splits are active } @@ -235,16 +249,22 @@ GList *da_transaction_sort(GList *list) } -static void da_transaction_insert_memo(Transaction *item) +gboolean da_transaction_insert_memo(Transaction *item) { - // append the memo if new - if( item->wording != NULL ) +gboolean retval = FALSE; + + if( item->memo != NULL ) { - if( g_hash_table_lookup(GLOBALS->h_memo, item->wording) == NULL ) + //# 1673048 add filter on status and date obsolete + if( (PREFS->txn_memoacp == TRUE) && (item->date >= (GLOBALS->today - PREFS->txn_memoacp_days)) ) { - g_hash_table_insert(GLOBALS->h_memo, g_strdup(item->wording), NULL); + if( g_hash_table_lookup(GLOBALS->h_memo, item->memo) == NULL ) + { + retval = g_hash_table_insert(GLOBALS->h_memo, g_strdup(item->memo), NULL); + } } } + return retval; } @@ -282,8 +302,11 @@ gboolean da_transaction_prepend(Transaction *item) Account *acc; acc = da_acc_get(item->kacc); - if(acc) - item->kcur = acc->kcur; + //#1661279 + if(!acc) + return FALSE; + + item->kcur = acc->kcur; g_queue_push_tail(acc->txn_queue, item); da_transaction_insert_memo(item); return TRUE; @@ -332,7 +355,12 @@ guint32 max_key = 0; static void da_transaction_goto_orphan(Transaction *txn) { const gchar *oatn = "orphaned transactions"; -Account *acc; +Account *ori_acc, *acc; +gboolean found; + + DB( g_print("\n[transaction] goto orphan\n") ); + + g_warning("txn consistency: moving to orphan %d '%s' %.2f", txn->date, txn->memo, txn->amount); acc = da_acc_get_by_name((gchar *)oatn); if(acc == NULL) @@ -340,8 +368,21 @@ Account *acc; acc = da_acc_malloc(); acc->name = g_strdup(oatn); da_acc_append(acc); + DB( g_print(" - created orphan acc %d\n", acc->key) ); + } + + ori_acc = da_acc_get(txn->kacc); + if( ori_acc ) + { + found = g_queue_remove(ori_acc->txn_queue, txn); + DB( g_print(" - found in origin ? %d\n", found) ); + if(found) + { + txn->kacc = acc->key; + da_transaction_insert_sorted (txn); + DB( g_print("moved txn to %d\n", txn->kacc) ); + } } - txn->kacc = acc->key; } @@ -350,7 +391,9 @@ void da_transaction_consistency(Transaction *item) Account *acc; Category *cat; Payee *pay; -gint nbsplit; +guint nbsplit; + + DB( g_print("\n[transaction] consistency\n") ); // ensure date is between range item->date = CLAMP(item->date, HB_MINDATE, HB_MAXDATE); @@ -373,16 +416,17 @@ gint nbsplit; GLOBALS->changes_count++; } - // check split category #1340142 - split_cat_consistency(item->splits); - - //# 1416624 empty category when split - nbsplit = da_splits_count(item->splits); - if(nbsplit > 0 && item->kcat > 0) + //#1340142 check split category + if( item->splits != NULL ) { - g_warning("txn consistency: fixed invalid cat on split txn"); - item->kcat = 0; - GLOBALS->changes_count++; + nbsplit = da_splits_consistency(item->splits); + //# 1416624 empty category when split + if(nbsplit > 0 && item->kcat > 0) + { + g_warning("txn consistency: fixed invalid cat on split txn"); + item->kcat = 0; + GLOBALS->changes_count++; + } } // check payee exists @@ -396,10 +440,30 @@ gint nbsplit; // reset dst acc for non xfer transaction if( item->paymode != PAYMODE_INTXFER ) + { + item->kxfer = 0; item->kxferacc = 0; + } - //#1628678 tags for internal xfer should be checked as well + // check dst account exists + if( item->paymode == PAYMODE_INTXFER ) + { + gint tak = item->kxferacc; + item->kxferacc = ABS(tak); //I crossed negative here one day + acc = da_acc_get(item->kxferacc); + if(acc == NULL) + { + g_warning("txn consistency: fixed invalid dst_acc %d", item->kxferacc); + da_transaction_goto_orphan(item); + item->kxfer = 0; + item->paymode = PAYMODE_XFER; + GLOBALS->changes_count++; + } + } + + //#1628678 tags for internal xfer should be checked as well + //#1787826 intxfer should not have split //#1295877 ensure income flag is correctly set item->flags &= ~(OF_INCOME); @@ -434,8 +498,9 @@ gchar swap; child->amount = -child->amount; child->flags ^= (OF_INCOME); // invert flag - //#1268026 - child->status = TXN_STATUS_NONE; + //#1268026 #1690555 + if( child->status != TXN_STATUS_REMIND ) + child->status = TXN_STATUS_NONE; //child->flags &= ~(OF_VALID); // delete reconcile state swap = child->kacc; @@ -507,12 +572,14 @@ gboolean retval = FALSE; } -static GList *transaction_xfer_child_might_list_get(Transaction *ope) +static GList *transaction_xfer_child_might_list_get(Transaction *ope, guint32 kdstacc) { GList *lst_acc, *lnk_acc; GList *list, *matchlist = NULL; - //DB( g_print("\n[transaction] xfer_child_might_list_get\n") ); + DB( g_print("\n[transaction] xfer_child_might_list_get\n") ); + + DB( g_print(" - kdstacc:%d\n", kdstacc) ); lst_acc = g_hash_table_get_values(GLOBALS->h_acc); lnk_acc = g_list_first(lst_acc); @@ -520,7 +587,7 @@ GList *list, *matchlist = NULL; { Account *acc = lnk_acc->data; - if( !(acc->flags & AF_CLOSED) && (acc->key != ope->kacc) ) + if( !(acc->flags & AF_CLOSED) && (acc->key != ope->kacc) && ( (acc->key == kdstacc) || kdstacc == 0 ) ) { list = g_queue_peek_tail_link(acc->txn_queue); while (list != NULL) @@ -533,7 +600,7 @@ GList *list, *matchlist = NULL; if( transaction_xfer_child_might(ope, item, 0) == TRUE ) { - //DB( g_print(" - match : %d %s %f %d=>%d\n", item->date, item->wording, item->amount, item->account, item->kxferacc) ); + DB( g_print(" - match : %d %s %f %d=>%d\n", item->date, item->memo, item->amount, item->kacc, item->kxferacc) ); matchlist = g_list_append(matchlist, item); } list = g_list_previous(list); @@ -548,14 +615,14 @@ GList *list, *matchlist = NULL; } -void transaction_xfer_search_or_add_child(GtkWindow *parentwindow, Transaction *ope, gboolean manual) +void transaction_xfer_search_or_add_child(GtkWindow *parent, Transaction *ope, guint32 kdstacc) { GList *matchlist; gint count; DB( g_print("\n[transaction] xfer_search_or_add_child\n") ); - matchlist = transaction_xfer_child_might_list_get(ope); + matchlist = transaction_xfer_child_might_list_get(ope, kdstacc); count = g_list_length(matchlist); DB( g_print(" - found %d might match, switching\n", count) ); @@ -567,7 +634,7 @@ gint count; break; //todo: maybe with just 1 match the user must choose ? - //#942346: bad idea so to no let the user confirm, so let hil confirm + //#942346: bad idea so to no let the user confirm, so let him confirm /* case 1: //transform the transaction to a child transfer { @@ -579,13 +646,20 @@ gint count; default: //the user must choose himself { + gint result; Transaction *child; - child = ui_dialog_transaction_xfer_select_child(ope, matchlist); - if(child == NULL) - transaction_xfer_create_child(ope); - else + result = ui_dialog_transaction_xfer_select_child(parent, ope, matchlist, &child); + if( result == GTK_RESPONSE_ACCEPT ) + { transaction_xfer_change_to_child(ope, child); + } + else //GTK_RESPONSE_CANCEL + { + ope->paymode = PAYMODE_NONE; + ope->kxfer = 0; + ope->kxferacc = 0; + } } } @@ -604,7 +678,7 @@ GList *list; if( !dstacc || src->kxfer <= 0 ) return NULL; - DB( g_print(" - search: %d %s %f %d=>%d - %d\n", src->date, src->wording, src->amount, src->kacc, src->kxferacc, src->kxfer) ); + DB( g_print(" - search: %d %s %f %d=>%d - %d\n", src->date, src->memo, src->amount, src->kacc, src->kxferacc, src->kxfer) ); list = g_queue_peek_tail_link(dstacc->txn_queue); while (list != NULL) @@ -619,7 +693,7 @@ GList *list; && item->kxfer == src->kxfer && item != src ) { - DB( g_print(" - found : %d %s %f %d=>%d - %d\n", item->date, item->wording, item->amount, item->kacc, item->kxferacc, src->kxfer) ); + DB( g_print(" - found : %d %s %f %d=>%d - %d\n", item->date, item->memo, item->amount, item->kacc, item->kxferacc, src->kxfer) ); return item; } list = g_list_previous(list); @@ -662,10 +736,24 @@ Account *dstacc; } -void transaction_xfer_sync_child(Transaction *s_txn, Transaction *child) +void transaction_xfer_child_sync(Transaction *s_txn, Transaction *child) { +Account *acc; - DB( g_print("\n[transaction] xfer_sync_child\n") ); + DB( g_print("\n[transaction] xfer_child_sync\n") ); + + if( child == NULL ) + { + DB( g_print(" - no child found\n") ); + return; + } + + DB( g_print(" - found do sync\n") ); + + /* update acc flags */ + acc = da_acc_get( child->kacc); + if(acc != NULL) + acc->flags |= AF_CHANGED; account_balances_sub (child); @@ -678,23 +766,32 @@ void transaction_xfer_sync_child(Transaction *s_txn, Transaction *child) child->flags |= (OF_INCOME); child->kpay = s_txn->kpay; child->kcat = s_txn->kcat; - if(child->wording) - g_free(child->wording); - child->wording = g_strdup(s_txn->wording); + if(child->memo) + g_free(child->memo); + child->memo = g_strdup(s_txn->memo); if(child->info) g_free(child->info); child->info = g_strdup(s_txn->info); + account_balances_add (child); + //#1252230 sync account also - child->kacc = s_txn->kxferacc; - child->kxferacc = s_txn->kacc; + //#1663789 idem after 5.1 + //source changed: update child key (move of s_txn is done in external_edit) + if( s_txn->kacc != child->kxferacc ) + { + child->kxferacc = s_txn->kacc; + } + + //dest changed: move child & update child key + if( s_txn->kxferacc != child->kacc ) + { + transaction_acc_move(child, child->kacc, s_txn->kxferacc); + } - account_balances_add (child); - //synchronise tags since 5.1 - if(child->tags) - g_free(child->tags); - transaction_tags_clone (s_txn, child); + g_free(child->tags); + child->tags = tags_clone (s_txn->tags); } @@ -706,20 +803,26 @@ Transaction *dst; DB( g_print("\n[transaction] xfer_remove_child\n") ); dst = transaction_xfer_child_strong_get( src ); - - DB( g_print(" -> return is %s, %p\n", dst->wording, dst) ); - if( dst != NULL ) { Account *acc = da_acc_get(dst->kacc); - DB( g_print("deleting...") ); - src->kxfer = 0; - src->kxferacc = 0; - account_balances_sub(dst); - g_queue_remove(acc->txn_queue, dst); - da_transaction_free (dst); + if( acc != NULL ) + { + DB( g_print("deleting...") ); + account_balances_sub(dst); + g_queue_remove(acc->txn_queue, dst); + //#1419304 we keep the deleted txn to a trash stack + //da_transaction_free (dst); + g_trash_stack_push(&GLOBALS->txn_stk, dst); + + //#1691992 + acc->flags |= AF_CHANGED; + } } + + src->kxfer = 0; + src->kxferacc = 0; } @@ -731,31 +834,34 @@ GList *list; DB( g_print("\n[transaction] get_child_transfer\n") ); - //DB( g_print(" search: %d %s %f %d=>%d\n", src->date, src->wording, src->amount, src->account, src->kxferacc) ); + //DB( g_print(" search: %d %s %f %d=>%d\n", src->date, src->memo, src->amount, src->account, src->kxferacc) ); acc = da_acc_get(src->kxferacc); - list = g_queue_peek_head_link(acc->txn_queue); - while (list != NULL) + if( acc != NULL ) { - Transaction *item = list->data; + list = g_queue_peek_head_link(acc->txn_queue); + while (list != NULL) + { + Transaction *item = list->data; - // no need to go higher than src txn date - if(item->date > src->date) - break; + // no need to go higher than src txn date + if(item->date > src->date) + break; - if( item->paymode == PAYMODE_INTXFER) - { - if( src->date == item->date && - src->kacc == item->kxferacc && - src->kxferacc == item->kacc && - ABS(src->amount) == ABS(item->amount) ) + if( item->paymode == PAYMODE_INTXFER) { - //DB( g_print(" found : %d %s %f %d=>%d\n", item->date, item->wording, item->amount, item->account, item->kxferacc) ); + if( src->date == item->date && + src->kacc == item->kxferacc && + src->kxferacc == item->kacc && + ABS(src->amount) == ABS(item->amount) ) + { + //DB( g_print(" found : %d %s %f %d=>%d\n", item->date, item->memo, item->amount, item->account, item->kxferacc) ); - return item; + return item; + } } + list = g_list_next(list); } - list = g_list_next(list); } DB( g_print(" not found...\n") ); @@ -767,7 +873,45 @@ GList *list; /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */ -void transaction_add(Transaction *ope, GtkWidget *treeview, guint32 accnum) +void transaction_remove(Transaction *ope) +{ +Account *acc; + + //controls accounts valid (archive scheduled maybe bad) + acc = da_acc_get(ope->kacc); + if(acc == NULL) return; + + account_balances_sub(ope); + + if( ope->paymode == PAYMODE_INTXFER ) + { + transaction_xfer_remove_child( ope ); + } + + g_queue_remove(acc->txn_queue, ope); + acc->flags |= AF_CHANGED; + //#1419304 we keep the deleted txn to a trash stack + //da_transaction_free(entry); + g_trash_stack_push(&GLOBALS->txn_stk, ope); +} + + +void transaction_changed(Transaction *txn) +{ +Account *acc; + + if( txn == NULL ) + return; + + acc = da_acc_get(txn->kacc); + if(acc == NULL) + return; + + acc->flags |= AF_CHANGED; +} + + +Transaction *transaction_add(GtkWindow *parent, Transaction *ope) { Transaction *newope; Account *acc; @@ -776,7 +920,7 @@ Account *acc; //controls accounts valid (archive scheduled maybe bad) acc = da_acc_get(ope->kacc); - if(acc == NULL) return; + if(acc == NULL) return NULL; DB( g_print(" acc is '%s' %d\n", acc->name, acc->key) ); @@ -785,10 +929,11 @@ Account *acc; if(ope->paymode == PAYMODE_INTXFER) { acc = da_acc_get(ope->kxferacc); - if(acc == NULL) return; + if(acc == NULL) return NULL; // delete any splits - da_splits_free(ope->splits); + da_split_destroy(ope->splits); + ope->splits = NULL; ope->flags &= ~(OF_SPLIT); //Flag that Splits are cleared } @@ -812,16 +957,19 @@ Account *acc; /* get the active account and the corresponding cheque number */ acc = da_acc_get( newope->kacc); - cheque = atol(newope->info); - - //DB( g_print(" -> should store cheque number %d to %d", cheque, newope->account) ); - if( newope->flags & OF_CHEQ2 ) + if( acc != NULL ) { - acc->cheque2 = MAX(acc->cheque2, cheque); - } - else - { - acc->cheque1 = MAX(acc->cheque1, cheque); + cheque = atol(newope->info); + + //DB( g_print(" -> should store cheque number %d to %d", cheque, newope->account) ); + if( newope->flags & OF_CHEQ2 ) + { + acc->cheque2 = MAX(acc->cheque2, cheque); + } + else + { + acc->cheque1 = MAX(acc->cheque1, cheque); + } } } @@ -835,47 +983,15 @@ Account *acc; //da_transaction_append(newope); da_transaction_insert_sorted(newope); - if(treeview != NULL) - transaction_add_treeview(newope, treeview, accnum); - account_balances_add(newope); if(newope->paymode == PAYMODE_INTXFER) { - transaction_xfer_search_or_add_child(NULL, newope, FALSE); + transaction_xfer_search_or_add_child(parent, newope, newope->kxferacc); } } -} - - -void transaction_add_treeview(Transaction *ope, GtkWidget *treeview, guint32 accnum) -{ -GtkTreeModel *model; -GtkTreeIter iter; -//GtkTreePath *path; -//GtkTreeSelection *sel; - - DB( g_print("\n[transaction] add_treeview\n") ); - - if(ope->kacc == accnum) - { - model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview)); - gtk_list_store_append (GTK_LIST_STORE(model), &iter); - - gtk_list_store_set (GTK_LIST_STORE(model), &iter, - LST_DSPOPE_DATAS, ope, - -1); - - //activate that new line - //path = gtk_tree_model_get_path(model, &iter); - //gtk_tree_view_expand_to_path(GTK_TREE_VIEW(treeview), path); - - //sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview)); - //gtk_tree_selection_select_iter(sel, &iter); - - //gtk_tree_path_free(path); - - } + + return newope; } @@ -885,21 +1001,29 @@ Account *oacc, *nacc; DB( g_print("\n[transaction] acc_move\n") ); + if( okacc == nkacc ) + return TRUE; + oacc = da_acc_get(okacc); nacc = da_acc_get(nkacc); if( oacc && nacc ) { + account_balances_sub(txn); if( g_queue_remove(oacc->txn_queue, txn) ) { g_queue_push_tail(nacc->txn_queue, txn); txn->kacc = nacc->key; txn->kcur = nacc->kcur; nacc->flags |= AF_CHANGED; + account_balances_add(txn); return TRUE; } else + { //ensure to keep txn into current account txn->kacc = okacc; + account_balances_add(txn); + } } return FALSE; } @@ -915,14 +1039,14 @@ gboolean match = FALSE; if(text == NULL) return FALSE; - //DB( g_print("search %s in %s\n", rul->name, ope->wording) ); + //DB( g_print("search %s in %s\n", rul->name, ope->memo) ); if( searchtext != NULL ) { if( exact == TRUE ) { if( g_strrstr(text, searchtext) != NULL ) { - DB( g_print(" found case '%s'\n", searchtext) ); + DB( g_print("-- found case '%s'\n", searchtext) ); match = TRUE; } } @@ -933,7 +1057,7 @@ gboolean match = FALSE; if( g_strrstr(word, needle) != NULL ) { - DB( g_print(" found nocase '%s'\n", searchtext) ); + DB( g_print("-- found nocase '%s'\n", searchtext) ); match = TRUE; } g_free(word); @@ -951,32 +1075,28 @@ gboolean match = FALSE; if(text == NULL) return FALSE; - DB( g_print("- match RE %s in %s\n", searchtext, text) ); + DB( g_print("-- match RE %s in %s\n", searchtext, text) ); if( searchtext != NULL ) { match = g_regex_match_simple(searchtext, text, ((exact == TRUE)?0:G_REGEX_CASELESS) | G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTEMPTY ); - if (match == TRUE) { DB( g_print(" found pattern '%s'\n", searchtext) ); } + if (match == TRUE) { DB( g_print("-- found pattern '%s'\n", searchtext) ); } } return match; } -static Assign *transaction_auto_assign_eval_txn(GList *l_rul, Transaction *txn) +static GList *transaction_auto_assign_eval_txn(GList *l_rul, Transaction *txn) { -Assign *rule = NULL; +GList *ret_list = NULL; GList *list; - - DB( g_print("\n[transaction] auto_assign_eval_txn\n") ); - - DB( g_print("- eval every rules, and return the last that match\n") ); +gchar *text; list = g_list_first(l_rul); while (list != NULL) { Assign *rul = list->data; - gchar *text; - text = txn->wording; + text = txn->memo; if(rul->field == 1) //payee { Payee *pay = da_pay_get(txn->kpay); @@ -987,30 +1107,28 @@ GList *list; if( !(rul->flags & ASGF_REGEX) ) { if( misc_text_match(text, rul->text, rul->flags & ASGF_EXACT) ) - rule = rul; + ret_list = g_list_append(ret_list, rul); } else { if( misc_regex_match(text, rul->text, rul->flags & ASGF_EXACT) ) - rule = rul; + ret_list = g_list_append(ret_list, rul); } list = g_list_next(list); } - return rule; + DB( g_print("- evaluated txn '%s'=> %d match\n", text, g_list_length (ret_list)) ); + + return ret_list; } -static Assign *transaction_auto_assign_eval(GList *l_rul, gchar *text) +static GList *transaction_auto_assign_eval(GList *l_rul, gchar *text) { -Assign *rule = NULL; +GList *ret_list = NULL; GList *list; - DB( g_print("\n[transaction] auto_assign_eval\n") ); - - DB( g_print("- eval every rules, and return the last that match\n") ); - list = g_list_first(l_rul); while (list != NULL) { @@ -1021,26 +1139,29 @@ GList *list; if( !(rul->flags & ASGF_REGEX) ) { if( misc_text_match(text, rul->text, rul->flags & ASGF_EXACT) ) - rule = rul; + ret_list = g_list_append(ret_list, rul); } else { if( misc_regex_match(text, rul->text, rul->flags & ASGF_EXACT) ) - rule = rul; + ret_list = g_list_append(ret_list, rul); } } list = g_list_next(list); } - return rule; + DB( g_print("- evaluated split '%s' => %d match\n", text, g_list_length (ret_list)) ); + + return ret_list; } -gint transaction_auto_assign(GList *ope_list, guint32 kacc) +guint transaction_auto_assign(GList *ope_list, guint32 kacc) { GList *l_ope; GList *l_rul; -gint changes = 0; +GList *l_match, *l_tmp; +guint changes = 0; DB( g_print("\n[transaction] auto_assign\n") ); @@ -1052,63 +1173,67 @@ gint changes = 0; Transaction *ope = l_ope->data; gboolean changed = FALSE; - DB( g_print("- eval ope '%s' : acc=%d, pay=%d, cat=%d\n", ope->wording, ope->kacc, ope->kpay, ope->kcat) ); + DB( g_print("\n- work on txn '%s' : acc=%d, pay=%d, cat=%d, %s\n", ope->memo, ope->kacc, ope->kpay, ope->kcat, (ope->flags & OF_SPLIT) ? "is_split" : "" ) ); //#1215521: added kacc == 0 if( (kacc == ope->kacc || kacc == 0) ) { - Assign *rul; - - rul = transaction_auto_assign_eval_txn(l_rul, ope); - if( rul != NULL ) + if( !(ope->flags & OF_SPLIT) ) { - if( (ope->kpay == 0 && (rul->flags & ASGF_DOPAY)) || (rul->flags & ASGF_OVWPAY) ) + l_match = l_tmp = transaction_auto_assign_eval_txn(l_rul, ope); + while( l_tmp != NULL ) { - if(ope->kpay != rul->kpay) { changed = TRUE; } - ope->kpay = rul->kpay; - } + Assign *rul = l_tmp->data; + + if( (ope->kpay == 0 && (rul->flags & ASGF_DOPAY)) || (rul->flags & ASGF_OVWPAY) ) + { + if(ope->kpay != rul->kpay) { changed = TRUE; } + ope->kpay = rul->kpay; + } - if( !(ope->flags & OF_SPLIT) ) - { if( (ope->kcat == 0 && (rul->flags & ASGF_DOCAT)) || (rul->flags & ASGF_OVWCAT) ) { if(ope->kcat != rul->kcat) { changed = TRUE; } ope->kcat = rul->kcat; } - } - if( (ope->paymode == 0 && (rul->flags & ASGF_DOMOD)) || (rul->flags & ASGF_OVWMOD) ) - { - //ugly hack - don't allow modify intxfer - if(ope->paymode != PAYMODE_INTXFER && rul->paymode != PAYMODE_INTXFER) + if( (ope->paymode == 0 && (rul->flags & ASGF_DOMOD)) || (rul->flags & ASGF_OVWMOD) ) { - if(ope->paymode != rul->paymode) { changed = TRUE; } - ope->paymode = rul->paymode; + //ugly hack - don't allow modify intxfer + if(ope->paymode != PAYMODE_INTXFER && rul->paymode != PAYMODE_INTXFER) + { + if(ope->paymode != rul->paymode) { changed = TRUE; } + ope->paymode = rul->paymode; + } } + l_tmp = g_list_next(l_tmp); } - + g_list_free(l_match); } - - if( ope->flags & OF_SPLIT ) + else { - guint i, nbsplit = da_splits_count(ope->splits); - + guint i, nbsplit = da_splits_length(ope->splits); + for(i=0;isplits[i]; + Split *split = da_splits_get(ope->splits, i); DB( g_print("- eval split '%s'\n", split->memo) ); - rul = transaction_auto_assign_eval(l_rul, split->memo); - if( rul != NULL ) + l_match = l_tmp = transaction_auto_assign_eval(l_rul, split->memo); + while( l_tmp != NULL ) { + Assign *rul = l_tmp->data; + //#1501144: check if user wants to set category in rule if( (split->kcat == 0 || (rul->flags & ASGF_OVWCAT)) && (rul->flags & ASGF_DOCAT) ) { if(split->kcat != rul->kcat) { changed = TRUE; } split->kcat = rul->kcat; } - } + l_tmp = g_list_next(l_tmp); + } + g_list_free(l_match); } } @@ -1128,144 +1253,267 @@ gint changes = 0; } -/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */ - -guint -transaction_tags_count(Transaction *ope) +static gboolean transaction_similar_match(Transaction *stxn, Transaction *dtxn, guint32 daygap) { -guint count = 0; -guint32 *ptr = ope->tags; - - //DB( g_print("\n[transaction] tags_count\n") ); - - if( ope->tags == NULL ) - return 0; +gboolean retval = FALSE; - while(*ptr++ != 0 && count < 32) - count++; + if(stxn == dtxn) + return FALSE; - return count; + DB( g_print(" date: %d - %d = %d\n", stxn->date, dtxn->date, stxn->date - dtxn->date) ); + + if( stxn->kcur == dtxn->kcur + && stxn->amount == dtxn->amount + && ( (stxn->date - dtxn->date) <= daygap ) + //todo: at import we also check payee, but maybe too strict here + && (hb_string_compare(stxn->memo, dtxn->memo) == 0) + ) + { + retval = TRUE; + } + return retval; } -void transaction_tags_clone(Transaction *src_txn, Transaction *dst_txn) +void transaction_similar_unmark(Account *acc) { -guint count; +GList *lnk_txn; - dst_txn->tags = NULL; - count = transaction_tags_count(src_txn); - if(count > 0) + lnk_txn = g_queue_peek_tail_link(acc->txn_queue); + while (lnk_txn != NULL) { - //1501962: we must also copy the final 0 - dst_txn->tags = g_memdup(src_txn->tags, (count+1)*sizeof(guint32)); + Transaction *stxn = lnk_txn->data; + stxn->marker = TXN_MARK_NONE; + lnk_txn = g_list_previous(lnk_txn); } } -guint -transaction_tags_parse(Transaction *ope, const gchar *tagstring) + +gint transaction_similar_mark(Account *acc, guint32 daygap) { -gchar **str_array; -guint count, i; -Tag *tag; +GList *lnk_txn, *list2; +gint nball = 0; +gint nbdup = 0; - DB( g_print("\n[transaction] tags_parse\n") ); + //warning the list must be sorted by date then amount + //ideally (easier to parse) we shoudl get a list sorted by amount, then date + DB( g_print("\n[transaction] check duplicate\n") ); - DB( g_print(" - tagstring='%s'\n", tagstring) ); + DB( g_print("\n - account:'%s' gap:%d\n", acc->name, daygap) ); - str_array = g_strsplit (tagstring, " ", 0); - count = g_strv_length( str_array ); + #if MYDEBUG == 1 + GTimer *t = g_timer_new(); + g_print(" - start parse\n"); + #endif - g_free(ope->tags); - ope->tags = NULL; - DB( g_print(" -> reset storage %p\n", ope->tags) ); + /* + llast = g_list_last(old ope list); + DB( g_print("- end last : %f sec\n", g_timer_elapsed(t, NULL)) ); + g_timer_reset(t); + ltxn = llast->data; + g_date_clear(&gd, 1); + g_date_set_julian(&gd, ltxn->date); + g_print(" - last julian=%u %02d-%02d-%04d\n", ltxn->date, g_date_get_day (&gd), g_date_get_month (&gd), g_date_get_year(&gd)); - if( count > 0 ) - { + minjulian = ltxn->date - (366*2); + g_date_clear(&gd, 1); + g_date_set_julian(&gd, minjulian); + g_print(" - min julian=%u %02d-%02d-%04d\n", minjulian, g_date_get_day (&gd), g_date_get_month (&gd), g_date_get_year(&gd)); + */ - ope->tags = g_new0(guint32, count + 1); + transaction_similar_unmark(acc); + + //mark duplicate + lnk_txn = g_queue_peek_tail_link(acc->txn_queue); + while (lnk_txn != NULL) + { + Transaction *stxn = lnk_txn->data; - DB( g_print(" -> storage %p\n", ope->tags) ); + //if(stxn->date < minjulian) + // break; + DB( g_print("------\n eval src: %d, '%s', '%s', %.2f\n", stxn->date, stxn->info, stxn->memo, stxn->amount) ); - for(i=0;idata; - newtag->name = g_strdup(str_array[i]); - da_tag_append(newtag); - tag = da_tag_get_by_name(str_array[i]); - } + DB( g_print(" + with dst: %d, '%s', '%s', %.2f\n", dtxn->date, dtxn->info, dtxn->memo, dtxn->amount) ); - DB( g_print(" -> storing %d=>%s at tags pos %d\n", tag->key, tag->name, i) ); + if( (stxn->date - dtxn->date) > daygap ) + { + DB( g_print(" break %d %d\n", (dtxn->date - daygap) , (stxn->date - daygap)) ); + break; + } + + if( dtxn->marker == TXN_MARK_NONE ) + { + if( transaction_similar_match(stxn, dtxn, daygap) ) + { + stxn->marker = TXN_MARK_DUPSRC; + dtxn->marker = TXN_MARK_DUPDST; + DB( g_print(" = dtxn marker=%d\n", dtxn->marker) ); + nball++; + } + } + else + { + DB( g_print(" already marked %d\n", dtxn->marker) ); + } - ope->tags[i] = tag->key; + + list2 = g_list_previous(list2); } - ope->tags[i] = 0; + + DB( g_print(" = stxn marker=%d\n", stxn->marker) ); + if( stxn->marker == TXN_MARK_DUPSRC ) + nbdup++; + + lnk_txn = g_list_previous(lnk_txn); } - //hex_dump(ope->tags, sizeof(guint32*)*count+1); + DB( g_print(" - end parse : %f sec\n", g_timer_elapsed(t, NULL)) ); + DB( g_timer_destroy (t) ); - g_strfreev (str_array); + DB( g_print(" - found: %d/%d dup\n", nbdup, nball ) ); - return count; + return nbdup; } -gchar * -transaction_tags_tostring(Transaction *ope) + +/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */ +/* = = experimental = = */ +/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */ + + +/* +probably add a structure hosted into a glist here +with kind of problem: duplicate, child xfer, orphan xfer +and collect all that with target txn +*/ + + +/*void future_transaction_test_account(Account *acc) { -guint count, i; -gchar **str_array; -gchar *tagstring; -Tag *tag; +GList *lnk_txn, *list2; +gint nball = 0; +gint nbdup = 0; +gint nbxfer = 0; +GPtrArray *array; - DB( g_print("\n[transaction] tags_tostring\n") ); +//future +gint gapday = 0, i; - DB( g_print(" -> tags at=%p\n", ope->tags) ); + //warning the list must be sorted by date then amount + //ideally (easier to parse) we shoudl get a list sorted by amount, then date - if( ope->tags == NULL ) - { + DB( g_print("\n[transaction] check duplicate\n") ); - return NULL; - } - else - { - count = transaction_tags_count(ope); - DB( g_print(" -> tags at=%p, nbtags=%d\n", ope->tags, count) ); - str_array = g_new0(gchar*, count+1); + DB( g_print("\n - account:'%s'\n", acc->name) ); - DB( g_print(" -> str_array at %p\n", str_array) ); + GTimer *t = g_timer_new(); + g_print(" - start parse\n"); - //hex_dump(ope->tags, sizeof(guint32*)*(count+1)); - for(i=0;idata; + g_date_clear(&gd, 1); + g_date_set_julian(&gd, ltxn->date); + g_print(" - last julian=%u %02d-%02d-%04d\n", ltxn->date, g_date_get_day (&gd), g_date_get_month (&gd), g_date_get_year(&gd)); + + minjulian = ltxn->date - (366*2); + g_date_clear(&gd, 1); + g_date_set_julian(&gd, minjulian); + g_print(" - min julian=%u %02d-%02d-%04d\n", minjulian, g_date_get_day (&gd), g_date_get_month (&gd), g_date_get_year(&gd)); + + array = g_ptr_array_sized_new (25); + + lnk_txn = g_queue_peek_tail_link(acc->txn_queue); + while (lnk_txn != NULL) + { + Transaction *stxn = lnk_txn->data; + + //if(stxn->date < minjulian) + // break; + DB( g_print("------\n eval src: %d, '%s', '%s', %2.f\n", stxn->date, stxn->info, stxn->memo, stxn->amount) ); + + stxn->marker = 0; + list2 = g_list_previous(lnk_txn); + while (list2 != NULL) { - DB( g_print(" -> try to get tag %d\n", ope->tags[i]) ); + Transaction *dtxn = list2->data; + + stxn->marker = 0; + if( (dtxn->date + gapday) < (stxn->date + gapday) ) + break; + + DB( g_print(" + with dst: %d, '%s', '%s', %2.f\n", dtxn->date, dtxn->info, dtxn->memo, dtxn->amount) ); - tag = da_tag_get(ope->tags[i]); - if( tag ) + if( transaction_similar_match(stxn, dtxn, gapday) ) { - DB( g_print(" -> get %s at %d\n", tag->name, i) ); - str_array[i] = tag->name; + g_ptr_array_add (array, stxn); + g_ptr_array_add (array, dtxn); + nbdup++; + DB( g_print(" + dst=1 src=1\n") ); } - else - str_array[i] = NULL; - + nball++; + list2 = g_list_previous(list2); } - tagstring = g_strjoinv(" ", str_array); + lnk_txn = g_list_previous(lnk_txn); + } - g_free (str_array); + DB( g_print(" - end parse : %f sec\n", g_timer_elapsed(t, NULL)) ); + DB( g_timer_destroy (t) ); + for(i=0;ilen;i++) + { + Transaction *txn = g_ptr_array_index(array, i); + txn->marker = 1; } - return tagstring; + g_ptr_array_free(array, TRUE); + + DB( g_print(" - found: %d/%d dup, %d xfer\n", nbdup, nball, nbxfer ) ); + } + + + + +//todo: add a limitation, no need to go through all txn +// 1 year in th past, or abolute number ? +gint future_transaction_test_notification(void) +{ +GList *lst_acc, *lnk_acc; + + DB( g_print("\ntransaction_test_notification\n") ); + + lst_acc = g_hash_table_get_values(GLOBALS->h_acc); + lnk_acc = g_list_first(lst_acc); + while (lnk_acc != NULL) + { + Account *acc = lnk_acc->data; + + transaction_similar_mark(acc); + + lnk_acc = g_list_next(lnk_acc); + } + g_list_free(lst_acc); + + return 0; +} +*/ + + +