r17708 - gnucash/trunk/src - Add a new account selection filter, disallowing bill post to wrong commodity account

Christian Stimming cstim at cvs.gnucash.org
Thu Nov 20 12:00:38 EST 2008


Author: cstim
Date: 2008-11-20 12:00:37 -0500 (Thu, 20 Nov 2008)
New Revision: 17708
Trac: http://svn.gnucash.org/trac/changeset/17708

Modified:
   gnucash/trunk/src/business/business-gnome/business-gnome-utils.c
   gnucash/trunk/src/business/business-gnome/business-gnome-utils.h
   gnucash/trunk/src/business/business-gnome/dialog-date-close.c
   gnucash/trunk/src/business/business-gnome/dialog-date-close.h
   gnucash/trunk/src/business/business-gnome/dialog-employee.c
   gnucash/trunk/src/business/business-gnome/dialog-invoice.c
   gnucash/trunk/src/engine/gnc-commodity.c
   gnucash/trunk/src/engine/gnc-commodity.h
   gnucash/trunk/src/gnome-utils/dialog-book-close.c
   gnucash/trunk/src/gnome-utils/dialog-options.c
   gnucash/trunk/src/gnome-utils/gnc-account-sel.c
   gnucash/trunk/src/gnome-utils/gnc-account-sel.h
   gnucash/trunk/src/gnome/druid-loan.c
   gnucash/trunk/src/gnome/gnc-plugin-page-account-tree.c
Log:
Add a new account selection filter, disallowing bill post to wrong commodity account

This patch adds a new account selection filter allowing valid commodity
to be restricted, and makes use of it for bill posting.  This filter is
allowed to be NULL (just as with the 'account type' filter) and when
NULL, isn't used.  The patch also fixes a bug where gnc commodity copy
wasn't copying mnemonic, thus erroneously making a copy not equal to the
original when comparison happens.  The behaviour is that the user isn't
able to select a post account in the wrong currency.  If there is no
possible account (aka, EVERYTHING gets filtered out) they get a blank
combo box.  This is consistent with what the account type filter does
when there are no valid account-type accounts.

Patch by Jamie Campbell and Mark Jenkins.

Modified: gnucash/trunk/src/business/business-gnome/business-gnome-utils.c
===================================================================
--- gnucash/trunk/src/business/business-gnome/business-gnome-utils.c	2008-11-20 09:49:00 UTC (rev 17707)
+++ gnucash/trunk/src/business/business-gnome/business-gnome-utils.c	2008-11-20 17:00:37 UTC (rev 17708)
@@ -366,6 +366,14 @@
   }
 }
 
+GList *
+gnc_business_commodities (GncOwner *owner)
+{
+  g_return_val_if_fail (owner, NULL);
+
+  return (g_list_prepend (NULL, gncOwnerGetCurrency(owner)));
+}
+
 /*********************************************************************/
 /* Option Menu creation                                              */
 

Modified: gnucash/trunk/src/business/business-gnome/business-gnome-utils.h
===================================================================
--- gnucash/trunk/src/business/business-gnome/business-gnome-utils.h	2008-11-20 09:49:00 UTC (rev 17707)
+++ gnucash/trunk/src/business/business-gnome/business-gnome-utils.h	2008-11-20 17:00:37 UTC (rev 17708)
@@ -62,6 +62,9 @@
 /* Return a list of account-types based on the owner type */
 GList * gnc_business_account_types (GncOwner *owner);
 
+/* Return a list of currencies associated with the owner */
+GList * gnc_business_commodities (GncOwner *owner);
+
 /* Fill in a combo box with the appropriate list of accounts */
 void gnc_fill_account_select_combo (GtkWidget *combo, GNCBook *book,
 				    GList *acct_types);

