<?php
namespace App\Entity\Formation;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\Formation\ApaymentRepository;
use App\Repository\Formation\TrashapaymentRepository;
use App\Traits\Actions;
use App\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityNotFoundException;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: TrashapaymentRepository::class)]
#[ORM\Table(name: 'hs_tc_formation_trashapayment')]
#[ApiResource]
class Trashapayment implements \Stringable
{
use Actions;
#[ORM\Column]
private ?int $participantId = null;
#[ORM\Column]
private ?int $classeId = null;
#[ORM\Column]
private ?string $classeName = null;
#[ORM\Column]
private ?string $participantFirstName = null;
#[ORM\Column]
private ?string $participantLastName = null;
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(nullable: true)]
private User $initie;
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(nullable: true)]
private User $suppressor;
#[ORM\Column(name: "costs", type: "string", length: 255)]
private string $costs;
#[ORM\Column(name: "way", type: "string", length: 255, nullable: true)]
private ?string $way;
#[ORM\Column(name: "amount", type: "float")]
private float $amount;
#[ORM\Column(name: "doc_number", type: "string", length: 255)]
private string $docNumber = '11';
// #[ORM\Column(name: "cached", type: "boolean")]
// private bool $cached;
#[ORM\Column(type: 'string', length: 255)]
private string $moyen;
#[ORM\OneToMany(mappedBy: "payment", targetEntity: Trashapayline::class, cascade: ["all"], orphanRemoval: true)]
private Collection $items;
#[ORM\Column(type: 'datetime', nullable: true)]
private ?\DateTime $trashedAt = null;
public function __construct(Apayment $apayment)
{
// date_default_timezone_set('Africa/Casablanca');
$this->way = $apayment->getWay();
$this->amount = $apayment->getAmount();
$this->costs = $apayment->getCosts();
$this->initie = $apayment->getInitie();
$this->createAt=$apayment->getCreateAt();
$this->publishedAt=$apayment->getPublishedAt();
$this->published=$apayment->isPublished();
$this->participantId=$apayment->getParticipant()->getId();
$this->participantFirstName=$apayment->getParticipant()->getFirstName();
$this->participantLastName=$apayment->getParticipant()->getLastName();
$this->classeId=$apayment->getParticipant()->getClasse()->getId();
$this->classeName=$apayment->getParticipant()->getClasse();
$this->moyen=$apayment->getMoyen();
$this->trashedAt=new \DateTime('now');
$this->items = new ArrayCollection();
foreach ($apayment->getItems() as $apayline) {
$item=new Trashapayline($apayline);
$item->setPayment($this);
$this->items->add($item);
}
}
public function restore(Participant $participant, array $costs)
{
$apayment = new Apayment();
// date_default_timezone_set('Africa/Casablanca');
$apayment->setWay($this->getWay());
$apayment->setAmount($this->getAmount());
$apayment->setCosts($this->getCosts());
$apayment->setInitie($this->getInitie());
$apayment->setCreateAt2($this->getCreateAt());
$apayment->setPublishedAt($this->publishedAt);
$apayment->setPublished($this->isPublished());
$apayment->setParticipant($participant);
$apayment->setMoyen($this->getMoyen());
foreach ($costs as $costItem) {
$apayline = $costItem['trashapayline']->restore($costItem['cost']);
$apayment->addItem($apayline);
}
return $apayment;
}
/**
* @return string
*/
public function getCosts(): ?string
{
return $this->costs;
}
/**
* @param string $costs
*/
public function setCosts(?string $costs): Trashapayment
{
$this->costs = $costs;
return $this;
}
/**
* Set way
*
* @param string $way
*
* @return Trashapayment
*/
public function setWay($way)
{
$this->way = $way;
return $this;
}
/**
* Get way
*
* @return string
*/
public function getWay()
{
return $this->way;
}
public function getMoyen(): string
{
return $this->moyen;
}
public function setMoyen(string $moyen): Trashapayment
{
$this->moyen = $moyen;
return $this;
}
/**
* Set amount
*
* @param float $amount
*
* @return Trashapayment
*/
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 Trashapayment
*/
public function setDocNumber($docNumber)
{
$this->docNumber = $docNumber;
return $this;
}
/**
* Get docNumber
*
* @return string
*/
public function getDocNumber()
{
return $this->docNumber;
}
public function getParticipantId(): ?int
{
return $this->participantId;
}
public function setParticipantId(?int $participantId): void
{
$this->participantId = $participantId;
}
public function getClasseId(): ?int
{
return $this->classeId;
}
public function setClasseId(?int $classeId): void
{
$this->classeId = $classeId;
}
public function getClasseName(): ?string
{
return $this->classeName;
}
public function setClasseName(?string $classeName): void
{
$this->classeName = $classeName;
}
public function getParticipantFirstName(): ?string
{
return $this->participantFirstName;
}
public function setParticipantFirstName(?string $participantFirstName): void
{
$this->participantFirstName = $participantFirstName;
}
public function getParticipantLastName(): ?string
{
return $this->participantLastName;
}
public function setParticipantLastName(?string $participantLastName): void
{
$this->participantLastName = $participantLastName;
}
/**
* Set initie
*
*
* @return Trashapayment
*/
public function setInitie(User $initie)
{
$this->initie = $initie;
return $this;
}
/**
* Get initie
*
* @return User
*/
public function getInitie()
{
return $this->initie;
}
/**
* Add item
*
*
* @return Trashapayment
*/
public function addItem(Trashapayline $item)
{
$item->setPayment($this);
$this->items->add($item);
return $this;
}
/**
* Remove item
*/
public function removeItem(Trashapayline $item)
{
$this->items->removeElement($item);
}
/**
* Get items
*
* @return Collection
*/
public function getItems()
{
return $this->items;
}
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->participant->getCompletName().'<br>'.
'Raison:'.'<br>'.
'Montant: '.$this->getAmount().' DH<br>'
;
}
public function getSuppressor(): User
{
return $this->suppressor;
}
public function setSuppressor(User $suppressor): Trashapayment
{
$this->suppressor = $suppressor;
return $this;
}
public function getTrashedAt(): ?\DateTime
{
return $this->trashedAt;
}
public function setTrashedAt(?\DateTime $trashedAt): Trashapayment
{
$this->trashedAt = $trashedAt;
return $this;
}
public function setCreateAt2(\DateTime $dateTime): Trashapayment
{
$this->createAt=$dateTime;
return $this;
}
public function getParticipantName(): ?string
{
return $this->getParticipantLastName().' '.$this->getParticipantFirstName();
}
}