質問

I have created mass action in order grid to create bulk invoice. And after successfully creating invoice on multiple orders I am facing issue with shipping charges.

i.e, for first order it is adding shipping amount in invoice and for other orders it is excluding shipping amount.

Here is my code for mass invoice:

foreach ($collection->getItems() as $order) {
    if (!$order->getEntityId()) {
        continue;
    }
    $loadedOrder = $model->load($order->getEntityId());

    if ($loadedOrder->canInvoice()) {
        $invoice = $this->_invoiceService->prepareInvoice($loadedOrder);
        if ($loadedOrder->getPayment()->getMethod() != "cashondelivery") {
            $invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_OFFLINE);
        }
        $invoice->register();
        $invoice->save();
        $transactionSave = $this->_transaction->addObject($invoice)->addObject($invoice->getOrder());
        $transactionSave->save();

//send notification code
        $loadedOrder->addStatusHistoryComment(
            __('Notified customer about invoice #%1. ' . $appendusername, $invoice->getId())
        )->setIsCustomerNotified(false)->save();

        $countInvoiceOrder++;
    } else {
        if (empty($NonInvoiceOrdernuumbers)) {
            $NonInvoiceOrdernuumbers = $NonInvoiceOrdernuumbers . $loadedOrder->getIncrementId();
        } else {
            $NonInvoiceOrdernuumbers = $NonInvoiceOrdernuumbers . ", " . $loadedOrder->getIncrementId();
        }
    }
}
役に立ちましたか?

解決

Replace your foreach loop with this

    foreach ($collection->getItems() as $order) {
        if (!$order->getEntityId()) {
            continue;
        }
        $loadedOrder = $model->load($order->getEntityId());

        if($loadedOrder->canInvoice()) {

        // Create invoice for this order
        $invoice = $this->_objectManager->create('Magento\Sales\Model\Service\InvoiceService')->prepareInvoice($loadedOrder);
        //$invoice->getOrder()->setIsInProcess(true);
        $invoice->setShippingAmount($loadedOrder->getShippingAmount());
        $invoice->setBaseShippingAmount($loadedOrder->getBaseShippingAmount());
        $invoice->setTaxAmount($loadedOrder->getTaxAmount());
        $invoice->setBaseTaxAmount($loadedOrder->getBaseTaxAmount());
        $invoice->setSubtotal($loadedOrder->getSubtotal());
        $invoice->setBaseSubtotal($loadedOrder->getBaseSubtotal());
        $invoice->setGrandTotal($loadedOrder->getGrandTotal());
        $invoice->setBaseGrandTotal($loadedOrder->getBaseGrandTotal());

        // Register as invoice item
        $invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_OFFLINE);
        $invoice->register();
        // Save the invoice to the order
        $transaction = $this->_objectManager->create('Magento\Framework\DB\Transaction')
        ->addObject($invoice)
        ->addObject($invoice->getOrder());
        $transaction->save();

        //send notification code
        $loadedOrder->addStatusHistoryComment(
            __('Notified customer about invoice #%1. '.$appendusername, $invoice->getId())
        )->setIsCustomerNotified(false)->save();

        $countInvoiceOrder++;
        }
        else {
            if (empty($NonInvoiceOrdernuumbers)){
            $NonInvoiceOrdernuumbers = $NonInvoiceOrdernuumbers.$loadedOrder->getIncrementId();
            }
            else{
            $NonInvoiceOrdernuumbers = $NonInvoiceOrdernuumbers.", ".$loadedOrder->getIncrementId();    
            }
        }
    }

In above code the only difference is, I assign shipping amount, tax amount, subtotal and grandtotal during invoice creation

I hope this will help

ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top