[Gnucash-changes] r13648 - gnucash/trunk - Move the computation of accounting period balances into the model and

David Hampton hampton at cvs.gnucash.org
Thu Mar 16 15:10:16 EST 2006


Author: hampton
Date: 2006-03-16 15:10:16 -0500 (Thu, 16 Mar 2006)
New Revision: 13648
Trac: http://svn.gnucash.org/trac/changeset/13648

Modified:
   gnucash/trunk/ChangeLog
   gnucash/trunk/src/gnome-utils/gnc-tree-model-account.c
   gnucash/trunk/src/gnome-utils/gnc-tree-model-account.h
   gnucash/trunk/src/gnome-utils/gnc-tree-view-account.c
Log:
Move the computation of accounting period balances into the model and
colorize it.


Modified: gnucash/trunk/ChangeLog
===================================================================
--- gnucash/trunk/ChangeLog	2006-03-16 15:38:46 UTC (rev 13647)
+++ gnucash/trunk/ChangeLog	2006-03-16 20:10:16 UTC (rev 13648)
@@ -1,3 +1,9 @@
+2006-03-16  David Hampton  <hampton at employees.org>
+
+	* src/gnome-utils/gnc-tree-model-account.[ch]:
+	* src/gnome-utils/gnc-tree-view-account.c: Move the computation of
+	accounting period balances into the model and colorize it.
+
 2006-03-16  Christian Stimming <stimming at tuhh.de>
 
 	* src/gnome/Makefile.am: Add more linker flags for referenced

Modified: gnucash/trunk/src/gnome-utils/gnc-tree-model-account.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-tree-model-account.c	2006-03-16 15:38:46 UTC (rev 13647)
+++ gnucash/trunk/src/gnome-utils/gnc-tree-model-account.c	2006-03-16 20:10:16 UTC (rev 13648)
@@ -33,6 +33,7 @@
 #include "gnc-component-manager.h"
 #include "Account.h"
 #include "Group.h"
+#include "gnc-accounting-period.h"
 #include "gnc-commodity.h"
 #include "gnc-gconf-utils.h"
 #include "gnc-engine.h"
@@ -400,6 +401,7 @@
 		case GNC_TREE_MODEL_ACCOUNT_COL_PRESENT_REPORT:
 		case GNC_TREE_MODEL_ACCOUNT_COL_BALANCE:
 		case GNC_TREE_MODEL_ACCOUNT_COL_BALANCE_REPORT:
+		case GNC_TREE_MODEL_ACCOUNT_COL_BALANCE_PERIOD:
 		case GNC_TREE_MODEL_ACCOUNT_COL_CLEARED:
 		case GNC_TREE_MODEL_ACCOUNT_COL_CLEARED_REPORT:
 		case GNC_TREE_MODEL_ACCOUNT_COL_RECONCILED:
@@ -408,16 +410,19 @@
 		case GNC_TREE_MODEL_ACCOUNT_COL_FUTURE_MIN_REPORT:
 		case GNC_TREE_MODEL_ACCOUNT_COL_TOTAL:
 		case GNC_TREE_MODEL_ACCOUNT_COL_TOTAL_REPORT:
+		case GNC_TREE_MODEL_ACCOUNT_COL_TOTAL_PERIOD:
 		case GNC_TREE_MODEL_ACCOUNT_COL_NOTES:
 		case GNC_TREE_MODEL_ACCOUNT_COL_TAX_INFO:
 		case GNC_TREE_MODEL_ACCOUNT_COL_LASTNUM:
 
 		case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_PRESENT:
 		case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_BALANCE:
+		case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_BALANCE_PERIOD:
 		case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_CLEARED:
 		case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_RECONCILED:
 		case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_FUTURE_MIN:
 		case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_TOTAL:
+		case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_TOTAL_PERIOD:
 			return G_TYPE_STRING;
 
 		case GNC_TREE_MODEL_ACCOUNT_COL_PLACEHOLDER:
@@ -592,6 +597,43 @@
 	  g_value_set_static_string (value, "black");
 }
 
