Line data Source code
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 *
47 0 : gnc_search_customer_on_id (QofBook * book, const gchar *id)
48 : {
49 0 : GncCustomer *customer = NULL;
50 0 : GncSearchType type = CUSTOMER;
51 0 : customer = (GncCustomer*)search(book, id, customer, type);
52 0 : return customer;
53 : }
54 :
55 : GncInvoice *
56 0 : gnc_search_invoice_on_id (QofBook * book, const gchar *id)
57 : {
58 0 : GncInvoice *invoice = NULL;
59 0 : GncSearchType type = INVOICE;
60 0 : invoice = (GncInvoice*)search(book, id, invoice, type);
61 0 : return invoice;
62 : }
63 :
64 : /* Essentially identical to above.*/
65 : GncInvoice *
66 0 : gnc_search_bill_on_id (QofBook * book, const gchar *id)
67 : {
68 0 : GncInvoice *bill = NULL;
69 0 : GncSearchType type = BILL;
70 0 : bill = (GncInvoice*)search(book, id, bill, type);
71 0 : return bill;
72 : }
73 :
74 : GncVendor *
75 0 : gnc_search_vendor_on_id (QofBook * book, const gchar *id)
76 : {
77 0 : GncVendor *vendor = NULL;
78 0 : GncSearchType type = VENDOR;
79 0 : vendor = (GncVendor*)search(book, id, vendor, type);
80 0 : 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 0 : 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 0 : PINFO("Type = %d", type);
96 0 : g_return_val_if_fail (type, NULL);
97 0 : g_return_val_if_fail (id, NULL);
98 0 : g_return_val_if_fail (book, NULL);
99 :
100 : // Build the query
101 0 : q = qof_query_create ();
102 0 : qof_query_set_book (q, book);
103 : // Search only the id field
104 0 : string_pred_data = qof_query_string_predicate (QOF_COMPARE_EQUAL, id, QOF_STRING_MATCH_NORMAL, FALSE);
105 0 : if (type == CUSTOMER)
106 : {
107 0 : qof_query_search_for(q,GNC_CUSTOMER_MODULE_NAME);
108 0 : qof_query_add_term (q, qof_query_build_param_list("CUSTOMER_ID"), string_pred_data, QOF_QUERY_AND);
109 : }
110 0 : else if (type == INVOICE || type == BILL)
111 : {
112 0 : qof_query_search_for(q,GNC_INVOICE_MODULE_NAME);
113 0 : qof_query_add_term (q, qof_query_build_param_list("INVOICE_ID"), string_pred_data, QOF_QUERY_AND);
114 : }
115 0 : else if (type == VENDOR)
116 : {
117 0 : qof_query_search_for(q,GNC_VENDOR_MODULE_NAME);
118 0 : 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 0 : result = qof_query_run (q);
124 :
125 : // now compare _exactly_
126 0 : if (gnc_list_length_cmp (result, 0))
127 : {
128 0 : result = g_list_first (result);
129 :
130 0 : while (result)
131 : {
132 0 : c = result->data;
133 :
134 0 : if (type == CUSTOMER && strcmp(id, gncCustomerGetID(c)) == 0)
135 : {
136 : // correct id found
137 0 : object = c;
138 0 : break;
139 : }
140 0 : else if (type == INVOICE && strcmp(id, gncInvoiceGetID(c)) == 0
141 0 : && gncInvoiceGetType(c) == GNC_INVOICE_CUST_INVOICE)
142 : {
143 0 : object = c;
144 0 : break;
145 : }
146 0 : else if (type == BILL && strcmp(id, gncInvoiceGetID(c)) == 0
147 0 : && gncInvoiceGetType(c) == GNC_INVOICE_VEND_INVOICE)
148 : {
149 0 : object = c;
150 0 : break;
151 : }
152 0 : else if (type == VENDOR && strcmp(id, gncVendorGetID(c)) == 0)
153 : {
154 0 : object = c;
155 0 : break;
156 : }
157 0 : result = g_list_next (result);
158 : }
159 : }
160 0 : qof_query_destroy (q);
161 0 : return object;
162 : }
|