Skip to content

Commit

Permalink
Bug 680104 - formula not calculated when variables are mixed with con…
Browse files Browse the repository at this point in the history
…stants

Fixes an error from 4ea1ea0 that assumed incorrectly that
gnc_exp_parser_parse_separate_variables() returns false if there are no variables
found. It doesn't.
  • Loading branch information
jralls committed Jan 9, 2016
1 parent a112667 commit 7a25e2a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
26 changes: 17 additions & 9 deletions src/app-utils/gnc-sx-instance-model.c
Expand Up @@ -283,8 +283,7 @@ gnc_sx_get_template_transaction_account(const SchedXaction *sx)
void
gnc_sx_get_variables(SchedXaction *sx, GHashTable *var_hash)
{
Account *sx_template_acct;
sx_template_acct = gnc_sx_get_template_transaction_account(sx);
Account *sx_template_acct = gnc_sx_get_template_transaction_account(sx);
xaccAccountForEachTransaction(sx_template_acct, _get_vars_helper, var_hash);
}

Expand Down Expand Up @@ -988,12 +987,15 @@ _get_sx_formula_value(const SchedXaction* sx, const Split *template_split, gnc_n
numeric_key,
NULL);
*numeric = kvp_value_get_numeric(kvp_val);
if ((gnc_numeric_check(*numeric) == GNC_ERROR_OK)
&& !gnc_numeric_zero_p(*numeric))
if ((variable_bindings == NULL ||
g_hash_table_size(variable_bindings) == 0) &&
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. */
/* If there are no variables to parse and we had a valid numeric stored
* then we can skip parsing the formula, which might save some
* localization problems with separators.
*/
return;
}

Expand Down Expand Up @@ -1672,9 +1674,15 @@ 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, GNC_SX_CREDIT_NUMERIC, 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, GNC_SX_DEBIT_NUMERIC, 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. */
Expand Down
49 changes: 33 additions & 16 deletions src/register/ledger-core/split-register-model-save.c
Expand Up @@ -705,6 +705,7 @@ gnc_template_register_save_debcred_cell (BasicCell * cell,
char *error_loc;
gnc_numeric new_amount;
gboolean parse_result;
GHashTable *parser_vars = g_hash_table_new(g_str_hash, g_str_equal);

g_return_if_fail (gnc_basic_cell_has_name (cell, FDEBT_CELL) ||
gnc_basic_cell_has_name (cell, FCRED_CELL));
Expand All @@ -728,16 +729,24 @@ gnc_template_register_save_debcred_cell (BasicCell * cell,
* 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)
parse_result = gnc_exp_parser_parse_separate_vars(value, &new_amount,
&error_loc, parser_vars);
if (g_hash_table_size(parser_vars) == 0)
{
new_amount = gnc_numeric_zero();
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);
}
else
{
g_hash_table_destroy(parser_vars);
parser_vars = g_hash_table_new (g_str_hash, g_str_equal);
}
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,
Expand All @@ -748,16 +757,24 @@ gnc_template_register_save_debcred_cell (BasicCell * cell,

/* 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)
parse_result = gnc_exp_parser_parse_separate_vars(value, &new_amount,
&error_loc, parser_vars);
if (parser_vars == NULL)
{
new_amount = gnc_numeric_zero();
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);
}
else
{
g_hash_table_destroy(parser_vars);
parser_vars = NULL;
}
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 */
Expand Down

0 comments on commit 7a25e2a

Please sign in to comment.