diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp index 1a53ff6026..378aff1a46 100644 --- a/libgnucash/engine/Account.cpp +++ b/libgnucash/engine/Account.cpp @@ -2579,6 +2579,34 @@ xaccAccountSetNotes (Account *acc, const char *str) set_kvp_string_tag (acc, "notes", str); } + +void +xaccAccountSetAssociatedAccount (Account *acc, const char *tag, const Account* assoc_acct) +{ + g_return_if_fail (GNC_IS_ACCOUNT(acc)); + g_return_if_fail (tag && *tag); + + std::vector path = { "associated-account", tag }; + xaccAccountBeginEdit(acc); + + PINFO ("setting %s assoc %s account = %s", xaccAccountGetName (acc), tag, + assoc_acct ? xaccAccountGetName (assoc_acct) : nullptr); + + if (GNC_IS_ACCOUNT(assoc_acct)) + { + GValue v = G_VALUE_INIT; + g_value_init (&v, GNC_TYPE_GUID); + g_value_set_static_boxed (&v, xaccAccountGetGUID (assoc_acct)); + qof_instance_set_path_kvp (QOF_INSTANCE (acc), &v, path); + g_value_unset (&v); + } + else + qof_instance_set_path_kvp (QOF_INSTANCE (acc), nullptr, path); + + mark_account (acc); + xaccAccountCommitEdit(acc); +} + void xaccAccountSetCommodity (Account * acc, gnc_commodity * com) { @@ -3367,6 +3395,28 @@ xaccAccountGetNotes (const Account *acc) return rv; } +Account* +xaccAccountGetAssociatedAccount (const Account *acc, const char *tag) +{ + g_return_val_if_fail (GNC_IS_ACCOUNT(acc), nullptr); + g_return_val_if_fail (tag && *tag, nullptr); + + GValue v = G_VALUE_INIT; + qof_instance_get_path_kvp (QOF_INSTANCE (acc), &v, { "associated-account", tag }); + + auto guid = static_cast(G_VALUE_HOLDS_BOXED (&v) ? g_value_get_boxed(&v) : nullptr); + g_value_unset (&v); + + if (!guid) + return nullptr; + + auto assoc_acct = xaccAccountLookup (guid, gnc_account_get_book (acc)); + PINFO ("retuning %s assoc %s account = %s", xaccAccountGetName (acc), tag, + xaccAccountGetName (assoc_acct)); + return assoc_acct; +} + + gnc_commodity * DxaccAccountGetCurrency (const Account *acc) { diff --git a/libgnucash/engine/Account.h b/libgnucash/engine/Account.h index f1db851cf4..daac6b52af 100644 --- a/libgnucash/engine/Account.h +++ b/libgnucash/engine/Account.h @@ -312,6 +312,11 @@ typedef enum void xaccAccountSetSortReversed (Account *account, gboolean sortreversed); /** Set the account's notes */ void xaccAccountSetNotes (Account *account, const char *notes); + + /** Set the account's associated account e.g. stock account -> dividend account */ + void xaccAccountSetAssociatedAccount (Account *acc, const char *tag, + const Account *assoc_acct); + /** Set the last num field of an Account */ void xaccAccountSetLastNum (Account *account, const char *num); /** Set the account's lot order policy */ @@ -416,6 +421,9 @@ typedef enum gboolean xaccAccountGetSortReversed (const Account *account); /** Get the account's notes */ const char * xaccAccountGetNotes (const Account *account); + + /** Get the account's associated account e.g. stock account -> dividend account */ + Account* xaccAccountGetAssociatedAccount (const Account *acc, const char *tag); /** Get the last num field of an Account */ const char * xaccAccountGetLastNum (const Account *account); /** Get the account's lot order policy */ diff --git a/libgnucash/engine/test/utest-Account.cpp b/libgnucash/engine/test/utest-Account.cpp index e09e5426b8..fbabc77f93 100644 --- a/libgnucash/engine/test/utest-Account.cpp +++ b/libgnucash/engine/test/utest-Account.cpp @@ -1237,6 +1237,25 @@ test_gnc_account_kvp_setters_getters (Fixture *fixture, gconstpointer pData) xaccAccountSetNotes (account, nullptr); g_assert_cmpstr (xaccAccountGetNotes (account), ==, nullptr); + // Associated Account getter/setter + g_assert_null (xaccAccountGetAssociatedAccount (account, "test")); + + g_test_expect_message ("gnc.engine", G_LOG_LEVEL_CRITICAL, + "*xaccAccountSetAssociatedAccount*assertion*tag && *tag*"); + xaccAccountSetAssociatedAccount (account, nullptr, account); + g_test_assert_expected_messages(); + + g_test_expect_message ("gnc.engine", G_LOG_LEVEL_CRITICAL, + "*xaccAccountSetAssociatedAccount*assertion*GNC_IS_ACCOUNT(acc)*"); + xaccAccountSetAssociatedAccount (nullptr, "test", account); + g_test_assert_expected_messages(); + + xaccAccountSetAssociatedAccount (account, "test", account); + g_assert_true (xaccAccountGetAssociatedAccount (account, "test") == account); + + xaccAccountSetAssociatedAccount (account, "test", nullptr); + g_assert_null (xaccAccountGetAssociatedAccount (account, "test")); + // Balance Limits getter/setter g_assert_true (xaccAccountGetHigherBalanceLimit (account, &balance_limit) == false); g_assert_true (xaccAccountGetLowerBalanceLimit (account, &balance_limit) == false);