<?php
namespace App\Entity\Channel;
use App\Entity\Crm\Opportunity\Opportunity;
use App\Entity\Crm\Target\TargetableInterface;
use App\Enum\TargetTypeEnum;
use App\Enum\Serialization\CompanySerializationGroups;
use App\Enum\Serialization\ReverseSyncSerializationGroups;
use App\Repository\Channel\CompanyRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Drosalys\Bundle\ApiBundle\Serializer\Attributes\IdGroups;
use Nellapp\Bundle\SDKBundle\Channel\Entity\Company\CompanyInterface;
use Nellapp\Bundle\SDKBundle\Channel\Entity\Company\CompanyMethodTrait;
use Nellapp\Bundle\SDKBundle\Entity\Common\Address\Address;
use Nellapp\Bundle\SDKBundle\Enum\Company\IndustryEnum;
use Nellapp\Bundle\SDKBundle\ReverseSync\Entity\ReverseSyncableInterface;
use Nellapp\Bundle\SDKBundle\ReverseSync\EntityKeys;
use Symfony\Component\Serializer\Annotation as Serial;
use Symfony\Component\Validator\Constraints as Assert;
#[
ORM\Entity(repositoryClass: CompanyRepository::class),
ORM\Table(name: 'channel_company'),
]
class Company implements CompanyInterface, TargetableInterface, ReverseSyncableInterface
{
use CompanyMethodTrait;
#[
ORM\Id,
ORM\GeneratedValue(strategy: 'NONE'),
ORM\Column(type: 'string', length: 36)
]
#[Serial\Groups([
CompanySerializationGroups::COLLECTION,
CompanySerializationGroups::DETAIL,
ReverseSyncSerializationGroups::REVERSE_SYNC,
])]
private ?string $id = null;
#[ORM\Column(type: 'string')]
#[Serial\Groups([
CompanySerializationGroups::COLLECTION,
CompanySerializationGroups::DETAIL,
CompanySerializationGroups::CREATE,
ReverseSyncSerializationGroups::REVERSE_SYNC,
])]
#[
Assert\NotBlank,
Assert\Length(
min: 2,
max: 255,
),
]
private ?string $name = null;
#[ORM\Column(type: 'string', nullable: true)]
#[
Assert\NotBlank(message: 'user.entity.email.NotBlank'),
Assert\Email(message: 'user.entity.email.Email', mode: 'strict'),
Assert\Length(
min: 6,
max: 64,
)
]
#[Serial\Groups([
CompanySerializationGroups::CREATE,
ReverseSyncSerializationGroups::REVERSE_SYNC,
])]
private ?string $email = null;
#[ORM\Column(type: 'string', length: 14, nullable: true)]
#[Serial\Groups([
CompanySerializationGroups::COLLECTION,
CompanySerializationGroups::DETAIL,
CompanySerializationGroups::CREATE,
ReverseSyncSerializationGroups::REVERSE_SYNC,
])]
#[
Assert\Length(
min: 9,
max: 14,
),
Assert\Luhn(
message: 'channel.professional_id.french.siret.luhn'
),
]
private ?string $siret = null;
#[ORM\Column(type: 'string', length: 32, nullable: true)]
#[
Assert\Choice(callback: [IndustryEnum::class, 'getChoices']),
]
#[Serial\Groups([
CompanySerializationGroups::COLLECTION,
CompanySerializationGroups::DETAIL,
CompanySerializationGroups::CREATE,
ReverseSyncSerializationGroups::REVERSE_SYNC,
])]
private ?string $industry = null;
#[ORM\Column(type: 'string', nullable: true)]
#[Serial\Groups([
CompanySerializationGroups::COLLECTION,
CompanySerializationGroups::DETAIL,
CompanySerializationGroups::CREATE,
ReverseSyncSerializationGroups::REVERSE_SYNC,
])]
#[
Assert\Length(
min: 8,
max: 20,
)
]
private ?string $phoneNumber = null;
#[ORM\Column(type: 'integer', nullable: true)]
#[Assert\GreaterThanOrEqual(value: 0)]
#[Serial\Groups([
CompanySerializationGroups::COLLECTION,
CompanySerializationGroups::DETAIL,
CompanySerializationGroups::CREATE,
ReverseSyncSerializationGroups::REVERSE_SYNC,
])]
private ?int $countEmployees = null;
#[ORM\Embedded(class: Address::class)]
private ?Address $address = null;
#[ORM\ManyToOne(targetEntity: Channel::class)]
#[ORM\JoinColumn(nullable: false, onDelete: "CASCADE")]
#[IdGroups([ReverseSyncSerializationGroups::REVERSE_SYNC])]
private ?Channel $channel = null;
#[ORM\OneToMany(mappedBy: 'targetCompany', targetEntity: Opportunity::class)]
private Collection $opportunities;
#[ORM\OneToMany(mappedBy: 'company', targetEntity: CompanyContact::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[Assert\Valid()]
private Collection $companyContacts;
public function __construct()
{
$this->initCompanyMethod();
$this->opportunities = new ArrayCollection();
}
public function __toString(): string
{
return $this->getDisplayName();
}
public static function getReverseSyncKey(): string
{
return EntityKeys::COMPANY;
}
public function getDisplayName(): string
{
return $this->name ?? '';
}
public function getOpportunityTargetType(): string
{
return TargetTypeEnum::COMPANY;
}
#[Serial\Groups([
CompanySerializationGroups::COLLECTION,
CompanySerializationGroups::DETAIL,
])]
public function getFullAddress(): ?string
{
return $this->address?->toMultiLine();
}
public function getPostCodeAndCity(): string
{
return $this->address?->getPostCodeAndCity() ?? '';
}
/**
* @return Collection<int, Opportunity>
*/
public function getOpportunities(): Collection
{
return $this->opportunities;
}
public function addOpportunity(Opportunity $opportunity): static
{
if (!$this->opportunities->contains($opportunity)) {
$this->opportunities->add($opportunity);
$opportunity->setTargetCompany($this);
}
return $this;
}
public function removeOpportunity(Opportunity $opportunity): static
{
if ($this->opportunities->removeElement($opportunity)) {
// set the owning side to null (unless already changed)
if ($opportunity->getTargetCompany() === $this) {
$opportunity->setTargetCompany(null);
}
}
return $this;
}
}