X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FReport%2FTable.pm;h=ffa11724041c56e92cf370bad9bd12bd39f9b9e0;hb=adcf4e1d0e02f9e91513bdccd4a8d811f052a7f6;hp=cba968b43fc6ea19f898b633d06f7bc25235e7d2;hpb=aa9537af853969b34d2da6ca855f1d2fcdec16d4;p=freeside.git diff --git a/FS/FS/Report/Table.pm b/FS/FS/Report/Table.pm index cba968b43..ffa117240 100644 --- a/FS/FS/Report/Table.pm +++ b/FS/FS/Report/Table.pm @@ -272,17 +272,53 @@ sub netrefunds { sub discounted { my( $self, $speriod, $eperiod, $agentnum, %opt) = @_; - $self->scalar_sql('SELECT SUM(cust_bill_pkg_discount.amount) - FROM cust_bill_pkg_discount - JOIN cust_bill_pkg USING ( billpkgnum ) - JOIN cust_bill USING ( invnum ) - JOIN cust_main USING ( custnum ) - WHERE '. $self->in_time_period_and_agent( $speriod, - $eperiod, - $agentnum, - 'cust_bill._date' - ). - $self->for_opts(%opt) + + my $sql = 'SELECT SUM('; + if ($opt{'setuprecur'}) { + $sql .= < 0) + OR (COALESCE(cust_bill_pkg.unitrecur,0) > 0)) + THEN +EOF + if ($opt{'setuprecur'} eq 'setup') { + $sql .= ' (COALESCE(cust_bill_pkg.unitsetup,0)'; + } elsif ($opt{'setuprecur'} eq 'recur') { + $sql .= ' (COALESCE(cust_bill_pkg.unitrecur,0)'; + } else { + die 'Unrecognized value for setuprecur'; + } + $sql .= ' / (COALESCE(cust_bill_pkg.unitsetup,0) + COALESCE(cust_bill_pkg.unitrecur,0)))'; + $sql .= " * cust_bill_pkg_discount.amount\n"; + $sql .= <scalar_sql( + $sql + . 'WHERE ' + . $self->in_time_period_and_agent( $speriod, + $eperiod, + $agentnum, + 'cust_bill._date' + ) + . $self->for_opts(%opt) ); } @@ -901,6 +937,7 @@ sub with_classnum { $classnum = [ $classnum ] if !ref($classnum); @$classnum = grep /^\d+$/, @$classnum; + return '' if !@$classnum; my $in = 'IN ('. join(',', @$classnum). ')'; if ( $use_override ) {