GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gncIDSearch.c
1 
24 #include "gncIDSearch.h"
25 
26 typedef enum
27 { UNDEFINED,
28  CUSTOMER,
29  VENDOR,
30  INVOICE,
31  BILL
32 }GncSearchType;
33 
34 static void * search(QofBook * book, const gchar *id, void * object, GncSearchType type);
35 static QofLogModule log_module = G_LOG_DOMAIN;
36 /***********************************************************************
37  * Search the book for a Customer/Invoice/Bill with the same ID.
38  * If it exists return a valid object, if not then returns NULL.
39  @param QofBook The book
40  @param gchar ID of the Customer
41  @return GncCustomer * Pointer to the customer or NULL of there is no customer
42  **********************************************************************/
43 
44 
46 gnc_search_customer_on_id (QofBook * book, const gchar *id)
47 {
48  GncCustomer *customer = NULL;
49  GncSearchType type = CUSTOMER;
50  customer = (GncCustomer*)search(book, id, customer, type);
51  return customer;
52 }
53 
54 GncInvoice *
55 gnc_search_invoice_on_id (QofBook * book, const gchar *id)
56 {
57  GncInvoice *invoice = NULL;
58  GncSearchType type = INVOICE;
59  invoice = (GncInvoice*)search(book, id, invoice, type);
60  return invoice;
61 }
62 
63 /* Essentially identical to above.*/
64 GncInvoice *
65 gnc_search_bill_on_id (QofBook * book, const gchar *id)
66 {
67  GncInvoice *bill = NULL;
68  GncSearchType type = BILL;
69  bill = (GncInvoice*)search(book, id, bill, type);
70  return bill;
71 }
72 
73 GncVendor *
74 gnc_search_vendor_on_id (QofBook * book, const gchar *id)
75 {
76  GncVendor *vendor = NULL;
77  GncSearchType type = VENDOR;
78  vendor = (GncVendor*)search(book, id, vendor, type);
79  return vendor;
80 }
81 
82 
83 /******************************************************************
84  * Generic search called after setting up stuff
85  * DO NOT call directly but type tests should fail anyway
86  ****************************************************************/
87 static void * search(QofBook * book, const gchar *id, void * object, GncSearchType type)
88 {
89  void *c;
90  GList *result;
91  QofQuery *q;
92  gint len;
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  len = g_list_length (result);
127  if (result && (len > 0))
128  {
129  result = g_list_first (result);
130 
131  while (result)
132  {
133  c = result->data;
134 
135  if (type == CUSTOMER && strcmp(id, gncCustomerGetID(c)) == 0)
136  {
137  // correct id found
138  object = c;
139  break;
140  }
141  else if (type == INVOICE && strcmp(id, gncInvoiceGetID(c)) == 0
142  && gncInvoiceGetType(c) == GNC_INVOICE_CUST_INVOICE)
143  {
144  object = c;
145  break;
146  }
147  else if (type == BILL && strcmp(id, gncInvoiceGetID(c)) == 0
148  && gncInvoiceGetType(c) == GNC_INVOICE_VEND_INVOICE)
149  {
150  object = c;
151  break;
152  }
153  else if (type == VENDOR && strcmp(id, gncVendorGetID(c)) == 0)
154  {
155  object = c;
156  break;
157  }
158  result = g_list_next (result);
159  }
160  }
161  qof_query_destroy (q);
162  return object;
163 }
void qof_query_add_term(QofQuery *query, QofQueryParamList *param_list, QofQueryPredData *pred_data, QofQueryOp op)
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
#define PINFO(format, args...)
Definition: qoflog.h:249
struct _QofQuery QofQuery
Definition: qofquery.h:90
void qof_query_destroy(QofQuery *q)
void qof_query_set_book(QofQuery *q, QofBook *book)
GList * qof_query_run(QofQuery *query)
QofQuery * qof_query_create(void)
void qof_query_search_for(QofQuery *query, QofIdTypeConst obj_type)
const gchar * QofLogModule
Definition: qofid.h:89