定制折扣未显示在发票上

问题描述 投票:0回答:1

我使用下面的代码在订单上添加了自定义折扣,它工作得很好,但是当从管理员创建发票时,自定义折扣没有显示。

文件:应用程序/代码/供应商名称/模块名称/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="Vendorname\Modulename\Model\Quote\Address\Total\PrepaidDiscount" sort_order="400"/>
        </group>
    </section>
</config>

然后创建模型 文件:应用程序/代码/供应商名称/模块名称/型号/报价/地址/总计/PrepaidDiscount.php

<?php
namespace Vendorname\Modulename\Model\Quote\Address\Total;


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

    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);
        
        $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;           
    }
}

但是创建发票时没有显示折扣,知道我做错了什么吗?

magento2 invoice discount
1个回答
0
投票

如下修改 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="Vendorname\Modulename\Model\Quote\Address\Total\PrepaidDiscount" sort_order="400"/>
        </group>
    </section>
    <section name="order_invoice">
    <group name="totals">
        <item name="prepaiddiscount" instance="Vendorname\Modulename\Model\Total\Invoice\PrepaidDiscount" sort_order="400"/>
    </group>
    </section>
    <section name="order_creditmemo">
        <group name="totals">
            <item name="prepaiddiscount" instance="Vendorname\Modulename\Model\Total\Creditmemo\PrepaidDiscount" sort_order="400"/>
        </group>
    </section>
</config>

使用以下详细信息创建 Vendorname\Modulename\Model\Total\Invoice\PrepaidDiscount.php

<?php
namespace Vendorname\Modulename\Model\Total\Invoice;

use Psr\Log\LoggerInterface;
use Magento\Sales\Model\Order\Invoice;
use Magento\Sales\Model\Order\Invoice\Total\Discount;

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($invoice->getGrandTotal() + $finalDiscount);
        $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $finalDiscount);
        $invoice->setDiscountDescription($finalDiscountDescription);


        return $this;           
    }

}

创建供应商名称\模块名称\模型\总计\Creditmemo\PrepaidDiscount.php

<?php
namespace Vendorname\Modulename\Model\Total\Creditmemo;

use Psr\Log\LoggerInterface;
use Magento\Tax\Model\Config;
use Magento\Sales\Model\Order\Creditmemo\Total\Discount;
use Magento\Sales\Model\Order\Creditmemo;

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;           
    }
}

仅此而已..干杯

© www.soinside.com 2019 - 2024. All rights reserved.