-<% if ( $conf->exists('invoice_latex') ) { %>
- <A HREF="<%= $p %>view/cust_bill-pdf.cgi?$link.pdf">View typeset invoice</A>
- <BR><BR>
-<% } %>
-
-<% #false laziness with search/cust_bill_event.cgi
- unless ( $templatename ) { %>
-
- <%= table() %>
- <TR>
- <TH>Event</TH>
- <TH>Date</TH>
- <TH>Status</TH>
- </TR>
-
- <% foreach my $cust_bill_event (
- sort { $a->_date <=> $b->_date } $cust_bill->cust_bill_event
- ) {
-
- my $status = $cust_bill_event->status;
- $status .= ': '. encode_entities($cust_bill_event->statustext)
- if $cust_bill_event->statustext;
- my $part_bill_event = $cust_bill_event->part_bill_event;
- %>
- <TR>
- <TD><%= $part_bill_event->event %>
-
- <% if (
- $part_bill_event->plan eq 'send_alternate'
- && $part_bill_event->plandata =~ /^(agent_)?templatename (.*)$/m
- ) {
- my $alt_templatename = $2;
- my $alt_link = "$templatename-$invnum";
- %>
- ( <A HREF="<%= $p %>view/cust_bill.cgi?<%= $alt_link %>">view</A>
- | <A HREF="<%= $p %>view/cust_bill-pdf.cgi?<%= $alt_link %>.pdf">view
- typeset</A>
- | <A HREF="<%= $p %>misc/print-invoice.cgi?<%= $alt_link %>">re-print</A>
- <% if ( grep { $_ ne 'POST' }
- $cust_bill->cust_main->invoicing_list ) { %>
- | <A HREF="<%= $p %>misc/email-invoice.cgi?<%= $alt_link %>">re-email</A>
- <% } %>
-
- <% if ( $conf->exists('hylafax')
- && length($cust_bill->cust_main->fax) ) { %>
- | <A HREF="<%= $p %>misc/fax-invoice.cgi?<%= $alt_link %>">re-fax</A>
- <% } %>
-
- )
- <% } %>
-
- </TD>
- <TD><%= time2str("%a %b %e %T %Y", $cust_bill_event->_date) %></TD>
- <TD><%= $status %></TD>
- </TR>
- <% } %>
-
- </TABLE>
- <BR>
-
-<% } %>
-
-<% if ( $conf->exists('invoice_html') ) { %>
- <%= $cust_bill->print_html('', $templatename) %>
-<% } else { %>
- <PRE><%= $cust_bill->print_text('', $templatename) %></PRE>
-<% } %>
-
-</BODY></HTML>
+% if ( $conf->exists('invoice_html') && ! $cgi->param('plaintext') ) {
+ <% join('', $cust_bill->print_html(\%opt) ) %>
+% } else {
+ <PRE><% join('', $cust_bill->print_text(\%opt) ) |h %></PRE>
+% }
+
+<& /elements/footer-cust_main.html &>
+<%init>
+
+my $curuser = $FS::CurrentUser::CurrentUser;
+
+die "access denied"
+ unless $curuser->access_right('View invoices');
+
+my $conf = FS::Conf->new;
+
+my( $invnum, $mode, $template, $notice_name, $no_coupon );
+my($query) = $cgi->keywords;
+if ( $query =~ /^((.+)-)?(\d+)$/ ) {
+ $template = $2;
+ $invnum = $3;
+ $notice_name = 'Invoice';
+} else {
+ $invnum = $cgi->param('invnum');
+ $template = $cgi->param('template');
+ $notice_name = $cgi->param('notice_name');
+ $mode = $cgi->param('mode');
+}
+
+if ($mode eq 'statement') {
+ $mode = undef;
+ $template = 'statement';
+ $notice_name = 'Statement';
+ $no_coupon = 1;
+}
+
+my $include_statement_template = $conf->config('payment_receipt_statement_mode') ? 0 : 1;
+
+my %opt = (
+ 'unsquelch_cdr' => $conf->exists('voip-cdr_email'),
+ 'template' => $template,
+ 'notice_name' => $notice_name,
+);
+
+$opt{'barcode_img'} = 1 if $conf->exists('invoice-barcode');
+
+my $cust_bill = qsearchs({
+ 'select' => 'cust_bill.*',
+ 'table' => 'cust_bill',
+ 'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
+ 'hashref' => { 'invnum' => $invnum },
+ 'extra_sql' => ' AND '. $curuser->agentnums_sql,
+});
+# if we're asked for a voided invnum, redirect appropriately
+if (!$cust_bill and FS::cust_bill_void->row_exists("invnum = $invnum") ) {
+ $m->clear_buffer;
+ my $url = $p.'view/cust_bill_void.html?'.$cgi->query_string;
+ $m->print( $cgi->redirect($url) );
+ $m->abort;
+}
+die "Invoice #$invnum not found!" unless $cust_bill;
+
+$cust_bill->set('mode' => $mode);
+
+my $custnum = $cust_bill->custnum;
+my $display_custnum = $cust_bill->cust_main->display_custnum;
+
+my $link = "invnum=$invnum";
+$link .= ';mode=' . $mode if $mode;
+$link .= ';template='. uri_escape($template) if $template;
+$link .= ';notice_name='. $notice_name if $notice_name;
+$link .= ';no_coupon=1' if $no_coupon;
+
+</%init>