forked from m2epro/magento2-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreditMemo.php
145 lines (112 loc) · 5.02 KB
/
CreditMemo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/*
* @author M2E Pro Developers Team
* @copyright M2E LTD
* @license Commercial use is forbidden
*/
namespace Ess\M2ePro\Observer;
class CreditMemo extends AbstractModel
{
protected $amazonFactory;
protected $urlBuilder;
protected $messageManager;
//########################################
public function __construct(
\Ess\M2ePro\Model\ActiveRecord\Component\Parent\Amazon\Factory $amazonFactory,
\Magento\Framework\UrlInterface $urlBuilder,
\Magento\Framework\Message\Manager $messageManager,
\Ess\M2ePro\Helper\Factory $helperFactory,
\Ess\M2ePro\Model\ActiveRecord\Factory $activeRecordFactory,
\Ess\M2ePro\Model\Factory $modelFactory
)
{
$this->amazonFactory = $amazonFactory;
$this->urlBuilder = $urlBuilder;
$this->messageManager = $messageManager;
parent::__construct($helperFactory, $activeRecordFactory, $modelFactory);
}
//########################################
public function process()
{
try {
/** @var \Magento\Sales\Model\Order\Creditmemo $creditmemo */
$creditmemo = $this->getEvent()->getCreditmemo();
$magentoOrderId = $creditmemo->getOrderId();
try {
/** @var $order \Ess\M2ePro\Model\Order */
$order = $this->activeRecordFactory->getObjectLoaded('Order', $magentoOrderId, 'magento_order_id');
} catch (\Exception $e) {
return;
}
if (is_null($order)) {
return;
}
if ($order->getComponentMode() == \Ess\M2ePro\Helper\Component\Walmart::NICK) {
$this->modelFactory->getObject('Walmart\Order\CreditMemo\Handler')->handle($order, $creditmemo);
return;
}
if ($order->getComponentMode() != \Ess\M2ePro\Helper\Component\Amazon::NICK) {
return;
}
/** @var \Ess\M2ePro\Model\Amazon\Order $amazonOrder */
$amazonOrder = $order->getChildObject();
if (!$amazonOrder->canRefund()) {
return;
}
$order->getLog()->setInitiator(\Ess\M2ePro\Helper\Data::INITIATOR_EXTENSION);
$itemsForCancel = array();
foreach ($creditmemo->getAllItems() as $creditmemoItem) {
/** @var \Magento\Sales\Model\Order\Creditmemo\Item $creditmemoItem */
$additionalData = $creditmemoItem->getOrderItem()->getAdditionalData();
if (!is_string($additionalData)) {
continue;
}
$additionalData = @unserialize($additionalData);
if (!is_array($additionalData) ||
empty($additionalData[\Ess\M2ePro\Helper\Data::CUSTOM_IDENTIFIER]['items'])
) {
continue;
}
foreach ($additionalData[\Ess\M2ePro\Helper\Data::CUSTOM_IDENTIFIER]['items'] as $item) {
$amazonOrderItemId = $item['order_item_id'];
if (in_array($amazonOrderItemId, $itemsForCancel)) {
continue;
}
$amazonOrderItemCollection = $this->amazonFactory
->getObject('Order\Item')
->getCollection();
$amazonOrderItemCollection->addFieldToFilter('amazon_order_item_id', $amazonOrderItemId);
/** @var \Ess\M2ePro\Model\Order\Item $orderItem */
$orderItem = $amazonOrderItemCollection->getFirstItem();
if (is_null($orderItem) || !$orderItem->getId()) {
continue;
}
/** @var \Ess\M2ePro\Model\Amazon\Order\Item $amazonOrderItem */
$amazonOrderItem = $orderItem->getChildObject();
$price = $creditmemoItem->getPriceInclTax();
if ($price > $amazonOrderItem->getPrice()) {
$price = $amazonOrderItem->getPrice();
}
$tax = $creditmemoItem->getTaxAmount();
if ($tax > $amazonOrderItem->getTaxAmount()) {
$tax = $amazonOrderItem->getTaxAmount();
}
$itemsForCancel[] = array(
'item_id' => $amazonOrderItemId,
'qty' => $creditmemoItem->getQty(),
'prices' => array(
'product' => $price,
),
'taxes' => array(
'product' => $tax,
),
);
}
}
$amazonOrder->refund($itemsForCancel);
} catch (\Exception $exception) {
$this->getHelper('Module\Exception')->process($exception);
}
}
//########################################
}