From b32a0aa0e7325848d6469c7d3b56fe7e9bde4a61 Mon Sep 17 00:00:00 2001 From: Christian Stimming Date: Sat, 8 Oct 2011 20:30:28 +0000 Subject: [PATCH] Let the "assign payment" feature choose customer or vendor payment, depending on whether the amount is positive or negative. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@21401 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/business/business-gnome/dialog-payment.c | 32 +++++++++++++++++++ src/business/business-gnome/dialog-payment.h | 4 +++ .../business-gnome/gnc-plugin-business.c | 6 +++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/business/business-gnome/dialog-payment.c b/src/business/business-gnome/dialog-payment.c index 1ba1bdea46..47df9836b7 100644 --- a/src/business/business-gnome/dialog-payment.c +++ b/src/business/business-gnome/dialog-payment.c @@ -826,6 +826,37 @@ static Split *getFirstOtherSplit(SplitList* slist, const Split *postaccount_spli // /////////////// +gboolean gnc_ui_payment_is_customer_payment(const Transaction *txn) +{ + SplitList *slist; + gboolean result = TRUE; + + if (!txn) + return result; + + // We require the txn to have one split in an A/R or A/P account. + + slist = xaccTransGetSplitList(txn); + if (!slist) + return result; + if (countAssetAccounts(slist) == 0) + { + g_message("No asset splits in txn \"%s\"; cannot use this for assigning a payment.", + xaccTransGetDescription(txn)); + return result; + } + + { + Split *assetaccount_split = getFirstAssetAccountSplit(slist); + gnc_numeric amount = xaccSplitGetValue(assetaccount_split); + gboolean result = gnc_numeric_positive_p(amount); // positive amounts == customer + //g_message("Amount=%s", gnc_numeric_to_string(amount)); + return result; + } +} + +// /////////////// + PaymentWindow * gnc_ui_payment_new_with_txn (GncOwner *owner, Transaction *txn) { SplitList *slist; @@ -853,6 +884,7 @@ PaymentWindow * gnc_ui_payment_new_with_txn (GncOwner *owner, Transaction *txn) PaymentWindow *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_message("Amount=%s", gnc_numeric_to_string(amount)); // Fill in the values from the given txn pw->pre_existing_txn = txn; diff --git a/src/business/business-gnome/dialog-payment.h b/src/business/business-gnome/dialog-payment.h index da0d6d7c4e..2f8fd42e97 100644 --- a/src/business/business-gnome/dialog-payment.h +++ b/src/business/business-gnome/dialog-payment.h @@ -36,6 +36,10 @@ PaymentWindow * gnc_ui_payment_new_with_invoice (GncOwner *owner, GncInvoice *invoice); PaymentWindow * gnc_ui_payment_new_with_txn (GncOwner *owner, Transaction *txn); +/** Returns TRUE if the given transaction (to be used with gnc_ui_payment_new_with_txn() ) + * is for a customer, or FALSE if it's from a vendor or employee voucher. */ +gboolean gnc_ui_payment_is_customer_payment(const Transaction *txn); + /* Destroy a payment window */ void gnc_ui_payment_window_destroy (PaymentWindow *pw); diff --git a/src/business/business-gnome/gnc-plugin-business.c b/src/business/business-gnome/gnc-plugin-business.c index 4cbc008f3b..cd8d3722e3 100644 --- a/src/business/business-gnome/gnc-plugin-business.c +++ b/src/business/business-gnome/gnc-plugin-business.c @@ -896,6 +896,7 @@ static void gnc_plugin_business_cmd_assign_payment (GtkAction *action, SplitRegister *reg; Split *split; Transaction *trans; + gboolean is_customer; g_return_if_fail (mw != NULL); g_return_if_fail (GNC_IS_PLUGIN_BUSINESS (mw->data)); @@ -919,13 +920,16 @@ static void gnc_plugin_business_cmd_assign_payment (GtkAction *action, trans = xaccSplitGetParent(split); g_return_if_fail(trans); + is_customer = gnc_ui_payment_is_customer_payment(trans); plugin_business = GNC_PLUGIN_BUSINESS (mw->data); plugin_business_priv = GNC_PLUGIN_BUSINESS_GET_PRIVATE (plugin_business); gnc_business_assign_payment (gnc_plugin_page_get_window(plugin_page), trans, - plugin_business_priv->last_customer); + is_customer + ? plugin_business_priv->last_customer + : plugin_business_priv->last_vendor); } static const gchar *register_txn_actions[] =