Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct check amount when printed from subaccount #1811

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 45 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 @@ -369,6 +370,44 @@ get_check_address( PrintCheckDialog *pcd)
return address;
}

struct _trans_amount
{
const Transaction* trans;
gnc_numeric amount;
};

static void
subtotal_subaccount(const Account *account, struct _trans_amount* trans_amount)
{
/* Get the amount of this account in the transaction.*/
gnc_numeric amount = xaccTransGetAccountAmount(trans_amount->trans, account);
/* Accumulate. */
trans_amount->amount = gnc_numeric_add_fixed(trans_amount->amount, amount);
}

/* This function returns the amount of the check.
*/
static gnc_numeric
get_check_amount(PrintCheckDialog *pcd)
{
gnc_numeric amount;
if (pcd->account)
{
/* A parent account, e.g. from a subaccount register plugin page.
* Subtotal the amount of all splits from descendant accounts. */
struct _trans_amount trans_amount;
trans_amount.trans = xaccSplitGetParent(pcd->split);
trans_amount.amount = gnc_numeric_zero();
gnc_account_foreach_descendant(pcd->account, (AccountCb)subtotal_subaccount, &trans_amount);
amount = trans_amount.amount;
}
else
{
/* Print just the amount of the split. */
amount = xaccSplitGetAmount(pcd->split);
}
return gnc_numeric_abs(amount);
}

//@{
/** @name Split printing functions */
Expand Down Expand Up @@ -1606,10 +1645,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 +1662,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 +2155,7 @@ 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));
amount = get_check_amount(pcd);

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