Completed
Push — master ( a18731...e2e070 )
by
unknown
99:54 queued 47:45
created

B2bCustomer::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace OroCRM\Bundle\SalesBundle\Entity;
4
5
use Doctrine\Common\Collections\Collection;
6
use Doctrine\ORM\Mapping as ORM;
7
use Doctrine\Common\Collections\ArrayCollection;
8
9
use Oro\Bundle\AddressBundle\Entity\Address;
10
use Oro\Bundle\EmailBundle\Entity\EmailOwnerInterface;
11
use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\Config;
12
use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\ConfigField;
13
use Oro\Bundle\OrganizationBundle\Entity\Organization;
14
use Oro\Bundle\UserBundle\Entity\User;
15
16
use OroCRM\Bundle\AccountBundle\Entity\Account;
17
use OroCRM\Bundle\ContactBundle\Entity\Contact;
18
use OroCRM\Bundle\SalesBundle\Model\ExtendB2bCustomer;
19
use OroCRM\Bundle\ChannelBundle\Model\ChannelEntityTrait;
20
use OroCRM\Bundle\ChannelBundle\Model\ChannelAwareInterface;
21
use OroCRM\Bundle\ChannelBundle\Model\CustomerIdentityInterface;
22
23
/**
24
 * @ORM\Entity(repositoryClass="OroCRM\Bundle\SalesBundle\Entity\Repository\B2bCustomerRepository")
25
 * @ORM\Table(name="orocrm_sales_b2bcustomer")
26
 * @ORM\HasLifecycleCallbacks()
27
 * @Config(
28
 *      routeName="orocrm_sales_b2bcustomer_index",
29
 *      routeView="orocrm_sales_b2bcustomer_view",
30
 *      defaultValues={
31
 *          "entity"={
32
 *              "icon"="icon-user-md",
33
 *              "contact_information"={
34
 *                  "email"={
35
 *                      {"fieldName"="primaryEmail"}
36
 *                  },
37
 *                  "phone"={
38
 *                      {"fieldName"="primaryPhone"}
39
 *                  }
40
 *              }
41
 *          },
42
 *          "ownership"={
43
 *              "owner_type"="USER",
44
 *              "owner_field_name"="owner",
45
 *              "owner_column_name"="user_owner_id",
46
 *              "organization_field_name"="organization",
47
 *              "organization_column_name"="organization_id"
48
 *          },
49
 *          "security"={
50
 *              "type"="ACL",
51
 *              "group_name"="",
52
 *              "category"="sales_data"
53
 *          },
54
 *          "dataaudit"={
55
 *              "auditable"=true
56
 *          },
57
 *          "form"={
58
 *              "form_type"="orocrm_sales_b2bcustomer_select"
59
 *          },
60
 *          "grid"={
61
 *              "default"="orocrm-sales-b2bcustomers-grid",
62
 *              "context"="orocrm-sales-b2bcustomers-for-context-grid"
63
 *          },
64
 *         "tag"={
65
 *              "enabled"=true
66
 *          }
67
 *      }
68
 * )
69
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
70
 * @SuppressWarnings(PHPMD.ExcessivePublicCount)
71
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
72
 */
73
class B2bCustomer extends ExtendB2bCustomer implements
74
    ChannelAwareInterface,
75
    CustomerIdentityInterface
76
{
77
    use ChannelEntityTrait;
78
79
    /**
80
     * @var integer
81
     *
82
     * @ORM\Column(name="id", type="integer")
83
     * @ORM\Id
84
     * @ORM\GeneratedValue(strategy="AUTO")
85
     * @ConfigField(
86
     *  defaultValues={
87
     *      "importexport"={
88
     *          "order"=0
89
     *      }
90
     *  }
91
     * )
92
     */
93
    protected $id;
94
95
    /**
96
     * @var string
97
     *
98
     * @ORM\Column(type="string", length=255)
99
     * @ConfigField(
100
     *      defaultValues={
101
     *          "dataaudit"={
102
     *              "auditable"=true
103
     *          },
104
     *          "importexport"={
105
     *              "identity"=true,
106
     *              "order"=10
107
     *          }
108
     *      }
109
     * )
110
     */
111
    protected $name;
112
113
    /**
114
     * @var double
115
     *
116
     * @ORM\Column(name="lifetime", type="money", nullable=true)
117
     * @ConfigField(
118
     *      defaultValues={
119
     *          "dataaudit"={
120
     *              "auditable"=true
121
     *          },
122
     *          "importexport"={
123
     *              "full"=true,
124
     *              "order"=15
125
     *          }
126
     *      }
127
     * )
128
     */
129
    protected $lifetime = 0;
130
131
    /**
132
     * @var Address $shippingAddress
133
     *
134
     * @ORM\ManyToOne(targetEntity="Oro\Bundle\AddressBundle\Entity\Address", cascade={"persist", "remove"})
135
     * @ORM\JoinColumn(name="shipping_address_id", referencedColumnName="id", onDelete="SET NULL")
136
     * @ConfigField(
137
     *      defaultValues={
138
     *          "importexport"={
139
     *              "full"=true,
140
     *              "order"=20
141
     *          }
142
     *      }
143
     * )
144
     */
145
    protected $shippingAddress;
146
147
    /**
148
     * @var Address $billingAddress
149
     *
150
     * @ORM\ManyToOne(targetEntity="Oro\Bundle\AddressBundle\Entity\Address", cascade={"persist", "remove"})
151
     * @ORM\JoinColumn(name="billing_address_id", referencedColumnName="id", onDelete="SET NULL")
152
     * @ConfigField(
153
     *      defaultValues={
154
     *          "importexport"={
155
     *              "full"=true,
156
     *              "order"=30
157
     *          }
158
     *      }
159
     * )
160
     */
161
    protected $billingAddress;
162
163
    /**
164
     * @var Account
165
     *
166
     * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\AccountBundle\Entity\Account", cascade="persist")
167
     * @ORM\JoinColumn(name="account_id", referencedColumnName="id", onDelete="SET NULL")
168
     * @ConfigField(
169
     *  defaultValues={
170
     *      "dataaudit"={"auditable"=true},
171
     *      "importexport"={
172
     *          "order"=40,
173
     *          "short"=true
174
     *      }
175
     *  }
176
     * )
177
     */
178
    protected $account;
179
180
    /**
181
     * @var Contact
182
     *
183
     * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\ContactBundle\Entity\Contact")
184
     * @ORM\JoinColumn(name="contact_id", referencedColumnName="id", onDelete="SET NULL")
185
     * @ConfigField(
186
     *  defaultValues={
187
     *      "dataaudit"={"auditable"=true},
188
     *      "importexport"={
189
     *          "order"=50,
190
     *          "short"=true
191
     *      }
192
     *  }
193
     * )
194
     */
195
    protected $contact;
196
197
    /**
198
     * @var ArrayCollection
199
     *
200
     * @ORM\OneToMany(targetEntity="OroCRM\Bundle\SalesBundle\Entity\Lead", mappedBy="customer", cascade={"remove"})
201
     */
202
    protected $leads;
203
204
    /**
205
     * @var ArrayCollection
206
     *
207
     * @ORM\OneToMany(
208
     *     targetEntity="OroCRM\Bundle\SalesBundle\Entity\Opportunity",
209
     *     mappedBy="customer",
210
     *     cascade={"remove"}
211
     * )
212
     */
213
    protected $opportunities;
214
215
    /**
216
     * @var User
217
     *
218
     * @ORM\ManyToOne(targetEntity="Oro\Bundle\UserBundle\Entity\User")
219
     * @ORM\JoinColumn(name="user_owner_id", referencedColumnName="id", onDelete="SET NULL")
220
     * @ConfigField(
221
     *  defaultValues={
222
     *      "dataaudit"={"auditable"=true},
223
     *      "importexport"={
224
     *          "order"=70,
225
     *          "short"=true
226
     *      }
227
     *  }
228
     * )
229
     */
230
    protected $owner;
231
232
    /**
233
     * @var Organization
234
     *
235
     * @ORM\ManyToOne(targetEntity="Oro\Bundle\OrganizationBundle\Entity\Organization")
236
     * @ORM\JoinColumn(name="organization_id", referencedColumnName="id", onDelete="SET NULL")
237
     */
238
    protected $organization;
239
240
    /**
241
     * @var \DateTime $created
242
     *
243
     * @ORM\Column(type="datetime")
244
     * @ConfigField(
245
     *      defaultValues={
246
     *          "entity"={
247
     *              "label"="oro.ui.created_at"
248
     *          },
249
     *          "importexport"={
250
     *              "excluded"=true
251
     *          }
252
     *      }
253
     * )
254
     */
255
    protected $createdAt;
256
257
    /**
258
     * @var \DateTime $updated
259
     *
260
     * @ORM\Column(type="datetime")
261
     * @ConfigField(
262
     *      defaultValues={
263
     *          "entity"={
264
     *              "label"="oro.ui.updated_at"
265
     *          },
266
     *          "importexport"={
267
     *              "excluded"=true
268
     *          }
269
     *      }
270
     * )
271
     */
272
    protected $updatedAt;
273
274
    /**
275
     * @var Collection
276
     *
277
     * @ORM\OneToMany(targetEntity="OroCRM\Bundle\SalesBundle\Entity\B2bCustomerPhone", mappedBy="owner",
278
     *    mappedBy="owner", cascade={"all"}, orphanRemoval=true
279
     * ))
280
     * @ORM\OrderBy({"primary" = "DESC"})
281
     * @ConfigField(
282
     *      defaultValues={
283
     *          "importexport"={
284
     *              "order"=80
285
     *          },
286
     *          "dataaudit"={
287
     *              "auditable"=true
288
     *          }
289
     *      }
290
     * )
291
     */
292
    protected $phones;
293
294
    /**
295
     * @var Collection
296
     *
297
     * @ORM\OneToMany(targetEntity="OroCRM\Bundle\SalesBundle\Entity\B2bCustomerEmail",
298
     *    mappedBy="owner", cascade={"all"}, orphanRemoval=true
299
     * )
300
     * @ORM\OrderBy({"primary" = "DESC"})
301
     * @ConfigField(
302
     *      defaultValues={
303
     *          "importexport"={
304
     *              "order"=90
305
     *          },
306
     *          "dataaudit"={
307
     *              "auditable"=true
308
     *          }
309
     *      }
310
     * )
311
     */
312
    protected $emails;
313
314 View Code Duplication
    public function __construct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
315
    {
316
        parent::__construct();
317
318
        $this->leads         = new ArrayCollection();
319
        $this->opportunities = new ArrayCollection();
320
        $this->phones        = new ArrayCollection();
321
        $this->emails        = new ArrayCollection();
322
    }
323
324
    /**
325
     * @return int
326
     */
327
    public function getId()
328
    {
329
        return $this->id;
330
    }
331
332
    /**
333
     * @return string
334
     */
335
    public function getName()
336
    {
337
        return $this->name;
338
    }
339
340
    /**
341
     * @param string $name
342
     */
343
    public function setName($name)
344
    {
345
        $this->name = $name;
346
    }
347
348
    /**
349
     * @return float
350
     */
351
    public function getLifetime()
352
    {
353
        return $this->lifetime;
354
    }
355
356
    /**
357
     * @param float $lifetime
358
     */
359
    public function setLifetime($lifetime)
360
    {
361
        $this->lifetime = $lifetime;
362
    }
363
364
    /**
365
     * @return Address
366
     */
367
    public function getShippingAddress()
368
    {
369
        return $this->shippingAddress;
370
    }
371
372
    /**
373
     * @param Address|null $shippingAddress
374
     */
375
    public function setShippingAddress(Address $shippingAddress = null)
376
    {
377
        $this->shippingAddress = $shippingAddress;
378
    }
379
380
    /**
381
     * @return Address
382
     */
383
    public function getBillingAddress()
384
    {
385
        return $this->billingAddress;
386
    }
387
388
    /**
389
     * @param Address|null $billingAddress
390
     */
391
    public function setBillingAddress(Address $billingAddress = null)
392
    {
393
        $this->billingAddress = $billingAddress;
394
    }
395
396
    /**
397
     * @return Account
398
     */
399
    public function getAccount()
400
    {
401
        return $this->account;
402
    }
403
404
    /**
405
     * @param Account|null $account
406
     */
407
    public function setAccount(Account $account = null)
408
    {
409
        $this->account = $account;
410
    }
411
412
    /**
413
     * @return Contact
414
     */
415
    public function getContact()
416
    {
417
        return $this->contact;
418
    }
419
420
    /**
421
     * @param Contact|null $contact
422
     */
423
    public function setContact(Contact $contact = null)
424
    {
425
        $this->contact = $contact;
426
    }
427
428
    /**
429
     * @return ArrayCollection
430
     */
431
    public function getLeads()
432
    {
433
        return $this->leads;
434
    }
435
436
    /**
437
     * @param ArrayCollection $leads
438
     */
439
    public function setLeads(ArrayCollection $leads)
440
    {
441
        $this->leads = $leads;
442
    }
443
444
    /**
445
     * @param Lead $lead
446
     */
447
    public function addLead(Lead $lead)
448
    {
449
        if (!$this->getLeads()->contains($lead)) {
450
            $this->getLeads()->add($lead);
451
            $lead->setCustomer($this);
452
        }
453
    }
454
455
    /**
456
     * @param Lead $lead
457
     */
458
    public function removeLead(Lead $lead)
459
    {
460
        if ($this->getLeads()->contains($lead)) {
461
            $this->getLeads()->removeElement($lead);
462
            $lead->removeCustomer();
463
        }
464
    }
465
466
    /**
467
     * @return ArrayCollection
468
     */
469
    public function getOpportunities()
470
    {
471
        return $this->opportunities;
472
    }
473
474
    /**
475
     * @param ArrayCollection $opportunities
476
     */
477
    public function setOpportunities(ArrayCollection $opportunities)
478
    {
479
        $this->opportunities = $opportunities;
480
    }
481
482
    /**
483
     * @param Opportunity $opportunity
484
     */
485
    public function addOpportunity(Opportunity $opportunity)
486
    {
487
        if (!$this->getOpportunities()->contains($opportunity)) {
488
            $this->getOpportunities()->add($opportunity);
489
            $opportunity->setCustomer($this);
490
        }
491
    }
492
493
    /**
494
     * @param Opportunity $opportunity
495
     */
496
    public function removeOpportunity(Opportunity $opportunity)
497
    {
498
        if ($this->getOpportunities()->contains($opportunity)) {
499
            $this->getOpportunities()->removeElement($opportunity);
500
            $opportunity->removeCustomer();
501
        }
502
    }
503
504
    /**
505
     * @return mixed
506
     */
507
    public function getOwner()
508
    {
509
        return $this->owner;
510
    }
511
512
    /**
513
     * @param User $owner
514
     */
515
    public function setOwner(User $owner = null)
516
    {
517
        $this->owner = $owner;
518
    }
519
520
    /**
521
     * @return \DateTime
522
     */
523
    public function getCreatedAt()
524
    {
525
        return $this->createdAt;
526
    }
527
528
    /**
529
     * @param \DateTime $createdAt
530
     */
531
    public function setCreatedAt(\DateTime $createdAt)
532
    {
533
        $this->createdAt = $createdAt;
534
    }
535
536
    /**
537
     * @return \DateTime
538
     */
539
    public function getUpdatedAt()
540
    {
541
        return $this->updatedAt;
542
    }
543
544
    /**
545
     * @param \DateTime $updatedAt
546
     */
547
    public function setUpdatedAt(\DateTime $updatedAt)
548
    {
549
        $this->updatedAt = $updatedAt;
550
    }
551
552
    /**
553
     * Pre persist event listener
554
     *
555
     * @ORM\PrePersist
556
     */
557
    public function prePersist()
558
    {
559
        $this->createdAt = $this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC'));
560
    }
561
562
    /**
563
     * Pre update event handler
564
     *
565
     * @ORM\PreUpdate
566
     */
567
    public function preUpdate()
568
    {
569
        $this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC'));
570
    }
571
572
    /**
573
     * @return string
574
     */
575
    public function __toString()
576
    {
577
        return (string) $this->getName();
578
    }
579
580
    /**
581
     * Set organization
582
     *
583
     * @param Organization $organization
584
     * @return B2bCustomer
585
     */
586
    public function setOrganization(Organization $organization = null)
587
    {
588
        $this->organization = $organization;
589
590
        return $this;
591
    }
592
593
    /**
594
     * Get organization
595
     *
596
     * @return Organization
597
     */
598
    public function getOrganization()
599
    {
600
        return $this->organization;
601
    }
602
603
    /**
604
     * Set phones.
605
     *
606
     * This method could not be named setPhones because of bug CRM-253.
607
     *
608
     * @param Collection|B2bCustomerPhone[] $phones
609
     *
610
     * @return B2bCustomer
611
     */
612
    public function resetPhones($phones)
613
    {
614
        $this->phones->clear();
615
        foreach ($phones as $phone) {
616
            $this->addPhone($phone);
617
        }
618
        return $this;
619
    }
620
    /**
621
     * Add phone
622
     *
623
     * @param B2bCustomerPhone $phone
624
     *
625
     * @return B2bCustomer
626
     */
627 View Code Duplication
    public function addPhone(B2bCustomerPhone $phone)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
628
    {
629
        if (!$this->phones->contains($phone)) {
630
            $this->phones->add($phone);
631
            $phone->setOwner($this);
632
        }
633
        return $this;
634
    }
635
    /**
636
     * Remove phone
637
     *
638
     * @param B2bCustomerPhone $phone
639
     *
640
     * @return B2bCustomer
641
     */
642
    public function removePhone(B2bCustomerPhone $phone)
643
    {
644
        if ($this->phones->contains($phone)) {
645
            $this->phones->removeElement($phone);
646
        }
647
        return $this;
648
    }
649
    /**
650
     * Get phones
651
     *
652
     * @return Collection|B2bCustomerPhone[]
653
     */
654
    public function getPhones()
655
    {
656
        return $this->phones;
657
    }
658
    /**
659
     * @param B2bCustomerPhone $phone
660
     *
661
     * @return bool
662
     */
663
    public function hasPhone(B2bCustomerPhone $phone)
664
    {
665
        return $this->getPhones()->contains($phone);
666
    }
667
    /**
668
     * Gets primary phone if it's available.
669
     *
670
     * @return B2bCustomerPhone|null
671
     */
672
    public function getPrimaryPhone()
673
    {
674
        $result = null;
675
        foreach ($this->getPhones() as $phone) {
676
            if ($phone->isPrimary()) {
677
                $result = $phone;
678
                break;
679
            }
680
        }
681
        return $result;
682
    }
683
    /**
684
     * @param B2bCustomerPhone $phone
685
     *
686
     * @return B2bCustomer
687
     */
688 View Code Duplication
    public function setPrimaryPhone(B2bCustomerPhone $phone)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
689
    {
690
        if ($this->hasPhone($phone)) {
691
            $phone->setPrimary(true);
692
            foreach ($this->getPhones() as $otherPhone) {
693
                if (!$phone->isEqual($otherPhone)) {
694
                    $otherPhone->setPrimary(false);
695
                }
696
            }
697
        }
698
        return $this;
699
    }
700
701
    /**
702
     * Set emails.
703
     *
704
     * This method could not be named setEmails because of bug CRM-253.
705
     *
706
     * @param Collection|B2bCustomerEmail[] $emails
707
     *
708
     * @return B2bCustomer
709
     */
710
    public function resetEmails($emails)
711
    {
712
        $this->emails->clear();
713
        foreach ($emails as $email) {
714
            $this->addEmail($email);
715
        }
716
        return $this;
717
    }
718
    /**
719
     * Add email
720
     *
721
     * @param B2bCustomerEmail $email
722
     *
723
     * @return B2bCustomer
724
     */
725 View Code Duplication
    public function addEmail(B2bCustomerEmail $email)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
726
    {
727
        if (!$this->emails->contains($email)) {
728
            $this->emails->add($email);
729
            $email->setOwner($this);
730
        }
731
        return $this;
732
    }
733
    /**
734
     * Remove email
735
     *
736
     * @param B2bCustomerEmail $email
737
     *
738
     * @return B2bCustomer
739
     */
740
    public function removeEmail(B2bCustomerEmail $email)
741
    {
742
        if ($this->emails->contains($email)) {
743
            $this->emails->removeElement($email);
744
        }
745
        return $this;
746
    }
747
    /**
748
     * Get emails
749
     *
750
     * @return Collection|B2bCustomerEmail[]
751
     */
752
    public function getEmails()
753
    {
754
        return $this->emails;
755
    }
756
    /**
757
     * @param B2bCustomerEmail $email
758
     *
759
     * @return bool
760
     */
761
    public function hasEmail(B2bCustomerEmail $email)
762
    {
763
        return $this->getEmails()->contains($email);
764
    }
765
    /**
766
     * Gets primary email if it's available.
767
     *
768
     * @return B2bCustomerEmail|null
769
     */
770
    public function getPrimaryEmail()
771
    {
772
        $result = null;
773
        foreach ($this->getEmails() as $email) {
774
            if ($email->isPrimary()) {
775
                $result = $email;
776
                break;
777
            }
778
        }
779
        return $result;
780
    }
781
782
    /**
783
     * @param B2bCustomerEmail $email
784
     *
785
     * @return B2bCustomer
786
     */
787 View Code Duplication
    public function setPrimaryEmail(B2bCustomerEmail $email)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
788
    {
789
        if ($this->hasEmail($email)) {
790
            $email->setPrimary(true);
791
            foreach ($this->getEmails() as $otherEmail) {
792
                if (!$email->isEqual($otherEmail)) {
793
                    $otherEmail->setPrimary(false);
794
                }
795
            }
796
        }
797
        return $this;
798
    }
799
}
800