my( $pr_total, @pr_cust_bill ) = $self->previous; #previous balance
# my( $cr_total, @cr_cust_credit ) = $self->cust_credit; #credits
#my $balance_due = $self->owed + $pr_total - $cr_total;
- my $balance_due = $self->owed + $pr_total;
+ my $balance_due = $self->owed;
+ if ( $self->enable_previous ) {
+ $balance_due += $pr_total;
+ }
+ # otherwise the previous balance is not shown, so including it in the
+ # balance due is just confusing
# the sum of amount owed on all invoices
# (this is used in the summary & on the payment coupon)
# "balance_date_range" unfortunately is unsuitable for this, since it
# cares about application dates. We want to know the sum of all
# _top-level transactions_ dated before the last invoice.
+ #
+ # still do this for the "Previous Balance" line of the summary block
my @sql =
map "$_ WHERE _date <= ? AND custnum = ?", (
"SELECT COALESCE( SUM(charged), 0 ) FROM cust_bill",
# longer stored in the database)
$invoice_data{'true_previous_balance'} = $last_bill_balance;
- # the change in balance from immediately after that invoice
- # to immediately before this one
- my $before_this_bill_balance = 0;
+ # Now, get all applications of credits/payments dated on or after the
+ # previous bill, to invoices before the current bill. (The
+ # credit/payment date restriction prevents these from intersecting
+ # the "Previous Balance" set.)
+ # These are "adjustments". The past due balance will be shown as
+ # Previous Balance - Adjustments.
+ my $adjustments = 0;
+ @sql = map {
+ "SELECT COALESCE(SUM(y.amount),0) FROM $_ JOIN cust_bill USING (invnum)
+ WHERE cust_bill._date < ?
+ AND x._date >= ?
+ AND cust_bill.custnum = ?"
+ } "cust_credit AS x JOIN cust_credit_bill y USING (crednum)",
+ "cust_pay AS x JOIN cust_bill_pay y USING (paynum)"
+ ;
foreach (@sql) {
my $delta = FS::Record->scalar_sql(
$_,
- $self->_date - 1,
+ $self->_date,
+ $last_bill->_date,
$self->custnum,
);
- $before_this_bill_balance += $delta;
+ $adjustments += $delta;
}
- $invoice_data{'balance_adjustments'} =
- sprintf("%.2f", $last_bill_balance - $before_this_bill_balance);
+ $invoice_data{'balance_adjustments'} = sprintf("%.2f", $adjustments);
warn sprintf("BALANCE ADJUSTMENTS: %.2f\n\n",
$invoice_data{'balance_adjustments'}
# usage subtotals
if ( $conf->exists('usage_class_summary')
and $self->can('_items_usage_class_summary') ) {
- my @usage_subtotals = $self->_items_usage_class_summary(escape => $escape_function);
+ my @usage_subtotals = $self->_items_usage_class_summary(escape => $escape_function, 'money_char' => $other_money_char);
if ( @usage_subtotals ) {
unshift @sections, $usage_subtotals[0]->{section}; # do not summarize
unshift @detail_items, @usage_subtotals;
# if the current line item is waiting to go out, and the one we're about
# to start is not bundled, then push out the current one and start a new
# one.
+ if ( $d ) {
+ $d->{amount} = $d->{setup_amount} + $d->{recur_amount};
+ }
foreach ( $s, $r, ($opt{skip_usage} ? () : $u ), $d ) {
if ( $_ && !$cust_bill_pkg->hidden ) {
$_->{amount} = sprintf( "%.2f", $_->{amount} );
# $item_discount->{amount} is negative
if ( $d and $cust_bill_pkg->hidden ) {
- $d->{amount} += $item_discount->{amount};
+ $d->{setup_amount} += $item_discount->{setup_amount};
+ $d->{recur_amount} += $item_discount->{recur_amount};
} else {
$d = $item_discount;
$_ = &{$escape_function}($_) foreach @{ $d->{ext_description} };
# update the active line (before the discount) to show the
# original price (whether this is a hidden line or not)
- #
- # quotation discounts keep track of setup and recur; invoice
- # discounts currently don't
- if ( exists $item_discount->{setup_amount} ) {
- $s->{amount} -= $item_discount->{setup_amount} if $s;
- $r->{amount} -= $item_discount->{recur_amount} if $r;
-
- } else {
-
- # $active_line is the line item hashref for the line that will
- # show the original price
- # (use the recur or single line for the package, unless we're
- # showing a setup line for a package with no recurring fee)
- my $active_line = $r;
- if ( $type eq 'S' ) {
- $active_line = $s;
- }
- $active_line->{amount} -= $item_discount->{amount};
-
- }
+ $s->{amount} -= $item_discount->{setup_amount} if $s;
+ $r->{amount} -= $item_discount->{recur_amount} if $r;
} # if there are any discounts
} # if this is an appropriate place to show discounts
}
+ # discount amount is internally split up
+ if ( $d ) {
+ $d->{amount} = $d->{setup_amount} + $d->{recur_amount};
+ }
+
foreach ( $s, $r, ($opt{skip_usage} ? () : $u ), $d ) {
if ( $_ ) {
$_->{amount} = sprintf( "%.2f", $_->{amount} ),