<?php
namespace App\Entity;
use App\Repository\CommandeFournisseurRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: CommandeFournisseurRepository::class)]
class CommandeFournisseur
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 50, unique: true)]
private ?string $numeroCommande = null;
#[ORM\ManyToOne(inversedBy: 'commandes')]
#[ORM\JoinColumn(nullable: false)]
private ?Fournisseur $fournisseur = null;
#[ORM\Column(type: Types::DATE_MUTABLE)]
private ?\DateTimeInterface $dateCommande = null;
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
private ?\DateTimeInterface $dateLivraisonPrevue = null;
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
private ?\DateTimeInterface $dateLivraisonReelle = null;
#[ORM\Column(length: 50)]
private ?string $statut = 'brouillon';
// brouillon, validee, envoyee, en_cours, partiellement_recue, recue, annulee
#[ORM\Column(type: Types::DECIMAL, precision: 15, scale: 2)]
private ?string $montantHT = '0.00';
#[ORM\Column(type: Types::DECIMAL, precision: 15, scale: 2, nullable: true)]
private ?string $montantTVA = null;
#[ORM\Column(type: Types::DECIMAL, precision: 15, scale: 2)]
private ?string $montantTotal = '0.00';
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $notes = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $conditionsLivraison = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $conditionsPaiement = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
private ?User $createdBy = null;
#[ORM\ManyToOne]
private ?User $validatedBy = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $dateValidation = null;
#[ORM\ManyToOne]
private ?Boutique $boutique = null;
#[ORM\OneToMany(mappedBy: 'commande', targetEntity: LigneCommande::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
private Collection $lignes;
#[ORM\OneToOne(mappedBy: 'commande', cascade: ['persist', 'remove'])]
private ?AchatFournisseur $achat = null;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $updatedAt = null;
public function __construct()
{
$this->lignes = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable();
$this->dateCommande = new \DateTime();
}
// Getters et Setters...
public function getId(): ?int
{
return $this->id;
}
public function getNumeroCommande(): ?string
{
return $this->numeroCommande;
}
public function setNumeroCommande(string $numeroCommande): static
{
$this->numeroCommande = $numeroCommande;
return $this;
}
public function getFournisseur(): ?Fournisseur
{
return $this->fournisseur;
}
public function setFournisseur(?Fournisseur $fournisseur): static
{
$this->fournisseur = $fournisseur;
return $this;
}
public function getDateCommande(): ?\DateTimeInterface
{
return $this->dateCommande;
}
public function setDateCommande(\DateTimeInterface $dateCommande): static
{
$this->dateCommande = $dateCommande;
return $this;
}
public function getDateLivraisonPrevue(): ?\DateTimeInterface
{
return $this->dateLivraisonPrevue;
}
public function setDateLivraisonPrevue(?\DateTimeInterface $dateLivraisonPrevue): static
{
$this->dateLivraisonPrevue = $dateLivraisonPrevue;
return $this;
}
public function getDateLivraisonReelle(): ?\DateTimeInterface
{
return $this->dateLivraisonReelle;
}
public function setDateLivraisonReelle(?\DateTimeInterface $dateLivraisonReelle): static
{
$this->dateLivraisonReelle = $dateLivraisonReelle;
return $this;
}
public function getStatut(): ?string
{
return $this->statut;
}
public function setStatut(string $statut): static
{
$this->statut = $statut;
return $this;
}
public function getMontantHT(): ?string
{
return $this->montantHT;
}
public function setMontantHT(string $montantHT): static
{
$this->montantHT = $montantHT;
return $this;
}
public function getMontantTVA(): ?string
{
return $this->montantTVA;
}
public function setMontantTVA(?string $montantTVA): static
{
$this->montantTVA = $montantTVA;
return $this;
}
public function getMontantTotal(): ?string
{
return $this->montantTotal;
}
public function setMontantTotal(string $montantTotal): static
{
$this->montantTotal = $montantTotal;
return $this;
}
public function getNotes(): ?string
{
return $this->notes;
}
public function setNotes(?string $notes): static
{
$this->notes = $notes;
return $this;
}
public function getConditionsLivraison(): ?string
{
return $this->conditionsLivraison;
}
public function setConditionsLivraison(?string $conditionsLivraison): static
{
$this->conditionsLivraison = $conditionsLivraison;
return $this;
}
public function getConditionsPaiement(): ?string
{
return $this->conditionsPaiement;
}
public function setConditionsPaiement(?string $conditionsPaiement): static
{
$this->conditionsPaiement = $conditionsPaiement;
return $this;
}
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function setCreatedBy(?User $createdBy): static
{
$this->createdBy = $createdBy;
return $this;
}
public function getValidatedBy(): ?User
{
return $this->validatedBy;
}
public function setValidatedBy(?User $validatedBy): static
{
$this->validatedBy = $validatedBy;
return $this;
}
public function getDateValidation(): ?\DateTimeInterface
{
return $this->dateValidation;
}
public function setDateValidation(?\DateTimeInterface $dateValidation): static
{
$this->dateValidation = $dateValidation;
return $this;
}
public function getBoutique(): ?Boutique
{
return $this->boutique;
}
public function setBoutique(?Boutique $boutique): static
{
$this->boutique = $boutique;
return $this;
}
/**
* @return Collection<int, LigneCommande>
*/
public function getLignes(): Collection
{
return $this->lignes;
}
public function addLigne(LigneCommande $ligne): static
{
if (!$this->lignes->contains($ligne)) {
$this->lignes->add($ligne);
$ligne->setCommande($this);
}
return $this;
}
public function removeLigne(LigneCommande $ligne): static
{
if ($this->lignes->removeElement($ligne)) {
if ($ligne->getCommande() === $this) {
$ligne->setCommande(null);
}
}
return $this;
}
public function getAchat(): ?AchatFournisseur
{
return $this->achat;
}
public function setAchat(?AchatFournisseur $achat): static
{
// set the owning side of the relation if necessary
if ($achat !== null && $achat->getCommande() !== $this) {
$achat->setCommande($this);
}
$this->achat = $achat;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeImmutable $updatedAt): static
{
$this->updatedAt = $updatedAt;
return $this;
}
public function calculerMontants(): void
{
$totalHT = 0;
foreach ($this->lignes as $ligne) {
$totalHT += floatval($ligne->getQuantite()) * floatval($ligne->getPrixUnitaire());
}
$this->montantHT = number_format($totalHT, 2, '.', '');
// TVA 18%
$montantTVA = $totalHT * 0.18;
$this->montantTVA = number_format($montantTVA, 2, '.', '');
$this->montantTotal = number_format($totalHT + $montantTVA, 2, '.', '');
}
public function genererNumeroCommande(): string
{
$date = $this->dateCommande ?? new \DateTime();
return 'CMD-' . $date->format('Ymd') . '-' . str_pad($this->id ?? 0, 5, '0', STR_PAD_LEFT);
}
public function peutEtreValidee(): bool
{
return $this->statut === 'brouillon' && $this->lignes->count() > 0;
}
public function peutEtreTransformeeEnAchat(): bool
{
return in_array($this->statut, ['recue', 'partiellement_recue']) && $this->achat === null;
}
public function __toString(): string
{
return $this->numeroCommande ?? 'Nouvelle commande';
}
}