<?php
namespace App\Entity;
use App\Repository\FeeRepository;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* @ORM\Entity(repositoryClass=FeeRepository::class)
*/
class Fee
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity=Promotion::class, inversedBy="fees")
* @ORM\JoinTable(name="fee_promotion")
*/
private $promotions;
/**
* @ORM\ManyToOne(targetEntity=SchoolYear::class, inversedBy="fees")
* @ORM\JoinColumn(nullable=false)
*/
private $schoolYear;
/**
* @ORM\ManyToOne(targetEntity=School::class, inversedBy="fees")
* @ORM\JoinColumn(nullable=false)
*/
private $school;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="decimal", precision=10, scale=2)
*/
private $amount;
/**
* @ORM\ManyToOne(targetEntity=Currency::class)
* @ORM\JoinColumn(nullable=false)
*/
private $currency;
/**
* @ORM\Column(type="datetime_immutable")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $updatedAt;
/**
* @ORM\OneToMany(targetEntity=Payment::class, mappedBy="fee")
*/
private $payments;
public function __construct()
{
$this->promotions = new ArrayCollection();
$this->payments = new ArrayCollection();
}
public function __toString(): string
{
return $this->name . ' - ' . $this->amount . ' ' . ($this->currency ? $this->currency->getSymbol() : '');
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return Collection<int, Promotion>
*/
public function getPromotions(): Collection
{
return $this->promotions;
}
public function addPromotion(Promotion $promotion): self
{
if (!$this->promotions->contains($promotion)) {
$this->promotions->add($promotion);
}
return $this;
}
public function removePromotion(Promotion $promotion): self
{
$this->promotions->removeElement($promotion);
return $this;
}
public function getSchoolYear(): ?SchoolYear
{
return $this->schoolYear;
}
public function setSchoolYear(?SchoolYear $schoolYear): self
{
$this->schoolYear = $schoolYear;
return $this;
}
public function getSchool(): ?School
{
return $this->school;
}
public function setSchool(?School $school): self
{
$this->school = $school;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getAmount(): ?float
{
return $this->amount ? (float) $this->amount : null;
}
public function setAmount(float $amount): self
{
$this->amount = $amount;
return $this;
}
public function getCurrency(): ?Currency
{
return $this->currency;
}
public function setCurrency(?Currency $currency): self
{
$this->currency = $currency;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return Collection<int, Payment>
*/
public function getPayments(): Collection
{
return $this->payments;
}
public function addPayment(Payment $payment): self
{
if (!$this->payments->contains($payment)) {
$this->payments->add($payment);
$payment->setFee($this);
}
return $this;
}
public function removePayment(Payment $payment): self
{
if ($this->payments->removeElement($payment)) {
// set the owning side to null (unless already changed)
if ($payment->getFee() === $this) {
$payment->setFee(null);
}
}
return $this;
}
/**
* Calcule le montant total payé pour ce frais
*/
public function getTotalAmountPaid(): float
{
$total = 0;
foreach ($this->payments as $payment) {
$total += (float) $payment->getAmountPaid();
}
return $total;
}
/**
* Calcule le montant restant à payer
*/
public function getRemainingAmount(): float
{
return (float) $this->amount - $this->getTotalAmountPaid();
}
/**
* Vérifie si le frais est complètement payé
*/
public function isFullyPaid(): bool
{
return $this->getRemainingAmount() <= 0;
}
/**
* Calcule le pourcentage de paiement
*/
public function getPaymentPercentage(): float
{
if ((float) $this->amount <= 0) {
return 0;
}
return ($this->getTotalAmountPaid() / (float) $this->amount) * 100;
}
}