Magento 2.3.4 – How to Override Class Magento\Sales\Model\Order\Pdf\Total\DefaultTotal

invoice-pdfmagento2.3.4override-modeltax

I am trying to override getFullTaxInfo() of class Magento\Sales\Model\Order\Pdf\Total\DefaultTotal using preference but this is not working.

Can anyone help to do it?

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="magento:framework:ObjectManager/etc/config.xsd">
        <preference for="Magento\Sales\Model\Order\Pdf\Total\DefaultTotal" type="Vendor\Module_name\Model\Rewrite\Sales\Order\Pdf\Total\DefaultTotal" />
</config>

Model File:
Vendor/Module_name/Model/Rewrite/Sales/Order/Pdf/Total/DefaultTotal.php

<?php

namespace Vendor\Module_name\Model\Rewrite\Sales\Order\Pdf\Total;


class DefaultTotal extends \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal
{
    public function getFullTaxInfo()
    {
        $fontSize = $this->getFontSize() ? $this->getFontSize() : 7;
        $taxClassAmount = $this->_taxHelper->getCalculatedTaxes($this->getSource());
        if (!empty($taxClassAmount)) {
            foreach ($taxClassAmount as &$tax) {
                $percent = $tax['percent'] ? ' (' . ($tax['percent']-5) . '%)' : '';
                $amount     = ($tax['tax_amount'] / 35) * 30;
                $tax['amount'] = $this->getAmountPrefix() . $this->getOrder()->formatPriceTxt($amount);
                $tax['label'] = __('Sales Taxes') . $percent . ':';
                $tax['font_size'] = $fontSize;
            }
        } else {
            /** @var $orders \Magento\Tax\Model\ResourceModel\Sales\Order\Tax\Collection */
            $orders = $this->_taxOrdersFactory->create();
            $rates = $orders->loadByOrder($this->getOrder())->toArray();
            $fullInfo = $this->_taxCalculation->reproduceProcess($rates['items']);
            $tax_info = [];

            if ($fullInfo) {
                foreach ($fullInfo as $info) {
                    if (isset($info['hidden']) && $info['hidden']) {
                        continue;
                    }

                    $_amount = $info['amount'];

                    foreach ($info['rates'] as $rate) {
                        $percent = $rate['percent'] ? ' (' . $rate['percent'] . '%)' : '';

                        $tax_info[] = [
                            'amount' => $this->getAmountPrefix() . $this->getOrder()->formatPriceTxt($_amount),
                            'label' => __($rate['title']) . $percent . ':',
                            'font_size' => $fontSize,
                        ];
                    }
                }
            }
            $taxClassAmount = $tax_info;
        }

        return $taxClassAmount;
    }
}

    

Best Answer

I have also tried with preference but it did not work so have done using the plugin. Please refer below files

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

<?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\Total\DefaultTotal">
        <plugin name="plugin_name" type="Vendor\Module\Plugin\Model\Order\Pdf\Total\DefaultTotal" sortOrder="10" />
    </type>
</config>

app/code/Vendor/Module/Plugin/Model/Order/Pdf/Total/DefaultTotal.php

<?php
    
namespace Vendor\Module\Plugin\Model\Order\Pdf\Total;

class DefaultTotal
{
    protected $_storeManager;

    public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
    {
        $this->_storeManager = $storeManager;
    }

    public function afterGetFullTaxInfo(
        \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal $subject,
        $result
    ) {    
        foreach ($result as &$tax) {
                $percent = $tax['percent'] ? ' (' . (float)$tax['percent'] . '%)' : '';
                $tax['label'] = __($tax['title']) . $percent . ':';
                
            }
        return $result; 
    }
}
Related Topic