0

enter image description here

I am getting these errors by creating error memo programatically

Creditmemo Document Validation Error(s):

  • A creditmemo can not be created when an order has a status of closed.
  • The quantity to creditmemo must not be greater than the unrefunded quantity for product SKU "S312179".
  • The credit memo's total must be positive

Code -

public function createCreditMemoByFilteredOrder($order_id)
    {
        $mageOrder   = $this->orderFactory->create()->loadByIncrementId($order_id);
        $invoiceColl = $mageOrder->getInvoiceCollection();
        foreach ($mageOrder->getInvoiceCollection() as $invoice) {
            $invoiceId = $invoice->getId();
        }
        try {
            $invoice = $this->invoiceRepository->get($invoiceId);
        } catch (\Magento\Framework\Exception\NoSuchEntityException $ex) {
            return;
        }

        $invoiceItems       = $invoice->getItems();
        $items              = [];
        $orderId            = $invoice->getOrderId();
        $creditmemo_product = 'S312179';
        $qty_to_refund      = '1';

        foreach ($invoiceItems as $invoiceItem) {
            $productId  = $invoiceItem->getProductId();
            $invoiceSku = $invoiceItem->getSku();
            /**
             * If product id match then create Credit memo for that order
             */
            if ($productId === $creditmemo_product) {
                $creditmemoItemCreation = $this->CreditmemoItemCreationFactory->create();
                $items[]                = $creditmemoItemCreation->setQty($qty_to_refund)
                    ->setOrderItemId($invoiceItem->getOrderItemId());
            }
            /**
             * Create Credit Memo
             */
            if ($this->refundOrder->execute($orderId, $items, true, false)) {
                $result[0] = true;
                $result[1] = $order_id;
                $result[2] = $row;
            } else {
                $result[0] = false;
                $result[1] = 'Could not create credit memo for Order ID "' . $order_id . '" for row no: ' . $csvRowCounter;
            }
        }
    }

Any thoughts, what is missing ?

6
  • any thoughts ???
    – devhs
    Mar 26, 2021 at 12:33
  • Any thoughts, what is missing ?
    – devhs
    Mar 26, 2021 at 12:40
  • Check the Order Status for which you are try to create the credit Memo. Mar 26, 2021 at 13:30
  • Thanks for the comment, It was processing, after running the above code it was set to "closed"
    – devhs
    Mar 30, 2021 at 4:39
  • so for closed magnto not able to generate credit memo. Mar 30, 2021 at 9:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.