1

I wanted to make some changes in invoice pdf. For example, adding price + tax and display in the subtotal column. I have already created a custom module for editing the invoice. I have made some in the invoice like adding custom attribute value in the Invoice.

But I don't know how to add price and tax and display in the subtotal column.

enter image description here

In the DefaultInvoice.php, there is a draw function having items price values.

public function draw()
{
  ------------------------
  foreach ($prices as $priceData) {
        if (isset($priceData['label'])) {
            // draw Price label
            $lines[$i][] = ['text' => $priceData['label'], 'feed' => $feedPrice, 'align' => 'right'];
            // draw Subtotal label
            $lines[$i][] = ['text' => $priceData['label'], 'feed' => $feedSubtotal, 'align' => 'right'];
            $i++;
        }
        // draw Price
        $lines[$i][] = [
            'text' => $priceData['price'],
            'feed' => $feedPrice,
            'font' => 'bold',
            'align' => 'right',
        ];
        // draw Subtotal
        $lines[$i][] = [
            'text' => $priceData['subtotal'],
            'feed' => $feedSubtotal,
            'font' => 'bold',
            'align' => 'right',
        ];
        $i++;
    }

    // draw Tax
    $lines[0][] = [
        'text' => $order->formatPriceTxt($item->getTaxAmount()),
        'feed' => 495,
        'font' => 'bold',
        'align' => 'right',
    ];
}

Please let me know how to do addition.

Thanks.

  • You want to show price+tax in subtotal column here ? – Kishan Savaliya Nov 19 '19 at 16:41
  • Yes. I just want to add price and tax amount. and display it in the subtotal column. – Sudhakar SJ Nov 19 '19 at 16:43
  • I believe you displayed Price and Tax column there you just need to know how you can show price+tax in subtotal column? – Kishan Savaliya Nov 19 '19 at 16:49
  • Yes.................. – Sudhakar SJ Nov 19 '19 at 16:50
  • Check the example invoice template image attached in this thread.(i.stack.imgur.com/6Nwem.png) – Sudhakar SJ Nov 19 '19 at 16:58
2

You need to create di.xml file here in your custom module

app/code/Vendor/Module/etc/di.xml

Content for this file is..

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Model\Order\Pdf\Items\AbstractItems">
        <plugin name="custom_plugin" type="Vendor\Module\Plugin\AbstractPdfItemsPlugin" sortOrder="10" disabled="false"  />
    </type>
</config>

Now you need to create one Plugin here in your custom module

app/code/Vendor/Module/Plugin/AbstractPdfItemsPlugin.php

Content for this file is..

<?php

namespace Vendor\Module\Plugin;

class AbstractPdfItemsPlugin
{
    protected $_taxData;

    public function __construct(
        \Magento\Tax\Helper\Data $taxData
    ) {
        $this->_taxData = $taxData;
    }

    public function aroundGetItemPricesForDisplay(
        \Magento\Sales\Model\Order\Pdf\Items\AbstractItems $subject, callable $proceed
    ){
        $result = $proceed();
        $order = $subject->getOrder();
        $item = $subject->getItem();
        if ($this->_taxData->displaySalesBothPrices()) {
            $prices = [
                [
                    'label' => __('Excl. Tax') . ':',
                    'price' => $order->formatPriceTxt($item->getPrice()),
                    'subtotal' => $order->formatPriceTxt($item->getRowTotal()),
                ],
                [
                    'label' => __('Incl. Tax') . ':',
                    'price' => $order->formatPriceTxt($item->getPriceInclTax()),
                    'subtotal' => $order->formatPriceTxt($item->getRowTotalInclTax())
                ],
            ];
        } elseif ($this->_taxData->displaySalesPriceInclTax()) {
            $prices = [
                [
                    'price' => $order->formatPriceTxt($item->getPriceInclTax()),
                    'subtotal' => $order->formatPriceTxt($item->getRowTotalInclTax()),
                ],
            ];
        } else {
            $prices = [
                [
                    'price' => $order->formatPriceTxt($item->getPrice()),
                    'subtotal' => $order->formatPriceTxt($item->getRowTotal() + $item->getTaxAmount()),
                ],
            ];
        }
        return $prices;
    }
}

I've updated below line in else condition

'subtotal' => $order->formatPriceTxt($item->getRowTotal() + $item->getTaxAmount()),

Hope this will work for you!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.