Item.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order\Invoice;
  7. use Magento\Framework\Api\AttributeValueFactory;
  8. use Magento\Sales\Api\Data\InvoiceItemInterface;
  9. use Magento\Sales\Model\AbstractModel;
  10. /**
  11. * @api
  12. * @method float getBaseWeeeTaxRowDisposition()
  13. * @method \Magento\Sales\Model\Order\Invoice\Item setBaseWeeeTaxRowDisposition(float $value)
  14. * @method float getWeeeTaxAppliedRowAmount()
  15. * @method \Magento\Sales\Model\Order\Invoice\Item setWeeeTaxAppliedRowAmount(float $value)
  16. * @method float getBaseWeeeTaxAppliedAmount()
  17. * @method \Magento\Sales\Model\Order\Invoice\Item setBaseWeeeTaxAppliedAmount(float $value)
  18. * @method float getWeeeTaxRowDisposition()
  19. * @method \Magento\Sales\Model\Order\Invoice\Item setWeeeTaxRowDisposition(float $value)
  20. * @method float getBaseWeeeTaxDisposition()
  21. * @method \Magento\Sales\Model\Order\Invoice\Item setBaseWeeeTaxDisposition(float $value)
  22. * @method float getWeeeTaxAppliedAmount()
  23. * @method \Magento\Sales\Model\Order\Invoice\Item setWeeeTaxAppliedAmount(float $value)
  24. * @method float getWeeeTaxDisposition()
  25. * @method \Magento\Sales\Model\Order\Invoice\Item setWeeeTaxDisposition(float $value)
  26. * @method float getBaseWeeeTaxAppliedRowAmnt()
  27. * @method \Magento\Sales\Model\Order\Invoice\Item setBaseWeeeTaxAppliedRowAmnt(float $value)
  28. * @method string getWeeeTaxApplied()
  29. * @method \Magento\Sales\Model\Order\Invoice\Item setWeeeTaxApplied(string $value)
  30. * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  31. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  32. * @since 100.0.2
  33. */
  34. class Item extends AbstractModel implements InvoiceItemInterface
  35. {
  36. /**
  37. * @var string
  38. */
  39. protected $_eventPrefix = 'sales_invoice_item';
  40. /**
  41. * @var string
  42. */
  43. protected $_eventObject = 'invoice_item';
  44. /**
  45. * @var \Magento\Sales\Model\Order\Item|null
  46. */
  47. protected $_orderItem = null;
  48. /**
  49. * @var \Magento\Sales\Model\Order\ItemFactory
  50. */
  51. protected $_orderItemFactory;
  52. /**
  53. * @param \Magento\Framework\Model\Context $context
  54. * @param \Magento\Framework\Registry $registry
  55. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  56. * @param AttributeValueFactory $customAttributeFactory
  57. * @param \Magento\Sales\Model\Order\ItemFactory $orderItemFactory
  58. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  59. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  60. * @param array $data
  61. */
  62. public function __construct(
  63. \Magento\Framework\Model\Context $context,
  64. \Magento\Framework\Registry $registry,
  65. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  66. AttributeValueFactory $customAttributeFactory,
  67. \Magento\Sales\Model\Order\ItemFactory $orderItemFactory,
  68. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  69. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  70. array $data = []
  71. ) {
  72. parent::__construct(
  73. $context,
  74. $registry,
  75. $extensionFactory,
  76. $customAttributeFactory,
  77. $resource,
  78. $resourceCollection,
  79. $data
  80. );
  81. $this->_orderItemFactory = $orderItemFactory;
  82. }
  83. /**
  84. * Initialize resource model
  85. *
  86. * @return void
  87. */
  88. protected function _construct()
  89. {
  90. $this->_init(\Magento\Sales\Model\ResourceModel\Order\Invoice\Item::class);
  91. }
  92. /**
  93. * Declare invoice instance
  94. *
  95. * @param \Magento\Sales\Api\Data\InvoiceInterface $invoice
  96. * @return $this
  97. */
  98. public function setInvoice(\Magento\Sales\Api\Data\InvoiceInterface $invoice)
  99. {
  100. return $this->setData(self::INVOICE, $invoice);
  101. }
  102. /**
  103. * Retrieve invoice instance
  104. *
  105. * @codeCoverageIgnore
  106. *
  107. * @return \Magento\Sales\Model\Order\Invoice
  108. */
  109. public function getInvoice()
  110. {
  111. return $this->getData(self::INVOICE);
  112. }
  113. /**
  114. * Declare order item instance
  115. *
  116. * @param \Magento\Sales\Model\Order\Item $item
  117. * @return $this
  118. */
  119. public function setOrderItem(\Magento\Sales\Model\Order\Item $item)
  120. {
  121. $this->_orderItem = $item;
  122. $this->setOrderItemId($item->getId());
  123. return $this;
  124. }
  125. /**
  126. * Retrieve order item instance
  127. *
  128. * @return \Magento\Sales\Model\Order\Item
  129. */
  130. public function getOrderItem()
  131. {
  132. if ($this->_orderItem === null) {
  133. if ($this->getInvoice()) {
  134. $this->_orderItem = $this->getInvoice()->getOrder()->getItemById($this->getOrderItemId());
  135. } else {
  136. $this->_orderItem = $this->_orderItemFactory->create()->load($this->getOrderItemId());
  137. }
  138. }
  139. return $this->_orderItem;
  140. }
  141. /**
  142. * Declare qty
  143. *
  144. * @codeCoverageIgnore
  145. *
  146. * @param float $qty
  147. * @return $this
  148. */
  149. public function setQty($qty)
  150. {
  151. return $this->setData(self::QTY, $qty);
  152. }
  153. /**
  154. * Applying qty to order item
  155. *
  156. * @return $this
  157. */
  158. public function register()
  159. {
  160. $orderItem = $this->getOrderItem();
  161. $orderItem->setQtyInvoiced($orderItem->getQtyInvoiced() + $this->getQty());
  162. $orderItem->setTaxInvoiced($orderItem->getTaxInvoiced() + $this->getTaxAmount());
  163. $orderItem->setBaseTaxInvoiced($orderItem->getBaseTaxInvoiced() + $this->getBaseTaxAmount());
  164. $orderItem->setDiscountTaxCompensationInvoiced(
  165. $orderItem->getDiscountTaxCompensationInvoiced() + $this->getDiscountTaxCompensationAmount()
  166. );
  167. $orderItem->setBaseDiscountTaxCompensationInvoiced(
  168. $orderItem->getBaseDiscountTaxCompensationInvoiced() + $this->getBaseDiscountTaxCompensationAmount()
  169. );
  170. $orderItem->setDiscountInvoiced($orderItem->getDiscountInvoiced() + $this->getDiscountAmount());
  171. $orderItem->setBaseDiscountInvoiced($orderItem->getBaseDiscountInvoiced() + $this->getBaseDiscountAmount());
  172. $orderItem->setRowInvoiced($orderItem->getRowInvoiced() + $this->getRowTotal());
  173. $orderItem->setBaseRowInvoiced($orderItem->getBaseRowInvoiced() + $this->getBaseRowTotal());
  174. return $this;
  175. }
  176. /**
  177. * Cancelling invoice item
  178. *
  179. * @return $this
  180. */
  181. public function cancel()
  182. {
  183. $orderItem = $this->getOrderItem();
  184. $orderItem->setQtyInvoiced($orderItem->getQtyInvoiced() - $this->getQty());
  185. $orderItem->setTaxInvoiced($orderItem->getTaxInvoiced() - $this->getTaxAmount());
  186. $orderItem->setBaseTaxInvoiced($orderItem->getBaseTaxInvoiced() - $this->getBaseTaxAmount());
  187. $orderItem->setDiscountTaxCompensationInvoiced(
  188. $orderItem->getDiscountTaxCompensationInvoiced() - $this->getDiscountTaxCompensationAmount()
  189. );
  190. $orderItem->setBaseDiscountTaxCompensationInvoiced(
  191. $orderItem->getBaseDiscountTaxCompensationInvoiced() - $this->getBaseDiscountTaxCompensationAmount()
  192. );
  193. $orderItem->setDiscountInvoiced($orderItem->getDiscountInvoiced() - $this->getDiscountAmount());
  194. $orderItem->setBaseDiscountInvoiced($orderItem->getBaseDiscountInvoiced() - $this->getBaseDiscountAmount());
  195. $orderItem->setRowInvoiced($orderItem->getRowInvoiced() - $this->getRowTotal());
  196. $orderItem->setBaseRowInvoiced($orderItem->getBaseRowInvoiced() - $this->getBaseRowTotal());
  197. return $this;
  198. }
  199. /**
  200. * Invoice item row total calculation
  201. *
  202. * @return $this
  203. */
  204. public function calcRowTotal()
  205. {
  206. $invoice = $this->getInvoice();
  207. $orderItem = $this->getOrderItem();
  208. $orderItemQty = $orderItem->getQtyOrdered();
  209. $rowTotal = $orderItem->getRowTotal() - $orderItem->getRowInvoiced();
  210. $baseRowTotal = $orderItem->getBaseRowTotal() - $orderItem->getBaseRowInvoiced();
  211. $rowTotalInclTax = $orderItem->getRowTotalInclTax();
  212. $baseRowTotalInclTax = $orderItem->getBaseRowTotalInclTax();
  213. if (!$this->isLast()) {
  214. $availableQty = $orderItemQty - $orderItem->getQtyInvoiced();
  215. $rowTotal = $invoice->roundPrice($rowTotal / $availableQty * $this->getQty());
  216. $baseRowTotal = $invoice->roundPrice($baseRowTotal / $availableQty * $this->getQty(), 'base');
  217. }
  218. $this->setRowTotal($rowTotal);
  219. $this->setBaseRowTotal($baseRowTotal);
  220. if ($rowTotalInclTax && $baseRowTotalInclTax) {
  221. $this->setRowTotalInclTax(
  222. $invoice->roundPrice($rowTotalInclTax / $orderItemQty * $this->getQty(), 'including')
  223. );
  224. $this->setBaseRowTotalInclTax(
  225. $invoice->roundPrice($baseRowTotalInclTax / $orderItemQty * $this->getQty(), 'including_base')
  226. );
  227. }
  228. return $this;
  229. }
  230. /**
  231. * Checking if the item is last
  232. *
  233. * @return bool
  234. */
  235. public function isLast()
  236. {
  237. if ((string)(double)$this->getQty() == (string)(double)$this->getOrderItem()->getQtyToInvoice()) {
  238. return true;
  239. }
  240. return false;
  241. }
  242. //@codeCoverageIgnoreStart
  243. /**
  244. * Returns additional_data
  245. *
  246. * @return string|null
  247. */
  248. public function getAdditionalData()
  249. {
  250. return $this->getData(InvoiceItemInterface::ADDITIONAL_DATA);
  251. }
  252. /**
  253. * Returns base_cost
  254. *
  255. * @return float|null
  256. */
  257. public function getBaseCost()
  258. {
  259. return $this->getData(InvoiceItemInterface::BASE_COST);
  260. }
  261. /**
  262. * Returns base_discount_amount
  263. *
  264. * @return float|null
  265. */
  266. public function getBaseDiscountAmount()
  267. {
  268. return $this->getData(InvoiceItemInterface::BASE_DISCOUNT_AMOUNT);
  269. }
  270. /**
  271. * Returns base_discount_tax_compensation_amount
  272. *
  273. * @return float|null
  274. */
  275. public function getBaseDiscountTaxCompensationAmount()
  276. {
  277. return $this->getData(InvoiceItemInterface::BASE_DISCOUNT_TAX_COMPENSATION_AMOUNT);
  278. }
  279. /**
  280. * Returns base_price
  281. *
  282. * @return float|null
  283. */
  284. public function getBasePrice()
  285. {
  286. return $this->getData(InvoiceItemInterface::BASE_PRICE);
  287. }
  288. /**
  289. * Returns base_price_incl_tax
  290. *
  291. * @return float|null
  292. */
  293. public function getBasePriceInclTax()
  294. {
  295. return $this->getData(InvoiceItemInterface::BASE_PRICE_INCL_TAX);
  296. }
  297. /**
  298. * Returns base_row_total
  299. *
  300. * @return float|null
  301. */
  302. public function getBaseRowTotal()
  303. {
  304. return $this->getData(InvoiceItemInterface::BASE_ROW_TOTAL);
  305. }
  306. /**
  307. * Returns base_row_total_incl_tax
  308. *
  309. * @return float|null
  310. */
  311. public function getBaseRowTotalInclTax()
  312. {
  313. return $this->getData(InvoiceItemInterface::BASE_ROW_TOTAL_INCL_TAX);
  314. }
  315. /**
  316. * Returns base_tax_amount
  317. *
  318. * @return float|null
  319. */
  320. public function getBaseTaxAmount()
  321. {
  322. return $this->getData(InvoiceItemInterface::BASE_TAX_AMOUNT);
  323. }
  324. /**
  325. * Returns description
  326. *
  327. * @return string|null
  328. */
  329. public function getDescription()
  330. {
  331. return $this->getData(InvoiceItemInterface::DESCRIPTION);
  332. }
  333. /**
  334. * Returns discount_amount
  335. *
  336. * @return float|null
  337. */
  338. public function getDiscountAmount()
  339. {
  340. return $this->getData(InvoiceItemInterface::DISCOUNT_AMOUNT);
  341. }
  342. /**
  343. * Returns discount_tax_compensation_amount
  344. *
  345. * @return float|null
  346. */
  347. public function getDiscountTaxCompensationAmount()
  348. {
  349. return $this->getData(InvoiceItemInterface::DISCOUNT_TAX_COMPENSATION_AMOUNT);
  350. }
  351. /**
  352. * Returns name
  353. *
  354. * @return string|null
  355. */
  356. public function getName()
  357. {
  358. return $this->getData(InvoiceItemInterface::NAME);
  359. }
  360. /**
  361. * Returns order_item_id
  362. *
  363. * @return int
  364. */
  365. public function getOrderItemId()
  366. {
  367. return $this->getData(InvoiceItemInterface::ORDER_ITEM_ID);
  368. }
  369. /**
  370. * Returns parent_id
  371. *
  372. * @return int|null
  373. */
  374. public function getParentId()
  375. {
  376. return $this->getData(InvoiceItemInterface::PARENT_ID);
  377. }
  378. /**
  379. * Returns price
  380. *
  381. * @return float|null
  382. */
  383. public function getPrice()
  384. {
  385. return $this->getData(InvoiceItemInterface::PRICE);
  386. }
  387. /**
  388. * Returns price_incl_tax
  389. *
  390. * @return float|null
  391. */
  392. public function getPriceInclTax()
  393. {
  394. return $this->getData(InvoiceItemInterface::PRICE_INCL_TAX);
  395. }
  396. /**
  397. * Returns product_id
  398. *
  399. * @return int|null
  400. */
  401. public function getProductId()
  402. {
  403. return $this->getData(InvoiceItemInterface::PRODUCT_ID);
  404. }
  405. /**
  406. * Returns qty
  407. *
  408. * @return float
  409. */
  410. public function getQty()
  411. {
  412. return $this->getData(InvoiceItemInterface::QTY);
  413. }
  414. /**
  415. * Returns row_total
  416. *
  417. * @return float|null
  418. */
  419. public function getRowTotal()
  420. {
  421. return $this->getData(InvoiceItemInterface::ROW_TOTAL);
  422. }
  423. /**
  424. * Returns row_total_incl_tax
  425. *
  426. * @return float|null
  427. */
  428. public function getRowTotalInclTax()
  429. {
  430. return $this->getData(InvoiceItemInterface::ROW_TOTAL_INCL_TAX);
  431. }
  432. /**
  433. * Returns sku
  434. *
  435. * @return string
  436. */
  437. public function getSku()
  438. {
  439. return $this->getData(InvoiceItemInterface::SKU);
  440. }
  441. /**
  442. * Returns tax_amount
  443. *
  444. * @return float|null
  445. */
  446. public function getTaxAmount()
  447. {
  448. return $this->getData(InvoiceItemInterface::TAX_AMOUNT);
  449. }
  450. /**
  451. * {@inheritdoc}
  452. */
  453. public function setParentId($id)
  454. {
  455. return $this->setData(InvoiceItemInterface::PARENT_ID, $id);
  456. }
  457. /**
  458. * {@inheritdoc}
  459. */
  460. public function setBasePrice($price)
  461. {
  462. return $this->setData(InvoiceItemInterface::BASE_PRICE, $price);
  463. }
  464. /**
  465. * {@inheritdoc}
  466. */
  467. public function setTaxAmount($amount)
  468. {
  469. return $this->setData(InvoiceItemInterface::TAX_AMOUNT, $amount);
  470. }
  471. /**
  472. * {@inheritdoc}
  473. */
  474. public function setBaseRowTotal($amount)
  475. {
  476. return $this->setData(InvoiceItemInterface::BASE_ROW_TOTAL, $amount);
  477. }
  478. /**
  479. * {@inheritdoc}
  480. */
  481. public function setDiscountAmount($amount)
  482. {
  483. return $this->setData(InvoiceItemInterface::DISCOUNT_AMOUNT, $amount);
  484. }
  485. /**
  486. * {@inheritdoc}
  487. */
  488. public function setRowTotal($amount)
  489. {
  490. return $this->setData(InvoiceItemInterface::ROW_TOTAL, $amount);
  491. }
  492. /**
  493. * {@inheritdoc}
  494. */
  495. public function setBaseDiscountAmount($amount)
  496. {
  497. return $this->setData(InvoiceItemInterface::BASE_DISCOUNT_AMOUNT, $amount);
  498. }
  499. /**
  500. * {@inheritdoc}
  501. */
  502. public function setPriceInclTax($amount)
  503. {
  504. return $this->setData(InvoiceItemInterface::PRICE_INCL_TAX, $amount);
  505. }
  506. /**
  507. * {@inheritdoc}
  508. */
  509. public function setBaseTaxAmount($amount)
  510. {
  511. return $this->setData(InvoiceItemInterface::BASE_TAX_AMOUNT, $amount);
  512. }
  513. /**
  514. * {@inheritdoc}
  515. */
  516. public function setBasePriceInclTax($amount)
  517. {
  518. return $this->setData(InvoiceItemInterface::BASE_PRICE_INCL_TAX, $amount);
  519. }
  520. /**
  521. * {@inheritdoc}
  522. */
  523. public function setBaseCost($baseCost)
  524. {
  525. return $this->setData(InvoiceItemInterface::BASE_COST, $baseCost);
  526. }
  527. /**
  528. * {@inheritdoc}
  529. */
  530. public function setPrice($price)
  531. {
  532. return $this->setData(InvoiceItemInterface::PRICE, $price);
  533. }
  534. /**
  535. * {@inheritdoc}
  536. */
  537. public function setBaseRowTotalInclTax($amount)
  538. {
  539. return $this->setData(InvoiceItemInterface::BASE_ROW_TOTAL_INCL_TAX, $amount);
  540. }
  541. /**
  542. * {@inheritdoc}
  543. */
  544. public function setRowTotalInclTax($amount)
  545. {
  546. return $this->setData(InvoiceItemInterface::ROW_TOTAL_INCL_TAX, $amount);
  547. }
  548. /**
  549. * {@inheritdoc}
  550. */
  551. public function setProductId($id)
  552. {
  553. return $this->setData(InvoiceItemInterface::PRODUCT_ID, $id);
  554. }
  555. /**
  556. * {@inheritdoc}
  557. */
  558. public function setOrderItemId($id)
  559. {
  560. return $this->setData(InvoiceItemInterface::ORDER_ITEM_ID, $id);
  561. }
  562. /**
  563. * {@inheritdoc}
  564. */
  565. public function setAdditionalData($additionalData)
  566. {
  567. return $this->setData(InvoiceItemInterface::ADDITIONAL_DATA, $additionalData);
  568. }
  569. /**
  570. * {@inheritdoc}
  571. */
  572. public function setDescription($description)
  573. {
  574. return $this->setData(InvoiceItemInterface::DESCRIPTION, $description);
  575. }
  576. /**
  577. * {@inheritdoc}
  578. */
  579. public function setSku($sku)
  580. {
  581. return $this->setData(InvoiceItemInterface::SKU, $sku);
  582. }
  583. /**
  584. * {@inheritdoc}
  585. */
  586. public function setName($name)
  587. {
  588. return $this->setData(InvoiceItemInterface::NAME, $name);
  589. }
  590. /**
  591. * {@inheritdoc}
  592. */
  593. public function setDiscountTaxCompensationAmount($amount)
  594. {
  595. return $this->setData(InvoiceItemInterface::DISCOUNT_TAX_COMPENSATION_AMOUNT, $amount);
  596. }
  597. /**
  598. * {@inheritdoc}
  599. */
  600. public function setBaseDiscountTaxCompensationAmount($amount)
  601. {
  602. return $this->setData(InvoiceItemInterface::BASE_DISCOUNT_TAX_COMPENSATION_AMOUNT, $amount);
  603. }
  604. /**
  605. * {@inheritdoc}
  606. *
  607. * @return \Magento\Sales\Api\Data\InvoiceItemExtensionInterface|null
  608. */
  609. public function getExtensionAttributes()
  610. {
  611. return $this->_getExtensionAttributes();
  612. }
  613. /**
  614. * {@inheritdoc}
  615. *
  616. * @param \Magento\Sales\Api\Data\InvoiceItemExtensionInterface $extensionAttributes
  617. * @return $this
  618. */
  619. public function setExtensionAttributes(\Magento\Sales\Api\Data\InvoiceItemExtensionInterface $extensionAttributes)
  620. {
  621. return $this->_setExtensionAttributes($extensionAttributes);
  622. }
  623. //@codeCoverageIgnoreEnd
  624. }