Skip to content

Commit

Permalink
Add entry to dialog-account to set account balance limits
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob-IT committed Dec 16, 2022
1 parent 518ecfe commit fb84cf7
Show file tree
Hide file tree
Showing 2 changed files with 282 additions and 5 deletions.
148 changes: 147 additions & 1 deletion gnucash/gnome-utils/dialog-account.c
Expand Up @@ -106,6 +106,13 @@ typedef struct _AccountWindow
GtkTreeView *parent_tree;
GtkWidget *parent_scroll;

GtkWidget *more_properties_page;

GtkWidget *balance_grid;
GtkWidget *higher_balance_limit_edit;
GtkWidget *lower_balance_limit_edit;
gboolean balance_is_reversed;

GtkWidget *opening_balance_button;
GtkWidget *opening_balance_edit;
GtkWidget *opening_balance_date_edit;
Expand Down Expand Up @@ -250,6 +257,8 @@ gnc_account_to_ui (AccountWindow *aw)
GdkRGBA color;
gboolean flag, nonstd_scu;
gint index;
gnc_numeric balance_limit;
gboolean balance_limit_valid;

ENTER("%p", aw);
account = aw_get_account (aw);
Expand Down Expand Up @@ -325,6 +334,36 @@ gnc_account_to_ui (AccountWindow *aw)
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(aw->hidden_button),
flag);

aw->balance_is_reversed = gnc_reverse_balance (account);

balance_limit_valid = xaccAccountGetHigherBalanceLimit (account, &balance_limit);
if (balance_limit_valid)
{
if (aw->balance_is_reversed)
{
balance_limit = gnc_numeric_neg (balance_limit);
gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT(aw->lower_balance_limit_edit),
balance_limit);
}
else
gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT(aw->higher_balance_limit_edit),
balance_limit);
}

balance_limit_valid = xaccAccountGetLowerBalanceLimit (account, &balance_limit);
if (balance_limit_valid)
{
if (aw->balance_is_reversed)
{
balance_limit = gnc_numeric_neg (balance_limit);
gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT(aw->higher_balance_limit_edit),
balance_limit);
}
else
gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT(aw->lower_balance_limit_edit),
balance_limit);
}

set_auto_interest_box (aw);
LEAVE(" ");
}
Expand Down Expand Up @@ -393,6 +432,8 @@ gnc_ui_to_account (AccountWindow *aw)
GdkRGBA color;
gboolean flag;
gnc_numeric balance;
gnc_numeric balance_limit;
gint balance_limit_valid;
gboolean use_equity, nonstd;
time64 date;
gint index, old_scu, new_scu;
Expand Down Expand Up @@ -505,6 +546,50 @@ gnc_ui_to_account (AccountWindow *aw)
if (parent_account != gnc_account_get_parent (account))
gnc_account_append_child (parent_account, account);

balance_limit_valid = gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT(aw->higher_balance_limit_edit),
&balance_limit, TRUE, NULL);

if (balance_limit_valid == 0)
{
if (aw->balance_is_reversed)
{
balance_limit = gnc_numeric_neg (balance_limit);
xaccAccountSetLowerBalanceLimit (account, balance_limit);
}
else
xaccAccountSetHigherBalanceLimit (account, balance_limit);
}

if (balance_limit_valid == -1)
{
if (aw->balance_is_reversed)
xaccAccountClearLowerBalanceLimit (account);
else
xaccAccountClearHigherBalanceLimit (account);
}

balance_limit_valid = gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT(aw->lower_balance_limit_edit),
&balance_limit, TRUE, NULL);

if (balance_limit_valid == 0)
{
if (aw->balance_is_reversed)
{
balance_limit = gnc_numeric_neg (balance_limit);
xaccAccountSetHigherBalanceLimit (account, balance_limit);
}
else
xaccAccountSetLowerBalanceLimit (account, balance_limit);
}

if (balance_limit_valid == -1)
{
if (aw->balance_is_reversed)
xaccAccountClearHigherBalanceLimit (account);
else
xaccAccountClearLowerBalanceLimit (account);
}

xaccAccountCommitEdit (account);

