gnucash master: Book-Currency Feature step 2

J. Alex Aycinena alex.aycinena at code.gnucash.org
Sat Feb 28 20:04:19 EST 2015


Updated	 via  https://github.com/Gnucash/gnucash/commit/906ed1fe (commit)
	from  https://github.com/Gnucash/gnucash/commit/e814221e (commit)



commit 906ed1fe26c86a921ed6e9afc098de918d35edb3
Author: Alex Aycinena <alex.aycinena at gmail.com>
Date:   Sat Feb 28 16:39:34 2015 -0800

    Book-Currency Feature step 2
    
    Store the book-currency in the book KVP. The changes made are:
    
         libqof/qof/qofbookslots.h - define a Book Currency option in addition to
             the Currency Accounting Method option previously defined
         libqof/qof/qofbook.cpp & .h - define function to get book-currency and add
             gobject properties accordingly
         libqof/qof/test/test-qofbook.c - add to test function for book-currency
             accounting method to also test for a specified book-currency

diff --git a/src/libqof/qof/qofbook.cpp b/src/libqof/qof/qofbook.cpp
index d0c7d1a..bf8c867 100644
--- a/src/libqof/qof/qofbook.cpp
+++ b/src/libqof/qof/qofbook.cpp
@@ -71,6 +71,9 @@ enum
      to use currency accounting property as of 2.7 */
     PROP_OPT_TRADING_ACCOUNTS,	/* KVP */
     PROP_OPT_CURRENCY_ACCOUNTING,	/* KVP */
+/*   Book currency property only applies if currency accounting method is
+     set to 'book-currency' */
+    PROP_OPT_BOOK_CURRENCY, 	/* KVP */
     PROP_OPT_AUTO_READONLY_DAYS,/* KVP */
     PROP_OPT_NUM_FIELD_SOURCE,	/* KVP */
     PROP_OPT_DEFAULT_BUDGET,	/* KVP */
