Skip to content

Commit

Permalink
Merge pull request #217 from magento-firedrakes/MAGETWO-55695
Browse files Browse the repository at this point in the history
[Firedrakes] Remove saving order from Creditmemo repository save
  • Loading branch information
magicbunneh authored Aug 5, 2016
2 parents 0878622 + ee19602 commit 9485e36
Show file tree
Hide file tree
Showing 10 changed files with 766 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function execute()
$creditmemoManagement = $this->_objectManager->create(
\Magento\Sales\Api\CreditmemoManagementInterface::class
);
$creditmemoManagement->refund($creditmemo, (bool)$data['do_offline'], !empty($data['send_email']));
$creditmemoManagement->refund($creditmemo, (bool)$data['do_offline']);

if (!empty($data['send_email'])) {
$this->creditmemoSender->send($creditmemo);
Expand Down
126 changes: 126 additions & 0 deletions app/code/Magento/Sales/Model/Order/Creditmemo/RefundOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Model\Order\Creditmemo;

use Magento\Sales\Api\Data\CreditmemoInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order\Creditmemo;

class RefundOperation
{
/**
* @var \Magento\Framework\Event\ManagerInterface
*/
private $eventManager;

/**
* @var \Magento\Framework\Pricing\PriceCurrencyInterface
*/
private $priceCurrency;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
) {
$this->eventManager = $context->getEventDispatcher();
$this->priceCurrency = $priceCurrency;
}

/**
* @param CreditmemoInterface $creditmemo
* @param OrderInterface $order
* @param bool $online
* @return OrderInterface
*/
public function execute(CreditmemoInterface $creditmemo, OrderInterface $order, $online = false)
{
if ($creditmemo->getState() == Creditmemo::STATE_REFUNDED
&& $creditmemo->getOrderId() == $order->getEntityId()
) {
foreach ($creditmemo->getItems() as $item) {
if ($item->isDeleted()) {
continue;
}
$item->setCreditMemo($creditmemo);
if ($item->getQty() > 0) {
$item->register();
} else {
$item->isDeleted(true);
}
}

$baseOrderRefund = $this->priceCurrency->round(
$order->getBaseTotalRefunded() + $creditmemo->getBaseGrandTotal()
);
$orderRefund = $this->priceCurrency->round(
$order->getTotalRefunded() + $creditmemo->getGrandTotal()
);
$order->setBaseTotalRefunded($baseOrderRefund);
$order->setTotalRefunded($orderRefund);

$order->setBaseSubtotalRefunded($order->getBaseSubtotalRefunded() + $creditmemo->getBaseSubtotal());
$order->setSubtotalRefunded($order->getSubtotalRefunded() + $creditmemo->getSubtotal());

$order->setBaseTaxRefunded($order->getBaseTaxRefunded() + $creditmemo->getBaseTaxAmount());
$order->setTaxRefunded($order->getTaxRefunded() + $creditmemo->getTaxAmount());
$order->setBaseDiscountTaxCompensationRefunded(
$order->getBaseDiscountTaxCompensationRefunded() + $creditmemo->getBaseDiscountTaxCompensationAmount()
);
$order->setDiscountTaxCompensationRefunded(
$order->getDiscountTaxCompensationRefunded() + $creditmemo->getDiscountTaxCompensationAmount()
);

$order->setBaseShippingRefunded($order->getBaseShippingRefunded() + $creditmemo->getBaseShippingAmount());
$order->setShippingRefunded($order->getShippingRefunded() + $creditmemo->getShippingAmount());

$order->setBaseShippingTaxRefunded(
$order->getBaseShippingTaxRefunded() + $creditmemo->getBaseShippingTaxAmount()
);
$order->setShippingTaxRefunded($order->getShippingTaxRefunded() + $creditmemo->getShippingTaxAmount());

$order->setAdjustmentPositive($order->getAdjustmentPositive() + $creditmemo->getAdjustmentPositive());
$order->setBaseAdjustmentPositive(
$order->getBaseAdjustmentPositive() + $creditmemo->getBaseAdjustmentPositive()
);

$order->setAdjustmentNegative($order->getAdjustmentNegative() + $creditmemo->getAdjustmentNegative());
$order->setBaseAdjustmentNegative(
$order->getBaseAdjustmentNegative() + $creditmemo->getBaseAdjustmentNegative()
);

$order->setDiscountRefunded($order->getDiscountRefunded() + $creditmemo->getDiscountAmount());
$order->setBaseDiscountRefunded($order->getBaseDiscountRefunded() + $creditmemo->getBaseDiscountAmount());

if ($online) {
$order->setTotalOnlineRefunded($order->getTotalOnlineRefunded() + $creditmemo->getGrandTotal());
$order->setBaseTotalOnlineRefunded(
$order->getBaseTotalOnlineRefunded() + $creditmemo->getBaseGrandTotal()
);
} else {
$order->setTotalOfflineRefunded($order->getTotalOfflineRefunded() + $creditmemo->getGrandTotal());
$order->setBaseTotalOfflineRefunded(
$order->getBaseTotalOfflineRefunded() + $creditmemo->getBaseGrandTotal()
);
}

$order->setBaseTotalInvoicedCost(
$order->getBaseTotalInvoicedCost() - $creditmemo->getBaseCost()
);

if ($online) {
$order->getPayment()->refund($creditmemo);
}

$this->eventManager->dispatch('sales_order_creditmemo_refund', ['creditmemo' => $creditmemo]);
}

return $order;
}
}
20 changes: 20 additions & 0 deletions app/code/Magento/Sales/Model/Order/PaymentAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,40 @@
*/
class PaymentAdapter implements PaymentAdapterInterface
{
/**
* @var \Magento\Sales\Model\Order\Creditmemo\RefundOperation
*/
private $refundOperation;

/**
* @var \Magento\Sales\Model\Order\Invoice\PayOperation
*/
private $payOperation;

/**
* PaymentAdapter constructor.
* @param \Magento\Sales\Model\Order\Creditmemo\RefundOperation $refundOperation
* @param \Magento\Sales\Model\Order\Invoice\PayOperation $payOperation
*/
public function __construct(
\Magento\Sales\Model\Order\Creditmemo\RefundOperation $refundOperation,
\Magento\Sales\Model\Order\Invoice\PayOperation $payOperation
) {
$this->refundOperation = $refundOperation;
$this->payOperation = $payOperation;
}

/**
* {@inheritdoc}
*/
public function refund(
\Magento\Sales\Api\Data\CreditmemoInterface $creditmemo,
\Magento\Sales\Api\Data\OrderInterface $order,
$isOnline = false
) {
return $this->refundOperation->execute($creditmemo, $order, $isOnline);
}

/**
* {@inheritdoc}
*/
Expand Down
12 changes: 12 additions & 0 deletions app/code/Magento/Sales/Model/Order/PaymentAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@ interface PaymentAdapterInterface
* @return OrderInterface
*/
public function pay(OrderInterface $order, InvoiceInterface $invoice, $capture);

/**
* @param \Magento\Sales\Api\Data\CreditmemoInterface $creditmemo
* @param \Magento\Sales\Api\Data\OrderInterface $order
* @param bool $isOnline
* @return \Magento\Sales\Api\Data\OrderInterface
*/
public function refund(
\Magento\Sales\Api\Data\CreditmemoInterface $creditmemo,
\Magento\Sales\Api\Data\OrderInterface $order,
$isOnline = false
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

/**
* Class Relation
* @deprecated
*/
class Refund implements RelationInterface
{
Expand Down
110 changes: 100 additions & 10 deletions app/code/Magento/Sales/Model/Service/CreditmemoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/**
* Class CreditmemoService
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CreditmemoService implements \Magento\Sales\Api\CreditmemoManagementInterface
{
Expand Down Expand Up @@ -46,6 +47,26 @@ class CreditmemoService implements \Magento\Sales\Api\CreditmemoManagementInterf
*/
protected $eventManager;

/**
* @var \Magento\Framework\App\ResourceConnection
*/
private $resource;

/**
* @var \Magento\Sales\Model\Order\PaymentAdapterInterface
*/
private $paymentAdapter;

/**
* @var \Magento\Sales\Api\OrderRepositoryInterface
*/
private $orderRepository;

/**
* @var \Magento\Sales\Api\InvoiceRepositoryInterface
*/
private $invoiceRepository;

/**
* @param \Magento\Sales\Api\CreditmemoRepositoryInterface $creditmemoRepository
* @param \Magento\Sales\Api\CreditmemoCommentRepositoryInterface $creditmemoCommentRepository
Expand Down Expand Up @@ -130,6 +151,7 @@ public function notify($id)
* @param \Magento\Sales\Api\Data\CreditmemoInterface $creditmemo
* @param bool $offlineRequested
* @return \Magento\Sales\Api\Data\CreditmemoInterface
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function refund(
\Magento\Sales\Api\Data\CreditmemoInterface $creditmemo,
Expand All @@ -138,19 +160,31 @@ public function refund(
$this->validateForRefund($creditmemo);
$creditmemo->setState(\Magento\Sales\Model\Order\Creditmemo::STATE_REFUNDED);

foreach ($creditmemo->getAllItems() as $item) {
$item->setCreditMemo($creditmemo);
if ($item->getQty() > 0) {
$item->register();
} else {
$item->isDeleted(true);
$connection = $this->getResource()->getConnection('sales');
$connection->beginTransaction();
try {
$order = $this->getPaymentAdapter()->refund(
$creditmemo,
$creditmemo->getOrder(),
!$offlineRequested
);
$this->getOrderRepository()->save($order);
$invoice = $creditmemo->getInvoice();
if ($invoice && !$offlineRequested) {
$invoice->setIsUsedForRefund(true);
$invoice->setBaseTotalRefunded(
$invoice->getBaseTotalRefunded() + $creditmemo->getBaseGrandTotal()
);
$creditmemo->setInvoiceId($invoice->getId());
$this->getInvoiceRepository()->save($creditmemo->getInvoice());
}
$this->creditmemoRepository->save($creditmemo);
$connection->commit();
} catch (\Exception $e) {
$connection->rollBack();
throw new \Magento\Framework\Exception\LocalizedException($e->getMessage());
}

$creditmemo->setDoTransaction(!$offlineRequested);

$this->eventManager->dispatch('sales_order_creditmemo_refund', ['creditmemo' => $creditmemo]);
$this->creditmemoRepository->save($creditmemo);
return $creditmemo;
}

Expand Down Expand Up @@ -183,4 +217,60 @@ protected function validateForRefund(\Magento\Sales\Api\Data\CreditmemoInterface
}
return true;
}

/**
* @return \Magento\Sales\Model\Order\PaymentAdapterInterface
*
* @deprecated
*/
private function getPaymentAdapter()
{
if ($this->paymentAdapter === null) {
$this->paymentAdapter = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Sales\Model\Order\PaymentAdapterInterface::class);
}
return $this->paymentAdapter;
}

/**
* @return \Magento\Framework\App\ResourceConnection|mixed
*
* @deprecated
*/
private function getResource()
{
if ($this->resource === null) {
$this->resource = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\App\ResourceConnection::class);
}
return $this->resource;
}

/**
* @return \Magento\Sales\Api\OrderRepositoryInterface
*
* @deprecated
*/
private function getOrderRepository()
{
if ($this->orderRepository === null) {
$this->orderRepository = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Sales\Api\OrderRepositoryInterface::class);
}
return $this->orderRepository;
}

/**
* @return \Magento\Sales\Api\InvoiceRepositoryInterface
*
* @deprecated
*/
private function getInvoiceRepository()
{
if ($this->invoiceRepository === null) {
$this->invoiceRepository = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Sales\Api\InvoiceRepositoryInterface::class);
}
return $this->invoiceRepository;
}
}
Loading

0 comments on commit 9485e36

Please sign in to comment.