From 4099500af1a15348bcb1d366297f34ce158472b9 Mon Sep 17 00:00:00 2001 From: Geert Janssens Date: Tue, 31 Aug 2010 17:06:21 +0000 Subject: [PATCH] Add vendor search to python bindings and refactors files gncIDSearch.c/h with vendor search. Patch by Mike Evans. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@19522 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/engine/gncIDSearch.c | 174 +++++++------------ src/engine/gncIDSearch.h | 12 +- src/optional/python-bindings/gnucash_core.py | 15 +- 3 files changed, 79 insertions(+), 122 deletions(-) diff --git a/src/engine/gncIDSearch.c b/src/engine/gncIDSearch.c index 3237feebb3..18cff64962 100644 --- a/src/engine/gncIDSearch.c +++ b/src/engine/gncIDSearch.c @@ -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); - } - } - - 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; + 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; + } + +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; +} + + +/****************************************************************** + * 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; + 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 @@ search_bill_on_id(QofBook *book, const gchar *id) 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 @@ search_bill_on_id(QofBook *book, const gchar *id) 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; } diff --git a/src/engine/gncIDSearch.h b/src/engine/gncIDSearch.h index d1b377bf73..505621abe3 100644 --- a/src/engine/gncIDSearch.h +++ b/src/engine/gncIDSearch.h @@ -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 diff --git a/src/optional/python-bindings/gnucash_core.py b/src/optional/python-bindings/gnucash_core.py index bb0ba21a31..6c09f21a7a 100644 --- a/src/optional/python-bindings/gnucash_core.py +++ b/src/optional/python-bindings/gnucash_core.py @@ -31,8 +31,8 @@ from function_class import \ 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,17 +192,22 @@ class Book(GnuCashCoreClass): 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