<?php
namespace App\Entity\Formation;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\Formation\CostRepository;
use App\Traits\Actions;
use App\Entity\Formation\Apayline;
use App\Entity\Formation\Participant;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: CostRepository::class)]
#[ORM\Table(name: 'hs_tc_formation_cost')]
#[ApiResource]
class Cost
{
use Actions;
#[ORM\ManyToOne(targetEntity: Participant::class, inversedBy: "costs")]
#[ORM\JoinColumn(nullable: false)]
private ?Participant $participant = 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: "activity_year", type: "string", length: 255, nullable: true)]
private ?string $activityYear = 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: "cost", targetEntity: Apayline::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): Cost
{
$this->montant = $montant;
return $this;
}
public function getInitialAmount(): ?float
{
return $this->initialAmount;
}
public function setInitialAmount(?float $initialAmount): Cost
{
$this->initialAmount = $initialAmount;
return $this;
}
public function getDisplayedAmount(): ?float
{
return $this->displayedAmount;
}
public function setDisplayedAmount(?float $displayedAmount): Cost
{
$this->displayedAmount = $displayedAmount;
return $this;
}
public function isExempt(): bool
{
return $this->exempt;
}
public function setExempt(bool $exempt): Cost
{
$this->exempt = $exempt;
return $this;
}
/**
* @return Participant
*/
public function getParticipant(): ?Participant
{
return $this->participant;
}
/**
* @param Participant $participant
*/
public function setParticipant(?Participant $participant): Cost
{
$this->participant = $participant;
return $this;
}
/**
* @return string
*/
public function getType(): ?string
{
return $this->type;
}
/**
* @param string $type
*/
public function setType(?string $type): Cost
{
$this->type = $type;
return $this;
}
public function getDisplayType(){
return match ($this->type) {
'monthlyPayment' => 'Frais de formation',
'registerCosts' => 'Frais d\'inscription',
'examCosts' => 'Frais d\'examen',
'diplomaCosts' => 'Frais de diplĂ´me',
'otherCosts' => 'Autre frais',
default => '',
};
}
/**
* @return string
*/
public function getName(): ?string
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(?string $name): Cost
{
$this->name = $name;
return $this;
}
/**
* @return DateTime
*/
public function getDatePaie(): ?DateTime
{
return $this->datePaie;
}
/**
* @param DateTime $datePaie
*/
public function setDatePaie(?DateTime $datePaie): Cost
{
$this->datePaie = $datePaie;
return $this;
}
/**
* @return DateTime
*/
public function getTerm(): ?DateTime
{
return $this->term;
}
/**
* @param DateTime $term
*/
public function setTerm(?DateTime $term): Cost
{
$this->term = $term;
return $this;
}
/**
* @return string
*/
public function getActivityYear(): ?string
{
return $this->activityYear;
}
/**
* @param string $activityYear
*/
public function setActivityYear(?string $activityYear): Cost
{
$this->activityYear = $activityYear;
return $this;
}
/**
* Add item
*
*
* @return Cost
*/
public function addItem(Apayline $item)
{
$item->setCost($this);
$this->items[] = $item;
return $this;
}
/**
* Remove item
*/
public function removeItem(Apayline $item)
{
$this->items->removeElement($item);
}
/**
* Get items
*
* @return Collection
*/
public function getItems()
{
return $this->items;
}
public function isNotAbandoned(){
if($this->participant->getStatus()=='Abandonné' && $this->datePaie>$this->participant->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 Apayline $item */
foreach ($this->items as $item){
$m+=$item->getAmount();
}
return $m;
}
public function restToPay(){
return max($this->getMontant()-$this->amountPaied(),0);
}
public function advance(){
return max($this->amountPaied()-$this->getMontant(),0);
}
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 getPcPay(){
if($this->getMontant()!=0)
return round($this->amountPaied()/$this->getMontant()*100,0);
else
return -1;
}
public function getSchoolyear(){
foreach ($this->getParticipant()->getClasse()->getActivityYears() as $schoolyear){
if($this->getParticipant()->getClasse()->getActivityYear($schoolyear)==$this->getActivityYear()) return $schoolyear;
}
return null;
}
public function getRegularAmount(){
return match ($this->type) {
'monthlyPayment' => $this->getParticipant()->getClasse()->getMonthlyPayment(),
'registerCosts' => $this->getParticipant()->getClasse()->getRegisterCosts(),
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->participant->isDiscount()) {
return $this->montant;
} else {
return $this->participant->getClasse()->getActivity()->getMonthlyPayment();
}
}
else return $this->montant;
}
}