Modified: gnucash/trunk/src/business/business-gnome/dialog-date-close.c
===================================================================
--- gnucash/trunk/src/business/business-gnome/dialog-date-close.c	2008-11-20 09:49:00 UTC (rev 17707)
+++ gnucash/trunk/src/business/business-gnome/dialog-date-close.c	2008-11-20 17:00:37 UTC (rev 17708)
@@ -47,6 +47,7 @@
   GncBillTerm *terms;
   Timespec *ts, *ts2;
   GList * acct_types;
+  GList * acct_commodities;
   GNCBook *book;
   Account *acct;
   char **memo;
@@ -106,7 +107,7 @@
   GNCAccountSel *gas = GNC_ACCOUNT_SEL (ddc->acct_combo);
 
   /* How do I set the book? */
-  gnc_account_sel_set_acct_filters( gas, ddc->acct_types );
+  gnc_account_sel_set_acct_filters( gas, ddc->acct_types, ddc->acct_commodities );
   gnc_account_sel_set_new_account_ability( gas, TRUE );
   gnc_account_sel_set_new_account_modal( gas, TRUE );
   gnc_account_sel_set_account( gas, ddc->acct, set_default_acct );
@@ -228,8 +229,8 @@
 				const char *question_check_message,
 				gboolean ok_is_default,
                                 gboolean set_default_acct,
-				GList * acct_types, GNCBook *book,
-				GncBillTerm *terms,
+				GList * acct_types, GList * acct_commodities, 
+                                GNCBook *book, GncBillTerm *terms,
 				/* Returned Data... */
 				Timespec *ddue, Timespec *post,
 				char **memo, Account **acct, gboolean *answer)
@@ -253,6 +254,7 @@
   ddc->ts2 = post;
   ddc->book = book;
   ddc->acct_types = acct_types;
+  ddc->acct_commodities = acct_commodities;
   ddc->acct = *acct;
   ddc->memo = memo;
   ddc->terms = terms;

Modified: gnucash/trunk/src/business/business-gnome/dialog-date-close.h
===================================================================
--- gnucash/trunk/src/business/business-gnome/dialog-date-close.h	2008-11-20 09:49:00 UTC (rev 17707)
+++ gnucash/trunk/src/business/business-gnome/dialog-date-close.h	2008-11-20 17:00:37 UTC (rev 17708)
@@ -52,9 +52,9 @@
 				const char *acct_label_message,
 				const char *question_check_message,
 				gboolean ok_is_default,
-				gboolean set_default_acct,
-				GList * acct_types, GNCBook *book,
-				GncBillTerm *terms,
+                                gboolean set_default_acct,
+				GList * acct_types, GList * acct_commodities, 
+                                GNCBook *book, GncBillTerm *terms,
 				/* Returned Data... */
 				Timespec *ddue, Timespec *post,
 				char **memo, Account **acct, gboolean *answer);

Modified: gnucash/trunk/src/business/business-gnome/dialog-employee.c
===================================================================
--- gnucash/trunk/src/business/business-gnome/dialog-employee.c	2008-11-20 09:49:00 UTC (rev 17707)
+++ gnucash/trunk/src/business/business-gnome/dialog-employee.c	2008-11-20 17:00:37 UTC (rev 17708)
@@ -474,7 +474,7 @@
 
   edit = gnc_account_sel_new();
   acct_types = g_list_prepend(NULL, (gpointer)ACCT_TYPE_CREDIT);
-  gnc_account_sel_set_acct_filters (GNC_ACCOUNT_SEL(edit), acct_types);
+  gnc_account_sel_set_acct_filters (GNC_ACCOUNT_SEL(edit), acct_types, NULL);
   g_list_free (acct_types);
 
   ew->ccard_acct_sel = edit;

Modified: gnucash/trunk/src/business/business-gnome/dialog-invoice.c
===================================================================
--- gnucash/trunk/src/business/business-gnome/dialog-invoice.c	2008-11-20 09:49:00 UTC (rev 17707)
+++ gnucash/trunk/src/business/business-gnome/dialog-invoice.c	2008-11-20 17:00:37 UTC (rev 17708)
@@ -602,6 +602,7 @@
   char *message, *memo, *ddue_label, *post_label, *acct_label, *question_label;
   Account *acc = NULL;
   GList * acct_types = NULL;
