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)
518 strcpy(
id, running_id->str);
519 gtk_list_store_set (store, &iter, ID,
id, -1);
523 if (!valid || (valid && g_strcmp0 (
id, running_id->str) != 0))
528 iter = first_row_of_invoice;
532 valid = gtk_list_store_remove (store, &iter);
533 if (valid) gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, ID, &
id, -1);
535 while (valid && (g_strcmp0 (
id, running_id->str) == 0));
537 if (running_id->len != 0)
539 g_string_append_printf (info,
540 _(
"Error(s) in invoice %s, all rows of this invoice ignored.\n"),
545 g_string_append_printf (info,
546 _(
"Error(s) in invoice without id, all rows of this invoice ignored.\n"));
550 fixed_for_invoice = 0;
552 ignore_invoice = FALSE;
555 on_first_row_of_invoice = TRUE;
556 (*n_rows_fixed) += fixed_for_invoice;
557 fixed_for_invoice = 0;
561 g_free (date_opened);
562 g_free (date_posted);
564 g_free (account_posted);
567 else on_first_row_of_invoice = FALSE;
578 g_string_free (running_id, TRUE);
606 guint * n_invoices_created,
607 guint * n_invoices_updated,
608 guint * n_rows_ignored,
609 gchar * type, gchar * open_mode, GString * info,
612 gboolean valid, on_first_row_of_invoice, invoice_posted;
613 GtkTreeIter iter, first_row_of_invoice;
614 gchar *
id = NULL, *date_opened = NULL, *owner_id = NULL, *billing_id = NULL, *notes = NULL;
615 gchar *date = NULL, *desc = NULL, *action = NULL, *account = NULL, *quantity = NULL,
616 *price = NULL, *disc_type = NULL, *disc_how = NULL, *discount = NULL, *taxable = NULL,
617 *taxincluded = NULL, *tax_table = NULL;
618 gchar *date_posted = NULL, *due_date = NULL, *account_posted = NULL, *memo_posted = NULL,
619 *accumulatesplits = NULL;
623 gint day, month, year;
627 enum update {YES = GTK_RESPONSE_YES, NO = GTK_RESPONSE_NO, NOT_ASKED = GTK_RESPONSE_NONE} update;
634 g_return_if_fail (store && book);
636 g_return_if_fail ((g_ascii_strcasecmp (type,
"INVOICE") == 0) ||
637 (g_ascii_strcasecmp (type,
"BILL") == 0));
640 if (!n_invoices_created)
641 n_invoices_created = &dummy;
642 if (!n_invoices_updated)
643 n_invoices_updated = &dummy;
644 *n_invoices_created = 0;
645 *n_invoices_updated = 0;
649 on_first_row_of_invoice = TRUE;
650 running_id = g_string_new(
"");
652 g_string_append_printf (info,
"\n%s\n", _(
"Processing…") );
654 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter);
658 gtk_tree_model_get (GTK_TREE_MODEL (store), &iter,
660 DATE_OPENED, &date_opened,
661 DATE_POSTED, &date_posted,
663 ACCOUNT_POSTED, &account_posted,
664 MEMO_POSTED, &memo_posted,
665 ACCU_SPLITS, &accumulatesplits,
667 BILLING_ID, &billing_id,
675 DISC_TYPE, &disc_type,
679 TAXINCLUDED, &taxincluded,
680 TAX_TABLE, &tax_table, -1);
682 if (on_first_row_of_invoice)
684 g_string_assign(running_id,
id);
685 first_row_of_invoice = iter;
687 if (g_ascii_strcasecmp (type,
"BILL") == 0)
688 invoice = gnc_search_bill_on_id (book,
id);
689 else if (g_ascii_strcasecmp (type,
"INVOICE") == 0)
690 invoice = gnc_search_invoice_on_id (book,
id);
691 DEBUG(
"Existing %s ID: %s\n", type, gncInvoiceGetID(invoice));
696 DEBUG(
"Creating a new : %s\n", type );
698 invoice = gncInvoiceCreate (book);
701 gncInvoiceBeginEdit (invoice);
702 gncInvoiceSetID (invoice,
id);
704 if (g_ascii_strcasecmp (type,
"BILL") == 0)
705 gncOwnerInitVendor (owner,
706 gnc_search_vendor_on_id (book, owner_id));
707 else if (g_ascii_strcasecmp (type,
"INVOICE") == 0)
708 gncOwnerInitCustomer (owner,
709 gnc_search_customer_on_id (book, owner_id));
710 gncInvoiceSetOwner (invoice, owner);
711 gncInvoiceSetCurrency (invoice, gncOwnerGetCurrency (owner));
713 gncInvoiceSetDateOpened (invoice,
714 gnc_dmy2time64 (day, month, year));
715 gncInvoiceSetBillingID (invoice, billing_id ? billing_id :
"");
716 notes = un_escape(notes);
717 gncInvoiceSetNotes (invoice, notes ? notes :
"");
718 gncInvoiceSetActive (invoice, TRUE);
720 (*n_invoices_created)++;
721 g_string_append_printf (info, _(
"Invoice %s created.\n"),
id);
723 gncInvoiceCommitEdit (invoice);
729 if (update == NOT_ASKED)
731 dialog = gtk_message_dialog_new (parent,
736 _(
"Do you want to update existing bills/invoices?"));
737 update = gtk_dialog_run (GTK_DIALOG (dialog));
738 gtk_widget_destroy (dialog);
744 g_string_append_printf (info,_(
"Invoice %s not updated because it already exists.\n"),
id);
745 while (valid && g_strcmp0 (
id, running_id->str) == 0)
748 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter);
750 gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, ID, &
id, -1);
752 on_first_row_of_invoice = TRUE;
756 if (gncInvoiceIsPosted (invoice))
759 g_string_append_printf (info,_(
"Invoice %s not updated because it is already posted.\n"),
id);
760 while (valid && g_strcmp0 (
id, running_id->str) == 0)
763 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter);
765 gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, ID, &
id, -1);
767 on_first_row_of_invoice = TRUE;
771 (*n_invoices_updated)++;
772 g_string_append_printf (info, _(
"Invoice %s updated.\n"),
id);
777 entry = gncEntryCreate (book);
778 gncEntryBeginEdit(entry);
781 GDate *date = g_date_new_dmy(day, month, year);
786 gncEntrySetDateEntered(entry, today);
788 desc = un_escape(desc);
789 notes = un_escape(notes);
790 gncEntrySetDescription (entry, desc);
791 gncEntrySetAction (entry, action);
792 value = gnc_numeric_zero();
793 gnc_exp_parser_parse (quantity, &value, NULL);
798 if (g_ascii_strcasecmp (type,
"BILL") == 0)
800 gncEntrySetBillAccount (entry, acc);
801 value = gnc_numeric_zero();
802 gnc_exp_parser_parse (price, &value, NULL);
803 gncEntrySetBillPrice (entry, value);
804 gncEntrySetBillTaxable (entry, text2bool (taxable));
805 gncEntrySetBillTaxIncluded (entry, text2bool (taxincluded));
806 gncEntrySetBillTaxTable (entry, gncTaxTableLookupByName (book, tax_table));
809 else if (g_ascii_strcasecmp (type,
"INVOICE") == 0)
811 gncEntrySetNotes (entry, notes);
812 gncEntrySetInvAccount (entry, acc);
813 value = gnc_numeric_zero();
814 gnc_exp_parser_parse (price, &value, NULL);
815 gncEntrySetInvPrice (entry, value);
816 gncEntrySetInvTaxable (entry, text2bool (taxable));
817 gncEntrySetInvTaxIncluded (entry, text2bool (taxincluded));
818 gncEntrySetInvTaxTable (entry, gncTaxTableLookupByName (book, tax_table));
819 value = gnc_numeric_zero();
820 gnc_exp_parser_parse (discount, &value, NULL);
821 gncEntrySetInvDiscount (entry, value);
822 gncEntrySetInvDiscountType (entry, text2disc_type (disc_type));
823 gncEntrySetInvDiscountHow (entry, text2disc_how (disc_how));
824 gncInvoiceAddEntry (invoice, entry);
826 gncEntryCommitEdit(entry);
827 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter);
831 gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, ID, &
id, -1);
835 if (g_strcmp0 (
id, running_id->str) == 0)
837 on_first_row_of_invoice = FALSE;
842 gtk_tree_model_get (GTK_TREE_MODEL (store), &first_row_of_invoice,
844 DATE_POSTED, &date_posted,
846 ACCOUNT_POSTED, &account_posted,
847 MEMO_POSTED, &memo_posted,
848 ACCU_SPLITS, &accumulatesplits, -1);
849 invoice_posted = FALSE;
851 if (strlen(date_posted) != 0)
854 GHashTable *foreign_currs;
858 gboolean scan_date_r;
859 scan_date_r =
qof_scan_date (date_posted, &day, &month, &year);
860 DEBUG(
"Invoice %s is marked to be posted because...",
id);
861 DEBUG(
"qof_scan_date = %d", scan_date_r);
862 if (g_ascii_strcasecmp (type,
"INVOICE") == 0)
868 curr_count = g_hash_table_size (foreign_currs);
869 DEBUG(
"curr_count = %d",curr_count);
874 (gnc_get_current_root_account (), account_posted);
879 p_date = gnc_dmy2time64 (day, month, year);
881 d_date = gnc_dmy2time64 (day, month, year);
884 text2bool (accumulatesplits),
886 PWARN(
"Invoice %s posted",
id);
887 invoice_posted = TRUE;
888 g_string_append_printf (info, _(
"Invoice %s posted.\n"),
id);
892 PWARN(
"Invoice %s NOT posted because currencies don't match",
id);
893 g_string_append_printf (info,_(
"Invoice %s NOT posted because currencies don't match.\n"),
id);
898 PWARN(
"Invoice %s NOT posted because it requires currency conversion.",
id);
899 g_string_append_printf (info,_(
"Invoice %s NOT posted because it requires currency conversion.\n"),
id);
901 g_hash_table_unref (foreign_currs);
905 PWARN(
"Invoice %s is NOT marked for posting",
id);
909 if (g_ascii_strcasecmp(open_mode,
"ALL") == 0
910 || (g_ascii_strcasecmp(open_mode,
"NOT_POSTED") == 0
913 iw = gnc_ui_invoice_edit (parent, invoice);
918 on_first_row_of_invoice = TRUE;
922 if (*n_invoices_updated + *n_invoices_created == 0)
923 g_string_append_printf (info, _(
"Nothing to process.\n"));
927 g_free (date_opened);
941 g_free (taxincluded);
943 g_free (date_posted);
945 g_free (account_posted);
946 g_free (memo_posted);
947 g_free (accumulatesplits);
949 g_string_free (running_id, TRUE);