Skip to main content

Magento 2 Adding Total Due and Total Paid to PDF order

Hi I am trying to add total paid and total due lines on my Magento PDF Invoice.

Firstly you have to create pdf.xml at path :

app\code\Pawan\PdfCustomiser\etc


  <?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="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>

Here, source_field: is the field name which you created as a field in order.
model: is the file in model in which you code to add field in totals.
font_size: default font_size is 7 you can change it.
display_zero: If you don’t want to display field if amount is 0 then set it to false, otherwise set it to true.
sort_order: sort order for the position of your field.

Now create file at path:  app\code\Pawan\PdfCustomiser\Model\Sales\Pdf\Totalpaid.php

<?php

namespace Pawan\PdfCustomiser\Model\Sales\Pdf;

class Totalpaid 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()->getTotalPaid());
        $store = $this->getOrder()->getStore();
        
        if (!$this->_taxConfig->displaySalesTaxWithGrandTotal($store)) {
            return parent::getTotalsForDisplay();
        }
        $totalpaid = $this->getOrder()->formatPriceTxt($this->getSource()->getTotalPaid());
        
        $fontSize = $this->getFontSize() ? $this->getFontSize() : 7;

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

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

same way new file Totaldue.php and small changes in place of $this->getSource()->getTotalPaid() replace with $this->getSource()->getTotalDue()

Next clear flush cache
after this your fields will be added in downloaded pdf of an order.

Comments

  1. Thanks for sharing informative knowledge about total due total paid PDF. Some other platform switch e-commerce site to Magento because of easily available Magento code.

    ReplyDelete
  2. Thanks for sharing this news here. This blog and solution you provided in this blog is really helpful.

    ReplyDelete

Post a Comment

Popular posts from this blog

magento 1 extension converted in to magento 2 process

Simple Install =================================== Download as a zip below link. unzip and upload it to your public_html or other rootdoc directory. https://github.com/magento/code-migration. login to your server via shell switch to the code-migration-develop directory by running following command   cd code-migration-develop Run command following command in root directory of this toolkit composer instal Before running the migration, the following directories need to be prepared: ===================================== * `<src>` - Directory that contains custom Magento 1.x code that is intended to be migrated. The code must follow the Magento 1.x directory structure.   Magento1.x core files must not be included. * `<dst>` - Empty directory for the toolkit to put the generated Magento 2 code to * `<m1>` - Directory that contains: * Vanilla Magento 1.x codebase, and * Custom Magento 1.x code same as...

Magento 2 product collection Filtering multi-select attribute values

  If you have multi-select attribute of product like below If you want filter value for this option Use below syntax to get product data: ->addAttributeToFilter('store_model', array('finset' => $params['store_model'])) finset key is used for multiselect attribute filter. $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $products = $objectManager->get('Magento\Catalog\Model\Product')         ->getCollection()         ->addAttributeToSelect('*')         ->addAttributeToSelect('store_brand')         ->addAttributeToSelect('store_model')         ->addAttributeToSelect('store_year')         ->addAttributeToFilter('store_brand', array('finset' => $params['store_brand']))         ->addAttributeToFilter('store_model', array('finset' => $params['store_model']))         ->ad...

Magento Debug HEADERS ALREADY SENT error

When you  receiving the following error in  system.log file: app\code\core\Mage\Core\Controller\Response\Http.php:44  [1] \www.pawan.com\lib\Zend\Controller\Response\Abstract.php:727  [2]  \www.pawan.com\app\code\core\Mage\Core\Controller\Response\Http.php:75  [3]  \www.pawan.com\app\code\core\Mage\Core\Controller\Varien\Front.php:188  [4]  \www.pawan.com\app\code\core\Mage\Core\Model\App.php:304  [5]  \www.pawan.com\app\Mage.php:596  [6]  \www.pawan.com\index.php:81 if you have no idea what file is causing this. Here is the steps to debug. That error is thrown from Mage_Core_Controller_Response_Http -> sendHeaders(). This function calls the super class function that actually does the check to see whether or not headers have already been sent, Zend_Controller_Response_Abstract -> canSendHeaders(). The Zend_Controller_Response_Abstract class handles, among other things, sending response header...