+# Join to get reason text and void username to avoid two extra query per row
+my $addl_from_void = join(' ',
+ $addl_from,
+ FS::UI::Web::join_cust_main('cust_credit_void'),
+ ' LEFT JOIN reason ON (reason.reasonnum = cust_credit_void.void_reasonnum) ',
+ ' LEFT JOIN access_user as vusers '.
+ 'on (vusers.usernum = cust_credit_void.void_usernum) ',
+);
+
+my $where_void = $where;
+$where_void =~ s/cust_credit/cust_credit_void/g;
+
+my $sql_query_void = {
+ 'table' => 'cust_credit_void',
+ 'select' => join(', ',@select_void),
+ 'hashref' => {},
+ 'extra_sql' => $where_void,
+ 'addl_from' => $addl_from_void,
+};
+
+if ($show_voided_credits) {
+
+ $sql_query = [$sql_query, $sql_query_void];
+
+ my $count_cust_credit;
+ my $count_cust_credit_void;
+ my $count_sum;
+
+ # Expected fields for count query are count, sum
+ # Get those totals here, and send a fake count query
+ my $count_row = qsearchs({
+ table => 'cust_credit',
+ select => 'count(*), sum(amount)',
+ extra_sql => $where,
+ addl_from => $addl_from . FS::UI::Web::join_cust_main('cust_credit'),
+ });
+ $count_cust_credit = $count_row->count || 0;
+ $count_sum = $count_row->sum || 0;
+
+ $count_row = qsearchs({
+ table => 'cust_credit_void',
+ select => 'count(*)',
+ extra_sql => $where_void,
+ addl_from => $addl_from_void,
+ });
+ $count_cust_credit_void = $count_row->count || 0;
+
+ my $count_combined = $count_cust_credit + $count_cust_credit_void;
+
+ # Fake count query providing needed values
+ $count_query = "SELECT $count_combined as count, $count_sum as sum";
+}
+