+static gchar *
+gnc_tree_model_account_compute_period_balance(GncTreeModelAccount *model,
+					      Account *acct,
+					      gboolean recurse,
+					      gboolean *negative)
+{
+  GncTreeModelAccountPrivate *priv;
+  time_t t1, t2;
+  gnc_numeric b1, b2, b3;  
+
+  priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
+  if (acct == priv->toplevel)
+    return g_strdup("");
+
+  t1 = gnc_accounting_period_fiscal_start();
+  t2 = gnc_accounting_period_fiscal_end();
+    
+  if (t1 > t2)
+    return g_strdup("");
+
+  if (recurse) {
+    b1 = xaccAccountGetBalanceAsOfDateInCurrency(acct, t1, NULL, TRUE);
+    b2 = xaccAccountGetBalanceAsOfDateInCurrency(acct, t2, NULL, TRUE);
+  } else {
+    b1 = xaccAccountGetBalanceAsOfDate(acct, t1);
+    b2 = xaccAccountGetBalanceAsOfDate(acct, t2);
+  }
+  b3 = gnc_numeric_sub(b2, b1, GNC_DENOM_AUTO, GNC_HOW_DENOM_FIXED);
+  if (gnc_reverse_balance (acct))
+    b3 = gnc_numeric_neg (b3);
+
+  if (negative)
+    *negative = gnc_numeric_negative_p(b3);
+
+  return g_strdup(xaccPrintAmount(b3, gnc_account_print_info(acct, TRUE)));
+}
+
 static void
 gnc_tree_model_account_get_value (GtkTreeModel *tree_model,
 				  GtkTreeIter *iter,
@@ -680,6 +722,17 @@
 			gnc_tree_model_account_set_color(model, negative, value);
 			g_free(string);
 			break;
+		case GNC_TREE_MODEL_ACCOUNT_COL_BALANCE_PERIOD:
+			g_value_init (value, G_TYPE_STRING);
+			string = gnc_tree_model_account_compute_period_balance(model, account, FALSE, &negative);
+			g_value_take_string (value, string);
+			break;
+		case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_BALANCE_PERIOD:
+			g_value_init (value, G_TYPE_STRING);
+			string = gnc_tree_model_account_compute_period_balance(model, account, FALSE, &negative);
+			gnc_tree_model_account_set_color(model, negative, value);
+			g_free (string);
+			break;
 
 		case GNC_TREE_MODEL_ACCOUNT_COL_CLEARED:
 			g_value_init (value, G_TYPE_STRING);
@@ -760,6 +813,17 @@
 			gnc_tree_model_account_set_color(model, negative, value);
 			g_free (string);
 			break;
+		case GNC_TREE_MODEL_ACCOUNT_COL_TOTAL_PERIOD:
+			g_value_init (value, G_TYPE_STRING);
+			string = gnc_tree_model_account_compute_period_balance(model, account, TRUE, &negative);
+			g_value_take_string (value, string);
+			break;
+		case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_TOTAL_PERIOD:
+			g_value_init (value, G_TYPE_STRING);
+			string = gnc_tree_model_account_compute_period_balance(model, account, TRUE, &negative);
+			gnc_tree_model_account_set_color(model, negative, value);
+			g_free (string);
+			break;
 
 		case GNC_TREE_MODEL_ACCOUNT_COL_NOTES:
 			g_value_init (value, G_TYPE_STRING);

Modified: gnucash/trunk/src/gnome-utils/gnc-tree-model-account.h
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-tree-model-account.h	2006-03-16 15:38:46 UTC (rev 13647)
+++ gnucash/trunk/src/gnome-utils/gnc-tree-model-account.h	2006-03-16 20:10:16 UTC (rev 13648)
@@ -64,6 +64,7 @@
 	GNC_TREE_MODEL_ACCOUNT_COL_PRESENT_REPORT,
 	GNC_TREE_MODEL_ACCOUNT_COL_BALANCE,
 	GNC_TREE_MODEL_ACCOUNT_COL_BALANCE_REPORT,
+	GNC_TREE_MODEL_ACCOUNT_COL_BALANCE_PERIOD,
 	GNC_TREE_MODEL_ACCOUNT_COL_CLEARED,
 	GNC_TREE_MODEL_ACCOUNT_COL_CLEARED_REPORT,
 	GNC_TREE_MODEL_ACCOUNT_COL_RECONCILED,
@@ -72,6 +73,7 @@
 	GNC_TREE_MODEL_ACCOUNT_COL_FUTURE_MIN_REPORT,
 	GNC_TREE_MODEL_ACCOUNT_COL_TOTAL,
 	GNC_TREE_MODEL_ACCOUNT_COL_TOTAL_REPORT,
+	GNC_TREE_MODEL_ACCOUNT_COL_TOTAL_PERIOD,
 	GNC_TREE_MODEL_ACCOUNT_COL_NOTES,
 	GNC_TREE_MODEL_ACCOUNT_COL_TAX_INFO,
 	GNC_TREE_MODEL_ACCOUNT_COL_PLACEHOLDER,
@@ -81,10 +83,12 @@
 	/* internal hidden columns */
 	GNC_TREE_MODEL_ACCOUNT_COL_COLOR_PRESENT,
 	GNC_TREE_MODEL_ACCOUNT_COL_COLOR_BALANCE,
+	GNC_TREE_MODEL_ACCOUNT_COL_COLOR_BALANCE_PERIOD,
 	GNC_TREE_MODEL_ACCOUNT_COL_COLOR_CLEARED,
 	GNC_TREE_MODEL_ACCOUNT_COL_COLOR_RECONCILED,
 	GNC_TREE_MODEL_ACCOUNT_COL_COLOR_FUTURE_MIN,
 	GNC_TREE_MODEL_ACCOUNT_COL_COLOR_TOTAL,
+	GNC_TREE_MODEL_ACCOUNT_COL_COLOR_TOTAL_PERIOD,
 
 	GNC_TREE_MODEL_ACCOUNT_NUM_COLUMNS
 } GncTreeModelAccountColumn;

