1

I'm trying to add a subtotal per vat rate to our PDF invoices. I've followed numerous sets of instructions (which are broadly similar) such as these:

In my class for 7% VAT which extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal, in the collect() function, I have something like this:

public function collect(
    \Magento\Quote\Model\Quote $quote,
    \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
    \Magento\Quote\Model\Quote\Address\Total $total
)
{
    parent::collect($quote, $shippingAssignment, $total);

    $items = $shippingAssignment->getItems();
    if (!count($items)) {
        return $this;
    }

    $subTotal = $this->determineSubtotalValue($quote);
    $subTotal =  $this->_priceCurrency->convert($subTotal);

    $total->setBaseTotalAmount('subtotal7pc', $subTotal);

    $total->setSubtotal7pc($subTotal);
    $total->setBaseSubtotal7pc($subTotal);

    return $this;
}

I have an equivalent class for the 19% VAT subtotal.

This does work correctly on the admin order placement screen, see screenshot: Admin Order Entry

However, if I then try invoice this order and try to read the total back into the PDF invoice, I always get a value of 0.00 €

My code for this is as follows:

pdf.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/pdf_file.xsd">
    <renderers>
        <page type="invoice">
            <renderer product_type="default">Magento\Sales\Model\Order\Pdf\Items\Invoice\DefaultInvoice</renderer>
        </page>
    </renderers>
    <totals>
        <total name="subtotal7pc">
            <title translate="true">Subtotal (7% Tax)</title>
            <source_field>subtotal7pc</source_field>
            <model>MyCompany\InvoiceTotals\Model\Order\Pdf\Total\SubTotalByTaxRate</model>
            <font_size>7</font_size>
            <display_zero>true</display_zero>
            <sort_order>30</sort_order>
        </total>
    </totals>
</config>

MyCompany\InvoiceTotals\Model\Order\Pdf\Total\SubTotalByTaxRate.php

class SubTotalByTaxRate extends \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal
{
    /**
     * @var \Magento\Tax\Helper\Data
     */
    protected $_taxHelper;

    /**
     * @var \Magento\Tax\Model\Calculation
     */
    protected $_taxCalculation;

    /**
     * @var \Magento\Tax\Model\ResourceModel\Sales\Order\Tax\CollectionFactory
     */
    protected $_taxOrdersFactory;

    public function __construct(
        \Magento\Tax\Helper\Data $taxHelper,
        \Magento\Tax\Model\Calculation $taxCalculation,
        \Magento\Tax\Model\ResourceModel\Sales\Order\Tax\CollectionFactory $ordersFactory,
        array $data = []
    ) {
        $this->_taxHelper = $taxHelper;
        $this->_taxCalculation = $taxCalculation;
        $this->_taxOrdersFactory = $ordersFactory;
        parent::__construct($taxHelper,$taxCalculation,$ordersFactory,$data);
    }

    public function getTotalsForDisplay()
    {
        $amount = $this->getOrder()->formatPriceTxt($this->getAmount());
        $title = __($this->getTitle());
        $fontSize = $this->getFontSize() ? $this->getFontSize() : 7;
        $total = ['amount' => $amount, 'label' => $title, 'font_size' => $fontSize];
        return [$total];
    }

    public function getAmount()
    {
        return $this->getOrder()->getDataUsingMethod($this->getSourceField());
    }
}

Here is a screenshot of the PDF invoice:

Invoice

So, I think I'm nearly there, but missing something. I've tried adding the following line to my collect() function:

$total->setTotalAmount('subtotal7pc', $subTotal);

Order with extra function line Invoice with extra function line

But that causes incorrect display prior to submitting the order and then has no effect on the PDF invoice.

I hope someone can help? Thank you.

  • Solved. I wrongly assumed that Magento would automatically save the quote total values, but it doesn't. You need an observer on the sales_order_place_after event to record the value so you can pull it out again in the PDF invoice. – Luke Cousins Nov 29 '17 at 13:11
  • I am trying to display fee in invoice pdf in vain. I tried to add pdf.xml and its model as you mentioned but got error 'The PDF total model Custom\PaymentFee\Model\Order\Pdf\Total\Fee must be or extend \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal' – Jsparo30 Feb 15 '18 at 12:01

Your Answer

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

Browse other questions tagged or ask your own question.