diff --git a/src/engine/gncOwner.c b/src/engine/gncOwner.c index 0c0af8b02d..c55dd5c483 100644 --- a/src/engine/gncOwner.c +++ b/src/engine/gncOwner.c @@ -637,6 +637,31 @@ gboolean gncOwnerGetOwnerFromLot (GNCLot *lot, GncOwner *owner) 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) { if (!owner) return FALSE; diff --git a/src/engine/gncOwner.h b/src/engine/gncOwner.h index b805ac6274..204bf34640 100644 --- a/src/engine/gncOwner.h +++ b/src/engine/gncOwner.h @@ -195,6 +195,15 @@ gint gncOwnerLotsSortFunc (GNCLot *lotA, GNCLot *lotB); */ 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); /** Get the kvp-frame from the underlying owner object */ diff --git a/src/gnome/dialog-print-check.c b/src/gnome/dialog-print-check.c index 78fdf222c6..1ec4c3eda9 100644 --- a/src/gnome/dialog-print-check.c +++ b/src/gnome/dialog-print-check.c @@ -1706,28 +1706,20 @@ gnc_ui_print_check_dialog_create(GtkWidget *parent, * it would create build problems */ if (g_list_length(pcd->splits) == 1) { - trans = xaccSplitGetParent((Split *)(pcd->splits->data)); - 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); - } + GncOwner txn_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))); - } + trans = xaccSplitGetParent((Split *)(pcd->splits->data)); + if (gncOwnerGetOwnerFromTxn (trans, &txn_owner)) + { + GncOwner owner; + gncOwnerCopy (gncOwnerGetEndOwner (&txn_owner), &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))); } }