Skip to content

Commit

Permalink
Implement a balance for owners and show it on the owners view page
Browse files Browse the repository at this point in the history
The Balance column is not visible by default, but can be activated
with the blue arrow in the top-right of the page.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@21182 57a11ea4-9604-0410-9ed3-97b8803252fd
  • Loading branch information
gjanssens committed Aug 13, 2011
1 parent 49a02f9 commit 1b7bd7b
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 139 deletions.
88 changes: 88 additions & 0 deletions src/app-utils/gnc-ui-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,94 @@ gnc_ui_account_get_balance_as_of_date (Account *account,
return balance;
}

/*
* This is a wrapper routine around an gncOwnerGetBalanceInCurrency
* function that handles additional needs of the gui.
*
* @param owner The owner to retrieve data about.
* @param negative An indication of whether or not the returned value
* is negative. This can be used by the caller to
* easily decode whether or not to color the output.
* @param commodity The commodity in which the account balance should
* be returned. If NULL, the value will be returned in
* the commodity of the owner. This is normally used
* to specify a currency, which forces the conversion
* of things like stock account values from share
* values to an amount the requested currency.
*/
gnc_numeric
gnc_ui_owner_get_balance_full (GncOwner *owner,
gboolean *negative,
const gnc_commodity *commodity)
{
gnc_numeric balance;

if (!owner)
return gnc_numeric_zero ();

balance = gncOwnerGetBalanceInCurrency (owner, commodity);

/* reverse sign if needed */
if ((gncOwnerGetType (owner) == GNC_OWNER_CUSTOMER))
balance = gnc_numeric_neg (balance);

/* Record whether the balance is negative. */
if (negative)
*negative = gnc_numeric_negative_p (balance);

return balance;
}


/**
* Wrapper around gnc_ui_owner_get_balance_full that converts
* the resulting number to a character string. The number is
* formatted according to the specification of the owner currency.
* The caller is responsible for g_free'ing the returned memory.
*
* @param owner The owner to retrieve data about.
* @param negative An indication of whether or not the returned value
* is negative. This can be used by the caller to
* easily decode whether or not to color the output.
*/
gchar *
gnc_ui_owner_get_print_balance (GncOwner *owner,
gboolean *negative)
{
gnc_numeric balance;
GNCPrintAmountInfo print_info;

balance = gnc_ui_owner_get_balance_full (owner, negative, NULL);
print_info = gnc_commodity_print_info (gncOwnerGetCurrency (owner), TRUE);
return g_strdup (xaccPrintAmount (balance, print_info));
}

/**
* Wrapper around gnc_ui_owner_get_balance_full that converts
* the resulting number to a character string. The number is
* formatted according to the specification of the default reporting
* currency.
*
* @param account The owner to retrieve data about.
* @param negative An indication of whether or not the returned value
* is negative. This can be used by the caller to
* easily decode whether or not to color the output.
*/
gchar *
gnc_ui_owner_get_print_report_balance (GncOwner *owner,
gboolean *negative)
{
GNCPrintAmountInfo print_info;
gnc_numeric balance;
gnc_commodity *report_commodity;

report_commodity = gnc_default_report_currency ();
balance = gnc_ui_owner_get_balance_full (owner, negative,
report_commodity);
print_info = gnc_commodity_print_info (report_commodity, TRUE);
return g_strdup (xaccPrintAmount (balance, print_info));
}

/* Caller is responsible for g_free'ing returned memory */
char *
gnc_ui_account_get_tax_info_string (const Account *account)
Expand Down
28 changes: 28 additions & 0 deletions src/app-utils/gnc-ui-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <locale.h>

#include "Account.h"
#include "gncOwner.h"
#include "qof.h"


Expand Down Expand Up @@ -175,6 +176,33 @@ gnc_ui_account_get_print_report_balance (xaccGetBalanceInCurrencyFn fn,
gboolean recurse,
gboolean *negative);

/** Get the balance for the underlying owner object.
* The returned value is always positive,
* intended to be displayed to a user. However the real sign
* of the balance is indicated via the "negative" parameter.
*/
gnc_numeric gnc_ui_owner_get_balance_full (GncOwner *owner,
gboolean *negative,
const gnc_commodity *commodity);

/** Get the balance for the underlying owner object in string format
* and the owner's native currency.
* The returned value is always positive,
* intended to be displayed to a user. However the real sign
* of the balance is indicated via the "negative" parameter.
*/
gchar * gnc_ui_owner_get_print_balance (GncOwner *owner,
gboolean *negative);

