<?php
namespace App\Entity;
use App\Repository\CaisseRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: CaisseRepository::class)]
class Caisse
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 100)]
private ?string $nom = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $description = null;
#[ORM\ManyToOne(inversedBy: 'caisses')]
#[ORM\JoinColumn(nullable: false)]
private ?Boutique $boutique = null;
#[ORM\Column(type: Types::DECIMAL, precision: 15, scale: 2)]
private ?string $fondsDepart = '0.00';
#[ORM\Column(type: Types::DECIMAL, precision: 15, scale: 2)]
private ?string $soldeActuel = '0.00';
#[ORM\Column(length: 50)]
private ?string $statut = 'fermee'; // fermee, ouverte
#[ORM\ManyToOne(targetEntity: User::class)]
private ?User $responsable = null;
#[ORM\Column]
private ?bool $estActive = true;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\OneToMany(mappedBy: 'caisse', targetEntity: SessionCaisse::class, orphanRemoval: true)]
private Collection $sessions;
#[ORM\OneToMany(mappedBy: 'caisse', targetEntity: MouvementCaisse::class, orphanRemoval: true)]
private Collection $mouvements;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $derniereOuverture = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $derniereFermeture = null;
public function __construct()
{
$this->sessions = new ArrayCollection();
$this->mouvements = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable();
}
public function getId(): ?int
{
return $this->id;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom): static
{
$this->nom = $nom;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): static
{
$this->description = $description;
return $this;
}
public function getBoutique(): ?Boutique
{
return $this->boutique;
}
public function setBoutique(?Boutique $boutique): static
{
$this->boutique = $boutique;
return $this;
}
public function getFondsDepart(): ?string
{
return $this->fondsDepart;
}
public function setFondsDepart(string $fondsDepart): static
{
$this->fondsDepart = $fondsDepart;
return $this;
}
public function getSoldeActuel(): ?string
{
return $this->soldeActuel;
}
public function setSoldeActuel(string $soldeActuel): static
{
$this->soldeActuel = $soldeActuel;
return $this;
}
public function getStatut(): ?string
{
return $this->statut;
}
public function setStatut(string $statut): static
{
$this->statut = $statut;
return $this;
}
public function getResponsable(): ?User
{
return $this->responsable;
}
public function setResponsable(?User $responsable): static
{
$this->responsable = $responsable;
return $this;
}
public function isEstActive(): ?bool
{
return $this->estActive;
}
public function setEstActive(bool $estActive): static
{
$this->estActive = $estActive;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return Collection<int, SessionCaisse>
*/
public function getSessions(): Collection
{
return $this->sessions;
}
public function addSession(SessionCaisse $session): static
{
if (!$this->sessions->contains($session)) {
$this->sessions->add($session);
$session->setCaisse($this);
}
return $this;
}
public function removeSession(SessionCaisse $session): static
{
if ($this->sessions->removeElement($session)) {
if ($session->getCaisse() === $this) {
$session->setCaisse(null);
}
}
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->setCaisse($this);
}
return $this;
}
public function removeMouvement(MouvementCaisse $mouvement): static
{
if ($this->mouvements->removeElement($mouvement)) {
if ($mouvement->getCaisse() === $this) {
$mouvement->setCaisse(null);
}
}
return $this;
}
public function getDerniereOuverture(): ?\DateTimeInterface
{
return $this->derniereOuverture;
}
public function setDerniereOuverture(?\DateTimeInterface $derniereOuverture): static
{
$this->derniereOuverture = $derniereOuverture;
return $this;
}
public function getDerniereFermeture(): ?\DateTimeInterface
{
return $this->derniereFermeture;
}
public function setDerniereFermeture(?\DateTimeInterface $derniereFermeture): static
{
$this->derniereFermeture = $derniereFermeture;
return $this;
}
public function getSessionOuverte(): ?SessionCaisse
{
foreach ($this->sessions as $session) {
if ($session->getStatut() === 'ouverte') {
return $session;
}
}
return null;
}
public function isOuverte(): bool
{
return $this->statut === 'ouverte' && $this->getSessionOuverte() !== null;
}
public function ajouterMontant(float $montant): void
{
$soldeActuel = floatval($this->soldeActuel);
$this->soldeActuel = number_format($soldeActuel + $montant, 2, '.', '');
}
public function retirerMontant(float $montant): void
{
$soldeActuel = floatval($this->soldeActuel);
$this->soldeActuel = number_format($soldeActuel - $montant, 2, '.', '');
}
public function __toString(): string
{
return $this->nom ?? '';
}
}