<?php
namespace App\Entity;
use App\Repository\DetteFournisseurRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: DetteFournisseurRepository::class)]
class DetteFournisseur
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'dettes')]
#[ORM\JoinColumn(nullable: true)]
private ?Fournisseur $fournisseur = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
private ?AchatFournisseur $achat = null;
#[ORM\Column]
private ?float $montantInitial = null;
#[ORM\Column]
private ?float $montantPaye = 0;
#[ORM\Column]
private ?float $montantRestant = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $dateCreation = null;
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
private ?\DateTimeInterface $dateEcheance = null;
#[ORM\Column]
private ?bool $estPayee = false;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $datePaiementComplet = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $notes = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
private ?Boutique $boutique = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $reference = null;
public function __construct()
{
$this->dateCreation = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getFournisseur(): ?Fournisseur
{
return $this->fournisseur;
}
public function setFournisseur(?Fournisseur $fournisseur): static
{
$this->fournisseur = $fournisseur;
return $this;
}
public function getAchat(): ?AchatFournisseur
{
return $this->achat;
}
public function setAchat(?AchatFournisseur $achat): static
{
$this->achat = $achat;
return $this;
}
public function getMontantInitial(): ?float
{
return $this->montantInitial;
}
public function setMontantInitial(float $montantInitial): static
{
$this->montantInitial = $montantInitial;
$this->montantRestant = $montantInitial;
return $this;
}
public function getMontantPaye(): ?float
{
return $this->montantPaye ?? 0;
}
public function setMontantPaye(float $montantPaye): static
{
$this->montantPaye = $montantPaye;
return $this;
}
public function getMontantRestant(): ?float
{
return $this->montantRestant;
}
public function setMontantRestant(float $montantRestant): static
{
$this->montantRestant = $montantRestant;
return $this;
}
public function getDateCreation(): ?\DateTimeInterface
{
return $this->dateCreation;
}
public function setDateCreation(\DateTimeInterface $dateCreation): static
{
$this->dateCreation = $dateCreation;
return $this;
}
public function getDateEcheance(): ?\DateTimeInterface
{
return $this->dateEcheance;
}
public function setDateEcheance(?\DateTimeInterface $dateEcheance): static
{
$this->dateEcheance = $dateEcheance;
return $this;
}
public function isEstPayee(): ?bool
{
return $this->estPayee;
}
public function setEstPayee(bool $estPayee): static
{
$this->estPayee = $estPayee;
return $this;
}
public function getDatePaiementComplet(): ?\DateTimeInterface
{
return $this->datePaiementComplet;
}
public function setDatePaiementComplet(?\DateTimeInterface $datePaiementComplet): static
{
$this->datePaiementComplet = $datePaiementComplet;
return $this;
}
public function getNotes(): ?string
{
return $this->notes;
}
public function setNotes(?string $notes): static
{
$this->notes = $notes;
return $this;
}
public function getBoutique(): ?Boutique
{
return $this->boutique;
}
public function setBoutique(?Boutique $boutique): static
{
$this->boutique = $boutique;
return $this;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference(?string $reference): static
{
$this->reference = $reference;
return $this;
}
/**
* Enregistre un paiement sur cette dette
*/
public function enregistrerPaiement(float $montant): void
{
$this->montantPaye += $montant;
$this->montantRestant -= $montant;
if ($this->montantRestant <= 0.01) {
$this->montantRestant = 0;
$this->estPayee = true;
$this->datePaiementComplet = new \DateTime();
}
}
/**
* Vérifie si la dette est en retard
*/
public function estEnRetard(): bool
{
if ($this->estPayee || !$this->dateEcheance) {
return false;
}
return $this->dateEcheance < new \DateTime();
}
public function __toString(): string
{
return sprintf(
'Dette #%d - %s - Restant: %s FCFA',
$this->id ?? 0,
$this->fournisseur ? $this->fournisseur->getNom() : 'Sans fournisseur',
number_format($this->montantRestant, 0, ',', ' ')
);
}
}