Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Item.php
Go to the documentation of this file.
1 <?php
7 
11 
18 class Item extends AbstractModel implements CreditmemoItemInterface
19 {
23  protected $_eventPrefix = 'sales_creditmemo_item';
24 
28  protected $_eventObject = 'creditmemo_item';
29 
33  protected $_creditmemo = null;
34 
38  protected $_orderItem = null;
39 
43  protected $_orderItemFactory;
44 
55  public function __construct(
56  \Magento\Framework\Model\Context $context,
57  \Magento\Framework\Registry $registry,
58  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
60  \Magento\Sales\Model\Order\ItemFactory $orderItemFactory,
61  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
62  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
63  array $data = []
64  ) {
65  parent::__construct(
66  $context,
67  $registry,
68  $extensionFactory,
70  $resource,
71  $resourceCollection,
72  $data
73  );
74  $this->_orderItemFactory = $orderItemFactory;
75  }
76 
82  protected function _construct()
83  {
84  $this->_init(\Magento\Sales\Model\ResourceModel\Order\Creditmemo\Item::class);
85  }
86 
93  public function setCreditmemo(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
94  {
95  $this->_creditmemo = $creditmemo;
96  return $this;
97  }
98 
104  public function getCreditmemo()
105  {
106  return $this->_creditmemo;
107  }
108 
115  public function setOrderItem(\Magento\Sales\Model\Order\Item $item)
116  {
117  $this->_orderItem = $item;
118  $this->setOrderItemId($item->getId());
119  return $this;
120  }
121 
127  public function getOrderItem()
128  {
129  if ($this->_orderItem === null) {
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;
136  }
137  return $this->_orderItem;
138  }
139 
147  private function isQtyAvailable($qty, \Magento\Sales\Model\Order\Item $orderItem)
148  {
149  return $qty <= $orderItem->getQtyToRefund() || $orderItem->isDummy();
150  }
151 
158  public function setQty($qty)
159  {
161  return $this;
162  }
163 
169  public function register()
170  {
171  $orderItem = $this->getOrderItem();
172 
173  $qty = $this->processQty();
174  $orderItem->setQtyRefunded($orderItem->getQtyRefunded() + $qty);
175  $orderItem->setTaxRefunded($orderItem->getTaxRefunded() + $this->getTaxAmount());
176  $orderItem->setBaseTaxRefunded($orderItem->getBaseTaxRefunded() + $this->getBaseTaxAmount());
177  $orderItem->setDiscountTaxCompensationRefunded(
178  $orderItem->getDiscountTaxCompensationRefunded() + $this->getDiscountTaxCompensationAmount()
179  );
180  $orderItem->setBaseDiscountTaxCompensationRefunded(
181  $orderItem->getBaseDiscountTaxCompensationRefunded() + $this->getBaseDiscountTaxCompensationAmount()
182  );
183  $orderItem->setAmountRefunded($orderItem->getAmountRefunded() + $this->getRowTotal());
184  $orderItem->setBaseAmountRefunded($orderItem->getBaseAmountRefunded() + $this->getBaseRowTotal());
185  $orderItem->setDiscountRefunded($orderItem->getDiscountRefunded() + $this->getDiscountAmount());
186  $orderItem->setBaseDiscountRefunded($orderItem->getBaseDiscountRefunded() + $this->getBaseDiscountAmount());
187 
188  return $this;
189  }
190 
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 
217  public function cancel()
218  {
219  $qty = $this->processQty();
220  $this->getOrderItem()->setQtyRefunded($this->getOrderItem()->getQtyRefunded() - $qty);
221  $this->getOrderItem()->setTaxRefunded(
222  $this->getOrderItem()->getTaxRefunded() -
223  $this->getOrderItem()->getBaseTaxAmount() *
224  $qty /
225  $this->getOrderItem()->getQtyOrdered()
226  );
227  $this->getOrderItem()->setDiscountTaxCompensationRefunded(
230  $qty /
231  $this->getOrderItem()->getQtyOrdered()
232  );
233  return $this;
234  }
235 
241  public function calcRowTotal()
242  {
243  $creditmemo = $this->getCreditmemo();
244  $orderItem = $this->getOrderItem();
245  $orderItemQtyInvoiced = $orderItem->getQtyInvoiced();
246 
247  $rowTotal = $orderItem->getRowInvoiced() - $orderItem->getAmountRefunded();
248  $baseRowTotal = $orderItem->getBaseRowInvoiced() - $orderItem->getBaseAmountRefunded();
249  $rowTotalInclTax = $orderItem->getRowTotalInclTax();
250  $baseRowTotalInclTax = $orderItem->getBaseRowTotalInclTax();
251 
252  $qty = $this->processQty();
253  if (!$this->isLast() && $orderItemQtyInvoiced > 0 && $qty >= 0) {
254  $availableQty = $orderItemQtyInvoiced - $orderItem->getQtyRefunded();
255  $rowTotal = $creditmemo->roundPrice($rowTotal / $availableQty * $qty);
256  $baseRowTotal = $creditmemo->roundPrice($baseRowTotal / $availableQty * $qty, 'base');
257  }
258  $this->setRowTotal($rowTotal);
259  $this->setBaseRowTotal($baseRowTotal);
260 
261  if ($rowTotalInclTax && $baseRowTotalInclTax) {
262  $orderItemQty = $orderItem->getQtyOrdered();
263  $this->setRowTotalInclTax(
264  $creditmemo->roundPrice($rowTotalInclTax / $orderItemQty * $qty, 'including')
265  );
266  $this->setBaseRowTotalInclTax(
267  $creditmemo->roundPrice($baseRowTotalInclTax / $orderItemQty * $qty, 'including_base')
268  );
269  }
270  return $this;
271  }
272 
278  public function isLast()
279  {
280  $orderItem = $this->getOrderItem();
281  $qty = $this->processQty();
282  if ((string)(double)$qty == (string)(double)$orderItem->getQtyToRefund()) {
283  return true;
284  }
285  return false;
286  }
287 
293  public function getAdditionalData()
294  {
296  }
297 
303  public function getBaseCost()
304  {
306  }
307 
313  public function getBaseDiscountAmount()
314  {
316  }
317 
324  {
326  }
327 
333  public function getBasePrice()
334  {
336  }
337 
343  public function getBasePriceInclTax()
344  {
346  }
347 
353  public function getBaseRowTotal()
354  {
356  }
357 
363  public function getBaseRowTotalInclTax()
364  {
366  }
367 
373  public function getBaseTaxAmount()
374  {
376  }
377 
383  public function getBaseWeeeTaxAppliedAmount()
384  {
386  }
387 
394  {
396  }
397 
403  public function getBaseWeeeTaxDisposition()
404  {
406  }
407 
414  {
416  }
417 
423  public function getDescription()
424  {
426  }
427 
433  public function getDiscountAmount()
434  {
436  }
437 
444  {
446  }
447 
453  public function getName()
454  {
455  return $this->getData(CreditmemoItemInterface::NAME);
456  }
457 
463  public function getOrderItemId()
464  {
466  }
467 
473  public function getParentId()
474  {
476  }
477 
483  public function getPrice()
484  {
486  }
487 
493  public function getPriceInclTax()
494  {
496  }
497 
503  public function getProductId()
504  {
506  }
507 
513  public function getQty()
514  {
515  return $this->getData(CreditmemoItemInterface::QTY);
516  }
517 
523  public function getRowTotal()
524  {
526  }
527 
533  public function getRowTotalInclTax()
534  {
536  }
537 
543  public function getSku()
544  {
545  return $this->getData(CreditmemoItemInterface::SKU);
546  }
547 
553  public function getTaxAmount()
554  {
556  }
557 
563  public function getWeeeTaxApplied()
564  {
566  }
567 
573  public function getWeeeTaxAppliedAmount()
574  {
576  }
577 
583  public function getWeeeTaxAppliedRowAmount()
584  {
586  }
587 
593  public function getWeeeTaxDisposition()
594  {
596  }
597 
603  public function getWeeeTaxRowDisposition()
604  {
606  }
607 
608  //@codeCoverageIgnoreStart
609 
613  public function setParentId($id)
614  {
616  }
617 
621  public function setBasePrice($price)
622  {
624  }
625 
629  public function setTaxAmount($amount)
630  {
632  }
633 
637  public function setBaseRowTotal($amount)
638  {
640  }
641 
645  public function setDiscountAmount($amount)
646  {
648  }
649 
653  public function setRowTotal($amount)
654  {
656  }
657 
662  {
664  }
665 
669  public function setPriceInclTax($amount)
670  {
672  }
673 
677  public function setBaseTaxAmount($amount)
678  {
680  }
681 
685  public function setBasePriceInclTax($amount)
686  {
688  }
689 
693  public function setBaseCost($baseCost)
694  {
695  return $this->setData(CreditmemoItemInterface::BASE_COST, $baseCost);
696  }
697 
701  public function setPrice($price)
702  {
704  }
705 
710  {
712  }
713 
717  public function setRowTotalInclTax($amount)
718  {
720  }
721 
725  public function setProductId($id)
726  {
728  }
729 
733  public function setOrderItemId($id)
734  {
736  }
737 
741  public function setAdditionalData($additionalData)
742  {
743  return $this->setData(CreditmemoItemInterface::ADDITIONAL_DATA, $additionalData);
744  }
745 
749  public function setDescription($description)
750  {
752  }
753 
757  public function setSku($sku)
758  {
759  return $this->setData(CreditmemoItemInterface::SKU, $sku);
760  }
761 
765  public function setName($name)
766  {
768  }
769 
774  {
776  }
777 
782  {
784  }
785 
789  public function setWeeeTaxDisposition($weeeTaxDisposition)
790  {
791  return $this->setData(CreditmemoItemInterface::WEEE_TAX_DISPOSITION, $weeeTaxDisposition);
792  }
793 
797  public function setWeeeTaxRowDisposition($weeeTaxRowDisposition)
798  {
799  return $this->setData(CreditmemoItemInterface::WEEE_TAX_ROW_DISPOSITION, $weeeTaxRowDisposition);
800  }
801 
805  public function setBaseWeeeTaxDisposition($baseWeeeTaxDisposition)
806  {
807  return $this->setData(CreditmemoItemInterface::BASE_WEEE_TAX_DISPOSITION, $baseWeeeTaxDisposition);
808  }
809 
813  public function setBaseWeeeTaxRowDisposition($baseWeeeTaxRowDisposition)
814  {
815  return $this->setData(CreditmemoItemInterface::BASE_WEEE_TAX_ROW_DISPOSITION, $baseWeeeTaxRowDisposition);
816  }
817 
821  public function setWeeeTaxApplied($weeeTaxApplied)
822  {
823  return $this->setData(CreditmemoItemInterface::WEEE_TAX_APPLIED, $weeeTaxApplied);
824  }
825 
830  {
832  }
833 
837  public function setBaseWeeeTaxAppliedRowAmnt($amnt)
838  {
840  }
841 
846  {
848  }
849 
854  {
856  }
857 
863  public function getExtensionAttributes()
864  {
865  return $this->_getExtensionAttributes();
866  }
867 
874  public function setExtensionAttributes(
875  \Magento\Sales\Api\Data\CreditmemoItemExtensionInterface $extensionAttributes
876  ) {
877  return $this->_setExtensionAttributes($extensionAttributes);
878  }
879 
880  //@codeCoverageIgnoreEnd
881 }
$orderItem
Definition: order.php:30
$id
Definition: fieldset.phtml:14
setOrderItem(\Magento\Sales\Model\Order\Item $item)
Definition: Item.php:115
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
setWeeeTaxDisposition($weeeTaxDisposition)
Definition: Item.php:789
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$price
setExtensionAttributes(\Magento\Sales\Api\Data\CreditmemoItemExtensionInterface $extensionAttributes)
Definition: Item.php:874
$amount
Definition: order.php:14
setBaseWeeeTaxRowDisposition($baseWeeeTaxRowDisposition)
Definition: Item.php:813
setBaseWeeeTaxDisposition($baseWeeeTaxDisposition)
Definition: Item.php:805
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Sales\Model\Order\ItemFactory $orderItemFactory, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Item.php:55
setWeeeTaxRowDisposition($weeeTaxRowDisposition)
Definition: Item.php:797
setCreditmemo(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
Definition: Item.php:93
if(!isset($_GET['name'])) $name
Definition: log.php:14