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)
70 customer_import_stats stats_fallback;
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);
190 if (stats == &stats_fallback)
192 g_string_free (stats->ignored_lines, TRUE);
200 gnc_customer_import_fix_customers (GtkListStore *store, guint *fixed, guint *deleted, gchar * type)
204 gchar *company, *name, *addr1, *addr2, *addr3, *addr4;
216 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(store), &iter);
220 gtk_tree_model_get (GTK_TREE_MODEL(store), &iter,
221 CI_COMPANY, &company,
231 if (strlen(company) == 0)
234 if (strlen(name) == 0)
237 valid = gtk_list_store_remove (store, &iter);
244 gtk_list_store_set (store, &iter, CI_COMPANY, name, -1);
257 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL(store), &iter);
262 gnc_customer_import_create_customers (GtkListStore *store, QofBook *book, guint *n_customers_created, guint *n_customers_updated, gchar * type)
266 gchar *id, *company, *name, *addr1, *addr2, *addr3, *addr4, *phone, *fax, *email;
267 gchar *notes, *shipname, *shipaddr1, *shipaddr2, *shipaddr3, *shipaddr4, *shipphone, *shipfax, *shipemail;
277 g_return_if_fail (store && book);
278 printf(
"\nTYPE = %s\n", type);
281 if (!n_customers_created)
282 n_customers_created = &dummy;
283 if (!n_customers_updated)
284 n_customers_updated = &dummy;
285 *n_customers_created = 0;
286 *n_customers_updated = 0;
288 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(store), &iter);
292 gtk_tree_model_get (GTK_TREE_MODEL(store), &iter,
294 CI_COMPANY, &company,
304 CI_SHIPNAME, &shipname,
305 CI_SHIPADDR1, &shipaddr1,
306 CI_SHIPADDR2, &shipaddr2,
307 CI_SHIPADDR3, &shipaddr3,
308 CI_SHIPADDR4, &shipaddr4,
309 CI_SHIPPHONE, &shipphone,
310 CI_SHIPFAX, &shipfax,
311 CI_SHIPEMAIL, &shipemail,
315 if (strlen (
id) == 0)
317 if (g_ascii_strcasecmp (type,
"CUSTOMER") == 0)
id = gncCustomerNextID (book);
318 else if (g_ascii_strcasecmp (type,
"VENDOR") == 0)
id = gncVendorNextID (book);
324 if (g_ascii_strcasecmp (type,
"CUSTOMER") == 0)
326 customer = gnc_search_customer_on_id (book,
id);
329 customer = gncCustomerCreate( book );
331 (*n_customers_created)++;
333 else (*n_customers_updated)++;
335 else if (g_ascii_strcasecmp (type,
"VENDOR") == 0)
337 vendor = gnc_search_vendor_on_id (book,
id);
340 vendor = gncVendorCreate( book );
342 (*n_customers_created)++;
344 else (*n_customers_updated)++;
347 if (g_ascii_strcasecmp (type,
"CUSTOMER") == 0)
349 gncCustomerBeginEdit (customer);
350 gncCustomerSetID (customer,
id);
351 gncCustomerSetName (customer, company);
352 gncCustomerSetNotes (customer, notes);
353 addr = gncCustomerGetAddr (customer);
354 shipaddr = gncCustomerGetShipAddr (customer);
356 else if (g_ascii_strcasecmp (type,
"VENDOR") == 0)
358 gncVendorBeginEdit (vendor);
359 gncVendorSetID (vendor,
id);
360 gncVendorSetName (vendor, company);
361 gncVendorSetNotes (vendor, notes);
362 addr = gncVendorGetAddr (vendor);
364 gncAddressSetName (addr, name);
365 gncAddressSetAddr1 (addr, addr1);
366 gncAddressSetAddr2 (addr, addr2);
367 gncAddressSetAddr3 (addr, addr3);
368 gncAddressSetAddr4 (addr, addr4);
369 gncAddressSetPhone (addr, phone);
370 gncAddressSetFax (addr, fax);
371 gncAddressSetEmail (addr, email);
372 if (g_ascii_strcasecmp (type,
"CUSTOMER") == 0)
374 gncAddressSetName (shipaddr, shipname);
375 gncAddressSetAddr1 (shipaddr, shipaddr1);
376 gncAddressSetAddr2 (shipaddr, shipaddr2);
377 gncAddressSetAddr3 (shipaddr, shipaddr3);
378 gncAddressSetAddr4 (shipaddr, shipaddr4);
379 gncAddressSetPhone (shipaddr, shipphone);
380 gncAddressSetFax (shipaddr, shipfax);
381 gncAddressSetEmail (shipaddr, shipemail);
382 gncCustomerSetActive (customer, TRUE);
383 gncCustomerCommitEdit (customer);
385 else if (g_ascii_strcasecmp (type,
"VENDOR") == 0)
387 gncVendorSetActive (vendor, TRUE);
388 gncVendorCommitEdit (vendor);
412 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.
credit, discount and shipaddr are unique to GncCustomer id, name, notes, terms, addr, currency, taxtable, taxtable_override taxincluded, active and jobs are identical to ::GncVendor.