1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\MagentoBundle\ImportExport\Strategy; |
4
|
|
|
|
5
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\CartStatus; |
6
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\Customer; |
7
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\Order; |
8
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\OrderAddress; |
9
|
|
|
use OroCRM\Bundle\MagentoBundle\Provider\MagentoConnectorInterface; |
10
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\OrderItem; |
11
|
|
|
|
12
|
|
|
class OrderStrategy extends AbstractImportStrategy |
13
|
|
|
{ |
14
|
|
|
const CONTEXT_ORDER_POST_PROCESS_IDS = 'postProcessOrderIds'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Order |
18
|
|
|
*/ |
19
|
|
|
protected $existingEntity; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Order $entity |
23
|
|
|
* |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
protected function beforeProcessEntity($entity) |
27
|
|
|
{ |
28
|
|
|
$this->existingEntity = $this->databaseHelper->findOneByIdentity($entity); |
29
|
|
|
if (!$this->existingEntity) { |
30
|
|
|
$this->existingEntity = $entity; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return parent::beforeProcessEntity($entity); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Order $entity |
38
|
|
|
* |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
protected function afterProcessEntity($entity) |
42
|
|
|
{ |
43
|
|
|
if (!$entity->getUpdatedAt() && $entity->getCreatedAt()) { |
44
|
|
|
$entity->setUpdatedAt($entity->getCreatedAt()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$now = new \DateTime('now', new \DateTimeZone('UTC')); |
48
|
|
|
if (!$entity->getImportedAt()) { |
49
|
|
|
$entity->setImportedAt($now); |
50
|
|
|
} |
51
|
|
|
$entity->setSyncedAt($now); |
52
|
|
|
|
53
|
|
|
/** @var Order $order */ |
54
|
|
|
$this->processCart($entity); |
55
|
|
|
$this->processItems($entity); |
56
|
|
|
$this->processAddresses($entity); |
57
|
|
|
$this->processCustomer($entity, $entity->getCustomer()); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$this->existingEntity = null; |
60
|
|
|
|
61
|
|
|
$this->appendDataToContext(self::CONTEXT_ORDER_POST_PROCESS_IDS, $entity->getIncrementId()); |
62
|
|
|
|
63
|
|
|
return parent::afterProcessEntity($entity); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Order $order |
68
|
|
|
* @param Customer $customer |
69
|
|
|
*/ |
70
|
|
|
protected function processCustomer(Order $order, Customer $customer = null) |
71
|
|
|
{ |
72
|
|
|
if (!$customer || !$customer->getId()) { |
73
|
|
|
$customer = $this->databaseHelper->findOneBy( |
74
|
|
|
'OroCRM\Bundle\MagentoBundle\Entity\Customer', |
75
|
|
|
[ |
76
|
|
|
'email' => $order->getCustomerEmail(), |
77
|
|
|
'channel' => $order->getChannel() |
78
|
|
|
] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($customer instanceof Customer) { |
83
|
|
|
// now customer orders subtotal calculation support only one currency. |
84
|
|
|
// also we do not take into account order refunds due to magento does not bring subtotal data |
85
|
|
|
// customer currency needs on customer's grid to format lifetime value. |
86
|
|
|
$customer->setCurrency($order->getCurrency()); |
87
|
|
|
} |
88
|
|
|
$order->setCustomer($customer); |
|
|
|
|
89
|
|
|
|
90
|
|
|
if ($order->getCart()) { |
91
|
|
|
$order->getCart()->setCustomer($customer); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* If cart exists then add relation to it, |
97
|
|
|
* do nothing otherwise |
98
|
|
|
* |
99
|
|
|
* @param Order $entity |
100
|
|
|
*/ |
101
|
|
|
protected function processCart(Order $entity) |
102
|
|
|
{ |
103
|
|
|
$cart = $entity->getCart(); |
104
|
|
|
|
105
|
|
|
if ($cart) { |
106
|
|
|
$statusClass = MagentoConnectorInterface::CART_STATUS_TYPE; |
107
|
|
|
/** @var CartStatus $purchasedStatus */ |
108
|
|
|
$purchasedStatus = $this->databaseHelper |
109
|
|
|
->findOneBy($statusClass, ['name' => CartStatus::STATUS_PURCHASED]); |
110
|
|
|
if ($purchasedStatus) { |
111
|
|
|
$cart->setStatus($purchasedStatus); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$entity->setCart($cart); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Order $order |
120
|
|
|
* |
121
|
|
|
* @return OrderStrategy |
122
|
|
|
*/ |
123
|
|
|
protected function processItems(Order $order) |
124
|
|
|
{ |
125
|
|
|
foreach ($order->getItems() as $item) { |
126
|
|
|
$item->setOwner($order->getOrganization()); |
127
|
|
|
$item->setOrder($order); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param Order $order |
135
|
|
|
* |
136
|
|
|
* @return OrderStrategy |
137
|
|
|
*/ |
138
|
|
|
protected function processAddresses(Order $order) |
139
|
|
|
{ |
140
|
|
|
/** @var OrderAddress $address */ |
141
|
|
|
foreach ($order->getAddresses() as $address) { |
142
|
|
|
$address->setOwner($order); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* BC layer to find existing collection items by old identity filed values |
150
|
|
|
* |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
protected function findExistingEntity($entity, array $searchContext = []) |
154
|
|
|
{ |
155
|
|
|
$existingEntity = parent::findExistingEntity($entity, $searchContext); |
156
|
|
|
|
157
|
|
|
if (!$existingEntity && $entity instanceof OrderAddress) { |
158
|
|
|
/** @var OrderAddress $existingEntity */ |
159
|
|
|
$existingEntity = $this->existingEntity->getAddresses() |
160
|
|
|
->filter( |
161
|
|
|
function (OrderAddress $address) use ($entity) { |
162
|
|
|
$isMatched = true; |
163
|
|
|
$fieldsToMatch = ['street', 'city', 'postalCode', 'country', 'region']; |
164
|
|
|
|
165
|
|
|
foreach ($fieldsToMatch as $fieldToMatch) { |
166
|
|
|
$addressValue = $this->getPropertyAccessor()->getValue($address, $fieldToMatch); |
167
|
|
|
$entityValue = $this->getPropertyAccessor()->getValue($entity, $fieldToMatch); |
168
|
|
|
$isMatched = $isMatched && ($addressValue === $entityValue); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $isMatched; |
172
|
|
|
} |
173
|
|
|
) |
174
|
|
|
->first(); |
175
|
|
|
|
176
|
|
|
if ($existingEntity && $entity->getOriginId()) { |
177
|
|
|
$existingEntity->setOriginId($entity->getOriginId()); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($entity instanceof OrderItem && is_null($entity->getName())) { |
182
|
|
|
//name can't be null, so to avoid import job failing empty string is used |
183
|
|
|
$entity->setName(''); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $existingEntity; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: