r19522 - gnucash/trunk/src - Add vendor search to python bindings and refactors files gncIDSearch.c/h with vendor search.
Geert Janssens
gjanssens at code.gnucash.org
Tue Aug 31 13:06:22 EDT 2010
Author: gjanssens
Date: 2010-08-31 13:06:21 -0400 (Tue, 31 Aug 2010)
New Revision: 19522
Trac: http://svn.gnucash.org/trac/changeset/19522
Modified:
gnucash/trunk/src/engine/gncIDSearch.c
gnucash/trunk/src/engine/gncIDSearch.h
gnucash/trunk/src/optional/python-bindings/gnucash_core.py
Log:
Add vendor search to python bindings and refactors files gncIDSearch.c/h with vendor search.
Patch by Mike Evans.
Modified: gnucash/trunk/src/engine/gncIDSearch.c
===================================================================
--- gnucash/trunk/src/engine/gncIDSearch.c 2010-08-31 16:48:12 UTC (rev 19521)
+++ gnucash/trunk/src/engine/gncIDSearch.c 2010-08-31 17:06:21 UTC (rev 19522)
@@ -23,128 +23,64 @@
#include "gncIDSearch.h"
-
+static void * search(QofBook * book, const gchar *id, void * object, GNCIdType type);
/***********************************************************************
- * Search the book for a Customer with the same ID. If it exists return a
- * Customer object, if nit then return NULL.
+ * Search the book for a Customer/Invoice/Bill with the same ID.
+ * If it exists return a valid object, if not then returns 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)
+gnc_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);
+ GNCIdType type = GNC_CUSTOMER_MODULE_NAME;
+ customer = (GncCustomer*)search(book, id, customer, type);
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)
+gnc_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);
+ GNCIdType type = GNC_INVOICE_MODULE_NAME;
+ invoice = (GncInvoice*)search(book,id, invoice, type);
+ return invoice;
}
+
+/* Essentially identical to above.*/
+GncInvoice *
+gnc_search_bill_on_id (QofBook * book, const gchar *id)
+{
+ GncInvoice *bill = NULL;
+ GNCIdType type = GNC_INVOICE_MODULE_NAME;
+ bill = (GncInvoice*)search(book, id, bill, type);
+ return bill;
}
- gncQueryDestroy (q);
- return invoice;
+GncVendor *
+gnc_search_vendor_on_id (QofBook * book, const gchar *id)
+{
+ GncVendor *vendor = NULL;
+ GNCIdType type = GNC_VENDOR_MODULE_NAME;
+ vendor = (GncVendor*)search(book, id, vendor, type);
+ return vendor;
}
-/***********************************************************************
- * 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)
+/******************************************************************
+ * Generic search called after setting up stuff
+ * DO NOT call directly but type tests should fail anyway
+ ****************************************************************/
+static void * search(QofBook * book, const gchar *id, void * object, GNCIdType type)
{
+ void *c;
+ GList *result;
QueryNew *q;
- GNCIdType type = GNC_INVOICE_MODULE_NAME;
- GList *result; // do not free this!
- QueryPredData_t string_pred_data;
- GncInvoice *bill = NULL;
gint len;
-
+ QueryPredData_t string_pred_data;
+ g_return_val_if_fail (type, NULL);
g_return_val_if_fail (id, NULL);
g_return_val_if_fail (book, NULL);
@@ -152,9 +88,24 @@
q = gncQueryCreateFor (type);
gncQuerySetBook (q, book);
- // Search only the invoice id field
+ // Search only the id field
string_pred_data = gncQueryStringPredicate (COMPARE_EQUAL, id, STRING_MATCH_NORMAL, FALSE);
+
+ if (strcmp(type,GNC_CUSTOMER_MODULE_NAME))
+ {
+ GncCustomer *c = NULL;
+ gncQueryAddTerm (q, gncQueryBuildParamList(CUSTOMER_ID), string_pred_data, QUERY_AND);
+ }
+ else if (strcmp(type,GNC_INVOICE_MODULE_NAME))
+ {
+ GncInvoice *c = NULL;
gncQueryAddTerm (q, gncQueryBuildParamList(INVOICE_ID), string_pred_data, QUERY_AND);
+ }
+ else if (strcmp(type,GNC_VENDOR_MODULE_NAME))
+ {
+ GncVendor *c = NULL;
+ gncQueryAddTerm (q, gncQueryBuildParamList(VENDOR_ID), string_pred_data, QUERY_AND);
+ }
// Run the query
result = gncQueryRun (q);
@@ -164,16 +115,15 @@
if (result && (len>0)) {
result = g_list_first (result);
while (result) {
- GncInvoice *c = result->data;
- if (strcmp(id,gncInvoiceGetID(c)) == 0) {
+ c = result->data;
+ if (strcmp(id,gncCustomerGetID(c)) == 0) {
// correct id found
- bill = c;
+ object = c;
break;
}
result = g_list_next (result);
}
}
-
gncQueryDestroy (q);
- return bill;
+ return object;
}
Modified: gnucash/trunk/src/engine/gncIDSearch.h
===================================================================
--- gnucash/trunk/src/engine/gncIDSearch.h 2010-08-31 16:48:12 UTC (rev 19521)
+++ gnucash/trunk/src/engine/gncIDSearch.h 2010-08-31 17:06:21 UTC (rev 19522)
@@ -36,12 +36,14 @@
#include "QueryNew.h"
#include "GNCId.h"
-#ifndef GNC_PLUGIN_invoice_import_invoice_import_H
-#define GNC_PLUGIN_invoice_import_invoice_import_H
+#ifndef GNC_invoice_import_invoice_import_H
+#define GNC_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);
+GncCustomer * gnc_search_customer_on_id (QofBook *book, const gchar *id);
+GncInvoice * gnc_search_invoice_on_id (QofBook *book, const gchar *id);
+GncInvoice * gnc_search_bill_on_id (QofBook *book, const gchar *id);
+GncVendor * gnc_search_vendor_on_id (QofBook *book, const gchar *id);
+
#endif
Modified: gnucash/trunk/src/optional/python-bindings/gnucash_core.py
===================================================================
--- gnucash/trunk/src/optional/python-bindings/gnucash_core.py 2010-08-31 16:48:12 UTC (rev 19521)
+++ gnucash/trunk/src/optional/python-bindings/gnucash_core.py 2010-08-31 17:06:21 UTC (rev 19522)
@@ -31,8 +31,8 @@
from gnucash_core_c import gncInvoiceLookup, gncInvoiceGetInvoiceFromTxn, \
gncInvoiceGetInvoiceFromLot, gncEntryLookup, gncInvoiceLookup, \
gncCustomerLookup, gncVendorLookup, gncJobLookup, gncEmployeeLookup, \
- gncTaxTableLookup, gncTaxTableLookupByName, search_invoice_on_id, \
- search_customer_on_id, search_bill_on_id
+ gncTaxTableLookup, gncTaxTableLookupByName, gnc_search_invoice_on_id, \
+ gnc_search_customer_on_id, gnc_search_bill_on_id , gnc_search_vendor_on_id
class GnuCashCoreClass(ClassFromFunctions):
@@ -192,18 +192,23 @@
def BillLoookupByID(self, id):
from gnucash_business import Bill
return self.do_lookup_create_oo_instance(
- search_bill_on_id, Bill, id)
+ gnc_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)
+ gnc_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)
+ gnc_search_customer_on_id, Customer, id)
+ def VendorLookupByID(self, id):
+ from gnucash_business import Vendor
+ return self.do_lookup_create_oo_instance(
+ gnc_search_vendor_on_id, Vendor, id)
+
class GncNumeric(GnuCashCoreClass):
"""Object used by GnuCash to store all numbers. Always consists of a
numerator and denominator.
More information about the gnucash-changes
mailing list