balance = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT(aw->opening_balance_edit));
Expand Down Expand Up @@ -811,6 +896,10 @@ gnc_common_ok (AccountWindow *aw)
gnc_commodity * commodity;
gchar *fullname, *fullname_parent;
const gchar *name, *separator;
gboolean higher_limit_valid;
gnc_numeric higher_balance_limit;
gboolean lower_limit_valid;
gnc_numeric lower_balance_limit;

ENTER("aw %p", aw);
root = gnc_book_get_root_account (aw->book);
Expand Down Expand Up @@ -892,6 +981,34 @@ gnc_common_ok (AccountWindow *aw)
return FALSE;
}

/* check for higher balance limit greater than lower */
higher_limit_valid = gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT(aw->higher_balance_limit_edit),
&higher_balance_limit, TRUE, NULL);

lower_limit_valid = gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT(aw->lower_balance_limit_edit),
&lower_balance_limit, TRUE, NULL);

if ((lower_limit_valid == 0) && (higher_limit_valid == 0))
{
gint compare = gnc_numeric_compare (higher_balance_limit,
lower_balance_limit);

if ((compare == 0) && (!gnc_numeric_zero_p (higher_balance_limit)))
{
const char *message = _("Balance limits must be different unless they are both zero.");
gnc_error_dialog (GTK_WINDOW(aw->dialog), "%s", message);
LEAVE("invalid balance limit, both the same but not zero");
return FALSE;
}
else if (compare == -1)
{
const char *message = _("The lower balance limit must be less than the higher limit.");
gnc_error_dialog (GTK_WINDOW(aw->dialog), "%s", message);
LEAVE("invalid balance limit, lower limit not less than upper");
return FALSE;
}
}

LEAVE("passed");
return TRUE;
}
Expand Down Expand Up @@ -1483,6 +1600,27 @@ gnc_account_window_create (GtkWindow *parent, AccountWindow *aw)
g_signal_connect (G_OBJECT(selection), "changed",
G_CALLBACK(gnc_account_parent_changed_cb), aw);

aw->balance_grid = GTK_WIDGET(gtk_builder_get_object (builder, "balance_grid"));

box = GTK_WIDGET(gtk_builder_get_object (builder, "higher_balance_limit_hbox"));
aw->higher_balance_limit_edit = gnc_amount_edit_new ();
gtk_box_pack_start (GTK_BOX(box), aw->higher_balance_limit_edit, TRUE, TRUE, 0);
gnc_amount_edit_set_evaluate_on_enter (GNC_AMOUNT_EDIT(aw->higher_balance_limit_edit), TRUE);
gnc_amount_edit_set_validate_on_change (GNC_AMOUNT_EDIT(aw->higher_balance_limit_edit), TRUE);
gnc_amount_edit_show_warning_symbol (GNC_AMOUNT_EDIT(aw->higher_balance_limit_edit), TRUE);
gtk_widget_show (aw->higher_balance_limit_edit);

box = GTK_WIDGET(gtk_builder_get_object (builder, "lower_balance_limit_hbox"));
aw->lower_balance_limit_edit = gnc_amount_edit_new ();
gtk_box_pack_start (GTK_BOX(box), aw->lower_balance_limit_edit, TRUE, TRUE, 0);
gnc_amount_edit_set_evaluate_on_enter (GNC_AMOUNT_EDIT(aw->lower_balance_limit_edit), TRUE);
gnc_amount_edit_set_validate_on_change (GNC_AMOUNT_EDIT(aw->lower_balance_limit_edit), TRUE);
gnc_amount_edit_show_warning_symbol (GNC_AMOUNT_EDIT(aw->lower_balance_limit_edit), TRUE);
gtk_widget_show (aw->lower_balance_limit_edit);

aw->more_properties_page =
gtk_notebook_get_nth_page (GTK_NOTEBOOK(aw->notebook), 1);

