PayOperation.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /**
  8. * Invoice pay operation.
  9. */
  10. class PayOperation
  11. {
  12. /**
  13. * @var \Magento\Framework\Event\ManagerInterface
  14. */
  15. private $eventManager;
  16. /**
  17. * @param \Magento\Framework\Model\Context $context
  18. */
  19. public function __construct(
  20. \Magento\Framework\Model\Context $context
  21. ) {
  22. $this->eventManager = $context->getEventDispatcher();
  23. }
  24. /**
  25. * @param \Magento\Sales\Api\Data\OrderInterface $order
  26. * @param \Magento\Sales\Api\Data\InvoiceInterface $invoice
  27. * @param bool $capture
  28. *
  29. * @return \Magento\Sales\Api\Data\OrderInterface
  30. */
  31. public function execute(
  32. \Magento\Sales\Api\Data\OrderInterface $order,
  33. \Magento\Sales\Api\Data\InvoiceInterface $invoice,
  34. $capture
  35. ) {
  36. $this->calculateOrderItemsTotals(
  37. $invoice->getItems()
  38. );
  39. if ($this->canCapture($order, $invoice)) {
  40. if ($capture) {
  41. $invoice->capture();
  42. } else {
  43. $invoice->setCanVoidFlag(false);
  44. $invoice->pay();
  45. }
  46. } elseif (!$order->getPayment()->getMethodInstance()->isGateway() || !$capture) {
  47. if (!$order->getPayment()->getIsTransactionPending()) {
  48. $invoice->setCanVoidFlag(false);
  49. $invoice->pay();
  50. }
  51. }
  52. $this->calculateOrderTotals($order, $invoice);
  53. if (null === $invoice->getState()) {
  54. $invoice->setState(\Magento\Sales\Model\Order\Invoice::STATE_OPEN);
  55. }
  56. $this->eventManager->dispatch(
  57. 'sales_order_invoice_register',
  58. ['invoice' => $invoice, 'order' => $order]
  59. );
  60. return $order;
  61. }
  62. /**
  63. * Calculates totals of Order Items according to given Invoice Items.
  64. *
  65. * @param \Magento\Sales\Api\Data\InvoiceItemInterface[] $items
  66. *
  67. * @return void
  68. */
  69. private function calculateOrderItemsTotals($items)
  70. {
  71. foreach ($items as $item) {
  72. if ($item->isDeleted()) {
  73. continue;
  74. }
  75. if ($item->getQty() > 0) {
  76. $item->register();
  77. } else {
  78. $item->isDeleted(true);
  79. }
  80. }
  81. }
  82. /**
  83. * Checks Invoice capture action availability.
  84. *
  85. * @param \Magento\Sales\Api\Data\OrderInterface $order
  86. * @param \Magento\Sales\Api\Data\InvoiceInterface $invoice
  87. *
  88. * @return bool
  89. */
  90. private function canCapture(
  91. \Magento\Sales\Api\Data\OrderInterface $order,
  92. \Magento\Sales\Api\Data\InvoiceInterface $invoice
  93. ) {
  94. return $invoice->getState() != \Magento\Sales\Model\Order\Invoice::STATE_CANCELED &&
  95. $invoice->getState() != \Magento\Sales\Model\Order\Invoice::STATE_PAID &&
  96. $order->getPayment()->canCapture();
  97. }
  98. /**
  99. * Calculates Order totals according to given Invoice.
  100. *
  101. * @param \Magento\Sales\Api\Data\OrderInterface $order
  102. * @param \Magento\Sales\Api\Data\InvoiceInterface $invoice
  103. *
  104. * @return void
  105. */
  106. private function calculateOrderTotals(
  107. \Magento\Sales\Api\Data\OrderInterface $order,
  108. \Magento\Sales\Api\Data\InvoiceInterface $invoice
  109. ) {
  110. $order->setTotalInvoiced(
  111. $order->getTotalInvoiced() + $invoice->getGrandTotal()
  112. );
  113. $order->setBaseTotalInvoiced(
  114. $order->getBaseTotalInvoiced() + $invoice->getBaseGrandTotal()
  115. );
  116. $order->setSubtotalInvoiced(
  117. $order->getSubtotalInvoiced() + $invoice->getSubtotal()
  118. );
  119. $order->setBaseSubtotalInvoiced(
  120. $order->getBaseSubtotalInvoiced() + $invoice->getBaseSubtotal()
  121. );
  122. $order->setTaxInvoiced(
  123. $order->getTaxInvoiced() + $invoice->getTaxAmount()
  124. );
  125. $order->setBaseTaxInvoiced(
  126. $order->getBaseTaxInvoiced() + $invoice->getBaseTaxAmount()
  127. );
  128. $order->setDiscountTaxCompensationInvoiced(
  129. $order->getDiscountTaxCompensationInvoiced() + $invoice->getDiscountTaxCompensationAmount()
  130. );
  131. $order->setBaseDiscountTaxCompensationInvoiced(
  132. $order->getBaseDiscountTaxCompensationInvoiced() + $invoice->getBaseDiscountTaxCompensationAmount()
  133. );
  134. $order->setShippingTaxInvoiced(
  135. $order->getShippingTaxInvoiced() + $invoice->getShippingTaxAmount()
  136. );
  137. $order->setBaseShippingTaxInvoiced(
  138. $order->getBaseShippingTaxInvoiced() + $invoice->getBaseShippingTaxAmount()
  139. );
  140. $order->setShippingInvoiced(
  141. $order->getShippingInvoiced() + $invoice->getShippingAmount()
  142. );
  143. $order->setBaseShippingInvoiced(
  144. $order->getBaseShippingInvoiced() + $invoice->getBaseShippingAmount()
  145. );
  146. $order->setDiscountInvoiced(
  147. $order->getDiscountInvoiced() + $invoice->getDiscountAmount()
  148. );
  149. $order->setBaseDiscountInvoiced(
  150. $order->getBaseDiscountInvoiced() + $invoice->getBaseDiscountAmount()
  151. );
  152. $order->setBaseTotalInvoicedCost(
  153. $order->getBaseTotalInvoicedCost() + $invoice->getBaseCost()
  154. );
  155. }
  156. }