Julien Fastré ea5f8c9d08 Feature: [docgen][person] add a new context to generate document with a
third party

This allow to prepare, for instance, mail (letters) to a thirdparty
about a Person
2022-10-19 14:16:59 +02:00

896 lines
22 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\ThirdPartyBundle\Entity;
use Chill\MainBundle\Doctrine\Model\TrackCreationInterface;
use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\Civility;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Validation\Constraint\PhonenumberConstraint;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use libphonenumber\PhoneNumber;
use Symfony\Component\Serializer\Annotation\Context;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use UnexpectedValueException;
use function array_filter;
use function array_map;
use function array_merge;
use function array_values;
use function in_array;
use function is_string;
use function spl_object_hash;
/**
* ThirdParty is a party recorded in the database.
*
* A party may be attached to multiple centers. Being attach to a center allow
* all users with the right 'CHILL_3PARTY_3PARTY_SEE', 'CHILL_3PARTY_3 to see, select and edit parties for this
* center.
*
* A ThirdParty may have:
*
* * 0, one or more categories;
* * 0, one or more type
* * 1 kind.
*
* ## Kind
*
* The kind indicate if a thirdparty is a:
*
* * company ("personne morale");
* * a contact ("personne morale")
* * a child inside a company ("contact" in French). Only companies may have childs
*
* **take care** the french translation may lead to misinterpretation, as the string "contact" is the translation of
* kind "child".
*
* ## Categories and types
*
* ThirdParty may have zero, one or more categories and/or type.
*
* Categories are managed in the database. The Chill administrator may create or desactivate categories.
*
* Types are also a way to categorize the thirdparties, but this is done **into the code**, through
*
* @see{Chill\ThirdPartyBundle\ThirdPartyType\ThirdPartyTypeProviderInterface}. The type is stored into the
* database by a Php array, mapped by a jsonb into the database. This has one advantage: it is easily searchable.
*
* As the list of type is hardcoded into database, it is more easily searchable. (for chill 2.0, the
* @see{Chill\ThirdPartyBundle\Form\Type\PickThirdPartyDynamicType} does not support it yet, but
* the legacy @see{Chill\ThirdPartyBundle\Form\Type\PickThirdPartyType} does.
*
* The difference between categories and types is transparent for user: they choose the same fields into the UI, without
* noticing a difference.
*
* @ORM\Entity
* @ORM\Table(name="chill_3party.third_party")
* @DiscriminatorMap(typeProperty="type", mapping={
* "thirdparty": ThirdParty::class
* })
*/
class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
{
public const KIND_CHILD = 'child';
public const KIND_COMPANY = 'company';
public const KIND_CONTACT = 'contact';
/**
* [fr] Sigle.
*
* @var string
* @ORM\Column(name="acronym", type="string", length=64, nullable=true)
* @Assert\Length(min="2")
* @Groups({"read", "write", "docgen:read", "docgen:read:3party:parent"})
*/
private ?string $acronym = '';
/**
* Soft-delete flag.
*
* @ORM\Column(name="active", type="boolean", options={"defaut": true})
*/
private bool $active = true;
/**
* @ORM\ManyToOne(targetEntity="\Chill\MainBundle\Entity\Address",
* cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
* @Groups({"read", "write", "docgen:read", "docgen:read:3party:parent"})
* @Context(normalizationContext={"groups": "docgen:read"}, groups={"docgen:read:3party:parent"})
*/
private ?Address $address = null;
/**
* Canonicalized form composed of name, company name and acronym.
*
* This field is read-only, and is generated on database side.
*
* @ORM\Column(name="canonicalized", type="text", options={"default": ""})
*/
private ?string $canonicalized = '';
/**
* @var ThirdPartyCategory
* @ORM\ManyToMany(targetEntity="Chill\ThirdPartyBundle\Entity\ThirdPartyCategory")
* @ORM\JoinTable(name="chill_3party.thirdparty_category",
* joinColumns={@ORM\JoinColumn(name="thirdparty_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")})
* @Groups({"docgen:read", "docgen:read:3party:parent"})
* @Context(normalizationContext={"groups": "docgen:read"}, groups={"docgen:read:3party:parent"})
*/
private Collection $categories;
/**
* @ORM\ManyToMany(targetEntity="\Chill\MainBundle\Entity\Center")
* @ORM\JoinTable(name="chill_3party.party_center")
*/
private Collection $centers;
/**
* Contact Persons: One Institutional ThirdParty has Many Contact Persons.
*
* @ORM\OneToMany(targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty", mappedBy="parent",
* cascade={"persist"}, orphanRemoval=true)
*
* @var Collection|ThirdParty[]
* @Assert\Valid(traverse=true)
*/
private Collection $children;
/**
* @var Civility
* @ORM\ManyToOne(targetEntity=Civility::class)
* ORM\JoinColumn(name="civility", referencedColumnName="id", nullable=true)
* @Groups({"read", "write", "docgen:read", "docgen:read:3party:parent"})
* @Context(normalizationContext={"groups": "docgen:read"}, groups={"docgen:read:3party:parent"})
*/
private ?Civility $civility = null;
/**
* @ORM\Column(name="comment", type="text", nullable=true)
* @Groups({"read", "write"})
*/
private ?string $comment = null;
/**
* @ORM\Column(name="contact_data_anonymous", type="boolean", options={"default": false})
* @Groups({"read", "docgen:read", "docgen:read:3party:parent"})
*/
private bool $contactDataAnonymous = false;
/**
* @ORM\Column(name="created_at", type="datetime_immutable", nullable=false)
*/
private DateTimeImmutable $createdAt;
/**
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User")
* @ORM\JoinColumn(name="created_by", referencedColumnName="id")
*/
private ?User $createdBy;
/**
* @ORM\Column(name="email", type="string", length=255, nullable=true)
* @Assert\Email(checkMX=false)
* @Groups({"read", "write", "docgen:read", "docgen:read:3party:parent"})
*/
private ?string $email = null;
/**
* @ORM\Column(name="firstname", type="text", options={"default": ""})
* @Groups({"read", "write", "docgen:read", "docgen:read:3party:parent"})
*/
private string $firstname = '';
/**
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @Groups({"read", "docgen:read", "docgen:read:3party:parent"})
*/
private ?int $id = null;
/**
* @ORM\Column(name="kind", type="string", length="20", options={"default": ""})
* @Groups({"write", "docgen:read", "docgen:read:3party:parent"})
*/
private ?string $kind = '';
/**
* @var string
* @ORM\Column(name="name", type="string", length=255)
* @Assert\Length(min="2")
* @Assert\NotNull
* @Assert\NotBlank
* @Groups({"read", "write", "docgen:read", "docgen:read:3party:parent"})
*/
private ?string $name = '';
/**
* [fr] Raison sociale.
*
* @var string
* @ORM\Column(name="name_company", type="string", length=255, nullable=true)
* @Assert\Length(min="3")
* @Groups({"read", "write", "docgen:read", "docgen:read:3party:parent"})
*/
private ?string $nameCompany = '';
/**
* Institutional ThirdParty: Many Contact Persons have One Institutional ThirdParty.
*
* @ORM\ManyToOne(targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
* @Groups({"read", "write", "docgen:read"})
* @Context(normalizationContext={"groups": "docgen:read:3party:parent"}, groups={"docgen:read"})
*/
private ?ThirdParty $parent = null;
/**
* [fr] Qualité.
*
* @var ThirdPartyProfession
* @ORM\ManyToOne(targetEntity="Chill\ThirdPartyBundle\Entity\ThirdPartyProfession")
* ORM\JoinColumn(name="profession", referencedColumnName="id", nullable=true)
* @Groups({"read", "write", "docgen:read", "docgen:read:3party:parent"})
* @Context(normalizationContext={"groups": "docgen:read"}, groups={"docgen:read:3party:parent"})
*/
private ?ThirdPartyProfession $profession = null;
/**
* @ORM\Column(name="telephone", type="phone_number", nullable=true)
* @PhonenumberConstraint(type="any")
* @Groups({"read", "write", "docgen:read", "docgen:read:3party:parent"})
*/
private ?PhoneNumber $telephone = null;
/**
* @ORM\Column(name="types", type="json", nullable=true)
*/
private ?array $thirdPartyTypes = [];
/**
* @ORM\Column(name="updated_at", type="datetime_immutable", nullable=true)
*/
private ?DateTimeImmutable $updatedAt;
/**
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User")
* @ORM\JoinColumn(name="updated_by", referencedColumnName="id")
*/
private ?User $updatedBy;
/**
* ThirdParty constructor.
*/
public function __construct()
{
$this->centers = new ArrayCollection();
$this->categories = new ArrayCollection();
$this->children = new ArrayCollection();
}
/**
* @return string
*/
public function __toString()
{
return $this->getName();
}
/**
* Add a child and set the child as active.
*
* Method used in conjonction with getActiveChildren in form.
*
* @internal use the method addChild
*
* @return $this
*/
public function addActiveChild(ThirdParty $child): self
{
$child->setActive(true);
return $this->addChild($child);
}
/**
* @return $this
*/
public function addCategory(ThirdPartyCategory $category): self
{
if (!$this->categories->contains($category)) {
$this->categories[] = $category;
}
foreach ($this->children as $child) {
$child->addCategory($category);
}
return $this;
}
public function addCenter(Center $center)
{
if (false === $this->centers->contains($center)) {
$this->centers->add($center);
}
}
/**
* @return $this
*/
public function addChild(ThirdParty $child): self
{
$this->children[] = $child;
$child->setParent($this)->setKind(ThirdParty::KIND_CHILD);
return $this;
}
public function addThirdPartyTypes(?string $type): self
{
if (null === $type) {
return $this;
}
if (!in_array($type, $this->thirdPartyTypes ?? [], true)) {
$this->thirdPartyTypes[] = $type;
}
foreach ($this->children as $child) {
$child->addThirdPartyTypes($type);
}
return $this;
}
public function addTypesAndCategories($typeAndCategory): self
{
if ($typeAndCategory instanceof ThirdPartyCategory) {
$this->addCategory($typeAndCategory);
return $this;
}
if (is_string($typeAndCategory)) {
$this->addThirdPartyTypes($typeAndCategory);
return $this;
}
throw new UnexpectedValueException(sprintf(
'typeAndCategory should be a string or a %s',
ThirdPartyCategory::class
));
}
/**
* @return string
*/
public function getAcronym(): ?string
{
return $this->acronym;
}
public function getActive(): bool
{
return $this->active;
}
/**
* Get the children where active = true.
*/
public function getActiveChildren(): Collection
{
return $this->children->filter(static fn (ThirdParty $tp) => $tp->getActive());
}
public function getAddress(): ?Address
{
if ($this->isChild()) {
return $this->getParent()->getAddress();
}
return $this->address;
}
public function getCategories(): Collection
{
return $this->categories;
}
public function getCenters(): Collection
{
return $this->centers;
}
public function getChildren(): Collection
{
return $this->children;
}
public function getCivility(): ?Civility
{
return $this->civility;
}
/**
* Get comment.
*
* @return string|null
*/
public function getComment()
{
return $this->comment;
}
public function getCreatedAt(): DateTimeImmutable
{
return $this->createdAt;
}
/**
* Get email.
*/
public function getEmail(): ?string
{
return $this->email;
}
public function getFirstname(): string
{
return $this->firstname;
}
public function getId(): ?int
{
return $this->id;
}
public function getKind(): ?string
{
return $this->kind;
}
public function getName(): string
{
return $this->name;
}
public function getNameCompany(): ?string
{
return $this->nameCompany;
}
public function getParent(): ?ThirdParty
{
return $this->parent;
}
/**
* @return ThirdPartyProfession
*/
public function getProfession(): ?ThirdPartyProfession
{
return $this->profession;
}
/**
* Get telephone.
*/
public function getTelephone(): ?PhoneNumber
{
return $this->telephone;
}
/**
* Get type.
*
* @return array|null
*/
public function getThirdPartyTypes()
{
return $this->thirdPartyTypes ?? [];
}
public function getTypesAndCategories(): array
{
return array_merge(
$this->getCategories()->toArray(),
$this->getThirdPartyTypes() ?? []
);
}
/**
* @return DateTime|null
*/
public function getUpdatedAt(): ?DateTimeImmutable
{
return $this->updatedAt;
}
public function getUpdatedBy(): ?User
{
return $this->updatedBy;
}
/**
* @Groups({"read", "docgen:read", "docgen:read:3party:parent"})
*/
public function isChild(): bool
{
return null !== $this->parent;
}
public function isContactDataAnonymous(): bool
{
return $this->contactDataAnonymous;
}
/**
* Mechanism to differentiate children and parents.
*/
public function isLeaf(): bool
{
return $this->children->count() !== 0;
}
public function isParent(): bool
{
return !$this->isChild();
}
/**
* mark the child as unactive, but keep the child existing in the
* database. To effectively remove the child, use removeChild instead.
*
* @return $this
*/
public function removeActiveChild(ThirdParty $child): self
{
$child->setActive(false);
return $this;
}
/**
* @return $this
*/
public function removeCategory(ThirdPartyCategory $category): self
{
$this->categories->removeElement($category);
foreach ($this->children as $child) {
$child->removeCategory($category);
}
return $this;
}
public function removeCenter(Center $center)
{
if ($this->centers->contains($center)) {
$this->centers->removeElement($center);
}
}
/**
* Remove the child from the database.
*
* If you want to keep the child into the database
* but desactivate it, use removeActiveChildren instead.
*
* @return $this
*/
public function removeChild(ThirdParty $child): self
{
$this->children->removeElement($child);
return $this;
}
public function removeThirdPartyTypes(?string $type): self
{
if (null === $type) {
return $this;
}
if (in_array($type, $this->thirdPartyTypes ?? [], true)) {
$this->thirdPartyTypes = array_filter($this->thirdPartyTypes, fn ($e) => !in_array($e, $this->thirdPartyTypes, true));
}
foreach ($this->children as $child) {
$child->removeThirdPartyTypes($type);
}
return $this;
}
public function removeTypesAndCategories($typeAndCategory): self
{
if ($typeAndCategory instanceof ThirdPartyCategory) {
$this->removeCategory($typeAndCategory);
return $this;
}
if (is_string($typeAndCategory)) {
$this->removeThirdPartyTypes($typeAndCategory);
return $this;
}
throw new UnexpectedValueException(sprintf(
'typeAndCategory should be a string or a %s',
ThirdPartyCategory::class
));
}
/**
* @param string $acronym
*
* @return $this
*/
public function setAcronym(?string $acronym = null): ThirdParty
{
$this->acronym = (string) $acronym;
return $this;
}
/**
* @return $this
*/
public function setActive(bool $active)
{
$this->active = $active;
foreach ($this->children as $child) {
$child->setActive($active);
}
return $this;
}
/**
* @return $this
*/
public function setAddress(?Address $address = null)
{
$this->address = $address;
return $this;
}
/**
* @return $this
*/
public function setCenters(Collection $centers)
{
foreach ($centers as $center) {
$this->addCenter($center);
}
foreach ($this->centers as $center) {
if (false === $centers->contains($center)) {
$this->removeCenter($center);
}
}
return $this;
}
/**
* @return $this
*/
public function setCivility(?Civility $civility): ThirdParty
{
$this->civility = $civility;
return $this;
}
/**
* Set comment.
*
* @param string|null $comment
*
* @return ThirdParty
*/
public function setComment($comment = null)
{
$this->comment = $comment;
return $this;
}
public function setContactDataAnonymous(bool $contactDataAnonymous): ThirdParty
{
$this->contactDataAnonymous = $contactDataAnonymous;
return $this;
}
/**
* @param DateTimeImmutable $createdAt
*
* @return $this
*/
public function setCreatedAt(DateTimeInterface $createdAt): ThirdParty
{
$this->createdAt = $createdAt;
return $this;
}
public function setCreatedBy(User $user): TrackCreationInterface
{
$this->createdBy = $user;
return $this;
}
/**
* Set email.
*
* @param string|null $email
*
* @return ThirdParty
*/
public function setEmail($email = null)
{
$this->email = trim((string) $email);
return $this;
}
public function setFirstname(?string $firstname): self
{
$this->firstname = trim((string) $firstname);
return $this;
}
public function setKind(?string $kind): ThirdParty
{
$this->kind = $kind;
return $this;
}
public function setName(?string $name): self
{
$this->name = (string) $name;
return $this;
}
/**
* @param string $nameCompany
*/
public function setNameCompany(?string $nameCompany): ThirdParty
{
$this->nameCompany = (string) $nameCompany;
return $this;
}
/**
* @return $this
*/
public function setParent(?ThirdParty $parent): ThirdParty
{
$this->parent = $parent;
return $this;
}
/**
* @return $this
*/
public function setProfession(?ThirdPartyProfession $profession): ThirdParty
{
$this->profession = $profession;
return $this;
}
/**
* Set telephone.
*/
public function setTelephone(?PhoneNumber $telephone = null): self
{
$this->telephone = $telephone;
return $this;
}
/**
* Set type.
*
* @return ThirdParty
*/
public function setThirdPartyTypes(?array $type = [])
{
// remove all keys from the input data
$this->thirdPartyTypes = array_values($type);
foreach ($this->children as $child) {
$child->setThirdPartyTypes($type);
}
return $this;
}
public function setTypesAndCategories(array $typesAndCategories): self
{
$types = array_filter($typesAndCategories, static fn ($item) => !$item instanceof ThirdPartyCategory);
$this->setThirdPartyTypes($types);
// handle categories
foreach ($typesAndCategories as $t) {
$this->addTypesAndCategories($t);
}
$categories = array_filter($typesAndCategories, static fn ($item) => $item instanceof ThirdPartyCategory);
$categoriesHashes = array_map(static fn (ThirdPartyCategory $c) => spl_object_hash($c), $categories);
foreach ($categories as $c) {
$this->addCategory($c);
}
foreach ($this->getCategories() as $t) {
if (!in_array(spl_object_hash($t), $categoriesHashes, true)) {
$this->removeCategory($t);
}
}
return $this;
}
/**
* @param DateTimeImmutable $updatedAt
*
* @return $this
*/
public function setUpdatedAt(DateTimeInterface $updatedAt): ThirdParty
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return $this
*/
public function setUpdatedBy(User $updatedBy): ThirdParty
{
$this->updatedBy = $updatedBy;
return $this;
}
}