aw->opening_balance_button = GTK_WIDGET(gtk_builder_get_object (builder, "opening_balance_button"));
aw->tax_related_button = GTK_WIDGET(gtk_builder_get_object (builder, "tax_related_button"));
aw->placeholder_button = GTK_WIDGET(gtk_builder_get_object (builder, "placeholder_button"));
Expand Down Expand Up @@ -1510,7 +1648,7 @@ gnc_account_window_create (GtkWindow *parent, AccountWindow *aw)
gtk_widget_show (date_edit);

aw->opening_balance_page =
gtk_notebook_get_nth_page (GTK_NOTEBOOK(aw->notebook), 1);
gtk_notebook_get_nth_page (GTK_NOTEBOOK(aw->notebook), 2);

aw->opening_equity_radio = GTK_WIDGET(gtk_builder_get_object (builder,
"opening_equity_radio"));
Expand Down Expand Up @@ -1965,6 +2103,14 @@ gnc_ui_edit_account_window (GtkWindow *parent, Account *account)
if (xaccAccountGetSplitList (account) != NULL)
gtk_widget_hide (aw->opening_balance_page);

// Note: we are hiding the notebook page here, when other items
// are added this will need adjusting.
if (gnc_account_n_children (account) > 0)
{
gtk_widget_hide (GTK_WIDGET(aw->balance_grid));
gtk_widget_hide (GTK_WIDGET(aw->more_properties_page));
}

parent_acct = gnc_account_get_parent (account);
if (parent_acct == NULL)
parent_acct = account; // must be at the root
Expand Down
139 changes: 135 additions & 4 deletions gnucash/gtkbuilder/dialog-account.glade
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<!-- Generated with glade 3.40.0 -->
<interface>
<requires lib="gtk+" version="3.22"/>
<object class="GtkDialog" id="account_cascade_dialog">
Expand Down Expand Up @@ -1682,7 +1682,138 @@
</packing>
</child>
<child>
<object class="GtkBox" id="vbox302">
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="border-width">6</property>
<property name="orientation">vertical</property>
<property name="spacing">18</property>
<child>
<!-- n-columns=2 n-rows=3 -->
<object class="GtkGrid" id="balance_grid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="hexpand">True</property>
<property name="row-spacing">3</property>
<property name="column-spacing">6</property>
<child>
<object class="GtkLabel" id="higher_balance_label">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">end</property>
<property name="label" translatable="yes">_Higher Balance Limit</property>
<property name="use-underline">True</property>
<property name="mnemonic-widget">higher_balance_limit_hbox</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
<object class="GtkBox" id="higher_balance_limit_hbox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="tooltip-text" translatable="yes">If a value is present, an indication can be shown in the chart of accounts when today's balance is more than this value.

i.e.
Today's balance of -90 will show icon if limit is set to -100
Today's balance of 100 will show icon if limit is set to 90

Clear the entry to have no warning.</property>
<property name="hexpand">True</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
<object class="GtkBox" id="lower_balance_limit_hbox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="tooltip-text" translatable="yes">If a value is present, an indication can be shown in the chart of accounts when today's balance is less than this value.

i.e.
Today's balance of -100 will show icon if limit is set to -90
Today's balance of 90 will show icon if limit is set to 100

Clear the entry to have no warning.</property>
<property name="hexpand">True</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="lower_balance_label">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">end</property>
<property name="label" translatable="yes">_Lower Balance Limit</property>
<property name="use-underline">True</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Balance Limit</property>
<attributes>
<attribute name="weight" value="bold"/>
</attributes>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
<property name="width">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<style>
<class name="gnc-class-account"/>
</style>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child type="tab">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">More Properties</property>
</object>
<packing>
<property name="position">1</property>
<property name="tab-fill">False</property>
</packing>
</child>
<child>
<object class="GtkBox" id="vbox1">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="border-width">6</property>
Expand Down Expand Up @@ -1857,7 +1988,7 @@
</style>
</object>
<packing>
<property name="position">1</property>
<property name="position">2</property>
</packing>
</child>
<child type="tab">
Expand All @@ -1868,7 +1999,7 @@
<property name="justify">center</property>
</object>
<packing>
<property name="position">1</property>
<property name="position">2</property>
<property name="tab-fill">False</property>
</packing>
</child>
Expand Down

0 comments on commit fb84cf7

Please sign in to comment.