@@ -127,28 +127,16 @@ public function setOrderItem(\Magento\Sales\Model\Order\Item $item)
127
127
public function getOrderItem ()
128
128
{
129
129
if ($ this ->_orderItem === null ) {
130
- $ this ->_orderItem = $ this ->getOrderItemWithoutCaching ();
130
+ if ($ this ->getCreditmemo ()) {
131
+ $ orderItem = $ this ->getCreditmemo ()->getOrder ()->getItemById ($ this ->getOrderItemId ());
132
+ } else {
133
+ $ orderItem = $ this ->_orderItemFactory ->create ()->load ($ this ->getOrderItemId ());
134
+ }
135
+ $ this ->_orderItem = $ orderItem ;
131
136
}
132
137
return $ this ->_orderItem ;
133
138
}
134
139
135
- /**
136
- * Retrieve order item instance without set it to property.
137
- * It is need for ability to process setQty on api when credit memo and order has not built yet.
138
- *
139
- * @return \Magento\Sales\Model\Order\Item
140
- */
141
- private function getOrderItemWithoutCaching ()
142
- {
143
- if ($ this ->getCreditmemo ()) {
144
- $ orderItem = $ this ->getCreditmemo ()->getOrder ()->getItemById ($ this ->getOrderItemId ());
145
- } else {
146
- $ orderItem = $ this ->_orderItemFactory ->create ()->load ($ this ->getOrderItemId ());
147
- }
148
-
149
- return $ orderItem ;
150
- }
151
-
152
140
/**
153
141
* Checks if quantity available for refund
154
142
*
@@ -164,26 +152,12 @@ private function isQtyAvailable($qty, \Magento\Sales\Model\Order\Item $orderItem
164
152
/**
165
153
* Declare qty
166
154
*
167
- * @param float $qty
155
+ * @param float $qty
168
156
* @return $this
169
- * @throws \Magento\Framework\Exception\LocalizedException
170
157
*/
171
158
public function setQty ($ qty )
172
159
{
173
- $ orderItem = $ this ->getOrderItemWithoutCaching ();
174
- if ($ orderItem ->getIsQtyDecimal ()) {
175
- $ qty = (double )$ qty ;
176
- } else {
177
- $ qty = (int )$ qty ;
178
- }
179
- $ qty = $ qty > 0 ? $ qty : 0 ;
180
- if ($ this ->isQtyAvailable ($ qty , $ orderItem )) {
181
- $ this ->setData ('qty ' , $ qty );
182
- } else {
183
- throw new \Magento \Framework \Exception \LocalizedException (
184
- __ ('We found an invalid quantity to refund item "%1". ' , $ this ->getName ())
185
- );
186
- }
160
+ $ this ->setData (CreditmemoItemInterface::QTY , $ qty );
187
161
return $ this ;
188
162
}
189
163
@@ -196,7 +170,8 @@ public function register()
196
170
{
197
171
$ orderItem = $ this ->getOrderItem ();
198
172
199
- $ orderItem ->setQtyRefunded ($ orderItem ->getQtyRefunded () + $ this ->getQty ());
173
+ $ qty = $ this ->processQty ();
174
+ $ orderItem ->setQtyRefunded ($ orderItem ->getQtyRefunded () + $ qty );
200
175
$ orderItem ->setTaxRefunded ($ orderItem ->getTaxRefunded () + $ this ->getTaxAmount ());
201
176
$ orderItem ->setBaseTaxRefunded ($ orderItem ->getBaseTaxRefunded () + $ this ->getBaseTaxAmount ());
202
177
$ orderItem ->setDiscountTaxCompensationRefunded (
@@ -213,22 +188,46 @@ public function register()
213
188
return $ this ;
214
189
}
215
190
191
+ /**
192
+ * @return int|float
193
+ * @throws \Magento\Framework\Exception\LocalizedException
194
+ */
195
+ private function processQty ()
196
+ {
197
+ $ orderItem = $ this ->getOrderItem ();
198
+ $ qty = $ this ->getQty ();
199
+ if ($ orderItem ->getIsQtyDecimal ()) {
200
+ $ qty = (double )$ qty ;
201
+ } else {
202
+ $ qty = (int )$ qty ;
203
+ }
204
+ $ qty = $ qty > 0 ? $ qty : 0 ;
205
+ if ($ this ->isQtyAvailable ($ qty , $ orderItem )) {
206
+ return $ qty ;
207
+ } else {
208
+ throw new \Magento \Framework \Exception \LocalizedException (
209
+ __ ('We found an invalid quantity to refund item "%1". ' , $ this ->getName ())
210
+ );
211
+ }
212
+ }
213
+
216
214
/**
217
215
* @return $this
218
216
*/
219
217
public function cancel ()
220
218
{
221
- $ this ->getOrderItem ()->setQtyRefunded ($ this ->getOrderItem ()->getQtyRefunded () - $ this ->getQty ());
219
+ $ qty = $ this ->processQty ();
220
+ $ this ->getOrderItem ()->setQtyRefunded ($ this ->getOrderItem ()->getQtyRefunded () - $ qty );
222
221
$ this ->getOrderItem ()->setTaxRefunded (
223
222
$ this ->getOrderItem ()->getTaxRefunded () -
224
223
$ this ->getOrderItem ()->getBaseTaxAmount () *
225
- $ this -> getQty () /
224
+ $ qty /
226
225
$ this ->getOrderItem ()->getQtyOrdered ()
227
226
);
228
227
$ this ->getOrderItem ()->setDiscountTaxCompensationRefunded (
229
228
$ this ->getOrderItem ()->getDiscountTaxCompensationRefunded () -
230
229
$ this ->getOrderItem ()->getDiscountTaxCompensationAmount () *
231
- $ this -> getQty () /
230
+ $ qty /
232
231
$ this ->getOrderItem ()->getQtyOrdered ()
233
232
);
234
233
return $ this ;
@@ -250,21 +249,22 @@ public function calcRowTotal()
250
249
$ rowTotalInclTax = $ orderItem ->getRowTotalInclTax ();
251
250
$ baseRowTotalInclTax = $ orderItem ->getBaseRowTotalInclTax ();
252
251
253
- if (!$ this ->isLast () && $ orderItemQtyInvoiced > 0 && $ this ->getQty () >= 0 ) {
252
+ $ qty = $ this ->processQty ();
253
+ if (!$ this ->isLast () && $ orderItemQtyInvoiced > 0 && $ qty >= 0 ) {
254
254
$ availableQty = $ orderItemQtyInvoiced - $ orderItem ->getQtyRefunded ();
255
- $ rowTotal = $ creditmemo ->roundPrice ($ rowTotal / $ availableQty * $ this -> getQty () );
256
- $ baseRowTotal = $ creditmemo ->roundPrice ($ baseRowTotal / $ availableQty * $ this -> getQty () , 'base ' );
255
+ $ rowTotal = $ creditmemo ->roundPrice ($ rowTotal / $ availableQty * $ qty );
256
+ $ baseRowTotal = $ creditmemo ->roundPrice ($ baseRowTotal / $ availableQty * $ qty , 'base ' );
257
257
}
258
258
$ this ->setRowTotal ($ rowTotal );
259
259
$ this ->setBaseRowTotal ($ baseRowTotal );
260
260
261
261
if ($ rowTotalInclTax && $ baseRowTotalInclTax ) {
262
262
$ orderItemQty = $ orderItem ->getQtyOrdered ();
263
263
$ this ->setRowTotalInclTax (
264
- $ creditmemo ->roundPrice ($ rowTotalInclTax / $ orderItemQty * $ this -> getQty () , 'including ' )
264
+ $ creditmemo ->roundPrice ($ rowTotalInclTax / $ orderItemQty * $ qty , 'including ' )
265
265
);
266
266
$ this ->setBaseRowTotalInclTax (
267
- $ creditmemo ->roundPrice ($ baseRowTotalInclTax / $ orderItemQty * $ this -> getQty () , 'including_base ' )
267
+ $ creditmemo ->roundPrice ($ baseRowTotalInclTax / $ orderItemQty * $ qty , 'including_base ' )
268
268
);
269
269
}
270
270
return $ this ;
@@ -278,7 +278,8 @@ public function calcRowTotal()
278
278
public function isLast ()
279
279
{
280
280
$ orderItem = $ this ->getOrderItem ();
281
- if ((string )(double )$ this ->getQty () == (string )(double )$ orderItem ->getQtyToRefund ()) {
281
+ $ qty = $ this ->processQty ();
282
+ if ((string )(double )$ qty == (string )(double )$ orderItem ->getQtyToRefund ()) {
282
283
return true ;
283
284
}
284
285
return false ;
0 commit comments