1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2017 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/>.
24 /****************************************************************************/
26 /****************************************************************************/
35 /* our global datas */
36 extern struct HomeBank
*GLOBALS
;
38 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
41 da_pay_free(Payee
*item
)
43 DB( g_print("da_pay_free\n") );
46 DB( g_print(" => %d, %s\n", item
->key
, item
->name
) );
57 DB( g_print("da_pay_malloc\n") );
58 return g_malloc0(sizeof(Payee
));
65 DB( g_print("da_pay_destroy\n") );
66 g_hash_table_destroy(GLOBALS
->h_pay
);
75 DB( g_print("da_pay_new\n") );
76 GLOBALS
->h_pay
= g_hash_table_new_full(g_int_hash
, g_int_equal
, (GDestroyNotify
)g_free
, (GDestroyNotify
)da_pay_free
);
78 // insert our 'no payee'
79 item
= da_pay_malloc();
80 item
->name
= g_strdup("");
85 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
86 static void da_pay_max_key_ghfunc(gpointer key
, Payee
*item
, guint32
*max_key
)
88 *max_key
= MAX(*max_key
, item
->key
);
91 static gboolean
da_pay_name_grfunc(gpointer key
, Payee
*item
, gchar
*name
)
93 if( name
&& item
->name
)
95 if(!strcasecmp(name
, item
->name
))
104 * Return value: the number of elements
109 return g_hash_table_size(GLOBALS
->h_pay
);
114 da_pay_create_none(void)
119 DB( g_print("da_pay_insert none\n") );
121 pay = da_pay_malloc();
122 new_key = g_new0(guint32, 1);
125 pay->name = g_strdup("");
127 DB( g_print(" -> insert id: %d\n", *new_key) );
129 g_hash_table_insert(GLOBALS->h_pay, new_key, pay);
140 * delete an payee from the GHashTable
142 * Return value: TRUE if the key was found and deleted
146 da_pay_remove(guint32 key
)
148 DB( g_print("da_pay_remove %d\n", key
) );
150 return g_hash_table_remove(GLOBALS
->h_pay
, &key
);
156 * insert an payee into the GHashTable
158 * Return value: TRUE if inserted
162 da_pay_insert(Payee
*item
)
166 DB( g_print("da_pay_insert\n") );
168 new_key
= g_new0(guint32
, 1);
169 *new_key
= item
->key
;
170 g_hash_table_insert(GLOBALS
->h_pay
, new_key
, item
);
179 * append a new payee into the GHashTable
181 * Return value: TRUE if inserted
185 da_pay_append(Payee
*item
)
190 DB( g_print("da_pay_append\n") );
192 /* ensure no duplicate */
193 //g_strstrip(item->name);
194 if( item
->name
!= NULL
)
196 existitem
= da_pay_get_by_name( item
->name
);
197 if( existitem
== NULL
)
199 new_key
= g_new0(guint32
, 1);
200 *new_key
= da_pay_get_max_key() + 1;
201 item
->key
= *new_key
;
203 DB( g_print(" -> append id: %d\n", *new_key
) );
205 g_hash_table_insert(GLOBALS
->h_pay
, new_key
, item
);
210 DB( g_print(" -> %s already exist: %d\n", item
->name
, item
->key
) );
216 * da_pay_get_max_key:
218 * Get the biggest key from the GHashTable
220 * Return value: the biggest key value
224 da_pay_get_max_key(void)
228 g_hash_table_foreach(GLOBALS
->h_pay
, (GHFunc
)da_pay_max_key_ghfunc
, &max_key
);
236 * da_pay_get_by_name:
238 * Get an payee structure by its name
240 * Return value: Payee * or NULL if not found
244 da_pay_get_by_name(gchar
*name
)
246 DB( g_print("da_pay_get_by_name\n") );
248 return g_hash_table_find(GLOBALS
->h_pay
, (GHRFunc
)da_pay_name_grfunc
, name
);
256 * Get an payee structure by key
258 * Return value: Payee * or NULL if not found
262 da_pay_get(guint32 key
)
264 //DB( g_print("da_pay_get\n") );
266 return g_hash_table_lookup(GLOBALS
->h_pay
, &key
);
270 void da_pay_consistency(Payee
*item
)
272 g_strstrip(item
->name
);
276 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
281 da_pay_debug_list_ghfunc(gpointer key
, gpointer value
, gpointer user_data
)
286 DB( g_print(" %d :: %s\n", *id
, item
->name
) );
291 da_pay_debug_list(void)
294 DB( g_print("\n** debug **\n") );
296 g_hash_table_foreach(GLOBALS
->h_pay
, da_pay_debug_list_ghfunc
, NULL
);
298 DB( g_print("\n** end debug **\n") );
305 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
308 payee_delete_unused(void)
312 lpay
= list
= g_hash_table_get_values(GLOBALS
->h_pay
);
315 Payee
*entry
= list
->data
;
317 if(entry
->usage_count
<= 0 && entry
->key
> 0)
318 da_pay_remove (entry
->key
);
319 list
= g_list_next(list
);
326 payee_fill_usage(void)
329 GList
*lst_acc
, *lnk_acc
;
333 lpay
= list
= g_hash_table_get_values(GLOBALS
->h_pay
);
336 Payee
*entry
= list
->data
;
337 entry
->usage_count
= 0;
338 list
= g_list_next(list
);
343 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
344 lnk_acc
= g_list_first(lst_acc
);
345 while (lnk_acc
!= NULL
)
347 Account
*acc
= lnk_acc
->data
;
349 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
350 while (lnk_txn
!= NULL
)
352 Transaction
*txn
= lnk_txn
->data
;
353 Payee
*pay
= da_pay_get (txn
->kpay
);
358 lnk_txn
= g_list_next(lnk_txn
);
361 lnk_acc
= g_list_next(lnk_acc
);
363 g_list_free(lst_acc
);
366 list
= g_list_first(GLOBALS
->arc_list
);
369 Archive
*entry
= list
->data
;
370 Payee
*pay
= da_pay_get (entry
->kpay
);
374 list
= g_list_next(list
);
377 lrul
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
380 Assign
*entry
= list
->data
;
381 Payee
*pay
= da_pay_get (entry
->kpay
);
386 list
= g_list_next(list
);
394 payee_move(guint32 key1
, guint32 key2
)
396 GList
*lst_acc
, *lnk_acc
;
400 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
401 lnk_acc
= g_list_first(lst_acc
);
402 while (lnk_acc
!= NULL
)
404 Account
*acc
= lnk_acc
->data
;
406 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
407 while (lnk_txn
!= NULL
)
409 Transaction
*txn
= lnk_txn
->data
;
411 if(txn
->kpay
== key1
)
414 txn
->flags
|= OF_CHANGED
;
416 lnk_txn
= g_list_next(lnk_txn
);
418 lnk_acc
= g_list_next(lnk_acc
);
420 g_list_free(lst_acc
);
423 list
= g_list_first(GLOBALS
->arc_list
);
426 Archive
*entry
= list
->data
;
427 if(entry
->kpay
== key1
)
431 list
= g_list_next(list
);
434 lrul
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
437 Assign
*entry
= list
->data
;
439 if(entry
->kpay
== key1
)
443 list
= g_list_next(list
);
450 payee_rename(Payee
*item
, const gchar
*newname
)
455 stripname
= g_strdup(newname
);
456 g_strstrip(stripname
);
458 existitem
= da_pay_get_by_name(stripname
);
460 if( existitem
!= NULL
)
462 if( existitem
->key
== item
->key
)
468 item
->name
= g_strdup(stripname
);
479 * payee_append_if_new:
481 * append a new payee into the GHashTable
483 * Return value: true/false + new payee
487 payee_append_if_new(gchar
*name
, Payee
**newpayee
)
489 gboolean retval
= FALSE
;
490 gchar
*stripname
= g_strdup(name
);
493 g_strstrip(stripname
);
494 item
= da_pay_get_by_name(stripname
);
497 item
= da_pay_malloc();
498 item
->name
= g_strdup(stripname
);
502 if( newpayee
!= NULL
)
511 payee_glist_name_compare_func(Payee
*a
, Payee
*b
)
513 return hb_string_utf8_compare(a
->name
, b
->name
);
518 payee_glist_key_compare_func(Payee
*a
, Payee
*b
)
520 return a
->key
- b
->key
;
524 GList
*payee_glist_sorted(gint column
)
526 GList
*list
= g_hash_table_get_values(GLOBALS
->h_pay
);
529 return g_list_sort(list
, (GCompareFunc
)payee_glist_key_compare_func
);
531 return g_list_sort(list
, (GCompareFunc
)payee_glist_name_compare_func
);
537 payee_load_csv(gchar
*filename
, gchar
**error
)
544 const gchar
*encoding
;
547 encoding
= homebank_file_getencoding(filename
);
551 io
= g_io_channel_new_file(filename
, "r", NULL
);
554 DB( g_print(" -> encoding should be %s\n", encoding
) );
555 if( encoding
!= NULL
)
557 g_io_channel_set_encoding(io
, encoding
, NULL
);
562 io_stat
= g_io_channel_read_line(io
, &tmpstr
, NULL
, NULL
, NULL
);
563 if( io_stat
== G_IO_STATUS_EOF
)
565 if( io_stat
== G_IO_STATUS_NORMAL
)
569 DB( g_print("\n + strip\n") );
570 hb_string_strip_crlf(tmpstr
);
572 DB( g_print(" + split '%s'\n", tmpstr
) );
573 str_array
= g_strsplit (tmpstr
, ";", 2);
574 // payee;category : later paymode?
576 nbcol
= g_strv_length (str_array
);
579 *error
= _("invalid CSV format");
581 DB( g_print(" + error %s\n", *error
) );
590 DB( g_print(" add pay:'%s' ?\n", str_array
[0]) );
591 added
= payee_append_if_new(str_array
[0], &pay
);
594 DB( g_print(" added: %p\n", pay
) );
595 GLOBALS
->changes_count
++;
603 DB( g_print(" add cat:'%s'\n", str_array
[1]) );
604 cat
= da_cat_append_ifnew_by_fullname(str_array
[1], FALSE
);
606 DB( g_print(" cat: %p %p\n", cat
, pay
) );
611 DB( g_print(" set default cat to %d\n", cat
->key
) );
612 pay
->kcat
= cat
->key
;
614 GLOBALS
->changes_count
++;
618 g_strfreev (str_array
);
624 g_io_channel_unref (io
);
632 payee_save_csv(gchar
*filename
)
638 io
= g_io_channel_new_file(filename
, "w", NULL
);
641 lpay
= list
= payee_glist_sorted(1);
645 Payee
*item
= list
->data
;
653 Category
*cat
= da_cat_get(item
->kcat
);
657 fullcatname
= da_cat_get_fullname (cat
);
661 if( fullcatname
!= NULL
)
662 outstr
= g_strdup_printf("%s;%s\n", item
->name
, fullcatname
);
664 outstr
= g_strdup_printf("%s;\n", item
->name
);
666 DB( g_print(" + export %s\n", outstr
) );
668 g_io_channel_write_chars(io
, outstr
, -1, NULL
, NULL
);
674 list
= g_list_next(list
);
678 g_io_channel_unref (io
);