1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oro\Bundle\DemoDataBundle\Migrations\Data\Demo\ORM; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\DataFixtures\AbstractFixture; |
6
|
|
|
use Doctrine\Common\DataFixtures\DependentFixtureInterface; |
7
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
12
|
|
|
|
13
|
|
|
use Oro\Bundle\AddressBundle\Entity\Country; |
14
|
|
|
use Oro\Bundle\AddressBundle\Entity\Region; |
15
|
|
|
use Oro\Bundle\IntegrationBundle\Entity\Channel as Integration; |
16
|
|
|
use Oro\Bundle\OrganizationBundle\Entity\Organization; |
17
|
|
|
use Oro\Bundle\UserBundle\Entity\User; |
18
|
|
|
use Oro\Bundle\AnalyticsBundle\Entity\RFMMetricCategory; |
19
|
|
|
use Oro\Bundle\ChannelBundle\Builder\BuilderFactory; |
20
|
|
|
use Oro\Bundle\ChannelBundle\Entity\Channel; |
21
|
|
|
use Oro\Bundle\ContactBundle\Entity\Contact; |
22
|
|
|
use Oro\Bundle\MagentoBundle\Entity\Cart; |
23
|
|
|
use Oro\Bundle\MagentoBundle\Entity\CartItem; |
24
|
|
|
use Oro\Bundle\MagentoBundle\Entity\CartStatus; |
25
|
|
|
use Oro\Bundle\MagentoBundle\Entity\Customer; |
26
|
|
|
use Oro\Bundle\MagentoBundle\Entity\CustomerGroup; |
27
|
|
|
use Oro\Bundle\MagentoBundle\Entity\MagentoSoapTransport; |
28
|
|
|
use Oro\Bundle\MagentoBundle\Entity\Order; |
29
|
|
|
use Oro\Bundle\MagentoBundle\Entity\OrderAddress; |
30
|
|
|
use Oro\Bundle\MagentoBundle\Entity\OrderItem; |
31
|
|
|
use Oro\Bundle\MagentoBundle\Entity\Store; |
32
|
|
|
use Oro\Bundle\MagentoBundle\Entity\Website; |
33
|
|
|
use Oro\Bundle\MagentoBundle\Provider\Transport\SoapTransport; |
34
|
|
|
|
35
|
|
|
class LoadMagentoData extends AbstractFixture implements ContainerAwareInterface, DependentFixtureInterface |
36
|
|
|
{ |
37
|
|
|
use ContainerAwareTrait; |
38
|
|
|
|
39
|
|
|
const TAX = 0.0838; |
40
|
|
|
|
41
|
|
|
/** @var array */ |
42
|
|
|
protected $users; |
43
|
|
|
|
44
|
|
|
/** @var array */ |
45
|
|
|
protected static $websiteCodes = ['admin', 'admin2']; |
46
|
|
|
|
47
|
|
|
/** @var array */ |
48
|
|
|
protected static $integrationNames = ['Demo Web store', 'Demo Web store 2']; |
49
|
|
|
|
50
|
|
|
/** @var array */ |
51
|
|
|
protected static $groupNames = ['General', 'Secondary']; |
52
|
|
|
|
53
|
|
|
/** @var array */ |
54
|
|
|
protected static $channelNames = ['Magento channel', 'Magento channel 2']; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function getDependencies() |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
'Oro\Bundle\DemoDataBundle\Migrations\Data\Demo\ORM\LoadContactData' |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritDoc} |
68
|
|
|
*/ |
69
|
|
|
public function load(ObjectManager $om) |
70
|
|
|
{ |
71
|
|
|
$organization = $this->getReference('default_organization'); |
72
|
|
|
$this->users = $om->getRepository('OroUserBundle:User')->findAll(); |
73
|
|
|
|
74
|
|
|
$stores = $this->persistDemoStores($om, $organization); |
75
|
|
|
foreach ($stores as $store) { |
76
|
|
|
$channel = $this->persistDemoChannel($om, $store->getChannel()); |
77
|
|
|
$group = $this->persistDemoUserGroup($om, $store->getChannel()); |
78
|
|
|
$customers = $this->persistDemoCustomers($om, $store, $group, $channel, $organization); |
79
|
|
|
$carts = $this->persistDemoCarts($om, $customers); |
80
|
|
|
$this->persistDemoOrders($om, $carts); |
81
|
|
|
$this->persistDemoRFM($om, $channel, $organization); |
82
|
|
|
|
83
|
|
|
$om->flush(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param ObjectManager $om |
89
|
|
|
* @param Organization $organization |
90
|
|
|
* |
91
|
|
|
* @return Store[] |
92
|
|
|
*/ |
93
|
|
|
protected function persistDemoStores(ObjectManager $om, Organization $organization) |
94
|
|
|
{ |
95
|
|
|
$countOfWebsites = count(static::$websiteCodes); |
96
|
|
|
$stores = []; |
97
|
|
|
|
98
|
|
|
for ($i = 0; $i < $countOfWebsites; $i++) { |
99
|
|
|
$code = static::$websiteCodes[$i]; |
100
|
|
|
$integration = $this->persistDemoIntegration($om, $organization); |
101
|
|
|
|
102
|
|
|
$website = new Website(); |
103
|
|
|
$website |
104
|
|
|
->setCode($code) |
105
|
|
|
->setName(ucfirst($code)); |
106
|
|
|
$om->persist($website); |
107
|
|
|
|
108
|
|
|
$store = new Store(); |
109
|
|
|
$store |
110
|
|
|
->setCode($website->getCode()) |
111
|
|
|
->setName($website->getName()) |
112
|
|
|
->setChannel($integration) |
113
|
|
|
->setWebsite($website); |
114
|
|
|
$om->persist($store); |
115
|
|
|
$stores[] = $store; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $stores; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param ObjectManager $om |
123
|
|
|
* @param Organization $organization |
124
|
|
|
* |
125
|
|
|
* @return Integration |
126
|
|
|
*/ |
127
|
|
|
protected function persistDemoIntegration(ObjectManager $om, Organization $organization) |
128
|
|
|
{ |
129
|
|
|
static $i = 0; |
130
|
|
|
|
131
|
|
|
$transport = new MagentoSoapTransport(); |
132
|
|
|
$transport->setApiUser('api_user'); |
133
|
|
|
$transport->setApiKey('api_key'); |
134
|
|
|
$transport->setExtensionVersion(SoapTransport::REQUIRED_EXTENSION_VERSION); |
135
|
|
|
$transport->setIsExtensionInstalled(true); |
136
|
|
|
$transport->setMagentoVersion('1.9.1.0'); |
137
|
|
|
$transport->setWsdlUrl('http://magento.domain'); |
138
|
|
|
$om->persist($transport); |
139
|
|
|
|
140
|
|
|
$integrationName = static::$integrationNames[$i++]; |
141
|
|
|
|
142
|
|
|
$integration = new Integration(); |
143
|
|
|
$integration->setType('magento'); |
144
|
|
|
$integration->setConnectors(['customer', 'cart', 'order', 'newsletter_subscriber']); |
145
|
|
|
$integration->setName($integrationName); |
146
|
|
|
$integration->setTransport($transport); |
147
|
|
|
$integration->setOrganization($organization); |
148
|
|
|
$om->persist($integration); |
149
|
|
|
|
150
|
|
|
return $integration; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param ObjectManager $om |
155
|
|
|
* @param Integration $integration |
156
|
|
|
* |
157
|
|
|
* @return Channel |
158
|
|
|
*/ |
159
|
|
|
protected function persistDemoChannel(ObjectManager $om, Integration $integration) |
160
|
|
|
{ |
161
|
|
|
static $i = 0; |
162
|
|
|
|
163
|
|
|
$name = static::$channelNames[$i++]; |
164
|
|
|
|
165
|
|
|
/** @var $factory BuilderFactory */ |
166
|
|
|
$factory = $this->container->get('oro_channel.builder.factory'); |
167
|
|
|
$builder = $factory->createBuilderForIntegration($integration); |
168
|
|
|
$builder->setOwner($integration->getOrganization()); |
169
|
|
|
$builder->setDataSource($integration); |
170
|
|
|
$builder->setStatus($integration->isEnabled() ? Channel::STATUS_ACTIVE : Channel::STATUS_INACTIVE); |
171
|
|
|
$builder->setName($name); |
172
|
|
|
$dataChannel = $builder->getChannel(); |
173
|
|
|
$om->persist($dataChannel); |
174
|
|
|
|
175
|
|
|
return $dataChannel; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param ObjectManager $om |
180
|
|
|
* @param Integration $integration |
181
|
|
|
* |
182
|
|
|
* @return CustomerGroup |
183
|
|
|
*/ |
184
|
|
|
protected function persistDemoUserGroup(ObjectManager $om, Integration $integration) |
185
|
|
|
{ |
186
|
|
|
static $i = 0; |
187
|
|
|
|
188
|
|
|
$groupName = static::$groupNames[$i++]; |
189
|
|
|
|
190
|
|
|
$group = new CustomerGroup(); |
191
|
|
|
$group->setName($groupName); |
192
|
|
|
$group->setOriginId(14999 + $i); |
193
|
|
|
$group->setChannel($integration); |
194
|
|
|
$om->persist($group); |
195
|
|
|
|
196
|
|
|
return $group; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param ObjectManager $om |
201
|
|
|
* @param Channel $dataChannel |
202
|
|
|
* @param Organization $organization |
203
|
|
|
*/ |
204
|
|
|
protected function persistDemoRFM(ObjectManager $om, Channel $dataChannel, Organization $organization) |
205
|
|
|
{ |
206
|
|
|
$rfmData = [ |
207
|
|
|
'recency' => [ |
208
|
|
|
['min' => null, 'max' => 7], |
209
|
|
|
['min' => 7, 'max' => 30], |
210
|
|
|
['min' => 30, 'max' => 90], |
211
|
|
|
['min' => 90, 'max' => 365], |
212
|
|
|
['min' => 365, 'max' => null], |
213
|
|
|
], |
214
|
|
|
'frequency' => [ |
215
|
|
|
['min' => 52, 'max' => null], |
216
|
|
|
['min' => 12, 'max' => 52], |
217
|
|
|
['min' => 4, 'max' => 12], |
218
|
|
|
['min' => 2, 'max' => 4], |
219
|
|
|
['min' => null, 'max' => 2], |
220
|
|
|
], |
221
|
|
|
'monetary' => [ |
222
|
|
|
['min' => 10000, 'max' => null], |
223
|
|
|
['min' => 1000, 'max' => 10000], |
224
|
|
|
['min' => 100, 'max' => 1000], |
225
|
|
|
['min' => 10, 'max' => 100], |
226
|
|
|
['min' => null, 'max' => 10], |
227
|
|
|
] |
228
|
|
|
]; |
229
|
|
|
|
230
|
|
|
foreach ($rfmData as $type => $values) { |
231
|
|
|
foreach ($values as $idx => $limits) { |
232
|
|
|
$category = new RFMMetricCategory(); |
233
|
|
|
$category->setCategoryIndex($idx + 1) |
234
|
|
|
->setChannel($dataChannel) |
235
|
|
|
->setCategoryType($type) |
236
|
|
|
->setMinValue($limits['min']) |
237
|
|
|
->setMaxValue($limits['max']) |
238
|
|
|
->setOwner($organization); |
239
|
|
|
|
240
|
|
|
$om->persist($category); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$data = $dataChannel->getData(); |
245
|
|
|
$data['rfm_enabled'] = true; |
246
|
|
|
$dataChannel->setData($data); |
247
|
|
|
$om->persist($dataChannel); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param ObjectManager $om |
252
|
|
|
* @param Customer[] $customers |
253
|
|
|
* |
254
|
|
|
* @return Cart[] |
255
|
|
|
*/ |
256
|
|
|
protected function persistDemoCarts(ObjectManager $om, array $customers) |
257
|
|
|
{ |
258
|
|
|
/** @var CartStatus $status */ |
259
|
|
|
$status = $om->getRepository('OroMagentoBundle:CartStatus')->findOneBy(['name' => 'open']); |
260
|
|
|
|
261
|
|
|
$carts = []; |
262
|
|
|
for ($i = 0; $i < 10; ++$i) { |
263
|
|
|
$customer = $customers[array_rand($customers)]; |
264
|
|
|
$cart = $this->generateShoppingCart( |
265
|
|
|
$om, |
266
|
|
|
$customer->getStore(), |
267
|
|
|
$customer->getDataChannel(), |
268
|
|
|
$customer, |
269
|
|
|
$status, |
270
|
|
|
$i |
271
|
|
|
); |
272
|
|
|
$this->generateShoppingCartItem($om, $cart); |
273
|
|
|
$carts[] = $cart; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return $carts; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param ObjectManager $om |
281
|
|
|
* @param Cart[] $carts |
282
|
|
|
*/ |
283
|
|
|
protected function persistDemoOrders(ObjectManager $om, array $carts) |
284
|
|
|
{ |
285
|
|
|
$paymentMethod = ['Ccsave', 'Checkmo']; |
286
|
|
|
$paymentMethodDetails = ['Card[MC]', 'Card[AE]', 'N/A']; |
287
|
|
|
$status = ['Pending', 'Processing', 'Completed', 'Canceled']; |
288
|
|
|
$i = 0; |
289
|
|
|
foreach ($carts as $cart) { |
290
|
|
|
/** @var Cart $cart */ |
291
|
|
|
$order = $this->generateOrder( |
292
|
|
|
$om, |
293
|
|
|
$cart->getStore(), |
294
|
|
|
$cart->getDataChannel(), |
295
|
|
|
$cart->getCustomer(), |
|
|
|
|
296
|
|
|
$status[array_rand($status)], |
297
|
|
|
$cart, |
298
|
|
|
$paymentMethod[array_rand($paymentMethod)], |
299
|
|
|
$paymentMethodDetails[array_rand($paymentMethodDetails)], |
300
|
|
|
$i++ |
301
|
|
|
); |
302
|
|
|
$this->generateOrderItem($om, $order, $cart); |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @param ObjectManager $om |
308
|
|
|
* @param Store $store |
309
|
|
|
* @param Integration $integration |
|
|
|
|
310
|
|
|
* @param Customer $customer |
311
|
|
|
* @param string $status |
312
|
|
|
* @param Cart $cart |
313
|
|
|
* @param string $paymentMethod |
314
|
|
|
* @param string $paymentMethodDetails |
315
|
|
|
* @param mixed $origin |
316
|
|
|
* |
317
|
|
|
* @return Order |
318
|
|
|
*/ |
319
|
|
|
protected function generateOrder( |
320
|
|
|
ObjectManager $om, |
321
|
|
|
Store $store, |
322
|
|
|
Channel $channel, |
323
|
|
|
Customer $customer, |
324
|
|
|
$status, |
325
|
|
|
Cart $cart, |
326
|
|
|
$paymentMethod, |
327
|
|
|
$paymentMethodDetails, |
328
|
|
|
$origin |
329
|
|
|
) { |
330
|
|
|
$order = new Order(); |
331
|
|
|
$order->setOrganization($customer->getOrganization()); |
332
|
|
|
$order->setChannel($channel->getDataSource()); |
333
|
|
|
$order->setCustomer($customer); |
334
|
|
|
$order->setOwner($customer->getOwner()); |
335
|
|
|
$order->setStatus($status); |
336
|
|
|
$order->setStore($store); |
337
|
|
|
$order->setStoreName($store->getName()); |
338
|
|
|
$order->setIsGuest(0); |
|
|
|
|
339
|
|
|
$order->setIncrementId((string)$origin); |
340
|
|
|
$order->setCreatedAt(new \DateTime('now')); |
341
|
|
|
$order->setUpdatedAt(new \DateTime('now')); |
342
|
|
|
$order->setCart($cart); |
343
|
|
|
$order->setCurrency($cart->getBaseCurrencyCode()); |
344
|
|
|
$order->setTotalAmount($cart->getGrandTotal()); |
345
|
|
|
$order->setTotalInvoicedAmount($cart->getGrandTotal()); |
346
|
|
|
$order->setDataChannel($channel); |
347
|
|
|
if ($status == 'Completed') { |
348
|
|
|
$order->setTotalPaidAmount($cart->getGrandTotal()); |
349
|
|
|
} |
350
|
|
|
$order->setSubtotalAmount($cart->getSubTotal()); |
351
|
|
|
$order->setShippingAmount(rand(5, 10)); |
352
|
|
|
$order->setPaymentMethod($paymentMethod); |
353
|
|
|
$order->setPaymentDetails($paymentMethodDetails); |
354
|
|
|
$order->setShippingMethod('flatrate_flatrate'); |
355
|
|
|
$address = $this->getOrderAddress($om); |
356
|
|
|
$order->addAddress($address); |
357
|
|
|
$address->setOwner($order); |
358
|
|
|
$om->persist($order); |
359
|
|
|
return $order; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @param ObjectManager $om |
365
|
|
|
* @param Order $order |
366
|
|
|
* @param Cart $cart |
367
|
|
|
* |
368
|
|
|
* @return OrderItem[] |
369
|
|
|
*/ |
370
|
|
|
protected function generateOrderItem(ObjectManager $om, Order $order, Cart $cart) |
371
|
|
|
{ |
372
|
|
|
$cartItems = $cart->getCartItems(); |
373
|
|
|
$orderItems = []; |
374
|
|
|
foreach ($cartItems as $cartItem) { |
375
|
|
|
$orderItem = new OrderItem(); |
376
|
|
|
$orderItem->setOriginId($cartItem->getOriginId()); |
377
|
|
|
$orderItem->setOrder($order); |
378
|
|
|
$orderItem->setTaxAmount($cartItem->getTaxAmount()); |
379
|
|
|
$orderItem->setTaxPercent($cartItem->getTaxPercent()); |
380
|
|
|
$orderItem->setRowTotal($cartItem->getRowTotal()); |
381
|
|
|
$orderItem->setProductType($cartItem->getProductType()); |
382
|
|
|
$orderItem->setIsVirtual((bool)$cartItem->getIsVirtual()); |
383
|
|
|
$orderItem->setQty($cartItem->getQty()); |
384
|
|
|
$orderItem->setSku($cartItem->getSku()); |
385
|
|
|
$orderItem->setPrice($cartItem->getPrice()); |
386
|
|
|
$orderItem->setOriginalPrice($cartItem->getPrice()); |
387
|
|
|
$orderItem->setName($cartItem->getName()); |
388
|
|
|
$orderItem->setOwner($order->getOrganization()); |
389
|
|
|
$orderItems[] = $orderItem; |
390
|
|
|
|
391
|
|
|
$om->persist($orderItem); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
$order->setItems($orderItems); |
395
|
|
|
$om->persist($order); |
396
|
|
|
|
397
|
|
|
return $orderItems; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* @param ObjectManager $om |
402
|
|
|
* @param Store $store |
403
|
|
|
* @param Channel $channel |
404
|
|
|
* @param Customer $customer |
405
|
|
|
* @param CartStatus $status |
406
|
|
|
* @param int $origin |
407
|
|
|
* @param string $currency |
408
|
|
|
* @param int $rate |
409
|
|
|
* |
410
|
|
|
* @return Cart |
411
|
|
|
*/ |
412
|
|
|
protected function generateShoppingCart( |
413
|
|
|
ObjectManager $om, |
414
|
|
|
Store $store, |
415
|
|
|
Channel $channel, |
416
|
|
|
Customer $customer, |
417
|
|
|
CartStatus $status, |
418
|
|
|
$origin, |
419
|
|
|
$currency = 'USD', |
420
|
|
|
$rate = 1 |
421
|
|
|
) { |
422
|
|
|
$cart = new Cart(); |
423
|
|
|
$cart->setOrganization($customer->getOrganization()); |
424
|
|
|
$cart->setChannel($channel->getDataSource()); |
425
|
|
|
$cart->setCustomer($customer); |
426
|
|
|
$cart->setOwner($customer->getOwner()); |
427
|
|
|
$cart->setStatus($status); |
428
|
|
|
$cart->setStore($store); |
429
|
|
|
$cart->setBaseCurrencyCode($currency); |
430
|
|
|
$cart->setStoreCurrencyCode($currency); |
431
|
|
|
$cart->setQuoteCurrencyCode($currency); |
432
|
|
|
$cart->setStoreToBaseRate($rate); |
433
|
|
|
$cart->setStoreToQuoteRate($rate); |
434
|
|
|
$cart->setIsGuest(0); |
435
|
|
|
$cart->setEmail($customer->getEmail()); |
436
|
|
|
|
437
|
|
|
$datetime = new \DateTime('now'); |
438
|
|
|
$datetime->modify(sprintf('-%s day', rand(1, 5))); |
439
|
|
|
|
440
|
|
|
$cart->setCreatedAt($datetime); |
441
|
|
|
$cart->setUpdatedAt($datetime); |
442
|
|
|
$cart->setOriginId($origin); |
443
|
|
|
$cart->setDataChannel($channel); |
444
|
|
|
$om->persist($cart); |
445
|
|
|
|
446
|
|
|
return $cart; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* @param ObjectManager $om |
451
|
|
|
* @param Cart $cart |
452
|
|
|
* |
453
|
|
|
* @return CartItem[] |
454
|
|
|
*/ |
455
|
|
|
protected function generateShoppingCartItem(ObjectManager $om, Cart $cart) |
456
|
|
|
{ |
457
|
|
|
|
458
|
|
|
$products = ['Computer', 'Gaming Computer', 'Universal Camera Case', 'SLR Camera Tripod', |
459
|
|
|
'Two Year Extended Warranty - Parts and Labor', 'Couch', 'Chair', 'Magento Red Furniture Set']; |
460
|
|
|
|
461
|
|
|
$cartItems = []; |
462
|
|
|
$cartItemsCount = rand(0, 2); |
463
|
|
|
$total = 0.0; |
464
|
|
|
$totalTaxAmount = 0.0; |
465
|
|
|
$shipping = rand(0, 1); |
466
|
|
|
for ($i = 0; $i <= $cartItemsCount; $i++) { |
467
|
|
|
$product = $products[rand(0, count($products)-1)]; |
468
|
|
|
$origin = $i+1; |
469
|
|
|
$price = rand(10, 200); |
470
|
|
|
$price = $price + rand(0, 99)/100.0; |
471
|
|
|
$taxAmount = $price * self::TAX; |
472
|
|
|
$totalTaxAmount = $totalTaxAmount + $taxAmount; |
473
|
|
|
$total = $total + $price + $taxAmount; |
474
|
|
|
$cartItem = new CartItem(); |
475
|
|
|
$cartItem->setProductId(rand(1, 100)); |
476
|
|
|
$cartItem->setFreeShipping((string)$shipping); |
477
|
|
|
$cartItem->setIsVirtual(0); |
478
|
|
|
$cartItem->setRowTotal($price + $taxAmount); |
479
|
|
|
$cartItem->setPriceInclTax($price + $taxAmount); |
480
|
|
|
$cartItem->setTaxAmount($taxAmount); |
481
|
|
|
$cartItem->setSku('sku-' . strtolower(str_replace(" ", "_", $product))); |
482
|
|
|
$cartItem->setProductType('simple'); |
483
|
|
|
$cartItem->setName($product); |
484
|
|
|
$cartItem->setQty(1); |
485
|
|
|
$cartItem->setPrice($price); |
486
|
|
|
$cartItem->setDiscountAmount(0); |
487
|
|
|
$cartItem->setTaxPercent(self::TAX); |
488
|
|
|
$cartItem->setCreatedAt(new \DateTime('now')); |
489
|
|
|
$cartItem->setUpdatedAt(new \DateTime('now')); |
490
|
|
|
$cartItem->setOriginId($origin); |
491
|
|
|
$cartItem->setCart($cart); |
492
|
|
|
$cartItem->setOwner($cart->getOrganization()); |
493
|
|
|
$cart->getCartItems()->add($cartItem); |
494
|
|
|
$cart->setItemsQty($i+1); |
495
|
|
|
$cart->setItemsCount($i+1); |
496
|
|
|
$om->persist($cartItem); |
497
|
|
|
$cartItems[] = $cartItem; |
498
|
|
|
} |
499
|
|
|
$cart->setSubTotal($total); |
500
|
|
|
$shippingAmount = 0.0; |
501
|
|
|
if ((bool)$shipping) { |
502
|
|
|
$shippingAmount = rand(3, 10); |
503
|
|
|
} |
504
|
|
|
$cart->setGrandTotal($total + $shippingAmount); |
505
|
|
|
$cart->setTaxAmount($totalTaxAmount); |
506
|
|
|
$om->persist($cart); |
507
|
|
|
return $cartItems; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
protected function getOrderAddress(ObjectManager $om) |
511
|
|
|
{ |
512
|
|
|
$address = new OrderAddress; |
513
|
|
|
$address->setCity('City'); |
514
|
|
|
$address->setStreet('First street'); |
515
|
|
|
$address->setPostalCode(123456); |
516
|
|
|
$address->setFirstName('John'); |
517
|
|
|
$address->setLastName('Doe'); |
518
|
|
|
/** @var Country $country */ |
519
|
|
|
$country = $om->getRepository('OroAddressBundle:Country')->findOneBy(['iso2Code' => 'US']); |
520
|
|
|
$address->setCountry($country); |
521
|
|
|
/** @var Region $region */ |
522
|
|
|
$region = $om->getRepository('OroAddressBundle:Region')->findOneBy(['combinedCode' => 'US-AK']); |
523
|
|
|
$address->setRegion($region); |
524
|
|
|
$om->persist($address); |
525
|
|
|
|
526
|
|
|
return $address; |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
/** |
530
|
|
|
* @param ObjectManager $om |
531
|
|
|
* @param Website $website |
|
|
|
|
532
|
|
|
* @param Store $store |
533
|
|
|
* @param CustomerGroup $group |
534
|
|
|
* @param Channel $channel |
535
|
|
|
* @param Organization $organization |
536
|
|
|
* |
537
|
|
|
* @return Customer[] |
538
|
|
|
*/ |
539
|
|
|
protected function persistDemoCustomers( |
540
|
|
|
ObjectManager $om, |
541
|
|
|
Store $store, |
542
|
|
|
CustomerGroup $group, |
543
|
|
|
Channel $channel, |
544
|
|
|
Organization $organization |
545
|
|
|
) { |
546
|
|
|
$accounts = $om->getRepository('OroAccountBundle:Account')->findAll(); |
547
|
|
|
$contacts = $om->getRepository('OroContactBundle:Contact')->findAll(); |
548
|
|
|
$customers = []; |
549
|
|
|
|
550
|
|
|
$buffer = range(0, count($accounts) - 1); |
551
|
|
|
for ($i = 0; $i < 25; ++$i) { |
552
|
|
|
$birthday = $this->generateBirthday(); |
553
|
|
|
|
554
|
|
|
/** @var Contact $contact */ |
555
|
|
|
$contact = $contacts[$buffer[$i]]; |
556
|
|
|
$customer = new Customer(); |
557
|
|
|
$customer->setWebsite($store->getWebsite()) |
558
|
|
|
->setChannel($channel->getDataSource()) |
559
|
|
|
->setStore($store) |
560
|
|
|
->setFirstName($contact->getFirstName()) |
561
|
|
|
->setLastName($contact->getLastName()) |
562
|
|
|
->setEmail($contact->getPrimaryEmail()) |
|
|
|
|
563
|
|
|
->setBirthday($birthday) |
564
|
|
|
->setVat(mt_rand(10000000, 99999999)) |
565
|
|
|
->setGroup($group) |
566
|
|
|
->setCreatedAt(new \DateTime('now')) |
567
|
|
|
->setUpdatedAt(new \DateTime('now')) |
568
|
|
|
->setOriginId($i + 1) |
569
|
|
|
->setAccount($accounts[$buffer[$i]]) |
570
|
|
|
->setContact($contact) |
571
|
|
|
->setOrganization($organization) |
572
|
|
|
->setOwner($this->getRandomOwner()) |
573
|
|
|
->setDataChannel($channel); |
574
|
|
|
|
575
|
|
|
$om->persist($customer); |
576
|
|
|
$customers[] = $customer; |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
return $customers; |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* Generates a date of birth |
584
|
|
|
* |
585
|
|
|
* @return \DateTime |
586
|
|
|
*/ |
587
|
|
View Code Duplication |
private function generateBirthday() |
|
|
|
|
588
|
|
|
{ |
589
|
|
|
// Convert to timestamps |
590
|
|
|
$min = strtotime('1950-01-01'); |
591
|
|
|
$max = strtotime('2000-01-01'); |
592
|
|
|
|
593
|
|
|
// Generate random number using above bounds |
594
|
|
|
$val = rand($min, $max); |
595
|
|
|
|
596
|
|
|
// Convert back to desired date format |
597
|
|
|
return new \DateTime(date('Y-m-d', $val), new \DateTimeZone('UTC')); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* @return User |
602
|
|
|
*/ |
603
|
|
|
protected function getRandomOwner() |
604
|
|
|
{ |
605
|
|
|
$randomUser = count($this->users)-1; |
606
|
|
|
$user = $this->users[rand(0, $randomUser)]; |
607
|
|
|
|
608
|
|
|
return $user; |
609
|
|
|
} |
610
|
|
|
} |
611
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: