<?php
namespace App\Entity\Training;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\Training\FpaymentRepository;
use App\Traits\Actions;
use App\Entity\Training\Fpayline;
use App\Entity\Training\Trainee;
use App\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: FpaymentRepository::class)]
#[ORM\Table(name: 'hs_tc_training_fpayment')]
#[ApiResource]
class Fpayment implements \Stringable
{
use Actions;
#[ORM\ManyToOne(targetEntity: "Trainee", inversedBy: "payments")]
#[ORM\JoinColumn(nullable: false)]
private Trainee $trainee;
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: "payments")]
#[ORM\JoinColumn(nullable: true)]
private User $initie;
#[ORM\Column(name: "fees", type: "string", length: 255)]
private string $fees;
#[ORM\Column(name: "way", type: "string", length: 255, nullable: true)]
private ?string $way = null;
#[ORM\Column(name: "amount", type: "float")]
private float $amount;
#[ORM\Column(name: "doc_number", type: "string", length: 255)]
private string $docNumber = '----';
// #[ORM\Column(name: "cached", type: "boolean")]
// private bool $cached;
#[ORM\Column(type: 'string', length: 255)]
private string $moyen;
#[ORM\OneToMany(mappedBy: "payment", targetEntity: Fpayline::class, cascade: ["all"], orphanRemoval: true)]
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 string
*/
public function getFees(): ?string
{
return $this->fees;
}
/**
* @param string $fees
*/
public function setFees(?string $fees): Fpayment
{
$this->fees = $fees;
return $this;
}
/**
* Set way
*
* @param string $way
*
* @return Fpayment
*/
public function setWay($way)
{
$this->way = $way;
return $this;
}
/**
* Get way
*
* @return string
*/
public function getWay()
{
return $this->way;
}
/**
* Set amount
*
* @param float $amount
*
* @return Fpayment
*/
public function setAmount($amount)
{
$this->amount = $amount;
return $this;
}
/**
* Get amount
*
* @return float
*/
public function getAmount()
{
return $this->amount;
}
/**
* Set docNumber
*
* @param string $docNumber
*
* @return Fpayment
*/
public function setDocNumber($docNumber)
{
$this->docNumber = $docNumber;
return $this;
}
/**
* Get docNumber
*
* @return string
*/
public function getDocNumber()
{
return $this->docNumber;
}
public function getMoyen(): string
{
return $this->moyen;
}
public function setMoyen(string $moyen): Fpayment
{
$this->moyen = $moyen;
return $this;
}
/**
* Set cached
*
* @param boolean $cached
*
* @return Fpayment
*/
/**
* Set trainee
*
*
* @return Fpayment
*/
public function setTrainee(Trainee $trainee)
{
$this->trainee = $trainee;
return $this;
}
/**
* Get trainee
*
* @return Trainee
*/
public function getTrainee()
{
return $this->trainee;
}
/**
* Set initie
*
*
* @return Fpayment
*/
public function setInitie(User $initie)
{
$this->initie = $initie;
return $this;
}
/**
* Get initie
*
* @return User
*/
public function getInitie()
{
return $this->initie;
}
/**
* Add item
*
*
* @return Fpayment
*/
public function addItem(Fpayline $item)
{
$item->setPayment($this);
$this->items->add($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 getPrevious(){
$items=[];
/** @var Fpayment $payment */
foreach ($this->getTrainee()->getPayments() as $payment){
if($payment->getCreateAt()<=$this->getCreateAt() && $payment->getId()<$this->getId()){
$items[]=$payment;
}
}
return $items;
}
public function getSumPrevious(){
$s=0;
/** @var Fpayment $pay */
foreach ($this->getPrevious() as $pay){
if($pay->getFees()==$this->getFees()) $s+=$pay->getAmount();
}
return $s;
}
public function getTotalAmountFee(){
$m=0;
/** @var Fpayline $item */
foreach ($this->items as $item){
$m+=$item->getFee()->getMontant();
}
return $m;
}
public function getPrinterAmount($amount){
if($this->fees=='Frais de formation' || $this->fees =='monthlyPayment') {
if ($this->trainee->isDiscount()) {
return $amount;
} else {
return ceil($amount * $this->items->count() * $this->trainee->getGroupe()->getTraining()->getMonthlyPayment() / $this->getTotalAmountFee());
}
}
else return $amount;
}
public function getMonthsPaid($format='long'){
$months=[];
$amount=$this->amount;
if($this->trainee->getFeesTraining()!=0) {
//if($this->trainee->isDiscount() || $this->trainee->getMonthlyPayment()==$this->trainee->getGroupe()->getTraining()->getMonthlyPayment()){
$index = intdiv($this->getSumPrevious(), $this->trainee->getFeesTraining());
if ($this->getSumPrevious() % $this->trainee->getFeesTraining() != 0) {
$months[] = ['month' => $this->trainee->getGroupe()->getMonth($index, $format), 'amount' => $this->getPrinterAmount($this->trainee->getFeesTraining() - ($this->getSumPrevious() % $this->trainee->getFeesTraining()))];
$index++;
$amount -= $this->trainee->getFeesTraining() - ($this->getSumPrevious() % $this->trainee->getFeesTraining());
}
while ($amount > 0) {
if ($amount >= $this->trainee->getFeesTraining()) {
$months[] = ['month' => $this->trainee->getGroupe()->getMonth($index, $format), 'amount' => $this->getPrinterAmount($this->trainee->getFeesTraining())];
} else {
$months[] = ['month' => $this->trainee->getGroupe()->getMonth($index, $format), 'amount' => $this->getPrinterAmount($amount)];
}
$amount -= $this->trainee->getFeesTraining();
$index++;
}
// }
// else{
// $index = intdiv($this->getSumPrevious(), $this->trainee->getFeesTraining());
// if ($this->getSumPrevious() % $this->trainee->getFeesTraining() != 0) {
// $months[] = ['month' => $this->trainee->getGroupe()->getMonth($index, $format), 'amount' => $this->trainee->getFeesTraining() - ($this->getSumPrevious() % $this->trainee->getFeesTraining())];
// $index++;
// $amount -= $this->trainee->getFeesTraining() - ($this->getSumPrevious() % $this->trainee->getFeesTraining());
// }
// while ($amount >= 0) {
// if ($amount >= $this->trainee->getFeesTraining()) {
// $months[] = ['month' => $this->trainee->getGroupe()->getMonth($index, $format), 'amount' => $this->trainee->getGroupe()->getTraining()->getMonthlyPayment()];
// } else {
// $months[] = ['month' => $this->trainee->getGroupe()->getMonth($index, $format), 'amount' => $amount];
// }
// $amount -= $this->trainee->getFeesTraining();
// $index++;
// }
// }
}
return $months;
}
public function getMonthPaid(){
$nbr =$this->trainee->getFeesTraining()!=0?intdiv($this->getSumPrevious()+$this->getAmount(), $this->trainee->getFeesTraining()):0;
return $nbr-1;
}
public function getMonths(){
return $this->way;
}
public function __toString(): string
{
return
'Id:'.$this->id.'<br>'.
'Date:'.$this->getCreateAt()->format('d-m-Y').'<br>'.
'Stagiaire: '.$this->trainee->getCompletName().'<br>'.
'Raison:'.'<br>'.
'Montant: '.$this->getAmount().' DH<br>'
;
}
public function getWayPerso(){
$way='';
/** @var Fpayline $item */
foreach ($this->getItems() as $key=>$item){
if($key==0) $way.=$item->getFee()->getDisplayType(). ': '; else $way.=' - ';
$way.=$item->getFee()->getName().' ('.number_format($item->getAmount(), 2).' DH)';
}
return $way;
}
public function getWayPrinter(){
$way='';
/** @var Fpayline $item */
foreach ($this->getItems() as $key=>$item){
if($key==0) $way.=$item->getFee()->getDisplayType(). ': '; else $way.=' - ';
$way.=$item->getFee()->getName().' ('.number_format($item->getPrinterAmount(), 2).' DH)';
}
return $way;
}
public function setCreateAt2(\DateTime $dateTime): Fpayment
{
$this->createAt=$dateTime;
return $this;
}
}