<?php
namespace App\Entity\Training;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\Training\FeeRepository;
use App\Traits\Actions;
use App\Entity\Training\Fpayline;
use App\Entity\Training\Trainee;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: FeeRepository::class)]
#[ORM\Table(name: 'hs_tc_training_fee')]
#[ApiResource]
class Fee
{
use Actions;
#[ORM\ManyToOne(targetEntity: Trainee::class, inversedBy: "fees")]
#[ORM\JoinColumn(nullable: false)]
private ?Trainee $trainee = null;
#[ORM\Column(name: "montant", type: "float", nullable: true)]
private ?float $montant = null;
#[ORM\Column(name: "initial_amount", type: "float", nullable: true)]
private ?float $initialAmount = null;
#[ORM\Column(name: "displayed_amount", type: "float", nullable: true)]
private ?float $displayedAmount = null;
#[ORM\Column(name: "discount", type: "boolean")]
private bool $exempt=false;
#[ORM\Column(name: "type", type: "string", length: 255, nullable: true)]
private ?string $type = null;
#[ORM\Column(name: "training_year", type: "string", length: 255, nullable: true)]
private ?string $trainingYear = null;
#[ORM\Column(name: "name", type: "string", length: 255, nullable: true)]
private ?string $name = null;
#[ORM\Column(name: "date_paie", type: "datetime")]
private ?\DateTime $datePaie = null;
#[ORM\Column(name: "term", type: "datetime")]
private ?\DateTime $term = null;
#[ORM\OneToMany(mappedBy: "fee", targetEntity: Fpayline::class, cascade: ["all"], orphanRemoval: false)]
private Collection $items;
public function __construct()
{
date_default_timezone_set('Africa/Casablanca');
$this->createAt=new \DateTime('now');
$this->published=false;
$this->items=new ArrayCollection();
}
/**
* @return float
*/
public function getMontant(): ?float
{
return $this->montant;
}
/**
* @param float $montant
*/
public function setMontant(?float $montant): Fee
{
$this->montant = $montant;
return $this;
}
public function getInitialAmount(): ?float
{
return $this->initialAmount;
}
public function setInitialAmount(?float $initialAmount): Fee
{
$this->initialAmount = $initialAmount;
return $this;
}
public function getDisplayedAmount(): ?float
{
return $this->displayedAmount;
}
public function setDisplayedAmount(?float $displayedAmount): Fee
{
$this->displayedAmount = $displayedAmount;
return $this;
}
public function isExempt(): bool
{
return $this->exempt;
}
public function setExempt(bool $exempt): Fee
{
$this->exempt = $exempt;
return $this;
}
/**
* @return Trainee
*/
public function getTrainee(): ?Trainee
{
return $this->trainee;
}
/**
* @param Trainee $trainee
*/
public function setTrainee(?Trainee $trainee): Fee
{
$this->trainee = $trainee;
return $this;
}
/**
* @return string
*/
public function getType(): ?string
{
return $this->type;
}
/**
* @param string $type
*/
public function setType(?string $type): Fee
{
$this->type = $type;
return $this;
}
public function getDisplayType(){
return match ($this->type) {
'monthlyPayment' => 'Frais de formation',
'registerFees' => 'Frais d\'inscription',
'examFees' => 'Frais d\'examen',
'diplomaFees' => 'Frais de diplĂ´me',
default => '',
};
}
/**
* @return string
*/
public function getName(): ?string
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(?string $name): Fee
{
$this->name = $name;
return $this;
}
/**
* @return DateTime
*/
public function getDatePaie(): ?DateTime
{
return $this->datePaie;
}
/**
* @param DateTime $datePaie
*/
public function setDatePaie(?DateTime $datePaie): Fee
{
$this->datePaie = $datePaie;
return $this;
}
/**
* @return DateTime
*/
public function getTerm(): ?DateTime
{
return $this->term;
}
/**
* @param DateTime $term
*/
public function setTerm(?DateTime $term): Fee
{
$this->term = $term;
return $this;
}
/**
* @return string
*/
public function getTrainingYear(): ?string
{
return $this->trainingYear;
}
/**
* @param string $trainingYear
*/
public function setTrainingYear(?string $trainingYear): Fee
{
$this->trainingYear = $trainingYear;
return $this;
}
/**
* Add item
*
*
* @return Fee
*/
public function addItem(Fpayline $item)
{
$item->setFee($this);
$this->items[] = $item;
return $this;
}
/**
* Remove item
*/
public function removeItem(Fpayline $item)
{
$this->items->removeElement($item);
}
/**
* Get items
*
* @return Collection
*/
public function getItems()
{
return $this->items;
}
public function isNotAbandoned(){
if($this->trainee->getStatus()=='Abandonné' && $this->datePaie>$this->trainee->getStatusAt()) return false;
return true;
}
public function isCounted(){
date_default_timezone_set('Africa/Casablanca');
$now=new \DateTime('now');
if($this->datePaie<=$now && $this->isNotAbandoned()) return true;
return false;
}
public function isExpired(){
date_default_timezone_set('Africa/Casablanca');
$now=new \DateTime('now');
if($this->term<$now && $this->isNotAbandoned()) return true;
return false;
}
public function amountPaied(){
$m=0;
/** @var Fpayline $item */
foreach ($this->items as $item){
$m+=$item->getAmount();
}
return $m;
}
public function restToPay(){
return $this->getMontant()-$this->amountPaied();
}
public function isPaied(){
if($this->getMontant()<=$this->amountPaied()) return true;
return false;
}
public function isInProgress(){
date_default_timezone_set('Africa/Casablanca');
$now=new \DateTime('now');
if($this->datePaie<$now) return true;
return false;
}
public function getSchoolyear(){
foreach ($this->getTrainee()->getGroupe()->getTrainingYears() as $schoolyear){
if($this->getTrainee()->getGroupe()->getTrainingYear($schoolyear)==$this->getTrainingYear()) return $schoolyear;
}
return null;
}
public function getRegularAmount(){
return match ($this->type) {
'monthlyPayment' => $this->getTrainee()->getGroupe()->getTraining()->getMonthlyPayment(),
'registerFees' => $this->getTrainee()->getGroupe()->getTraining()->getRegisterFees(),
'examFees' => $this->getTrainee()->getGroupe()->getTraining()->getExamFees(),
'diplomaFees' => $this->getTrainee()->getGroupe()->getTraining()->getDiplomaFees(),
default => 0,
};
}
public function isDiscount(){
if($this->montant<$this->getRegularAmount()) return true;
return false;
}
public function getPrinterAmount(){
if($this->getDisplayType()=='Frais de formation') {
if ($this->trainee->isDiscount()) {
return $this->montant;
} else {
return $this->trainee->getGroupe()->getTraining()->getMonthlyPayment();
}
}
else return $this->montant;
}
}