r22176 - gnucash/trunk/src/business/business-gnome - Allow to change document type when duplicating an invoice.

Geert Janssens gjanssens at code.gnucash.org
Tue May 8 17:37:28 EDT 2012


Author: gjanssens
Date: 2012-05-08 17:37:27 -0400 (Tue, 08 May 2012)
New Revision: 22176
Trac: http://svn.gnucash.org/trac/changeset/22176

Modified:
   gnucash/trunk/src/business/business-gnome/dialog-invoice.c
Log:
Allow to change document type when duplicating an invoice.
This allows to quickly create a canceling credit note for an invoice.

Modified: gnucash/trunk/src/business/business-gnome/dialog-invoice.c
===================================================================
--- gnucash/trunk/src/business/business-gnome/dialog-invoice.c	2012-05-08 11:39:25 UTC (rev 22175)
+++ gnucash/trunk/src/business/business-gnome/dialog-invoice.c	2012-05-08 21:37:27 UTC (rev 22176)
@@ -98,6 +98,7 @@
 #define ENUM_INVOICE_TYPE(_) \
   _(NEW_INVOICE, )  \
   _(MOD_INVOICE, )  \
+  _(DUP_INVOICE, )  \
   _(EDIT_INVOICE, ) \
   _(VIEW_INVOICE, )
 
@@ -269,12 +270,22 @@
     return gncInvoiceLookup (iw->book, &iw->invoice_guid);
 }
 
+static void
+set_gncEntry_switch_type (gpointer data, gpointer user_data)
+{
+    GncEntry *entry = data;
+    //g_warning("Modifying date for entry with desc=\"%s\"", gncEntryGetDescription(entry));
+
+    gncEntrySetQuantity (entry, gnc_numeric_neg (gncEntryGetQuantity (entry)));
+}
+
 static void gnc_ui_to_invoice (InvoiceWindow *iw, GncInvoice *invoice)
 {
     GtkTextBuffer* text_buffer;
     GtkTextIter start, end;
     gchar *text;
     Timespec ts;
+    gboolean is_credit_note = gncInvoiceGetIsCreditNote (invoice);
 
     if (iw->dialog_type == VIEW_INVOICE)
         return;
@@ -329,10 +340,19 @@
             gncInvoiceSetBillTo (invoice, &iw->proj_cust);
     }
 
-    /* Document type can only be modified for a new invoice/credit note */
-    if (iw->dialog_type == NEW_INVOICE)
+    /* Document type can only be modified for a new or duplicated invoice/credit note */
+    if (iw->dialog_type == NEW_INVOICE || iw->dialog_type == DUP_INVOICE)
         gncInvoiceSetIsCreditNote (invoice, iw->is_credit_note);
 
+    /* If the document type changed on a duplicated invoice,
+     * its entries should be updated
+     */
+    if (iw->dialog_type == DUP_INVOICE && iw->is_credit_note != is_credit_note)
+    {
+        g_list_foreach(gncInvoiceGetEntries(invoice),
+                       &set_gncEntry_switch_type, NULL);
+    }
+
     gncInvoiceCommitEdit (invoice);
     gnc_resume_gui_refresh ();
 }
@@ -407,11 +427,12 @@
     /* Ok, we don't need this anymore */
     iw->invoice_guid = *guid_null ();
 
-    /* if this is a NEW_INVOICE, and created_invoice is NON-NULL, then
-     * open up a new window with the invoice.  This used to be done
+    /* if this is a new or duplicated invoice, and created_invoice is NON-NULL,
+     * then open up a new window with the invoice.  This used to be done
      * in gnc_ui_invoice_new() but cannot be done anymore
      */
-    if (iw->dialog_type == NEW_INVOICE && iw->created_invoice)
+    if ((iw->dialog_type == NEW_INVOICE || iw->dialog_type == DUP_INVOICE)
+            && iw->created_invoice)
         gnc_ui_invoice_edit (iw->created_invoice);
 
     gnc_close_gui_component (iw->component_id);
@@ -1227,6 +1248,7 @@
             break;
         case NEW_INVOICE:
         case MOD_INVOICE:
+        case DUP_INVOICE:
             iw->job_choice =
                 gnc_general_search_new (GNC_JOB_MODULE_NAME, _("Select..."), TRUE,
                                         gnc_invoice_select_job_cb, iw, iw->book);
@@ -1298,6 +1320,7 @@
         break;
     case NEW_INVOICE:
     case MOD_INVOICE:
+    case DUP_INVOICE:
         if (iw->proj_cust.owner.undefined == NULL)
         {
             iw->proj_job_choice = NULL;
@@ -1603,6 +1626,7 @@
         break;
     case NEW_INVOICE:
     case MOD_INVOICE:
+    case DUP_INVOICE:
         iw->owner_choice =
             gnc_owner_select_create (iw->owner_label, iw->owner_box, iw->book,
                                      &(iw->owner));
@@ -1706,7 +1730,9 @@
     }
 
     gnc_invoice_id_changed_cb(NULL, iw);
