1 /** utils.c
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16 * MA 02110-1301, USA.
17 *
18 * Developed (aka copied?) from code written by Sebastian Held <sebastian.held@gmx.de>
19 * as part of his GnuCash invoice importer module
20 * Mike Evans <mikee@saxicola.co.uk>
21 *
22 **********************************************************************/
23
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
46 GncCustomer *
gnc_search_customer_on_id(QofBook * book,const gchar * id)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 *
gnc_search_invoice_on_id(QofBook * book,const gchar * id)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 *
gnc_search_bill_on_id(QofBook * book,const gchar * id)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 *
gnc_search_vendor_on_id(QofBook * book,const gchar * id)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 ****************************************************************/
search(QofBook * book,const gchar * id,void * object,GncSearchType type)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 }
163