X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fhomebank;a=blobdiff_plain;f=src%2Fhb-payee.c;h=896e9360057443c46d40eafbcde67b092c5014cd;hp=4a99bb33f2d0136a412e2de00cebc13f3501ca58;hb=cd13d9691c46c2b2d6d459e9e6a76bed1c21b7a6;hpb=27f6e3b112df235c8e9afc9911b1f6bce208a001 diff --git a/src/hb-payee.c b/src/hb-payee.c index 4a99bb3..896e936 100644 --- a/src/hb-payee.c +++ b/src/hb-payee.c @@ -1,5 +1,5 @@ /* HomeBank -- Free, easy, personal accounting for everyone. - * Copyright (C) 1995-2014 Maxime DOYEN + * Copyright (C) 1995-2018 Maxime DOYEN * * This file is part of HomeBank. * @@ -137,9 +137,9 @@ guint32 *new_key; /** * da_pay_remove: * - * remove an payee from the GHashTable + * delete an payee from the GHashTable * - * Return value: TRUE if the key was found and removed + * Return value: TRUE if the key was found and deleted * */ gboolean @@ -302,33 +302,75 @@ da_pay_debug_list(void) #endif - - /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */ +void +payee_delete_unused(void) +{ +GList *lpay, *list; + + lpay = list = g_hash_table_get_values(GLOBALS->h_pay); + while (list != NULL) + { + Payee *entry = list->data; + if(entry->usage_count <= 0 && entry->key > 0) + da_pay_remove (entry->key); + list = g_list_next(list); + } + g_list_free(lpay); +} -gboolean -payee_is_used(guint32 key) +void +payee_fill_usage(void) { +GList *lpay; +GList *lst_acc, *lnk_acc; +GList *lnk_txn; GList *lrul, *list; - list = g_list_first(GLOBALS->ope_list); + lpay = list = g_hash_table_get_values(GLOBALS->h_pay); while (list != NULL) { - Transaction *entry = list->data; - if( key == entry->kpay ) - return TRUE; + Payee *entry = list->data; + entry->usage_count = 0; list = g_list_next(list); } + g_list_free(lpay); + + + 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; + + lnk_txn = g_queue_peek_head_link(acc->txn_queue); + while (lnk_txn != NULL) + { + Transaction *txn = lnk_txn->data; + Payee *pay = da_pay_get (txn->kpay); + + if(pay) + pay->usage_count++; + + lnk_txn = g_list_next(lnk_txn); + } + + lnk_acc = g_list_next(lnk_acc); + } + g_list_free(lst_acc); + list = g_list_first(GLOBALS->arc_list); while (list != NULL) { Archive *entry = list->data; - if( key == entry->kpay ) - return TRUE; + Payee *pay = da_pay_get (entry->kpay); + + if(pay) + pay->usage_count++; list = g_list_next(list); } @@ -336,33 +378,47 @@ GList *lrul, *list; while (list != NULL) { Assign *entry = list->data; + Payee *pay = da_pay_get (entry->kpay); + + if(pay) + pay->usage_count++; - if( key == entry->kpay) - return TRUE; list = g_list_next(list); } g_list_free(lrul); - - return FALSE; } + void payee_move(guint32 key1, guint32 key2) { +GList *lst_acc, *lnk_acc; +GList *lnk_txn; GList *lrul, *list; - list = g_list_first(GLOBALS->ope_list); - while (list != NULL) + lst_acc = g_hash_table_get_values(GLOBALS->h_acc); + lnk_acc = g_list_first(lst_acc); + while (lnk_acc != NULL) { - Transaction *entry = list->data; - if(entry->kpay == key1) + Account *acc = lnk_acc->data; + + lnk_txn = g_queue_peek_head_link(acc->txn_queue); + while (lnk_txn != NULL) { - entry->kpay = key2; - entry->flags |= OF_CHANGED; + Transaction *txn = lnk_txn->data; + + if(txn->kpay == key1) + { + txn->kpay = key2; + txn->flags |= OF_CHANGED; + } + lnk_txn = g_list_next(lnk_txn); } - list = g_list_next(list); + lnk_acc = g_list_next(lnk_acc); } + g_list_free(lst_acc); + list = g_list_first(GLOBALS->arc_list); while (list != NULL) @@ -424,31 +480,31 @@ gchar *stripname; * * append a new payee into the GHashTable * - * Return value: a new Payee or NULL + * Return value: true/false + new payee * */ -Payee * -payee_append_if_new(gchar *name) +gboolean +payee_append_if_new(gchar *name, Payee **newpayee) { -gchar *stripname; +gboolean retval = FALSE; +gchar *stripname = g_strdup(name); Payee *item; - stripname = g_strdup(name); g_strstrip(stripname); item = da_pay_get_by_name(stripname); - if(item == NULL) { item = da_pay_malloc(); item->name = g_strdup(stripname); da_pay_append(item); + retval = TRUE; } - else - item = NULL; + if( newpayee != NULL ) + *newpayee = item; g_free(stripname); - return item; + return retval; } static gint @@ -477,16 +533,21 @@ GList *list = g_hash_table_get_values(GLOBALS->h_pay); -void -payee_load_csv(gchar *filename) +gboolean +payee_load_csv(gchar *filename, gchar **error) { +gboolean retval; GIOChannel *io; gchar *tmpstr; gint io_stat; +gchar **str_array; const gchar *encoding; +gint nbcol; encoding = homebank_file_getencoding(filename); + retval = TRUE; + *error = NULL; io = g_io_channel_new_file(filename, "r", NULL); if(io != NULL) { @@ -505,15 +566,56 @@ const gchar *encoding; { if( tmpstr != NULL) { + DB( g_print("\n + strip\n") ); hb_string_strip_crlf(tmpstr); - DB( g_print(" read %s\n", tmpstr) ); + DB( g_print(" + split '%s'\n", tmpstr) ); + str_array = g_strsplit (tmpstr, ";", 2); + // payee;category : later paymode? - if( payee_append_if_new( tmpstr ) ) + nbcol = g_strv_length (str_array); + if( nbcol > 2 ) { - GLOBALS->changes_count++; + *error = _("invalid CSV format"); + retval = FALSE; + DB( g_print(" + error %s\n", *error) ); } - + else + { + gboolean added; + Payee *pay = NULL; + + if( nbcol >= 1 ) + { + DB( g_print(" add pay:'%s' ?\n", str_array[0]) ); + added = payee_append_if_new(str_array[0], &pay); + if( added ) + { + DB( g_print(" added: %p\n", pay) ); + GLOBALS->changes_count++; + } + } + + if( nbcol == 2 ) + { + Category *cat; + + DB( g_print(" add cat:'%s'\n", str_array[1]) ); + cat = da_cat_append_ifnew_by_fullname(str_array[1], FALSE); + + DB( g_print(" cat: %p %p\n", cat, pay) ); + if( cat != NULL ) + { + if( pay != NULL) + { + DB( g_print(" set default cat to %d\n", cat->key) ); + pay->kcat = cat->key; + } + GLOBALS->changes_count++; + } + } + } + g_strfreev (str_array); } g_free(tmpstr); } @@ -522,6 +624,7 @@ const gchar *encoding; g_io_channel_unref (io); } + return retval; } @@ -540,15 +643,33 @@ gchar *outstr; while (list != NULL) { Payee *item = list->data; + gchar *fullcatname; if(item->key != 0) { - outstr = g_strdup_printf("%s\n", item->name); - g_io_channel_write_chars(io, outstr, -1, NULL, NULL); + fullcatname = NULL; + if( item->kcat > 0 ) + { + Category *cat = da_cat_get(item->kcat); + + if( cat != NULL ) + { + fullcatname = da_cat_get_fullname (cat); + } + } - DB( g_print("%s", outstr) ); + if( fullcatname != NULL ) + outstr = g_strdup_printf("%s;%s\n", item->name, fullcatname); + else + outstr = g_strdup_printf("%s;\n", item->name); + + DB( g_print(" + export %s\n", outstr) ); + + g_io_channel_write_chars(io, outstr, -1, NULL, NULL); g_free(outstr); + g_free(fullcatname); + } list = g_list_next(list); }