r19431 - gnucash/trunk/src/optional/python-bindings - Bug #625193: Added 'search by ID' in python binding for invoices, customers and bills.

Christian Stimming cstim at code.gnucash.org
Sat Aug 14 16:48:18 EDT 2010


Author: cstim
Date: 2010-08-14 16:48:18 -0400 (Sat, 14 Aug 2010)
New Revision: 19431
Trac: http://svn.gnucash.org/trac/changeset/19431

Added:
   gnucash/trunk/src/optional/python-bindings/utils.c
   gnucash/trunk/src/optional/python-bindings/utils.h
Modified:
   gnucash/trunk/src/optional/python-bindings/Makefile.am
   gnucash/trunk/src/optional/python-bindings/example_scripts/simple_business_create.py
   gnucash/trunk/src/optional/python-bindings/example_scripts/simple_invoice_insert.py
   gnucash/trunk/src/optional/python-bindings/gnucash_core.i
   gnucash/trunk/src/optional/python-bindings/gnucash_core.py
Log:
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.

Modified: gnucash/trunk/src/optional/python-bindings/Makefile.am
===================================================================
--- gnucash/trunk/src/optional/python-bindings/Makefile.am	2010-08-14 20:48:05 UTC (rev 19430)
+++ gnucash/trunk/src/optional/python-bindings/Makefile.am	2010-08-14 20:48:18 UTC (rev 19431)
@@ -12,15 +12,22 @@
 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_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 @@
   glib.i
 
 MAINTAINERCLEANFILES = gnucash-core.c
+

Modified: gnucash/trunk/src/optional/python-bindings/example_scripts/simple_business_create.py
===================================================================
--- gnucash/trunk/src/optional/python-bindings/example_scripts/simple_business_create.py	2010-08-14 20:48:05 UTC (rev 19430)
+++ gnucash/trunk/src/optional/python-bindings/example_scripts/simple_business_create.py	2010-08-14 20:48:18 UTC (rev 19431)
@@ -147,6 +147,16 @@
 
 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()

Modified: gnucash/trunk/src/optional/python-bindings/example_scripts/simple_invoice_insert.py
===================================================================
--- gnucash/trunk/src/optional/python-bindings/example_scripts/simple_invoice_insert.py	2010-08-14 20:48:05 UTC (rev 19430)
+++ gnucash/trunk/src/optional/python-bindings/example_scripts/simple_invoice_insert.py	2010-08-14 20:48:18 UTC (rev 19431)
@@ -82,10 +82,7 @@
 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) )
 

Modified: gnucash/trunk/src/optional/python-bindings/gnucash_core.i
===================================================================
--- gnucash/trunk/src/optional/python-bindings/gnucash_core.i	2010-08-14 20:48:05 UTC (rev 19430)
+++ gnucash/trunk/src/optional/python-bindings/gnucash_core.i	2010-08-14 20:48:18 UTC (rev 19431)
@@ -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 <timespec.i>
@@ -174,15 +177,18 @@
 %include <gncJob.h>
 %include <gncEntry.h>
 %include <gncTaxTable.h>
+%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();
 %}
+

Modified: gnucash/trunk/src/optional/python-bindings/gnucash_core.py
===================================================================
--- gnucash/trunk/src/optional/python-bindings/gnucash_core.py	2010-08-14 20:48:05 UTC (rev 19430)
+++ gnucash/trunk/src/optional/python-bindings/gnucash_core.py	2010-08-14 20:48:18 UTC (rev 19431)
@@ -31,7 +31,8 @@
 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 @@
         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.

Added: gnucash/trunk/src/optional/python-bindings/utils.c
===================================================================
--- gnucash/trunk/src/optional/python-bindings/utils.c	                        (rev 0)
+++ gnucash/trunk/src/optional/python-bindings/utils.c	2010-08-14 20:48:18 UTC (rev 19431)
@@ -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 <sebastian.held at gmx.de>
+* as part of his GnuCash invoice importer module
+* Mike Evans <mikee at saxicola.co.uk>
+*
+**********************************************************************/
+
+#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;
+}


Property changes on: gnucash/trunk/src/optional/python-bindings/utils.c
___________________________________________________________________
Added: svn:eol-style
   + LF

Added: gnucash/trunk/src/optional/python-bindings/utils.h
===================================================================
--- gnucash/trunk/src/optional/python-bindings/utils.h	                        (rev 0)
+++ gnucash/trunk/src/optional/python-bindings/utils.h	2010-08-14 20:48:18 UTC (rev 19431)
@@ -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 <sebastian.held at gmx.de>
+* as part of his invoice importer module
+* Mike Evans <mikee at saxicola.co.uk>
+*
+**********************************************************************/
+
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <glib/gi18n.h>
+#include <regex.h>
+#include <glib.h>
+#include <glib/gstdio.h>
+
+#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


Property changes on: gnucash/trunk/src/optional/python-bindings/utils.h
___________________________________________________________________
Added: svn:eol-style
   + LF



More information about the gnucash-changes mailing list