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
This commit is contained in:
Christian Stimming 2010-08-14 20:48:18 +00:00
parent f8df02cdb4
commit 5fbfa23dbb
7 changed files with 283 additions and 8 deletions

View File

@ -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

View File

@ -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()

View File

@ -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) )

View File

@ -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();
%}

View File

@ -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.

View File

@ -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@gmx.de>
* as part of his GnuCash invoice importer module
* Mike Evans <mikee@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;
}

View File

@ -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@gmx.de>
* as part of his invoice importer module
* Mike Evans <mikee@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