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