/** Get the balance for the underlying owner object in string format
* and in the default report currency.
* The returned value is always positive,
* intended to be displayed to a user. However the real sign
* of the balance is indicated via the "negative" parameter.
*/
gchar * gnc_ui_owner_get_print_report_balance (GncOwner *owner,
gboolean *negative);

char *gnc_ui_account_get_tax_info_string (const Account *account);

char *gnc_ui_account_get_tax_info_sub_acct_string (const Account *account);
Expand Down
63 changes: 63 additions & 0 deletions src/engine/gncOwner.c
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,69 @@ gncOwnerGetCommoditiesList (const GncOwner *owner)
return (g_list_prepend (NULL, gncOwnerGetCurrency(owner)));
}

/*********************************************************************/
/* Owner balance calculation routines */

/*
* Given an owner, extract the open balance from the owner and then
* convert it to the desired currency.
*/
gnc_numeric
gncOwnerGetBalanceInCurrency (GncOwner *owner,
const gnc_commodity *report_currency)
{
gnc_numeric balance = gnc_numeric_zero ();
GList *acct_list, *acct_node, *acct_types, *lot_list = NULL, *lot_node;
QofBook *book;
gnc_commodity *owner_currency;
GNCPriceDB *pdb;

g_return_val_if_fail (owner, gnc_numeric_zero ());

/* Get account list */
book = qof_instance_get_book (qofOwnerGetOwner (owner));
acct_list = gnc_account_get_descendants (gnc_book_get_root_account (book));
acct_types = gncOwnerGetAccountTypesList (owner);
owner_currency = gncOwnerGetCurrency (owner);

/* For each account */
for (acct_node = acct_list; acct_node; acct_node = acct_node->next)
{
Account *account = acct_node->data;

/* Check if this account can have lots for the owner, otherwise skip to next */
if (g_list_index (acct_types, (gpointer)xaccAccountGetType (account))
== -1)
continue;


if (!gnc_commodity_equal (owner_currency, xaccAccountGetCommodity (account)))
continue;

/* Get a list of open lots for this owner and account */
lot_list = xaccAccountFindOpenLots (account, gnc_lot_match_invoice_owner,
owner,
(GCompareFunc)gnc_lot_sort_func);
/* For each lot */
for (lot_node = lot_list; lot_node; lot_node = lot_node->next)
{
GNCLot *lot = lot_node->data;
gnc_numeric lot_balance = gnc_lot_get_balance (lot);
balance = gnc_numeric_add (balance, lot_balance,
gnc_commodity_get_fraction (owner_currency), GNC_HOW_RND_ROUND_HALF_UP);
}
}

pdb = gnc_pricedb_get_db (book);

if (report_currency)
balance = gnc_pricedb_convert_balance_latest_price (
pdb, balance, owner_currency, report_currency);

return balance;
}


/* XXX: Yea, this is broken, but it should work fine for Queries.
* We're single-threaded, right?
*/
Expand Down
8 changes: 8 additions & 0 deletions src/engine/gncOwner.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ GList * gncOwnerGetAccountTypesList (const GncOwner *owner);
/** Returns a GList of currencies associated with the owner */
GList * gncOwnerGetCommoditiesList (const GncOwner *owner);


/** Given an owner, extract the open balance from the owner and then
* convert it to the desired currency.
*/
gnc_numeric
gncOwnerGetBalanceInCurrency (GncOwner *owner,
const gnc_commodity *report_currency);

