Support for custom template - If > 0 then do not display

Hi community,

I am trying to create an Invoice PDF template according to my wishes and have been stuck for a few days, I hope you can help me?

I am trying to get the values that are 0 or empty not to be displayed in the PDF.
I am currently having problems with discount and mulltiple tax (see screenshot).

As soon as I have finalized the template, I would like to share the template and everything about it with the community.

Many thanks in advance.

<?php
        $taxarray2 = array();
        $linecounter = 0;
        foreach ($items as $item) { ?>
        <?php
                      // load all tax_rates/mdl_tax_rates and calc subtotal per rate
                      //$this->load->model('tax_rates/mdl_tax_rates');
                      //$taxarray1 = $this->mdl_tax_rates->where('tax_rate_id', $item->item_tax_rate_id)->get()->row();                                 
                      //$taxarray2[$item->item_tax_rate_id]->taxcode = $taxarray1->tax_rate_code;
                      if (!isset($taxarray2[$item->item_tax_rate_id]))
                            $taxarray2[$item->item_tax_rate_id]=array();
                      $taxarray2[$item->item_tax_rate_id]['taxdescr'] = $item->item_tax_rate_name;
                      $taxarray2[$item->item_tax_rate_id]['taxperc'] = $item->item_tax_rate_percent;
                      if (isset($taxarray2[$item->item_tax_rate_id]['netamount']))
                            $taxarray2[$item->item_tax_rate_id]['netamount'] += ($item->item_subtotal - $item->item_discount);
                      else
                            $taxarray2[$item->item_tax_rate_id]['netamount'] = ($item->item_subtotal - $item->item_discount);
                      if (isset($taxarray2[$item->item_tax_rate_id]['taxamount'])) 
                            $taxarray2[$item->item_tax_rate_id]['taxamount'] += $item->item_tax_total;
                      else
                            $taxarray2[$item->item_tax_rate_id]['taxamount'] = $item->item_tax_total;
                ?>
            <tr>
                <td><?php _htmlsc($item->item_name); ?></td>
                <td><?php echo nl2br(htmlsc($item->item_description)); ?></td>
                <td class="text-right">
				<?php if ($item->item_quantity > 0) : ?>
                    <?php echo format_amount($item->item_quantity); ?>
                    <?php if ($item->item_product_unit) : ?>
                        <br>
                        <small><?php _htmlsc($item->item_product_unit); ?></small>
                    <?php endif; ?>
				<?php endif; ?>
                </td>
                <td class="text-right">
				<?php if ($item->item_price > 0) : ?>
                    <?php echo format_currency($item->item_price); ?>
				<?php endif; ?>
                </td>
                <td class="text-right">
				<?php if ($item->item_tax_rate_percent > 0) : ?>
                    <?php echo format_amount($item->item_tax_rate_percent) . '%'; ?>
				<?php endif; ?>
                </td>
                <?php if ($show_item_discounts) : ?>
                    <td class="text-right">
                        <?php echo format_currency($item->item_discount); ?>
                    </td>
                <?php endif; ?>
                <td class="text-right">
				<?php if ($item->item_subtotal> 0) : ?>
                    <?php echo format_currency($item->item_subtotal); ?>
				<?php endif; ?>
                </td>
            </tr>
        <?php } ?>


        </tbody>
        <tbody class="invoice-sums">

        <tr>
            <td class="text-right"></td>
            <td <?php echo($show_item_discounts ? 'colspan="5"' : 'colspan="4"'); ?> class="text-right">
                <?php _trans('subtotal'); ?>
            </td>
            <td class="text-right"><?php echo format_currency($invoice->invoice_item_subtotal); ?></td>
        </tr>
       
        <?php if ($invoice->invoice_item_tax_total > 0) {
            foreach($taxarray2 as $key => $value) :?>
                <tr>
                    <td <?php echo($show_item_discounts ? 'colspan="5"' : 'colspan="4"'); ?> class="text-right"></td>
                    <td class="text-right"><?php echo 'USt.('. format_amount($value['taxperc']).'%)'; ?></>
                    </td>
                    <td class="text-right"><?php echo format_currency($value['taxamount']); ?></td>
                </tr>
        <?php endforeach ?>
        <!--<tr> Steuer Summe
                <td class="text-right"></td>
                <td <?php echo($show_item_discounts ? 'colspan="5"' : 'colspan="4"'); ?> class="text-right">
                    <?php _trans('item_tax'); ?>
                </td>
                <td class="text-right">
                    <?php echo format_currency($invoice->invoice_item_tax_total);?>
                </td>
            </tr>-->
       <?php } ?>
