@@ -127,28 +127,16 @@ public function setOrderItem(\Magento\Sales\Model\Order\Item $item)
127127 public function getOrderItem ()
128128 {
129129 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 ;
131136 }
132137 return $ this ->_orderItem ;
133138 }
134139
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-
152140 /**
153141 * Checks if quantity available for refund
154142 *
@@ -164,26 +152,12 @@ private function isQtyAvailable($qty, \Magento\Sales\Model\Order\Item $orderItem
164152 /**
165153 * Declare qty
166154 *
167- * @param float $qty
155+ * @param float $qty
168156 * @return $this
169- * @throws \Magento\Framework\Exception\LocalizedException
170157 */
171158 public function setQty ($ qty )
172159 {
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 );
187161 return $ this ;
188162 }
189163
@@ -196,7 +170,8 @@ public function register()
196170 {
197171 $ orderItem = $ this ->getOrderItem ();
198172
199- $ orderItem ->setQtyRefunded ($ orderItem ->getQtyRefunded () + $ this ->getQty ());
173+ $ qty = $ this ->processQty ();
174+ $ orderItem ->setQtyRefunded ($ orderItem ->getQtyRefunded () + $ qty );
200175 $ orderItem ->setTaxRefunded ($ orderItem ->getTaxRefunded () + $ this ->getTaxAmount ());
201176 $ orderItem ->setBaseTaxRefunded ($ orderItem ->getBaseTaxRefunded () + $ this ->getBaseTaxAmount ());
202177 $ orderItem ->setDiscountTaxCompensationRefunded (
@@ -213,22 +188,46 @@ public function register()
213188 return $ this ;
214189 }
215190
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+
216214 /**
217215 * @return $this
218216 */
219217 public function cancel ()
220218 {
221- $ this ->getOrderItem ()->setQtyRefunded ($ this ->getOrderItem ()->getQtyRefunded () - $ this ->getQty ());
219+ $ qty = $ this ->processQty ();
220+ $ this ->getOrderItem ()->setQtyRefunded ($ this ->getOrderItem ()->getQtyRefunded () - $ qty );
222221 $ this ->getOrderItem ()->setTaxRefunded (
223222 $ this ->getOrderItem ()->getTaxRefunded () -
224223 $ this ->getOrderItem ()->getBaseTaxAmount () *
225- $ this -> getQty () /
224+ $ qty /
226225 $ this ->getOrderItem ()->getQtyOrdered ()
227226 );
228227 $ this ->getOrderItem ()->setDiscountTaxCompensationRefunded (
229228 $ this ->getOrderItem ()->getDiscountTaxCompensationRefunded () -
230229 $ this ->getOrderItem ()->getDiscountTaxCompensationAmount () *
231- $ this -> getQty () /
230+ $ qty /
232231 $ this ->getOrderItem ()->getQtyOrdered ()
233232 );
234233 return $ this ;
@@ -250,21 +249,22 @@ public function calcRowTotal()
250249 $ rowTotalInclTax = $ orderItem ->getRowTotalInclTax ();
251250 $ baseRowTotalInclTax = $ orderItem ->getBaseRowTotalInclTax ();
252251
253- if (!$ this ->isLast () && $ orderItemQtyInvoiced > 0 && $ this ->getQty () >= 0 ) {
252+ $ qty = $ this ->processQty ();
253+ if (!$ this ->isLast () && $ orderItemQtyInvoiced > 0 && $ qty >= 0 ) {
254254 $ 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 ' );
257257 }
258258 $ this ->setRowTotal ($ rowTotal );
259259 $ this ->setBaseRowTotal ($ baseRowTotal );
260260
261261 if ($ rowTotalInclTax && $ baseRowTotalInclTax ) {
262262 $ orderItemQty = $ orderItem ->getQtyOrdered ();
263263 $ this ->setRowTotalInclTax (
264- $ creditmemo ->roundPrice ($ rowTotalInclTax / $ orderItemQty * $ this -> getQty () , 'including ' )
264+ $ creditmemo ->roundPrice ($ rowTotalInclTax / $ orderItemQty * $ qty , 'including ' )
265265 );
266266 $ this ->setBaseRowTotalInclTax (
267- $ creditmemo ->roundPrice ($ baseRowTotalInclTax / $ orderItemQty * $ this -> getQty () , 'including_base ' )
267+ $ creditmemo ->roundPrice ($ baseRowTotalInclTax / $ orderItemQty * $ qty , 'including_base ' )
268268 );
269269 }
270270 return $ this ;
@@ -278,7 +278,8 @@ public function calcRowTotal()
278278 public function isLast ()
279279 {
280280 $ 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 ()) {
282283 return true ;
283284 }
284285 return false ;
0 commit comments