+  GList * acct_commodities = NULL;
   Timespec ddue, postdate;
   gboolean accumulate;
   QofInstance *owner_inst;
@@ -650,6 +651,9 @@
   /* Determine the type of account to post to */
   acct_types = gnc_business_account_types (&(iw->owner));
 
+  /* Determine which commodity we're working with */
+  acct_commodities = gnc_business_commodities(&(iw->owner));
+  
   /* Get the due date and posted account */
   timespecFromTime_t (&postdate, time(NULL));
   ddue = postdate;
@@ -665,7 +669,7 @@
 
   if (!gnc_dialog_dates_acct_question_parented (iw_get_window(iw), message, ddue_label,
 				       post_label, acct_label, question_label, TRUE, TRUE,
-				       acct_types, iw->book, iw->terms,
+				       acct_types, acct_commodities, iw->book, iw->terms,
 				       &ddue, &postdate, &memo, &acc, &accumulate))
     return;
 

Modified: gnucash/trunk/src/engine/gnc-commodity.c
===================================================================
--- gnucash/trunk/src/engine/gnc-commodity.c	2008-11-20 09:49:00 UTC (rev 17707)
+++ gnucash/trunk/src/engine/gnc-commodity.c	2008-11-20 17:00:37 UTC (rev 17708)
@@ -889,6 +889,7 @@
   CommodityPrivate* dest_priv = GET_PRIVATE(dest);
 
   gnc_commodity_set_fullname (dest, src_priv->fullname);
+  gnc_commodity_set_mnemonic (dest, gnc_commodity_get_mnemonic(src));
   dest_priv->namespace = src_priv->namespace;
   gnc_commodity_set_fraction (dest, src_priv->fraction);
   gnc_commodity_set_cusip (dest, src_priv->cusip);
@@ -1494,6 +1495,17 @@
   return TRUE;
 }
 
+int gnc_commodity_compare(const gnc_commodity * a, const gnc_commodity * b)
+{
+    if (gnc_commodity_equal(a,b))
+    {
+        return 0;
+    }
+    else
+    {
+        return 1;
+    }
+}
 
 /************************************************************
  *                   Namespace functions                    *

Modified: gnucash/trunk/src/engine/gnc-commodity.h
===================================================================
--- gnucash/trunk/src/engine/gnc-commodity.h	2008-11-20 09:49:00 UTC (rev 17707)
+++ gnucash/trunk/src/engine/gnc-commodity.h	2008-11-20 17:00:37 UTC (rev 17708)
@@ -642,6 +642,15 @@
  *  fullname, exchange private code and fraction.
  */
 gboolean gnc_commodity_equal(const gnc_commodity * a, const gnc_commodity * b);
+
+/** This routine returns 0 if the two commodities are equal, 1 otherwise.
+ *  Commodities are equal if they have the same namespace, mnemonic,
+ *  fullname, exchange private code and fraction.
+ *  This function is useful for list-traversal comparison purposes where
+ *  The semantics are 0, <0, or >0 (equal, greater than, less than) rather
+ *   than "true or false"
+ */
+int gnc_commodity_compare(const gnc_commodity * a, const gnc_commodity * b);
 /** @} */
 
 

Modified: gnucash/trunk/src/gnome/druid-loan.c
===================================================================
--- gnucash/trunk/src/gnome/druid-loan.c	2008-11-20 09:49:00 UTC (rev 17707)
+++ gnucash/trunk/src/gnome/druid-loan.c	2008-11-20 17:00:37 UTC (rev 17708)
@@ -503,7 +503,7 @@
                                         gas, gas_data[i].newAcctAbility );
                                 if ( gas_data[i].allowableAccounts != NULL ) {
                                         gnc_account_sel_set_acct_filters(
-                                                gas, gas_data[i].allowableAccounts );
+                                                gas, gas_data[i].allowableAccounts, NULL );
                                 }
                                 gtk_container_add( GTK_CONTAINER(a),
                                                    GTK_WIDGET(gas) );

