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