<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Reference\ReferenceCollaboratorState;
use App\Repository\AgentRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Controller\Agent\Agent\ActivateAgentController;
use App\Controller\Manager\Agent\CreateAgentController;
use App\Controller\Agent\Agent\DisableAgentController;
use App\Controller\Agent\Agent\SuspendAgentController;
use App\Controller\Agent\Agent\ArchiveAgentController;
use App\Controller\Agent\Agent\AcceptTermsController;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
use App\EventListener\AgentListener;
/**
* @ApiResource(
* security = "is_granted('ROLE_AGENT') or is_granted('ROLE_MANAGER')",
* normalizationContext = { "groups" = {"agent:read"}, "swagger_definition_name" = "Read" },
* denormalizationContext = { "groups" = {"agent:write"}, "swagger_definition_name" = "Write" },
* itemOperations={
* "get",
* "put" = {
* "denormalization_context" = { "groups" = {"profile:update"} },
* "security" = "is_granted('PROFILE_UPDATE', object)",
* },
* "activate_agent" = {
* "denormalization_context"={"groups"={"agent:activate"}},
* "access_control"="is_granted('ROLE_MANAGER')",
* "method"="PATCH",
* "path"="/agent/{id}/activate",
* "controller"=ActivateAgentController::class,
* "read"=true,
* "write"=false,
* },
* "disable_agent" = {
* "denormalization_context"={"groups"={"agent:disable"}},
* "access_control"="is_granted('ROLE_MANAGER')",
* "method"="PATCH",
* "path"="/agent/{id}/disable",
* "controller"=DisableAgentController::class,
* "read"=true,
* "write"=false,
* },
* "suspend_agent" = {
* "denormalization_context"={"groups"={"agent:suspend"}},
* "access_control"="is_granted('ROLE_MANAGER')",
* "method"="PATCH",
* "path"="/agent/{id}/suspend",
* "controller"=SuspendAgentController::class,
* "read"=true,
* "write"=false,
* },
* "archive_agent" = {
* "denormalization_context"={"groups"={"agent:archive"}},
* "access_control"="is_granted('ROLE_MANAGER')",
* "method"="PATCH",
* "path"="/agent/{id}/archive",
* "controller"=ArchiveAgentController::class,
* "read"=true,
* "write"=false,
* }
* },
* collectionOperations = {
* "get",
* "post" = {
* "security"="is_granted('ROLE_MANAGER')",
* "controller"=CreateAgentController::class,
* },
* "accept_terms" = {
* "method"="POST",
* "access_control"="is_granted('ROLE_AGENT')",
* "path"="/agent/accept_terms",
* "controller"=AcceptTermsController::class,
* "openapi_context"={
* "summary"="Accept terms",
* "description"="Accept terms"
* }
*
* },
* "milkiya_agents"={
* "security"="is_granted('PUBLIC_ACCESS')",
* "access_control"="is_granted('PUBLIC_ACCESS')",
* "normalization_context"={"groups"={"milkiya:read"}},
* "method"="GET",
* "path"="/internal/agents",
* "pagination_enabled" = false,
* "formats"={"json"}
* },
* "list_agent_light"={
* "normalization_context"={"groups"={"agent:light"}},
* "method"="GET",
* "path"="/agents/light",
* "pagination_enabled" = false
* },
* },
* attributes={
* "formats"={"jsonld", "json", "html", "jsonhal", "csv"={"text/csv"} }
* }
* )
* @ApiFilter(SearchFilter::class, properties={
* "roles",
* "status.id": "exact"
* })
* @ApiFilter(BooleanFilter::class, properties={"isRecommender"})
* @ORM\Entity(repositoryClass=AgentRepository::class)
* @UniqueEntity(
* fields={"cin" },
* entityClass=Agent::class,
* message="AbstractPeople with cin {{ value }} is already used."
* )
* @UniqueEntity(
* fields={"email"},
* entityClass=Agent::class,
* message="AbstractPeople with email {{ value }} is already used."
* )
* @UniqueEntity(
* fields={"mainPhone"},
* entityClass=Agent::class,
* message="AbstractPeople with main phone {{ value }} is already used."
* )
* @UniqueEntity(
* fields={"email"},
* entityClass=Agent::class,
* message="AbstractPeople with email {{ value }} is already used."
* )
* @UniqueEntity(
* fields={"professionalEmail"},
* entityClass=Agent::class,
* message="AbstractPeople with emailPro {{ value }} is already used."
* )
* @ORM\HasLifecycleCallbacks()
* @ORM\EntityListeners({AgentListener::class})
*/
class Agent extends AbstractCollaborator
{
/**
* @ORM\ManyToOne(targetEntity=QualificationLevel::class, inversedBy="collaborators")
* @ORM\JoinColumn(nullable=true)
* @Groups({
* "candidate:read",
* "genealogy:read",
* "abstract-collaborator:read",
* "directory:collaborator:search",
* "agent:read","siege:put","siege:write",
* "milkiya:read"
* })
*
*/
private ?QualificationLevel $qualificationLevel;
/**
* @ORM\OneToMany(targetEntity=Genealogy::class, mappedBy="collaborator")
*/
private $genealogies;
/**
* @ORM\OneToMany(targetEntity=Property::class, mappedBy="collaborator")
*/
private $properties;
/**
* @ORM\OneToMany(targetEntity=Candidate::class, mappedBy="sponsor")
*/
private $candidates;
/**
* @ORM\OneToMany(targetEntity=Contact::class, mappedBy="collaborator")
*/
private $contacts;
/**
* @ORM\OneToMany(targetEntity=Prescriber::class, mappedBy="collaborator")
*/
private $prescribers;
/**
* @ORM\OneToMany(targetEntity=Property::class, mappedBy="prescriber")
*/
private $prescriberProperties;
/**
* @ORM\OneToOne(targetEntity=Sector::class, cascade={"persist", "remove"})
*
* @Groups({
*
* "directory:collaborator:search",
* "siege:put",
* "mandate:read",
* "agent:read",
* "siege:write"
* })
*/
private ?Sector $sector = null;
/**
* @ORM\ManyToOne(targetEntity=Manager::class, inversedBy="agents")
* @Groups({
* "mandate:read",
* "agent:read",
* "siege:write"
* })
*/
private ?Manager $manager;
/**
* @ORM\ManyToOne(targetEntity=ReferenceCollaboratorState::class, inversedBy="agents")
* @Groups({
* "abstract-collaborator:read",
* "agent:read","siege:put","siege:write"
*
* })
*/
private ?ReferenceCollaboratorState $status;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Groups({
* "abstract-collaborator:read",
* "directory:collaborator:search",
* "agent:read","siege:put","siege:write"
* })
*/
private ?string $cin;
/**
* @ORM\Column(type="boolean")
* @Groups({
* "candidate:read",
* "genealogy:read",
* "abstract-collaborator:read",
* "directory:collaborator:search",
* "agent:read","siege:write"
* })
*/
private bool $isRecommender = false;
/**
* @ORM\OneToMany(targetEntity=Property::class, mappedBy="recommendation")
*/
private $recommandations;
/**
* @ORM\ManyToOne(targetEntity=Agent::class)
* @Groups({
* "abstract-collaborator:read",
* "directory:collaborator:search",
* "agent:read","siege:write"
* })
*/
private ?Agent $sponsor;
/**
* @ORM\Column(type="string", length=100, nullable=true)
* @Groups({
* "abstract-collaborator:read",
* "directory:collaborator:search",
* "agent:read","siege:put","siege:write"
* })
*/
private $cnss;
/**
* @ORM\Column(type="string", length=100, nullable=true)
* @Groups({
* "abstract-collaborator:read",
* "directory:collaborator:search",
* "agent:read","siege:put","siege:write"
* })
*/
private $rc;
/**
* @ORM\Column(type="string", length=100, nullable=true)
* @Groups({
* "abstract-collaborator:read",
* "directory:collaborator:search",
* "agent:read","siege:put","siege:write"
* })
*/
private $professionalStatus;
/**
* @ORM\ManyToOne(targetEntity=Nationality::class)
* @Groups({
* "abstract-collaborator:read",
* "directory:collaborator:search",
* "agent:read","siege:put","siege:write"
* })
*/
private ?Nationality $nationality;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Groups({
* "abstract-collaborator:read",
* "directory:collaborator:search",
* "agent:read","siege:put", "siege:write"
* })
*/
private $subjectToVAT;
/**
* @ORM\Column(type="date_immutable", nullable=true)
* @Groups({
* "abstract-collaborator:read",
* "directory:collaborator:search",
* "agent:read","siege:put"
* })
*/
private ?\DateTimeImmutable $createdQualifiqautionDate;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Groups({
* "abstract-collaborator:read",
* "directory:collaborator:search",
* "agent:read","siege:put","siege:write"
* })
*/
private ?string $companyName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Groups({
* "abstract-collaborator:read",
* "directory:collaborator:search",
* "agent:read","siege:put","siege:write"
* })
*/
private ?string $commonCompanyIdentifier;
/**
* @ORM\OneToMany(targetEntity=IdentityVerificationImage::class, mappedBy="agent")
*
* @Groups({
* "directory:collaborator:search",
* "agent:read"
* })
*/
private ?Collection $identityFiles = null;
/**
* @ORM\OneToMany(targetEntity=AgentsOptions::class, mappedBy="agent")
*
* @Groups({
* "abstract-collaborator:read",
* "directory:collaborator:search",
* "agent:read","siege:put","siege:write",
* "milkiya:read", "agent:put", "contact:read"
* })
*/
private ?Collection $agentOptions = null;
public function __construct()
{
parent::__construct();
$this->genealogies = new ArrayCollection();
$this->properties = new ArrayCollection();
$this->candidates = new ArrayCollection();
$this->contacts = new ArrayCollection();
$this->prescribers = new ArrayCollection();
$this->prescriberProperties = new ArrayCollection();
$this->recommandations = new ArrayCollection();
$this->identityFiles = new ArrayCollection();
$this->agentOptions = new ArrayCollection();
}
public function getQualificationLevel(): ?QualificationLevel
{
return $this->qualificationLevel;
}
public function setQualificationLevel(?QualificationLevel $qualificationLevel): self
{
$this->qualificationLevel = $qualificationLevel;
return $this;
}
/**
* @return Collection|Genealogy[]
*/
public function getGenealogies(): Collection
{
return $this->genealogies;
}
public function addGenealogy(Genealogy $genealogy): self
{
if (!$this->genealogies->contains($genealogy)) {
$this->genealogies[] = $genealogy;
$genealogy->setCollaborator($this);
}
return $this;
}
public function removeGenealogy(Genealogy $genealogy): self
{
// set the owning side to null (unless already changed)
if ($this->genealogies->removeElement($genealogy) && $genealogy->getCollaborator() === $this) {
$genealogy->setCollaborator(null);
}
return $this;
}
/**
* @return Collection|Property[]
*/
public function getProperties(): Collection
{
return $this->properties;
}
public function addProperty(Property $property): self
{
if (!$this->properties->contains($property)) {
$this->properties[] = $property;
$property->setCollaborator($this);
}
return $this;
}
public function removeProperty(Property $property): self
{
// set the owning side to null (unless already changed)
if ($this->properties->removeElement($property) && $property->getCollaborator() === $this) {
$property->setCollaborator(null);
}
return $this;
}
/**
* @return Collection|Candidate[]
*/
public function getCandidates(): Collection
{
return $this->candidates;
}
public function addCandidate(Candidate $candidate): self
{
if (!$this->candidates->contains($candidate)) {
$this->candidates[] = $candidate;
$candidate->setSponsor($this);
}
return $this;
}
public function removeCandidate(Candidate $candidate): self
{
// set the owning side to null (unless already changed)
if ($this->candidates->removeElement($candidate) && $candidate->getSponsor() === $this) {
$candidate->setSponsor(null);
}
return $this;
}
/**
* @return Collection|Contact[]
*/
public function getContacts(): Collection
{
return $this->contacts;
}
public function addContact(Contact $contact): self
{
if (!$this->contacts->contains($contact)) {
$this->contacts[] = $contact;
$contact->setCollaborator($this);
}
return $this;
}
public function removeContact(Contact $contact): self
{
// set the owning side to null (unless already changed)
if ($this->contacts->removeElement($contact) && $contact->getCollaborator() === $this) {
$contact->setCollaborator(null);
}
return $this;
}
/**
* @return Collection|Prescriber[]
*/
public function getPrescribers(): Collection
{
return $this->prescribers;
}
public function addPrescriber(Prescriber $prescriber): self
{
if (!$this->prescribers->contains($prescriber)) {
$this->prescribers[] = $prescriber;
$prescriber->setCollaborator($this);
}
return $this;
}
public function removePrescriber(Prescriber $prescriber): self
{
// set the owning side to null (unless already changed)
if ($this->prescribers->removeElement($prescriber) && $prescriber->getCollaborator() === $this) {
$prescriber->setCollaborator(null);
}
return $this;
}
/**
* @return Collection|Property[]
*/
public function getPrescriberProperties(): Collection
{
return $this->prescriberProperties;
}
public function addPrescriberProperty(Property $prescriberProperty): self
{
if (!$this->prescriberProperties->contains($prescriberProperty)) {
$this->prescriberProperties[] = $prescriberProperty;
$prescriberProperty->setPrescriber($this);
}
return $this;
}
public function removePrescriberProperty(Property $prescriberProperty): self
{
// set the owning side to null (unless already changed)
if (
$this->prescriberProperties->removeElement($prescriberProperty) &&
$prescriberProperty->getPrescriber() === $this
) {
$prescriberProperty->setPrescriber(null);
}
return $this;
}
public function getSector(): ?Sector
{
return $this->sector;
}
public function setSector(?Sector $sector): self
{
$this->sector = $sector;
return $this;
}
public function getManager(): ?Manager
{
return $this->manager;
}
public function setManager(?Manager $manager): self
{
$this->manager = $manager;
return $this;
}
public function getStatus(): ?ReferenceCollaboratorState
{
return $this->status;
}
/**
* @SerializedName ("referenceCollaboratorState")
*/
public function setStatus(?ReferenceCollaboratorState $status): self
{
$this->status = $status;
return $this;
}
public function removeRecommandation(Recommandation $recommandation): self
{
// set the owning side to null (unless already changed)
if ($this->recommandations->removeElement($recommandation) && $recommandation->getAgent() === $this) {
$recommandation->setAgent(null);
}
return $this;
}
public function getCin(): ?string
{
return $this->cin;
}
public function setCin(?string $cin): self
{
$this->cin = $cin;
return $this;
}
public function getIsRecommender(): ?bool
{
return $this->isRecommender;
}
public function setIsRecommender(bool $isRecommender): self
{
$this->isRecommender = $isRecommender;
return $this;
}
/**
* @return Collection<int, Property>
*/
public function getRecommandations(): Collection
{
return $this->recommandations;
}
public function addRecommandation(Property $recommandation): self
{
if (!$this->recommandations->contains($recommandation)) {
$this->recommandations[] = $recommandation;
$recommandation->setRecommendation($this);
}
return $this;
}
public function getSponsor(): ?self
{
return $this->sponsor;
}
public function setSponsor(?self $sponsor): self
{
$this->sponsor = $sponsor;
return $this;
}
public function getCnss(): ?string
{
return $this->cnss;
}
public function setCnss(?string $cnss): self
{
$this->cnss = $cnss;
return $this;
}
public function getRc(): ?string
{
return $this->rc;
}
public function setRc(?string $rc): self
{
$this->rc = $rc;
return $this;
}
public function getProfessionalStatus(): ?string
{
return $this->professionalStatus;
}
public function setProfessionalStatus(?string $professionalStatus): self
{
$this->professionalStatus = $professionalStatus;
return $this;
}
public function getNationality(): ?Nationality
{
return $this->nationality;
}
public function setNationality(?Nationality $nationality): self
{
$this->nationality = $nationality;
return $this;
}
public function getSubjectToVAT(): ?bool
{
return $this->subjectToVAT;
}
public function setSubjectToVAT(?bool $subjectToVAT): self
{
$this->subjectToVAT = $subjectToVAT;
return $this;
}
/**
* @return \DateTimeImmutable|null
*/
public function getCreatedQualifiqautionDate(): ?\DateTimeImmutable
{
return $this->createdQualifiqautionDate;
}
/**
* @param \DateTimeImmutable|null $createdQualifiqautionDate
*/
public function setCreatedQualifiqautionDate(?\DateTimeImmutable $createdQualifiqautionDate): self
{
$this->createdQualifiqautionDate = $createdQualifiqautionDate;
return $this;
}
public function getCompanyName(): ?string
{
return $this->companyName;
}
public function setCompanyName(?string $companyName): self
{
$this->companyName = $companyName;
return $this;
}
public function getCommonCompanyIdentifier(): ?string
{
return $this->commonCompanyIdentifier;
}
public function setCommonCompanyIdentifier(?string $commonCompanyIdentifier): self
{
$this->commonCompanyIdentifier = $commonCompanyIdentifier;
return $this;
}
/**
* @return Collection<int, IdentityVerificationImage>
*/
public function getIdentityFiles(): Collection
{
return $this->identityFiles;
}
public function addIdentityFile(IdentityVerificationImage $identityFile): self
{
if (!$this->identityFiles->contains($identityFile)) {
$this->identityFiles[] = $identityFile;
$identityFile->setAgent($this);
}
return $this;
}
public function removeIdentityFile(IdentityVerificationImage $identityFile): self
{
if ($this->identityFiles->removeElement($identityFile)) {
if ($identityFile->getAgent() === $this) {
$identityFile->setAgent(null);
}
}
return $this;
}
/**
* @Groups({"milkiya:read"})
* @SerializedName("sector_city")
*/
public function getMainCityName(): ?string
{
if (null === $sector = $this->getSector()) {
return null;
}
return $sector->getMainCity() ? $sector->getMainCity()->getName() : null;
}
/**
* @Groups({"milkiya:read"})
* @SerializedName("sector")
*/
public function getSectorInfo(): array
{
$citiesNeighborhoods = [];
if (null === $sector = $this->getSector()) {
return $citiesNeighborhoods;
}
$cities = $sector->getCities();
$neighborhoods = $sector->getNeighborhoods();
foreach ($cities as $city) {
$neighborhoodList = [];
foreach ($city->getNeighborhoods() as $neighborhood) {
if ($neighborhoods->contains($neighborhood)) {
$neighborhoodList[] = [
'id' => $neighborhood->getId(),
'name' => $neighborhood->getName(),
'latitude' => $neighborhood->getLatitude(),
'longitude' => $neighborhood->getLongitude()
];
}
}
$cityInfo = [
'city' => [
'id' => $city->getId(),
'name' => $city->getName(),
'country' => $city->getCountry()->getName()
],
'neighborhoods' => $neighborhoodList,
];
$citiesNeighborhoods[] = $cityInfo;
}
return $citiesNeighborhoods;
}
/**
* @return Collection<int, AgentsOptions>
*/
public function getAgentOptions(): ?Collection
{
return $this->agentOptions;
}
public function addAgentOptions(AgentsOptions $agentOptions): self
{
if (!$this->agentOptions->contains($agentOptions)) {
$this->agentOptions[] = $agentOptions;
$agentOptions->setAgent($this);
}
return $this;
}
public function removeAgentOptions(AgentsOptions $agentOptions): self
{
if ($this->agentOptions->removeElement($agentOptions)) {
// set the owning side to null (unless already changed)
if ($agentOptions->getAgent() === $this) {
$agentOptions->setAgent(null);
}
}
return $this;
}
}