Custom discount not showing on invoice

I have added a custom discount on order using below code and its working perfectly fine but when create invoice from admin then custom discount not showing.

File: app/code/Vendorname/Modulename/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="prepaiddiscount" instance="VendornameModulenameModelQuoteAddressTotalPrepaidDiscount" sort_order="400"/>
        </group>
    </section>
</config>

then created the model
File: app/code/Vendorname/Modulename/Model/Quote/Address/Total/PrepaidDiscount.php

<?php
namespace VendornameModulenameModelQuoteAddressTotal;


class PrepaidDiscount extends MagentoQuoteModelQuoteAddressTotalAbstractTotal
{

    public function collect(
        MagentoQuoteModelQuote $quote,
        MagentoQuoteApiDataShippingAssignmentInterface $shippingAssignment,
        MagentoQuoteModelQuoteAddressTotal $total
    )
    {
        parent::collect($quote, $shippingAssignment, $total);
        
        $items = $shippingAssignment->getItems();
        if (!count($items)) {
            return $this;
        }
        $prepaidDiscount = 10;
        if($prepaidDiscount){
            $address             = $shippingAssignment->getShipping()->getAddress();
            $label               = 'Prepaid Discount';
            $discountAmount      = $prepaidDiscount;   
            $appliedCartDiscount = 0;
            if($total->getDiscountAmount()) {
                $appliedCartDiscount = $total->getDiscountAmount();
                $discountAmount      = $total->getDiscountAmount() + $discountAmount;
                $label               = $total->getDiscountDescription().', '.$label;
            }    
            
            $total->addTotalAmount($label, -$discountAmount);
            $total->addBaseTotalAmount($label, -$discountAmount);
            
            $total->setDiscountDescription($label);
            $total->setDiscountAmount(-$discountAmount);
            $total->setBaseDiscountAmount(-$discountAmount);

            $quote->setCustomDiscount(-$discountAmount);
            $quote->setSubtotal($quote->getSubtotal() - $discountAmount);
            $quote->setGrandTotal($quote->getGrandTotal() - $discountAmount);
            $quote->setBaseSubtotal($quote->getBaseSubtotal() - $discountAmount);
            $quote->setBaseGrandTotal($quote->getBaseGrandTotal() - $discountAmount);            
        }

        return $this;           
    }
}

But discount not showing while creating the invoice, Any idea what I am doing wrong??

Modify your sales.xml as below to add custom discount in invoice and credit memo and then create respective class to set the discount amount in invoice and credit memo.

<?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="prepaiddiscount" instance="VendornameModulenameModelQuoteAddressTotalPrepaidDiscount" sort_order="400"/>
        </group>
    </section>
    <section name="order_invoice">
    <group name="totals">
        <item name="prepaiddiscount" instance="VendornameModulenameModelTotalInvoicePrepaidDiscount" sort_order="400"/>
    </group>
    </section>
    <section name="order_creditmemo">
        <group name="totals">
            <item name="prepaiddiscount" instance="VendornameModulenameModelTotalCreditmemoPrepaidDiscount" sort_order="400"/>
        </group>
    </section>
</config>

Create VendornameModulenameModelTotalInvoicePrepaidDiscount.php with following details

<?php
namespace VendornameModulenameModelTotalInvoice;

use PsrLogLoggerInterface;
use MagentoSalesModelOrderInvoice;
use MagentoSalesModelOrderInvoiceTotalDiscount;

class PrepaidDiscount extends Discount
{
    protected $logger;

    public function __construct(
        LoggerInterface $logger
    ) {
        $this->logger = $logger;
    }

    public function collect(
        Invoice $invoice
    )
    {
        parent::collect($invoice);
        $finalDiscount = $invoice->getOrder()->getDiscountAmount();
        $finalDiscountDescription = $invoice->getOrder()->getDiscountDescription();    
        $invoice->setDiscountAmount($finalDiscount);
        $invoice->setBaseDiscountAmount($finalDiscount);
        $invoice->setGrandTotal($order->getGrandTotal());
        $invoice->setBaseGrandTotal($order->getBaseGrandTotal());
        $invoice->setDiscountDescription($finalDiscountDescription);


        return $this;           
    }

}

Create VendornameModulenameModelTotalCreditmemoPrepaidDiscount.php

<?php
namespace VendornameModulenameModelTotalCreditmemo;

use PsrLogLoggerInterface;
use MagentoTaxModelConfig;
use MagentoSalesModelOrderCreditmemoTotalDiscount;
use MagentoSalesModelOrderCreditmemo;

class PrepaidDiscount extends Discount
{
    protected $logger;

    /**
     * @var Config
     */
    private $taxConfig;

    public function __construct(
        LoggerInterface $logger,
        Config $taxConfig,
        array $data = []
    ) {
        $this->taxConfig = $taxConfig;
        $this->logger = $logger;
        parent::__construct($taxConfig, $data);
    }

    public function collect(
        Creditmemo $creditmemo
    )
    {
        parent::collect($creditmemo);

        $finalDiscount = $creditmemo->getOrder()->getDiscountAmount();
        $finalDiscountDescription = $creditmemo->getOrder()->getDiscountDescription();    
        $creditmemo->setDiscountAmount($finalDiscount);
        $creditmemo->setBaseDiscountAmount($finalDiscount);
        $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $finalDiscount);
        $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $finalDiscount);
        $creditmemo->setDiscountDescription($finalDiscountDescription);


        return $this;           
    }
}

That’s All .. Cheers

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật