From 5fbfa23dbbe28cb3c05a9bb831b5a868711501eb Mon Sep 17 00:00:00 2001 From: Christian Stimming Date: Sat, 14 Aug 2010 20:48:18 +0000 Subject: [PATCH] Bug #625193: Added 'search by ID' in python binding for invoices, customers and bills. Patch by Mike E and Mark Jenkins: When creating or appending to invoices, customers and bills, searching by ID is likely more useful than by GUID. I've added this functionality to the Python bindings. Search by ID using the python code: tmp = gnucash.gnucash_core_c.search_invoice_on_id(ID,book.instance) if tmp: invoice = gnucash.gnucash_business.Invoice(instance=tmp) Use the invoice object as in sample_scripts/simple_invoice_insert.py I support this patch, but I've made a few improvments of my own. I switched up the arguments in search_customer_on_id, search_invoice_on_id, search_bill_on_id to have Book first and ID second. The reason for this was to make these functions more consistent with the other functions where a search is done through a book on a particular attribute. Also added some specific python bindings support to allow this to be used as methods of Book: Book.CustomerLookupByID, Book.InvoiceLookupByID, Book.BillLoookupByID. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@19431 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/optional/python-bindings/Makefile.am | 14 +- .../example_scripts/simple_business_create.py | 10 + .../example_scripts/simple_invoice_insert.py | 5 +- src/optional/python-bindings/gnucash_core.i | 8 +- src/optional/python-bindings/gnucash_core.py | 18 +- src/optional/python-bindings/utils.c | 179 ++++++++++++++++++ src/optional/python-bindings/utils.h | 57 ++++++ 7 files changed, 283 insertions(+), 8 deletions(-) create mode 100644 src/optional/python-bindings/utils.c create mode 100644 src/optional/python-bindings/utils.h diff --git a/src/optional/python-bindings/Makefile.am b/src/optional/python-bindings/Makefile.am index 25c7fa162a..6f5874e9e1 100644 --- a/src/optional/python-bindings/Makefile.am +++ b/src/optional/python-bindings/Makefile.am @@ -12,15 +12,22 @@ pkgpython_PYTHON = \ pkgpyexec_LTLIBRARIES = _gnucash_core_c.la _gnucash_core_c_la_SOURCES = \ - gnucash_core.c + gnucash_core.c utils.c _gnucash_core_c_la_CPPFLAGS = \ $(PYTHON_CPPFLAGS) \ $(GLIB_CFLAGS) \ + $(GNOME_CFLAGS) \ -I${top_srcdir}/src/libqof/qof \ -I$(top_srcdir)/src \ -I$(top_srcdir)/src/engine \ - -I$(top_srcdir)/src/business/business-core + -I$(top_srcdir)/src/business/business-core \ + -I${top_srcdir}/src/gnome-utils \ + -I${top_srcdir}/src/app-utils \ + -I${top_srcdir}/src/gnc-module \ + -I${top_srcdir}/src/gnome \ + -I${top_srcdir}/src/core-utils \ + -I${top_srcdir}/src/gnc-module # Suppress all warnings for now, but we really only need to -Wno-implicit AM_CFLAGS = -w @@ -29,6 +36,8 @@ _gnucash_core_c_la_LDFLAGS = -avoid-version -module _gnucash_core_c_la_LIBADD = \ ${GLIB_LIBS} \ + ${GNOME_LIBS} \ + ${GLADE_LIBS} \ ${top_builddir}/src/libqof/qof/libgnc-qof.la \ ${top_builddir}/src/gnc-module/libgnc-module.la \ ${top_builddir}/src/engine/libgncmod-engine.la \ @@ -60,3 +69,4 @@ EXTRA_DIST = \ glib.i MAINTAINERCLEANFILES = gnucash-core.c + diff --git a/src/optional/python-bindings/example_scripts/simple_business_create.py b/src/optional/python-bindings/example_scripts/simple_business_create.py index f4a697243a..78f46c38a2 100644 --- a/src/optional/python-bindings/example_scripts/simple_business_create.py +++ b/src/optional/python-bindings/example_scripts/simple_business_create.py @@ -147,6 +147,16 @@ new_customer.ApplyPayment(None, a2, a6, GncNumeric(100,100), new_customer.ApplyPayment(invoice_customer, a2, a6, GncNumeric(7,100), GncNumeric(1), datetime.date.today(), "", "") + +vendor_bill_returns = book.BillLoookupByID("7") +assert( vendor_bill_returns.GetID() == "7" ) +vendor_extract = vendor_bill_returns.GetOwner() +assert( vendor_extract.GetName() == new_vendor.GetName() ) +customer_invoice_returns = book.InvoiceLookupByID("5") +assert( customer_invoice_returns.GetID() == "5" ) +customer_returns = book.CustomerLookupByID("1") +assert( customer_returns.GetName() == new_customer.GetName() ) + s.save() s.end() diff --git a/src/optional/python-bindings/example_scripts/simple_invoice_insert.py b/src/optional/python-bindings/example_scripts/simple_invoice_insert.py index 1417db3460..db833c4eb0 100644 --- a/src/optional/python-bindings/example_scripts/simple_invoice_insert.py +++ b/src/optional/python-bindings/example_scripts/simple_invoice_insert.py @@ -82,10 +82,7 @@ root = book.get_root_account() commod_table = book.get_table() CAD = commod_table.lookup('CURRENCY', 'CAD') -my_customer_guid = GUID() -result = string_to_guid(argv[2], my_customer_guid.get_instance()) -assert( result ) -my_customer = book.CustomerLookup(my_customer_guid) +my_customer = book.LookupByID(arg[2]) assert( my_customer != None ) assert( isinstance(my_customer, Customer) ) diff --git a/src/optional/python-bindings/gnucash_core.i b/src/optional/python-bindings/gnucash_core.i index df5b19b487..82d1a5ea9e 100644 --- a/src/optional/python-bindings/gnucash_core.i +++ b/src/optional/python-bindings/gnucash_core.i @@ -51,11 +51,14 @@ #include "gncVendor.h" #include "gncAddress.h" #include "gncBillTerm.h" + #include "gncOwner.h" #include "gncInvoice.h" #include "gncJob.h" #include "gncEntry.h" #include "gncTaxTable.h" +#include "utils.h" + %} %include @@ -174,15 +177,18 @@ %include %include %include +%include "utils.h" + %init %{ - qof_log_init(); qof_init(); gnc_module_system_init(); char * no_args[1] = { NULL }; gnc_engine_init_static(0, no_args); + gnc_module_init_backend_xml(); gnc_module_init_backend_dbi(); %} + diff --git a/src/optional/python-bindings/gnucash_core.py b/src/optional/python-bindings/gnucash_core.py index 43fcac3c7c..bb0ba21a31 100644 --- a/src/optional/python-bindings/gnucash_core.py +++ b/src/optional/python-bindings/gnucash_core.py @@ -31,7 +31,8 @@ from function_class import \ from gnucash_core_c import gncInvoiceLookup, gncInvoiceGetInvoiceFromTxn, \ gncInvoiceGetInvoiceFromLot, gncEntryLookup, gncInvoiceLookup, \ gncCustomerLookup, gncVendorLookup, gncJobLookup, gncEmployeeLookup, \ - gncTaxTableLookup, gncTaxTableLookupByName + gncTaxTableLookup, gncTaxTableLookupByName, search_invoice_on_id, \ + search_customer_on_id, search_bill_on_id class GnuCashCoreClass(ClassFromFunctions): @@ -188,6 +189,21 @@ class Book(GnuCashCoreClass): return self.do_lookup_create_oo_instance( gncTaxTableLookupByName, TaxTable, name) + def BillLoookupByID(self, id): + from gnucash_business import Bill + return self.do_lookup_create_oo_instance( + search_bill_on_id, Bill, id) + + def InvoiceLookupByID(self, id): + from gnucash_business import Invoice + return self.do_lookup_create_oo_instance( + search_invoice_on_id, Invoice, id) + + def CustomerLookupByID(self, id): + from gnucash_business import Customer + return self.do_lookup_create_oo_instance( + search_customer_on_id, Customer, id) + class GncNumeric(GnuCashCoreClass): """Object used by GnuCash to store all numbers. Always consists of a numerator and denominator. diff --git a/src/optional/python-bindings/utils.c b/src/optional/python-bindings/utils.c new file mode 100644 index 0000000000..f3710dda3d --- /dev/null +++ b/src/optional/python-bindings/utils.c @@ -0,0 +1,179 @@ +/** utils.c +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +* MA 02110-1301, USA. +* +* Developed (aka copied?) from code written by Sebastian Held +* as part of his GnuCash invoice importer module +* Mike Evans +* +**********************************************************************/ + +#include "utils.h" + + +/*********************************************************************** + * Search the book for a Customer with the same ID. If it exists return a + * Customer object, if nit then return NULL. + @param QofBook The book + @param gchar ID of the Customer + @return GncCustomer * Pointer to the customer or NULL of there is no customer + * + **********************************************************************/ +GncCustomer * +search_customer_on_id (QofBook * book, const gchar *id) +{ + QueryNew *q; + GNCIdType type = GNC_CUSTOMER_MODULE_NAME; + GList *result; // do not free this! + QueryPredData_t string_pred_data; + GncCustomer *customer = NULL; + gint len; + + g_return_val_if_fail (id, NULL); + g_return_val_if_fail (book, NULL); + + // Build the query + q = gncQueryCreateFor (type); + gncQuerySetBook (q, book); + + // Search only the customer id field + string_pred_data = gncQueryStringPredicate (COMPARE_EQUAL, id, STRING_MATCH_NORMAL, FALSE); + gncQueryAddTerm (q, gncQueryBuildParamList(CUSTOMER_ID), string_pred_data, QUERY_AND); + + // Run the query + result = gncQueryRun (q); + + // now compare _exactly_ + len = g_list_length (result); + if (result && (len>0)) { + result = g_list_first (result); + while (result) { + GncCustomer *c = result->data; + if (strcmp(id,gncCustomerGetID(c)) == 0) { + // correct id found + customer = c; + break; + } + result = g_list_next (result); + } + } + + gncQueryDestroy (q); + return customer; +} + +/*********************************************************************** + * Search the book for an Invoice with the same ID. If it exists return an + * Invoice object, if not then return NULL. + @param QofBook The book + @param gchar ID of the invoice + @return GncCustomer * Pointer to the Invoice or NULL of there is no customer + * + **********************************************************************/ +GncInvoice * +search_invoice_on_id(QofBook *book, const gchar *id) +{ + QueryNew *q; + GNCIdType type = GNC_INVOICE_MODULE_NAME; + GList *result; // do not free this! + QueryPredData_t string_pred_data; + GncInvoice *invoice = NULL; + gint len; + + g_return_val_if_fail (id, NULL); + g_return_val_if_fail (book, NULL); + + // Build the query + q = gncQueryCreateFor (type); + gncQuerySetBook (q, book); + + // Search only the invoice id field + string_pred_data = gncQueryStringPredicate (COMPARE_EQUAL, id, STRING_MATCH_NORMAL, FALSE); + gncQueryAddTerm (q, gncQueryBuildParamList(INVOICE_ID), string_pred_data, QUERY_AND); + + // Run the query + result = gncQueryRun (q); + + // now compare _exactly_ + len = g_list_length (result); + if (result && (len>0)) { + result = g_list_first (result); + while (result) { + GncInvoice *c = result->data; + if (strcmp(id,gncInvoiceGetID(c)) == 0) { + // correct id found + invoice = c; + break; + } + result = g_list_next (result); + } + } + + gncQueryDestroy (q); + return invoice; +} + + +/*********************************************************************** + * Search the book for a Bill with the same ID. If it exists return an + * Invoice object, if not then return NULL. + @param QofBook The book + @param gchar ID of the invoice + @return GncCustomer * Pointer to the Invoice or NULL of there is no customer + * + **********************************************************************/ +GncInvoice * +search_bill_on_id(QofBook *book, const gchar *id) +{ + QueryNew *q; + GNCIdType type = GNC_INVOICE_MODULE_NAME; + GList *result; // do not free this! + QueryPredData_t string_pred_data; + GncInvoice *bill = NULL; + gint len; + + g_return_val_if_fail (id, NULL); + g_return_val_if_fail (book, NULL); + + // Build the query + q = gncQueryCreateFor (type); + gncQuerySetBook (q, book); + + // Search only the invoice id field + string_pred_data = gncQueryStringPredicate (COMPARE_EQUAL, id, STRING_MATCH_NORMAL, FALSE); + gncQueryAddTerm (q, gncQueryBuildParamList(INVOICE_ID), string_pred_data, QUERY_AND); + + // Run the query + result = gncQueryRun (q); + + // now compare _exactly_ + len = g_list_length (result); + if (result && (len>0)) { + result = g_list_first (result); + while (result) { + GncInvoice *c = result->data; + if (strcmp(id,gncInvoiceGetID(c)) == 0) { + // correct id found + bill = c; + break; + } + result = g_list_next (result); + } + } + + gncQueryDestroy (q); + return bill; +} diff --git a/src/optional/python-bindings/utils.h b/src/optional/python-bindings/utils.h new file mode 100644 index 0000000000..36eb31db7b --- /dev/null +++ b/src/optional/python-bindings/utils.h @@ -0,0 +1,57 @@ +/** utils.h +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +* MA 02110-1301, USA. +* +* Developed from code written by Sebastian Held +* as part of his invoice importer module +* Mike Evans +* +**********************************************************************/ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include + +#include "gnc-ui.h" +#include "gnc-ui-util.h" +#include "gnome-utils/gnc-gui-query.h" +#include "gncAddress.h" +#include "gncCustomerP.h" +#include "gncCustomer.h" +#include "gncInvoice.h" +#include "gnc-exp-parser.h" + +// query +#include "QueryCore.h" +#include "QueryNew.h" +#include "GNCId.h" + + + +#ifndef GNC_PLUGIN_invoice_import_invoice_import_H +#define GNC_PLUGIN_invoice_import_invoice_import_H + +GncCustomer * search_customer_on_id (QofBook *book, const gchar *id); +GncInvoice * search_invoice_on_id (QofBook *book, const gchar *id); +GncInvoice * search_bill_on_id (QofBook *book, const gchar *id); + +#endif