277 GString * info, gchar *type)
279 GtkTreeIter iter, first_row_of_invoice;
280 gboolean valid, row_fixed, on_first_row_of_invoice, ignore_invoice;
281 gchar *
id = NULL, *date_opened = NULL, *date_posted = NULL, *due_date = NULL, *account_posted = NULL,
282 *owner_id = NULL, *date = NULL, *account = NULL, *quantity = NULL, *price = NULL;
286 gint row = 1, fixed_for_invoice = 0, invoice_line = 0;
289 DEBUG(
"date_format_string: %s",date_format_string);
292 n_rows_fixed = &dummy;
294 n_rows_ignored = &dummy;
300 running_id = g_string_new(
"");
301 ignore_invoice = FALSE;
302 on_first_row_of_invoice = TRUE;
304 g_string_append_printf (info, _(
"Validation…\n") );
307 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter);
314 if (on_first_row_of_invoice)
316 gtk_tree_model_get (GTK_TREE_MODEL (store), &iter,
318 DATE_OPENED, &date_opened,
319 DATE_POSTED, &date_posted,
321 ACCOUNT_POSTED, &account_posted,
322 OWNER_ID, &owner_id, -1);
324 g_string_assign (running_id,
id);
325 first_row_of_invoice = iter;
328 if (strlen (
id) == 0)
332 ignore_invoice = TRUE;
333 g_string_append_printf (info,
334 _(
"Row %d: no invoice ID in first row of import file.\n"), row);
338 if (strlen (owner_id) == 0)
340 ignore_invoice = TRUE;
341 g_string_append_printf (info,
342 _(
"Row %d, invoice %s/%u: owner not set.\n"),
343 row,
id, invoice_line);
346 if (g_ascii_strcasecmp (type,
"BILL") == 0)
348 if (!gnc_search_vendor_on_id
349 (gnc_get_current_book (), owner_id))
352 ignore_invoice = TRUE;
353 g_string_append_printf (info,
354 _(
"Row %d, invoice %s/%u: vendor %s does not exist.\n"),
355 row,
id, invoice_line, owner_id);
358 else if (g_ascii_strcasecmp (type,
"INVOICE") == 0)
360 if (!gnc_search_customer_on_id
361 (gnc_get_current_book (), owner_id))
364 ignore_invoice = TRUE;
365 g_string_append_printf (info,
366 _(
"Row %d, invoice %s/%u: customer %s does not exist.\n"),
367 row,
id, invoice_line, owner_id);
371 if (strlen(date_posted) != 0)
374 if (!isDateValid(date_posted))
377 ignore_invoice = TRUE;
378 g_string_append_printf (info,
379 _(
"Row %d, invoice %s/%u: %s is not a valid posting date.\n"),
380 row,
id, invoice_line, date_posted);
383 if (!isDateValid(due_date))
386 g_string_append_printf (info,
387 _(
"Row %d, invoice %s/%u: %s is not a valid due date.\n"),
388 row,
id, invoice_line, due_date);
394 if (!isDateValid(due_date))
397 gtk_list_store_set (store, &iter, DUE_DATE,
406 (gnc_get_current_root_account (), account_posted);
409 ignore_invoice = TRUE;
410 g_string_append_printf (info,
411 _(
"Row %d, invoice %s/%u: account %s does not exist.\n"),
412 row,
id, invoice_line, account_posted);
416 if (g_ascii_strcasecmp (type,
"BILL") == 0)
421 ignore_invoice = TRUE;
422 g_string_append_printf (info,
423 _(
"Row %d, invoice %s/%u: account %s is not of type Accounts Payable.\n"),
424 row,
id, invoice_line, account_posted);
427 else if (g_ascii_strcasecmp (type,
"INVOICE") == 0)
431 ignore_invoice = TRUE;
432 g_string_append_printf (info,
433 _(
"Row %d, invoice %s/%u: account %s is not of type Accounts Receivable.\n"),
434 row,
id, invoice_line, account_posted);
441 if(!isDateValid(date_opened))
446 g_date_clear (&date, 1);
448 g_date_strftime (temp, 20, date_format_string, &date);
449 gtk_list_store_set (store, &iter, DATE_OPENED,
458 gtk_tree_model_get (GTK_TREE_MODEL (store), &iter,
466 if (strlen (price) == 0)
469 ignore_invoice = TRUE;
470 g_string_append_printf (info,
471 _(
"Row %d, invoice %s/%u: price not set.\n"),
472 row,
id, invoice_line);
480 ignore_invoice = TRUE;
481 g_string_append_printf (info,
482 _(
"Row %d, invoice %s/%u: account %s does not exist.\n"),
483 row,
id, invoice_line, account);
491 if (strlen (quantity) == 0)
494 gtk_list_store_set (store, &iter, QUANTITY,
"1", -1);
499 if(!isDateValid(date))
502 gtk_list_store_set (store, &iter, DATE,
508 if (row_fixed) ++fixed_for_invoice;
511 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter);
512 if (valid) gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, ID, &
id, -1);
516 if (valid && strlen(
id) == 0)
519 id = g_strdup (running_id->str);
520 gtk_list_store_set (store, &iter, ID,
id, -1);
524 if (!valid || (valid && g_strcmp0 (
id, running_id->str) != 0))
529 iter = first_row_of_invoice;
533 valid = gtk_list_store_remove (store, &iter);
534 if (valid) gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, ID, &
id, -1);
536 while (valid && (g_strcmp0 (
id, running_id->str) == 0));
538 if (running_id->len != 0)
540 g_string_append_printf (info,
541 _(
"Error(s) in invoice %s, all rows of this invoice ignored.\n"),
546 g_string_append_printf (info,
547 _(
"Error(s) in invoice without id, all rows of this invoice ignored.\n"));
551 fixed_for_invoice = 0;
553 ignore_invoice = FALSE;
556 on_first_row_of_invoice = TRUE;
557 (*n_rows_fixed) += fixed_for_invoice;
558 fixed_for_invoice = 0;
562 g_free (date_opened);
563 g_free (date_posted);
565 g_free (account_posted);
568 else on_first_row_of_invoice = FALSE;
579 g_string_free (running_id, TRUE);
607 guint * n_invoices_created,
608 guint * n_invoices_updated,
609 guint * n_rows_ignored,
610 gchar * type, gchar * open_mode, GString * info,
613 gboolean valid, on_first_row_of_invoice, invoice_posted;
614 GtkTreeIter iter, first_row_of_invoice;
615 gchar *
id = NULL, *date_opened = NULL, *owner_id = NULL, *billing_id = NULL, *notes = NULL;
616 gchar *date = NULL, *desc = NULL, *action = NULL, *account = NULL, *quantity = NULL,
617 *price = NULL, *disc_type = NULL, *disc_how = NULL, *discount = NULL, *taxable = NULL,
618 *taxincluded = NULL, *tax_table = NULL;
619 gchar *date_posted = NULL, *due_date = NULL, *account_posted = NULL, *memo_posted = NULL,
620 *accumulatesplits = NULL;
624 gint day, month, year;
628 enum update {YES = GTK_RESPONSE_YES, NO = GTK_RESPONSE_NO, NOT_ASKED = GTK_RESPONSE_NONE} update;
635 g_return_if_fail (store && book);
637 g_return_if_fail ((g_ascii_strcasecmp (type,
"INVOICE") == 0) ||
638 (g_ascii_strcasecmp (type,
"BILL") == 0));
641 if (!n_invoices_created)
642 n_invoices_created = &dummy;
643 if (!n_invoices_updated)
644 n_invoices_updated = &dummy;
645 *n_invoices_created = 0;
646 *n_invoices_updated = 0;
650 on_first_row_of_invoice = TRUE;
651 running_id = g_string_new(
"");
653 g_string_append_printf (info,
"\n%s\n", _(
"Processing…") );
655 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter);
659 gtk_tree_model_get (GTK_TREE_MODEL (store), &iter,
661 DATE_OPENED, &date_opened,
662 DATE_POSTED, &date_posted,
664 ACCOUNT_POSTED, &account_posted,
665 MEMO_POSTED, &memo_posted,
666 ACCU_SPLITS, &accumulatesplits,
668 BILLING_ID, &billing_id,
676 DISC_TYPE, &disc_type,
680 TAXINCLUDED, &taxincluded,
681 TAX_TABLE, &tax_table, -1);
683 if (on_first_row_of_invoice)
685 g_string_assign(running_id,
id);
686 first_row_of_invoice = iter;
688 if (g_ascii_strcasecmp (type,
"BILL") == 0)
689 invoice = gnc_search_bill_on_id (book,
id);
690 else if (g_ascii_strcasecmp (type,
"INVOICE") == 0)
691 invoice = gnc_search_invoice_on_id (book,
id);
692 DEBUG(
"Existing %s ID: %s\n", type, gncInvoiceGetID(invoice));
697 DEBUG(
"Creating a new : %s\n", type );
699 invoice = gncInvoiceCreate (book);
702 gncInvoiceBeginEdit (invoice);
703 gncInvoiceSetID (invoice,
id);
705 if (g_ascii_strcasecmp (type,
"BILL") == 0)
706 gncOwnerInitVendor (owner,
707 gnc_search_vendor_on_id (book, owner_id));
708 else if (g_ascii_strcasecmp (type,
"INVOICE") == 0)
709 gncOwnerInitCustomer (owner,
710 gnc_search_customer_on_id (book, owner_id));
711 gncInvoiceSetOwner (invoice, owner);
712 gncInvoiceSetCurrency (invoice, gncOwnerGetCurrency (owner));
714 gncInvoiceSetDateOpened (invoice,
715 gnc_dmy2time64 (day, month, year));
716 gncInvoiceSetBillingID (invoice, billing_id ? billing_id :
"");
717 notes = un_escape(notes);
718 gncInvoiceSetNotes (invoice, notes ? notes :
"");
719 gncInvoiceSetActive (invoice, TRUE);
721 (*n_invoices_created)++;
722 g_string_append_printf (info, _(
"Invoice %s created.\n"),
id);
724 gncInvoiceCommitEdit (invoice);
730 if (update == NOT_ASKED)
732 dialog = gtk_message_dialog_new (parent,
737 _(
"Do you want to update existing bills/invoices?"));
738 update = gtk_dialog_run (GTK_DIALOG (dialog));
739 gtk_widget_destroy (dialog);
745 g_string_append_printf (info,_(
"Invoice %s not updated because it already exists.\n"),
id);
746 while (valid && g_strcmp0 (
id, running_id->str) == 0)
749 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter);
751 gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, ID, &
id, -1);
753 on_first_row_of_invoice = TRUE;
757 if (gncInvoiceIsPosted (invoice))
760 g_string_append_printf (info,_(
"Invoice %s not updated because it is already posted.\n"),
id);
761 while (valid && g_strcmp0 (
id, running_id->str) == 0)
764 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter);
766 gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, ID, &
id, -1);
768 on_first_row_of_invoice = TRUE;
772 (*n_invoices_updated)++;
773 g_string_append_printf (info, _(
"Invoice %s updated.\n"),
id);
778 entry = gncEntryCreate (book);
779 gncEntryBeginEdit(entry);
782 GDate *date = g_date_new_dmy(day, month, year);
787 gncEntrySetDateEntered(entry, today);
789 desc = un_escape(desc);
790 notes = un_escape(notes);
791 gncEntrySetDescription (entry, desc);
792 gncEntrySetAction (entry, action);
793 value = gnc_numeric_zero();
794 gnc_exp_parser_parse (quantity, &value, NULL);
799 if (g_ascii_strcasecmp (type,
"BILL") == 0)
801 gncEntrySetBillAccount (entry, acc);
802 value = gnc_numeric_zero();
803 gnc_exp_parser_parse (price, &value, NULL);
804 gncEntrySetBillPrice (entry, value);
805 gncEntrySetBillTaxable (entry, text2bool (taxable));
806 gncEntrySetBillTaxIncluded (entry, text2bool (taxincluded));
807 gncEntrySetBillTaxTable (entry, gncTaxTableLookupByName (book, tax_table));
810 else if (g_ascii_strcasecmp (type,
"INVOICE") == 0)
812 gncEntrySetNotes (entry, notes);
813 gncEntrySetInvAccount (entry, acc);
814 value = gnc_numeric_zero();
815 gnc_exp_parser_parse (price, &value, NULL);
816 gncEntrySetInvPrice (entry, value);
817 gncEntrySetInvTaxable (entry, text2bool (taxable));
818 gncEntrySetInvTaxIncluded (entry, text2bool (taxincluded));
819 gncEntrySetInvTaxTable (entry, gncTaxTableLookupByName (book, tax_table));
820 value = gnc_numeric_zero();
821 gnc_exp_parser_parse (discount, &value, NULL);
822 gncEntrySetInvDiscount (entry, value);
823 gncEntrySetInvDiscountType (entry, text2disc_type (disc_type));
824 gncEntrySetInvDiscountHow (entry, text2disc_how (disc_how));
825 gncInvoiceAddEntry (invoice, entry);
827 gncEntryCommitEdit(entry);
828 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter);
832 gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, ID, &
id, -1);
836 if (g_strcmp0 (
id, running_id->str) == 0)
838 on_first_row_of_invoice = FALSE;
843 gtk_tree_model_get (GTK_TREE_MODEL (store), &first_row_of_invoice,
845 DATE_POSTED, &date_posted,
847 ACCOUNT_POSTED, &account_posted,
848 MEMO_POSTED, &memo_posted,
849 ACCU_SPLITS, &accumulatesplits, -1);
850 invoice_posted = FALSE;
852 if (strlen(date_posted) != 0)
855 GHashTable *foreign_currs;
859 gboolean scan_date_r;
860 scan_date_r =
qof_scan_date (date_posted, &day, &month, &year);
861 DEBUG(
"Invoice %s is marked to be posted because...",
id);
862 DEBUG(
"qof_scan_date = %d", scan_date_r);
863 if (g_ascii_strcasecmp (type,
"INVOICE") == 0)
869 curr_count = g_hash_table_size (foreign_currs);
870 DEBUG(
"curr_count = %d",curr_count);
875 (gnc_get_current_root_account (), account_posted);
880 p_date = gnc_dmy2time64 (day, month, year);
882 d_date = gnc_dmy2time64 (day, month, year);
885 text2bool (accumulatesplits),
887 PWARN(
"Invoice %s posted",
id);
888 invoice_posted = TRUE;
889 g_string_append_printf (info, _(
"Invoice %s posted.\n"),
id);
893 PWARN(
"Invoice %s NOT posted because currencies don't match",
id);
894 g_string_append_printf (info,_(
"Invoice %s NOT posted because currencies don't match.\n"),
id);
899 PWARN(
"Invoice %s NOT posted because it requires currency conversion.",
id);
900 g_string_append_printf (info,_(
"Invoice %s NOT posted because it requires currency conversion.\n"),
id);
902 g_hash_table_unref (foreign_currs);
906 PWARN(
"Invoice %s is NOT marked for posting",
id);
910 if (g_ascii_strcasecmp(open_mode,
"ALL") == 0
911 || (g_ascii_strcasecmp(open_mode,
"NOT_POSTED") == 0
914 iw = gnc_ui_invoice_edit (parent, invoice);
919 on_first_row_of_invoice = TRUE;
923 if (*n_invoices_updated + *n_invoices_created == 0)
924 g_string_append_printf (info, _(
"Nothing to process.\n"));
928 g_free (date_opened);
942 g_free (taxincluded);
944 g_free (date_posted);
946 g_free (account_posted);
947 g_free (memo_posted);
948 g_free (accumulatesplits);
950 g_string_free (running_id, TRUE);