1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\SalesBundle\Tests\Functional\Fixture; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\DataFixtures\AbstractFixture; |
6
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
7
|
|
|
|
8
|
|
|
use Oro\Bundle\AddressBundle\Entity\Address; |
9
|
|
|
use Oro\Bundle\AddressBundle\Entity\Country; |
10
|
|
|
use Oro\Bundle\UserBundle\Entity\User; |
11
|
|
|
use Oro\Bundle\OrganizationBundle\Entity\Organization; |
12
|
|
|
|
13
|
|
|
use OroCRM\Bundle\AccountBundle\Entity\Account; |
14
|
|
|
use OroCRM\Bundle\ContactBundle\Entity\Contact; |
15
|
|
|
use OroCRM\Bundle\ChannelBundle\Builder\BuilderFactory; |
16
|
|
|
use OroCRM\Bundle\ChannelBundle\Entity\Channel; |
17
|
|
|
use OroCRM\Bundle\SalesBundle\Entity\B2bCustomer; |
18
|
|
|
use OroCRM\Bundle\SalesBundle\Entity\Lead; |
19
|
|
|
use OroCRM\Bundle\SalesBundle\Entity\LeadEmail; |
20
|
|
|
use OroCRM\Bundle\SalesBundle\Entity\Opportunity; |
21
|
|
|
use OroCRM\Bundle\SalesBundle\Entity\SalesFunnel; |
22
|
|
|
|
23
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
24
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
25
|
|
|
|
26
|
|
|
class LoadSalesBundleFixtures extends AbstractFixture implements ContainerAwareInterface |
27
|
|
|
{ |
28
|
|
|
const CUSTOMER_NAME = 'b2bCustomer name'; |
29
|
|
|
const CHANNEL_TYPE = 'b2b'; |
30
|
|
|
const CHANNEL_NAME = 'b2b Channel'; |
31
|
|
|
const ACCOUNT_NAME = 'some account name'; |
32
|
|
|
|
33
|
|
|
/** @var ObjectManager */ |
34
|
|
|
protected $em; |
35
|
|
|
|
36
|
|
|
/** @var BuilderFactory */ |
37
|
|
|
protected $factory; |
38
|
|
|
|
39
|
|
|
/** @var Channel */ |
40
|
|
|
protected $channel; |
41
|
|
|
|
42
|
|
|
/** @var User */ |
43
|
|
|
protected $user; |
44
|
|
|
|
45
|
|
|
/** @var Organization */ |
46
|
|
|
protected $organization; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
|
|
public function setContainer(ContainerInterface $container = null) |
52
|
|
|
{ |
53
|
|
|
$this->factory = $container->get('orocrm_channel.builder.factory'); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
|
|
public function load(ObjectManager $manager) |
60
|
|
|
{ |
61
|
|
|
$this->em = $manager; |
62
|
|
|
$this->organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst(); |
63
|
|
|
|
64
|
|
|
$this->createChannel(); |
65
|
|
|
$this->createAccount(); |
66
|
|
|
$this->createContact(); |
67
|
|
|
$this->createB2bCustomer(); |
68
|
|
|
$this->createLead(); |
69
|
|
|
$this->createOpportunity(); |
70
|
|
|
$this->createSalesFunnelByLead(); |
71
|
|
|
$this->createSalesFunnelByOpportunity(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
protected function createAccount() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$account = new Account(); |
77
|
|
|
$account->setName(self::ACCOUNT_NAME); |
78
|
|
|
$account->setOrganization($this->organization); |
79
|
|
|
|
80
|
|
|
$this->em->persist($account); |
81
|
|
|
$this->em->flush(); |
82
|
|
|
|
83
|
|
|
$this->setReference('default_account', $account); |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function createContact() |
89
|
|
|
{ |
90
|
|
|
$contact = new Contact(); |
91
|
|
|
$contact->setFirstName('John'); |
92
|
|
|
$contact->setLastName('Doe'); |
93
|
|
|
$contact->setOrganization($this->organization); |
94
|
|
|
|
95
|
|
|
$this->em->persist($contact); |
96
|
|
|
$this->em->flush(); |
97
|
|
|
|
98
|
|
|
$this->setReference('default_contact', $contact); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function createB2bCustomer() |
104
|
|
|
{ |
105
|
|
|
$customer = new B2bCustomer(); |
106
|
|
|
$customer->setAccount($this->getReference('default_account')); |
107
|
|
|
$customer->setName(self::CUSTOMER_NAME); |
108
|
|
|
$customer->setDataChannel($this->getReference('default_channel')); |
109
|
|
|
$customer->setOrganization($this->organization); |
110
|
|
|
$customer->setBillingAddress($this->getBillingAddress()); |
111
|
|
|
$customer->setShippingAddress($this->getShippingAddress()); |
112
|
|
|
|
113
|
|
|
$this->em->persist($customer); |
114
|
|
|
$this->em->flush(); |
115
|
|
|
|
116
|
|
|
$this->setReference('default_b2bcustomer', $customer); |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function createLead() |
122
|
|
|
{ |
123
|
|
|
$lead = new Lead(); |
124
|
|
|
$lead->setDataChannel($this->getReference('default_channel')); |
125
|
|
|
$lead->setName('Lead name'); |
126
|
|
|
$lead->setFirstName('fname'); |
127
|
|
|
$lead->setLastName('lname'); |
128
|
|
|
$lead->setCustomer($this->getReference('default_b2bcustomer')); |
129
|
|
|
$email = new LeadEmail('[email protected]'); |
130
|
|
|
$email->setPrimary(true); |
131
|
|
|
$lead->addEmail($email); |
132
|
|
|
$lead->setOrganization($this->organization); |
133
|
|
|
|
134
|
|
|
$lead2 = new Lead(); |
135
|
|
|
$lead2->setDataChannel($this->getReference('default_channel')); |
136
|
|
|
$lead2->setName('Lead name 2'); |
137
|
|
|
$lead2->setFirstName('fname 2'); |
138
|
|
|
$lead2->setLastName('lname 2'); |
139
|
|
|
$lead2->setCustomer($this->getReference('default_b2bcustomer')); |
140
|
|
|
$email = new LeadEmail('[email protected]'); |
141
|
|
|
$email->setPrimary(true); |
142
|
|
|
$lead2->addEmail($email); |
143
|
|
|
$lead2->setOrganization($this->organization); |
144
|
|
|
|
145
|
|
|
$lead3 = new Lead(); |
146
|
|
|
$lead3->setDataChannel($this->getReference('default_channel')); |
147
|
|
|
$lead3->setName('Lead name 3'); |
148
|
|
|
$lead3->setFirstName('fname 3'); |
149
|
|
|
$lead3->setLastName('lname 3'); |
150
|
|
|
$lead3->setCustomer($this->getReference('default_b2bcustomer')); |
151
|
|
|
$email = new LeadEmail('[email protected]'); |
152
|
|
|
$email->setPrimary(true); |
153
|
|
|
$lead3->addEmail($email); |
154
|
|
|
$lead3->setOrganization($this->organization); |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
$this->em->persist($lead); |
158
|
|
|
$this->em->persist($lead2); |
159
|
|
|
$this->em->persist($lead3); |
160
|
|
|
$this->em->flush(); |
161
|
|
|
|
162
|
|
|
$this->setReference('default_lead', $lead); |
163
|
|
|
$this->setReference('second_lead', $lead2); |
164
|
|
|
$this->setReference('third_lead', $lead3); |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
protected function createOpportunity() |
170
|
|
|
{ |
171
|
|
|
$opportunity = new Opportunity(); |
172
|
|
|
$opportunity->setName('opname'); |
173
|
|
|
$opportunity->setCustomer($this->getReference('default_b2bcustomer')); |
174
|
|
|
$opportunity->setDataChannel($this->getReference('default_channel')); |
175
|
|
|
$opportunity->setBudgetAmount(50.00); |
176
|
|
|
$opportunity->setProbability(10); |
177
|
|
|
$opportunity->setOrganization($this->organization); |
178
|
|
|
|
179
|
|
|
$this->em->persist($opportunity); |
180
|
|
|
$this->em->flush(); |
181
|
|
|
|
182
|
|
|
$this->setReference('default_opportunity', $opportunity); |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
View Code Duplication |
protected function createSalesFunnelByLead() |
|
|
|
|
188
|
|
|
{ |
189
|
|
|
$date = new \DateTime('now'); |
190
|
|
|
|
191
|
|
|
$salesFunnel = new SalesFunnel(); |
192
|
|
|
$salesFunnel->setDataChannel($this->getReference('default_channel')); |
193
|
|
|
$salesFunnel->setLead($this->getReference('default_lead')); |
194
|
|
|
$salesFunnel->setOwner($this->getUser()); |
195
|
|
|
$salesFunnel->setStartDate($date); |
196
|
|
|
$salesFunnel->setOrganization($this->organization); |
197
|
|
|
|
198
|
|
|
$this->em->persist($salesFunnel); |
199
|
|
|
$this->em->flush(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
View Code Duplication |
protected function createSalesFunnelByOpportunity() |
|
|
|
|
203
|
|
|
{ |
204
|
|
|
$date = new \DateTime('now'); |
205
|
|
|
|
206
|
|
|
$salesFunnel = new SalesFunnel(); |
207
|
|
|
$salesFunnel->setDataChannel($this->getReference('default_channel')); |
208
|
|
|
$salesFunnel->setOpportunity($this->getReference('default_opportunity')); |
209
|
|
|
$salesFunnel->setOwner($this->getUser()); |
210
|
|
|
$salesFunnel->setStartDate($date); |
211
|
|
|
$salesFunnel->setOrganization($this->organization); |
212
|
|
|
|
213
|
|
|
$this->em->persist($salesFunnel); |
214
|
|
|
$this->em->flush(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return Channel |
219
|
|
|
*/ |
220
|
|
View Code Duplication |
protected function createChannel() |
|
|
|
|
221
|
|
|
{ |
222
|
|
|
$channel = $this |
223
|
|
|
->factory |
224
|
|
|
->createBuilder() |
225
|
|
|
->setName(self::CHANNEL_NAME) |
226
|
|
|
->setChannelType(self::CHANNEL_TYPE) |
227
|
|
|
->setStatus(Channel::STATUS_ACTIVE) |
228
|
|
|
->setOwner($this->organization) |
229
|
|
|
->setEntities() |
230
|
|
|
->getChannel(); |
231
|
|
|
|
232
|
|
|
$this->em->persist($channel); |
233
|
|
|
$this->em->flush(); |
234
|
|
|
|
235
|
|
|
$this->setReference('default_channel', $channel); |
236
|
|
|
|
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return User |
242
|
|
|
*/ |
243
|
|
|
protected function getUser() |
244
|
|
|
{ |
245
|
|
|
if (empty($this->user)) { |
246
|
|
|
$this->user = $this->em->getRepository('OroUserBundle:User')->findOneBy(['username' => 'admin']); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $this->user; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return Address |
254
|
|
|
*/ |
255
|
|
View Code Duplication |
protected function getBillingAddress() |
|
|
|
|
256
|
|
|
{ |
257
|
|
|
$address = new Address(); |
258
|
|
|
$address->setCountry($this->getCounty()); |
259
|
|
|
$address->setStreet('1215 Caldwell Road'); |
260
|
|
|
$address->setCity('Rochester'); |
261
|
|
|
$address->setPostalCode('14608'); |
262
|
|
|
$address->setRegionText('Arizona1'); |
263
|
|
|
|
264
|
|
|
return $address; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return Address |
269
|
|
|
*/ |
270
|
|
View Code Duplication |
protected function getShippingAddress() |
|
|
|
|
271
|
|
|
{ |
272
|
|
|
$address = new Address(); |
273
|
|
|
$address->setCountry($this->getCounty()); |
274
|
|
|
$address->setStreet('1215 Caldwell Road'); |
275
|
|
|
$address->setCity('Rochester'); |
276
|
|
|
$address->setPostalCode('14608'); |
277
|
|
|
$address->setRegionText('Arizona1'); |
278
|
|
|
|
279
|
|
|
return $address; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return Country |
284
|
|
|
*/ |
285
|
|
|
protected function getCounty() |
286
|
|
|
{ |
287
|
|
|
$country = $this->em->getRepository('OroAddressBundle:Country')->findOneBy([ |
288
|
|
|
'iso2Code'=>'US' |
289
|
|
|
]); |
290
|
|
|
|
291
|
|
|
return $country; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: