Move some utility functions from gui code to engine

And reduce some of the clutter in these functions and their callers.
This commit is contained in:
Geert Janssens 2016-03-25 12:10:10 +01:00
parent 163c4dd44a
commit 531335a8f9
5 changed files with 63 additions and 78 deletions

View File

@ -821,15 +821,6 @@ gnc_payment_leave_amount_cb (GtkWidget *widget, GdkEventFocus *event,
gnc_payment_window_check_payment (pw);
}
static gboolean AccountTypeOkForPayments (GNCAccountType type)
{
if (xaccAccountIsAssetLiabType(type) ||
xaccAccountIsEquityType(type))
return TRUE;
else
return FALSE;
}
/* Select the list of accounts to show in the tree */
static void
gnc_payment_set_account_types (GncTreeViewAccount *tree)
@ -840,7 +831,7 @@ gnc_payment_set_account_types (GncTreeViewAccount *tree)
gnc_tree_view_account_get_view_info (tree, &avi);
for (i = 0; i < NUM_ACCOUNT_TYPES; i++)
avi.include_type[i] = AccountTypeOkForPayments (i);
avi.include_type[i] = gncBusinessIsPaymentAcctType (i);
gnc_tree_view_account_set_view_info (tree, &avi);
}
@ -1151,67 +1142,11 @@ gnc_ui_payment_new (GncOwner *owner, QofBook *book)
return gnc_ui_payment_new_with_invoice (owner, book, NULL);
}
// ////////////////////////////////////////////////////////////
static void increment_if_asset_account (gpointer data,
gpointer user_data)
{
int *r = user_data;
const Split *split = data;
const Account *account = xaccSplitGetAccount(split);
if (AccountTypeOkForPayments(xaccAccountGetType (account)))
++(*r);
}
static int countAssetAccounts(SplitList* slist)
{
int result = 0;
g_list_foreach(slist, &increment_if_asset_account, &result);
return result;
}
static gint predicate_is_asset_account(gconstpointer a,
gconstpointer user_data)
{
const Split *split = a;
const Account *account = xaccSplitGetAccount(split);
if (AccountTypeOkForPayments(xaccAccountGetType(account)))
return 0;
else
return -1;
}
static gint predicate_is_apar_account(gconstpointer a,
gconstpointer user_data)
{
const Split *split = a;
const Account *account = xaccSplitGetAccount(split);
if (xaccAccountIsAPARType(xaccAccountGetType(account)))
return 0;
else
return -1;
}
static Split *getFirstAssetAccountSplit(SplitList* slist)
{
GList *r = g_list_find_custom(slist, NULL, &predicate_is_asset_account);
if (r)
return r->data;
else
return NULL;
}
static Split *getFirstAPARAccountSplit(SplitList* slist)
{
GList *r = g_list_find_custom(slist, NULL, &predicate_is_apar_account);
if (r)
return r->data;
else
return NULL;
}
// ///////////////
gboolean gnc_ui_payment_is_customer_payment(const Transaction *txn)
{
SplitList *slist;
gboolean result = TRUE;
Split *assetaccount_split;
gnc_numeric amount;
@ -1220,17 +1155,17 @@ gboolean gnc_ui_payment_is_customer_payment(const Transaction *txn)
// We require the txn to have one split in an A/R or A/P account.
slist = xaccTransGetSplitList(txn);
if (!slist)
if (!xaccTransGetSplitList(txn))
return result;
if (countAssetAccounts(slist) == 0)
assetaccount_split = xaccTransGetFirstPaymentAcctSplit(txn);
if (!assetaccount_split)
{
g_message("No asset splits in txn \"%s\"; cannot use this for assigning a payment.",
xaccTransGetDescription(txn));
return result;
}
assetaccount_split = getFirstAssetAccountSplit(slist);
assetaccount_split = xaccTransGetFirstPaymentAcctSplit(txn);
amount = xaccSplitGetValue(assetaccount_split);
result = gnc_numeric_positive_p(amount); // positive amounts == customer
//g_message("Amount=%s", gnc_numeric_to_string(amount));
@ -1241,8 +1176,6 @@ gboolean gnc_ui_payment_is_customer_payment(const Transaction *txn)
PaymentWindow * gnc_ui_payment_new_with_txn (GncOwner *owner, Transaction *txn)
{
SplitList *slist;
Split *assetaccount_split;
Split *postaccount_split;
gnc_numeric amount;
@ -1253,23 +1186,22 @@ PaymentWindow * gnc_ui_payment_new_with_txn (GncOwner *owner, Transaction *txn)
// We require the txn to have one split in an Asset account.
slist = xaccTransGetSplitList(txn);
if (!slist)
if (!xaccTransGetSplitList(txn))
return NULL;
if (countAssetAccounts(slist) == 0)
assetaccount_split = xaccTransGetFirstPaymentAcctSplit(txn);
if (!assetaccount_split)
{
g_message("No asset splits in txn \"%s\"; cannot use this for assigning a payment.",
xaccTransGetDescription(txn));
return NULL;
}
assetaccount_split = getFirstAssetAccountSplit(slist);
postaccount_split = getFirstAPARAccountSplit(slist); // watch out: Might be NULL
postaccount_split = xaccTransGetFirstAPARAcctSplit(txn); // watch out: Might be NULL
amount = xaccSplitGetValue(assetaccount_split);
pw = gnc_ui_payment_new(owner,
qof_instance_get_book(QOF_INSTANCE(txn)));
g_assert(assetaccount_split); // we can rely on this because of the countAssetAccounts() check above
g_assert(assetaccount_split); // we can rely on this because of the check above
g_debug("Amount=%s", gnc_numeric_to_string(amount));
// Fill in the values from the given txn

View File

@ -60,6 +60,7 @@ struct timeval
#include <gnc-gdate-utils.h>
#include "SchedXaction.h"
#include "qofbackend-p.h"
#include "gncBusiness.h"
/* Notes about xaccTransBeginEdit(), xaccTransCommitEdit(), and
* xaccTransRollback():
@ -2141,6 +2142,28 @@ xaccTransGetSplitList (const Transaction *trans)
return trans ? trans->splits : NULL;
}
Split *xaccTransGetFirstPaymentAcctSplit(const Transaction *trans)
{
FOR_EACH_SPLIT (trans,
const Account *account = xaccSplitGetAccount(s);
if (gncBusinessIsPaymentAcctType(xaccAccountGetType(account)))
return s;
);
return NULL;
}
Split *xaccTransGetFirstAPARAcctSplit(const Transaction *trans)
{
FOR_EACH_SPLIT (trans,
const Account *account = xaccSplitGetAccount(s);
if (xaccAccountIsAPARType(xaccAccountGetType(account)))
return s;
);
return NULL;
}
int
xaccTransCountSplits (const Transaction *trans)
{

View File

@ -370,6 +370,21 @@ int xaccTransGetSplitIndex(const Transaction *trans, const Split *split);
SplitList * xaccTransGetSplitList (const Transaction *trans);
gboolean xaccTransStillHasSplit(const Transaction *trans, const Split *s);
/** The xaccTransGetFirstPaymentAcctSplit() method returns a pointer to the first
split in this transaction that belongs to an account which is considered a
valid account for business payments.
@param trans The transaction
If there is no such split in the transaction NULL will be returned. */
Split * xaccTransGetFirstPaymentAcctSplit (const Transaction *trans);
/** The xaccTransGetFirstPaymentAcctSplit() method returns a pointer to the first
split in this transaction that belongs to an AR or AP account.
@param trans The transaction
If there is no such split in the transaction NULL will be returned. */
Split * xaccTransGetFirstAPARAcctSplit (const Transaction *trans);
/** Set the transaction to be ReadOnly by setting a non-NULL value as "reason".
*
* FIXME: If "reason" is NULL, this function does nothing, instead of removing the

View File

@ -88,3 +88,12 @@ GList * gncBusinessGetOwnerList (QofBook *book, const char *type_name,
return data.result;
}
gboolean gncBusinessIsPaymentAcctType (GNCAccountType type)
{
if (xaccAccountIsAssetLiabType(type) ||
xaccAccountIsEquityType(type))
return TRUE;
else
return FALSE;
}

View File

@ -36,6 +36,7 @@
#include <glib.h>
#include "qof.h"
#include "Account.h"
/* @deprecated backwards-compat definitions */
#define GNC_BILLTERM_MODULE_NAME GNC_ID_BILLTERM
@ -80,5 +81,10 @@ typedef GList OwnerList;
OwnerList * gncBusinessGetOwnerList (QofBook *book, QofIdTypeConst type_name,
gboolean all_including_inactive);
/** Returns whether the given account type is a valid type to use in
* business payments. Currently payments are allowed to/from assets,
* liabilities and equity accounts. */
gboolean gncBusinessIsPaymentAcctType (GNCAccountType type);
#endif /* GNC_BUSINESS_H_ */