Bug #370331: Store the numeric values of SX in their kvp as well, if possible.

For SX whose values do not depend on any variables, we now store
the numeric value as well. This way, we get rid of locale-dependent
parsing errors due to changing decimal separators in changing
locales - at least for SX which do not use variables.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@19550 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Christian Stimming 2010-09-04 21:48:22 +00:00
parent 56623986c7
commit 4ea1ea0208
7 changed files with 94 additions and 12 deletions

View File

@ -947,13 +947,29 @@ _get_template_split_account(const SchedXaction* sx, const Split *template_split,
}
static void
_get_sx_formula_value(const SchedXaction* sx, const Split *template_split, gnc_numeric *numeric, GList **creation_errors, const char *formula_key, GHashTable *variable_bindings)
_get_sx_formula_value(const SchedXaction* sx, const Split *template_split, gnc_numeric *numeric, GList **creation_errors, const char *formula_key, const char* numeric_key, GHashTable *variable_bindings)
{
kvp_frame *split_kvpf;
kvp_value *kvp_val;
char *formula_str, *parseErrorLoc;
split_kvpf = xaccSplitGetSlots(template_split);
/* First look up the gnc_numeric value in the template split */
kvp_val = kvp_frame_get_slot_path(split_kvpf,
GNC_SX_ID,
numeric_key,
NULL);
*numeric = kvp_value_get_numeric(kvp_val);
if ((gnc_numeric_check(*numeric) == GNC_ERROR_OK)
&& !gnc_numeric_zero_p(*numeric))
{
/* Already a valid non-zero result? Then return and don't
* parse the string. Luckily we avoid any locale problems with
* decimal points here! Phew. */
return;
}
kvp_val = kvp_frame_get_slot_path(split_kvpf,
GNC_SX_ID,
formula_key,
@ -995,13 +1011,13 @@ _get_sx_formula_value(const SchedXaction* sx, const Split *template_split, gnc_n
static void
_get_credit_formula_value(GncSxInstance *instance, const Split *template_split, gnc_numeric *credit_num, GList **creation_errors)
{
_get_sx_formula_value(instance->parent->sx, template_split, credit_num, creation_errors, GNC_SX_CREDIT_FORMULA, instance->variable_bindings);
_get_sx_formula_value(instance->parent->sx, template_split, credit_num, creation_errors, GNC_SX_CREDIT_FORMULA, GNC_SX_CREDIT_NUMERIC, instance->variable_bindings);
}
static void
_get_debit_formula_value(GncSxInstance *instance, const Split *template_split, gnc_numeric *debit_num, GList **creation_errors)
{
_get_sx_formula_value(instance->parent->sx, template_split, debit_num, creation_errors, GNC_SX_DEBIT_FORMULA, instance->variable_bindings);
_get_sx_formula_value(instance->parent->sx, template_split, debit_num, creation_errors, GNC_SX_DEBIT_FORMULA, GNC_SX_DEBIT_NUMERIC, instance->variable_bindings);
}
static gboolean
@ -1544,9 +1560,9 @@ create_cashflow_helper(Transaction *template_txn, void *user_data)
gint gncn_error;
/* Credit value */
_get_sx_formula_value(creation_data->sx, template_split, &credit_num, creation_data->creation_errors, GNC_SX_CREDIT_FORMULA, NULL);
_get_sx_formula_value(creation_data->sx, template_split, &credit_num, creation_data->creation_errors, GNC_SX_CREDIT_FORMULA, GNC_SX_CREDIT_NUMERIC, NULL);
/* Debit value */
_get_sx_formula_value(creation_data->sx, template_split, &debit_num, creation_data->creation_errors, GNC_SX_DEBIT_FORMULA, NULL);
_get_sx_formula_value(creation_data->sx, template_split, &debit_num, creation_data->creation_errors, GNC_SX_DEBIT_FORMULA, GNC_SX_DEBIT_NUMERIC, NULL);
/* The resulting cash flow number: debit minus credit,
* multiplied with the count factor. */

View File

@ -44,6 +44,7 @@ struct TTSplitInfo_s
/* FIXME: What about the split's KvpFrame */
char *memo; /* owned by us */
char *credit_formula, *debit_formula; /* owned by us */
gnc_numeric credit_numeric, debit_numeric;
Account *acc;
};
@ -241,11 +242,13 @@ gnc_ttsplitinfo_set_credit_formula_numeric(TTSplitInfo *ttsi, gnc_numeric credit
g_free(ttsi->credit_formula);
ttsi->credit_formula = gnc_numeric_to_string(credit);
ttsi->credit_numeric = credit;
if (ttsi->debit_formula)
{
g_free(ttsi->debit_formula);
ttsi->debit_formula = NULL;
ttsi->debit_numeric = gnc_numeric_zero();
}
}
@ -263,6 +266,7 @@ gnc_ttsplitinfo_set_credit_formula(TTSplitInfo *ttsi, const char *credit_formula
{
g_free(ttsi->debit_formula);
ttsi->debit_formula = NULL;
ttsi->debit_numeric = gnc_numeric_zero();
}
return;
}
@ -282,6 +286,18 @@ gnc_ttsplitinfo_get_debit_formula(TTSplitInfo *ttsi)
return ttsi->debit_formula;
}
gnc_numeric gnc_ttsplitinfo_get_credit_numeric(const TTSplitInfo *ttsi)
{
g_return_val_if_fail(ttsi, gnc_numeric_zero());
return ttsi->credit_numeric;
}
gnc_numeric gnc_ttsplitinfo_get_debit_numeric(const TTSplitInfo *ttsi)
{
g_return_val_if_fail(ttsi, gnc_numeric_zero());
return ttsi->debit_numeric;
}
void
gnc_ttsplitinfo_set_debit_formula_numeric(TTSplitInfo *ttsi, gnc_numeric debit)
{
@ -292,11 +308,13 @@ gnc_ttsplitinfo_set_debit_formula_numeric(TTSplitInfo *ttsi, gnc_numeric debit)
g_free(ttsi->debit_formula);
}
ttsi->debit_formula = gnc_numeric_to_string(debit);
ttsi->debit_numeric = debit;
if (ttsi->credit_formula)
{
g_free(ttsi->credit_formula);
ttsi->credit_formula = NULL;
ttsi->credit_numeric = gnc_numeric_zero();
}
return;
}
@ -315,6 +333,7 @@ gnc_ttsplitinfo_set_debit_formula(TTSplitInfo *ttsi, const char *debit_formula)
{
g_free(ttsi->credit_formula);
ttsi->credit_formula = NULL;
ttsi->credit_numeric = gnc_numeric_zero();
}
return;
}

