<?php
namespace Evo\Infrastructure\MappingORM;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Controller\Api\CreateOrUpdateDocumentAction;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ApiResource(
* attributes={
* "normalization_context"={"groups"={"read_document", "admin:doc:list"}},
* "denormalization_context"={"groups"={"write_document"}}
* },
* collectionOperations={
* "post"={
* "controller"=CreateOrUpdateDocumentAction::class,
* "deserialize"=false,
* },
* "get"
* },
* itemOperations={
* "get",
* "put"={
* "controller"=CreateOrUpdateDocumentAction::class,
* "deserialize"=false,
* },
* "delete"={"security"="is_granted('ROLE_ADMIN')"}
* }
* )
* @ORM\Entity
* @ORM\Table(name="document")
* @Vich\Uploadable
* @ApiFilter(OrderFilter::class, properties={"id", "createdAt", "signedBy", "status"}, arguments={"orderParameterName"="order"})
* @ApiFilter(GroupFilter::class, arguments={"parameterName": "groups", "overrideDefaultGroups": true})
* @ApiFilter(SearchFilter::class, properties={"person": "exact", "organization": "exact", "organization.status": "exact", "potentialCase.id": "exact", "status": "exact", "type":"exact", "person.legalRepresentatives.organizations.id": "exact"})
* @ApiFilter(PropertyFilter::class)
* @ApiFilter(ExistsFilter::class, properties={"signedBy", "signature", "rejectionMessage"})
* @ORM\Entity(repositoryClass="App\Repository\DocumentRepository")
*/
class Document
{
/**
* @var int The entity Id
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue()
* @Groups({"read_document", "admin:doc:list", "read_organization","read_person","legal_representative", "associate"})
*/
private ?int $id = null;
/**
* @Groups({"read_document", "admin:doc:list", "write_document", "read_organization", "associate"})
* @ORM\ManyToOne(targetEntity="Evo\Infrastructure\MappingORM\Organization", inversedBy="documents")
* @ORM\JoinColumn(name="organization_id", referencedColumnName="id", nullable=true)
*/
private ?Organization $organization = null;
/**
* @var string The document name
*
* @Groups({"read_document", "admin:doc:list", "write_document", "write_organization", "read_organization","read_person","legal_representative", "associate"})
* @ORM\Column(type="string", nullable=true)
* @Assert\NotBlank()
*/
private ?string $name = null;
/**
* @var string The document type
*
* @Groups({"read_document", "admin:doc:list", "write_document", "write_organization", "read_organization","read_person","legal_representative", "associate"})
* @ORM\Column(type="string", length=50)
* @Assert\NotBlank()
*/
private ?string $type = null;
/**
* @var string The document status
*
* @Assert\Choice(callback={"App\Enum\DocumentStatusEnum", "getChoices"})
* @Groups({"read_document", "admin:doc:list", "write_document", "write_organization", "read_organization","read_person","legal_representative", "associate"})
* @ORM\Column(type="string", length=20, nullable=true)
* @Assert\NotBlank()
*/
private ?string $status = null;
/**
* @Groups({"read_document", "admin:doc:list", "write_document", "write_organization", "read_organization","read_person","legal_representative", "associate"})
* @ORM\Column(type="text", nullable=true)
*/
private ?string $reasonNotApproved = null;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="document", fileNameProperty="path")
* @Assert\File(
* maxSize = "5M",
* mimeTypes = {"application/pdf", "application/x-pdf", "image/jpeg", "image/png"},
* mimeTypesMessage = "error.vivh.format.imagePdf"
* )
*/
private ?File $documentFile = null;
/**
* @var string The document file path
*
* @ApiProperty(writable=false)
* @Groups({"read_document", "admin:doc:list", "read_organization","read_person","legal_representative", "associate"})
* @ORM\Column(type="string", nullable=true)
*/
private ?string $path = null;
/**
* @Groups({"read_document", "admin:doc:list", "write_document", "write_organization", "read_organization","read_person","legal_representative", "associate"})
* @ORM\Column(type="datetime", nullable=true)
*/
private ?\DateTimeInterface $expirationDate = null;
/**
* @var \DateTimeInterface
*
* @Groups({"read_document", "admin:doc:list", "read_organization", "associate"})
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @Groups({"read_document", "write_organization", "read_organization", "write_document", "associate"})
* @MaxDepth(1)
* @ORM\ManyToOne(targetEntity="Evo\Infrastructure\MappingORM\DocumentRejectionMessage", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private ?DocumentRejectionMessage $rejectionMessage = null;
/**
* @Groups({"read_document", "admin:doc:list", "write_document", "read_organization","read_person", "associate"})
* @ORM\ManyToOne(targetEntity="Evo\Infrastructure\MappingORM\Person", inversedBy="documents", cascade={"persist"})
*/
private ?Person $person = null;
/**
* @ApiProperty(writable=false)
* @Groups({"read_document", "admin:doc:list", "read_organization","read_person", "associate"})
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $awsPath = null;
/**
* @Groups({"read_document", "admin:doc:list", "write_document", "write_organization", "read_organization","read_person","legal_representative", "associate"})
* @ORM\ManyToMany(targetEntity="Evo\Infrastructure\MappingORM\ProcessYousign", mappedBy="documents", cascade={"persist"})
*/
private ?Collection $processYousigns;
/**
* @Assert\Choice(callback={"App\Enum\SignatoryEnum", "getChoices"})
* @Groups({"read_document", "admin:doc:list", "write_document", "read_organization","read_person"})
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $signedBy = null;
/**
* @Groups({"read_document", "admin:doc:list", "write_document", "write_organization", "read_organization","read_person","legal_representative"})
* @ORM\OneToMany(targetEntity="Evo\Infrastructure\MappingORM\Signature", mappedBy="document", cascade={"persist","remove"})
*/
private ?Collection $signature;
/**
* @Groups({"admin:doc:list"})
* @ORM\Column(type="text", nullable=true)
*/
private ?string $yousignLink = null;
/**
* @Groups({"read_document", "write_document", "admin:doc:list", "read_organization", "read_person"})
* @ORM\Column(type="datetime", nullable=true)
*/
private ?\DateTimeInterface $awaitingValidationDate = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $yousignError = null;
/**
* @Groups({"read_document", "admin:doc:list"})
* @ORM\Column(type="text", nullable=true)
*/
private ?string $htmlDocument = null;
/**
* @Groups({"read_document","write_document", "admin:doc:list"})
* @ORM\ManyToOne(targetEntity="Evo\Infrastructure\MappingORM\PotentialCase", inversedBy="documents")
*/
private ?PotentialCase $potentialCase = null;
/**
* @Groups({"read_document", "admin:doc:list", "write_document", "write_organization", "read_organization","read_person","legal_representative", "associate"})
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $label = null;
/**
* @Groups({"read_document", "admin:doc:list", "write_document", "write_organization", "read_organization","read_person","legal_representative", "associate"})
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $rejectionMessageTitle = null;
/**
* @Groups({"read_document", "admin:doc:list", "write_document", "write_organization", "read_organization","read_person","legal_representative", "associate"})
* @ORM\Column(type="text", nullable=true)
*/
private ?string $rejectionMessageDescription = null;
/**
* @Groups({"read_document"})
* @ORM\Column(type="boolean", options={"default": false})
*/
private bool $isFromPappers = false;
private bool $temporaryFileForPappers = false;
private bool $bypassSubscriber = false;
public function __construct()
{
$this->processYousigns = new ArrayCollection();
$this->signature = new ArrayCollection();
$this->createdAt = new \DateTime();
}
public function __toString()
{
return ('' !== $this->name && '0' !== $this->name) ? $this->getName() : 'Document';
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): Document
{
$this->name = $name;
return $this;
}
public function getId(): ?int
{
return $this->id;
}
public function resetId()
{
$this->id = null;
return $this;
}
public function getOrganization(): ?Organization
{
return $this->organization;
}
public function setOrganization(?Organization $organization): Document
{
$this->organization = $organization;
return $this;
}
public function getType(): string
{
return $this->type;
}
public function setType(string $type): Document
{
$this->type = $type;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(?string $status): Document
{
$this->status = $status;
return $this;
}
public function getReasonNotApproved(): ?string
{
return $this->reasonNotApproved;
}
public function setReasonNotApproved(?string $reasonNotApproved): Document
{
$this->reasonNotApproved = $reasonNotApproved;
return $this;
}
public function getDocumentFile(): ?File
{
return $this->documentFile;
}
public function setDocumentFile(?File $documentFile): Document
{
$this->documentFile = $documentFile;
return $this;
}
public function getPath(): ?string
{
return $this->path;
}
public function setPath(?string $path): Document
{
$this->path = $path;
return $this;
}
public function getExpirationDate(): ?\DateTimeInterface
{
return $this->expirationDate;
}
public function setExpirationDate(?\DateTimeInterface $expirationDate): Document
{
$this->expirationDate = $expirationDate;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): Document
{
$this->createdAt = $createdAt;
return $this;
}
public function getRejectionMessage(): ?DocumentRejectionMessage
{
return $this->rejectionMessage;
}
public function setRejectionMessage(?DocumentRejectionMessage $rejectionMessage): self
{
$this->rejectionMessage = $rejectionMessage;
return $this;
}
public function getPerson(): ?Person
{
return $this->person;
}
public function setPerson(?Person $person): self
{
$this->person = $person;
return $this;
}
/**
* @return Collection|ProcessYousign[]
*/
public function getProcessYousigns(): Collection
{
return $this->processYousigns;
}
public function addProcessYousign(ProcessYousign $processYousign): self
{
if (!$this->processYousigns->contains($processYousign)) {
$this->processYousigns[] = $processYousign;
$processYousign->addDocument($this);
}
return $this;
}
public function removeProcessYousign(ProcessYousign $processYousign): self
{
if ($this->processYousigns->contains($processYousign)) {
$this->processYousigns->removeElement($processYousign);
$processYousign->removeDocument($this);
}
return $this;
}
public function getSignedBy(): ?string
{
return $this->signedBy;
}
public function setSignedBy(?string $signedBy): self
{
$this->signedBy = $signedBy;
return $this;
}
/**
* @return Collection|Signature[]
*/
public function getSignature(): Collection
{
return $this->signature;
}
public function addSignature(Signature $signature): self
{
if (!$this->signature->contains($signature)) {
$this->signature[] = $signature;
$signature->setDocument($this);
}
return $this;
}
public function removeSignature(Signature $signature): self
{
if ($this->signature->contains($signature)) {
$this->signature->removeElement($signature);
// set the owning side to null (unless already changed)
if ($signature->getDocument() === $this) {
$signature->setDocument(null);
}
}
return $this;
}
public function getExtension()
{
if ($this->getAwsPath()) {
$tmp = explode('.', $this->getAwsPath());
return (isset($tmp[0])) ? end($tmp) : null;
}
return null;
}
public function getAwsPath(): ?string
{
return $this->awsPath;
}
public function setAwsPath(?string $awsPath): self
{
$this->awsPath = $awsPath;
return $this;
}
public function getYousignLink(): ?string
{
return $this->yousignLink;
}
public function setYousignLink(?string $yousignLink): self
{
$this->yousignLink = $yousignLink;
return $this;
}
public function getAwaitingValidationDate(): ?\DateTimeInterface
{
return $this->awaitingValidationDate;
}
public function setAwaitingValidationDate(?\DateTimeInterface $awaitingValidationDate): self
{
$this->awaitingValidationDate = $awaitingValidationDate;
return $this;
}
public function getYousignError(): ?string
{
return $this->yousignError;
}
public function setYousignError(?string $yousignError): self
{
$this->yousignError = $yousignError;
return $this;
}
public function getHtmlDocument(): ?string
{
return $this->htmlDocument;
}
public function setHtmlDocument(?string $htmlDocument): self
{
$this->htmlDocument = $htmlDocument;
return $this;
}
public function getPotentialCase(): ?PotentialCase
{
return $this->potentialCase;
}
public function setPotentialCase(?PotentialCase $potentialCase): self
{
$this->potentialCase = $potentialCase;
return $this;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(?string $label): self
{
$this->label = $label;
return $this;
}
public function getIsFromPappers(): bool
{
return $this->isFromPappers;
}
public function setIsFromPappers(bool $isFromPappers): self
{
$this->isFromPappers = $isFromPappers;
return $this;
}
public function isDocumentOfPerson(): bool
{
return null !== $this->getPerson();
}
public function isDocumentOfOrganization(): bool
{
return null !== $this->getOrganization();
}
public function isTemporaryFileForPappers(): bool
{
return $this->temporaryFileForPappers;
}
public function setTemporaryFileForPappers(bool $temporaryFileForPappers): self
{
$this->temporaryFileForPappers = $temporaryFileForPappers;
return $this;
}
public function isBypassSubscriber(): bool
{
return $this->bypassSubscriber;
}
public function setBypassSubscriber(bool $bypassSubscriber): self
{
$this->bypassSubscriber = $bypassSubscriber;
return $this;
}
public function getRejectionMessageDescription(): ?string
{
return $this->rejectionMessageDescription;
}
public function setRejectionMessageDescription(?string $rejectionMessageDescription): self
{
$this->rejectionMessageDescription = $rejectionMessageDescription;
return $this;
}
public function getRejectionMessageTitle(): ?string
{
return $this->rejectionMessageTitle;
}
public function setRejectionMessageTitle(?string $rejectionMessageTitle): self
{
$this->rejectionMessageTitle = $rejectionMessageTitle;
return $this;
}
public static function create(array $data): Document
{
return (new self())
->setType($data['type'])
->setIsFromPappers($data['isFromPappers'])
->setOrganization($data['organization'] ?? null)
->setExpirationDate($data['expirationDate'] ?? null)
->setStatus($data['status'])
->setPath($data['path'] ?? null)
->setPerson($data['person'] ?? null)
->setSignedBy($data['signedBy'] ?? null)
->setTemporaryFileForPappers($data['temporaryFileForPappers'] ?? false)
;
}
public function resetDocumentRejectionMessage(): self
{
$this->rejectionMessageTitle = null;
$this->rejectionMessageDescription = null;
return $this;
}
}