src/Entity/Contact.php line 72

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use App\Entity\Reference\ReferenceContactType;
  5. use App\Repository\ContactRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use ApiPlatform\Core\Annotation\ApiResource;
  10. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  11. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  12. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. use Symfony\Component\Serializer\Annotation\SerializedName;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. use App\Filter\ContactFilter;
  17. use App\Filter\RecommenderFilter;
  18. /**
  19.  * @ApiResource(
  20.  *     security= "is_granted('ROLE_AGENT') or is_granted('ROLE_MANAGER')",
  21.  *     normalizationContext={"groups"={"contact:read"}, "swagger_definition_name"="Read"},
  22.  *     denormalizationContext={"groups"={"contact:write"}, "swagger_definition_name"="Write","skip_null_values" = true},
  23.  *     itemOperations={
  24.  *          "get"={"security"="is_granted('ROLE_AGENT') or is_granted('ROLE_MANAGER')"},
  25.  *          "put" = {
  26.  *                  "denormalization_context"={"groups"={"contact:put"}},
  27.  *                  "access_control"="is_granted('ROLE_AGENT') or is_granted('ROLE_MANAGER')",
  28.  *              },
  29.  *
  30.  *     },
  31.  *     collectionOperations={
  32.  *          "get"={"security"="is_granted('ROLE_AGENT') or is_granted('ROLE_MANAGER')"},
  33.  *          "post"={"security"="is_granted('ROLE_AGENT') or is_granted('ROLE_MANAGER')"}
  34.  *
  35.  *     },
  36.  *     attributes={
  37.  *          "pagination_items_per_page"=10,
  38.  *          "formats"={"jsonld", "json", "html", "jsonhal", "csv"={"text/csv"} },
  39.  *          "order"={"id":"DESC"}
  40.  *     }
  41.  * )
  42.  * @ApiFilter(OrderFilter::class, properties={"id","firstName","lastName","contactTypeReference.name","createdAt"} ,
  43.  *              arguments= {"orderParameterName" : "order"})
  44.  * @ApiFilter(SearchFilter::class, properties={
  45.  *     "contactTypeReference.id": "exact",
  46.  *     "contactTypeReference.code": "exact",
  47.  *     "createdAt[before]": "exact",
  48.  * })
  49.  * @ApiFilter(RecommenderFilter::class, properties={"recommender"})
  50.  * @ApiFilter(ContactFilter::class, properties={"collaborator.id" : "exact"})
  51.  * @ORM\Entity(repositoryClass=ContactRepository::class)
  52.  * @UniqueEntity(
  53.  *          fields={"cin" },
  54.  *          entityClass=Contact::class,
  55.  *          message="AbstractPeople with cin {{ value }} is already used."
  56.  * )
  57.  * @UniqueEntity(
  58.  *           fields={"email"},
  59.  *           entityClass=Contact::class,
  60.  *           message="AbstractPeople with email {{ value }} is already used."
  61.  *     )
  62.  * @UniqueEntity(
  63.  *           fields={"mainPhone"},
  64.  *           entityClass=Contact::class,
  65.  *           message="AbstractPeople with main phone {{ value }} is already used."
  66.  *     )
  67.  * @ORM\HasLifecycleCallbacks()
  68.  */
  69. class Contact extends AbstractPeople implements Loggable
  70. {
  71.     public const CONTACT_TYPE_BUYER 'buyer';
  72.     public const CONTACT_TYPE_SELLER 'seller';
  73.     public const ALL_CONTACT_TYPE = [self::CONTACT_TYPE_SELLERself::CONTACT_TYPE_BUYER];
  74.     /**
  75.      * @ORM\ManyToOne(targetEntity=Agent::class, inversedBy="contacts")
  76.      * @ORM\JoinColumn(nullable=false)
  77.      * @Groups({
  78.      *    "contact:read","property:read",
  79.      *    "siege:write","siege:put"
  80.      * })
  81.      */
  82.     private ?Agent $collaborator;
  83.     /**
  84.      * @ORM\ManyToOne(targetEntity=ReferenceContactType::class, inversedBy="contacts")
  85.      * @ORM\JoinColumn(nullable=false)
  86.      * @Assert\NotBlank
  87.      * @Groups({
  88.      *    "contact:read","contact:write","contact:put",
  89.      *    "property:read",
  90.      *    "transaction:read",
  91.      *    "mandate:read",
  92.      *    "business-indication:item:read",
  93.      *    "property-visit:read",
  94.      * })
  95.      */
  96.     private ?ReferenceContactType $contactTypeReference;
  97.     /**
  98.      * @ORM\OneToMany(targetEntity=Property::class, mappedBy="contact")
  99.      */
  100.     private $properties;
  101.     /**
  102.      * @ORM\OneToMany(targetEntity=Transaction::class, mappedBy="contact")
  103.      */
  104.     private $transactions;
  105.     /**
  106.      * @ORM\OneToMany(targetEntity=PropertyVisitVoucher::class, mappedBy="contact")
  107.      * @Groups({
  108.      *    "contact:write","contact:put",
  109.      *    "mandate:read",
  110.      * })
  111.      */
  112.     private $propertyVisitVouchers;
  113.     /**
  114.      * @ORM\ManyToOne(targetEntity=Agent::class)
  115.      * @Groups({
  116.      *   "contact:read","contact:write","siege:put","contact:put",
  117.      *    "property:read"
  118.      * })
  119.      */
  120.     private ?Agent $recommender;
  121.     /**
  122.      * @ORM\Column(type="boolean", nullable=true, options={"default"=false})
  123.      * @Groups({
  124.      *    "contact:read","siege:write","siege:put",
  125.      *    "property:read",
  126.      *    "business-indication:item:read",
  127.      *    "business-indication:collection:read"
  128.      * })
  129.      */
  130.     private ?bool $isBusinessIndication false;
  131.     /**
  132.      * @ORM\Column(type="boolean", nullable=true)
  133.      * @Groups({
  134.      *    "contact:read","contact:write","contact:put",
  135.      *    "mandate:read",
  136.      * })
  137.      */
  138.     private ?bool $isPreoccupation false;
  139.     /**
  140.      * @ORM\OneToOne(targetEntity=ProxyContact::class, cascade={"persist", "remove"})
  141.      * @Assert\Valid
  142.      * @Groups({
  143.      *   "contact:read","contact:write","contact:put",
  144.      *   "mandate:read"
  145.      * })
  146.      */
  147.     private ?ProxyContact $proxyContact;
  148.     public function __construct()
  149.     {
  150.         $this->properties = new ArrayCollection();
  151.         $this->transactions = new ArrayCollection();
  152.         $this->propertyVisitVouchers = new ArrayCollection();
  153.     }
  154.     /**
  155.      * @SerializedName("countPropertyVisit")
  156.      * @Groups({
  157.      *    "contact:read",
  158.      * })
  159.      */
  160.     public function getCountTotalPropertyVisit(): int
  161.     {
  162.         return $this->propertyVisitVouchers->filter(
  163.             fn (PropertyVisitVoucher $visit) => $visit->getCreatedBy() === $this->getCollaborator()->getId()
  164.         )->count();
  165.     }
  166.     /**
  167.      * @SerializedName("countBusinessIndication")
  168.      * @Groups({
  169.      *    "contact:read"
  170.      * })
  171.      */
  172.     public function getCountTotalBusinessIndication(): int
  173.     {
  174.         return count(
  175.             $this->transactions->filter(
  176.                 fn (AbstractTransaction $transaction) => $transaction instanceof BusinessIndication
  177.             )
  178.         );
  179.     }
  180.     public function getCollaborator(): ?Agent
  181.     {
  182.         return $this->collaborator;
  183.     }
  184.     public function setCollaborator(?Agent $collaborator): self
  185.     {
  186.         $this->collaborator $collaborator;
  187.         return $this;
  188.     }
  189.     public function getContactTypeReference(): ?ReferenceContactType
  190.     {
  191.         return $this->contactTypeReference;
  192.     }
  193.     public function setContactTypeReference(?ReferenceContactType $contactTypeReference): self
  194.     {
  195.         $this->contactTypeReference $contactTypeReference;
  196.         return $this;
  197.     }
  198.     /**
  199.      * @return Collection|Property[]
  200.      */
  201.     public function getProperties(): Collection
  202.     {
  203.         return $this->properties;
  204.     }
  205.     public function addProperty(Property $property): self
  206.     {
  207.         if (!$this->properties->contains($property)) {
  208.             $this->properties[] = $property;
  209.             $property->setContact($this);
  210.         }
  211.         return $this;
  212.     }
  213.     public function removeProperty(Property $property): self
  214.     {
  215.         // set the owning side to null (unless already changed)
  216.         if ($this->properties->removeElement($property) && $property->getContact() === $this) {
  217.             $property->setContact(null);
  218.         }
  219.         return $this;
  220.     }
  221.     /**
  222.      * @return Collection<int, Transaction>
  223.      */
  224.     public function getTransactions(): Collection
  225.     {
  226.         return $this->transactions;
  227.     }
  228.     public function addTransaction(Transaction $transaction): self
  229.     {
  230.         if (!$this->transactions->contains($transaction)) {
  231.             $this->transactions[] = $transaction;
  232.             $transaction->setContact($this);
  233.         }
  234.         return $this;
  235.     }
  236.     public function removeTransaction(Transaction $transaction): self
  237.     {
  238.         if ($this->transactions->removeElement($transaction)) {
  239.             // set the owning side to null (unless already changed)
  240.             if ($transaction->getContact() === $this) {
  241.                 $transaction->setContact(null);
  242.             }
  243.         }
  244.         return $this;
  245.     }
  246.     /**
  247.      * @return Collection<int, PropertyVisitVoucher>
  248.      */
  249.     public function getPropertyVisitVouchers(): Collection
  250.     {
  251.         return $this->propertyVisitVouchers;
  252.     }
  253.     public function addPropertyVisitVoucher(PropertyVisitVoucher $propertyVisitVoucher): self
  254.     {
  255.         if (!$this->propertyVisitVouchers->contains($propertyVisitVoucher)) {
  256.             $this->propertyVisitVouchers[] = $propertyVisitVoucher;
  257.             $propertyVisitVoucher->setContact($this);
  258.         }
  259.         return $this;
  260.     }
  261.     public function removePropertyVisitVoucher(PropertyVisitVoucher $propertyVisitVoucher): self
  262.     {
  263.         // set the owning side to null (unless already changed)
  264.         if (
  265.             $this->propertyVisitVouchers->removeElement($propertyVisitVoucher)
  266.             && $propertyVisitVoucher->getContact() === $this
  267.         ) {
  268.             $propertyVisitVoucher->setContact(null);
  269.         }
  270.         return $this;
  271.     }
  272.     public function getRecommender(): ?Agent
  273.     {
  274.         return $this->recommender;
  275.     }
  276.     public function setRecommender(?Agent $recommender): self
  277.     {
  278.         $this->recommender $recommender;
  279.         return $this;
  280.     }
  281.     /**
  282.      * @return bool|null
  283.      */
  284.     public function getIsBusinessIndication(): ?bool
  285.     {
  286.         return $this->isBusinessIndication;
  287.     }
  288.     /**
  289.      * @param bool $isBusinessIndication
  290.      *
  291.      * @return $this
  292.      */
  293.     public function setIsBusinessIndication(bool $isBusinessIndication): self
  294.     {
  295.         $this->isBusinessIndication $isBusinessIndication;
  296.         return $this;
  297.     }
  298.     public function getIsPreoccupation(): ?bool
  299.     {
  300.         return $this->isPreoccupation;
  301.     }
  302.     public function setIsPreoccupation(?bool $isPreoccupation): self
  303.     {
  304.         $this->isPreoccupation $isPreoccupation;
  305.         return $this;
  306.     }
  307.     public function getProxyContact(): ?ProxyContact
  308.     {
  309.         return $this->proxyContact;
  310.     }
  311.     public function setProxyContact(?ProxyContact $proxyContact): self
  312.     {
  313.         $this->proxyContact $proxyContact;
  314.         return $this;
  315.     }
  316.     /**
  317.      * @return array
  318.      */
  319.     public function getFields(): array
  320.     {
  321.         return [
  322.             'gender' => 'gender' ,
  323.             'address' => 'address' ,
  324.             'email' => 'email' ,
  325.             'mainPhone' => 'mainPhone' ,
  326.             'birthDay' => 'birthDay' ,
  327.             'cin' => 'cin' ,
  328.             'rc' => 'rc' ,
  329.             'commonCompanyIdentifier' => 'commonCompanyIdentifier' ,
  330.             'companyName' => 'companyName' ,
  331.             'contactTypeReference' => 'contactTypeReference' ,
  332.             'recommender' => 'recommender'
  333.         ];
  334.     }
  335. }