Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento 2 add extra fee into admin invoice

Tags:

php

magento2

I have created one module for add extra fee into order. In front when i was place the order, it was successfully added extra fee in front side. but in Admin side did not add any extra fee. Please help me for where i am doing mistake to add extra fee into admin invoice ? . I have create collect method in Model.. code is given below.

namespace Mageniks\Alltest\Model\Total;

class Fee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{

    protected $quoteValidator = null; 
     protected $_checkoutSession;
    public function __construct(\Magento\Quote\Model\QuoteValidator $quoteValidator, \Magento\Checkout\Model\Session $checkoutSession)
    {
        $this->quoteValidator = $quoteValidator;
         $this->_checkoutSession = $checkoutSession;
    }
  public function collect(
        \Magento\Quote\Model\Quote $quote,
        \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
        \Magento\Quote\Model\Quote\Address\Total $total
    ) {
        parent::collect($quote, $shippingAssignment, $total);


        $exist_amount = 0; //$quote->getFee(); 
        $giftprice = $this->_checkoutSession->getGiftwrapprice();
        $fee = $giftprice; 
        $balance = $fee - $exist_amount;

        $total->setTotalAmount('fee', $balance);
        $total->setBaseTotalAmount('fee', $balance);

        $total->setFee($balance);
        $total->setBaseFee($balance);

        $total->setGrandTotal($total->getGrandTotal() + $balance);
        $total->setBaseGrandTotal($total->getBaseGrandTotal() + $balance);


        return $this;
    } 

    protected function clearValues(Address\Total $total)
    {
        $total->setTotalAmount('subtotal', 0);
        $total->setBaseTotalAmount('subtotal', 0);
        $total->setTotalAmount('tax', 0);
        $total->setBaseTotalAmount('tax', 0);
        $total->setTotalAmount('discount_tax_compensation', 0);
        $total->setBaseTotalAmount('discount_tax_compensation', 0);
        $total->setTotalAmount('shipping_discount_tax_compensation', 0);
        $total->setBaseTotalAmount('shipping_discount_tax_compensation', 0);
        $total->setSubtotalInclTax(0);
        $total->setBaseSubtotalInclTax(0);
    }

    public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
    {
        $giftprice = $this->_checkoutSession->getGiftwrapprice();
        return [
            'code' => 'fee',
            'title' => 'Giftwrap',
            'value' => $giftprice
        ];
    }

    public function getLabel()
    {
        return __('Giftwrap');
    }
}

I was also follow this link : https://magento.stackexchange.com/questions/92774/how-to-add-fee-to-order-totals-in-magento2 but it can not add extra fee into admin invoice.

Any HELP would be appreciated. Thanks

like image 458
Niks Avatar asked Feb 06 '23 09:02

Niks


1 Answers

For add extra fee into admin invoice please follow the following steps :

Create Xml file at app/code/Namespace/Modulename/view/adminhtml/layout/sales_order_invoice_new.xml

    <?xml version="1.0"?>
    <!--
    /**
     * Copyright © 2015 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */
    -->
    <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="Namespace\Module\Block\Adminhtml\Sales\Order\Invoice\Extrafee" name="extrafee"/>
            </referenceContainer>
        </body>
    </page>

Create File at app/code/Namespace/Module/Block/Adminhtml/Sales/Order/Invoice/Extrafee.php

    <?php
    /**
     * Copyright © 2015 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */

    /**
     * Tax totals modification block. Can be used just as subblock of \Magento\Sales\Block\Order\Totals
     */
    namespace Namespace\Module\Block\Adminhtml\Sales\Order\Invoice;



    class Extrafee 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,
            array $data = []
        ) {
            $this->_config = $taxConfig;
            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 initTotals()
        {
            $parent = $this->getParentBlock();
            $this->_order = $parent->getOrder();
            $this->_source = $parent->getSource();

            $store = $this->getStore();

                $fee = new \Magento\Framework\DataObject(
                        [
                            'code' => 'fee',
                            'strong' => false,
                            'value' => $this->_order->getFee(),
                            'base_value' => $this->_order->getFee(),
                            'label' => __('Fee'),
                        ]
                    );
                    $parent->addTotal($fee, 'fee');
                    return $this;

        }

    }

I hope it will help you :)

like image 112
Emipro Technologies Pvt. Ltd. Avatar answered Feb 08 '23 03:02

Emipro Technologies Pvt. Ltd.