<?php
namespace App\Entity;
use App\Repository\SessionCaisseRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: SessionCaisseRepository::class)]
class SessionCaisse
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'sessions')]
#[ORM\JoinColumn(nullable: false)]
private ?Caisse $caisse = null;
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(nullable: false)]
private ?User $utilisateur = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $dateOuverture = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $dateFermeture = null;
#[ORM\Column(type: Types::DECIMAL, precision: 15, scale: 2)]
private ?string $fondsOuverture = '0.00';
#[ORM\Column(type: Types::DECIMAL, precision: 15, scale: 2, nullable: true)]
private ?string $fondsFermeture = null;
#[ORM\Column(type: Types::DECIMAL, precision: 15, scale: 2, nullable: true)]
private ?string $fondsTheoriqueFermeture = null;
#[ORM\Column(type: Types::DECIMAL, precision: 15, scale: 2, nullable: true)]
private ?string $ecart = null;
#[ORM\Column(length: 50)]
private ?string $statut = 'ouverte'; // ouverte, fermee, validee
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $noteOuverture = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $noteFermeture = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $justificationEcart = null;
#[ORM\OneToMany(mappedBy: 'session', targetEntity: MouvementCaisse::class)]
private Collection $mouvements;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\ManyToOne(targetEntity: User::class)]
private ?User $fermePar = null;
#[ORM\ManyToOne(targetEntity: User::class)]
private ?User $validePar = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $dateValidation = null;
public function __construct()
{
$this->mouvements = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable();
$this->dateOuverture = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getCaisse(): ?Caisse
{
return $this->caisse;
}
public function setCaisse(?Caisse $caisse): static
{
$this->caisse = $caisse;
return $this;
}
public function getUtilisateur(): ?User
{
return $this->utilisateur;
}
public function setUtilisateur(?User $utilisateur): static
{
$this->utilisateur = $utilisateur;
return $this;
}
public function getDateOuverture(): ?\DateTimeInterface
{
return $this->dateOuverture;
}
public function setDateOuverture(\DateTimeInterface $dateOuverture): static
{
$this->dateOuverture = $dateOuverture;
return $this;
}
public function getDateFermeture(): ?\DateTimeInterface
{
return $this->dateFermeture;
}
public function setDateFermeture(?\DateTimeInterface $dateFermeture): static
{
$this->dateFermeture = $dateFermeture;
return $this;
}
public function getFondsOuverture(): ?string
{
return $this->fondsOuverture;
}
public function setFondsOuverture(string $fondsOuverture): static
{
$this->fondsOuverture = $fondsOuverture;
return $this;
}
public function getFondsFermeture(): ?string
{
return $this->fondsFermeture;
}
public function setFondsFermeture(?string $fondsFermeture): static
{
$this->fondsFermeture = $fondsFermeture;
return $this;
}
public function getFondsTheoriqueFermeture(): ?string
{
return $this->fondsTheoriqueFermeture;
}
public function setFondsTheoriqueFermeture(?string $fondsTheoriqueFermeture): static
{
$this->fondsTheoriqueFermeture = $fondsTheoriqueFermeture;
return $this;
}
public function getEcart(): ?string
{
return $this->ecart;
}
public function setEcart(?string $ecart): static
{
$this->ecart = $ecart;
return $this;
}
public function getStatut(): ?string
{
return $this->statut;
}
public function setStatut(string $statut): static
{
$this->statut = $statut;
return $this;
}
public function getNoteOuverture(): ?string
{
return $this->noteOuverture;
}
public function setNoteOuverture(?string $noteOuverture): static
{
$this->noteOuverture = $noteOuverture;
return $this;
}
public function getNoteFermeture(): ?string
{
return $this->noteFermeture;
}
public function setNoteFermeture(?string $noteFermeture): static
{
$this->noteFermeture = $noteFermeture;
return $this;
}
public function getJustificationEcart(): ?string
{
return $this->justificationEcart;
}
public function setJustificationEcart(?string $justificationEcart): static
{
$this->justificationEcart = $justificationEcart;
return $this;
}
/**
* @return Collection<int, MouvementCaisse>
*/
public function getMouvements(): Collection
{
return $this->mouvements;
}
public function addMouvement(MouvementCaisse $mouvement): static
{
if (!$this->mouvements->contains($mouvement)) {
$this->mouvements->add($mouvement);
$mouvement->setSession($this);
}
return $this;
}
public function removeMouvement(MouvementCaisse $mouvement): static
{
if ($this->mouvements->removeElement($mouvement)) {
if ($mouvement->getSession() === $this) {
$mouvement->setSession(null);
}
}
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function getFermePar(): ?User
{
return $this->fermePar;
}
public function setFermePar(?User $fermePar): static
{
$this->fermePar = $fermePar;
return $this;
}
public function getValidePar(): ?User
{
return $this->validePar;
}
public function setValidePar(?User $validePar): static
{
$this->validePar = $validePar;
return $this;
}
public function getDateValidation(): ?\DateTimeInterface
{
return $this->dateValidation;
}
public function setDateValidation(?\DateTimeInterface $dateValidation): static
{
$this->dateValidation = $dateValidation;
return $this;
}
public function calculerFondsTheorique(): void
{
$totalEntrees = 0;
$totalSorties = 0;
foreach ($this->mouvements as $mouvement) {
if ($mouvement->getTypeMouvement() === 'entree') {
$totalEntrees += floatval($mouvement->getMontant());
} else {
$totalSorties += floatval($mouvement->getMontant());
}
}
$fondsTheorique = floatval($this->fondsOuverture) + $totalEntrees - $totalSorties;
$this->fondsTheoriqueFermeture = number_format($fondsTheorique, 2, '.', '');
}
public function calculerEcart(): void
{
if ($this->fondsFermeture !== null && $this->fondsTheoriqueFermeture !== null) {
$ecart = floatval($this->fondsFermeture) - floatval($this->fondsTheoriqueFermeture);
$this->ecart = number_format($ecart, 2, '.', '');
}
}
public function getDuree(): ?int
{
if (!$this->dateOuverture) {
return null;
}
$dateFin = $this->dateFermeture ?? new \DateTime();
return $this->dateOuverture->diff($dateFin)->h;
}
public function getTotalEntrees(): float
{
$total = 0;
foreach ($this->mouvements as $mouvement) {
if ($mouvement->getTypeMouvement() === 'entree') {
$total += floatval($mouvement->getMontant());
}
}
return $total;
}
public function getTotalSorties(): float
{
$total = 0;
foreach ($this->mouvements as $mouvement) {
if ($mouvement->getTypeMouvement() === 'sortie') {
$total += floatval($mouvement->getMontant());
}
}
return $total;
}
public function __toString(): string
{
return 'Session ' . ($this->dateOuverture ? $this->dateOuverture->format('d/m/Y H:i') : '');
}
}