r22736 - gnucash/trunk/src/register/ledger-core - Do a better job of calculating debit and credit fractions.

Mike Alexander mta at code.gnucash.org
Wed Jan 30 00:22:46 EST 2013


Author: mta
Date: 2013-01-30 00:22:46 -0500 (Wed, 30 Jan 2013)
New Revision: 22736
Trac: http://svn.gnucash.org/trac/changeset/22736

Modified:
   gnucash/trunk/src/register/ledger-core/split-register-control.c
   gnucash/trunk/src/register/ledger-core/split-register-util.c
Log:
Do a better job of calculating debit and credit fractions.
Make gnc_split_register_set_cell_fractions agree with
gnc_split_register_get_debcred_entry.
If the account for a split changes recalculate the fractions.

Modified: gnucash/trunk/src/register/ledger-core/split-register-control.c
===================================================================
--- gnucash/trunk/src/register/ledger-core/split-register-control.c	2013-01-30 05:22:38 UTC (rev 22735)
+++ gnucash/trunk/src/register/ledger-core/split-register-control.c	2013-01-30 05:22:46 UTC (rev 22736)
@@ -262,6 +262,7 @@
     SRInfo *info;
     ComboCell *cell = NULL;
     Account* new_acct;
+    Split *split;
     char *name;
 
     g_return_val_if_fail(reg, TRUE);
@@ -300,11 +301,13 @@
                cell->cell.value);
     if (!new_acct)
         return FALSE;
+    
+    split = gnc_split_register_get_current_split(reg);
+    gnc_split_register_set_cell_fractions (reg, split);
 
     /* See if we need to reset the exchange rate. */
     if (gnc_split_reg_has_rate_cell(reg->type))
     {
-        Split         *split     = gnc_split_register_get_current_split(reg);
         PriceCell     *rate_cell = (PriceCell *) gnc_table_layout_get_cell (reg->table->layout,
                                                                             RATE_CELL);
         Account       *orig_acct = xaccSplitGetAccount(split);

Modified: gnucash/trunk/src/register/ledger-core/split-register-util.c
===================================================================
--- gnucash/trunk/src/register/ledger-core/split-register-util.c	2013-01-30 05:22:38 UTC (rev 22735)
+++ gnucash/trunk/src/register/ledger-core/split-register-util.c	2013-01-30 05:22:46 UTC (rev 22736)
@@ -338,31 +338,79 @@
 void
 gnc_split_register_set_cell_fractions (SplitRegister *reg, Split *split)
 {
-    Account *account;
+    Account *split_account;
+    Account *reg_account;
     Transaction *trans;
-    gnc_commodity *currency;
+    gnc_commodity *trans_currency;  /* or default currency if no transaction */
     PriceCell *cell;
     int fraction;
+    gboolean trading_accts;
+    gnc_commodity *commodity;
+    
+    /* This function must use the same algorithm as gnc_split_register_get_shares_entry
+       and gnc_split_register_get_debcred_entry.  Changes here may require changes in
+       one of them or vice versa. */
 
+    /* If the split has a new account use that, otherwise use the one it
+       had before we started editing it. */
+    split_account = gnc_split_register_get_account (reg, XFRM_CELL);
+    if (!split_account)
+        split_account = xaccSplitGetAccount (split);
+
+    reg_account = gnc_split_register_get_default_account (reg);
+
     trans = xaccSplitGetParent (split);
-    if (gnc_split_register_current_trans_expanded (reg) &&
-        xaccTransUseTradingAccounts (trans))
+    if (trans)
     {
-        /* If the transaction is expanded and using trading accounts then
-         * the debit and credit fields are in the split's commodity not
-         * the transaction's currency
-         */
-        currency = xaccAccountGetCommodity (xaccSplitGetAccount (split));
+        trading_accts = xaccTransUseTradingAccounts (trans);
+        trans_currency = xaccTransGetCurrency (trans);
     }
     else
     {
-        currency = xaccTransGetCurrency (trans);
+        /* It should be ok to use the current book since that's what 
+           gnc_split_register_get_account uses to find the account. */
+        trading_accts = qof_book_use_trading_accounts (gnc_get_current_book());
+        trans_currency = gnc_default_currency();
     }
-    if (!currency)
-        currency = gnc_default_currency ();
 
-    fraction = gnc_commodity_get_fraction (currency);
+    /* What follows is similar to the tests in gnc_split_register_get_debcred_entry */
+    if (trading_accts)
+    {
+        if (reg->type == STOCK_REGISTER ||
+            reg->type == CURRENCY_REGISTER ||
+            reg->type == PORTFOLIO_LEDGER)
+        {
+            /* These tests are similar to gnc_split_register_use_security_cells */
+            if (!split_account)
+                commodity = trans_currency;
+            else if (trading_accts &&
+                     !gnc_commodity_is_iso (xaccAccountGetCommodity(split_account)))
+                commodity = trans_currency;
+            else if (xaccAccountIsPriced (split_account))
+                commodity = trans_currency;
+            else
+                commodity = xaccAccountGetCommodity (split_account);
+        }
+        else
+        {
+            commodity = xaccAccountGetCommodity (split_account);
+        }
+    }
+    else
+    {
+        /* Not trading accounts */
+        if (reg->type == STOCK_REGISTER ||
+            reg->type == CURRENCY_REGISTER ||
+            reg->type == PORTFOLIO_LEDGER)
+            commodity = trans_currency;
+        else
+            commodity = xaccAccountGetCommodity (reg_account);
+    }
+    if (!commodity)
+        commodity = gnc_default_currency ();
 
+    fraction = gnc_commodity_get_fraction (commodity);
+
     cell = (PriceCell *) gnc_table_layout_get_cell (reg->table->layout,
             DEBT_CELL);
     gnc_price_cell_set_fraction (cell, fraction);
@@ -371,13 +419,12 @@
             CRED_CELL);
     gnc_price_cell_set_fraction (cell, fraction);
 
-    account = xaccSplitGetAccount (split);
-
     cell = (PriceCell *) gnc_table_layout_get_cell (reg->table->layout,
             SHRS_CELL);
 
-    if (account)
-        gnc_price_cell_set_fraction (cell, xaccAccountGetCommoditySCU (account));
+    /* gnc_split_register_get_shares_entry always uses the split's commodity */
+    if (split_account)
+        gnc_price_cell_set_fraction (cell, xaccAccountGetCommoditySCU (split_account));
     else
         gnc_price_cell_set_fraction (cell, 1000000);
 }



More information about the gnucash-changes mailing list