5

I am trying to add partial credit memo programatically but it is consuming whole quantity in first credit memo, How can I pass specific items to credit memo?

public function createCreditMemo ($entityId,$invId,$shipping,$subtotal,$grandtotal,$tax) {

        $post_data = json_decode(file_get_contents('php://input'), true);
    $itemsArray = array();

        foreach($post_data['items'] as $index => $details){ 
                 $quantity = $details['qty'];           
         $order_item_id = $details['order_item_id'];
                 $itemsArray[$order_item_id]=$quantity; 
            }

        $orderId = $entityId; 
        $order = $this->_orderRepository->get($orderId);    
        $shippingAmount = $shipping;
        $subTotal = $subtotal;
        $taxAmount = $tax;
        $grandTotal = $grandtotal;
        $invoiceId = $invId;

        $invoiceobj =  $this->invoice->load($invId);
        $creditmemo = $this->creditmemoFactory->createByInvoice($invoiceobj,$itemsArray);

        $creditmemo->setShippingAmount($shippingAmount);
        $creditmemo->setTaxAmount($taxAmount);
        $creditmemo->setBaseTaxAmount($taxAmount);
        $creditmemo->setSubtotal($subTotal);
        $creditmemo->setBaseSubtotal($subTotal);
        $creditmemo->setGrandTotal($grandTotal);
        $creditmemo->setBaseGrandTotal($grandTotal);

        $this->creditmemoService->refund($creditmemo);

       return $creditmemo->getId();




    }
}

I am passing item array still it is taking all the items of invoice.

  • You can check the native adminhtml flow here : vendor/magento/module-sales/Controller/Adminhtml/Order/Creditmemo/Save.php – Franck Garnier Nov 21 '18 at 8:59
0

replace of your code line from :

$creditmemo = $this->creditmemoFactory->createByInvoice($invoiceobj,$itemsArray);

to

$creditmemo = $this->creditmemoFactory->createByOrder($order,$itemsArray);
  • Now it is taking full ordered quantity. – akshay billore Nov 13 '18 at 15:24
  • do you verify that, you get proper data into $itemsArray? – Dhiren Vasoya Nov 14 '18 at 3:50
  • Yes, I am sending only one item I have verified that. Is there any specific form of array required? – akshay billore Nov 14 '18 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.