47 \
Magento\Sales\Model\Convert\OrderFactory $convertOrderFactory,
51 $this->convertor = $convertOrderFactory->create();
69 $qtyList = isset(
$data[
'qtys']) ?
$data[
'qtys'] : [];
77 $qty = $this->getQtyToRefund(
$orderItem, $qtyList);
101 $qtyList = isset(
$data[
'qtys']) ?
$data[
'qtys'] : [];
105 $invoiceRefundLimitsQtyList = $this->getInvoiceRefundLimitsQtyList(
$invoice);
116 $this->getQtyToRefund(
$orderItem, $qtyList, $invoiceRefundLimitsQtyList),
127 if (!isset(
$data[
'shipping_amount'])) {
128 $baseAllowedAmount = $this->getShippingAmount(
$invoice);
129 $creditmemo->setBaseShippingAmount($baseAllowedAmount);
147 if (
$item->isDummy()) {
148 if (
$item->getHasChildren()) {
149 foreach (
$item->getChildrenItems() as $child) {
155 if (isset($qtys[$child->getId()]) && $qtys[$child->getId()] > 0) {
162 $parent =
$item->getParentItem();
166 return isset($qtys[$parent->getId()]) && $qtys[$parent->getId()] > 0;
183 if (
$item->getQtyToRefund() < 0) {
186 if (isset($invoiceQtysRefundLimits[
$item->getId()])) {
187 return $invoiceQtysRefundLimits[
$item->getId()] > 0;
201 if (isset(
$data[
'shipping_amount'])) {
204 if (isset(
$data[
'adjustment_positive'])) {
207 if (isset(
$data[
'adjustment_negative'])) {
217 private function calculateProductOptions(
Item $orderItem,
int $parentQty): int
222 $bundleSelectionAttributes = $this->serializer->unserialize(
225 if ($bundleSelectionAttributes) {
226 $qty = $bundleSelectionAttributes[
'qty'] * $parentQty;
238 private function getInvoiceRefundedQtyList(Invoice
$invoice): array
240 $invoiceRefundedQtyList = [];
245 foreach (
$creditmemo->getAllItems() as $creditmemoItem) {
246 $orderItemId = $creditmemoItem->getOrderItem()->getId();
247 if (isset($invoiceRefundedQtyList[$orderItemId])) {
248 $invoiceRefundedQtyList[$orderItemId] += $creditmemoItem->getQty();
250 $invoiceRefundedQtyList[$orderItemId] = $creditmemoItem->getQty();
256 return $invoiceRefundedQtyList;
265 private function getInvoiceRefundLimitsQtyList(Invoice
$invoice): array
267 $invoiceRefundLimitsQtyList = [];
268 $invoiceRefundedQtyList = $this->getInvoiceRefundedQtyList(
$invoice);
273 if (isset($invoiceRefundedQtyList[$orderItemId])) {
274 $qtyCanBeRefunded = $qtyCanBeRefunded - $invoiceRefundedQtyList[$orderItemId];
276 $invoiceRefundLimitsQtyList[$orderItemId] = $qtyCanBeRefunded;
279 return $invoiceRefundLimitsQtyList;
290 private function getQtyToRefund(Item
$orderItem, array $qtyList, array $refundLimits = []): float
294 if (isset($qtyList[
$orderItem->getParentItemId()])) {
295 $parentQty = $qtyList[
$orderItem->getParentItemId()];
301 $qty = $this->calculateProductOptions(
$orderItem, $parentQty);
305 }
elseif (!count($qtyList)) {
311 if (isset($refundLimits[
$orderItem->getId()])) {
312 $qty = min($qty, $refundLimits[
$orderItem->getId()]);
325 private function getShippingAmount(Invoice
$invoice): float
328 $isShippingInclTax = $this->taxConfig->displaySalesShippingInclTax(
$order->getStoreId());
329 if ($isShippingInclTax) {
331 $order->getBaseShippingRefunded() -
332 $order->getBaseShippingTaxRefunded();
createByOrder(\Magento\Sales\Model\Order $order, array $data=[])
elseif(isset( $params[ 'redirect_parent']))
initData($creditmemo, $data)
canRefundItem($item, $qtys=[], $invoiceQtysRefundLimits=[])
canRefundNoDummyItem($item, $invoiceQtysRefundLimits=[])
__construct(\Magento\Sales\Model\Convert\OrderFactory $convertOrderFactory, \Magento\Tax\Model\Config $taxConfig, \Magento\Framework\Serialize\Serializer\Json $serializer=null)