/* HomeBank -- Free, easy, personal accounting for everyone.
- * Copyright (C) 1995-2014 Maxime DOYEN
+ * Copyright (C) 1995-2019 Maxime DOYEN
*
* This file is part of HomeBank.
*
#include "hb-archive.h"
#include "hb-transaction.h"
+
/****************************************************************************/
/* Debug macros */
/****************************************************************************/
extern struct Preferences *PREFS;
+/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
+
+
+gboolean hbfile_file_isbackup(gchar *filepath)
+{
+gboolean retval = FALSE;
+
+ if( filepath == NULL )
+ return FALSE;
+
+ if( g_str_has_suffix(filepath, "xhb~") || g_str_has_suffix(filepath, "bak") )
+ retval = TRUE;
+
+ return retval;
+}
+
-gboolean hbfile_file_hasbackup(gchar *filepath)
+gboolean hbfile_file_hasrevert(gchar *filepath)
{
gchar *bakfilepath;
- bakfilepath = hb_filename_new_with_extention(GLOBALS->xhb_filepath, "xhb~");
- GLOBALS->xhb_hasbak = g_file_test(bakfilepath, G_FILE_TEST_EXISTS);
+ bakfilepath = hb_filename_new_with_extension(GLOBALS->xhb_filepath, "xhb~");
+ GLOBALS->xhb_hasrevert = g_file_test(bakfilepath, G_FILE_TEST_EXISTS);
g_free(bakfilepath);
//todo check here if need to return something
- return GLOBALS->xhb_hasbak;
+ return GLOBALS->xhb_hasrevert;
}
+//#1750161
+guint64 hbfile_file_get_time_modified(gchar *filepath)
+{
+guint64 retval = 0ULL;
+GFile *gfile;
+GFileInfo *gfileinfo;
+
+ DB( g_print("\n[hbfile] get time modified\n") );
+
+ gfile = g_file_new_for_path(filepath);
+ gfileinfo = g_file_query_info (gfile, G_FILE_ATTRIBUTE_TIME_MODIFIED, 0, NULL, NULL);
+ if( gfileinfo )
+ {
+ retval = g_file_info_get_attribute_uint64 (gfileinfo, G_FILE_ATTRIBUTE_TIME_MODIFIED);
+ DB( g_print("- '%s' last access = %lu\n", filepath, retval) );
+ g_object_unref(gfileinfo);
+ }
+ g_object_unref(gfile);
+
+ return retval;
+}
+
+
+void hbfile_file_default(void)
+{
+ DB( g_print("\n[hbfile] default\n") );
+
+ //todo: maybe translate this also
+ hbfile_change_filepath(g_build_filename(PREFS->path_hbfile, "untitled.xhb", NULL));
+ GLOBALS->hbfile_is_new = TRUE;
+ GLOBALS->hbfile_is_bak = FALSE;
+ GLOBALS->xhb_timemodified = 0ULL;
+
+ DB( g_print("- path_hbfile is '%s'\n", PREFS->path_hbfile) );
+ DB( g_print("- xhb_filepath is '%s'\n", GLOBALS->xhb_filepath) );
+}
-
-
-
}
*/
+void hbfile_replace_basecurrency(Currency4217 *curfmt)
+{
+Currency *item;
+guint32 oldkcur;
+
+ DB( g_print("\n[hbfile] replace base currency\n") );
+
+ oldkcur = GLOBALS->kcur;
+ da_cur_remove(oldkcur);
+ item = currency_add_from_user(curfmt);
+ GLOBALS->kcur = item->key;
+
+ DB( g_print(" %d ==> %d %s\n", oldkcur, GLOBALS->kcur, item->iso_code) );
+}
-/*
void hbfile_change_basecurrency(guint32 key)
{
GList *list;
-
+guint32 oldkcur;
+
+ // set every rate to 0
list = g_hash_table_get_values(GLOBALS->h_cur);
while (list != NULL)
{
Currency *entry = list->data;
if(entry->key != GLOBALS->kcur)
+ {
entry->rate = 0.0;
+ entry->mdate = 0;
+ }
list = g_list_next(list);
}
g_list_free(list);
+ oldkcur = GLOBALS->kcur;
+ GLOBALS->kcur = key;
+
+ // update account with old base currency
+ list = g_hash_table_get_values(GLOBALS->h_acc);
+ while (list != NULL)
+ {
+ Account *acc = list->data;
+
+ if( acc->kcur == oldkcur )
+ acc->kcur = key;
+
+ list = g_list_next(list);
+ }
+ g_list_free(list);
+
GLOBALS->changes_count++;
- GLOBALS->kcur = key;
+}
+
+
+GList *hbfile_transaction_get_all(void)
+{
+GList *lst_acc, *lnk_acc;
+GList *lnk_txn;
+GList *list;
+
+ list = NULL;
+ 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;
+
+ //#1674045 ony rely on nosummary
+ //if( (acc->flags & (AF_CLOSED|AF_NOREPORT)) )
+ if( (acc->flags & (AF_NOREPORT)) )
+ goto next_acc;
+
+ lnk_txn = g_queue_peek_head_link(acc->txn_queue);
+ while (lnk_txn != NULL)
+ {
+ list = g_list_append(list, lnk_txn->data);
+ lnk_txn = g_list_next(lnk_txn);
+ }
+
+ next_acc:
+ lnk_acc = g_list_next(lnk_acc);
+ }
+ g_list_free(lst_acc);
+
+ return da_transaction_sort (list);
}
-*/
-void hbfile_change_owner(gchar *owner)
+static GQueue *hbfile_transaction_get_partial_internal(guint32 minjulian, guint32 maxjulian, gushort exclusionflags)
{
- g_free(GLOBALS->owner);
- GLOBALS->owner = (owner != NULL) ? owner : NULL;
+GList *lst_acc, *lnk_acc;
+GList *lnk_txn;
+GQueue *txn_queue;
+
+ txn_queue = g_queue_new ();
+
+ 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;
+
+ if( (acc->flags & exclusionflags) )
+ goto next_acc;
+
+ lnk_txn = g_queue_peek_tail_link(acc->txn_queue);
+ while (lnk_txn != NULL)
+ {
+ Transaction *txn = lnk_txn->data;
+
+ if( txn->date < minjulian ) //no need to go below mindate
+ break;
+
+ if( !(txn->status == TXN_STATUS_REMIND)
+ && (txn->date >= minjulian)
+ && (txn->date <= maxjulian)
+ )
+ {
+ g_queue_push_head (txn_queue, txn);
+ }
+
+ lnk_txn = g_list_previous(lnk_txn);
+ }
+
+ next_acc:
+ lnk_acc = g_list_next(lnk_acc);
+ }
+ g_list_free(lst_acc);
+
+ return txn_queue;
}
-void hbfile_change_filepath(gchar *filepath)
+GQueue *hbfile_transaction_get_partial(guint32 minjulian, guint32 maxjulian)
{
- g_free(GLOBALS->xhb_filepath);
- GLOBALS->xhb_filepath = (filepath != NULL) ? filepath : NULL;
+ //#1674045 ony rely on nosummary
+ //return hbfile_transaction_get_partial_internal(minjulian, maxjulian, (AF_CLOSED|AF_NOREPORT));
+ return hbfile_transaction_get_partial_internal(minjulian, maxjulian, (AF_NOREPORT));
+}
+
+
+GQueue *hbfile_transaction_get_partial_budget(guint32 minjulian, guint32 maxjulian)
+{
+ //#1674045 ony rely on nosummary
+ //return hbfile_transaction_get_partial_internal(minjulian, maxjulian, (AF_CLOSED|AF_NOREPORT|AF_NOBUDGET));
+ return hbfile_transaction_get_partial_internal(minjulian, maxjulian, (AF_NOREPORT|AF_NOBUDGET));
}
void hbfile_sanity_check(void)
{
+GList *lst_acc, *lnk_acc;
+GList *lnk_txn;
GList *lxxx, *list;
- DB( g_print("\n[hbfile] sanity_check\n") );
-
+ DB( g_print("\n[hbfile] !! sanity_check !! \n") );
- list = g_list_first(GLOBALS->ope_list);
- while (list != NULL)
+ DB( g_print(" - transaction\n") );
+ 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;
+ Account *acc = lnk_acc->data;
- da_transaction_consistency(entry);
- list = g_list_next(list);
+ lnk_txn = g_queue_peek_head_link(acc->txn_queue);
+ while (lnk_txn != NULL)
+ {
+ Transaction *txn = lnk_txn->data;
+
+ da_transaction_consistency(txn);
+ lnk_txn = g_list_next(lnk_txn);
+ }
+ lnk_acc = g_list_next(lnk_acc);
}
+ g_list_free(lst_acc);
+ DB( g_print(" - scheduled/template\n") );
list = g_list_first(GLOBALS->arc_list);
while (list != NULL)
{
}
+ DB( g_print(" - account\n") );
lxxx = list = g_hash_table_get_values(GLOBALS->h_acc);
while (list != NULL)
{
g_list_free(lxxx);
+ DB( g_print(" - payee\n") );
lxxx = list = g_hash_table_get_values(GLOBALS->h_pay);
while (list != NULL)
{
g_list_free(lxxx);
+ DB( g_print(" - category\n") );
lxxx = list = g_hash_table_get_values(GLOBALS->h_cat);
while (list != NULL)
{
void hbfile_anonymize(void)
{
+GList *lst_acc, *lnk_acc;
+GList *lnk_txn;
GList *lxxx, *list;
guint cnt, i;
if(item->key != 0)
{
- g_free(item->name);
- item->name = g_strdup_printf("assign %d", item->key);
+ g_free(item->text);
+ item->text = g_strdup_printf("assign %d", item->key);
GLOBALS->changes_count++;
}
list = g_list_next(list);
{
Archive *item = list->data;
- g_free(item->wording);
- item->wording = g_strdup_printf("archive %d", cnt++);
+ g_free(item->memo);
+ item->memo = g_strdup_printf("archive %d", cnt++);
GLOBALS->changes_count++;
//later split anonymize also
}
//transaction
- 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 *item = list->data;
- Split *split;
+ Account *acc = lnk_acc->data;
- g_free(item->info);
- item->info = NULL;
- g_free(item->wording);
- item->wording = g_strdup_printf("memo %d", item->date);
- GLOBALS->changes_count++;
-
- if(item->flags & OF_SPLIT)
+ lnk_txn = g_queue_peek_head_link(acc->txn_queue);
+ while (lnk_txn != NULL)
{
- for(i=0;i<TXN_MAX_SPLIT;i++)
- {
- split = item->splits[i];
- if( split == NULL ) break;
+ Transaction *item = lnk_txn->data;
+ Split *split;
- if(split->memo != NULL)
- g_free(split->memo);
-
- split->memo = g_strdup_printf("memo %d", i);
- GLOBALS->changes_count++;
- }
-
+ g_free(item->info);
+ item->info = NULL;
+ g_free(item->memo);
+ item->memo = g_strdup_printf("memo %d", item->date);
+ GLOBALS->changes_count++;
+ if(item->flags & OF_SPLIT)
+ {
+ cnt = da_splits_length (item->splits);
+ for(i=0;i<cnt;i++)
+ {
+ split = da_splits_get(item->splits, i);
+ if( split == NULL ) break;
+
+ if(split->memo != NULL)
+ g_free(split->memo);
+
+ split->memo = g_strdup_printf("memo %d", i);
+ GLOBALS->changes_count++;
+ }
+ }
+ lnk_txn = g_list_next(lnk_txn);
}
-
- list = g_list_next(list);
+ lnk_acc = g_list_next(lnk_acc);
}
+ g_list_free(lst_acc);
}
+/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
+
+
+void hbfile_change_owner(gchar *owner)
+{
+ g_free(GLOBALS->owner);
+ GLOBALS->owner = (owner != NULL) ? owner : NULL;
+}
+
+
+void hbfile_change_filepath(gchar *filepath)
+{
+ g_free(GLOBALS->xhb_filepath);
+ GLOBALS->xhb_filepath = (filepath != NULL) ? filepath : NULL;
+}
+
+
void hbfile_cleanup(gboolean file_clear)
{
+Transaction *txn;
+
DB( g_print("\n[hbfile] cleanup\n") );
DB( g_print("- file clear is %d\n", file_clear) );
// Free data storage
- //da_cur_destroy();
- da_acc_destroy();
- da_pay_destroy();
- da_cat_destroy();
- da_tag_destroy();
- da_asg_destroy();
- g_hash_table_destroy(GLOBALS->h_memo);
+ txn = g_trash_stack_pop(&GLOBALS->txn_stk);
+ while( txn != NULL )
+ {
+ da_transaction_free (txn);
+ txn = g_trash_stack_pop(&GLOBALS->txn_stk);
+ }
+
+ da_transaction_destroy();
da_archive_destroy(GLOBALS->arc_list);
- da_transaction_destroy(GLOBALS->ope_list);
+ g_hash_table_destroy(GLOBALS->h_memo);
+ da_asg_destroy();
+ da_tag_destroy();
+ da_cat_destroy();
+ da_pay_destroy();
+ da_acc_destroy();
+ da_cur_destroy();
hbfile_change_owner(NULL);
DB( g_print("- file clear is %d\n", file_clear) );
// Allocate data storage
- //da_cur_new();
+ da_cur_new();
da_acc_new();
da_pay_new();
da_cat_new();
GLOBALS->h_memo = g_hash_table_new_full(g_str_hash, g_str_equal, (GDestroyNotify)g_free, NULL);
GLOBALS->arc_list = NULL;
- GLOBALS->ope_list = NULL;
-
+ GLOBALS->txn_stk = NULL;
if(file_clear == TRUE)
{
- //todo: maybe translate this also
- hbfile_change_filepath(g_build_filename(PREFS->path_hbfile, "untitled.xhb", NULL));
- GLOBALS->hbfile_is_new = TRUE;
-
- DB( g_print("- path_hbfile is '%s'\n", PREFS->path_hbfile) );
- DB( g_print("- xhb_filepath is '%s'\n", GLOBALS->xhb_filepath) );
+ hbfile_file_default();
}
else
{
hbfile_change_owner(g_strdup(_("Unknown")));
- //GLOBALS->kcur = 0;
+ GLOBALS->kcur = 1;
GLOBALS->vehicle_category = 0;
GLOBALS->changes_count = 0;
- GLOBALS->xhb_hasbak = FALSE;
+ GLOBALS->xhb_hasrevert = FALSE;
}