4

magento 2 extra fee not adding to grandtotal in invoice

block is calling but model is not calling

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>        
    <referenceContainer name="invoice_totals">
        <block class="Custom\LoyaltyProgram\Block\Adminhtml\Sales\Order\Invoice\Fee" name="fee"/>
    </referenceContainer>
</body>

Block

<?php
 namespace Custom\LoyaltyProgram\Block\Adminhtml\Sales\Order\Invoice;

class Fee extends \Magento\Framework\View\Element\Template
{
protected $_config;
protected $_order;
protected $_source;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Tax\Model\Config $taxConfig,
    \Custom\LoyaltyProgram\Helper\Data $helper,
    \Custom\LoyaltyProgram\Model\Extrafee $extraFeeModel,
    array $data = []
) {
    $this->_config = $taxConfig;
    $this->_helper = $helper;
    $this->extraFeeModel = $extraFeeModel;
    parent::__construct($context, $data);
}

public function displayFullSummary()
{
    return true;
}

public function getSource()
{
    return $this->_source;
} 
public function getStore()
{
    return $this->_order->getStore();
}
public function getOrder()
{
    return $this->_order;
}
public function getLabelProperties()
{
    return $this->getParentBlock()->getLabelProperties();
}

public function getValueProperties()
{
    return $this->getParentBlock()->getValueProperties();
}
public function getInvoice()
{
    return $this->getParentBlock()->getInvoice();
}
 public function initTotals()
{
    $this->getParentBlock();
    $this->getInvoice();
    $this->getSource();

    $parent = $this->getParentBlock();
    $this->_order = $parent->getOrder();
    $this->_source = $parent->getSource();

    $orderId = $this->_order->getId();
    $store = $this->getStore();

    $collection = $this->extraFeeModel->getCollection();
    $collection->addFieldToFilter('order_id', $orderId);

    if($collection->getSize()){

        $extraFee = $collection->getFirstItem()->getFee();
        $extraFee = -1*$extraFee;

        $fee = new \Magento\Framework\DataObject(
                [
                    'code' => 'fee',
                    'strong' => false,
                    'value' => $extraFee,
                    'label' => $this->_helper->getTitle()
                ]
            );
        $parent->addTotal($fee, 'fee');
    }

    $this->getInvoice()->setGrandTotal($this->getInvoice()->getGrandTotal() - $extraFee); // "2" extra fee
    $logger->info('2getGrandTotalblock'.$this->getInvoice()->getGrandTotal());

    $this->getParentBlock()->addTotalBefore($fee, 'grand_total');
    return $this;

}}

Model

<?php

namespace Ewall\LoyaltyProgram\Model\Invoice\Total;

use Magento\Sales\Model\Order\Invoice\Total\AbstractTotal;

class Fee extends AbstractTotal
{
/**
 * @param \Magento\Sales\Model\Order\Invoice $invoice
 * @return $this
 */
public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
{

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/fee.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('model calling:');

    $invoice->setFee(0);

    $amount = $invoice->getOrder()->getFee();
    $invoice->setFee($amount);


    $invoice->setGrandTotal($invoice->getGrandTotal() - 2); // 2 extra fee
    $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - 2);  // 2 extra fee

    return $this;
}}
1
  • please chanfe <referenceContainer name="invoice_totals"> to <referenceBlock name="invoice_totals"> – Charvi Parikh Jan 22 '19 at 8:43
3

I forgot to add etc/sales.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd">
<section name="quote">
    <group name="totals">
        <item name="fee" instance="Custom\LoyaltyProgram\Model\Total\Fee" sort_order="150"/>
    </group>  
</section>
 <section name="order_invoice">
    <group name="totals">
        <item name="fee" instance="Custom\LoyaltyProgram\Model\Invoice\Total\Fee" sort_order="160"/>
    </group>
</section>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.