1 package FS::Template_Mixin;
4 use vars qw( $DEBUG $me
7 use vars qw( $invoice_lines @buf ); #yuck
8 use List::Util qw(sum);
11 use Text::Template 1.20;
17 use FS::Record qw( qsearch qsearchs );
19 use FS::Misc qw( generate_ps generate_pdf );
26 $me = '[FS::Template_Mixin]';
27 FS::UID->install_callback( sub {
28 my $conf = new FS::Conf; #global
29 $money_char = $conf->config('money_char') || '$';
34 Returns a configuration handle (L<FS::Conf>) set to the customer's locale.
36 If the "mode" pseudo-field is set on the object, the configuration handle
37 will be an L<FS::invoice_conf> for that invoice mode (and the customer's
44 my $mode = $self->get('mode');
45 if ($self->{_conf} and !defined($mode)) {
46 return $self->{_conf};
49 my $cust_main = $self->cust_main;
50 my $locale = $cust_main ? $cust_main->locale : '';
53 if ( ref $mode and $mode->isa('FS::invoice_mode') ) {
54 $mode = $mode->modenum;
55 } elsif ( $mode =~ /\D/ ) {
56 die "invalid invoice mode $mode";
58 $conf = qsearchs('invoice_conf', { modenum => $mode, locale => $locale });
60 $conf = qsearchs('invoice_conf', { modenum => $mode, locale => '' });
61 # it doesn't have a locale, but system conf still might
62 $conf->set('locale' => $locale) if $conf;
65 # if $mode is unspecified, or if there is no invoice_conf matching this mode
66 # and locale, then use the system config only (but with the locale)
67 $conf ||= FS::Conf->new({ 'locale' => $locale });
69 return $self->{_conf} = $conf;
72 =item print_text OPTIONS
74 Returns an text invoice, as a list of lines.
76 Options can be passed as a hash.
78 I<time>, if specified, is used to control the printing of overdue messages. The
79 default is now. It isn't the date of the invoice; that's the `_date' field.
80 It is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
81 L<Time::Local> and L<Date::Parse> for conversion functions.
83 I<template>, if specified, is the name of a suffix for alternate invoices.
85 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
93 %params = %{ shift() };
98 $params{'format'} = 'template'; # for some reason
100 $self->print_generic( %params );
103 =item print_latex HASHREF
105 Internal method - returns a filename of a filled-in LaTeX template for this
106 invoice (Note: add ".tex" to get the actual filename), and a filename of
107 an associated logo (with the .eps extension included).
109 See print_ps and print_pdf for methods that return PostScript and PDF output.
111 Options can be passed as a hash.
113 I<time>, if specified, is used to control the printing of overdue messages. The
114 default is now. It isn't the date of the invoice; that's the `_date' field.
115 It is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
116 L<Time::Local> and L<Date::Parse> for conversion functions.
118 I<template>, if specified, is the name of a suffix for alternate invoices.
119 This is strongly deprecated; see L<FS::invoice_conf> for the right way to
120 customize invoice templates for different purposes.
122 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
131 %params = %{ shift() };
136 $params{'format'} = 'latex';
137 my $conf = $self->conf;
139 # this needs to go away
140 my $template = $params{'template'};
141 # and this especially
142 $template ||= $self->_agent_template
143 if $self->can('_agent_template');
145 my $pkey = $self->primary_key;
146 my $tmp_template = $self->table. '.'. $self->$pkey. '.XXXXXXXX';
148 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
149 my $lh = new File::Temp(
150 TEMPLATE => $tmp_template,
154 ) or die "can't open temp file: $!\n";
156 my $agentnum = $self->agentnum;
158 if ( $template && $conf->exists("logo_${template}.eps", $agentnum) ) {
159 print $lh $conf->config_binary("logo_${template}.eps", $agentnum)
160 or die "can't write temp file: $!\n";
162 print $lh $conf->config_binary('logo.eps', $agentnum)
163 or die "can't write temp file: $!\n";
166 $params{'logo_file'} = $lh->filename;
168 if( $conf->exists('invoice-barcode')
169 && $self->can('invoice_barcode')
170 && $self->invnum ) { # don't try to barcode statements
171 my $png_file = $self->invoice_barcode($dir);
172 my $eps_file = $png_file;
173 $eps_file =~ s/\.png$/.eps/g;
174 $png_file =~ /(barcode.*png)/;
176 $eps_file =~ /(barcode.*eps)/;
179 my $curr_dir = cwd();
181 # after painfuly long experimentation, it was determined that sam2p won't
182 # accept : and other chars in the path, no matter how hard I tried to
183 # escape them, hence the chdir (and chdir back, just to be safe)
184 system('sam2p', '-j:quiet', $png_file, 'EPS:', $eps_file ) == 0
185 or die "sam2p failed: $!\n";
189 $params{'barcode_file'} = $eps_file;
192 my @filled_in = $self->print_generic( %params );
194 my $fh = new File::Temp( TEMPLATE => $tmp_template,
198 ) or die "can't open temp file: $!\n";
199 binmode($fh, ':utf8'); # language support
200 print $fh join('', @filled_in );
203 $fh->filename =~ /^(.*).tex$/ or die "unparsable filename: ". $fh->filename;
204 return ($1, $params{'logo_file'}, $params{'barcode_file'});
210 my $cust_main = $self->cust_main;
211 $cust_main ? $cust_main->agentnum : $self->prospect_main->agentnum;
214 =item print_generic OPTION => VALUE ...
216 Internal method - returns a filled-in template for this invoice as a scalar.
218 See print_ps and print_pdf for methods that return PostScript and PDF output.
220 Non optional options include
221 format - latex, html, template
223 Optional options include
225 template - a value used as a suffix for a configuration template. Please
228 time - a value used to control the printing of overdue messages. The
229 default is now. It isn't the date of the invoice; that's the `_date' field.
230 It is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
231 L<Time::Local> and L<Date::Parse> for conversion functions.
235 unsquelch_cdr - overrides any per customer cdr squelching when true
237 notice_name - overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
239 locale - override customer's locale
243 #what's with all the sprintf('%10.2f')'s in here? will it cause any
244 # (alignment in text invoice?) problems to change them all to '%.2f' ?
245 # yes: fixed width/plain text printing will be borked
247 my( $self, %params ) = @_;
248 my $conf = $self->conf;
250 my $today = $params{today} ? $params{today} : time;
251 warn "$me print_generic called on $self with suffix $params{template}\n"
254 my $format = $params{format};
255 die "Unknown format: $format"
256 unless $format =~ /^(latex|html|template)$/;
258 my $cust_main = $self->cust_main || $self->prospect_main;
259 $cust_main->payname( $cust_main->first. ' '. $cust_main->getfield('last') )
260 unless $cust_main->payname
261 && $cust_main->payby !~ /^(CARD|DCRD|CHEK|DCHK)$/;
263 my $locale = $params{'locale'} || $cust_main->locale;
265 my %delimiters = ( 'latex' => [ '[@--', '--@]' ],
266 'html' => [ '<%=', '%>' ],
267 'template' => [ '{', '}' ],
270 warn "$me print_generic creating template\n"
273 # set the notice name here, and nowhere else.
274 my $notice_name = $params{notice_name}
275 || $conf->config('notice_name')
276 || $self->notice_name;
279 my $template = $params{template} ? $params{template} : $self->_agent_template;
280 my $templatefile = $self->template_conf. $format;
281 $templatefile .= "_$template"
282 if length($template) && $conf->exists($templatefile."_$template");
285 my @invoice_template = map "$_\n", $conf->config($templatefile)
286 or die "cannot load config data $templatefile";
289 if ( $format eq 'latex' && grep { /^%%Detail/ } @invoice_template ) {
290 #change this to a die when the old code is removed
291 warn "old-style invoice template $templatefile; ".
292 "patch with conf/invoice_latex.diff or use new conf/invoice_latex*\n";
294 @invoice_template = _translate_old_latex_format(@invoice_template);
297 warn "$me print_generic creating T:T object\n"
300 my $text_template = new Text::Template(
302 SOURCE => \@invoice_template,
303 DELIMITERS => $delimiters{$format},
306 warn "$me print_generic compiling T:T object\n"
309 $text_template->compile()
310 or die "Can't compile $templatefile: $Text::Template::ERROR\n";
313 # additional substitution could possibly cause breakage in existing templates
316 'notes' => sub { map "$_", @_ },
317 'footer' => sub { map "$_", @_ },
318 'smallfooter' => sub { map "$_", @_ },
319 'returnaddress' => sub { map "$_", @_ },
320 'coupon' => sub { map "$_", @_ },
321 'summary' => sub { map "$_", @_ },
327 s/%%(.*)$/<!-- $1 -->/g;
328 s/\\section\*\{\\textsc\{(.)(.*)\}\}/<p><b><font size="+1">$1<\/font>\U$2<\/b>/g;
329 s/\\begin\{enumerate\}/<ol>/g;
331 s/\\end\{enumerate\}/<\/ol>/g;
332 s/\\textbf\{(.*)\}/<b>$1<\/b>/g;
341 sub { map { s/~/ /g; s/\\\\\*?\s*$/<BR>/; $_; } @_ },
343 sub { map { s/~/ /g; s/\\\\\*?\s*$/<BR>/; $_; } @_ },
349 s/\\hyphenation\{[\w\s\-]+}//;
354 'coupon' => sub { "" },
355 'summary' => sub { "" },
362 s/\\section\*\{\\textsc\{(.*)\}\}/\U$1/g;
363 s/\\begin\{enumerate\}//g;
365 s/\\end\{enumerate\}//g;
366 s/\\textbf\{(.*)\}/$1/g;
373 sub { map { s/~/ /g; s/\\\\\*?\s*$/\n/; $_; } @_ },
375 sub { map { s/~/ /g; s/\\\\\*?\s*$/\n/; $_; } @_ },
380 s/\\\\\*?\s*$/\n/; # dubious
381 s/\\hyphenation\{[\w\s\-]+}//;
385 'coupon' => sub { "" },
386 'summary' => sub { "" },
391 # hashes for differing output formats
392 my %nbsps = ( 'latex' => '~',
393 'html' => '', # '&nbps;' would be nice
394 'template' => '', # not used
396 my $nbsp = $nbsps{$format};
398 my %escape_functions = ( 'latex' => \&_latex_escape,
399 'html' => \&_html_escape_nbsp,#\&encode_entities,
400 'template' => sub { shift },
402 my $escape_function = $escape_functions{$format};
403 my $escape_function_nonbsp = ($format eq 'html')
404 ? \&_html_escape : $escape_function;
406 my %newline_tokens = ( 'latex' => '\\\\',
410 my $newline_token = $newline_tokens{$format};
412 warn "$me generating template variables\n"
415 # generate template variables
419 defined( $conf->config_orbase( "invoice_${format}returnaddress",
423 && length( $conf->config_orbase( "invoice_${format}returnaddress",
429 $returnaddress = join("\n",
430 $conf->config_orbase("invoice_${format}returnaddress", $template)
434 $conf->config_orbase('invoice_latexreturnaddress', $template) ) {
436 my $convert_map = $convert_maps{$format}{'returnaddress'};
439 &$convert_map( $conf->config_orbase( "invoice_latexreturnaddress",
444 } elsif ( grep /\S/, $conf->config('company_address', $cust_main->agentnum) ) {
446 my $convert_map = $convert_maps{$format}{'returnaddress'};
447 $returnaddress = join( "\n", &$convert_map(
448 map { s/( {2,})/'~' x length($1)/eg;
452 ( $conf->config('company_name', $cust_main->agentnum),
453 $conf->config('company_address', $cust_main->agentnum),
460 my $warning = "Couldn't find a return address; ".
461 "do you need to set the company_address configuration value?";
463 $returnaddress = $nbsp;
464 #$returnaddress = $warning;
468 warn "$me generating invoice data\n"
471 my $agentnum = $cust_main->agentnum;
476 'company_name' => scalar( $conf->config('company_name', $agentnum) ),
477 'company_address' => join("\n", $conf->config('company_address', $agentnum) ). "\n",
478 'company_phonenum'=> scalar( $conf->config('company_phonenum', $agentnum) ),
479 'returnaddress' => $returnaddress,
480 'agent' => &$escape_function($cust_main->agent->agent),
482 #invoice/quotation info
483 'no_number' => $params{'no_number'},
484 'invnum' => ( $params{'no_number'} ? '' : $self->invnum ),
485 'quotationnum' => $self->quotationnum,
486 'no_date' => $params{'no_date'},
487 '_date' => ( $params{'no_date'} ? '' : $self->_date ),
488 # workaround for inconsistent behavior in the early plain text
489 # templates; see RT#28271
490 'date' => ( $params{'no_date'}
492 : ($format eq 'template'
494 : $self->time2str_local('long', $self->_date, $format)
497 'today' => $self->time2str_local('long', $today, $format),
498 'terms' => $self->terms,
499 'template' => $template, #params{'template'},
500 'notice_name' => $notice_name, # escape?
501 'current_charges' => sprintf("%.2f", $self->charged),
502 'duedate' => $self->due_date2str('rdate'), #date_format?
505 'custnum' => $cust_main->display_custnum,
506 'prospectnum' => $cust_main->prospectnum,
507 'agent_custid' => &$escape_function($cust_main->agent_custid),
508 ( map { $_ => &$escape_function($cust_main->$_()) } qw(
509 payname company address1 address2 city state zip fax
513 'ship_enable' => $conf->exists('invoice-ship_address'),
514 'unitprices' => $conf->exists('invoice-unitprice'),
515 'smallernotes' => $conf->exists('invoice-smallernotes'),
516 'smallerfooter' => $conf->exists('invoice-smallerfooter'),
517 'balance_due_below_line' => $conf->exists('balance_due_below_line'),
519 #layout info -- would be fancy to calc some of this and bury the template
521 'topmargin' => scalar($conf->config('invoice_latextopmargin', $agentnum)),
522 'headsep' => scalar($conf->config('invoice_latexheadsep', $agentnum)),
523 'textheight' => scalar($conf->config('invoice_latextextheight', $agentnum)),
524 'extracouponspace' => scalar($conf->config('invoice_latexextracouponspace', $agentnum)),
525 'couponfootsep' => scalar($conf->config('invoice_latexcouponfootsep', $agentnum)),
526 'verticalreturnaddress' => $conf->exists('invoice_latexverticalreturnaddress', $agentnum),
527 'addresssep' => scalar($conf->config('invoice_latexaddresssep', $agentnum)),
528 'amountenclosedsep' => scalar($conf->config('invoice_latexcouponamountenclosedsep', $agentnum)),
529 'coupontoaddresssep' => scalar($conf->config('invoice_latexcoupontoaddresssep', $agentnum)),
530 'addcompanytoaddress' => $conf->exists('invoice_latexcouponaddcompanytoaddress', $agentnum),
532 # better hang on to conf_dir for a while (for old templates)
533 'conf_dir' => "$FS::UID::conf_dir/conf.$FS::UID::datasrc",
535 #these are only used when doing paged plaintext
542 $invoice_data{'emt'} = sub { &$escape_function($self->mt(@_)) };
543 # prototype here to silence warnings
544 $invoice_data{'time2str'} = sub ($;$$) { $self->time2str_local(@_, $format) };
546 my $min_sdate = 999999999999;
548 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
549 next unless $cust_bill_pkg->pkgnum > 0;
550 $min_sdate = $cust_bill_pkg->sdate
551 if length($cust_bill_pkg->sdate) && $cust_bill_pkg->sdate < $min_sdate;
552 $max_edate = $cust_bill_pkg->edate
553 if length($cust_bill_pkg->edate) && $cust_bill_pkg->edate > $max_edate;
556 $invoice_data{'bill_period'} = '';
557 $invoice_data{'bill_period'} =
558 $self->time2str_local('%e %h', $min_sdate, $format)
560 $self->time2str_local('%e %h', $max_edate, $format)
561 if ($max_edate != 0 && $min_sdate != 999999999999);
563 $invoice_data{finance_section} = '';
564 if ( $conf->config('finance_pkgclass') ) {
566 qsearchs('pkg_class', { classnum => $conf->config('finance_pkgclass') });
567 $invoice_data{finance_section} = $pkg_class->categoryname;
569 $invoice_data{finance_amount} = '0.00';
570 $invoice_data{finance_section} ||= 'Finance Charges'; #avoid config confusion
572 my $countrydefault = $conf->config('countrydefault') || 'US';
573 foreach ( qw( address1 address2 city state zip country fax) ){
574 my $method = 'ship_'.$_;
575 $invoice_data{"ship_$_"} = $escape_function->($cust_main->$method);
577 if ( length($cust_main->ship_company) ) {
578 $invoice_data{'ship_company'} = $escape_function->($cust_main->ship_company);
580 $invoice_data{'ship_company'} = $escape_function->($cust_main->company);
582 $invoice_data{'ship_contact'} = $escape_function->($cust_main->contact);
583 $invoice_data{'ship_country'} = ''
584 if ( $invoice_data{'ship_country'} eq $countrydefault );
586 $invoice_data{'cid'} = $params{'cid'}
589 if ( $cust_main->country eq $countrydefault ) {
590 $invoice_data{'country'} = '';
592 $invoice_data{'country'} = &$escape_function(code2country($cust_main->country));
596 $invoice_data{'address'} = \@address;
599 ( ( $cust_main->payby eq 'BILL' ) && $cust_main->payinfo
600 ? " (P.O. #". $cust_main->payinfo. ")"
604 push @address, $cust_main->company
605 if $cust_main->company;
606 push @address, $cust_main->address1;
607 push @address, $cust_main->address2
608 if $cust_main->address2;
610 $cust_main->city. ", ". $cust_main->state. " ". $cust_main->zip;
611 push @address, $invoice_data{'country'}
612 if $invoice_data{'country'};
614 while (scalar(@address) < 5);
616 $invoice_data{'logo_file'} = $params{'logo_file'}
617 if $params{'logo_file'};
618 $invoice_data{'barcode_file'} = $params{'barcode_file'}
619 if $params{'barcode_file'};
620 $invoice_data{'barcode_img'} = $params{'barcode_img'}
621 if $params{'barcode_img'};
622 $invoice_data{'barcode_cid'} = $params{'barcode_cid'}
623 if $params{'barcode_cid'};
625 my( $pr_total, @pr_cust_bill ) = $self->previous; #previous balance
626 # my( $cr_total, @cr_cust_credit ) = $self->cust_credit; #credits
627 #my $balance_due = $self->owed + $pr_total - $cr_total;
628 my $balance_due = $self->owed + $pr_total;
630 #these are used on the summary page only
632 # the customer's current balance as shown on the invoice before this one
633 $invoice_data{'true_previous_balance'} = sprintf("%.2f", ($self->previous_balance || 0) );
635 # the change in balance from that invoice to this one
636 $invoice_data{'balance_adjustments'} = sprintf("%.2f", ($self->previous_balance || 0) - ($self->billing_balance || 0) );
638 # the sum of amount owed on all previous invoices
639 # ($pr_total is used elsewhere but not as $previous_balance)
640 $invoice_data{'previous_balance'} = sprintf("%.2f", $pr_total);
642 # the sum of amount owed on all invoices
643 # (this is used in the summary & on the payment coupon)
644 $invoice_data{'balance'} = sprintf("%.2f", $balance_due);
646 # info from customer's last invoice before this one, for some
648 $invoice_data{'last_bill'} = {};
650 if ( $self->custnum && $self->invnum ) {
652 if ( $self->previous_bill ) {
653 my $last_bill = $self->previous_bill;
654 $invoice_data{'last_bill'} = {
655 '_date' => $last_bill->_date, #unformatted
657 my (@payments, @credits);
658 # for formats that itemize previous payments
659 foreach my $cust_pay ( qsearch('cust_pay', {
660 'custnum' => $self->custnum,
661 '_date' => { op => '>=',
662 value => $last_bill->_date }
665 next if $cust_pay->_date > $self->_date;
667 '_date' => $cust_pay->_date,
668 'date' => $self->time2str_local('long', $cust_pay->_date, $format),
669 'payinfo' => $cust_pay->payby_payinfo_pretty,
670 'amount' => sprintf('%.2f', $cust_pay->paid),
672 # not concerned about applications
674 foreach my $cust_credit ( qsearch('cust_credit', {
675 'custnum' => $self->custnum,
676 '_date' => { op => '>=',
677 value => $last_bill->_date }
680 next if $cust_credit->_date > $self->_date;
682 '_date' => $cust_credit->_date,
683 'date' => $self->time2str_local('long', $cust_credit->_date, $format),
684 'creditreason'=> $cust_credit->reason,
685 'amount' => sprintf('%.2f', $cust_credit->amount),
688 $invoice_data{'previous_payments'} = \@payments;
689 $invoice_data{'previous_credits'} = \@credits;
694 my $summarypage = '';
695 if ( $conf->exists('invoice_usesummary', $agentnum) ) {
698 $invoice_data{'summarypage'} = $summarypage;
700 warn "$me substituting variables in notes, footer, smallfooter\n"
703 my $tc = $self->template_conf;
704 my @include = ( [ $tc, 'notes' ],
705 [ 'invoice_', 'footer' ],
706 [ 'invoice_', 'smallfooter', ],
708 push @include, [ $tc, 'coupon', ]
709 unless $params{'no_coupon'};
711 foreach my $i (@include) {
713 my($base, $include) = @$i;
715 my $inc_file = $conf->key_orbase("$base$format$include", $template);
718 if ( $conf->exists($inc_file, $agentnum)
719 && length( $conf->config($inc_file, $agentnum) ) ) {
721 @inc_src = $conf->config($inc_file, $agentnum);
725 $inc_file = $conf->key_orbase("${base}latex$include", $template);
727 my $convert_map = $convert_maps{$format}{$include};
729 @inc_src = map { s/\[\@--/$delimiters{$format}[0]/g;
730 s/--\@\]/$delimiters{$format}[1]/g;
733 &$convert_map( $conf->config($inc_file, $agentnum) );
737 my $inc_tt = new Text::Template (
739 SOURCE => [ map "$_\n", @inc_src ],
740 DELIMITERS => $delimiters{$format},
741 ) or die "Can't create new Text::Template object: $Text::Template::ERROR";
743 unless ( $inc_tt->compile() ) {
744 my $error = "Can't compile $inc_file template: $Text::Template::ERROR\n";
745 warn $error. "Template:\n". join('', map "$_\n", @inc_src);
749 $invoice_data{$include} = $inc_tt->fill_in( HASH => \%invoice_data );
751 $invoice_data{$include} =~ s/\n+$//
752 if ($format eq 'latex');
755 # let invoices use either of these as needed
756 $invoice_data{'po_num'} = ($cust_main->payby eq 'BILL')
757 ? $cust_main->payinfo : '';
758 $invoice_data{'po_line'} =
759 ( $cust_main->payby eq 'BILL' && $cust_main->payinfo )
760 ? &$escape_function($self->mt("Purchase Order #").$cust_main->payinfo)
763 my %money_chars = ( 'latex' => '',
764 'html' => $conf->config('money_char') || '$',
767 my $money_char = $money_chars{$format};
769 my %other_money_chars = ( 'latex' => '\dollar ',#XXX should be a config too
770 'html' => $conf->config('money_char') || '$',
773 my $other_money_char = $other_money_chars{$format};
774 $invoice_data{'dollar'} = $other_money_char;
776 my %minus_signs = ( 'latex' => '$-$',
778 'template' => '- ' );
779 my $minus = $minus_signs{$format};
781 my @detail_items = ();
782 my @total_items = ();
786 $invoice_data{'detail_items'} = \@detail_items;
787 $invoice_data{'total_items'} = \@total_items;
788 $invoice_data{'buf'} = \@buf;
789 $invoice_data{'sections'} = \@sections;
791 warn "$me generating sections\n"
795 my $tax_section = { 'description' => $self->mt('Taxes, Surcharges, and Fees'),
796 'subtotal' => $taxtotal, # adjusted below
799 my $tax_weight = _pkg_category($tax_section->{description})
800 ? _pkg_category($tax_section->{description})->weight
802 $tax_section->{'summarized'} = ''; #why? $summarypage && !$tax_weight ? 'Y' : '';
803 $tax_section->{'sort_weight'} = $tax_weight;
806 my $adjust_section = {
807 'description' => $self->mt('Credits, Payments, and Adjustments'),
808 'adjust_section' => 1,
809 'subtotal' => 0, # adjusted below
811 my $adjust_weight = _pkg_category($adjust_section->{description})
812 ? _pkg_category($adjust_section->{description})->weight
814 $adjust_section->{'summarized'} = ''; #why? $summarypage && !$adjust_weight ? 'Y' : '';
815 $adjust_section->{'sort_weight'} = $adjust_weight;
817 my $unsquelched = $params{unsquelch_cdr} || $cust_main->squelch_cdr ne 'Y';
818 my $multisection = $conf->exists($tc.'sections', $cust_main->agentnum) ||
819 $conf->exists($tc.'sections_by_location', $cust_main->agentnum);
820 $invoice_data{'multisection'} = $multisection;
822 my $extra_sections = [];
823 my $extra_lines = ();
825 # default section ('Charges')
826 my $default_section = { 'description' => '',
831 # Previous Charges section
832 # subtotal is the first return value from $self->previous
833 my $previous_section;
834 # if the invoice has major sections, or if we're summarizing previous
835 # charges with a single line, or if we've been specifically told to put them
836 # in a section, create a section for previous charges:
837 if ( $multisection or
838 $conf->exists('previous_balance-summary_only') or
839 $conf->exists('previous_balance-section') ) {
841 $previous_section = { 'description' => $self->mt('Previous Charges'),
842 'subtotal' => $other_money_char.
843 sprintf('%.2f', $pr_total),
844 'summarized' => '', #why? $summarypage ? 'Y' : '',
846 $previous_section->{posttotal} = '0 / 30 / 60 / 90 days overdue '.
847 join(' / ', map { $cust_main->balance_date_range(@$_) }
848 $self->_prior_month30s
850 if $conf->exists('invoice_include_aging');
853 # otherwise put them in the main section
854 $previous_section = $default_section;
857 if ( $multisection ) {
858 ($extra_sections, $extra_lines) =
859 $self->_items_extra_usage_sections($escape_function_nonbsp, $format)
860 if $conf->exists('usage_class_as_a_section', $cust_main->agentnum)
861 && $self->can('_items_extra_usage_sections');
863 push @$extra_sections, $adjust_section if $adjust_section->{sort_weight};
865 push @detail_items, @$extra_lines if $extra_lines;
867 # the code is written so that both methods can be used together, but
868 # we haven't yet changed the template to take advantage of that, so for
869 # now, treat them as mutually exclusive.
870 my %section_method = ( by_category => 1 );
871 if ( $conf->exists($tc.'sections_by_location') ) {
872 %section_method = ( by_location => 1 );
875 $self->_items_sections( 'summary' => $summarypage,
876 'escape' => $escape_function_nonbsp,
877 'extra_sections' => $extra_sections,
881 push @sections, @$early;
882 $late_sections = $late;
884 if ( $conf->exists('svc_phone_sections')
885 && $self->can('_items_svc_phone_sections')
888 my ($phone_sections, $phone_lines) =
889 $self->_items_svc_phone_sections($escape_function_nonbsp, $format);
890 push @{$late_sections}, @$phone_sections;
891 push @detail_items, @$phone_lines;
893 if ( $conf->exists('voip-cust_accountcode_cdr')
894 && $cust_main->accountcode_cdr
895 && $self->can('_items_accountcode_cdr')
898 my ($accountcode_section, $accountcode_lines) =
899 $self->_items_accountcode_cdr($escape_function_nonbsp,$format);
900 if ( scalar(@$accountcode_lines) ) {
901 push @{$late_sections}, $accountcode_section;
902 push @detail_items, @$accountcode_lines;
905 } else {# not multisection
906 # make a default section
907 push @sections, $default_section;
908 # and calculate the finance charge total, since it won't get done otherwise.
909 # and the default section total
910 # XXX possibly finance_pkgclass should not be used in this manner?
913 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
914 if ( $invoice_data{finance_section} and
915 grep { $_->section eq $invoice_data{finance_section} }
916 $cust_bill_pkg->cust_bill_pkg_display ) {
917 # I think these are always setup fees, but just to be sure...
918 push @finance_charges, $cust_bill_pkg->recur + $cust_bill_pkg->setup;
920 push @charges, $cust_bill_pkg->recur + $cust_bill_pkg->setup;
923 $invoice_data{finance_amount} =
924 sprintf('%.2f', sum( @finance_charges ) || 0);
925 $default_section->{subtotal} = $other_money_char.
926 sprintf('%.2f', sum( @charges ) || 0);
929 # previous invoice balances in the Previous Charges section if there
930 # is one, otherwise in the main detail section
931 # (except if summary_only is enabled, don't show them at all)
932 if ( $self->can('_items_previous') &&
933 $self->enable_previous &&
934 ! $conf->exists('previous_balance-summary_only') ) {
936 warn "$me adding previous balances\n"
939 foreach my $line_item ( $self->_items_previous ) {
942 ref => $line_item->{'pkgnum'},
943 pkgpart => $line_item->{'pkgpart'},
944 #quantity => 1, # not really correct
945 section => $previous_section, # which might be $default_section
946 description => &$escape_function($line_item->{'description'}),
947 ext_description => [ map { &$escape_function($_) }
948 @{ $line_item->{'ext_description'} || [] }
950 amount => ( $old_latex ? '' : $money_char).
951 $line_item->{'amount'},
952 product_code => $line_item->{'pkgpart'} || 'N/A',
955 push @detail_items, $detail;
956 push @buf, [ $detail->{'description'},
957 $money_char. sprintf("%10.2f", $line_item->{'amount'}),
963 if ( @pr_cust_bill && $self->enable_previous ) {
964 push @buf, ['','-----------'];
965 push @buf, [ $self->mt('Total Previous Balance'),
966 $money_char. sprintf("%10.2f", $pr_total) ];
970 if ( $conf->exists('svc_phone-did-summary') && $self->can('_did_summary') ) {
971 warn "$me adding DID summary\n"
974 my ($didsummary,$minutes) = $self->_did_summary;
975 my $didsummary_desc = 'DID Activity Summary (since last invoice)';
977 { 'description' => $didsummary_desc,
978 'ext_description' => [ $didsummary, $minutes ],
982 foreach my $section (@sections, @$late_sections) {
984 # begin some normalization
985 $section->{'subtotal'} = $section->{'amount'}
987 && !exists($section->{subtotal})
988 && exists($section->{amount});
990 $invoice_data{finance_amount} = sprintf('%.2f', $section->{'subtotal'} )
991 if ( $invoice_data{finance_section} &&
992 $section->{'description'} eq $invoice_data{finance_section} );
994 $section->{'subtotal'} = $other_money_char.
995 sprintf('%.2f', $section->{'subtotal'})
998 # continue some normalization
999 $section->{'amount'} = $section->{'subtotal'}
1003 if ( $section->{'description'} ) {
1004 push @buf, ( [ &$escape_function($section->{'description'}), '' ],
1009 warn "$me setting options\n"
1013 $options{'section'} = $section if $multisection;
1014 $options{'format'} = $format;
1015 $options{'escape_function'} = $escape_function;
1016 $options{'no_usage'} = 1 unless $unsquelched;
1017 $options{'unsquelched'} = $unsquelched;
1018 $options{'summary_page'} = $summarypage;
1019 $options{'skip_usage'} =
1020 scalar(@$extra_sections) && !grep{$section == $_} @$extra_sections;
1022 warn "$me searching for line items\n"
1025 foreach my $line_item ( $self->_items_pkg(%options),
1026 $self->_items_fee(%options) ) {
1028 warn "$me adding line item $line_item\n"
1032 ext_description => [],
1034 $detail->{'ref'} = $line_item->{'pkgnum'};
1035 $detail->{'pkgpart'} = $line_item->{'pkgpart'};
1036 $detail->{'quantity'} = $line_item->{'quantity'};
1037 $detail->{'section'} = $section;
1038 $detail->{'description'} = &$escape_function($line_item->{'description'});
1039 if ( exists $line_item->{'ext_description'} ) {
1040 @{$detail->{'ext_description'}} = @{$line_item->{'ext_description'}};
1042 $detail->{'amount'} = ( $old_latex ? '' : $money_char ).
1043 $line_item->{'amount'};
1044 if ( exists $line_item->{'unit_amount'} ) {
1045 $detail->{'unit_amount'} = ( $old_latex ? '' : $money_char ).
1046 $line_item->{'unit_amount'};
1048 $detail->{'product_code'} = $line_item->{'pkgpart'} || 'N/A';
1050 $detail->{'sdate'} = $line_item->{'sdate'};
1051 $detail->{'edate'} = $line_item->{'edate'};
1052 $detail->{'seconds'} = $line_item->{'seconds'};
1053 $detail->{'svc_label'} = $line_item->{'svc_label'};
1054 $detail->{'usage_item'} = $line_item->{'usage_item'};
1056 push @detail_items, $detail;
1057 push @buf, ( [ $detail->{'description'},
1058 $money_char. sprintf("%10.2f", $line_item->{'amount'}),
1060 map { [ " ". $_, '' ] } @{$detail->{'ext_description'}},
1064 if ( $section->{'description'} ) {
1065 push @buf, ( ['','-----------'],
1066 [ $section->{'description'}. ' sub-total',
1067 $section->{'subtotal'} # already formatted this
1076 $invoice_data{current_less_finance} =
1077 sprintf('%.2f', $self->charged - $invoice_data{finance_amount} );
1079 # if there's anything in the Previous Charges section, prepend it to the list
1080 if ( $pr_total and $previous_section ne $default_section ) {
1081 unshift @sections, $previous_section;
1084 warn "$me adding taxes\n"
1087 my @items_tax = $self->_items_tax;
1088 foreach my $tax ( @items_tax ) {
1090 $taxtotal += $tax->{'amount'};
1092 my $description = &$escape_function( $tax->{'description'} );
1093 my $amount = sprintf( '%.2f', $tax->{'amount'} );
1095 if ( $multisection ) {
1097 my $money = $old_latex ? '' : $money_char;
1098 push @detail_items, {
1099 ext_description => [],
1102 description => $description,
1103 amount => $money. $amount,
1105 section => $tax_section,
1110 push @total_items, {
1111 'total_item' => $description,
1112 'total_amount' => $other_money_char. $amount,
1117 push @buf,[ $description,
1118 $money_char. $amount,
1125 $total->{'total_item'} = $self->mt('Sub-total');
1126 $total->{'total_amount'} =
1127 $other_money_char. sprintf('%.2f', $self->charged - $taxtotal );
1129 if ( $multisection ) {
1130 $tax_section->{'subtotal'} = $other_money_char.
1131 sprintf('%.2f', $taxtotal);
1132 $tax_section->{'pretotal'} = 'New charges sub-total '.
1133 $total->{'total_amount'};
1134 push @sections, $tax_section if $taxtotal;
1136 unshift @total_items, $total;
1139 $invoice_data{'taxtotal'} = sprintf('%.2f', $taxtotal);
1141 push @buf,['','-----------'];
1142 push @buf,[$self->mt(
1143 (!$self->enable_previous)
1145 : 'Total New Charges'
1147 $money_char. sprintf("%10.2f",$self->charged) ];
1155 my %embolden_functions = (
1156 'latex' => sub { return '\textbf{'. shift(). '}' },
1157 'html' => sub { return '<b>'. shift(). '</b>' },
1158 'template' => sub { shift },
1160 my $embolden_function = $embolden_functions{$format};
1162 if ( $self->can('_items_total') ) { # quotations
1164 $self->_items_total(\@total_items);
1166 foreach ( @total_items ) {
1167 $_->{'total_item'} = &$embolden_function( $_->{'total_item'} );
1168 $_->{'total_amount'} = &$embolden_function( $other_money_char.
1169 $_->{'total_amount'}
1173 } else { #normal invoice case
1175 # calculate total, possibly including total owed on previous
1179 $item = $conf->config('previous_balance-exclude_from_total')
1180 || 'Total New Charges'
1181 if $conf->exists('previous_balance-exclude_from_total');
1182 my $amount = $self->charged;
1183 if ( $self->enable_previous and !$conf->exists('previous_balance-exclude_from_total') ) {
1184 $amount += $pr_total;
1187 $total->{'total_item'} = &$embolden_function($self->mt($item));
1188 $total->{'total_amount'} =
1189 &$embolden_function( $other_money_char. sprintf( '%.2f', $amount ) );
1190 if ( $multisection ) {
1191 if ( $adjust_section->{'sort_weight'} ) {
1192 $adjust_section->{'posttotal'} = $self->mt('Balance Forward').' '.
1193 $other_money_char. sprintf("%.2f", ($self->billing_balance || 0) );
1195 $adjust_section->{'pretotal'} = $self->mt('New charges total').' '.
1196 $other_money_char. sprintf('%.2f', $self->charged );
1199 push @total_items, $total;
1201 push @buf,['','-----------'];
1204 sprintf( '%10.2f', $amount )
1208 # if we're showing previous invoices, also show previous
1209 # credits and payments
1210 if ( $self->enable_previous
1211 and $self->can('_items_credits')
1212 and $self->can('_items_payments') )
1214 #foreach my $thing ( sort { $a->_date <=> $b->_date } $self->_items_credits, $self->_items_payments
1217 my $credittotal = 0;
1218 foreach my $credit (
1219 $self->_items_credits( 'template' => $template, 'trim_len' => 60 )
1223 $total->{'total_item'} = &$escape_function($credit->{'description'});
1224 $credittotal += $credit->{'amount'};
1225 $total->{'total_amount'} = $minus.$other_money_char.$credit->{'amount'};
1226 $adjusttotal += $credit->{'amount'};
1227 if ( $multisection ) {
1228 my $money = $old_latex ? '' : $money_char;
1229 push @detail_items, {
1230 ext_description => [],
1233 description => &$escape_function($credit->{'description'}),
1234 amount => $money. $credit->{'amount'},
1236 section => $adjust_section,
1239 push @total_items, $total;
1243 $invoice_data{'credittotal'} = sprintf('%.2f', $credittotal);
1246 foreach my $credit (
1247 $self->_items_credits( 'template' => $template, 'trim_len'=>32 )
1249 push @buf, [ $credit->{'description'}, $money_char.$credit->{'amount'} ];
1253 my $paymenttotal = 0;
1254 foreach my $payment (
1255 $self->_items_payments( 'template' => $template )
1258 $total->{'total_item'} = &$escape_function($payment->{'description'});
1259 $paymenttotal += $payment->{'amount'};
1260 $total->{'total_amount'} = $minus.$other_money_char.$payment->{'amount'};
1261 $adjusttotal += $payment->{'amount'};
1262 if ( $multisection ) {
1263 my $money = $old_latex ? '' : $money_char;
1264 push @detail_items, {
1265 ext_description => [],
1268 description => &$escape_function($payment->{'description'}),
1269 amount => $money. $payment->{'amount'},
1271 section => $adjust_section,
1274 push @total_items, $total;
1276 push @buf, [ $payment->{'description'},
1277 $money_char. sprintf("%10.2f", $payment->{'amount'}),
1280 $invoice_data{'paymenttotal'} = sprintf('%.2f', $paymenttotal);
1282 if ( $multisection ) {
1283 $adjust_section->{'subtotal'} = $other_money_char.
1284 sprintf('%.2f', $adjusttotal);
1285 push @sections, $adjust_section
1286 unless $adjust_section->{sort_weight};
1289 # create Balance Due message
1292 $total->{'total_item'} = &$embolden_function($self->balance_due_msg);
1293 $total->{'total_amount'} =
1294 &$embolden_function(
1295 $other_money_char. sprintf('%.2f', #why? $summarypage
1296 # ? $self->charged +
1297 # $self->billing_balance
1299 $self->owed + $pr_total
1302 if ( $multisection && !$adjust_section->{sort_weight} ) {
1303 $adjust_section->{'posttotal'} = $total->{'total_item'}. ' '.
1304 $total->{'total_amount'};
1306 push @total_items, $total;
1308 push @buf,['','-----------'];
1309 push @buf,[$self->balance_due_msg, $money_char.
1310 sprintf("%10.2f", $balance_due ) ];
1313 if ( $conf->exists('previous_balance-show_credit')
1314 and $cust_main->balance < 0 ) {
1315 my $credit_total = {
1316 'total_item' => &$embolden_function($self->credit_balance_msg),
1317 'total_amount' => &$embolden_function(
1318 $other_money_char. sprintf('%.2f', -$cust_main->balance)
1321 if ( $multisection ) {
1322 $adjust_section->{'posttotal'} .= $newline_token .
1323 $credit_total->{'total_item'} . ' ' . $credit_total->{'total_amount'};
1326 push @total_items, $credit_total;
1328 push @buf,['','-----------'];
1329 push @buf,[$self->credit_balance_msg, $money_char.
1330 sprintf("%10.2f", -$cust_main->balance ) ];
1334 } #end of default total adding ! can('_items_total')
1336 if ( $multisection ) {
1337 if ( $conf->exists('svc_phone_sections')
1338 && $self->can('_items_svc_phone_sections')
1342 $total->{'total_item'} = &$embolden_function($self->balance_due_msg);
1343 $total->{'total_amount'} =
1344 &$embolden_function(
1345 $other_money_char. sprintf('%.2f', $self->owed + $pr_total)
1347 my $last_section = pop @sections;
1348 $last_section->{'posttotal'} = $total->{'total_item'}. ' '.
1349 $total->{'total_amount'};
1350 push @sections, $last_section;
1352 push @sections, @$late_sections
1356 # make a discounts-available section, even without multisection
1357 if ( $conf->exists('discount-show_available')
1358 and my @discounts_avail = $self->_items_discounts_avail ) {
1359 my $discount_section = {
1360 'description' => $self->mt('Discounts Available'),
1365 push @sections, $discount_section;
1366 push @detail_items, map { +{
1367 'ref' => '', #should this be something else?
1368 'section' => $discount_section,
1369 'description' => &$escape_function( $_->{description} ),
1370 'amount' => $money_char . &$escape_function( $_->{amount} ),
1371 'ext_description' => [ &$escape_function($_->{ext_description}) || () ],
1372 } } @discounts_avail;
1375 my @summary_subtotals;
1376 # the templates say "$_->{tax_section} || !$_->{summarized}"
1377 # except 'summarized' is only true when tax_section is true, so this
1378 # is always true, so what's the deal?
1379 foreach my $s (@sections) {
1380 # not to include in the "summary of new charges" block:
1381 # finance charges, adjustments, previous charges,
1382 # and itemized phone usage sections
1383 if ( $s eq $adjust_section or
1384 ($s eq $previous_section and $s ne $default_section) or
1385 ($invoice_data{'finance_section'} and
1386 $invoice_data{'finance_section'} eq $s->{description}) or
1387 $s->{'description'} =~ /^\d+ $/ ) {
1390 push @summary_subtotals, $s;
1392 $invoice_data{summary_subtotals} = \@summary_subtotals;
1395 if ( $conf->exists('usage_class_summary')
1396 and $self->can('_items_usage_class_summary') ) {
1397 my @usage_subtotals = $self->_items_usage_class_summary(escape => $escape_function);
1398 if ( @usage_subtotals ) {
1399 unshift @sections, $usage_subtotals[0]->{section};
1400 unshift @detail_items, @usage_subtotals;
1404 # invoice history "section" (not really a section)
1405 # not to be included in any subtotals, completely independent of
1407 if ( $conf->exists('previous_invoice_history') ) {
1410 foreach my $cust_bill ( $cust_main->cust_bill ) {
1411 # XXX hardcoded format, and currently only 'charged'; add other fields
1412 # if they become necessary
1413 my $date = $self->time2str_local('%b %Y', $cust_bill->_date);
1414 $history{$date} ||= 0;
1415 $history{$date} += $cust_bill->charged;
1416 # just so we have a numeric sort key
1417 $monthorder{$date} ||= $cust_bill->_date;
1419 my @sorted_months = sort { $monthorder{$a} <=> $monthorder{$b} }
1421 my @sorted_amounts = map { sprintf('%.2f', $history{$_}) } @sorted_months;
1422 $invoice_data{monthly_history} = [ \@sorted_months, \@sorted_amounts ];
1425 # debugging hook: call this with 'diag' => 1 to just get a hash of
1426 # the invoice variables
1427 return \%invoice_data if ( $params{'diag'} );
1429 # All sections and items are built; now fill in templates.
1430 my @includelist = ();
1431 push @includelist, 'summary' if $summarypage;
1432 foreach my $include ( @includelist ) {
1434 my $inc_file = $conf->key_orbase("invoice_${format}$include", $template);
1437 if ( length( $conf->config($inc_file, $agentnum) ) ) {
1439 @inc_src = $conf->config($inc_file, $agentnum);
1443 $inc_file = $conf->key_orbase("invoice_latex$include", $template);
1445 my $convert_map = $convert_maps{$format}{$include};
1447 @inc_src = map { s/\[\@--/$delimiters{$format}[0]/g;
1448 s/--\@\]/$delimiters{$format}[1]/g;
1451 &$convert_map( $conf->config($inc_file, $agentnum) );
1455 my $inc_tt = new Text::Template (
1457 SOURCE => [ map "$_\n", @inc_src ],
1458 DELIMITERS => $delimiters{$format},
1459 ) or die "Can't create new Text::Template object: $Text::Template::ERROR";
1461 unless ( $inc_tt->compile() ) {
1462 my $error = "Can't compile $inc_file template: $Text::Template::ERROR\n";
1463 warn $error. "Template:\n". join('', map "$_\n", @inc_src);
1467 $invoice_data{$include} = $inc_tt->fill_in( HASH => \%invoice_data );
1469 $invoice_data{$include} =~ s/\n+$//
1470 if ($format eq 'latex');
1475 foreach ( grep /invoice_lines\(\d*\)/, @invoice_template ) { #kludgy
1476 /invoice_lines\((\d*)\)/;
1477 $invoice_lines += $1 || scalar(@buf);
1480 die "no invoice_lines() functions in template?"
1481 if ( $format eq 'template' && !$wasfunc );
1483 if ($format eq 'template') {
1485 if ( $invoice_lines ) {
1486 $invoice_data{'total_pages'} = int( scalar(@buf) / $invoice_lines );
1487 $invoice_data{'total_pages'}++
1488 if scalar(@buf) % $invoice_lines;
1491 #setup subroutine for the template
1492 $invoice_data{invoice_lines} = sub {
1493 my $lines = shift || scalar(@buf);
1505 push @collect, split("\n",
1506 $text_template->fill_in( HASH => \%invoice_data )
1508 $invoice_data{'page'}++;
1510 map "$_\n", @collect;
1512 } else { # this is where we actually create the invoice
1514 warn "filling in template for invoice ". $self->invnum. "\n"
1516 warn join("\n", map " $_ => ". $invoice_data{$_}, keys %invoice_data). "\n"
1519 $text_template->fill_in(HASH => \%invoice_data);
1523 sub notice_name { '('.shift->table.')'; }
1525 sub template_conf { 'invoice_'; }
1527 # helper routine for generating date ranges
1528 sub _prior_month30s {
1531 [ 1, 2592000 ], # 0-30 days ago
1532 [ 2592000, 5184000 ], # 30-60 days ago
1533 [ 5184000, 7776000 ], # 60-90 days ago
1534 [ 7776000, 0 ], # 90+ days ago
1537 map { [ $_->[0] ? $self->_date - $_->[0] - 1 : '',
1538 $_->[1] ? $self->_date - $_->[1] - 1 : '',
1543 =item print_ps HASHREF | [ TIME [ , TEMPLATE ] ]
1545 Returns an postscript invoice, as a scalar.
1547 Options can be passed as a hashref (recommended) or as a list of time, template
1548 and then any key/value pairs for any other options.
1550 I<time> an optional value used to control the printing of overdue messages. The
1551 default is now. It isn't the date of the invoice; that's the `_date' field.
1552 It is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
1553 L<Time::Local> and L<Date::Parse> for conversion functions.
1555 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1562 my ($file, $logofile, $barcodefile) = $self->print_latex(@_);
1563 my $ps = generate_ps($file);
1565 unlink($barcodefile) if $barcodefile;
1570 =item print_pdf HASHREF | [ TIME [ , TEMPLATE ] ]
1572 Returns an PDF invoice, as a scalar.
1574 Options can be passed as a hashref (recommended) or as a list of time, template
1575 and then any key/value pairs for any other options.
1577 I<time> an optional value used to control the printing of overdue messages. The
1578 default is now. It isn't the date of the invoice; that's the `_date' field.
1579 It is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
1580 L<Time::Local> and L<Date::Parse> for conversion functions.
1582 I<template>, if specified, is the name of a suffix for alternate invoices.
1584 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1591 my ($file, $logofile, $barcodefile) = $self->print_latex(@_);
1592 my $pdf = generate_pdf($file);
1594 unlink($barcodefile) if $barcodefile;
1599 =item print_html HASHREF | [ TIME [ , TEMPLATE [ , CID ] ] ]
1601 Returns an HTML invoice, as a scalar.
1603 I<time> an optional value used to control the printing of overdue messages. The
1604 default is now. It isn't the date of the invoice; that's the `_date' field.
1605 It is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
1606 L<Time::Local> and L<Date::Parse> for conversion functions.
1608 I<template>, if specified, is the name of a suffix for alternate invoices.
1610 I<notice_name>, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required)
1612 I<cid> is a MIME Content-ID used to create a "cid:" URL for the logo image, used
1613 when emailing the invoice as part of a multipart/related MIME email.
1621 %params = %{ shift() };
1625 $params{'format'} = 'html';
1627 $self->print_generic( %params );
1630 # quick subroutine for print_latex
1632 # There are ten characters that LaTeX treats as special characters, which
1633 # means that they do not simply typeset themselves:
1634 # # $ % & ~ _ ^ \ { }
1636 # TeX ignores blanks following an escaped character; if you want a blank (as
1637 # in "10% of ..."), you have to "escape" the blank as well ("10\%\ of ...").
1641 $value =~ s/([#\$%&~_\^{}])( )?/"\\$1". ( ( defined($2) && length($2) ) ? "\\$2" : '' )/ge;
1642 $value =~ s/([<>])/\$$1\$/g;
1648 encode_entities($value);
1652 sub _html_escape_nbsp {
1653 my $value = _html_escape(shift);
1654 $value =~ s/ +/ /g;
1658 #utility methods for print_*
1660 sub _translate_old_latex_format {
1661 warn "_translate_old_latex_format called\n"
1668 if ( $line =~ /^%%Detail\s*$/ ) {
1670 push @template, q![@--!,
1671 q! foreach my $_tr_line (@detail_items) {!,
1672 q! if ( scalar ($_tr_item->{'ext_description'} ) ) {!,
1673 q! $_tr_line->{'description'} .= !,
1674 q! "\\tabularnewline\n~~".!,
1675 q! join( "\\tabularnewline\n~~",!,
1676 q! @{$_tr_line->{'ext_description'}}!,
1680 while ( ( my $line_item_line = shift )
1681 !~ /^%%EndDetail\s*$/ ) {
1682 $line_item_line =~ s/'/\\'/g; # nice LTS
1683 $line_item_line =~ s/\\/\\\\/g; # escape quotes and backslashes
1684 $line_item_line =~ s/\$(\w+)/'. \$_tr_line->{$1}. '/g;
1685 push @template, " \$OUT .= '$line_item_line';";
1688 push @template, '}',
1691 } elsif ( $line =~ /^%%TotalDetails\s*$/ ) {
1693 push @template, '[@--',
1694 ' foreach my $_tr_line (@total_items) {';
1696 while ( ( my $total_item_line = shift )
1697 !~ /^%%EndTotalDetails\s*$/ ) {
1698 $total_item_line =~ s/'/\\'/g; # nice LTS
1699 $total_item_line =~ s/\\/\\\\/g; # escape quotes and backslashes
1700 $total_item_line =~ s/\$(\w+)/'. \$_tr_line->{$1}. '/g;
1701 push @template, " \$OUT .= '$total_item_line';";
1704 push @template, '}',
1708 $line =~ s/\$(\w+)/[\@-- \$$1 --\@]/g;
1709 push @template, $line;
1715 warn "$_\n" foreach @template;
1723 my $conf = $self->conf;
1725 #check for an invoice-specific override
1726 return $self->invoice_terms if $self->invoice_terms;
1728 #check for a customer- specific override
1729 my $cust_main = $self->cust_main;
1730 return $cust_main->invoice_terms if $cust_main && $cust_main->invoice_terms;
1732 #use configured default
1733 $conf->config('invoice_default_terms') || '';
1739 if ( $self->terms =~ /^\s*Net\s*(\d+)\s*$/ ) {
1740 $duedate = $self->_date() + ( $1 * 86400 );
1747 $self->due_date ? $self->time2str_local(shift, $self->due_date) : '';
1750 sub balance_due_msg {
1752 my $msg = $self->mt('Balance Due');
1753 return $msg unless $self->terms;
1754 if ( $self->due_date ) {
1755 $msg .= ' - ' . $self->mt('Please pay by'). ' '.
1756 $self->due_date2str('short');
1757 } elsif ( $self->terms ) {
1758 $msg .= ' - '. $self->terms;
1763 sub balance_due_date {
1765 my $conf = $self->conf;
1767 if ( $conf->exists('invoice_default_terms')
1768 && $conf->config('invoice_default_terms')=~ /^\s*Net\s*(\d+)\s*$/ ) {
1769 $duedate = $self->time2str_local('rdate', $self->_date + ($1*86400) );
1774 sub credit_balance_msg {
1776 $self->mt('Credit Balance Remaining')
1781 Returns a string with the date, for example: "3/20/2008"
1787 $self->time2str_local('short', $self->_date);
1790 =item _items_sections OPTIONS
1792 Generate section information for all items appearing on this invoice.
1793 This will only be called for multi-section invoices.
1795 For each line item (L<FS::cust_bill_pkg> record), this will fetch all
1796 related display records (L<FS::cust_bill_pkg_display>) and organize
1797 them into two groups ("early" and "late" according to whether they come
1798 before or after the total), then into sections. A subtotal is calculated
1801 Section descriptions are returned in sort weight order. Each consists
1802 of a hash containing:
1804 description: the package category name, escaped
1805 subtotal: the total charges in that section
1806 tax_section: a flag indicating that the section contains only tax charges
1807 summarized: same as tax_section, for some reason
1808 sort_weight: the package category's sort weight
1810 If 'condense' is set on the display record, it also contains everything
1811 returned from C<_condense_section()>, i.e. C<_condensed_foo_generator>
1812 coderefs to generate parts of the invoice. This is not advised.
1814 The method returns two arrayrefs, one of "early" sections and one of "late"
1817 OPTIONS may include:
1819 by_location: a flag to divide the invoice into sections by location.
1820 Each section hash will have a 'location' element containing a hashref of
1821 the location fields (see L<FS::cust_location>). The section description
1822 will be the location label, but the template can use any of the location
1823 fields to create a suitable label.
1825 by_category: a flag to divide the invoice into sections using display
1826 records (see L<FS::cust_bill_pkg_display>). This is the "traditional"
1827 behavior. Each section hash will have a 'category' element containing
1828 the section name from the display record (which probably equals the
1829 category name of the package, but may not in some cases).
1831 summary: a flag indicating that this is a summary-format invoice.
1832 Turning this on has the following effects:
1833 - Ignores display items with the 'summary' flag.
1834 - Places all sections in the "early" group even if they have post_total.
1835 - Creates sections for all non-disabled package categories, even if they
1836 have no charges on this invoice, as well as a section with no name.
1838 escape: an escape function to use for section titles.
1840 extra_sections: an arrayref of additional sections to return after the
1841 sorted list. If there are any of these, section subtotals exclude
1844 format: 'latex', 'html', or 'template' (i.e. text). Not used, but
1845 passed through to C<_condense_section()>.
1849 use vars qw(%pkg_category_cache);
1850 sub _items_sections {
1854 my $escape = $opt{escape};
1855 my @extra_sections = @{ $opt{extra_sections} || [] };
1857 # $subtotal{$locationnum}{$categoryname} = amount.
1858 # if we're not using by_location, $locationnum is undef.
1859 # if we're not using by_category, you guessed it, $categoryname is undef.
1860 # if we're not using either one, we shouldn't be here in the first place...
1862 my %late_subtotal = ();
1865 # About tax items + multisection invoices:
1866 # If either invoice_*summary option is enabled, AND there is a
1867 # package category with the name of the tax, then there will be
1868 # a display record assigning the tax item to that category.
1870 # However, the taxes are always placed in the "Taxes, Surcharges,
1871 # and Fees" section regardless of that. The only effect of the
1872 # display record is to create a subtotal for the summary page.
1875 my $pkg_hash = $self->cust_pkg_hash;
1877 foreach my $cust_bill_pkg ( $self->cust_bill_pkg )
1880 my $usage = $cust_bill_pkg->usage;
1883 if ( $opt{by_location} ) {
1884 if ( $cust_bill_pkg->pkgnum ) {
1885 $locationnum = $pkg_hash->{ $cust_bill_pkg->pkgnum }->locationnum;
1890 $locationnum = undef;
1893 # as in _items_cust_pkg, if a line item has no display records,
1894 # cust_bill_pkg_display() returns a default record for it
1896 foreach my $display ($cust_bill_pkg->cust_bill_pkg_display) {
1897 next if ( $display->summary && $opt{summary} );
1899 my $section = $display->section;
1900 my $type = $display->type;
1901 # Set $section = undef if we're sectioning by location and this
1902 # line item _has_ a location (i.e. isn't a fee).
1903 $section = undef if $locationnum;
1905 # set this flag if the section is not tax-only
1906 $not_tax{$locationnum}{$section} = 1
1907 if $cust_bill_pkg->pkgnum or $cust_bill_pkg->feepart;
1909 # there's actually a very important piece of logic buried in here:
1910 # incrementing $late_subtotal{$section} CREATES
1911 # $late_subtotal{$section}. keys(%late_subtotal) is later used
1912 # to define the list of late sections, and likewise keys(%subtotal).
1913 # When _items_cust_bill_pkg is called to generate line items for
1914 # real, it will be called with 'section' => $section for each
1916 if ( $display->post_total && !$opt{summary} ) {
1917 if (! $type || $type eq 'S') {
1918 $late_subtotal{$locationnum}{$section} += $cust_bill_pkg->setup
1919 if $cust_bill_pkg->setup != 0
1920 || $cust_bill_pkg->setup_show_zero;
1924 $late_subtotal{$locationnum}{$section} += $cust_bill_pkg->recur
1925 if $cust_bill_pkg->recur != 0
1926 || $cust_bill_pkg->recur_show_zero;
1929 if ($type && $type eq 'R') {
1930 $late_subtotal{$locationnum}{$section} += $cust_bill_pkg->recur - $usage
1931 if $cust_bill_pkg->recur != 0
1932 || $cust_bill_pkg->recur_show_zero;
1935 if ($type && $type eq 'U') {
1936 $late_subtotal{$locationnum}{$section} += $usage
1937 unless scalar(@extra_sections);
1940 } else { # it's a pre-total (normal) section
1942 # skip tax items unless they're explicitly included in a section
1943 next if $cust_bill_pkg->pkgnum == 0 and
1944 ! $cust_bill_pkg->feepart and
1947 if (! $type || $type eq 'S') {
1948 $subtotal{$locationnum}{$section} += $cust_bill_pkg->setup
1949 if $cust_bill_pkg->setup != 0
1950 || $cust_bill_pkg->setup_show_zero;
1954 $subtotal{$locationnum}{$section} += $cust_bill_pkg->recur
1955 if $cust_bill_pkg->recur != 0
1956 || $cust_bill_pkg->recur_show_zero;
1959 if ($type && $type eq 'R') {
1960 $subtotal{$locationnum}{$section} += $cust_bill_pkg->recur - $usage
1961 if $cust_bill_pkg->recur != 0
1962 || $cust_bill_pkg->recur_show_zero;
1965 if ($type && $type eq 'U') {
1966 $subtotal{$locationnum}{$section} += $usage
1967 unless scalar(@extra_sections);
1976 %pkg_category_cache = ();
1978 # summary invoices need subtotals for all non-disabled package categories,
1979 # even if they're zero
1980 # but currently assume that there are no location sections, or at least
1981 # that the summary page doesn't care about them
1982 if ( $opt{summary} ) {
1983 foreach my $category (qsearch('pkg_category', {disabled => ''})) {
1984 $subtotal{''}{$category->categoryname} ||= 0;
1986 $subtotal{''}{''} ||= 0;
1990 foreach my $post_total (0,1) {
1992 my $s = $post_total ? \%late_subtotal : \%subtotal;
1993 foreach my $locationnum (keys %$s) {
1994 foreach my $sectionname (keys %{ $s->{$locationnum} }) {
1996 'subtotal' => $s->{$locationnum}{$sectionname},
1997 'post_total' => $post_total,
2000 if ( $locationnum ) {
2001 $section->{'locationnum'} = $locationnum;
2002 my $location = FS::cust_location->by_key($locationnum);
2003 $section->{'description'} = &{ $escape }($location->location_label);
2004 # Better ideas? This will roughly group them by proximity,
2005 # which alpha sorting on any of the address fields won't.
2006 # Sorting by locationnum is meaningless.
2007 # We have to sort on _something_ or the order may change
2008 # randomly from one invoice to the next, which will confuse
2010 $section->{'sort_weight'} = sprintf('%012s',$location->zip) .
2012 $section->{'location'} = {
2013 map { $_ => &{ $escape }($location->get($_)) }
2017 $section->{'category'} = $sectionname;
2018 $section->{'description'} = &{ $escape }($sectionname);
2019 if ( _pkg_category($_) ) {
2020 $section->{'sort_weight'} = _pkg_category($_)->weight;
2021 if ( _pkg_category($_)->condense ) {
2022 $section = { %$section, $self->_condense_section($opt{format}) };
2026 if ( !$post_total and !$not_tax{$locationnum}{$sectionname} ) {
2027 # then it's a tax-only section
2028 $section->{'summarized'} = 'Y';
2029 $section->{'tax_section'} = 'Y';
2031 push @these, $section;
2032 } # foreach $sectionname
2033 } #foreach $locationnum
2034 push @these, @extra_sections if $post_total == 0;
2035 # need an alpha sort for location sections, because postal codes can
2037 $sections[ $post_total ] = [ sort {
2038 $opt{'by_location'} ?
2039 ($a->{sort_weight} cmp $b->{sort_weight}) :
2040 ($a->{sort_weight} <=> $b->{sort_weight})
2042 } #foreach $post_total
2044 return @sections; # early, late
2047 #helper subs for above
2051 $self->{cust_pkg} ||= { map { $_->pkgnum => $_ } $self->cust_pkg };
2055 my $categoryname = shift;
2056 $pkg_category_cache{$categoryname} ||=
2057 qsearchs( 'pkg_category', { 'categoryname' => $categoryname } );
2060 my %condensed_format = (
2061 'label' => [ qw( Description Qty Amount ) ],
2063 sub { shift->{description} },
2064 sub { shift->{quantity} },
2065 sub { my($href, %opt) = @_;
2066 ($opt{dollar} || ''). $href->{amount};
2069 'align' => [ qw( l r r ) ],
2070 'span' => [ qw( 5 1 1 ) ], # unitprices?
2071 'width' => [ qw( 10.7cm 1.4cm 1.6cm ) ], # don't like this
2074 sub _condense_section {
2075 my ( $self, $format ) = ( shift, shift );
2077 map { my $method = "_condensed_$_"; $_ => $self->$method($format) }
2078 qw( description_generator
2081 total_line_generator
2086 sub _condensed_generator_defaults {
2087 my ( $self, $format ) = ( shift, shift );
2088 return ( \%condensed_format, ' ', ' ', ' ', sub { shift } );
2097 sub _condensed_header_generator {
2098 my ( $self, $format ) = ( shift, shift );
2100 my ( $f, $prefix, $suffix, $separator, $column ) =
2101 _condensed_generator_defaults($format);
2103 if ($format eq 'latex') {
2104 $prefix = "\\hline\n\\rule{0pt}{2.5ex}\n\\makebox[1.4cm]{}&\n";
2105 $suffix = "\\\\\n\\hline";
2108 sub { my ($d,$a,$s,$w) = @_;
2109 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
2111 } elsif ( $format eq 'html' ) {
2112 $prefix = '<th></th>';
2116 sub { my ($d,$a,$s,$w) = @_;
2117 return qq!<th align="$html_align{$a}">$d</th>!;
2125 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
2127 &{$column}( map { $f->{$_}->[$i] } qw(label align span width) );
2130 $prefix. join($separator, @result). $suffix;
2135 sub _condensed_description_generator {
2136 my ( $self, $format ) = ( shift, shift );
2138 my ( $f, $prefix, $suffix, $separator, $column ) =
2139 _condensed_generator_defaults($format);
2141 my $money_char = '$';
2142 if ($format eq 'latex') {
2143 $prefix = "\\hline\n\\multicolumn{1}{c}{\\rule{0pt}{2.5ex}~} &\n";
2145 $separator = " & \n";
2147 sub { my ($d,$a,$s,$w) = @_;
2148 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{\\textbf{$d}}}";
2150 $money_char = '\\dollar';
2151 }elsif ( $format eq 'html' ) {
2152 $prefix = '"><td align="center"></td>';
2156 sub { my ($d,$a,$s,$w) = @_;
2157 return qq!<td align="$html_align{$a}">$d</td>!;
2159 #$money_char = $conf->config('money_char') || '$';
2160 $money_char = ''; # this is madness
2168 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
2170 $dollar = $money_char if $i == scalar(@{$f->{label}})-1;
2172 &{$column}( &{$f->{fields}->[$i]}($href, 'dollar' => $dollar),
2173 map { $f->{$_}->[$i] } qw(align span width)
2177 $prefix. join( $separator, @result ). $suffix;
2182 sub _condensed_total_generator {
2183 my ( $self, $format ) = ( shift, shift );
2185 my ( $f, $prefix, $suffix, $separator, $column ) =
2186 _condensed_generator_defaults($format);
2189 if ($format eq 'latex') {
2192 $separator = " & \n";
2194 sub { my ($d,$a,$s,$w) = @_;
2195 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
2197 }elsif ( $format eq 'html' ) {
2201 $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
2203 sub { my ($d,$a,$s,$w) = @_;
2204 return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
2213 # my $r = &{$f->{fields}->[$i]}(@args);
2214 # $r .= ' Total' unless $i;
2216 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
2218 &{$column}( &{$f->{fields}->[$i]}(@args). ($i ? '' : ' Total'),
2219 map { $f->{$_}->[$i] } qw(align span width)
2223 $prefix. join( $separator, @result ). $suffix;
2228 =item total_line_generator FORMAT
2230 Returns a coderef used for generation of invoice total line items for this
2231 usage_class. FORMAT is either html or latex
2235 # should not be used: will have issues with hash element names (description vs
2236 # total_item and amount vs total_amount -- another array of functions?
2238 sub _condensed_total_line_generator {
2239 my ( $self, $format ) = ( shift, shift );
2241 my ( $f, $prefix, $suffix, $separator, $column ) =
2242 _condensed_generator_defaults($format);
2245 if ($format eq 'latex') {
2248 $separator = " & \n";
2250 sub { my ($d,$a,$s,$w) = @_;
2251 return "\\multicolumn{$s}{$a}{\\makebox[$w][$a]{$d}}";
2253 }elsif ( $format eq 'html' ) {
2257 $style = 'border-top: 3px solid #000000;border-bottom: 3px solid #000000;';
2259 sub { my ($d,$a,$s,$w) = @_;
2260 return qq!<td align="$html_align{$a}" style="$style">$d</td>!;
2269 foreach (my $i = 0; $f->{label}->[$i]; $i++) {
2271 &{$column}( &{$f->{fields}->[$i]}(@args),
2272 map { $f->{$_}->[$i] } qw(align span width)
2276 $prefix. join( $separator, @result ). $suffix;
2281 =item _items_pkg [ OPTIONS ]
2283 Return line item hashes for each package item on this invoice. Nearly
2286 $self->_items_cust_bill_pkg([ $self->cust_bill_pkg ])
2288 The only OPTIONS accepted is 'section', which may point to a hashref
2289 with a key named 'condensed', which may have a true value. If it
2290 does, this method tries to merge identical items into items with
2291 'quantity' equal to the number of items (not the sum of their
2292 separate quantities, for some reason).
2298 # The order of these is important. Bundled line items will be merged into
2299 # the most recent non-hidden item, so it needs to be the one with:
2301 # - the same start date
2302 # - no pkgpart_override
2304 # So: sort by pkgnum,
2306 # then sort the base line item before any overrides
2307 # then sort hidden before non-hidden add-ons
2308 # then sort by override pkgpart (for consistency)
2309 sort { $a->pkgnum <=> $b->pkgnum or
2310 $a->sdate <=> $b->sdate or
2311 ($a->pkgpart_override ? 0 : -1) or
2312 ($b->pkgpart_override ? 0 : 1) or
2313 $b->hidden cmp $a->hidden or
2314 $a->pkgpart_override <=> $b->pkgpart_override
2316 # and of course exclude taxes and fees
2317 grep { $_->pkgnum > 0 } $self->cust_bill_pkg;
2323 my @cust_bill_pkg = grep { $_->feepart } $self->cust_bill_pkg;
2325 foreach my $cust_bill_pkg (@cust_bill_pkg) {
2326 # cache this, so we don't look it up again in every section
2327 my $part_fee = $cust_bill_pkg->get('part_fee')
2328 || $cust_bill_pkg->part_fee;
2329 $cust_bill_pkg->set('part_fee', $part_fee);
2331 #die "fee definition not found for line item #".$cust_bill_pkg->billpkgnum."\n"; # might make more sense
2332 warn "fee definition not found for line item #".$cust_bill_pkg->billpkgnum."\n";
2335 if ( exists($options{section}) and exists($options{section}{category}) )
2337 my $categoryname = $options{section}{category};
2338 # then filter for items that have that section
2339 if ( $part_fee->categoryname ne $categoryname ) {
2340 warn "skipping fee '".$part_fee->itemdesc."'--not in section $categoryname\n" if $DEBUG;
2343 } # otherwise include them all in the main section
2344 # XXX what to do when sectioning by location?
2347 my %base_invnums; # invnum => invoice date
2348 foreach ($cust_bill_pkg->cust_bill_pkg_fee) {
2349 if ($_->base_invnum) {
2350 my $base_bill = FS::cust_bill->by_key($_->base_invnum);
2351 my $base_date = $self->time2str_local('short', $base_bill->_date)
2353 $base_invnums{$_->base_invnum} = $base_date || '';
2356 foreach (sort keys(%base_invnums)) {
2357 next if $_ == $self->invnum;
2359 $self->mt('from invoice \\#[_1] on [_2]', $_, $base_invnums{$_});
2362 { feepart => $cust_bill_pkg->feepart,
2363 amount => sprintf('%.2f', $cust_bill_pkg->setup + $cust_bill_pkg->recur),
2364 description => $part_fee->itemdesc_locale($self->cust_main->locale),
2365 ext_description => \@ext_desc
2376 warn "$me _items_pkg searching for all package line items\n"
2379 my @cust_bill_pkg = $self->_items_nontax;
2381 warn "$me _items_pkg filtering line items\n"
2383 my @items = $self->_items_cust_bill_pkg(\@cust_bill_pkg, @_);
2385 if ($options{section} && $options{section}->{condensed}) {
2387 warn "$me _items_pkg condensing section\n"
2391 local $Storable::canonical = 1;
2392 foreach ( @items ) {
2394 delete $item->{ref};
2395 delete $item->{ext_description};
2396 my $key = freeze($item);
2397 $itemshash{$key} ||= 0;
2398 $itemshash{$key} ++; # += $item->{quantity};
2400 @items = sort { $a->{description} cmp $b->{description} }
2401 map { my $i = thaw($_);
2402 $i->{quantity} = $itemshash{$_};
2404 sprintf( "%.2f", $i->{quantity} * $i->{amount} );#unit_amount
2410 warn "$me _items_pkg returning ". scalar(@items). " items\n"
2417 return 0 unless $a->itemdesc cmp $b->itemdesc;
2418 return -1 if $b->itemdesc eq 'Tax';
2419 return 1 if $a->itemdesc eq 'Tax';
2420 return -1 if $b->itemdesc eq 'Other surcharges';
2421 return 1 if $a->itemdesc eq 'Other surcharges';
2422 $a->itemdesc cmp $b->itemdesc;
2427 my @cust_bill_pkg = sort _taxsort grep { ! $_->pkgnum and ! $_->feepart }
2428 $self->cust_bill_pkg;
2429 my @items = $self->_items_cust_bill_pkg(\@cust_bill_pkg, @_);
2431 if ( $self->conf->exists('always_show_tax') ) {
2432 my $itemdesc = $self->conf->config('always_show_tax') || 'Tax';
2433 if (0 == grep { $_->{description} eq $itemdesc } @items) {
2435 { 'description' => $itemdesc,
2442 =item _items_cust_bill_pkg CUST_BILL_PKGS OPTIONS
2444 Takes an arrayref of L<FS::cust_bill_pkg> objects, and returns a
2445 list of hashrefs describing the line items they generate on the invoice.
2447 OPTIONS may include:
2449 format: the invoice format.
2451 escape_function: the function used to escape strings.
2453 DEPRECATED? (expensive, mostly unused?)
2454 format_function: the function used to format CDRs.
2456 section: a hashref containing 'category' and/or 'locationnum'; if this
2457 is present, only returns line items that belong to that category and/or
2458 location (whichever is defined).
2460 multisection: a flag indicating that this is a multisection invoice,
2461 which does something complicated.
2463 Returns a list of hashrefs, each of which may contain:
2465 pkgnum, description, amount, unit_amount, quantity, pkgpart, _is_setup, and
2466 ext_description, which is an arrayref of detail lines to show below
2471 sub _items_cust_bill_pkg {
2473 my $conf = $self->conf;
2474 my $cust_bill_pkgs = shift;
2477 my $format = $opt{format} || '';
2478 my $escape_function = $opt{escape_function} || sub { shift };
2479 my $format_function = $opt{format_function} || '';
2480 my $no_usage = $opt{no_usage} || '';
2481 my $unsquelched = $opt{unsquelched} || ''; #unused
2482 my ($section, $locationnum, $category);
2483 if ( $opt{section} ) {
2484 $category = $opt{section}->{category};
2485 $locationnum = $opt{section}->{locationnum};
2487 my $summary_page = $opt{summary_page} || ''; #unused
2488 my $multisection = defined($category) || defined($locationnum);
2489 my $discount_show_always = 0;
2491 my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 50;
2493 my $cust_main = $self->cust_main;#for per-agent cust_bill-line_item-ate_style
2494 # and location labels
2497 my ($s, $r, $u) = ( undef, undef, undef );
2498 foreach my $cust_bill_pkg ( @$cust_bill_pkgs )
2501 foreach ( $s, $r, ($opt{skip_usage} ? () : $u ) ) {
2502 if ( $_ && !$cust_bill_pkg->hidden ) {
2503 $_->{amount} = sprintf( "%.2f", $_->{amount} ),
2504 $_->{amount} =~ s/^\-0\.00$/0.00/;
2505 $_->{unit_amount} = sprintf( "%.2f", $_->{unit_amount} ),
2507 if $_->{amount} != 0
2508 || $discount_show_always
2509 || ( ! $_->{_is_setup} && $_->{recur_show_zero} )
2510 || ( $_->{_is_setup} && $_->{setup_show_zero} )
2516 if ( $locationnum ) {
2517 # this is a location section; skip packages that aren't at this
2519 next if $cust_bill_pkg->pkgnum == 0; # skips fees...
2520 next if $self->cust_pkg_hash->{ $cust_bill_pkg->pkgnum }->locationnum
2524 # Consider display records for this item to determine if it belongs
2525 # in this section. Note that if there are no display records, there
2526 # will be a default pseudo-record that includes all charge types
2527 # and has no section name.
2528 my @cust_bill_pkg_display = $cust_bill_pkg->can('cust_bill_pkg_display')
2529 ? $cust_bill_pkg->cust_bill_pkg_display
2530 : ( $cust_bill_pkg );
2532 warn "$me _items_cust_bill_pkg considering cust_bill_pkg ".
2533 $cust_bill_pkg->billpkgnum. ", pkgnum ". $cust_bill_pkg->pkgnum. "\n"
2536 if ( defined($category) ) {
2537 # then this is a package category section; process all display records
2538 # that belong to this section.
2539 @cust_bill_pkg_display = grep { $_->section eq $category }
2540 @cust_bill_pkg_display;
2542 # otherwise, process all display records that aren't usage summaries
2543 # (I don't think there should be usage summaries if you aren't using
2544 # category sections, but this is the historical behavior)
2545 @cust_bill_pkg_display = grep { !$_->summary }
2546 @cust_bill_pkg_display;
2549 my $classname = ''; # package class name, will fill in later
2551 foreach my $display (@cust_bill_pkg_display) {
2553 warn "$me _items_cust_bill_pkg considering cust_bill_pkg_display ".
2554 $display->billpkgdisplaynum. "\n"
2557 my $type = $display->type;
2559 my $desc = $cust_bill_pkg->desc( $cust_main ? $cust_main->locale : '' );
2560 $desc = substr($desc, 0, $maxlength). '...'
2561 if $format eq 'latex' && length($desc) > $maxlength;
2563 my %details_opt = ( 'format' => $format,
2564 'escape_function' => $escape_function,
2565 'format_function' => $format_function,
2566 'no_usage' => $opt{'no_usage'},
2569 if ( ref($cust_bill_pkg) eq 'FS::quotation_pkg' ) {
2571 warn "$me _items_cust_bill_pkg cust_bill_pkg is quotation_pkg\n"
2573 # quotation_pkgs are never fees, so don't worry about the case where
2574 # part_pkg is undefined
2576 if ( $cust_bill_pkg->setup != 0 ) {
2577 my $description = $desc;
2578 $description .= ' Setup'
2579 if $cust_bill_pkg->recur != 0
2580 || $discount_show_always
2581 || $cust_bill_pkg->recur_show_zero;
2583 'description' => $description,
2584 'amount' => sprintf("%.2f", $cust_bill_pkg->setup),
2587 if ( $cust_bill_pkg->recur != 0 ) {
2589 'description' => "$desc (". $cust_bill_pkg->part_pkg->freq_pretty.")",
2590 'amount' => sprintf("%.2f", $cust_bill_pkg->recur),
2594 } elsif ( $cust_bill_pkg->pkgnum > 0 ) { # and it's not a quotation_pkg
2596 warn "$me _items_cust_bill_pkg cust_bill_pkg is non-tax\n"
2599 my $cust_pkg = $cust_bill_pkg->cust_pkg;
2600 my $part_pkg = $cust_pkg->part_pkg;
2602 # which pkgpart to show for display purposes?
2603 my $pkgpart = $cust_bill_pkg->pkgpart_override || $cust_pkg->pkgpart;
2605 # start/end dates for invoice formats that do nonstandard
2607 my %item_dates = ();
2608 %item_dates = map { $_ => $cust_bill_pkg->$_ } ('sdate', 'edate')
2609 unless $part_pkg->option('disable_line_item_date_ranges',1);
2611 # not normally used, but pass this to the template anyway
2612 $classname = $part_pkg->classname;
2614 if ( (!$type || $type eq 'S')
2615 && ( $cust_bill_pkg->setup != 0
2616 || $cust_bill_pkg->setup_show_zero
2621 warn "$me _items_cust_bill_pkg adding setup\n"
2624 my $description = $desc;
2625 $description .= ' Setup'
2626 if $cust_bill_pkg->recur != 0
2627 || $discount_show_always
2628 || $cust_bill_pkg->recur_show_zero;
2630 $description .= $cust_bill_pkg->time_period_pretty( $part_pkg,
2632 if $part_pkg->is_prepaid #for prepaid, "display the validity period
2633 # triggered by the recurring charge freq
2635 && $cust_bill_pkg->recur == 0
2636 && ! $cust_bill_pkg->recur_show_zero;
2641 # always pass the svc_label through to the template, even if
2642 # not displaying it as an ext_description
2643 my @svc_labels = map &{$escape_function}($_),
2644 $cust_pkg->h_labels_short($self->_date, undef, 'I');
2646 $svc_label = $svc_labels[0];
2648 unless ( $cust_pkg->part_pkg->hide_svc_detail
2649 || $cust_bill_pkg->hidden )
2652 push @d, @svc_labels
2653 unless $cust_bill_pkg->pkgpart_override; #don't redisplay services
2654 my $lnum = $cust_main ? $cust_main->ship_locationnum
2655 : $self->prospect_main->locationnum;
2656 # show the location label if it's not the customer's default
2657 # location, and we're not grouping items by location already
2658 if ( $cust_pkg->locationnum != $lnum and !defined($locationnum) ) {
2659 my $loc = $cust_pkg->location_label;
2660 $loc = substr($loc, 0, $maxlength). '...'
2661 if $format eq 'latex' && length($loc) > $maxlength;
2662 push @d, &{$escape_function}($loc);
2665 } #unless hiding service details
2667 push @d, $cust_bill_pkg->details(%details_opt)
2668 if $cust_bill_pkg->recur == 0;
2670 if ( $cust_bill_pkg->hidden ) {
2671 $s->{amount} += $cust_bill_pkg->setup;
2672 $s->{unit_amount} += $cust_bill_pkg->unitsetup;
2673 push @{ $s->{ext_description} }, @d;
2677 description => $description,
2678 pkgpart => $pkgpart,
2679 pkgnum => $cust_bill_pkg->pkgnum,
2680 amount => $cust_bill_pkg->setup,
2681 setup_show_zero => $cust_bill_pkg->setup_show_zero,
2682 unit_amount => $cust_bill_pkg->unitsetup,
2683 quantity => $cust_bill_pkg->quantity,
2684 ext_description => \@d,
2685 svc_label => ($svc_label || ''),
2691 if ( ( !$type || $type eq 'R' || $type eq 'U' )
2693 $cust_bill_pkg->recur != 0
2694 || $cust_bill_pkg->setup == 0
2695 || $discount_show_always
2696 || $cust_bill_pkg->recur_show_zero
2701 warn "$me _items_cust_bill_pkg adding recur/usage\n"
2704 my $is_summary = $display->summary;
2705 my $description = $desc;
2706 if ( $type eq 'U' and defined($r) ) {
2707 # don't just show the same description as the recur line
2708 $description = $self->mt('Usage charges');
2711 my $part_pkg = $cust_pkg->part_pkg;
2713 $description .= $cust_bill_pkg->time_period_pretty( $part_pkg,
2717 my @seconds = (); # for display of usage info
2720 #at least until cust_bill_pkg has "past" ranges in addition to
2721 #the "future" sdate/edate ones... see #3032
2722 my @dates = ( $self->_date );
2723 my $prev = $cust_bill_pkg->previous_cust_bill_pkg;
2724 push @dates, $prev->sdate if $prev;
2725 push @dates, undef if !$prev;
2727 my @svc_labels = map &{$escape_function}($_),
2728 $cust_pkg->h_labels_short(@dates, 'I');
2729 $svc_label = $svc_labels[0];
2731 # show service labels, unless...
2732 # the package is set not to display them
2733 unless ( $part_pkg->hide_svc_detail
2734 # or this is a tax-like line item
2735 || $cust_bill_pkg->itemdesc
2736 # or this is a hidden (bundled) line item
2737 || $cust_bill_pkg->hidden
2738 # or this is a usage summary line
2739 || $is_summary && $type && $type eq 'U'
2740 # or this is a usage line and there's a recurring line
2741 # for the package in the same section (which will
2742 # have service labels already)
2743 || ($type eq 'U' and defined($r))
2747 warn "$me _items_cust_bill_pkg adding service details\n"
2750 push @d, @svc_labels
2751 unless $cust_bill_pkg->pkgpart_override; #don't redisplay services
2752 warn "$me _items_cust_bill_pkg done adding service details\n"
2755 my $lnum = $cust_main ? $cust_main->ship_locationnum
2756 : $self->prospect_main->locationnum;
2757 # show the location label if it's not the customer's default
2758 # location, and we're not grouping items by location already
2759 if ( $cust_pkg->locationnum != $lnum and !defined($locationnum) ) {
2760 my $loc = $cust_pkg->location_label;
2761 $loc = substr($loc, 0, $maxlength). '...'
2762 if $format eq 'latex' && length($loc) > $maxlength;
2763 push @d, &{$escape_function}($loc);
2766 # Display of seconds_since_sqlradacct:
2767 # On the invoice, when processing @detail_items, look for a field
2768 # named 'seconds'. This will contain total seconds for each
2769 # service, in the same order as @ext_description. For services
2770 # that don't support this it will show undef.
2771 if ( $conf->exists('svc_acct-usage_seconds')
2772 and ! $cust_bill_pkg->pkgpart_override ) {
2773 foreach my $cust_svc (
2774 $cust_pkg->h_cust_svc(@dates, 'I')
2777 # eval because not having any part_export_usage exports
2778 # is a fatal error, last_bill/_date because that's how
2779 # sqlradius_hour billing does it
2781 $cust_svc->seconds_since_sqlradacct($dates[1] || 0, $dates[0]);
2783 push @seconds, $sec;
2785 } #if svc_acct-usage_seconds
2787 } # if we are showing service labels
2789 unless ( $is_summary ) {
2790 warn "$me _items_cust_bill_pkg adding details\n"
2793 #instead of omitting details entirely in this case (unwanted side
2794 # effects), just omit CDRs
2795 $details_opt{'no_usage'} = 1
2796 if $type && $type eq 'R';
2798 push @d, $cust_bill_pkg->details(%details_opt);
2801 warn "$me _items_cust_bill_pkg calculating amount\n"
2806 $amount = $cust_bill_pkg->recur;
2807 } elsif ($type eq 'R') {
2808 $amount = $cust_bill_pkg->recur - $cust_bill_pkg->usage;
2809 } elsif ($type eq 'U') {
2810 $amount = $cust_bill_pkg->usage;
2813 if ( !$type || $type eq 'R' ) {
2815 warn "$me _items_cust_bill_pkg adding recur\n"
2819 ( $cust_bill_pkg->unitrecur > 0 ) ? $cust_bill_pkg->unitrecur
2822 if ( $cust_bill_pkg->hidden ) {
2823 $r->{amount} += $amount;
2824 $r->{unit_amount} += $unit_amount;
2825 push @{ $r->{ext_description} }, @d;
2828 description => $description,
2829 pkgpart => $pkgpart,
2830 pkgnum => $cust_bill_pkg->pkgnum,
2832 recur_show_zero => $cust_bill_pkg->recur_show_zero,
2833 unit_amount => $unit_amount,
2834 quantity => $cust_bill_pkg->quantity,
2836 ext_description => \@d,
2837 svc_label => ($svc_label || ''),
2839 $r->{'seconds'} = \@seconds if grep {defined $_} @seconds;
2842 } else { # $type eq 'U'
2844 warn "$me _items_cust_bill_pkg adding usage\n"
2847 if ( $cust_bill_pkg->hidden and defined($u) ) {
2848 # if this is a hidden package and there's already a usage
2849 # line for the bundle, add this package's total amount and
2850 # usage details to it
2851 $u->{amount} += $amount;
2852 push @{ $u->{ext_description} }, @d;
2853 } elsif ( $amount ) {
2854 # create a new usage line
2856 description => $description,
2857 pkgpart => $pkgpart,
2858 pkgnum => $cust_bill_pkg->pkgnum,
2861 recur_show_zero => $cust_bill_pkg->recur_show_zero,
2863 ext_description => \@d,
2865 } # else this has no usage, so don't create a usage section
2868 } # recurring or usage with recurring charge
2870 } else { # taxes and fees
2872 warn "$me _items_cust_bill_pkg cust_bill_pkg is tax\n"
2875 # items of this kind should normally not have sdate/edate.
2877 'description' => $desc,
2878 'amount' => sprintf('%.2f', $cust_bill_pkg->setup
2879 + $cust_bill_pkg->recur)
2882 } # if quotation / package line item / other line item
2884 } # foreach $display
2886 $discount_show_always = ($cust_bill_pkg->cust_bill_pkg_discount
2887 && $conf->exists('discount-show-always'));
2891 foreach ( $s, $r, ($opt{skip_usage} ? () : $u ) ) {
2893 $_->{amount} = sprintf( "%.2f", $_->{amount} ),
2894 if exists($_->{amount});
2895 $_->{amount} =~ s/^\-0\.00$/0.00/;
2896 $_->{unit_amount} = sprintf('%.2f', $_->{unit_amount})
2897 if exists($_->{unit_amount});
2900 if $_->{amount} != 0
2901 || $discount_show_always
2902 || ( ! $_->{_is_setup} && $_->{recur_show_zero} )
2903 || ( $_->{_is_setup} && $_->{setup_show_zero} )
2907 warn "$me _items_cust_bill_pkg done considering cust_bill_pkgs\n"
2914 =item _items_discounts_avail
2916 Returns an array of line item hashrefs representing available term discounts
2917 for this invoice. This makes the same assumptions that apply to term
2918 discounts in general: that the package is billed monthly, at a flat rate,
2919 with no usage charges. A prorated first month will be handled, as will
2920 a setup fee if the discount is allowed to apply to setup fees.
2924 sub _items_discounts_avail {
2927 #maybe move this method from cust_bill when quotations support discount_plans
2928 return () unless $self->can('discount_plans');
2929 my %plans = $self->discount_plans;
2931 my $list_pkgnums = 0; # if any packages are not eligible for all discounts
2932 $list_pkgnums = grep { $_->list_pkgnums } values %plans;
2936 my $plan = $plans{$months};
2938 my $term_total = sprintf('%.2f', $plan->discounted_total);
2939 my $percent = sprintf('%.0f',
2940 100 * (1 - $term_total / $plan->base_total) );
2941 my $permonth = sprintf('%.2f', $term_total / $months);
2942 my $detail = $self->mt('discount on item'). ' '.
2943 join(', ', map { "#$_" } $plan->pkgnums)
2946 # discounts for non-integer months don't work anyway
2947 $months = sprintf("%d", $months);
2950 description => $self->mt('Save [_1]% by paying for [_2] months',
2952 amount => $self->mt('[_1] ([_2] per month)',
2953 $term_total, $money_char.$permonth),
2954 ext_description => ($detail || ''),
2957 sort { $b <=> $a } keys %plans;