Modified: gnucash/trunk/src/gnome-utils/gnc-tree-view-account.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-tree-view-account.c	2006-03-16 15:38:46 UTC (rev 13647)
+++ gnucash/trunk/src/gnome-utils/gnc-tree-view-account.c	2006-03-16 20:10:16 UTC (rev 13648)
@@ -32,7 +32,6 @@
 #include "gkeyfile.h"
 #endif
 
-#include "gnc-accounting-period.h"
 #include "gnc-tree-view.h"
 #include "gnc-tree-model-account.h"
 #include "gnc-tree-model-account-types.h"
@@ -357,50 +356,6 @@
   return 1;
 }
 
-#define RECURSE GINT_TO_POINTER(1)
-
-static void
-cdf_period(GtkTreeViewColumn *col, GtkCellRenderer *cell, 
-           GtkTreeModel *s_model, GtkTreeIter *iter, gpointer data)
-{
-    Account *acct;
-    time_t t1, t2;
-    const char *str;
-
-    g_return_if_fail(GTK_TREE_VIEW_COLUMN(col));
-    g_return_if_fail(GTK_CELL_RENDERER(cell));
-    g_return_if_fail(GTK_TREE_MODEL_SORT(s_model));
-
-    acct = gnc_tree_view_account_get_account_from_iter(s_model, iter);
-
-    if (acct == gtva_get_top_level_from_model(s_model)) {
-      g_object_set(cell, "text", "", NULL);
-      return;
-    }
-
-    t1 = gnc_accounting_period_fiscal_start();
-    t2 = gnc_accounting_period_fiscal_end();
-    
-    if (t2 > t1) {
-        gnc_numeric b1, b2, b3;  
-
-        if (data == RECURSE) {
-            b1 = xaccAccountGetBalanceAsOfDateInCurrency(acct, t1, NULL, TRUE);
-            b2 = xaccAccountGetBalanceAsOfDateInCurrency(acct, t2, NULL, TRUE);
-        } else {
-            b1 = xaccAccountGetBalanceAsOfDate(acct, t1);
-            b2 = xaccAccountGetBalanceAsOfDate(acct, t2);
-        }
-        b3 = gnc_numeric_sub(b2, b1, GNC_DENOM_AUTO, GNC_HOW_DENOM_FIXED);
-        if (gnc_reverse_balance (acct))
-            b3 = gnc_numeric_neg (b3);
-        
-        str = xaccPrintAmount(b3, gnc_account_print_info(acct, TRUE));
-
-    } else str = "";    
-    g_object_set(cell, "text", str, NULL);
-}
-
 /************************************************************/
 /*                    New View Creation                     */
 /************************************************************/
