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
This commit is contained in:
Alex Aycinena
2015-02-28 16:39:34 -08:00
parent e814221efc
commit 906ed1fe26
4 changed files with 131 additions and 9 deletions
+99 -2
View File
@@ -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,
@@ -283,6 +300,17 @@ qof_book_class_init (QofBookClass *klass)
NULL,
G_PARAM_READWRITE));
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,
@@ -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)
+3
View File
@@ -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);
+2
View File
@@ -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
+27 -7
View File
@@ -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);
qof_instance_set (QOF_INSTANCE (fixture->book),
"currency-accounting", "book-currency",
NULL);
g_assert( qof_book_use_book_currency( fixture-> book ) == TRUE );
g_test_message( "Testing with existing currency-accounting set to 'trading'" );
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 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);
}