@@ -149,6 +152,13 @@ qof_book_get_property (GObject* object,
 	qof_instance_get_kvp (QOF_INSTANCE (book), key, value);
 	g_free (key);
 	break;
+    case PROP_OPT_BOOK_CURRENCY:
+	key = g_strdup_printf ("%s/%s/%s", KVP_OPTION_PATH,
+			       OPTION_SECTION_ACCOUNTS,
+                   OPTION_NAME_BOOK_CURRENCY);
+	qof_instance_get_kvp (QOF_INSTANCE (book), key, value);
+	g_free (key);
+	break;
     case PROP_OPT_AUTO_READONLY_DAYS:
 	key = g_strdup_printf ("%s/%s/%s", KVP_OPTION_PATH,
 			       OPTION_SECTION_ACCOUNTS,
@@ -212,6 +222,13 @@ qof_book_set_property (GObject      *object,
 	qof_instance_set_kvp (QOF_INSTANCE (book), key, value);
 	g_free (key);
 	break;
+    case PROP_OPT_BOOK_CURRENCY:
+	key = g_strdup_printf ("%s/%s/%s", KVP_OPTION_PATH,
+			       OPTION_SECTION_ACCOUNTS,
+                   OPTION_NAME_BOOK_CURRENCY);
+	qof_instance_set_kvp (QOF_INSTANCE (book), key, value);
+	g_free (key);
+	break;
     case PROP_OPT_AUTO_READONLY_DAYS:
 	key = g_strdup_printf ("%s/%s/%s", KVP_OPTION_PATH,
 			       OPTION_SECTION_ACCOUNTS,
@@ -285,6 +302,17 @@ qof_book_class_init (QofBookClass *klass)
 
     g_object_class_install_property
     (gobject_class,
+     PROP_OPT_BOOK_CURRENCY,
+     g_param_spec_string("book-currency",
+                         "Select Book Currency",
+			 "The reference currency used to manage multiple-currency "
+             "transactions when 'book-currency' currency accounting method "
+             "selected.",
+                         NULL,
+                         G_PARAM_READWRITE));
+
+    g_object_class_install_property
+    (gobject_class,
      PROP_OPT_NUM_FIELD_SOURCE,
      g_param_spec_string("split-action-num-field",
                          "Use Split-Action in the Num Field",
@@ -897,14 +925,83 @@ qof_book_currency_accounting_method (const QofBook *book)
                                      NULL);
     if (value)
     {
-        if (strcmp (kvp_value_get_string (value), "book-currency") == 0)
-           return CURRENCY_ACCOUNTING_BOOK_CURRENCY;
         if (strcmp (kvp_value_get_string (value), "trading") == 0)
            return CURRENCY_ACCOUNTING_TRADING;
+        if (strcmp (kvp_value_get_string (value), "book-currency") == 0)
+        /* Must have a specified book-currency for 'book-currency' Currency
+           Accounting Method to be valid */
+        {
+            value = kvp_frame_get_slot_path (kvp,
+                                             KVP_OPTION_PATH,
+                                             OPTION_SECTION_ACCOUNTS,
+                                             OPTION_NAME_BOOK_CURRENCY,
+                                             NULL);
+            if (!value)
+            /* Book-currency currency accounting method selected but no
+               book-currency specified - something is wrong */
+            {
+                return CURRENCY_ACCOUNTING_NEITHER;
+            }
+            /* Something is stored there - presumably a valid currency */
+            return CURRENCY_ACCOUNTING_BOOK_CURRENCY;
+        }
     }
     return CURRENCY_ACCOUNTING_NEITHER;
 }
 
+/* Returns pointer to Book Currency unique_name for book or NULL */
+const gchar *
+qof_book_get_book_currency_unique_name (QofBook *book)
+{
+    KvpFrame *kvp;
+    KvpValue *value;
+
+    if (!book)
+    {
+        PWARN ("No book!!!");
+        return NULL;
+    }
+
+    /* Get the KVP from the current book */
+    kvp = qof_instance_get_slots (QOF_INSTANCE (book));
+
+    if (!kvp)
+    {
+        PWARN ("Book has no KVP_Frame");
+        return NULL;
+    }
+
+    /* Get the Currency Accounting Method */
+    value = kvp_frame_get_slot_path (kvp,
+                                     KVP_OPTION_PATH,
+                                     OPTION_SECTION_ACCOUNTS,
+                                     OPTION_NAME_CURRENCY_ACCOUNTING,
+                                     NULL);
+    if (!value)
+       /* No currency accounting method selected; therefore no book-currency */
+       return NULL;
+
+    if (!strcmp (kvp_value_get_string (value), "book-currency") == 0)
+       /* Not book-currency currency accounting method; therefore no
+           book-currency */
+       return NULL;
+
+    /* Book-currency is the currency accounting method; get the book currency */
+    value = kvp_frame_get_slot_path (kvp,
+                                     KVP_OPTION_PATH,
+                                     OPTION_SECTION_ACCOUNTS,
+                                     OPTION_NAME_BOOK_CURRENCY,
+                                     NULL);
+    if (!value)
+    /* Book-currency currency accounting method selected but no book-currency
+       specified - something is wrong */
+    {
+        return NULL;
+    }
+
+    return kvp_value_get_string (value);
+}
+
 /* Determine whether this book uses trading accounts */
 gboolean
 qof_book_use_trading_accounts (const QofBook *book)
diff --git a/src/libqof/qof/qofbook.h b/src/libqof/qof/qofbook.h
index 9725984..e3f27fa 100644
--- a/src/libqof/qof/qofbook.h
+++ b/src/libqof/qof/qofbook.h
@@ -248,6 +248,9 @@ gboolean qof_book_use_trading_accounts (const QofBook *book);
 /** Returns TRUE if this book uses a book-currency */
 gboolean qof_book_use_book_currency (const QofBook *book);
 
+/** Returns pointer to Book Currency unique_name for book or NULL */
+const gchar * qof_book_get_book_currency_unique_name (QofBook *book);
+
 /** Returns TRUE if the auto-read-only feature should be used, otherwise
  * FALSE. This is just a wrapper on qof_book_get_num_days_autoreadonly() == 0. */
 gboolean qof_book_uses_autoreadonly (const QofBook *book);
diff --git a/src/libqof/qof/qofbookslots.h b/src/libqof/qof/qofbookslots.h
index 2ccce86..8259462 100644
--- a/src/libqof/qof/qofbookslots.h
+++ b/src/libqof/qof/qofbookslots.h
@@ -65,6 +65,7 @@
 #define OPTION_SECTION_ACCOUNTS        N_("Accounts")
 #define OPTION_NAME_TRADING_ACCOUNTS   N_("Use Trading Accounts")
 #define OPTION_NAME_CURRENCY_ACCOUNTING   N_("Select Currency Accounting Method")
+#define OPTION_NAME_BOOK_CURRENCY      N_("Select Book Currency")
 #define OPTION_NAME_AUTO_READONLY_DAYS N_("Day Threshold for Read-Only Transactions (red line)")
 #define OPTION_NAME_NUM_FIELD_SOURCE   N_("Use Split Action Field for Number")
 
@@ -78,6 +79,7 @@
  * OPTION-SECTION-ACCOUNTS
  * OPTION-NAME-TRADING-ACCOUNTS
  * OPTION-NAME-CURRENCY-ACCOUNTING
+ * OPTION-NAME-BOOK-CURRENCY
  * OPTION-NAME-AUTO-READONLY-DAYS
  * OPTION-NAME_NUM-FIELD-SOURCE
  * OPTION-SECTION-BUDGETING
diff --git a/src/libqof/qof/test/test-qofbook.c b/src/libqof/qof/test/test-qofbook.c
index d6922a5..eb1aa2e 100644
--- a/src/libqof/qof/test/test-qofbook.c
+++ b/src/libqof/qof/test/test-qofbook.c
@@ -412,20 +412,40 @@ test_book_use_trading_accounts_currency_accounting( Fixture *fixture, gconstpoin
 static void
 test_book_use_book_currency( Fixture *fixture, gconstpointer pData )
 {
+    const char *cur;
+
     g_assert( qof_book_use_book_currency( fixture-> book ) == FALSE );
+    g_assert( qof_book_get_book_currency_unique_name( fixture-> book ) == FALSE );
 
-    g_test_message( "Testing with existing currency-accounting set to 'book-currency'" );
     qof_book_begin_edit (fixture->book);
+    g_test_message( "Testing with currency-accounting set to 'trading' and no book-currency" );
+    qof_instance_set (QOF_INSTANCE (fixture->book),
+		      "currency-accounting", "trading",
+		      NULL);
+    g_assert( qof_book_use_book_currency( fixture-> book ) == FALSE );
+    g_assert( qof_book_get_book_currency_unique_name( fixture-> book ) == FALSE );
+
+    g_test_message( "Testing with currency-accounting set to 'book-currency' and no book-currency set" );
     qof_instance_set (QOF_INSTANCE (fixture->book),
 		      "currency-accounting", "book-currency",
 		      NULL);
+    g_assert( qof_book_use_book_currency( fixture-> book ) == FALSE );
+    g_assert( qof_book_get_book_currency_unique_name( fixture-> book ) == FALSE );
+
+    g_test_message( "Testing with currency-accounting set to 'book-currency' and  book-currency set" );
+    qof_instance_set (QOF_INSTANCE (fixture->book),
+		      "book-currency", "CURRENCY::USD",
+		      NULL);
     g_assert( qof_book_use_book_currency( fixture-> book ) == TRUE );
+    cur = qof_book_get_book_currency_unique_name( fixture->book );
+    g_assert_cmpstr( cur, == , "CURRENCY::USD");
 
-    g_test_message( "Testing with existing currency-accounting set to 'trading'" );
+    g_test_message( "Testing with currency-accounting set to 'trading' and book-currency still set" );
     qof_instance_set (QOF_INSTANCE (fixture->book),
 		      "currency-accounting", "trading",
 		      NULL);
     g_assert( qof_book_use_book_currency( fixture-> book ) == FALSE );
+    g_assert( qof_book_get_book_currency_unique_name( fixture-> book ) == FALSE );
     qof_book_commit_edit (fixture->book);
 
 }



Summary of changes:
 src/libqof/qof/qofbook.cpp         | 101 ++++++++++++++++++++++++++++++++++++-
 src/libqof/qof/qofbook.h           |   3 ++
 src/libqof/qof/qofbookslots.h      |   2 +
 src/libqof/qof/test/test-qofbook.c |  24 ++++++++-
 4 files changed, 126 insertions(+), 4 deletions(-)



More information about the gnucash-changes mailing list