+
+ if ( $self->custnum && $self->invnum ) {
+
+ my $last_bill = $self->previous_bill;
+ if ( $last_bill ) {
+
+ # "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.
+ my @sql = (
+ 'SELECT SUM(charged) FROM cust_bill WHERE _date <= ? AND custnum = ?',
+ 'SELECT -1*SUM(amount) FROM cust_credit WHERE _date <= ? AND custnum = ?',
+ 'SELECT -1*SUM(paid) FROM cust_pay WHERE _date <= ? AND custnum = ?',
+ 'SELECT SUM(refund) FROM cust_refund WHERE _date <= ? AND custnum = ?',
+ );
+
+ # the customer's current balance immediately after generating the last
+ # bill
+
+ my $last_bill_balance = $last_bill->charged;
+ foreach (@sql) {
+ #warn "$_\n";
+ my $delta = FS::Record->scalar_sql(
+ $_,
+ $last_bill->_date - 1,
+ $self->custnum,
+ );
+ #warn "$delta\n";
+ $last_bill_balance += $delta;
+ }
+
+ $last_bill_balance = sprintf("%.2f", $last_bill_balance);
+
+ warn sprintf("LAST BILL: INVNUM %d, DATE %s, BALANCE %.2f\n\n",
+ $last_bill->invnum,
+ $self->time2str_local('%D', $last_bill->_date),
+ $last_bill_balance
+ ) if $DEBUG > 0;
+ # ("true_previous_balance" is a terrible name, but at least it's no
+ # 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;
+ foreach (@sql) {
+ #warn "$_\n";
+ my $delta = FS::Record->scalar_sql(
+ $_,
+ $self->_date - 1,
+ $self->custnum,
+ );
+ #warn "$delta\n";
+ $before_this_bill_balance += $delta;
+ }
+ $invoice_data{'balance_adjustments'} =
+ sprintf("%.2f", $last_bill_balance - $before_this_bill_balance);
+
+ warn sprintf("BALANCE ADJUSTMENTS: %.2f\n\n",
+ $invoice_data{'balance_adjustments'}
+ ) if $DEBUG > 0;
+
+ # the sum of amount owed on all previous invoices
+ # ($pr_total is used elsewhere but not as $previous_balance)
+ $invoice_data{'previous_balance'} = sprintf("%.2f", $pr_total);
+
+ $invoice_data{'last_bill'} = {
+ '_date' => $last_bill->_date, #unformatted
+ };
+ my (@payments, @credits);
+ # for formats that itemize previous payments
+ foreach my $cust_pay ( qsearch('cust_pay', {
+ 'custnum' => $self->custnum,
+ '_date' => { op => '>=',
+ value => $last_bill->_date }
+ } ) )
+ {
+ next if $cust_pay->_date > $self->_date;
+ push @payments, {
+ '_date' => $cust_pay->_date,
+ 'date' => $self->time2str_local('long', $cust_pay->_date, $format),
+ 'payinfo' => $cust_pay->payby_payinfo_pretty,
+ 'amount' => sprintf('%.2f', $cust_pay->paid),
+ };
+ # not concerned about applications
+ }
+ foreach my $cust_credit ( qsearch('cust_credit', {
+ 'custnum' => $self->custnum,
+ '_date' => { op => '>=',
+ value => $last_bill->_date }
+ } ) )
+ {
+ next if $cust_credit->_date > $self->_date;
+ push @credits, {
+ '_date' => $cust_credit->_date,
+ 'date' => $self->time2str_local('long', $cust_credit->_date, $format),
+ 'creditreason'=> $cust_credit->reason,
+ 'amount' => sprintf('%.2f', $cust_credit->amount),
+ };
+ }
+ $invoice_data{'previous_payments'} = \@payments;
+ $invoice_data{'previous_credits'} = \@credits;
+ } else {
+ # there is no $last_bill
+ $invoice_data{'true_previous_balance'} =
+ $invoice_data{'balance_adjustments'} =
+ $invoice_data{'previous_balance'} = '0.00';
+ $invoice_data{'previous_payments'} = [];
+ $invoice_data{'previous_credits'} = [];
+ }
+ } # if this is an invoice