@@ -511,16 +466,12 @@
 				       GNC_TREE_VIEW_COLUMN_VISIBLE_ALWAYS,
 				       sort_by_balance_value);
 
-  {
-      GtkTreeViewColumn *col = gnc_tree_view_add_numeric_column(
-          view, _("Balance (Period)"), "balance-period", SAMPLE_ACCOUNT_VALUE, 
-          GNC_TREE_VIEW_COLUMN_DATA_NONE, GNC_TREE_VIEW_COLUMN_COLOR_NONE, 
-          GNC_TREE_VIEW_COLUMN_VISIBLE_ALWAYS, NULL);
-      GtkCellRenderer *cr = gnc_tree_view_column_get_renderer(col);
-      gtk_tree_view_column_set_cell_data_func(
-          col, cr, cdf_period, NULL, NULL);
-  }
-  
+  gnc_tree_view_add_numeric_column(view, _("Balance (Period)"), "balance-period",
+				   SAMPLE_ACCOUNT_VALUE, 
+				   GNC_TREE_MODEL_ACCOUNT_COL_BALANCE_PERIOD,
+				   GNC_TREE_MODEL_ACCOUNT_COL_COLOR_BALANCE_PERIOD,
+				   GNC_TREE_VIEW_COLUMN_VISIBLE_ALWAYS,
+				   NULL);
   gnc_tree_view_add_numeric_column(view, _("Cleared"), "cleared",
 				   SAMPLE_ACCOUNT_VALUE,
 				   GNC_TREE_MODEL_ACCOUNT_COL_CLEARED,
@@ -573,16 +524,12 @@
 				       GNC_TREE_MODEL_ACCOUNT_COL_COLOR_TOTAL,
 				       GNC_TREE_VIEW_COLUMN_VISIBLE_ALWAYS,
 				       sort_by_total_value);
-  {
-      GtkTreeViewColumn *col = gnc_tree_view_add_numeric_column(
-          view, _("Total (Period)"), "total-period", SAMPLE_ACCOUNT_VALUE, 
-          GNC_TREE_VIEW_COLUMN_DATA_NONE, GNC_TREE_VIEW_COLUMN_COLOR_NONE, 
-          GNC_TREE_VIEW_COLUMN_VISIBLE_ALWAYS, NULL);
-      GtkCellRenderer *cr = gnc_tree_view_column_get_renderer(col);
-      gtk_tree_view_column_set_cell_data_func(
-          col, cr, cdf_period, RECURSE, NULL);
-  }
-  
+  gnc_tree_view_add_numeric_column(view, _("Total (Period)"), "total-period",
+				   SAMPLE_ACCOUNT_VALUE, 
+				   GNC_TREE_MODEL_ACCOUNT_COL_TOTAL_PERIOD,
+				   GNC_TREE_MODEL_ACCOUNT_COL_COLOR_TOTAL_PERIOD,
+				   GNC_TREE_VIEW_COLUMN_VISIBLE_ALWAYS,
+				   NULL);
   priv->notes_column
     = gnc_tree_view_add_text_column(view, _("Notes"), "notes", NULL,
                                     "Sample account notes.",



More information about the gnucash-changes mailing list