#define OWNER_TYPE "type"
#define OWNER_TYPE_STRING "type-string" /**< Allows the type to be handled externally. */
#define OWNER_CUSTOMER "customer"
Expand Down
72 changes: 7 additions & 65 deletions src/gnome-utils/gnc-tree-model-owner.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,9 @@ gnc_tree_model_owner_get_column_type (GtkTreeModel *tree_model,
case GNC_TREE_MODEL_OWNER_COL_EMAIL:
case GNC_TREE_MODEL_OWNER_COL_BALANCE:
case GNC_TREE_MODEL_OWNER_COL_BALANCE_REPORT:
case GNC_TREE_MODEL_OWNER_COL_BALANCE_PERIOD:
case GNC_TREE_MODEL_OWNER_COL_NOTES:

case GNC_TREE_MODEL_OWNER_COL_COLOR_BALANCE:
case GNC_TREE_MODEL_OWNER_COL_COLOR_BALANCE_PERIOD:
return G_TYPE_STRING;

case GNC_TREE_MODEL_OWNER_COL_ACTIVE:
Expand Down Expand Up @@ -526,43 +524,6 @@ gnc_tree_model_owner_set_color(GncTreeModelOwner *model,
g_value_set_static_string (value, "black");
}

static gchar *
gnc_tree_model_owner_compute_period_balance(GncTreeModelOwner *model,
GncOwner *acct,
gboolean recurse,
gboolean *negative)
{
GncTreeModelOwnerPrivate *priv;
time_t t1, t2;
gnc_numeric b3;

if ( negative )
*negative = FALSE;

/* FIXME Figure out how to calculate the payment balance for an owner.
* The code below is from the account tree model and should be
* adapted for owners.
priv = GNC_TREE_MODEL_OWNER_GET_PRIVATE(model);
if (acct == priv->root)
return g_strdup("");
t1 = gnc_ownering_period_fiscal_start();
t2 = gnc_ownering_period_fiscal_end();
if (t1 > t2)
return g_strdup("");
b3 = xaccOwnerGetBalanceChangeForPeriod(acct, t1, t2, recurse);
if (gnc_reverse_balance (acct))
b3 = gnc_numeric_neg (b3);
if (negative)
*negative = gnc_numeric_negative_p(b3);
return g_strdup(xaccPrintAmount(b3, gnc_owner_print_info(acct, TRUE))); */
return g_strdup("0");
}

static void
gnc_tree_model_owner_get_value (GtkTreeModel *tree_model,
GtkTreeIter *iter,
Expand Down Expand Up @@ -674,39 +635,20 @@ gnc_tree_model_owner_get_value (GtkTreeModel *tree_model,

case GNC_TREE_MODEL_OWNER_COL_BALANCE:
g_value_init (value, G_TYPE_STRING);
/* FIXME how to calculate an Owner's balance ?
string = gnc_ui_owner_get_print_balance(xaccOwnerGetBalanceInCurrency,
owner, TRUE, &negative);
g_value_take_string (value, string); */
g_value_set_static_string (value, "0");
string = gnc_ui_owner_get_print_balance(owner, &negative);
g_value_take_string (value, string);
break;

case GNC_TREE_MODEL_OWNER_COL_BALANCE_REPORT:
g_value_init (value, G_TYPE_STRING);
/* FIXME how to calculate an Owner's balance ?
string = gnc_ui_owner_get_print_report_balance(xaccOwnerGetBalanceInCurrency,
owner, TRUE, &negative);
g_value_take_string (value, string); */
g_value_set_static_string (value, "0");
break;
case GNC_TREE_MODEL_OWNER_COL_COLOR_BALANCE:
g_value_init (value, G_TYPE_STRING);
/* FIXME how to calculate an Owner's balance ?
string = gnc_ui_owner_get_print_balance(xaccOwnerGetBalanceInCurrency,
owner, TRUE, &negative);*/
negative = FALSE;
gnc_tree_model_owner_set_color(model, negative, value);
/* g_free(string);*/
break;
case GNC_TREE_MODEL_OWNER_COL_BALANCE_PERIOD:
g_value_init (value, G_TYPE_STRING);
string = gnc_tree_model_owner_compute_period_balance(model, owner, FALSE, &negative);
string = gnc_ui_owner_get_print_report_balance(owner, &negative);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_OWNER_COL_COLOR_BALANCE_PERIOD:
case GNC_TREE_MODEL_OWNER_COL_COLOR_BALANCE:
g_value_init (value, G_TYPE_STRING);
string = gnc_tree_model_owner_compute_period_balance(model, owner, FALSE, &negative);
string = gnc_ui_owner_get_print_balance(owner, &negative);
gnc_tree_model_owner_set_color(model, negative, value);
g_free (string);
g_free(string);
break;

case GNC_TREE_MODEL_OWNER_COL_NOTES:
Expand Down
2 changes: 0 additions & 2 deletions src/gnome-utils/gnc-tree-model-owner.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,13 @@ typedef enum
GNC_TREE_MODEL_OWNER_COL_EMAIL,
GNC_TREE_MODEL_OWNER_COL_BALANCE,
GNC_TREE_MODEL_OWNER_COL_BALANCE_REPORT,
GNC_TREE_MODEL_OWNER_COL_BALANCE_PERIOD,
GNC_TREE_MODEL_OWNER_COL_NOTES,
GNC_TREE_MODEL_OWNER_COL_ACTIVE,

GNC_TREE_MODEL_OWNER_COL_LAST_VISIBLE = GNC_TREE_MODEL_OWNER_COL_ACTIVE,

/* internal hidden columns */
GNC_TREE_MODEL_OWNER_COL_COLOR_BALANCE,
GNC_TREE_MODEL_OWNER_COL_COLOR_BALANCE_PERIOD,

GNC_TREE_MODEL_OWNER_NUM_COLUMNS
} GncTreeModelOwnerColumn;
Expand Down
Loading

0 comments on commit 1b7bd7b

Please sign in to comment.