Modified: gnucash/trunk/src/gnome/gnc-plugin-page-account-tree.c
===================================================================
--- gnucash/trunk/src/gnome/gnc-plugin-page-account-tree.c	2008-11-20 09:49:00 UTC (rev 17707)
+++ gnucash/trunk/src/gnome/gnc-plugin-page-account-tree.c	2008-11-20 17:00:37 UTC (rev 17708)
@@ -852,7 +852,7 @@
   filter = g_object_get_data(G_OBJECT(dialog), DELETE_DIALOG_FILTER);
 
   /* Setting the account type filter triggers GNCAccountSel population. */
-  gnc_account_sel_set_acct_filters (gas, filter);
+  gnc_account_sel_set_acct_filters (gas, filter, NULL);
 
   /* Accounts to be deleted must be removed. */
   gnc_account_sel_purge_account( gas, account, exclude_subaccounts);

Modified: gnucash/trunk/src/gnome-utils/dialog-book-close.c
===================================================================
--- gnucash/trunk/src/gnome-utils/dialog-book-close.c	2008-11-20 09:49:00 UTC (rev 17707)
+++ gnucash/trunk/src/gnome-utils/dialog-book-close.c	2008-11-20 17:00:37 UTC (rev 17708)
@@ -333,7 +333,7 @@
   box = glade_xml_get_widget(xml, "income_acct_box");
   cbw->income_acct_widget = gnc_account_sel_new();
   gnc_account_sel_set_acct_filters(GNC_ACCOUNT_SEL(cbw->income_acct_widget),
-				   equity_list);
+				   equity_list, NULL);
   gnc_account_sel_set_new_account_ability(GNC_ACCOUNT_SEL(cbw->income_acct_widget), TRUE);
   gtk_box_pack_start(GTK_BOX(box), cbw->income_acct_widget, TRUE, TRUE, 0);
 
@@ -341,7 +341,7 @@
   box = glade_xml_get_widget(xml, "expense_acct_box");
   cbw->expense_acct_widget = gnc_account_sel_new();
   gnc_account_sel_set_acct_filters(GNC_ACCOUNT_SEL(cbw->expense_acct_widget),
-				   equity_list);
+				   equity_list, NULL);
   gnc_account_sel_set_new_account_ability(GNC_ACCOUNT_SEL(cbw->expense_acct_widget), TRUE);
   gtk_box_pack_start(GTK_BOX(box), cbw->expense_acct_widget, TRUE, TRUE, 0);
 

Modified: gnucash/trunk/src/gnome-utils/dialog-options.c
===================================================================
--- gnucash/trunk/src/gnome-utils/dialog-options.c	2008-11-20 09:49:00 UTC (rev 17707)
+++ gnucash/trunk/src/gnome-utils/dialog-options.c	2008-11-20 17:00:37 UTC (rev 17708)
@@ -1828,7 +1828,7 @@
 
   acct_type_list = gnc_option_get_account_type_list(option);
   value = gnc_account_sel_new();
-  gnc_account_sel_set_acct_filters(GNC_ACCOUNT_SEL(value), acct_type_list);
+  gnc_account_sel_set_acct_filters(GNC_ACCOUNT_SEL(value), acct_type_list, NULL);
 
   g_signal_connect(value, "account_sel_changed",
 		   G_CALLBACK(gnc_option_changed_widget_cb), option);

Modified: gnucash/trunk/src/gnome-utils/gnc-account-sel.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-account-sel.c	2008-11-20 09:49:00 UTC (rev 17707)
+++ gnucash/trunk/src/gnome-utils/gnc-account-sel.c	2008-11-20 17:00:37 UTC (rev 17708)
@@ -31,10 +31,12 @@
 #include "dialog-account.h"
 #include "GNCId.h"
 #include "gnc-account-sel.h"
+#include "gnc-commodity.h"
 #include "gnc-exp-parser.h"
 #include "gnc-gtk-utils.h"
 #include "gnc-ui-util.h"
 #include "qof.h"
