32 #include <glib/gi18n.h>
35 #include <glib/gstdio.h>
39 #include "gnc-gui-query.h"
41 #include "gncCustomerP.h"
42 #include "gncVendorP.h"
46 #include "gncIDSearch.h"
58 #define FILL_IN_HELPER(match_name,column) \
59 temp = g_match_info_fetch_named (match_info, match_name); \
63 gtk_list_store_set (store, &iter, column, temp, -1); \
66 customer_import_result
67 gnc_customer_import_read_file (
const gchar *filename,
const gchar *parser_regexp, GtkListStore *store, guint max_rows,
customer_import_stats *stats)
75 gchar *line_utf8, *temp;
76 GMatchInfo *match_info;
83 f = g_fopen( filename,
"rt" );
87 return CI_RESULT_OPEN_FAILED;
92 stats = &stats_fallback;
96 regexpat = g_regex_new (parser_regexp, G_REGEX_EXTENDED | G_REGEX_OPTIMIZE | G_REGEX_DUPNAMES, 0, &err);
102 errmsg = g_strdup_printf (_(
"Error in regular expression '%s':\n%s"),
103 parser_regexp, err->message);
107 dialog = gtk_message_dialog_new (NULL,
112 gtk_dialog_run (GTK_DIALOG (dialog));
113 gtk_widget_destroy(dialog);
118 return CI_RESULT_ERROR_IN_REGEXP;
122 stats->n_imported = 0;
123 stats->n_ignored = 0;
124 stats->ignored_lines = g_string_new (NULL);
125 #define buffer_size 1000
126 line = g_malloc0 (buffer_size);
127 while (!feof (f) && ((max_rows == 0) || (stats->n_imported + stats->n_ignored < max_rows)))
131 if (!fgets (line, buffer_size, f))
135 if ((l > 0) && (line[l - 1] ==
'\n'))
139 line_utf8 = g_locale_to_utf8 (line, -1, NULL, NULL, NULL);
143 if (g_regex_match (regexpat, line_utf8, 0, &match_info))
149 gtk_list_store_append (store, &iter);
150 FILL_IN_HELPER (
"id", CI_ID);
151 FILL_IN_HELPER (
"company", CI_COMPANY);
152 FILL_IN_HELPER (
"name", CI_NAME);
153 FILL_IN_HELPER (
"addr1", CI_ADDR1);
154 FILL_IN_HELPER (
"addr2", CI_ADDR2);
155 FILL_IN_HELPER (
"addr3", CI_ADDR3);
156 FILL_IN_HELPER (
"addr4", CI_ADDR4);
157 FILL_IN_HELPER (
"phone", CI_PHONE);
158 FILL_IN_HELPER (
"fax", CI_FAX);
159 FILL_IN_HELPER (
"email", CI_EMAIL);
160 FILL_IN_HELPER (
"notes", CI_NOTES);
161 FILL_IN_HELPER (
"shipname", CI_SHIPNAME);
162 FILL_IN_HELPER (
"shipaddr1", CI_SHIPADDR1);
163 FILL_IN_HELPER (
"shipaddr2", CI_SHIPADDR2);
164 FILL_IN_HELPER (
"shipaddr3", CI_SHIPADDR3);
165 FILL_IN_HELPER (
"shipaddr4", CI_SHIPADDR4);
166 FILL_IN_HELPER (
"shipphone", CI_SHIPPHONE);
167 FILL_IN_HELPER (
"shipfax", CI_SHIPFAX);
168 FILL_IN_HELPER (
"shipemail", CI_SHIPEMAIL);
174 g_string_append (stats->ignored_lines, line_utf8);
175 g_string_append_c (stats->ignored_lines,
'\n');
178 g_match_info_free (match_info);
186 g_regex_unref (regexpat);
192 if (stats == &stats_fallback)
194 g_string_free (stats->ignored_lines, TRUE);
202 gnc_customer_import_fix_customers (GtkListStore *store, guint *fixed, guint *deleted, gchar * type)
206 gchar *company, *name, *addr1, *addr2, *addr3, *addr4, *phone, *fax, *email,
207 *notes, *shipname, *shipaddr1, *shipaddr2, *shipaddr3, *shipaddr4,
208 *shipphone, *shipfax, *shipemail;
220 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(store), &iter);
224 gtk_tree_model_get (GTK_TREE_MODEL(store), &iter,
225 CI_COMPANY, &company,
235 CI_SHIPNAME, &shipname,
236 CI_SHIPADDR1, &shipaddr1,
237 CI_SHIPADDR2, &shipaddr2,
238 CI_SHIPADDR3, &shipaddr3,
239 CI_SHIPADDR4, &shipaddr4,
240 CI_SHIPPHONE, &shipphone,
241 CI_SHIPFAX, &shipfax,
242 CI_SHIPEMAIL, &shipemail,
245 if (strlen(company) == 0)
247 if (strlen(name) == 0)
250 gtk_list_store_remove (store, &iter);
257 gtk_list_store_set (store, &iter, CI_COMPANY, name, -1);
281 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL(store), &iter);
286 gnc_customer_import_create_customers (GtkListStore *store,
QofBook *book, guint *n_customers_created, guint *n_customers_updated, gchar * type)
290 gchar *id, *company, *name, *addr1, *addr2, *addr3, *addr4, *phone, *fax, *email;
291 gchar *notes, *shipname, *shipaddr1, *shipaddr2, *shipaddr3, *shipaddr4, *shipphone, *shipfax, *shipemail;
301 g_return_if_fail (store && book);
302 printf(
"\nTYPE = %s\n", type);
305 if (!n_customers_created)
306 n_customers_created = &dummy;
307 if (!n_customers_updated)
308 n_customers_updated = &dummy;
309 *n_customers_created = 0;
310 *n_customers_updated = 0;
312 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(store), &iter);
316 gtk_tree_model_get (GTK_TREE_MODEL(store), &iter,
318 CI_COMPANY, &company,
328 CI_SHIPNAME, &shipname,
329 CI_SHIPADDR1, &shipaddr1,
330 CI_SHIPADDR2, &shipaddr2,
331 CI_SHIPADDR3, &shipaddr3,
332 CI_SHIPADDR4, &shipaddr4,
333 CI_SHIPPHONE, &shipphone,
334 CI_SHIPFAX, &shipfax,
335 CI_SHIPEMAIL, &shipemail,
339 if (strlen (
id) == 0)
342 if (g_ascii_strcasecmp (type,
"CUSTOMER") == 0)
id = gncCustomerNextID (book);
343 else if (g_ascii_strcasecmp (type,
"VENDOR") == 0)
id = gncVendorNextID (book);
349 if (g_ascii_strcasecmp (type,
"CUSTOMER") == 0)
351 customer = gnc_search_customer_on_id (book,
id);
354 customer = gncCustomerCreate( book );
356 (*n_customers_created)++;
358 else (*n_customers_updated)++;
360 else if (g_ascii_strcasecmp (type,
"VENDOR") == 0)
362 vendor = gnc_search_vendor_on_id (book,
id);
365 vendor = gncVendorCreate( book );
367 (*n_customers_created)++;
369 else (*n_customers_updated)++;
372 if (g_ascii_strcasecmp (type,
"CUSTOMER") == 0)
374 gncCustomerBeginEdit (customer);
375 gncCustomerSetID (customer,
id);
376 gncCustomerSetName (customer, company);
377 gncCustomerSetNotes (customer, notes);
378 addr = gncCustomerGetAddr (customer);
379 shipaddr = gncCustomerGetShipAddr (customer);
381 else if (g_ascii_strcasecmp (type,
"VENDOR") == 0)
383 gncVendorBeginEdit (vendor);
384 gncVendorSetID (vendor,
id);
385 gncVendorSetName (vendor, company);
386 gncVendorSetNotes (vendor, notes);
387 addr = gncVendorGetAddr (vendor);
389 gncAddressSetName (addr, name);
390 gncAddressSetAddr1 (addr, addr1);
391 gncAddressSetAddr2 (addr, addr2);
392 gncAddressSetAddr3 (addr, addr3);
393 gncAddressSetAddr4 (addr, addr4);
394 gncAddressSetPhone (addr, phone);
395 gncAddressSetFax (addr, fax);
396 gncAddressSetEmail (addr, email);
397 if (g_ascii_strcasecmp (type,
"CUSTOMER") == 0)
399 gncAddressSetName (shipaddr, shipname);
400 gncAddressSetAddr1 (shipaddr, shipaddr1);
401 gncAddressSetAddr2 (shipaddr, shipaddr2);
402 gncAddressSetAddr3 (shipaddr, shipaddr3);
403 gncAddressSetAddr4 (shipaddr, shipaddr4);
404 gncAddressSetPhone (shipaddr, shipphone);
405 gncAddressSetFax (shipaddr, shipfax);
406 gncAddressSetEmail (shipaddr, shipemail);
407 gncCustomerSetActive (customer, TRUE);
408 gncCustomerCommitEdit (customer);
410 else if (g_ascii_strcasecmp (type,
"VENDOR") == 0)
412 gncVendorSetActive (vendor, TRUE);
413 gncVendorCommitEdit (vendor);
436 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL(store), &iter);
core import functions for customer import plugin
utility functions for the GnuCash UI
gnc_commodity * gnc_default_currency(void)