Magento – Magento 2 Adding Total Due and Total Paid to PDF order

invoice-pdfmagento2orderspdftotals

Hi I am trying to add total paid and total due lines on my Magento PDF Invoice. I have added these lines in the config file in sales/etc/config.xml.

I tried in order pdf i am not getting those fields
can some one pls share knowledge about how it will work

Best Answer

After some debugging i have done this functionality by default core code pdf.xml file format the below fields having total PDF lines to display

enter image description here

and etc/pdf xml file having

    <?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/pdf_file.xsd">
    <totals>
        <total name="subtotal">
            <model>Magento\Tax\Model\Sales\Pdf\Subtotal</model>
        </total>
        <total name="shipping">
            <model>Magento\Tax\Model\Sales\Pdf\Shipping</model>
        </total>
        <total name="grand_total">
            <model>Magento\Tax\Model\Sales\Pdf\Grandtotal</model>
        </total>
        <total name="tax">
            <title translate="true">Tax</title>
            <source_field>tax_amount</source_field>
            <model>Magento\Tax\Model\Sales\Pdf\Tax</model>
            <font_size>7</font_size>
            <display_zero>false</display_zero>
            <sort_order>300</sort_order>
        </total>
    </totals>
</config>

same format i have create module in custom module added pdf.xml file

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/pdf_file.xsd">
    <totals>
        <total name="tax">
            <sort_order>650</sort_order>
        </total>
<!--        <total name="total_due">
            <title translate="true">Total Due</title>
            <source_field>total_due</source_field>
            <model>Pawan\PdfCustomiser\Model\Sales\Pdf\Totaldue</model>
            <font_size>7</font_size>
            <display_zero>false</display_zero>
            <sort_order>750</sort_order>
        </total>
        <total name="total_paid">
            <title translate="true">Total Paid</title>
            <source_field>total_paid</source_field>
            <model>Pawan\PdfCustomiser\Model\Sales\Pdf\Totalpaid</model>
            <font_size>7</font_size>
            <display_zero>false</display_zero>
            <sort_order>700</sort_order>
        </total>-->

    </totals>
</config>

next created model file and extends with \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal every pdf totals must extends with DefaultTotal class only

my model class

<?php

namespace Pawan\PdfCustomiser\Model\Sales\Pdf;

class Totaldue extends \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal
{
    /**
     * @var \Magento\Tax\Model\Config
     */
    protected $_taxConfig;

    /**
     * @param \Magento\Tax\Helper\Data $taxHelper
     * @param \Magento\Tax\Model\Calculation $taxCalculation
     * @param \Magento\Tax\Model\ResourceModel\Sales\Order\Tax\CollectionFactory $ordersFactory
     * @param \Magento\Tax\Model\Config $taxConfig
     * @param array $data
     */
    public function __construct(
        \Magento\Tax\Helper\Data $taxHelper,
        \Magento\Tax\Model\Calculation $taxCalculation,
        \Magento\Tax\Model\ResourceModel\Sales\Order\Tax\CollectionFactory $ordersFactory,
        \Magento\Tax\Model\Config $taxConfig,
        array $data = []
    ) {
        $this->_taxConfig = $taxConfig;
        parent::__construct($taxHelper, $taxCalculation, $ordersFactory, $data);
    }

    /**
     * Check if tax amount should be included to grandtotals block
     * array(
     *  $index => array(
     *      'amount'   => $amount,
     *      'label'    => $label,
     *      'font_size'=> $font_size
     *  )
     * )
     * @return array
     */
    public function getTotalsForDisplay()
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/pdftotalstotaldue.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info("sdjkhsahda". $this->getSource()->getTotalDue());
        $store = $this->getOrder()->getStore();

        if (!$this->_taxConfig->displaySalesTaxWithGrandTotal($store)) {
            return parent::getTotalsForDisplay();
        }
        $totaldue = $this->getOrder()->formatPriceTxt($this->getSource()->getTotalDue());

        $fontSize = $this->getFontSize() ? $this->getFontSize() : 7;

        $totals = [
            [
                'amount' => $this->getAmountPrefix() . $totaldue,
                'label' => __('Total Due') . ':',
                'font_size' => $fontSize,
            ],
        ];




        $totals[] = [
            'amount' => $this->getAmountPrefix() . $totaldue,
            'label' => __('Total Due') . ':',
            'font_size' => $fontSize,
        ];
        return $totals;
    }
}

This way i was done adding total due and total paid in to download pdf

Related Topic