<?php
namespace App\Entity;
use App\Repository\FactureRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: FactureRepository::class)]
class Facture
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $type = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $statutPaiement = null;
#[ORM\Column]
private ?float $montantTotal = null;
#[ORM\ManyToOne(inversedBy: 'factures')]
private ?Vente $vente = null;
#[ORM\OneToMany(mappedBy: 'facture', targetEntity: Echeance::class)]
private Collection $echeance;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $createdAt = null;
#[ORM\ManyToOne(inversedBy: 'facture')]
private ?Client $client = null;
#[ORM\Column(nullable: true)]
private ?float $montantPaye = null;
#[ORM\Column(nullable: true)]
private ?float $fraisTransport = null;
#[ORM\Column(nullable: true)]
private ?float $tva = null;
public function __construct()
{
$this->createdAt = new \DateTime();
$this->echeance = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): static
{
$this->type = $type;
return $this;
}
public function getStatutPaiement(): ?string
{
return $this->statutPaiement;
}
public function setStatutPaiement(?string $statutPaiement): static
{
$this->statutPaiement = $statutPaiement;
return $this;
}
public function getMontantTotal(): ?float
{
return $this->montantTotal;
}
public function setMontantTotal(float $montantTotal): static
{
$this->montantTotal = $montantTotal;
return $this;
}
public function getVente(): ?Vente
{
return $this->vente;
}
public function setVente(?Vente $vente): static
{
$this->vente = $vente;
return $this;
}
/**
* @return Collection<int, Echeance>
*/
public function getEcheance(): Collection
{
return $this->echeance;
}
public function addEcheance(Echeance $echeance): static
{
if (!$this->echeance->contains($echeance)) {
$this->echeance->add($echeance);
$echeance->setFacture($this);
}
return $this;
}
public function removeEcheance(Echeance $echeance): static
{
if ($this->echeance->removeElement($echeance)) {
// set the owning side to null (unless already changed)
if ($echeance->getFacture() === $this) {
$echeance->setFacture(null);
}
}
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function getClient(): ?Client
{
return $this->client;
}
public function setClient(?Client $client): static
{
$this->client = $client;
return $this;
}
public function getMontantPaye(): ?float
{
return $this->montantPaye;
}
public function setMontantPaye(?float $montantPaye): static
{
$this->montantPaye = $montantPaye;
return $this;
}
public function calculerMontantPaye(): float
{
$montantPaye = 0;
foreach ($this->echeance as $echeance) {
if ($echeance->isIsPaid()) {
$montantPaye += $echeance->getAmount();
}
}
return $montantPaye;
}
public function getResteAPayer(): float
{
// Inclure les frais de transport dans le calcul
$montantTotalAvecFrais = $this->montantTotal + ($this->fraisTransport ?? 0);
return $montantTotalAvecFrais - $this->calculerMontantPaye();
}
public function updateMontantPaye(): void
{
$this->montantPaye = $this->calculerMontantPaye();
}
public function isCompletementPayee(): bool
{
// Inclure les frais de transport dans la vérification
$montantTotalAvecFrais = $this->montantTotal + ($this->fraisTransport ?? 0);
return $this->calculerMontantPaye() >= $montantTotalAvecFrais;
}
public function getFraisTransport(): ?float
{
return $this->fraisTransport;
}
public function setFraisTransport(?float $fraisTransport): static
{
$this->fraisTransport = $fraisTransport;
return $this;
}
public function getTva(): ?float
{
return $this->tva;
}
public function setTva(?float $tva): static
{
$this->tva = $tva;
return $this;
}
/**
* Calcule le montant total avec frais de transport
*/
public function getMontantTotalAvecFrais(): float
{
return $this->montantTotal + ($this->fraisTransport ?? 0);
}
/**
* Calcule le pourcentage de paiement en tenant compte des frais de transport
*/
public function getPourcentagePaiement(): float
{
$montantTotalAvecFrais = $this->getMontantTotalAvecFrais();
if ($montantTotalAvecFrais <= 0) {
return 0;
}
$montantPaye = $this->calculerMontantPaye();
return min(100, ($montantPaye / $montantTotalAvecFrais) * 100);
}
}