1 Like

Your problem might be with the format_currency function, but let’s focus on 1 thing first.
See this:

$invoice->invoice_item_tax_total);

In all those places where you see those zeroes as amounts, make it this:

($invoice->invoice_item_tax_total != 0) ? $invoice->invoice_item_tax_total : null;

Try it out with one of those items, see what it looks Iike.

As soon as I have finalized the template, I would like to share the template and everything about it with the community

That’s really appreciated.
I’ve created a branch for you on the InvoicePlane repository: feature/custom-template-with-discount-and-multiple-tax

If you check out that branch and apply your customizations there, it’s also a little bit easier to see.

Your second tax part probably needs a database change.
And the name for your tax … Mwst? That needs a translation in ip_lang.php in application/language/

Hi UnderDog,

many thanks for your reply.
I have tested this, unfortunately the lines are still shown on the pdf. (screenshot below)

<?php if ($invoice->invoice_item_tax_total > 0) {
            foreach($taxarray2 as $key => $value) :?>
                <tr>
                    <td <?php echo($show_item_discounts ? 'colspan="5"' : 'colspan="4"'); ?> class="text-right"></td>
                    <td class="text-right"><?php echo 'USt.('. format_amount($value['taxperc']).'%)'; ?></>
                    </td>
                    <td class="text-right"><?php echo format_currency($value['taxamount']); ?></td>
                </tr>
        <?php endforeach ?>
        <!--<tr> Steuer Summe
                <td class="text-right"></td>
                <td <?php echo($show_item_discounts ? 'colspan="5"' : 'colspan="4"'); ?> class="text-right">
                    <?php _trans('item_tax'); ?>
                </td>
                <td class="text-right">
                    <?php echo format_currency($invoice->invoice_item_tax_total != 0) ? $invoice->invoice_item_tax_total : null;?>
                </td>
            </tr>-->

image

many thanks
mctiger

Hi community,

below you will find the solution:

discount (fixed)

 <?php if ($show_item_discounts) : ?>
					<?php if ($item->item_discount > 0) : ?>
						<td class="text-right">
						<?php echo format_currency($item->item_discount); ?>
						</td>
					<?php else : ?>
						<td class="text-right"></td>
					<?php endif; ?>
				<?php endif; ?>

tax

 <?php 
if ($invoice->invoice_item_tax_total > 0) {
    foreach($taxarray2 as $key => $value ) :
        if ($value['taxamount'] != 0 && !empty($value['taxamount'])) :
?>
            <tr>
                <td <?php echo($show_item_discounts ? 'colspan="5"' : 'colspan="4"'); ?> class="text-right"></td>
                <td class="text-right"><?php echo 'USt.('. format_amount($value['taxperc']).'%)'; ?></>
                </td>
                <td class="text-right"><?php echo format_currency($value['taxamount']); ?></td>
            </tr>
<?php 
        endif;
    endforeach;
}
?>

@UnderDog , many thanks for your Support.

1 Like

Hello @mctiger, happy to hear you could find the solution you were looking for. Feel free to post the whole solution / template file in the themes and templates section https://community.invoiceplane.com/c/templates/32

Hi Naui95,
that’s the plan once I’ve finalized the templates.

At the moment I still have two problems.

  1. if I don’t have a discount, the formatting doesn’t fit.
  2. the “$item->item_discount” is including taxes, but is deducted from the subtotal (net), which is wrong. (this is also the case in the standard IP template)

with discount:
image

without discount:

and regarding discount calculation, here with the standard teemplate as an example:

correct: 100 x 19% = 119€ - 5€ discount = 114€
wrong: subtotal - 5€ (100€ + 50€ - 5€ =145€ x 19% =172,55€)
image

EDIT:

  1. issue is resolved, code below.
<?php if ($show_item_discounts) : ?>
					<?php if ($item->item_discount > 0) : ?>
						<td class="text-right">
						<?php echo format_currency($item->item_discount); ?>
						</td>
					<?php else : ?>
						<td class="text-right"></td>
					<?php endif; ?>
				<?php endif; ?>