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.
vendor/magento/module-sales/Controller/Adminhtml/Order/Creditmemo/Save.php