@@ -74,34 +74,13 @@ public function validate($entity)
74
74
75
75
$ totalQuantity = 0 ;
76
76
foreach ($ entity ->getItems () as $ item ) {
77
- if (!isset ($ orderItemsById [$ item ->getOrderItemId ()])) {
78
- $ messages [] = __ (
79
- 'The creditmemo contains product SKU "%1" that is not part of the original order. ' ,
80
- $ item ->getSku ()
81
- );
82
- continue ;
83
- }
84
- $ orderItem = $ orderItemsById [$ item ->getOrderItemId ()];
85
-
86
- if ($ this ->isValidRefundQty ($ orderItem ->getIsQtyDecimal (), $ item ->getQty ())) {
87
- $ messages [] =__ (
88
- 'You cannot use decimal quantity to refund item "%1". ' ,
89
- $ orderItem ->getSku ()
90
- );
91
- continue ;
92
- }
93
-
94
- if (!$ this ->canRefundItem ($ orderItem , $ item ->getQty (), $ invoiceQtysRefundLimits ) ||
95
- !$ this ->isQtyAvailable ($ orderItem , $ item ->getQty ())
96
- ) {
97
- $ messages [] =__ (
98
- 'The quantity to creditmemo must not be greater than the unrefunded quantity '
99
- . ' for product SKU "%1". ' ,
100
- $ orderItem ->getSku ()
101
- );
102
- } else {
103
- $ totalQuantity += $ item ->getQty ();
104
- }
77
+ $ this ->calculateTotalQuantity (
78
+ $ orderItemsById ,
79
+ $ messages ,
80
+ $ totalQuantity ,
81
+ $ item ,
82
+ $ invoiceQtysRefundLimits
83
+ );
105
84
}
106
85
107
86
if ($ entity ->getGrandTotal () <= 0 ) {
@@ -128,6 +107,50 @@ private function isValidRefundQty($isQtyDecimal, float $itemQty): bool
128
107
return false ;
129
108
}
130
109
110
+ /**
111
+ * Calculate total quantity.
112
+ *
113
+ * @param array $orderItemsById
114
+ * @param array &$messages
115
+ * @param int &$totalQuantity
116
+ * @param Magento\Sales\Api\Data\CreditmemoItemInterface|mixed $item
117
+ * @param array $invoiceQtysRefundLimits
118
+ * @return void
119
+ */
120
+ private function calculateTotalQuantity (
121
+ array $ orderItemsById ,
122
+ array &$ messages ,
123
+ int &$ totalQuantity ,
124
+ $ item ,
125
+ array $ invoiceQtysRefundLimits
126
+ ) {
127
+ if (!isset ($ orderItemsById [$ item ->getOrderItemId ()])) {
128
+ $ messages [] = __ (
129
+ 'The creditmemo contains product SKU "%1" that is not part of the original order. ' ,
130
+ $ item ->getSku ()
131
+ );
132
+ } else {
133
+ $ orderItem = $ orderItemsById [$ item ->getOrderItemId ()];
134
+
135
+ if ($ this ->isValidRefundQty ($ orderItem ->getIsQtyDecimal (), $ item ->getQty ())) {
136
+ $ messages [] =__ (
137
+ 'You cannot use decimal quantity to refund item "%1". ' ,
138
+ $ orderItem ->getSku ()
139
+ );
140
+ } elseif (!$ this ->canRefundItem ($ orderItem , $ item ->getQty (), $ invoiceQtysRefundLimits ) ||
141
+ !$ this ->isQtyAvailable ($ orderItem , $ item ->getQty ())
142
+ ) {
143
+ $ messages [] =__ (
144
+ 'The quantity to creditmemo must not be greater than the unrefunded quantity '
145
+ . ' for product SKU "%1". ' ,
146
+ $ orderItem ->getSku ()
147
+ );
148
+ } else {
149
+ $ totalQuantity += $ item ->getQty ();
150
+ }
151
+ }
152
+ }
153
+
131
154
/**
132
155
* We can have problem with float in php (on some server $a=762.73;$b=762.73; $a-$b!=0)
133
156
* for this we have additional diapason for 0
0 commit comments