src/Entity/Empresa.php line 64

  1. <?php
  2. namespace App\Entity;
  3. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  4. use Symfony\Component\Validator\Constraints as Assert;
  5. use Symfony\Component\Serializer\Annotation\Groups;
  6. use Symfony\Component\HttpFoundation\File\File;
  7. use Ramsey\Uuid\Uuid;
  8. use Gedmo\Mapping\Annotation as Gedmo;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use App\Trait\UuidTrait;
  13. use App\Trait\TimestampableTrait;
  14. use App\State\EmpresaStateProcessor;
  15. use App\Repository\EmpresaRepository;
  16. use App\Entity\Telefono;
  17. use ApiPlatform\Metadata\Put;
  18. use ApiPlatform\Metadata\Post;
  19. use ApiPlatform\Metadata\Patch;
  20. use ApiPlatform\Metadata\GetCollection;
  21. use ApiPlatform\Metadata\Get;
  22. use ApiPlatform\Metadata\Delete;
  23. use ApiPlatform\Metadata\ApiResource;
  24. use ApiPlatform\Metadata\ApiFilter;
  25. use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
  26. use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  27. #[ORM\Entity(repositoryClassEmpresaRepository::class)]
  28. #[ApiResource(
  29.     operations: [
  30.         new GetCollection(),
  31.         new Post(validationContext: ['groups' => ['Default''empresa:create']], denormalizationContext: ['groups' => ['empresa:create']], processorEmpresaStateProcessor::class),
  32.         new Get(),
  33.         new Put(denormalizationContext: ['groups' => ['empresa:update']]),
  34.         new Patch(denormalizationContext: ['groups' => ['empresa:update']]),
  35.         new Post(
  36.             uriTemplate'/empresas/{uuid}/logo',
  37.             denormalizationContext: ['groups' => ['empresa:changeLogo']],
  38.             validationContext: ['groups' => ['empresa:changeLogo']],
  39.             inputFormats: ['multipart' => ['multipart/form-data']]
  40.         ),
  41.         new Delete(),
  42.     ],
  43.     normalizationContext: ['groups' => ['empresa:read''uuid']],
  44. )]
  45. #[ApiFilter(
  46.     OrderFilter::class,
  47.     properties: ['nombre''emails.direccion''telefonos.numero''direcciones.direccion'],
  48. )]
  49. #[ApiFilter(
  50.     SearchFilter::class,
  51.     properties: [
  52.         'nombre' => 'exact',
  53.         'cif' => 'exact',
  54.         'codigo' => 'exact',
  55.         'telefonos.numero' => 'exact',
  56.         'emails.direccion' => 'exact',
  57.         'direcciones.direccion' => 'exact',
  58.     ]
  59. )]
  60. #[Vich\Uploadable]
  61. class Empresa
  62. {
  63.     use UuidTrait;
  64.     use TimestampableTrait;
  65.     #[Assert\NotBlank(groups: ['empresa:create'])]
  66.     #[Groups(['empresa:read''empresa:create''empresa:update''grupoEmpresa:read''user:read'])]
  67.     #[ORM\Column(length255nullabletrue)]
  68.     private ?string $nombre null;
  69.     #[Groups(['empresa:read''empresa:create''empresa:update'])]
  70.     #[ORM\Column(length255nullabletrue)]
  71.     private ?string $cif null;
  72.     #[Groups(['empresa:read''empresa:create''empresa:update'])]
  73.     #[ORM\Column(nullabletrue)]
  74.     private ?bool $isActivo null;
  75.     #[Groups(['empresa:read''grupoEmpresa:read'])]
  76.     #[ORM\Column(length255nullabletrue)]
  77.     #[Gedmo\Slug(fields: ['nombre'])]
  78.     private ?string $codigo null;
  79.     #[Groups(['empresa:read''empresa:create''empresa:update'])]
  80.     #[ORM\OneToMany(mappedBy'empresa'targetEntityTelefono::class, orphanRemovaltruecascade: ['persist''remove'])]
  81.     private Collection $telefonos;
  82.     #[Groups(['empresa:read''empresa:create''empresa:update'])]
  83.     #[ORM\OneToMany(mappedBy'empresa'targetEntityEmail::class, orphanRemovaltruecascade: ['persist''remove'])]
  84.     private Collection $emails;
  85.     #[Groups(['empresa:read''empresa:create''empresa:update'])]
  86.     #[ORM\OneToMany(mappedBy'empresa'targetEntityDireccion::class, orphanRemovaltruecascade: ['persist''remove'])]
  87.     private Collection $direcciones;
  88.     #[ORM\OneToMany(mappedBy'empresa'targetEntityEvento::class, orphanRemovaltruecascade: ['persist''remove'])]
  89.     private Collection $eventos;
  90.     #[ORM\OneToMany(mappedBy'empresa'targetEntityUser::class, orphanRemovaltruecascade: ['persist''remove'])]
  91.     private Collection $users;
  92.     #[Groups(['empresa:read''empresa:create''user:read'])]
  93.     #[ORM\ManyToOne(inversedBy'empresas')]
  94.     #[ORM\JoinColumn(nullablefalse)]
  95.     private ?GrupoEmpresa $grupoEmpresa null;
  96.     #[Groups(['empresa:read''empresa:create''empresa:update'])]
  97.     #[ORM\Column(nullabletrue)]
  98.     private ?array $paramsCalculos = [];
  99.     #[Groups(['empresa:read''empresa:create''empresa:update'])]
  100.     #[ORM\Column(nullabletrue)]
  101.     private ?array $paramsInformes = [];
  102.     #[Groups(['user:read''empresa:changeLogo'])]
  103.     #[Vich\UploadableField(mapping'logos'fileNameProperty'logoName')]
  104.     private ?File $logo null;
  105.     #[ORM\Column(length255nullabletrue)]
  106.     private ?string $logoName null;
  107.     #[Groups(['user:read''empresa:read'])]
  108.     public ?string $contentUrl null;
  109.     #[Groups(['empresa:create'])]
  110.     public ?string $username null;
  111.     #[Groups(['empresa:create'])]
  112.     public ?string $password null;
  113.     public function __construct()
  114.     {
  115.         $this->telefonos = new ArrayCollection();
  116.         $this->emails = new ArrayCollection();
  117.         $this->direcciones = new ArrayCollection();
  118.         $this->eventos = new ArrayCollection();
  119.         $this->users = new ArrayCollection();
  120.         $this->uuid Uuid::uuid4()->toString();
  121.     }
  122.     public function getNombre(): ?string
  123.     {
  124.         return $this->nombre;
  125.     }
  126.     public function setNombre(?string $nombre): self
  127.     {
  128.         $this->nombre $nombre;
  129.         return $this;
  130.     }
  131.     public function getCif(): ?string
  132.     {
  133.         return $this->cif;
  134.     }
  135.     public function setCif(?string $cif): self
  136.     {
  137.         $this->cif $cif;
  138.         return $this;
  139.     }
  140.     public function isIsActivo(): ?bool
  141.     {
  142.         return $this->isActivo;
  143.     }
  144.     public function setIsActivo(?bool $isActivo): self
  145.     {
  146.         $this->isActivo $isActivo;
  147.         return $this;
  148.     }
  149.     public function getCodigo(): ?string
  150.     {
  151.         return $this->codigo;
  152.     }
  153.     public function setCodigo(?string $codigo): self
  154.     {
  155.         $this->codigo $codigo;
  156.         return $this;
  157.     }
  158.     /**
  159.      * @return Collection<int, Telefono>
  160.      */
  161.     public function getTelefonos(): Collection
  162.     {
  163.         return $this->telefonos;
  164.     }
  165.     public function addTelefono(Telefono $telefono): self
  166.     {
  167.         if (!$this->telefonos->contains($telefono)) {
  168.             $this->telefonos->add($telefono);
  169.             $telefono->setEmpresa($this);
  170.         }
  171.         return $this;
  172.     }
  173.     public function removeTelefono(Telefono $telefono): self
  174.     {
  175.         if ($this->telefonos->removeElement($telefono)) {
  176.             // set the owning side to null (unless already changed)
  177.             if ($telefono->getEmpresa() === $this) {
  178.                 $telefono->setEmpresa(null);
  179.             }
  180.         }
  181.         return $this;
  182.     }
  183.     /**
  184.      * @return Collection<int, Email>
  185.      */
  186.     public function getEmails(): Collection
  187.     {
  188.         return $this->emails;
  189.     }
  190.     public function addEmail(Email $email): self
  191.     {
  192.         if (!$this->emails->contains($email)) {
  193.             $this->emails->add($email);
  194.             $email->setEmpresa($this);
  195.         }
  196.         return $this;
  197.     }
  198.     public function removeEmail(Email $email): self
  199.     {
  200.         if ($this->emails->removeElement($email)) {
  201.             // set the owning side to null (unless already changed)
  202.             if ($email->getEmpresa() === $this) {
  203.                 $email->setEmpresa(null);
  204.             }
  205.         }
  206.         return $this;
  207.     }
  208.     /**
  209.      * @return Collection<int, Direccion>
  210.      */
  211.     public function getDirecciones(): Collection
  212.     {
  213.         return $this->direcciones;
  214.     }
  215.     public function addDireccione(Direccion $direccione): self
  216.     {
  217.         if (!$this->direcciones->contains($direccione)) {
  218.             $this->direcciones->add($direccione);
  219.             $direccione->setEmpresa($this);
  220.         }
  221.         return $this;
  222.     }
  223.     public function removeDireccione(Direccion $direccione): self
  224.     {
  225.         if ($this->direcciones->removeElement($direccione)) {
  226.             // set the owning side to null (unless already changed)
  227.             if ($direccione->getEmpresa() === $this) {
  228.                 $direccione->setEmpresa(null);
  229.             }
  230.         }
  231.         return $this;
  232.     }
  233.     /**
  234.      * @return Collection<int, Evento>
  235.      */
  236.     public function getEventos(): Collection
  237.     {
  238.         return $this->eventos;
  239.     }
  240.     public function addEvento(Evento $evento): self
  241.     {
  242.         if (!$this->eventos->contains($evento)) {
  243.             $this->eventos->add($evento);
  244.             $evento->setEmpresa($this);
  245.         }
  246.         return $this;
  247.     }
  248.     public function removeEvento(Evento $evento): self
  249.     {
  250.         if ($this->eventos->removeElement($evento)) {
  251.             // set the owning side to null (unless already changed)
  252.             if ($evento->getEmpresa() === $this) {
  253.                 $evento->setEmpresa(null);
  254.             }
  255.         }
  256.         return $this;
  257.     }
  258.     /**
  259.      * @return Collection<int, User>
  260.      */
  261.     public function getUsers(): Collection
  262.     {
  263.         return $this->users;
  264.     }
  265.     public function addUser(User $user): self
  266.     {
  267.         if (!$this->users->contains($user)) {
  268.             $this->users->add($user);
  269.             $user->setEmpresa($this);
  270.         }
  271.         return $this;
  272.     }
  273.     public function removeUser(User $user): self
  274.     {
  275.         if ($this->users->removeElement($user)) {
  276.             // set the owning side to null (unless already changed)
  277.             if ($user->getEmpresa() === $this) {
  278.                 $user->setEmpresa(null);
  279.             }
  280.         }
  281.         return $this;
  282.     }
  283.     public function getGrupoEmpresa(): ?GrupoEmpresa
  284.     {
  285.         return $this->grupoEmpresa;
  286.     }
  287.     public function setGrupoEmpresa(?GrupoEmpresa $grupoEmpresa): self
  288.     {
  289.         $this->grupoEmpresa $grupoEmpresa;
  290.         return $this;
  291.     }
  292.     public function getParamsCalculos(): ?array
  293.     {
  294.         return $this->paramsCalculos;
  295.     }
  296.     public function setParamsCalculos(?array $paramsCalculos): self
  297.     {
  298.         $this->paramsCalculos $paramsCalculos;
  299.         return $this;
  300.     }
  301.     public function getParamsInformes(): ?array
  302.     {
  303.         return $this->paramsInformes;
  304.     }
  305.     public function setParamsInformes(?array $paramsInformes): static
  306.     {
  307.         $this->paramsInformes $paramsInformes;
  308.         return $this;
  309.     }
  310.     public function setLogo(?File $logo null): void
  311.     {
  312.         $this->logo $logo;
  313.         if (null !== $logo) {
  314.             $this->updatedAt = new \DateTimeImmutable();
  315.         }
  316.     }
  317.     public function getLogo(): ?File
  318.     {
  319.         return $this->logo;
  320.     }
  321.     public function getLogoName(): ?string
  322.     {
  323.         return $this->logoName;
  324.     }
  325.     public function setLogoName(?string $logoName): static
  326.     {
  327.         $this->logoName $logoName;
  328.         return $this;
  329.     }
  330.     public function getContentUrl(): ?string
  331.     {
  332.         return $this->contentUrl;
  333.     }
  334.     public function setContentUrl(?string $contentUrl): self
  335.     {
  336.         $this->contentUrl $contentUrl;
  337.         return $this;
  338.     }
  339.     public function getUsername(): ?string
  340.     {
  341.         return $this->username;
  342.     }
  343.     public function setUsername(?string $username): self
  344.     {
  345.         $this->username $username;
  346.         return $this;
  347.     }
  348.     public function getPassword(): ?string
  349.     {
  350.         return $this->password;
  351.     }
  352.     public function setPassword(?string $password): self
  353.     {
  354.         $this->password $password;
  355.         return $this;
  356.     }
  357. }