<STYLE TYPE="text/css">
.invoice {
  font-family: sans-serif;
  font-size: 10pt;
  display: inline-block;
  padding: 4pt;
  border: 1px solid black;
  background-color: white;
  min-width: 625px;
  position: relative;
}
.invoice_header {
  font-size: 10pt;
  border-spacing: 4pt;
}
.invoice_headerright TH { border-top: 2px solid #000000; border-bottom: 2px solid #000000 }
.invoice_headerright TD { font-size: 10pt; empty-cells: show }
.invoice_summary TH { border-bottom: 2px solid #000000 }
.invoice_summary TD { font-size: 10pt; empty-cells: show }
.invoice_longtable table { cellspacing: none }
.invoice_longtable TH { border-top: 2px solid #000000; border-bottom: 1px solid #000000; padding-left: none; padding-right: none; font-size: 10pt }
.invoice_desc TD { border-top: 2px solid #000000; font-weight: bold; font-size: 10pt }
.invoice_desc_more TD { font-weight: bold; font-size: 10pt }
.invoice_extdesc TD { font-size: 8pt }
.invoice_totaldesc TD { font-size: 10pt; empty-cells: show }
.allcaps { text-transform:uppercase; font-size: 12pt }
.watermark-box {
  z-index: 10;
  position: absolute;
  text-align: center;
  padding: 0;
  filter: alpha(opacity=25);
  opacity: 0.25;
  width: 100%;
  height: 100%;
  display: table;
  pointer-events: none;
}
.watermark-content {
  display: table-cell;
  vertical-align: middle;
  font-size: 96pt;
}
</STYLE>

<div class="invoice">

  <DIV CLASS="watermark-box"><DIV CLASS="watermark-content">
      <%= $watermark %>
  </DIV></DIV>

  <table class="invoice_header" width="100%">
    <tr>
     <td align="left"><%= $returnaddress %></td>
     <td align="left"><img src="<%= $cid ? "cid:$cid" : "cust_bill-logo.cgi?invnum=$invnum;template=$template" %>"></td>
      <td align="right">
        <table CLASS="invoice_headerright" cellspacing=0>
          <tr>
            <td align="center">
              <%= emt('Invoice date') %><BR>
              <B><%= $date %></B>
            </td>
            <td>
            </td>
            <td align="center">
              <%= emt('Invoice #') %><BR>
              <B><%= $invnum %></B>
            </td>
            <td>
            </td>
            <td align="center">
              <%= emt('Customer #') %><BR>
              <B><%= $custnum %></B>
            </td>
          </tr>
          <tr>
            <th>&nbsp;</th>
            <th colspan=3 align="center" class="allcaps">
	      <FONT SIZE="+3"><%= substr(emt($notice_name),0,1) %></FONT><FONT SIZE="+2"><%= substr(emt($notice_name),1) %></FONT>
            </th>
            <th>&nbsp;</th>
          </tr>
        </table>
      </td>
    </tr>
  </table>

  <table class="invoice_header" width="100%">
    <tr>
      <td WIDTH="10%">
      </td>
      <td align="left">
        <b><%= $payname %></b><BR>
        <%= join('<BR>', grep length($_), $company,
                                          $address1,
                                          $address2,
                                          "$city,&nbsp;$state&nbsp;&nbsp;$zip",
                                          $country,
                )
        %>
      </td>
      <%= $ship_enable ? ('<td align="left">'.
                          join('<BR>',grep length($_), '<b>'.emt('Service Address').'</b>',
                                                       $ship_company,
                                                       $ship_address1,
                                                       $ship_address2,
                                                       "$ship_city,&nbsp;$ship_state&nbsp;$ship_zip",
                                                       $ship_country,
                                                       ' ',
                                                       ' ',
                              ).
                           '</td><tr><td></td><td></td>'
                         )
                       : ''
      %>
      <td align="right">
    <%=
	if($barcode_cid) {
	    $OUT .= qq! <img src="cid:$barcode_cid"><br> !;
	}
	elsif($barcode_img) {
	    $OUT .= qq! <img src="cust_bill-barcode.cgi?invnum=$invnum;template=$template"><br> !;
	}
    %>
        <%= $terms ? emt('Terms') . ': ' . emt($terms) : '' %><BR>
        <%= $po_line %>
      </td>
    </tr>

  </table>
  <%= $summary %>
  <%=
      my $notfirst = 0;
      my $columncount = $unitprices ? 5 : 3;
      foreach my $section ( grep { !$summary || $_->{description} ne $finance_section } @sections ) {
        if ($section->{'pretotal'} && !$summary) {
          $OUT .= '</table>' if $notfirst++;
          $OUT .=
            '<table width="100%"><tr><td>'.
            '<p align="right"><b><font size="+1">'.
            uc(substr($section->{'pretotal'},0,1)).
            '</font><font size="+0">'. uc(substr($section->{'pretotal'},1)).
            '</font></b>'.
            '<p>'.
            '</td></tr>';
        }
        unless ($section->{'summarized'}) {
          if ( $notfirst || $section->{'pretotal'} && !$summary ) {
            $OUT .= '</table>';
            $notfirst = 1;
          }
          $OUT .= '<table><tr><td>';
          $OUT .= '<p class="allcaps"><b>';
          my $sectionhead;
          if ( $section->{'location'} ) {
            $sectionhead .= $section->{'location'}{'label_prefix'}. ': '
              if length($section->{'location'}{'label_prefix'});
            $sectionhead = $section->{'location'}{'address1'};
            $sectionhead .= ', '.$section->{'location'}{'address2'}
              if length($section->{'location'}{'address2'});
            $sectionhead .= ', ' .
                            $section->{'location'}{'city'} . ', ' .
                            $section->{'location'}{'state'} . ' ' .
                            $section->{'location'}{'zip'};
            $OUT .= $sectionhead;
          } else {
            $sectionhead = $section->{'description'} || emt('Charges');
            $OUT .= '<font size="+1">'. substr($sectionhead,0,1).
              '</font><font size="+0">'. substr($sectionhead,1).
          
              '</font>';
          }
          $OUT .= '</b></p></td></tr></table>';

          $OUT .=
            '<table class="invoice_longtable" CELLSPACING=0 WIDTH="100%">'.
            '<tr>';

          if ($section->{header_generator}) {
            my $header = &{$section->{header_generator}}();
            $OUT .= $header;
            $columncount = scalar(my @array = split /<\/th><th/i, $header);
          } else {
            my @headings = ( '', 'Description', 'Amount' );
            my @aligns = ( 'center', 'left', 'right' );
            if ( $unitprices ) {
              splice @headings, 2, 0, 'Unit Price', 'Quantity';
              splice @aligns,   2, 0, 'right', 'right';
            }
            if ( $section->{usage_section} ) {
              @headings = ( '', 'Description', 'Calls', 'Duration', 'Amount' );
              @aligns = ( '', 'left', 'right', 'right', 'right' );
              $columncount = 5;
            }

            while ( @headings ) {
              my $heading = shift @headings;
              $heading = emt($heading) if $heading;
              my $align = shift @aligns;
              $OUT .= '
                <th align="' . $align . '">' . $heading . '</th>';
            }
          }

          $OUT .= '</tr>';

          my $lastref = 0;
          foreach my $line (
            grep { ( scalar(@sections) > 1 
                   ? $section->{'description'} eq $_->{'section'}->{'description'}
                   : 1
                 ) }
            @detail_items )
          {
            if ( $section->{description_generator} ) {
              $OUT .= '<tr class="invoice_desc' .
                      &{$section->{description_generator}}($line);
            } elsif ( $section->{usage_section} ) {
              my $minutes = sprintf('%d', $line->{'duration'} / 60);
              my $seconds = $line->{'duration'} % 60;
              $OUT .= '
                <tr class="invoice_desc_more">
                  <td></td>
                  <td align="left">' . $line->{'description'} . '</td>
                  <td align="right">' . $line->{'quantity'} . '</td>
                  <td align="right">' . $minutes . 'm ' . $seconds . 's' . '</td>
                  <td align="right">' . $line->{'amount'} . '</td>
                </tr>';
            } else {
              my $class = 'invoice_desc_more';
              if ( ($line->{'ref'} || 0) ne $lastref ) {
                # then it's a new package (not a continuation)
                $class = 'invoice_desc';
              }
              $OUT .= '<tr class="'.$class.'">
                       <td align="center">';
              $OUT .= '</td>
                       <td align="left">'. $line->{'description'}. '</td>';
              if ( $unitprices ) {
                $OUT .= 
                  '<td align="right">'. $line->{'unit_amount'}. '</td>'.
                  '<td align="right">'. $line->{'quantity'}. '</td>';
              }
              $OUT .= '<td align="right">'. $line->{'amount'}. '</td>';
            }
            $OUT .= '</tr>';
            $lastref = $line->{'ref'} || 0;
            if ( @{$line->{'ext_description'} } ) {
              unless ( $section->{description_generator} ) {
                $OUT .= '<tr class="invoice_extdesc"><td></td><td';
                $OUT .= $unitprices ? ' colspan=3' : '';
                $OUT .= '><table width="100%">';
              }
              foreach my $ext_desc ( @{$line->{'ext_description'} } ) {
                $OUT .=
                  '<tr class="invoice_extdesc">'.
                    ( $section->{'description_generator'} ? '<td></td>' : '' ).
                    '<td align="left" '.
                         ( $ext_desc =~ /<\/?TD>/i ? '' : 'colspan=99' ). '>'.
                      '&nbsp;&nbsp;'. $ext_desc.
                    '</td>'.
                  '</tr>'
              }
              unless ( $section->{description_generator} ) {
                $OUT .= '</table></td><td></td>';
              }
              $OUT .= '</tr>';
            }
          }


          if ($section->{'description'} || $multisection and !$section->{no_subtotal}) {
            my $style = 'border-top: 3px solid #000000;'.
                        'border-bottom: 3px solid #000000;';
            $OUT .=
              '<tr class="invoice_totaldesc">'.
              qq(<td style="$style">&nbsp;</td>);
            if ($section->{total_generator}) {
              $OUT .= &{$section->{total_generator}}($section);
            } else {
              $OUT .= qq(<td align="left" style="$style"). 
                      ( $unitprices ? ' colspan=3>' : '>' ).
                      $section->{'description'}. ' ' . emt('Total') . '</td>'.
                      qq(<td align="right" style="$style">).
                      $section->{'subtotal'}. '</td>';
            }
            $OUT .= '</tr>';
          }
        } # if !$section->{summarized}
        if ($section->{'posttotal'}) {
          $OUT .= '<tr><td align="right" colspan='. $columncount. '>';
          $OUT .=
            '<p><font size="+1">'. $section->{'posttotal'}.
            '</font>'.
            '<p>';
          $OUT .= '</td></tr>';
        }
      }

      my $style = 'border-top: 3px solid #000000;';
      my $linenum = 0;

      foreach my $line ( @total_items ) {

        $style .= 'border-bottom: 3px solid #000000;'
          if ++$linenum == scalar(@total_items) - ( $balance_due_below_line ? 1 : 0 );

        $OUT .=
          '<tr class="invoice_totaldesc">';
        if ($section->{total_line_generator}) {
          $OUT .= &{$section->{total_line_generator}}($line);
        } else {
          $OUT .= qq(<td style="$style">&nbsp;</td>).
                  qq(<td align="left" style="$style" colspan=").
                    ( $columncount - 2 ). '">'.
                    $line->{'total_item'}. '</td>'.
                  qq(<td align="right" style="$style">).
                    $line->{'total_amount'}. '</td>';
        }
        $OUT .= '</tr>';

        $style='';

      }

    %>
  </table>
  <br><br>

<%=

  my @location_summary_sections =
    grep {
      ref $_->{location}
      && $_->{locationnum}
      && $_->{description}
      && $_->{description} ne $finance_section
    } @sections;

  if ( $multisection eq 'location' && scalar(@location_summary_sections) > 1 ) {

    $OUT .= '
      <hr>
      <table width="100%">
        <tr>
          <td>
            <p class="allcaps">
              <b>'.emt('Summary Of New Charges By Location').'</b>
            <p>
          </td>
        </tr>
      </table>

      <table class="invoice_longtable" cellspacing="0" width="100%">
      <thead>
        <tr>
          <th></th>
          <th align="left">'.emt('Location').'</th>
          <th align="right">'.emt('Amount').'</th>
        </tr>
      </thead>
      <tbody>
    ';

    for my $section (@location_summary_sections) {
      next unless $section->{description};
      $OUT .= '
        <tr class="invoice_desc_more">
          <td></td>
          <td>'.$section->{description}.'</td>
          <td align="right">'. $section->{subtotal} .'</td>
        </tr>
      ';
    }

    $OUT .= '
      <tr class="invoice_desc"><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
      </tbody>
      </table>
      <br><br>
    ';

} %>

<%= length($summary)
      ? ''
      : ( $smallernotes
            ? '<FONT SIZE="-1">'.$notes.'</FONT>'
            : $notes
        )
%>

  <hr NOSHADE SIZE=2 COLOR="#000000">
  <p align="center" <%= $smallerfooter ? 'STYLE="font-size:75%;"' : '' %>><%= $footer %>

</div>
