<?php
namespace App\Entity;
use App\Repository\AchatFournisseurRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: AchatFournisseurRepository::class)]
class AchatFournisseur
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'achats')]
#[ORM\JoinColumn(nullable: true)]
private ?Fournisseur $fournisseur = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $numeroFacture = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $dateAchat = null;
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
private ?\DateTimeInterface $dateEcheance = null;
#[ORM\Column]
private ?float $montantHT = null;
#[ORM\Column(nullable: true)]
private ?float $montantTVA = null;
#[ORM\Column]
private ?float $montantTotal = null;
#[ORM\Column(nullable: true)]
private ?float $montantPaye = 0;
#[ORM\Column(length: 50)]
private ?string $statut = 'EN_ATTENTE'; // EN_ATTENTE, PARTIELLEMENT_PAYE, PAYE, ANNULE
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $description = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
private ?Boutique $boutique = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true)]
private ?User $utilisateur = null;
#[ORM\OneToMany(mappedBy: 'achat', targetEntity: LigneAchat::class, cascade: ['persist', 'remove'])]
private Collection $lignes;
#[ORM\OneToMany(mappedBy: 'achat', targetEntity: PaiementFournisseur::class)]
private Collection $paiements;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $createdAt = null;
#[ORM\OneToOne(inversedBy: 'achat', cascade: ['persist', 'remove'])]
private ?CommandeFournisseur $commande = null;
#[ORM\OneToMany(mappedBy: 'achatFournisseur', targetEntity: EntreeStock::class)]
private Collection $entreesStock;
public function __construct()
{
$this->lignes = new ArrayCollection();
$this->paiements = new ArrayCollection();
$this->entreesStock = new ArrayCollection();
$this->createdAt = new \DateTime();
$this->dateAchat = 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 getNumeroFacture(): ?string
{
return $this->numeroFacture;
}
public function setNumeroFacture(?string $numeroFacture): static
{
$this->numeroFacture = $numeroFacture;
return $this;
}
public function getDateAchat(): ?\DateTimeInterface
{
return $this->dateAchat;
}
public function setDateAchat(\DateTimeInterface $dateAchat): static
{
$this->dateAchat = $dateAchat;
return $this;
}
public function getDateEcheance(): ?\DateTimeInterface
{
return $this->dateEcheance;
}
public function setDateEcheance(?\DateTimeInterface $dateEcheance): static
{
$this->dateEcheance = $dateEcheance;
return $this;
}
public function getMontantHT(): ?float
{
return $this->montantHT;
}
public function setMontantHT(float $montantHT): static
{
$this->montantHT = $montantHT;
return $this;
}
public function getMontantTVA(): ?float
{
return $this->montantTVA;
}
public function setMontantTVA(?float $montantTVA): static
{
$this->montantTVA = $montantTVA;
return $this;
}
public function getMontantTotal(): ?float
{
return $this->montantTotal;
}
public function setMontantTotal(float $montantTotal): static
{
$this->montantTotal = $montantTotal;
return $this;
}
public function getMontantPaye(): ?float
{
return $this->montantPaye ?? 0;
}
public function setMontantPaye(?float $montantPaye): static
{
$this->montantPaye = $montantPaye;
return $this;
}
public function getStatut(): ?string
{
return $this->statut;
}
public function setStatut(string $statut): static
{
$this->statut = $statut;
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 getUtilisateur(): ?User
{
return $this->utilisateur;
}
public function setUtilisateur(?User $utilisateur): static
{
$this->utilisateur = $utilisateur;
return $this;
}
/**
* @return Collection<int, LigneAchat>
*/
public function getLignes(): Collection
{
return $this->lignes;
}
public function addLigne(LigneAchat $ligne): static
{
if (!$this->lignes->contains($ligne)) {
$this->lignes->add($ligne);
$ligne->setAchat($this);
}
return $this;
}
public function removeLigne(LigneAchat $ligne): static
{
if ($this->lignes->removeElement($ligne)) {
if ($ligne->getAchat() === $this) {
$ligne->setAchat(null);
}
}
return $this;
}
/**
* @return Collection<int, PaiementFournisseur>
*/
public function getPaiements(): Collection
{
return $this->paiements;
}
public function addPaiement(PaiementFournisseur $paiement): static
{
if (!$this->paiements->contains($paiement)) {
$this->paiements->add($paiement);
$paiement->setAchat($this);
}
return $this;
}
public function removePaiement(PaiementFournisseur $paiement): static
{
if ($this->paiements->removeElement($paiement)) {
if ($paiement->getAchat() === $this) {
$paiement->setAchat(null);
}
}
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Calcule le montant restant à payer
*/
public function getMontantRestant(): float
{
return $this->montantTotal - $this->getMontantPaye();
}
/**
* Met à jour le statut selon les paiements
*/
public function updateStatut(): void
{
$montantPaye = $this->calculerMontantPaye();
$this->setMontantPaye($montantPaye);
if ($montantPaye <= 0) {
$this->setStatut('EN_ATTENTE');
} elseif ($montantPaye >= $this->montantTotal) {
$this->setStatut('PAYE');
} else {
$this->setStatut('PARTIELLEMENT_PAYE');
}
}
/**
* Calcule le montant payé via les paiements
*/
public function calculerMontantPaye(): float
{
$total = 0;
foreach ($this->paiements as $paiement) {
$total += $paiement->getMontant();
}
return $total;
}
public function getCommande(): ?CommandeFournisseur
{
return $this->commande;
}
public function setCommande(?CommandeFournisseur $commande): static
{
$this->commande = $commande;
return $this;
}
/**
* @return Collection<int, EntreeStock>
*/
public function getEntreesStock(): Collection
{
return $this->entreesStock;
}
public function addEntreeStock(EntreeStock $entreeStock): static
{
if (!$this->entreesStock->contains($entreeStock)) {
$this->entreesStock->add($entreeStock);
$entreeStock->setAchatFournisseur($this);
}
return $this;
}
public function removeEntreeStock(EntreeStock $entreeStock): static
{
if ($this->entreesStock->removeElement($entreeStock)) {
if ($entreeStock->getAchatFournisseur() === $this) {
$entreeStock->setAchatFournisseur(null);
}
}
return $this;
}
public function __toString(): string
{
return sprintf(
'Achat #%d - %s - %s FCFA',
$this->id ?? 0,
$this->fournisseur ? $this->fournisseur->getNom() : 'Sans fournisseur',
number_format($this->montantTotal, 0, ',', ' ')
);
}
}