1

I am canceling credit memo using below code:

foreach ($collection as $creditmemo) {
    if($creditmemo->getState() != 3){
            $order = $creditmemo->getOrder();
            $creditmemo->setState(\Magento\Sales\Model\Order\Creditmemo::STATE_CANCELED);
            foreach ($creditmemo->getAllItems() as $item) {
                $item->cancel();
                $item->save();        
            }
            $creditmemo->save();
    }
}

The above code gives the following error:

We found an invalid quantity to refund item

The cancel() method checks the quantity available for a given item using isQtyAvailable() method which is returning 0.

I also tried the following solution:

foreach ($creditmemo->getAllItems() as $item) {
    // $getQtyReturned = $item->getOrderItem()->getQtyRefunded();
    // $creditmemoQty = $item->getQty();
    // $item->getOrderItem()->setQtyRefunded($getQtyReturned - $creditmemoQty);
    $qty = $item->getQty();
    $item->getOrderItem()->setQtyRefunded($item->getOrderItem()->getQtyRefunded() - $qty);
    $item->getOrderItem()->setTaxRefunded(
        $item->getOrderItem()->getTaxRefunded() -
        $item->getOrderItem()->getBaseTaxAmount() *
        $qty /
        $item->getOrderItem()->getQtyOrdered()
    );
    $item->getOrderItem()->setDiscountTaxCompensationRefunded(
        $item->getOrderItem()->getDiscountTaxCompensationRefunded() -
        $item->getOrderItem()->getDiscountTaxCompensationAmount() *
        $qty /
        $item->getOrderItem()->getQtyOrdered()
    );
    $item->save();        
}

The above solution works, but during the creation of a credit memo for the same order again, it gives the subtotal of credit memo as 0.

0

Your Answer

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

Browse other questions tagged or ask your own question.