Subtotal.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\Total;
  7. class Subtotal extends AbstractTotal
  8. {
  9. /**
  10. * Collect invoice subtotal
  11. *
  12. * @param \Magento\Sales\Model\Order\Invoice $invoice
  13. * @return $this
  14. */
  15. public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
  16. {
  17. $subtotal = 0;
  18. $baseSubtotal = 0;
  19. $subtotalInclTax = 0;
  20. $baseSubtotalInclTax = 0;
  21. $order = $invoice->getOrder();
  22. foreach ($invoice->getAllItems() as $item) {
  23. if ($item->getOrderItem()->isDummy()) {
  24. continue;
  25. }
  26. $item->calcRowTotal();
  27. $subtotal += $item->getRowTotal();
  28. $baseSubtotal += $item->getBaseRowTotal();
  29. $subtotalInclTax += $item->getRowTotalInclTax();
  30. $baseSubtotalInclTax += $item->getBaseRowTotalInclTax();
  31. }
  32. $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced();
  33. $baseAllowedSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced();
  34. //Note: The $subtotalInclTax and $baseSubtotalInclTax are not adjusted from those provide by the line items
  35. //because the "InclTax" is displayed before any tax adjustments based on discounts, shipping, etc.
  36. if ($invoice->isLast()) {
  37. $subtotal = $allowedSubtotal;
  38. $baseSubtotal = $baseAllowedSubtotal;
  39. } else {
  40. $subtotal = min($allowedSubtotal, $subtotal);
  41. $baseSubtotal = min($baseAllowedSubtotal, $baseSubtotal);
  42. }
  43. $invoice->setSubtotal($subtotal);
  44. $invoice->setBaseSubtotal($baseSubtotal);
  45. $invoice->setSubtotalInclTax($subtotalInclTax);
  46. $invoice->setBaseSubtotalInclTax($baseSubtotalInclTax);
  47. $invoice->setGrandTotal($invoice->getGrandTotal() + $subtotal);
  48. $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseSubtotal);
  49. return $this;
  50. }
  51. }