Factor out a convenience function to retrieve a (business) owner starting from a transaction

This commit is contained in:
Geert Janssens 2016-03-25 17:39:20 +01:00
parent 656a3a9b57
commit 6fd35694e2
3 changed files with 47 additions and 21 deletions

View File

@ -637,6 +637,31 @@ gboolean gncOwnerGetOwnerFromLot (GNCLot *lot, GncOwner *owner)
return (owner->owner.undefined != NULL); return (owner->owner.undefined != NULL);
} }
gboolean gncOwnerGetOwnerFromTxn (Transaction *txn, GncOwner *owner)
{
Split *apar_split = NULL;
if (!txn || !owner) return FALSE;
if (xaccTransGetTxnType (txn) == TXN_TYPE_NONE)
return FALSE;
apar_split = xaccTransGetFirstAPARAcctSplit (txn);
if (apar_split)
{
GNCLot *lot = xaccSplitGetLot (apar_split);
GncInvoice *invoice = gncInvoiceGetInvoiceFromLot (lot);
if (invoice)
gncOwnerCopy (gncInvoiceGetOwner (invoice), owner);
else if (!gncOwnerGetOwnerFromLot (lot, owner))
return FALSE;
return TRUE; // Got owner from either invoice or lot
}
return FALSE;
}
gboolean gncOwnerIsValid (const GncOwner *owner) gboolean gncOwnerIsValid (const GncOwner *owner)
{ {
if (!owner) return FALSE; if (!owner) return FALSE;

View File

@ -195,6 +195,15 @@ gint gncOwnerLotsSortFunc (GNCLot *lotA, GNCLot *lotB);
*/ */
gboolean gncOwnerGetOwnerFromLot (GNCLot *lot, GncOwner *owner); gboolean gncOwnerGetOwnerFromLot (GNCLot *lot, GncOwner *owner);
/** Convenience function to get the owner from a transaction.
* Transactions don't really have an owner. What this function will
* do it figure out whether the transaction is part of a business
* transaction (either a posted invoice/bill/voucher/credit note or
* a payment transaction) and use the business object behind it
* to extract owner information.
*/
gboolean gncOwnerGetOwnerFromTxn (Transaction *txn, GncOwner *owner);
gboolean gncOwnerGetOwnerFromTypeGuid (QofBook *book, GncOwner *owner, QofIdType type, GncGUID *guid); gboolean gncOwnerGetOwnerFromTypeGuid (QofBook *book, GncOwner *owner, QofIdType type, GncGUID *guid);
/** Get the kvp-frame from the underlying owner object */ /** Get the kvp-frame from the underlying owner object */

View File

@ -1706,28 +1706,20 @@ gnc_ui_print_check_dialog_create(GtkWidget *parent,
* it would create build problems */ * it would create build problems */
if (g_list_length(pcd->splits) == 1) if (g_list_length(pcd->splits) == 1)
{ {
trans = xaccSplitGetParent((Split *)(pcd->splits->data)); GncOwner txn_owner;
if (xaccTransGetTxnType (trans) == TXN_TYPE_PAYMENT)
{
Split *apar_split = xaccTransGetFirstAPARAcctSplit (trans);
if (apar_split)
{
GNCLot *lot = xaccSplitGetLot (apar_split);
GncOwner owner;
if (!gncOwnerGetOwnerFromLot (lot, &owner))
{
GncInvoice *invoice = gncInvoiceGetInvoiceFromLot (lot);
if (invoice)
gncOwnerCopy (gncOwnerGetEndOwner (gncInvoiceGetOwner (invoice)), &owner);
}
/* Got a business owner, get the address */ trans = xaccSplitGetParent((Split *)(pcd->splits->data));
gtk_entry_set_text(GTK_ENTRY(pcd->check_address_name), gncOwnerGetName(&owner)); if (gncOwnerGetOwnerFromTxn (trans, &txn_owner))
gtk_entry_set_text(GTK_ENTRY(pcd->check_address_1), gncAddressGetAddr1 (gncOwnerGetAddr(&owner))); {
gtk_entry_set_text(GTK_ENTRY(pcd->check_address_2), gncAddressGetAddr2 (gncOwnerGetAddr(&owner))); GncOwner owner;
gtk_entry_set_text(GTK_ENTRY(pcd->check_address_3), gncAddressGetAddr3 (gncOwnerGetAddr(&owner))); gncOwnerCopy (gncOwnerGetEndOwner (&txn_owner), &owner);
gtk_entry_set_text(GTK_ENTRY(pcd->check_address_4), gncAddressGetAddr4 (gncOwnerGetAddr(&owner)));
} /* Got a business owner, get the address */
gtk_entry_set_text(GTK_ENTRY(pcd->check_address_name), gncOwnerGetName(&owner));
gtk_entry_set_text(GTK_ENTRY(pcd->check_address_1), gncAddressGetAddr1 (gncOwnerGetAddr(&owner)));
gtk_entry_set_text(GTK_ENTRY(pcd->check_address_2), gncAddressGetAddr2 (gncOwnerGetAddr(&owner)));
gtk_entry_set_text(GTK_ENTRY(pcd->check_address_3), gncAddressGetAddr3 (gncOwnerGetAddr(&owner)));
gtk_entry_set_text(GTK_ENTRY(pcd->check_address_4), gncAddressGetAddr4 (gncOwnerGetAddr(&owner)));
} }
} }