GnuCash  5.6-150-g038405b370+
gncIDSearch.c
1 
24 #include "gncIDSearch.h"
25 #include <gnc-glib-utils.h>
26 
27 typedef enum
28 { UNDEFINED,
29  CUSTOMER,
30  VENDOR,
31  INVOICE,
32  BILL
33 }GncSearchType;
34 
35 static void * search(QofBook * book, const gchar *id, void * object, GncSearchType type);
36 static 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 
47 gnc_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 
55 GncInvoice *
56 gnc_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.*/
65 GncInvoice *
66 gnc_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 
74 GncVendor *
75 gnc_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  ****************************************************************/
88 static 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  }
160  qof_query_destroy (q);
161  return object;
162 }
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
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
#define PINFO(format, args...)
Print an informational note.
Definition: qoflog.h:256
void qof_query_destroy(QofQuery *query)
Frees the resources associate with a Query object.
void qof_query_set_book(QofQuery *query, QofBook *book)
Set the book to be searched.
credit, discount and shipaddr are unique to GncCustomer id, name, notes, terms, addr, currency, taxtable, taxtable_override taxincluded, active and jobs are identical to ::GncVendor.
GLib helper routines.
GList * qof_query_run(QofQuery *query)
Perform the query, return the results.
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...
QofQuery * qof_query_create(void)
Create a new query.
Definition: qofquery.cpp:918
A Query.
Definition: qofquery.cpp:74
void qof_query_search_for(QofQuery *q, QofIdTypeConst obj_type)
Set the object type to be searched for.
Definition: qofquery.cpp:926