View File

@ -72,12 +72,14 @@ void gnc_ttsplitinfo_set_credit_formula(TTSplitInfo *split_i,
void gnc_ttsplitinfo_set_credit_formula_numeric(TTSplitInfo *split_i,
gnc_numeric credit_formula);
const char *gnc_ttsplitinfo_get_credit_formula(TTSplitInfo *split_i);
gnc_numeric gnc_ttsplitinfo_get_credit_numeric(const TTSplitInfo *split_i);
void gnc_ttsplitinfo_set_debit_formula(TTSplitInfo *split_i,
const char *debit_formula);
void gnc_ttsplitinfo_set_debit_formula_numeric(TTSplitInfo *split_i,
gnc_numeric debit_formula);
const char *gnc_ttsplitinfo_get_debit_formula(TTSplitInfo *split_i);
gnc_numeric gnc_ttsplitinfo_get_debit_numeric(const TTSplitInfo *split_i);
void gnc_ttsplitinfo_set_account(TTSplitInfo *split_i, Account *acc);
Account *gnc_ttsplitinfo_get_account(TTSplitInfo *split_i);

View File

@ -1061,6 +1061,15 @@ pack_split_info (TTSplitInfo *s_info, Account *parent_acct,
NULL);
kvp_value_delete(tmp_value);
tmp_value
= kvp_value_new_numeric(gnc_ttsplitinfo_get_credit_numeric(s_info));
kvp_frame_set_slot_path(split_frame,
tmp_value,
GNC_SX_ID,
GNC_SX_CREDIT_NUMERIC,
NULL);
kvp_value_delete(tmp_value);
tmp_value
= kvp_value_new_string(gnc_ttsplitinfo_get_debit_formula(s_info));
@ -1072,6 +1081,15 @@ pack_split_info (TTSplitInfo *s_info, Account *parent_acct,
kvp_value_delete(tmp_value);
tmp_value
= kvp_value_new_numeric(gnc_ttsplitinfo_get_debit_numeric(s_info));
kvp_frame_set_slot_path(split_frame,
tmp_value,
GNC_SX_ID,
GNC_SX_DEBIT_NUMERIC,
NULL);
kvp_value_delete(tmp_value);
acc_guid = qof_entity_get_guid(QOF_INSTANCE(gnc_ttsplitinfo_get_account(s_info)));
tmp_value = kvp_value_new_guid(acc_guid);

View File

@ -315,6 +315,8 @@ GList *gnc_sx_get_defer_instances( SchedXaction *sx );
#define GNC_SX_ACCOUNT "account"
#define GNC_SX_CREDIT_FORMULA "credit-formula"
#define GNC_SX_DEBIT_FORMULA "debit-formula"
#define GNC_SX_CREDIT_NUMERIC "credit-numeric"
#define GNC_SX_DEBIT_NUMERIC "debit-numeric"
#define GNC_SX_SHARES "shares"
#define GNC_SX_AMOUNT "amnt"
#define GNC_SX_FROM_SCHED_XACTION "from-sched-xaction"

View File

@ -231,7 +231,6 @@ sxftd_add_template_trans(SXFromTransInfo *sxfti)
Split *sp;
gnc_numeric runningBalance;
gnc_numeric split_value;
const char *tmpStr;
runningBalance = gnc_numeric_zero();
@ -252,16 +251,12 @@ sxftd_add_template_trans(SXFromTransInfo *sxfti)
if (gnc_numeric_positive_p(split_value))
{
tmpStr = xaccPrintAmount( split_value,
gnc_default_print_info(FALSE) );
gnc_ttsplitinfo_set_debit_formula( ttsi, tmpStr );
gnc_ttsplitinfo_set_debit_formula_numeric( ttsi, split_value );
}
else
{
/* Negate the numeric so it prints w/o the sign at the front. */
tmpStr = xaccPrintAmount( gnc_numeric_neg( split_value ),
gnc_default_print_info(FALSE) );
gnc_ttsplitinfo_set_credit_formula( ttsi, tmpStr );
gnc_ttsplitinfo_set_credit_formula_numeric ( ttsi, split_value );
}
/* Copy over per-split account info */

View File

@ -33,6 +33,7 @@
#include "recncell.h"
#include "split-register-model-save.h"
#include "split-register-p.h"
#include "app-utils/gnc-exp-parser.h"
struct sr_save_data
@ -677,6 +678,9 @@ gnc_template_register_save_debcred_cell (BasicCell * cell,
SplitRegister *reg = user_data;
kvp_frame *kvpf;
const char *value;
char *error_loc;
gnc_numeric new_amount;
gboolean parse_result;
g_return_if_fail (gnc_basic_cell_has_name (cell, FDEBT_CELL) ||
gnc_basic_cell_has_name (cell, FCRED_CELL));
@ -696,6 +700,20 @@ gnc_template_register_save_debcred_cell (BasicCell * cell,
GNC_SX_CREDIT_FORMULA,
NULL);
/* If the value can be parsed into a numeric result (without any
* further variable definitions), store that numeric value
* additionally in the kvp. Otherwise store a zero numeric
* there.*/
parse_result = gnc_exp_parser_parse_separate_vars(value, &new_amount, &error_loc, NULL);
if (!parse_result)
{
new_amount = gnc_numeric_zero();
}
kvp_frame_set_slot_path (kvpf, kvp_value_new_numeric (new_amount),
GNC_SX_ID,
GNC_SX_CREDIT_NUMERIC,
NULL);
value = gnc_table_layout_get_cell_value (reg->table->layout, FDEBT_CELL);
kvp_frame_set_slot_path (kvpf,
@ -704,6 +722,18 @@ gnc_template_register_save_debcred_cell (BasicCell * cell,
GNC_SX_DEBIT_FORMULA,
NULL);
/* If the value can be parsed into a numeric result, store that
* numeric value additionally. See above comment.*/
parse_result = gnc_exp_parser_parse_separate_vars(value, &new_amount, &error_loc, NULL);
if (!parse_result)
{
new_amount = gnc_numeric_zero();
}
kvp_frame_set_slot_path (kvpf, kvp_value_new_numeric (new_amount),
GNC_SX_ID,
GNC_SX_DEBIT_NUMERIC,
NULL);
DEBUG ("kvp_frame after: %s\n", kvp_frame_to_string (kvpf));
/* set the amount to an innocuous value */