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 
36 class Item extends AbstractModel implements InvoiceItemInterface
37 {
41  protected $_eventPrefix = 'sales_invoice_item';
42 
46  protected $_eventObject = 'invoice_item';
47 
51  protected $_orderItem = null;
52 
56  protected $_orderItemFactory;
57 
68  public function __construct(
69  \Magento\Framework\Model\Context $context,
70  \Magento\Framework\Registry $registry,
71  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
73  \Magento\Sales\Model\Order\ItemFactory $orderItemFactory,
74  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
75  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
76  array $data = []
77  ) {
78  parent::__construct(
79  $context,
80  $registry,
81  $extensionFactory,
83  $resource,
84  $resourceCollection,
85  $data
86  );
87  $this->_orderItemFactory = $orderItemFactory;
88  }
89 
95  protected function _construct()
96  {
97  $this->_init(\Magento\Sales\Model\ResourceModel\Order\Invoice\Item::class);
98  }
99 
106  public function setInvoice(\Magento\Sales\Api\Data\InvoiceInterface $invoice)
107  {
108  return $this->setData(self::INVOICE, $invoice);
109  }
110 
118  public function getInvoice()
119  {
120  return $this->getData(self::INVOICE);
121  }
122 
129  public function setOrderItem(\Magento\Sales\Model\Order\Item $item)
130  {
131  $this->_orderItem = $item;
132  $this->setOrderItemId($item->getId());
133  return $this;
134  }
135 
141  public function getOrderItem()
142  {
143  if ($this->_orderItem === null) {
144  if ($this->getInvoice()) {
145  $this->_orderItem = $this->getInvoice()->getOrder()->getItemById($this->getOrderItemId());
146  } else {
147  $this->_orderItem = $this->_orderItemFactory->create()->load($this->getOrderItemId());
148  }
149  }
150  return $this->_orderItem;
151  }
152 
161  public function setQty($qty)
162  {
163  return $this->setData(self::QTY, $qty);
164  }
165 
171  public function register()
172  {
173  $orderItem = $this->getOrderItem();
174  $orderItem->setQtyInvoiced($orderItem->getQtyInvoiced() + $this->getQty());
175 
176  $orderItem->setTaxInvoiced($orderItem->getTaxInvoiced() + $this->getTaxAmount());
177  $orderItem->setBaseTaxInvoiced($orderItem->getBaseTaxInvoiced() + $this->getBaseTaxAmount());
178  $orderItem->setDiscountTaxCompensationInvoiced(
179  $orderItem->getDiscountTaxCompensationInvoiced() + $this->getDiscountTaxCompensationAmount()
180  );
181  $orderItem->setBaseDiscountTaxCompensationInvoiced(
182  $orderItem->getBaseDiscountTaxCompensationInvoiced() + $this->getBaseDiscountTaxCompensationAmount()
183  );
184 
185  $orderItem->setDiscountInvoiced($orderItem->getDiscountInvoiced() + $this->getDiscountAmount());
186  $orderItem->setBaseDiscountInvoiced($orderItem->getBaseDiscountInvoiced() + $this->getBaseDiscountAmount());
187 
188  $orderItem->setRowInvoiced($orderItem->getRowInvoiced() + $this->getRowTotal());
189  $orderItem->setBaseRowInvoiced($orderItem->getBaseRowInvoiced() + $this->getBaseRowTotal());
190  return $this;
191  }
192 
198  public function cancel()
199  {
200  $orderItem = $this->getOrderItem();
201  $orderItem->setQtyInvoiced($orderItem->getQtyInvoiced() - $this->getQty());
202 
203  $orderItem->setTaxInvoiced($orderItem->getTaxInvoiced() - $this->getTaxAmount());
204  $orderItem->setBaseTaxInvoiced($orderItem->getBaseTaxInvoiced() - $this->getBaseTaxAmount());
205  $orderItem->setDiscountTaxCompensationInvoiced(
206  $orderItem->getDiscountTaxCompensationInvoiced() - $this->getDiscountTaxCompensationAmount()
207  );
208  $orderItem->setBaseDiscountTaxCompensationInvoiced(
209  $orderItem->getBaseDiscountTaxCompensationInvoiced() - $this->getBaseDiscountTaxCompensationAmount()
210  );
211 
212  $orderItem->setDiscountInvoiced($orderItem->getDiscountInvoiced() - $this->getDiscountAmount());
213  $orderItem->setBaseDiscountInvoiced($orderItem->getBaseDiscountInvoiced() - $this->getBaseDiscountAmount());
214 
215  $orderItem->setRowInvoiced($orderItem->getRowInvoiced() - $this->getRowTotal());
216  $orderItem->setBaseRowInvoiced($orderItem->getBaseRowInvoiced() - $this->getBaseRowTotal());
217  return $this;
218  }
219 
225  public function calcRowTotal()
226  {
227  $invoice = $this->getInvoice();
228  $orderItem = $this->getOrderItem();
229  $orderItemQty = $orderItem->getQtyOrdered();
230 
231  $rowTotal = $orderItem->getRowTotal() - $orderItem->getRowInvoiced();
232  $baseRowTotal = $orderItem->getBaseRowTotal() - $orderItem->getBaseRowInvoiced();
233  $rowTotalInclTax = $orderItem->getRowTotalInclTax();
234  $baseRowTotalInclTax = $orderItem->getBaseRowTotalInclTax();
235 
236  if (!$this->isLast()) {
237  $availableQty = $orderItemQty - $orderItem->getQtyInvoiced();
238  $rowTotal = $invoice->roundPrice($rowTotal / $availableQty * $this->getQty());
239  $baseRowTotal = $invoice->roundPrice($baseRowTotal / $availableQty * $this->getQty(), 'base');
240  }
241 
242  $this->setRowTotal($rowTotal);
243  $this->setBaseRowTotal($baseRowTotal);
244 
245  if ($rowTotalInclTax && $baseRowTotalInclTax) {
246  $this->setRowTotalInclTax(
247  $invoice->roundPrice($rowTotalInclTax / $orderItemQty * $this->getQty(), 'including')
248  );
249  $this->setBaseRowTotalInclTax(
250  $invoice->roundPrice($baseRowTotalInclTax / $orderItemQty * $this->getQty(), 'including_base')
251  );
252  }
253  return $this;
254  }
255 
261  public function isLast()
262  {
263  if ((string)(double)$this->getQty() == (string)(double)$this->getOrderItem()->getQtyToInvoice()) {
264  return true;
265  }
266  return false;
267  }
268 
269  //@codeCoverageIgnoreStart
270 
276  public function getAdditionalData()
277  {
279  }
280 
286  public function getBaseCost()
287  {
289  }
290 
296  public function getBaseDiscountAmount()
297  {
299  }
300 
307  {
309  }
310 
316  public function getBasePrice()
317  {
319  }
320 
326  public function getBasePriceInclTax()
327  {
329  }
330 
336  public function getBaseRowTotal()
337  {
339  }
340 
346  public function getBaseRowTotalInclTax()
347  {
349  }
350 
356  public function getBaseTaxAmount()
357  {
359  }
360 
366  public function getDescription()
367  {
369  }
370 
376  public function getDiscountAmount()
377  {
379  }
380 
387  {
389  }
390 
396  public function getName()
397  {
398  return $this->getData(InvoiceItemInterface::NAME);
399  }
400 
406  public function getOrderItemId()
407  {
409  }
410 
416  public function getParentId()
417  {
419  }
420 
426  public function getPrice()
427  {
428  return $this->getData(InvoiceItemInterface::PRICE);
429  }
430 
436  public function getPriceInclTax()
437  {
439  }
440 
446  public function getProductId()
447  {
449  }
450 
456  public function getQty()
457  {
458  return $this->getData(InvoiceItemInterface::QTY);
459  }
460 
466  public function getRowTotal()
467  {
469  }
470 
476  public function getRowTotalInclTax()
477  {
479  }
480 
486  public function getSku()
487  {
488  return $this->getData(InvoiceItemInterface::SKU);
489  }
490 
496  public function getTaxAmount()
497  {
499  }
500 
504  public function setParentId($id)
505  {
507  }
508 
512  public function setBasePrice($price)
513  {
515  }
516 
520  public function setTaxAmount($amount)
521  {
523  }
524 
528  public function setBaseRowTotal($amount)
529  {
531  }
532 
536  public function setDiscountAmount($amount)
537  {
539  }
540 
544  public function setRowTotal($amount)
545  {
547  }
548 
553  {
555  }
556 
560  public function setPriceInclTax($amount)
561  {
563  }
564 
568  public function setBaseTaxAmount($amount)
569  {
571  }
572 
576  public function setBasePriceInclTax($amount)
577  {
579  }
580 
584  public function setBaseCost($baseCost)
585  {
586  return $this->setData(InvoiceItemInterface::BASE_COST, $baseCost);
587  }
588 
592  public function setPrice($price)
593  {
595  }
596 
601  {
603  }
604 
608  public function setRowTotalInclTax($amount)
609  {
611  }
612 
616  public function setProductId($id)
617  {
619  }
620 
624  public function setOrderItemId($id)
625  {
627  }
628 
632  public function setAdditionalData($additionalData)
633  {
634  return $this->setData(InvoiceItemInterface::ADDITIONAL_DATA, $additionalData);
635  }
636 
640  public function setDescription($description)
641  {
643  }
644 
648  public function setSku($sku)
649  {
650  return $this->setData(InvoiceItemInterface::SKU, $sku);
651  }
652 
656  public function setName($name)
657  {
658  return $this->setData(InvoiceItemInterface::NAME, $name);
659  }
660 
665  {
667  }
668 
673  {
675  }
676 
682  public function getExtensionAttributes()
683  {
684  return $this->_getExtensionAttributes();
685  }
686 
693  public function setExtensionAttributes(\Magento\Sales\Api\Data\InvoiceItemExtensionInterface $extensionAttributes)
694  {
695  return $this->_setExtensionAttributes($extensionAttributes);
696  }
697 
698  //@codeCoverageIgnoreEnd
699 }
setExtensionAttributes(\Magento\Sales\Api\Data\InvoiceItemExtensionInterface $extensionAttributes)
Definition: Item.php:693
$orderItem
Definition: order.php:30
$id
Definition: fieldset.phtml:14
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
$resource
Definition: bulk.php:12
$price
setOrderItem(\Magento\Sales\Model\Order\Item $item)
Definition: Item.php:129
__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:68
$amount
Definition: order.php:14
setAdditionalData($additionalData)
Definition: Item.php:632
setInvoice(\Magento\Sales\Api\Data\InvoiceInterface $invoice)
Definition: Item.php:106
$invoice
if(!isset($_GET['name'])) $name
Definition: log.php:14