Skip to content

Commit

Permalink
Correct check amount when printed from subaccount
Browse files Browse the repository at this point in the history
Previously, GnuCash would include only a single split's amount as the check
amount. If a "bank" account has multiple subaccounts for budgeting reasons, and
a transaction involves both "internal" and external transfers (e.g. from one
bank subaccount to another bank subaccount), then even when a "subaccount"
register showing the top-level bank account is printed from, the check will
have the amount of the first split rather than the total of all splits which
are from subaccounts.

This change adds an option to the check printing dialog to subtotal the amounts
of all splits if they belong to subaccounts of a given account.
  • Loading branch information
mabrowning committed Nov 4, 2023
1 parent 2ca6cb2 commit fd0a85a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion gnucash/gnome/dialog-payment.c
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ gnc_payment_ok_cb (G_GNUC_UNUSED GtkWidget *widget, gpointer data)
Split *split = xaccTransFindSplitByAccount (pw->tx_info->txn, pw->xfer_acct);
GList *splits = NULL;
splits = g_list_append(splits, split);
gnc_ui_print_check_dialog_create(NULL, splits);
gnc_ui_print_check_dialog_create(NULL, splits, NULL);
g_list_free (splits);
}

Expand Down
41 changes: 39 additions & 2 deletions gnucash/gnome/dialog-print-check.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ struct _print_check_dialog

Split *split;
GList *splits;
Account* account;

GtkWidget *format_combobox;
gint format_max;
Expand Down Expand Up @@ -1606,10 +1607,13 @@ initialize_format_combobox (PrintCheckDialog *pcd)
/*****************************************************
* gnc_ui_print_check_dialog_create *
* make a new print check dialog and wait for it. *
* If account is given, this is a parent account to *
* subtotal the amount of all splits under it. *
*****************************************************/
void
gnc_ui_print_check_dialog_create(GtkWidget *parent,
GList *splits)
GList *splits,
Account* account)
{
PrintCheckDialog *pcd;
GtkBuilder *builder;
Expand All @@ -1620,6 +1624,7 @@ gnc_ui_print_check_dialog_create(GtkWidget *parent,
pcd = g_new0(PrintCheckDialog, 1);
pcd->caller_window = GTK_WINDOW(parent);
pcd->splits = g_list_copy(splits);
pcd->account = account;

builder = gtk_builder_new();
gnc_builder_add_from_file (builder, "dialog-print-check.glade", "adjustment1");
Expand Down Expand Up @@ -2112,7 +2117,39 @@ draw_page_items(GtkPrintContext *context,
trans = xaccSplitGetParent(pcd->split);
/* This was valid when the check printing dialog was instantiated. */
g_return_if_fail(trans);
amount = gnc_numeric_abs(xaccSplitGetAmount(pcd->split));

if (pcd->account)
{
/* A parent account, e.g. from a subaccount register plugin page.
* Subtotal the amount of all splits from children accounts. */
GList *children_accounts = NULL;
GList *node, *child;
children_accounts = gnc_account_get_descendants(pcd->account);
children_accounts = g_list_append(children_accounts, pcd->account);
amount = gnc_numeric_zero();

for (node = xaccTransGetSplitList(trans); node; node = node->next)
{
Split *split = node->data;
Account* acct = xaccSplitGetAccount(split);
for (child = children_accounts; child; child = child->next)
{
if (acct == child->data)
{
amount = gnc_numeric_add_fixed(amount, xaccSplitGetAmount(split));
break;
}
}
}
g_list_free(children_accounts);
children_accounts = NULL;
}
else
{
/* Print just the amount of the split. */
amount = xaccSplitGetAmount(pcd->split);
}
amount = gnc_numeric_abs(amount);

if (format->font)
default_desc = pango_font_description_from_string(format->font);
Expand Down
3 changes: 2 additions & 1 deletion gnucash/gnome/dialog-print-check.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
typedef struct _print_check_dialog PrintCheckDialog;

void gnc_ui_print_check_dialog_create(GtkWidget *parent,
GList *splits);
GList *splits,
Account* account);

#endif
14 changes: 10 additions & 4 deletions gnucash/gnome/gnc-plugin-page-register.c
Original file line number Diff line number Diff line change
Expand Up @@ -3458,7 +3458,7 @@ gnc_plugin_page_register_cmd_print_check (GSimpleAction *simple,
Transaction* trans;
GList* splits = NULL, *item;
GNCLedgerDisplayType ledger_type;
Account* account;
Account* account, *subaccount = NULL;
GtkWidget* window;

ENTER ("(action %p, page %p)", simple, page);
Expand All @@ -3474,13 +3474,19 @@ gnc_plugin_page_register_cmd_print_check (GSimpleAction *simple,
account = gnc_plugin_page_register_get_account (page);
split = gnc_split_register_get_current_split (reg);
trans = xaccSplitGetParent (split);
if (ledger_type == LD_SUBACCOUNT)
{
/* Set up subaccount printing, where the check amount matches the
* value displayed in the register. */
subaccount = account;
}

if (split && trans)
{
if (xaccSplitGetAccount (split) == account)
{
splits = g_list_prepend (splits, split);
gnc_ui_print_check_dialog_create (window, splits);
gnc_ui_print_check_dialog_create (window, splits, subaccount);
g_list_free (splits);
}
else
Expand All @@ -3491,7 +3497,7 @@ gnc_plugin_page_register_cmd_print_check (GSimpleAction *simple,
if (split)
{
splits = g_list_prepend (splits, split);
gnc_ui_print_check_dialog_create (window, splits);
gnc_ui_print_check_dialog_create (window, splits, subaccount);
g_list_free (splits);
}
}
Expand Down Expand Up @@ -3544,7 +3550,7 @@ gnc_plugin_page_register_cmd_print_check (GSimpleAction *simple,
}
}
}
gnc_ui_print_check_dialog_create (window, splits);
gnc_ui_print_check_dialog_create (window, splits, NULL);
}
else
{
Expand Down

0 comments on commit fd0a85a

Please sign in to comment.