src/Entity/Family.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Enum\RegistrationStatus;
  4. use App\Entity\Traits\Identifiable;
  5. use App\Repository\FamilyRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. #[ORM\Entity(repositoryClassFamilyRepository::class)]
  10. class Family
  11. {
  12.     //TODO: ajouter un listener pour définir le responsable automatiquement à la création
  13.     use Identifiable;
  14.     #[ORM\Column(type'string'length150)]
  15.     private $lastName;
  16.     #[ORM\OneToMany(mappedBy'family'targetEntityPerson::class, cascade: ['remove'])]
  17.     private $persons;
  18.     #[ORM\OneToOne(targetEntityPerson::class)]
  19.     private $responsiblePerson;
  20.     #[ORM\Embedded(class: Address::class)]
  21.     private $address;
  22.     #[ORM\Column(type'integer'nullabletrue)]
  23.     private $oldId;
  24.     #[ORM\OneToMany(mappedBy'family'targetEntityLegalPerson::class)]
  25.     private Collection $legalPersons;
  26.     public function __construct()
  27.     {
  28.         $this->persons = new ArrayCollection();
  29.         $this->legalPersons = new ArrayCollection();
  30.     }
  31.     public function __toString(): string
  32.     {
  33.         return $this->getLastName(true);
  34.     }
  35.     public function getId(): ?int
  36.     {
  37.         return $this->id;
  38.     }
  39.     public function getLastName(?bool $uppercase false): ?string
  40.     {
  41.         return $uppercase mb_strtoupper($this->lastName) : $this->lastName;
  42.     }
  43.     public function setLastName(string $lastName): self
  44.     {
  45.         $this->lastName $lastName;
  46.         return $this;
  47.     }
  48.     /**
  49.      * @return Collection<int, Person>
  50.      */
  51.     public function getPersons(): Collection
  52.     {
  53.         return $this->persons;
  54.     }
  55.     public function addPerson(Person $person): self
  56.     {
  57.         if (!$this->persons->contains($person)) {
  58.             $this->persons[] = $person;
  59.             $person->setFamily($this);
  60.         }
  61.         return $this;
  62.     }
  63.     public function removePerson(Person $person): self
  64.     {
  65.         if ($this->persons->removeElement($person)) {
  66.             // set the owning side to null (unless already changed)
  67.             if ($person->getFamily() === $this) {
  68.                 $person->setFamily(null);
  69.             }
  70.         }
  71.         return $this;
  72.     }
  73.     /**
  74.      * @param array|Person[] $persons
  75.      * @return $this
  76.      */
  77.     public function setPersons(array $persons): self
  78.     {
  79.         $collection = new ArrayCollection();
  80.         foreach ($persons as $person) {
  81.             if (!$collection->contains($person)) {
  82.                 $collection->add($person);
  83.             }
  84.         }
  85.         $this->persons $collection;
  86.         return $this;
  87.     }
  88.     /**
  89.      * @return Collection<int, Person>
  90.      */
  91.     public function getAdults(): Collection
  92.     {
  93.         $maxBirthDate = new \DateTimeImmutable('-18 years');
  94.         $collection = new ArrayCollection();
  95.         $responsiblePerson $this->getResponsiblePerson();
  96.         if ($responsiblePerson) {
  97.             $collection->add($responsiblePerson);
  98.         }
  99.         foreach ($this->getPersons() as $person) {
  100.             if ($collection->contains($person)) {
  101.                 continue;
  102.             }
  103.             $birthDate $person->getBirthDate();
  104.             if ($birthDate && $birthDate $maxBirthDate) {
  105.                 $collection->add($person);
  106.                 continue;
  107.             }
  108.             if ($person->getWards()->count() > 0) {
  109.                 $collection->add($person);
  110.             }
  111.         }
  112.         return $collection;
  113.     }
  114.     public function getResponsiblePerson(): ?Person
  115.     {
  116.         return $this->responsiblePerson;
  117.     }
  118.     public function setResponsiblePerson(?Person $responsiblePerson): self
  119.     {
  120.         $this->responsiblePerson $responsiblePerson;
  121.         return $this;
  122.     }
  123.     public function getAddress(): ?Address
  124.     {
  125.         return $this->address;
  126.     }
  127.     public function setAddress(?Address $address)
  128.     {
  129.         $this->address $address;
  130.         return $this;
  131.     }
  132.     /**
  133.      * @param Section|null $section
  134.      * @return Collection<int, Registration>
  135.      */
  136.     public function getRegistrations(?Section $section null): Collection
  137.     {
  138.         $registrations = new ArrayCollection();
  139.         foreach ($this->getPersons() as $person) {
  140.             foreach ($person->getRegistrations($section) as $registration) {
  141.                 $registrations->add($registration);
  142.             }
  143.         }
  144.         return $registrations;
  145.     }
  146.     /**
  147.      * @param Section|null $section
  148.      * @return Collection<int, Registration>
  149.      */
  150.     public function getActiveRegistrations(?Section $section null): Collection
  151.     {
  152.         $registrations = new ArrayCollection();
  153.         foreach ($this->getPersons() as $person) {
  154.             foreach ($person->getActiveRegistrations($section) as $activeRegistration) {
  155.                 $registrations->add($activeRegistration);
  156.             }
  157.         }
  158.         return $registrations;
  159.     }
  160.     public function getOldId(): ?int
  161.     {
  162.         return $this->oldId;
  163.     }
  164.     public function setOldId(?int $oldId): self
  165.     {
  166.         $this->oldId $oldId;
  167.         return $this;
  168.     }
  169.     public function remainingToPay(?Section $section null$validStatusesOnly false): float
  170.     {
  171.         $remaining 0;
  172.         foreach ($this->getPersons() as $person) {
  173.             foreach ($person->getActiveRegistrations($section) as $activeRegistration) {
  174.                 if ($validStatusesOnly && !in_array(
  175.                         $activeRegistration->getStatus(),
  176.                         RegistrationStatus::VALID_VALUES,
  177.                         true
  178.                     )) {
  179.                     continue;
  180.                 }
  181.                 $remaining += $activeRegistration->getOrder()->getRemainingAmount(false);
  182.             }
  183.         }
  184.         return $remaining;
  185.     }
  186.     public function getTotalToPay(?Section $section null): float
  187.     {
  188.         $total 0;
  189.         foreach ($this->getPersons() as $person) {
  190.             foreach ($person->getActiveRegistrations() as $activeRegistration) {
  191.                 if ($section === null || $section === $activeRegistration->getSeason()->getSection()) {
  192.                     if (in_array($activeRegistration->getStatus(), RegistrationStatus::VALID_VALUEStrue)) {
  193.                         $total += $activeRegistration->getOrder()?->getAmount() ?? 0.0;
  194.                     }
  195.                 }
  196.             }
  197.         }
  198.         return $total;
  199.     }
  200.     /**
  201.      * @return Collection<int, Debt>
  202.      */
  203.     public function getDebts(): Collection
  204.     {
  205.         $collection = new ArrayCollection();
  206.         foreach ($this->getPersons() as $person) {
  207.             foreach ($person->getDebts() as $debt) {
  208.                 $collection->add($debt);
  209.             }
  210.         }
  211.         return $collection;
  212.     }
  213.     /**
  214.      * @return Collection<int, Debt>
  215.      */
  216.     public function getUnpaidDebts(): Collection
  217.     {
  218.         return $this->getDebts()->filter(fn(Debt $debt) => $debt->getAmountPayed() < $debt->getAmount());
  219.     }
  220.     /**
  221.      * @return Collection<int, LegalPerson>
  222.      */
  223.     public function getLegalPersons(): Collection
  224.     {
  225.         return $this->legalPersons;
  226.     }
  227.     public function addLegalPerson(LegalPerson $legalPerson): self
  228.     {
  229.         if (!$this->legalPersons->contains($legalPerson)) {
  230.             $this->legalPersons->add($legalPerson);
  231.             $legalPerson->setFamily($this);
  232.         }
  233.         return $this;
  234.     }
  235.     public function removeLegalPerson(LegalPerson $legalPerson): self
  236.     {
  237.         if ($this->legalPersons->removeElement($legalPerson)) {
  238.             // set the owning side to null (unless already changed)
  239.             if ($legalPerson->getFamily() === $this) {
  240.                 $legalPerson->setFamily(null);
  241.             }
  242.         }
  243.         return $this;
  244.     }
  245.     /**
  246.      * @return Collection<int, Donation>
  247.      */
  248.     public function getDonations(?Section $section null): Collection
  249.     {
  250.         $donations = new ArrayCollection();
  251.         foreach ($this->getPersons() as $person) {
  252.             foreach ($person->getDonations() as $donation) {
  253.                 if ($section === null || $donation->getSection() === $section) {
  254.                     $donations->add($donation);
  255.                 }
  256.             }
  257.         }
  258.         foreach ($this->getLegalPersons() as $person) {
  259.             foreach ($person->getDonations() as $donation) {
  260.                 if ($section === null || $donation->getSection() === $section) {
  261.                     $donations->add($donation);
  262.                 }
  263.             }
  264.         }
  265.         return $donations;
  266.     }
  267. }