<?php
$taxes = [];
$item_cnt = 0;
foreach ($invoice->getItems() as $item) {
    if ($prefix == 'second_' && !$item->rebill_times) {
        //skip not recurring items
        continue;
    }
    $item_cnt++;
    if ($item->tax_rate && $item->{$prefix.'tax'}) {
        if (!isset($taxes[$item->tax_rate])) {
            $taxes[$item->tax_rate] = 0;
        }
        $taxes[$item->tax_rate] += $item->{$prefix.'tax'};
    }
}
if (!$taxes) {
    $taxes[$invoice->tax_rate] = $invoice->{$prefix.'tax'};
}

$subtotal = $invoice->{$prefix.'subtotal'};
$discount = $invoice->{$prefix.'discount'};
$shipping = $invoice->{$prefix.'shipping'};
$tax = $invoice->{$prefix.'tax'};
$taxable_subtotal = $subtotal - $discount;
$total = $invoice->{$prefix.'total'};

$show_tax = (float)$tax || ($di->plugins_tax->getEnabled() && $di->config->get('invoice_always_tax'));
?>
<table width="100%" style="border-collapse:collapse">
<?php $num=1; ?>
    <tr style="border-bottom:1px solid #eee; background:white; font-weight:bold;">
        <?php if ($item_cnt>1) : ?><th width="1%" style="padding:0.5em" nowrap="nowrap">#</th><?php endif; ?>
        <th style="padding:0.5em; text-align:left"><?php __e('Subscription/Product Title');?></th>
        <th style="padding:0.5em; text-align:right"><?php __e('Unit Price');?></th>
        <th style="padding:0.5em; text-align:right"><?php __e('Total Price');?></th>
    </tr>
<?php foreach ($invoice->getItems() as $item) : ?>
    <?php if ($prefix == 'second_' && !$item->rebill_times) continue; //skip not recurring items ?>
    <tr style="border-bottom:1px solid #eee; background:white">
        <?php if ($item_cnt>1) : ?><td width="1%" style="padding:0.5em" nowrap="nowrap"><?php p($num++) ?>.</td><?php endif; ?>
        <td style="padding:0.5em"><?php
            $item_title = ___($item->item_title);
            $options = [];
            foreach($item->getOptions() as $optKey => $opt) {
                $options[] = sprintf('%s: %s',
                    strip_tags($opt['optionLabel']),
                    implode(', ', array_map('strip_tags', (array)$opt['valueLabel'])));
            }
            if ($options) {
                $item_title .= sprintf(' (%s)', implode(', ', $options));
            }
            p($item_title) ?></td>
        <td style="padding:0.5em; text-align:right"><?php p($item->qty) ?>&times;<?php p(Am_Currency::render($item->{$prefix.'price'}, $invoice->currency)) ?></td>
        <td style="padding:0.5em; text-align:right"><?php p(Am_Currency::render(moneyRound($item->{$prefix.'price'} * $item->qty), $invoice->currency)) ?></td>
    </tr>
<?php endforeach; ?>
<?php if ($subtotal != $total) : ?>
    <tr style="border-bottom:1px solid #eee; background:#eee">
        <?php if ($item_cnt>1) : ?><td style="padding:0.5em"></td><?php endif; ?>
        <td colspan="2" style="padding:0.5em; text-align:right"><?php __e('Subtotal') ?></td>
        <td style="padding:0.5em; text-align:right"><?php p(Am_Currency::render($subtotal, $invoice->currency)) ?></td>
    </tr>
<?php endif; ?>
<?php if ($discount > 0) : ?>
<tr style="border-bottom:1px solid #eee; background:#eee">
    <?php if ($item_cnt>1) : ?><td style="padding:0.5em"></td><?php endif; ?>
    <td colspan="2" style="padding:0.5em; text-align:right"><?php __e('Discount') ?></td>
    <td style="padding:0.5em; text-align:right"><?php p(Am_Currency::render($discount, $invoice->currency)) ?></td>
</tr>
<?php endif; ?>
<?php if ($shipping > 0) : ?>
<tr style="border-bottom:1px solid #eee; background:#eee">
    <?php if ($item_cnt>1) : ?><td style="padding:0.5em"></td><?php endif; ?>
    <td colspan="2" style="padding:0.5em; text-align:right"><?php __e('Shipping') ?></td>
    <td style="padding:0.5em; text-align:right"><?php p(Am_Currency::render($shipping, $invoice->currency)) ?></td>
</tr>
<?php endif; ?>
<?php if ($show_tax) : ?>
    <tr style="border-bottom:1px solid #eee; background:#eee">
        <?php if ($item_cnt>1) : ?><td style="padding:0.5em"></td><?php endif; ?>
        <td colspan="2" style="padding:0.5em; text-align:right"><?php __e('Taxable Subtotal') ?></td>
        <td style="padding:0.5em; text-align:right"><?php echo $invoice->getCurrency($taxable_subtotal) ?></td>
    </tr>
    <?php foreach ($taxes as $rate => $tax_total) : ?>
    <tr style="border-bottom:1px solid #eee; background:#eee">
        <?php if ($item_cnt>1) : ?><td style="padding:0.5em"></td><?php endif; ?>
        <td colspan="2" style="padding:0.5em; text-align:right"><?php __e('Tax') ?> (<?php p($rate) ?>%)</td>
        <td style="padding:0.5em; text-align:right"><?php echo $invoice->getCurrency($tax_total) ?></td>
    </tr>
    <?php endforeach; ?>
<?php endif; ?>
<tr style="border-bottom:1px solid #eee; background:#eee; font-weight:bold">
    <?php if ($item_cnt>1) : ?><td style="padding:0.5em"></td><?php endif; ?>
    <td colspan="2" style="padding:0.5em; text-align:right"><?php __e('Total') ?></td>
    <td style="padding:0.5em; text-align:right"><?php p(Am_Currency::render($total, $invoice->currency)) ?></td>
</tr>
</table>