GnuCash c935c2f+
Loading...
Searching...
No Matches
gncIDSearch.c
1
24#include "gncIDSearch.h"
25#include <gnc-glib-utils.h>
26
27typedef enum
28{ UNDEFINED,
29 CUSTOMER,
30 VENDOR,
31 INVOICE,
32 BILL
33}GncSearchType;
34
35static void * search(QofBook * book, const gchar *id, void * object, GncSearchType type);
36static QofLogModule log_module = G_LOG_DOMAIN;
37/***********************************************************************
38 * Search the book for a Customer/Invoice/Bill with the same ID.
39 * If it exists return a valid object, if not then returns NULL.
40 @param QofBook The book
41 @param gchar ID of the Customer
42 @return GncCustomer * Pointer to the customer or NULL of there is no customer
43 **********************************************************************/
44
45
47gnc_search_customer_on_id (QofBook * book, const gchar *id)
48{
49 GncCustomer *customer = NULL;
50 GncSearchType type = CUSTOMER;
51 customer = (GncCustomer*)search(book, id, customer, type);
52 return customer;
53}
54
55GncInvoice *
56gnc_search_invoice_on_id (QofBook * book, const gchar *id)
57{
58 GncInvoice *invoice = NULL;
59 GncSearchType type = INVOICE;
60 invoice = (GncInvoice*)search(book, id, invoice, type);
61 return invoice;
62}
63
64/* Essentially identical to above.*/
65GncInvoice *
66gnc_search_bill_on_id (QofBook * book, const gchar *id)
67{
68 GncInvoice *bill = NULL;
69 GncSearchType type = BILL;
70 bill = (GncInvoice*)search(book, id, bill, type);
71 return bill;
72}
73
74GncVendor *
75gnc_search_vendor_on_id (QofBook * book, const gchar *id)
76{
77 GncVendor *vendor = NULL;
78 GncSearchType type = VENDOR;
79 vendor = (GncVendor*)search(book, id, vendor, type);
80 return vendor;
81}
82
83
84/******************************************************************
85 * Generic search called after setting up stuff
86 * DO NOT call directly but type tests should fail anyway
87 ****************************************************************/
88static void * search(QofBook * book, const gchar *id, void * object, GncSearchType type)
89{
90 void *c;
91 GList *result;
92 QofQuery *q;
93 QofQueryPredData* string_pred_data;
94
95 PINFO("Type = %d", type);
96 g_return_val_if_fail (type, NULL);
97 g_return_val_if_fail (id, NULL);
98 g_return_val_if_fail (book, NULL);
99
100 // Build the query
101 q = qof_query_create ();
102 qof_query_set_book (q, book);
103 // Search only the id field
104 string_pred_data = qof_query_string_predicate (QOF_COMPARE_EQUAL, id, QOF_STRING_MATCH_NORMAL, FALSE);
105 if (type == CUSTOMER)
106 {
107 qof_query_search_for(q,GNC_CUSTOMER_MODULE_NAME);
108 qof_query_add_term (q, qof_query_build_param_list("CUSTOMER_ID"), string_pred_data, QOF_QUERY_AND);
109 }
110 else if (type == INVOICE || type == BILL)
111 {
112 qof_query_search_for(q,GNC_INVOICE_MODULE_NAME);
113 qof_query_add_term (q, qof_query_build_param_list("INVOICE_ID"), string_pred_data, QOF_QUERY_AND);
114 }
115 else if (type == VENDOR)
116 {
117 qof_query_search_for(q,GNC_VENDOR_MODULE_NAME);
118 qof_query_add_term (q, qof_query_build_param_list("VENDOR_ID"), string_pred_data, QOF_QUERY_AND);
119 }
120
121
122 // Run the query
123 result = qof_query_run (q);
124
125 // now compare _exactly_
126 if (gnc_list_length_cmp (result, 0))
127 {
128 result = g_list_first (result);
129
130 while (result)
131 {
132 c = result->data;
133
134 if (type == CUSTOMER && strcmp(id, gncCustomerGetID(c)) == 0)
135 {
136 // correct id found
137 object = c;
138 break;
139 }
140 else if (type == INVOICE && strcmp(id, gncInvoiceGetID(c)) == 0
141 && gncInvoiceGetType(c) == GNC_INVOICE_CUST_INVOICE)
142 {
143 object = c;
144 break;
145 }
146 else if (type == BILL && strcmp(id, gncInvoiceGetID(c)) == 0
147 && gncInvoiceGetType(c) == GNC_INVOICE_VEND_INVOICE)
148 {
149 object = c;
150 break;
151 }
152 else if (type == VENDOR && strcmp(id, gncVendorGetID(c)) == 0)
153 {
154 object = c;
155 break;
156 }
157 result = g_list_next (result);
158 }
159 }
161 return object;
162}
GLib helper routines.
gint gnc_list_length_cmp(const GList *list, size_t len)
Scans the GList elements the minimum number of iterations required to test it against a specified siz...
#define PINFO(format, args...)
Print an informational note.
Definition qoflog.h:256
void qof_query_add_term(QofQuery *q, QofQueryParamList *param_list, QofQueryPredData *pred_data, QofQueryOp op)
This is the general function that adds a new Query Term to a query.
Definition qofquery.cpp:681
void qof_query_set_book(QofQuery *query, QofBook *book)
Set the book to be searched.
void qof_query_destroy(QofQuery *query)
Frees the resources associate with a Query object.
QofQuery * qof_query_create(void)
Create a new query.
Definition qofquery.cpp:918
void qof_query_search_for(QofQuery *q, QofIdTypeConst obj_type)
Set the object type to be searched for.
Definition qofquery.cpp:926
GList * qof_query_run(QofQuery *query)
Perform the query, return the results.
credit, discount and shipaddr are unique to GncCustomer id, name, notes, terms, addr,...
QofBook reference.
Definition qofbook-p.hpp:47
A Query.
Definition qofquery.cpp:75