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.
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.
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.
ReplyDeleteThanks for sharing this news here. This blog and solution you provided in this blog is really helpful.
ReplyDelete