@ISA = qw( FS::cust_main_Mixin FS::Record );
-$DEBUG = 0;
+$DEBUG = 1;
$me = '[FS::cust_bill]';
#ask FS::UID to run this stuff for us later
=item charged - amount of this invoice
+=item invoice_terms - optional terms override for this specific invoice
+
+=back
+
+Customer info at invoice generation time
+
+=over 4
+
+=item previous_balance
+
+=item billing_balance
+
=back
Deprecated
#what's with all the sprintf('%10.2f')'s in here? will it cause any
# (alignment in text invoice?) problems to change them all to '%.2f' ?
+# yes: fixed width (dot matrix) text printing will be borked
sub print_generic {
my( $self, %params ) = @_;
$line_item_line =~ s/\$(\w+)/'. \$_tr_line->{$1}. '/g;
push @template, " \$OUT .= '$line_item_line';";
}
-
+
push @template, '}',
'--@]';
-
+ #' doh, gvim
} elsif ( $line =~ /^%%TotalDetails\s*$/ ) {
push @template, '[@--',
sub terms {
my $self = shift;
- #check for an invoice- specific override (eventually)
+ #check for an invoice-specific override
+ return $self->invoice_terms if $self->invoice_terms;
#check for a customer- specific override
- return $self->cust_main->invoice_terms
- if $self->cust_main->invoice_terms;
+ my $cust_main = $self->cust_main;
+ return $cust_main->invoice_terms if $cust_main->invoice_terms;
#use configured default
$conf->config('invoice_default_terms') || '';
time2str('%x', $self->_date);
}
+use vars qw(%pkg_category_cache);
sub _items_sections {
my $self = shift;
my $late = shift;
my $summarypage = shift;
my $escape = shift;
- my %s = ();
- my %l = ();
+ my %subtotal = ();
+ my %late_subtotal = ();
my %not_tax = ();
foreach my $cust_bill_pkg ( $self->cust_bill_pkg )
{
-
my $usage = $cust_bill_pkg->usage;
foreach my $display ($cust_bill_pkg->cust_bill_pkg_display) {
next if ( $display->summary && $summarypage );
- my $desc = $display->section;
- my $type = $display->type;
+ my $section = $display->section;
+ my $type = $display->type;
- if ( $cust_bill_pkg->pkgnum > 0 ) {
- $not_tax{$desc} = 1;
- }
+ $not_tax{$section} = 1
+ unless $cust_bill_pkg->pkgnum == 0;
if ( $display->post_total && !$summarypage ) {
if (! $type || $type eq 'S') {
- $l{$desc} += $cust_bill_pkg->setup
- if ( $cust_bill_pkg->setup != 0 );
+ $late_subtotal{$section} += $cust_bill_pkg->setup
+ if $cust_bill_pkg->setup != 0;
}
if (! $type) {
- $l{$desc} += $cust_bill_pkg->recur
- if ( $cust_bill_pkg->recur != 0 );
+ $late_subtotal{$section} += $cust_bill_pkg->recur
+ if $cust_bill_pkg->recur != 0;
}
if ($type && $type eq 'R') {
- $l{$desc} += $cust_bill_pkg->recur - $usage
- if ( $cust_bill_pkg->recur != 0 );
+ $late_subtotal{$section} += $cust_bill_pkg->recur - $usage
+ if $cust_bill_pkg->recur != 0;
}
if ($type && $type eq 'U') {
- $l{$desc} += $usage;
+ $late_subtotal{$section} += $usage;
}
} else {
+
+ next if $cust_bill_pkg->pkgnum == 0 && ! $section;
+
if (! $type || $type eq 'S') {
- $s{$desc} += $cust_bill_pkg->setup
- if ( $cust_bill_pkg->setup != 0 );
+ $subtotal{$section} += $cust_bill_pkg->setup
+ if $cust_bill_pkg->setup != 0;
}
if (! $type) {
- $s{$desc} += $cust_bill_pkg->recur
- if ( $cust_bill_pkg->recur != 0 );
+ $subtotal{$section} += $cust_bill_pkg->recur
+ if $cust_bill_pkg->recur != 0;
}
if ($type && $type eq 'R') {
- $s{$desc} += $cust_bill_pkg->recur - $usage
- if ( $cust_bill_pkg->recur != 0 );
+ $subtotal{$section} += $cust_bill_pkg->recur - $usage
+ if $cust_bill_pkg->recur != 0;
}
if ($type && $type eq 'U') {
- $s{$desc} += $usage;
+ $subtotal{$section} += $usage;
}
}
}
- my %cache = map { $_->categoryname => $_ }
- qsearch( 'pkg_category', {disabled => 'Y'} );
- $cache{$_->categoryname} = $_
- foreach qsearch( 'pkg_category', {disabled => ''} );
+ %pkg_category_cache = ();
push @$late, map { { 'description' => &{$escape}($_),
- 'subtotal' => $l{$_},
+ 'subtotal' => $late_subtotal{$_},
'post_total' => 1,
} }
- sort { $cache{$a}->weight <=> $cache{$b}->weight } keys %l;
+ sort _categorysort keys %late_subtotal;
+
+ my @sections;
+ if ( $summarypage ) {
+ @sections = grep { exists($subtotal{$_}) || ! _pkg_category($_)->disabled }
+ map { $_->categoryname } qsearch('pkg_category', {});
+ } else {
+ @sections = keys %subtotal;
+ }
map { { 'description' => &{$escape}($_),
- 'subtotal' => $s{$_},
+ 'subtotal' => $subtotal{$_},
'summarized' => $not_tax{$_} ? '' : 'Y',
'tax_section' => $not_tax{$_} ? '' : 'Y',
- } }
- sort { $cache{$a}->weight <=> $cache{$b}->weight }
- ( $summarypage
- ? ( grep { exists($s{$_}) || !$cache{$_}->disabled } keys %cache )
- : ( keys %s )
- );
+ }
+ }
+ sort _categorysort @sections;
+
+}
+
+#helper subs for above
+sub _categorysort {
+ _pkg_category($a)->weight <=> _pkg_category($b)->weight;
+}
+
+sub _pkg_category {
+ my $categoryname = shift;
+ $pkg_category_cache{$categoryname} ||=
+ qsearchs( 'pkg_category', { 'categoryname' => $categoryname } );
}
sub _items {
? $_->section eq $section
: 1
}
- grep { $_->summary || !$summary_page }
+ grep { !$_->summary || !$summary_page }
$cust_bill_pkg->cust_bill_pkg_display
)
{
=over 4
-=item begin
-
-Epoch date (UNIX timestamp) setting a lower bound for _date values
+=item _date
-=item end
-
-Epoch date (UNIX timestamp) setting an upper bound for _date values
+List reference of start date, end date, as UNIX timestamps.
=item invnum_min
=item agentnum
+=item charged
+
+List reference of charged limits (exclusive).
+
=item owed
+List reference of charged limits (exclusive).
+
+=item open
+
+flag, return open invoices only
+
=item net
+flag, return net invoices only
+
=item days
=item newest_percust
my @search = ();
- if ( $param->{'begin'} =~ /^(\d+)$/ ) {
- push @search, "cust_bill._date >= $1";
+ #agentnum
+ if ( $param->{'agentnum'} =~ /^(\d+)$/ ) {
+ push @search, "cust_main.agentnum = $1";
}
- if ( $param->{'end'} =~ /^(\d+)$/ ) {
- push @search, "cust_bill._date < $1";
+
+ #_date
+ if ( $param->{_date} ) {
+ my($beginning, $ending) = @{$param->{_date}};
+
+ push @search, "cust_bill._date >= $beginning",
+ "cust_bill._date < $ending";
}
+
+ #invnum
if ( $param->{'invnum_min'} =~ /^(\d+)$/ ) {
push @search, "cust_bill.invnum >= $1";
}
if ( $param->{'invnum_max'} =~ /^(\d+)$/ ) {
push @search, "cust_bill.invnum <= $1";
}
- if ( $param->{'agentnum'} =~ /^(\d+)$/ ) {
- push @search, "cust_main.agentnum = $1";
+
+ #charged
+ if ( $param->{charged} ) {
+ my @charged = ref($param->{charged})
+ ? @{ $param->{charged} }
+ : ($param->{charged});
+
+ push @search, map { s/^charged/cust_bill.charged/; $_; }
+ @charged;
}
- push @search, '0 != '. FS::cust_bill->owed_sql
- if $param->{'open'};
+ my $owed_sql = FS::cust_bill->owed_sql;
+
+ #owed
+ if ( $param->{owed} ) {
+ my @owed = ref($param->{owed})
+ ? @{ $param->{owed} }
+ : ($param->{owed});
+ push @search, map { s/^owed/$owed_sql/; $_; }
+ @owed;
+ }
+ #open/net flags
+ push @search, "0 != $owed_sql"
+ if $param->{'open'};
push @search, '0 != '. FS::cust_bill->net_sql
if $param->{'net'};
+ #days
push @search, "cust_bill._date < ". (time-86400*$param->{'days'})
if $param->{'days'};
+ #newest_percust
if ( $param->{'newest_percust'} ) {
#$distinct = 'DISTINCT ON ( cust_bill.custnum )';
}
+ #agent virtualization
my $curuser = $FS::CurrentUser::CurrentUser;
if ( $curuser->username eq 'fs_queue'
&& $param->{'CurrentUser'} =~ /^(\w+)$/ ) {
warn "$me WARNING: (fs_queue) can't find CurrentUser $username\n";
}
}
-
push @search, $curuser->agentnums_sql;
join(' AND ', @search );