32#include <glib/gi18n.h>
35#include <glib/gstdio.h>
40#include "gnc-gui-query.h"
42#include "gncCustomerP.h"
43#include "gncVendorP.h"
47#include "gncIDSearch.h"
59#define FILL_IN_HELPER(match_name,column) \
60 temp = g_match_info_fetch_named (match_info, match_name); \
64 gtk_list_store_set (store, &iter, column, temp, -1); \
68gnc_customer_import_read_file (
const gchar *filename,
const gchar *parser_regexp, GtkListStore *store, guint max_rows, customer_import_stats *stats)
71 customer_import_stats stats_fallback;
76 gchar *line_utf8, *temp;
77 GMatchInfo *match_info;
84 f = g_fopen( filename,
"rt" );
88 return CI_RESULT_OPEN_FAILED;
93 stats = &stats_fallback;
97 regexpat = g_regex_new (parser_regexp, G_REGEX_EXTENDED | G_REGEX_OPTIMIZE | G_REGEX_DUPNAMES, 0, &err);
103 errmsg = g_strdup_printf (
_(
"Error in regular expression '%s':\n%s"),
104 parser_regexp, err->message);
108 dialog = gtk_message_dialog_new (NULL,
113 gtk_dialog_run (GTK_DIALOG (dialog));
114 gtk_widget_destroy(dialog);
119 return CI_RESULT_ERROR_IN_REGEXP;
123 stats->n_imported = 0;
124 stats->n_ignored = 0;
125 stats->ignored_lines = g_string_new (NULL);
126#define buffer_size 1000
127 line = g_malloc0 (buffer_size);
128 while (!feof (f) && ((max_rows == 0) || (stats->n_imported + stats->n_ignored < max_rows)))
132 if (!fgets (line, buffer_size, f))
136 if ((l > 0) && (line[l - 1] ==
'\n'))
141 if (g_utf8_validate(line, -1, NULL))
144 line_utf8 = g_locale_to_utf8 (line, -1, NULL, NULL, NULL);
151 if (g_regex_match (regexpat, line_utf8, 0, &match_info))
157 gtk_list_store_append (store, &iter);
158 FILL_IN_HELPER (
"id", CI_ID);
159 FILL_IN_HELPER (
"company", CI_COMPANY);
160 FILL_IN_HELPER (
"name", CI_NAME);
161 FILL_IN_HELPER (
"addr1", CI_ADDR1);
162 FILL_IN_HELPER (
"addr2", CI_ADDR2);
163 FILL_IN_HELPER (
"addr3", CI_ADDR3);
164 FILL_IN_HELPER (
"addr4", CI_ADDR4);
165 FILL_IN_HELPER (
"phone", CI_PHONE);
166 FILL_IN_HELPER (
"fax", CI_FAX);
167 FILL_IN_HELPER (
"email", CI_EMAIL);
168 FILL_IN_HELPER (
"notes", CI_NOTES);
169 FILL_IN_HELPER (
"shipname", CI_SHIPNAME);
170 FILL_IN_HELPER (
"shipaddr1", CI_SHIPADDR1);
171 FILL_IN_HELPER (
"shipaddr2", CI_SHIPADDR2);
172 FILL_IN_HELPER (
"shipaddr3", CI_SHIPADDR3);
173 FILL_IN_HELPER (
"shipaddr4", CI_SHIPADDR4);
174 FILL_IN_HELPER (
"shipphone", CI_SHIPPHONE);
175 FILL_IN_HELPER (
"shipfax", CI_SHIPFAX);
176 FILL_IN_HELPER (
"shipemail", CI_SHIPEMAIL);
182 g_string_append (stats->ignored_lines, line_utf8);
183 g_string_append_c (stats->ignored_lines,
'\n');
186 g_match_info_free (match_info);
187 if (line_utf8 != line)
193 g_regex_unref (regexpat);
197 if (stats == &stats_fallback)
199 g_string_free (stats->ignored_lines, TRUE);
207gnc_customer_import_fix_customers (GtkListStore *store, guint *fixed, guint *deleted, gchar * type)
211 gchar *company, *name, *addr1, *addr2, *addr3, *addr4;
223 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(store), &iter);
227 gtk_tree_model_get (GTK_TREE_MODEL(store), &iter,
228 CI_COMPANY, &company,
238 if (strlen(company) == 0)
241 if (strlen(name) == 0)
244 valid = gtk_list_store_remove (store, &iter);
251 gtk_list_store_set (store, &iter, CI_COMPANY, name, -1);
264 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL(store), &iter);
269gnc_customer_import_create_customers (GtkListStore *store,
QofBook *book, guint *n_customers_created, guint *n_customers_updated, gchar * type)
273 gchar *id, *company, *name, *addr1, *addr2, *addr3, *addr4, *phone, *fax, *email;
274 gchar *notes, *shipname, *shipaddr1, *shipaddr2, *shipaddr3, *shipaddr4, *shipphone, *shipfax, *shipemail;
284 g_return_if_fail (store && book);
285 printf(
"\nTYPE = %s\n", type);
288 if (!n_customers_created)
289 n_customers_created = &dummy;
290 if (!n_customers_updated)
291 n_customers_updated = &dummy;
292 *n_customers_created = 0;
293 *n_customers_updated = 0;
295 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(store), &iter);
299 gtk_tree_model_get (GTK_TREE_MODEL(store), &iter,
301 CI_COMPANY, &company,
311 CI_SHIPNAME, &shipname,
312 CI_SHIPADDR1, &shipaddr1,
313 CI_SHIPADDR2, &shipaddr2,
314 CI_SHIPADDR3, &shipaddr3,
315 CI_SHIPADDR4, &shipaddr4,
316 CI_SHIPPHONE, &shipphone,
317 CI_SHIPFAX, &shipfax,
318 CI_SHIPEMAIL, &shipemail,
322 if (strlen (
id) == 0)
324 if (g_ascii_strcasecmp (type,
"CUSTOMER") == 0)
id = gncCustomerNextID (book);
325 else if (g_ascii_strcasecmp (type,
"VENDOR") == 0)
id = gncVendorNextID (book);
331 if (g_ascii_strcasecmp (type,
"CUSTOMER") == 0)
333 customer = gnc_search_customer_on_id (book,
id);
336 customer = gncCustomerCreate( book );
338 (*n_customers_created)++;
340 else (*n_customers_updated)++;
342 else if (g_ascii_strcasecmp (type,
"VENDOR") == 0)
344 vendor = gnc_search_vendor_on_id (book,
id);
347 vendor = gncVendorCreate( book );
349 (*n_customers_created)++;
351 else (*n_customers_updated)++;
354 if (g_ascii_strcasecmp (type,
"CUSTOMER") == 0)
356 gncCustomerBeginEdit (customer);
357 gncCustomerSetID (customer,
id);
358 gncCustomerSetName (customer, company);
359 gncCustomerSetNotes (customer, notes);
360 addr = gncCustomerGetAddr (customer);
361 shipaddr = gncCustomerGetShipAddr (customer);
363 else if (g_ascii_strcasecmp (type,
"VENDOR") == 0)
365 gncVendorBeginEdit (vendor);
366 gncVendorSetID (vendor,
id);
367 gncVendorSetName (vendor, company);
368 gncVendorSetNotes (vendor, notes);
369 addr = gncVendorGetAddr (vendor);
371 gncAddressSetName (addr, name);
372 gncAddressSetAddr1 (addr, addr1);
373 gncAddressSetAddr2 (addr, addr2);
374 gncAddressSetAddr3 (addr, addr3);
375 gncAddressSetAddr4 (addr, addr4);
376 gncAddressSetPhone (addr, phone);
377 gncAddressSetFax (addr, fax);
378 gncAddressSetEmail (addr, email);
379 if (g_ascii_strcasecmp (type,
"CUSTOMER") == 0)
381 gncAddressSetName (shipaddr, shipname);
382 gncAddressSetAddr1 (shipaddr, shipaddr1);
383 gncAddressSetAddr2 (shipaddr, shipaddr2);
384 gncAddressSetAddr3 (shipaddr, shipaddr3);
385 gncAddressSetAddr4 (shipaddr, shipaddr4);
386 gncAddressSetPhone (shipaddr, shipphone);
387 gncAddressSetFax (shipaddr, shipfax);
388 gncAddressSetEmail (shipaddr, shipemail);
389 gncCustomerSetActive (customer, TRUE);
390 gncCustomerCommitEdit (customer);
392 else if (g_ascii_strcasecmp (type,
"VENDOR") == 0)
394 gncVendorSetActive (vendor, TRUE);
395 gncVendorCommitEdit (vendor);
419 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)
Return the default currency set by the user.
void gnc_utf8_strip_invalid(gchar *str)
Strip any non-UTF-8 characters from a string.
credit, discount and shipaddr are unique to GncCustomer id, name, notes, terms, addr,...