Skip to content

Commit c2ef684

Browse files
committed
Correct check amount when printed from subaccount
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" (e.g. from one bank subaccount to another bank subaccount) and external transfers, 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 communicates the parent account (when viewed from a "subccount" register) to the check printing dialog so that the check amount matches the amount displayed in the register.
1 parent 2ca6cb2 commit c2ef684

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

gnucash/gnome/dialog-payment.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ gnc_payment_ok_cb (G_GNUC_UNUSED GtkWidget *widget, gpointer data)
10411041
Split *split = xaccTransFindSplitByAccount (pw->tx_info->txn, pw->xfer_acct);
10421042
GList *splits = NULL;
10431043
splits = g_list_append(splits, split);
1044-
gnc_ui_print_check_dialog_create(NULL, splits);
1044+
gnc_ui_print_check_dialog_create(NULL, splits, NULL);
10451045
g_list_free (splits);
10461046
}
10471047

gnucash/gnome/dialog-print-check.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ struct _print_check_dialog
278278

279279
Split *split;
280280
GList *splits;
281+
Account* account;
281282

282283
GtkWidget *format_combobox;
283284
gint format_max;
@@ -369,6 +370,44 @@ get_check_address( PrintCheckDialog *pcd)
369370
return address;
370371
}
371372

373+
struct _trans_amount
374+
{
375+
const Transaction* trans;
376+
gnc_numeric amount;
377+
};
378+
379+
static void
380+
subtotal_subaccount(const Account *account, struct _trans_amount* trans_amount)
381+
{
382+
/* Get the amount of this account in the transaction.*/
383+
gnc_numeric amount = xaccTransGetAccountAmount(trans_amount->trans, account);
384+
/* Accumulate. */
385+
trans_amount->amount = gnc_numeric_add_fixed(trans_amount->amount, amount);
386+
}
387+
388+
/* This function returns the amount of the check.
389+
*/
390+
static gnc_numeric
391+
get_check_amount(PrintCheckDialog *pcd)
392+
{
393+
gnc_numeric amount;
394+
if (pcd->account)
395+
{
396+
/* A parent account, e.g. from a subaccount register plugin page.
397+
* Subtotal the amount of all splits from descendant accounts. */
398+
struct _trans_amount trans_amount;
399+
trans_amount.trans = xaccSplitGetParent(pcd->split);
400+
trans_amount.amount = gnc_numeric_zero();
401+
gnc_account_foreach_descendant(pcd->account, (AccountCb)subtotal_subaccount, &trans_amount);
402+
amount = trans_amount.amount;
403+
}
404+
else
405+
{
406+
/* Print just the amount of the split. */
407+
amount = xaccSplitGetAmount(pcd->split);
408+
}
409+
return gnc_numeric_abs(amount);
410+
}
372411

373412
//@{
374413
/** @name Split printing functions */
@@ -1606,10 +1645,13 @@ initialize_format_combobox (PrintCheckDialog *pcd)
16061645
/*****************************************************
16071646
* gnc_ui_print_check_dialog_create *
16081647
* make a new print check dialog and wait for it. *
1648+
* If account is given, this is a parent account to *
1649+
* subtotal the amount of all splits under it. *
16091650
*****************************************************/
16101651
void
16111652
gnc_ui_print_check_dialog_create(GtkWidget *parent,
1612-
GList *splits)
1653+
GList *splits,
1654+
Account* account)
16131655
{
16141656
PrintCheckDialog *pcd;
16151657
GtkBuilder *builder;
@@ -1620,6 +1662,7 @@ gnc_ui_print_check_dialog_create(GtkWidget *parent,
16201662
pcd = g_new0(PrintCheckDialog, 1);
16211663
pcd->caller_window = GTK_WINDOW(parent);
16221664
pcd->splits = g_list_copy(splits);
1665+
pcd->account = account;
16231666

16241667
builder = gtk_builder_new();
16251668
gnc_builder_add_from_file (builder, "dialog-print-check.glade", "adjustment1");
@@ -2112,7 +2155,7 @@ draw_page_items(GtkPrintContext *context,
21122155
trans = xaccSplitGetParent(pcd->split);
21132156
/* This was valid when the check printing dialog was instantiated. */
21142157
g_return_if_fail(trans);
2115-
amount = gnc_numeric_abs(xaccSplitGetAmount(pcd->split));
2158+
amount = get_check_amount(pcd);
21162159

21172160
if (format->font)
21182161
default_desc = pango_font_description_from_string(format->font);

gnucash/gnome/dialog-print-check.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
typedef struct _print_check_dialog PrintCheckDialog;
3030

3131
void gnc_ui_print_check_dialog_create(GtkWidget *parent,
32-
GList *splits);
32+
GList *splits,
33+
Account* account);
3334

3435
#endif

gnucash/gnome/gnc-plugin-page-register.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,7 +3458,7 @@ gnc_plugin_page_register_cmd_print_check (GSimpleAction *simple,
34583458
Transaction* trans;
34593459
GList* splits = NULL, *item;
34603460
GNCLedgerDisplayType ledger_type;
3461-
Account* account;
3461+
Account* account, *subaccount = NULL;
34623462
GtkWidget* window;
34633463

34643464
ENTER ("(action %p, page %p)", simple, page);
@@ -3474,13 +3474,19 @@ gnc_plugin_page_register_cmd_print_check (GSimpleAction *simple,
34743474
account = gnc_plugin_page_register_get_account (page);
34753475
split = gnc_split_register_get_current_split (reg);
34763476
trans = xaccSplitGetParent (split);
3477+
if (ledger_type == LD_SUBACCOUNT)
3478+
{
3479+
/* Set up subaccount printing, where the check amount matches the
3480+
* value displayed in the register. */
3481+
subaccount = account;
3482+
}
34773483

34783484
if (split && trans)
34793485
{
34803486
if (xaccSplitGetAccount (split) == account)
34813487
{
34823488
splits = g_list_prepend (splits, split);
3483-
gnc_ui_print_check_dialog_create (window, splits);
3489+
gnc_ui_print_check_dialog_create (window, splits, subaccount);
34843490
g_list_free (splits);
34853491
}
34863492
else
@@ -3491,7 +3497,7 @@ gnc_plugin_page_register_cmd_print_check (GSimpleAction *simple,
34913497
if (split)
34923498
{
34933499
splits = g_list_prepend (splits, split);
3494-
gnc_ui_print_check_dialog_create (window, splits);
3500+
gnc_ui_print_check_dialog_create (window, splits, subaccount);
34953501
g_list_free (splits);
34963502
}
34973503
}
@@ -3544,7 +3550,7 @@ gnc_plugin_page_register_cmd_print_check (GSimpleAction *simple,
35443550
}
35453551
}
35463552
}
3547-
gnc_ui_print_check_dialog_create (window, splits);
3553+
gnc_ui_print_check_dialog_create (window, splits, NULL);
35483554
}
35493555
else
35503556
{

0 commit comments

Comments
 (0)