-    if (iw->dialog_type == NEW_INVOICE || iw->dialog_type == MOD_INVOICE)
+    if (iw->dialog_type == NEW_INVOICE ||
+        iw->dialog_type == DUP_INVOICE ||
+        iw->dialog_type == MOD_INVOICE)
     {
         if (widget)
             gtk_widget_show (widget);
@@ -1808,6 +1834,7 @@
                        : _("New Invoice");
             break;
         case MOD_INVOICE:
+        case DUP_INVOICE:
         case EDIT_INVOICE:
             wintitle = iw->is_credit_note ? _("Edit Credit Note")
                        : _("Edit Invoice");
@@ -1826,6 +1853,7 @@
                        : _("New Bill");
             break;
         case MOD_INVOICE:
+        case DUP_INVOICE:
         case EDIT_INVOICE:
             wintitle = iw->is_credit_note ? _("Edit Credit Note")
                        : _("Edit Bill");
@@ -1844,6 +1872,7 @@
                        : _("New Expense Voucher");
             break;
         case MOD_INVOICE:
+        case DUP_INVOICE:
         case EDIT_INVOICE:
             wintitle = iw->is_credit_note ? _("Edit Credit Note")
                        : _("Edit Expense Voucher");
@@ -1925,7 +1954,7 @@
     GncOwner *billto;
     GncPluginPage *new_page;
 
-    g_assert (type != NEW_INVOICE && type != MOD_INVOICE);
+    g_assert (type != NEW_INVOICE && type != MOD_INVOICE && type != DUP_INVOICE);
     g_assert (invoice != NULL);
 
     /*
@@ -2291,15 +2320,18 @@
 }
 
 static InvoiceWindow *
-gnc_invoice_window_new_invoice (QofBook *bookp, const GncOwner *owner,
-                                GncInvoice *invoice)
+gnc_invoice_window_new_invoice (InvoiceDialogType dialog_type, QofBook *bookp,
+                                const GncOwner *owner, GncInvoice *invoice)
 {
     InvoiceWindow *iw;
     GtkBuilder *builder;
     GtkWidget *hbox;
     GncOwner *billto;
     const GncOwner *start_owner;
+    GncBillTerm *owner_terms = NULL;
 
+    g_assert (dialog_type == NEW_INVOICE || dialog_type == MOD_INVOICE || dialog_type == DUP_INVOICE);
+
     if (invoice)
     {
         /*
@@ -2323,11 +2355,13 @@
      */
 
     iw = g_new0 (InvoiceWindow, 1);
+    iw->dialog_type = dialog_type;
 
-    if (invoice == NULL)
+    switch (dialog_type)
     {
-        GncBillTerm *owner_terms = NULL;
-        iw->dialog_type = NEW_INVOICE;
+    case NEW_INVOICE:
+        g_assert (bookp);
+
         invoice = gncInvoiceCreate (bookp);
         gncInvoiceSetCurrency (invoice, gnc_default_currency ());
         iw->book = bookp;
@@ -2345,12 +2379,16 @@
         }
         if (owner_terms)
             gncInvoiceSetTerms (invoice, owner_terms);
-    }
-    else
-    {
-        iw->dialog_type = MOD_INVOICE;
+        break;
+
+    case MOD_INVOICE:
+    case DUP_INVOICE:
         start_owner = gncInvoiceGetOwner (invoice);
         iw->book = gncInvoiceGetBook (invoice);
+        break;
+    default:
+        /* The assert at the beginning of this function should prevent this switch case ! */
+        return NULL;
     }
 
     /* Save this for later */
@@ -2374,16 +2412,19 @@
     iw->type_hbox = GTK_WIDGET (gtk_builder_get_object (builder, "dialog_type_choice_hbox"));
     iw->type_choice = GTK_WIDGET (gtk_builder_get_object (builder, "dialog_type_invoice"));
     /* configure the type related widgets based on dialog type and invoice type */
-    if (iw->dialog_type == NEW_INVOICE)
+    switch (dialog_type)
     {
+    case NEW_INVOICE:
+    case DUP_INVOICE:
         gtk_widget_show_all (iw->type_hbox);
         gtk_widget_hide (iw->type_label);
-    }
-    else
-    {
+        break;
+    case MOD_INVOICE:
         gtk_widget_hide_all (iw->type_hbox);
         gtk_widget_show (iw->type_label);
-
+        break;
+    default:
+        break;
     }
 
     iw->id_entry = GTK_WIDGET (gtk_builder_get_object (builder, "dialog_id_entry"));
@@ -2467,7 +2508,7 @@
     InvoiceWindow *iw;
     if (!invoice) return NULL;
 
-    iw = gnc_invoice_window_new_invoice (NULL, NULL, invoice);
+    iw = gnc_invoice_window_new_invoice (MOD_INVOICE, NULL, NULL, invoice);
     return iw;
 }
 
@@ -2535,18 +2576,17 @@
     g_list_foreach(gncInvoiceGetEntries(new_invoice),
                    &set_gncEntry_date, &new_date_gdate);
 
-    // Now open that newly created invoice in the "edit" window
-    iw = gnc_ui_invoice_edit (new_invoice);
-    iw->created_invoice = new_invoice;
 
     if (open_properties)
     {
-        // And also open the "properties" pop-up... however, changing the
-        // invoice ID won't be copied over to the tab title even though
-        // it's correctly copied into the invoice.
-        iw = gnc_ui_invoice_modify (new_invoice);
-        iw->created_invoice = new_invoice;
+        // Open the "properties" pop-up for the invoice...
+        iw = gnc_invoice_window_new_invoice (DUP_INVOICE, NULL, NULL, new_invoice);
     }
+    else
+    {
+        // Open the newly created invoice in the "edit" window
+        iw = gnc_ui_invoice_edit (new_invoice);
+    }
 
     return iw;
 }
@@ -2567,7 +2607,7 @@
     /* Make sure required options exist */
     if (!bookp) return NULL;
 
-    iw = gnc_invoice_window_new_invoice (bookp, &owner, NULL);
+    iw = gnc_invoice_window_new_invoice (NEW_INVOICE, bookp, &owner, NULL);
 
     return iw;
 }



More information about the gnucash-changes mailing list