+#include "gnc-session.h"
 
 #define ACCT_DATA_TAG "gnc-account-sel_acct"
 
@@ -249,6 +251,17 @@
                         return;
                 }
         }
+
+        if ( atnd->gas->acctCommodityFilters ) {
+                if ( g_list_find_custom( atnd->gas->acctCommodityFilters,
+                                  GINT_TO_POINTER(xaccAccountGetCommodity( a )),
+                                  gnc_commodity_compare) 
+                     == NULL ) {
+                        return;
+                }
+        }
+
+        
         *atnd->outList = g_list_append( *atnd->outList, a );
 }
 
@@ -324,19 +337,54 @@
 
 
 void
-gnc_account_sel_set_acct_filters( GNCAccountSel *gas, GList *filters )
+gnc_account_sel_set_acct_filters( GNCAccountSel *gas, GList *typeFilters, GList *commodityFilters )
 {
+        GList *src=NULL;
+        GList *dest=NULL;
+        gnc_commodity* commClone=NULL;
+
         if ( gas->acctTypeFilters != NULL ) {
                 g_list_free( gas->acctTypeFilters );
                 gas->acctTypeFilters = NULL;
         }
-        /* If it's null, then no filters exist. */
-        if ( ! filters ) {
+
+        if ( gas->acctCommodityFilters != NULL) {
+                g_list_free( gas->acctCommodityFilters );
+                gas->acctCommodityFilters = NULL;
+        }
+
+        /* If both filters are null, then no filters exist. */
+        if (( ! typeFilters ) && ( ! commodityFilters)) {
                 return;
         }
+
         /* This works because the GNCAccountTypes in the list are
          * ints-casted-as-pointers. */
-        gas->acctTypeFilters = g_list_copy( filters );
+        if (typeFilters)
+        {
+            gas->acctTypeFilters = g_list_copy( typeFilters );
+        }
+
+        if (commodityFilters)
+        {
+            src = commodityFilters;
+
+            while (src->data != NULL)
+            {
+                //gnc_commodity_clone would have been nice but it expects me to 
+                //insert the clone into a book, which I don't want to do.
+                commClone = gnc_commodity_new(qof_session_get_book(gnc_get_current_session()), "","","","",1);
+                gnc_commodity_copy(commClone, src->data);
+                dest = g_list_prepend(dest, commClone);
+                if (src->next == NULL)
+                {
+                    break;
+                }
+                src = src->next;
+            }
+            gas->acctCommodityFilters = dest;
+        }
+
         gas_populate_list( gas );
 }
 

Modified: gnucash/trunk/src/gnome-utils/gnc-account-sel.h
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-account-sel.h	2008-11-20 09:49:00 UTC (rev 17707)
+++ gnucash/trunk/src/gnome-utils/gnc-account-sel.h	2008-11-20 17:00:37 UTC (rev 17708)
@@ -46,6 +46,7 @@
 	GtkListStore *store;
         GtkComboBoxEntry *combo;
         GList *acctTypeFilters;
+	GList *acctCommodityFilters;
         gint eventHandlerId;
         /* The state of this pointer also serves as a flag about what state
          * the widget is in WRT the new-account-button ability. */
@@ -81,10 +82,11 @@
 
 /**
  * The GNCAccountSel can be setup to filter the accounts displayed.
- * @param filters A GList of GNCAccountType identifiers which are allowed.
+ * @param typeFilters A GList of GNCAccountType identifiers which are allowed.
+ * @param commodityFilters A GList of gnc_commodity types which are allowed.
  * The list is copied, of course.
  **/
-void gnc_account_sel_set_acct_filters( GNCAccountSel *gas, GList *filters );
+void gnc_account_sel_set_acct_filters( GNCAccountSel *gas, GList *typeFilters, GList *commodityFilters );
 
 /**
  * Conditional inclusion of a new-account button to the right of the



More information about the gnucash-changes mailing list