<?php
namespace App\Entity;
use App\Repository\DocumentRepository;
use App\Tools\Fnc;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=DocumentRepository::class)
*/
class Document
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $type_document;
/**
* @ORM\ManyToOne(targetEntity=Projet::class, inversedBy="documents")
* @ORM\JoinColumn(nullable=false)
*/
private $projet;
/**
* @ORM\ManyToOne(targetEntity=Societe::class, inversedBy="documents")
* @ORM\JoinColumn(nullable=false)
*/
private $tiers;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $statut;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $reference;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $num_piece;
/**
* @ORM\Column(type="datetime")
*/
private $date_crea;
/**
* @ORM\Column(type="integer")
*/
private $user_crea;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $date_modif;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $user_modif;
/**
* @ORM\ManyToOne(targetEntity=Adresse::class)
*/
private $adresse;
/**
* @ORM\Column(type="float")
*/
private $montant_ht;
/**
* @ORM\Column(type="float")
*/
private $montant_tva;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $date_envoi;
/**
* @ORM\OneToMany(targetEntity=DocumentLigne::class, mappedBy="document", cascade={"persist"})
*/
private $documentLignes;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $date_paiement;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $contact;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $id_doc_origine;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $commentaire;
/**
* @ORM\Column(type="date", nullable=true)
*/
private $date_bdc;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $lib_projet;
/**
* @ORM\ManyToOne(targetEntity=Tiers::class, inversedBy="ndf")
*/
private $salarie;
/**
* @ORM\Column(type="date", nullable=true)
*/
private $date_ndf;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $libelle;
public function __construct()
{
$this->documentLignes = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTypeDocument(): ?string
{
return $this->type_document;
}
public function setTypeDocument(string $type_document): self
{
$this->type_document = $type_document;
return $this;
}
public function getProjet(): ?Projet
{
return $this->projet;
}
public function setProjet(?Projet $projet): self
{
$this->projet = $projet;
return $this;
}
public function getTiers(): ?Societe
{
return $this->tiers;
}
public function setTiers(?Societe $tiers): self
{
$this->tiers = $tiers;
return $this;
}
public function getStatut(): ?string
{
return $this->statut;
}
public function setStatut(?string $statut): self
{
$this->statut = $statut;
return $this;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference(?string $reference): self
{
$this->reference = $reference;
return $this;
}
public function getNumPiece(): ?string
{
return $this->num_piece;
}
public function setNumPiece(?string $num_piece): self
{
$this->num_piece = $num_piece;
return $this;
}
public function getDateCrea(): ?\DateTimeInterface
{
return $this->date_crea;
}
public function setDateCrea(\DateTimeInterface $date_crea): self
{
$this->date_crea = $date_crea;
return $this;
}
public function getUserCrea(): ?int
{
return $this->user_crea;
}
public function setUserCrea(int $user_crea): self
{
$this->user_crea = $user_crea;
return $this;
}
public function getDateModif(): ?\DateTimeInterface
{
return $this->date_modif;
}
public function setDateModif(?\DateTimeInterface $date_modif): self
{
$this->date_modif = $date_modif;
return $this;
}
public function getUserModif(): ?int
{
return $this->user_modif;
}
public function setUserModif(?int $user_modif): self
{
$this->user_modif = $user_modif;
return $this;
}
public function getAdresse(): ?Adresse
{
return $this->adresse;
}
public function setAdresse(?Adresse $adresse): self
{
$this->adresse = $adresse;
return $this;
}
public function getMontantHt(): ?float
{
return $this->montant_ht;
}
public function setMontantHt(float $montant_ht): self
{
$this->montant_ht = $montant_ht;
return $this;
}
public function getMontantTva(): ?float
{
return $this->montant_tva;
}
public function setMontantTva(float $montant_tva): self
{
$this->montant_tva = $montant_tva;
return $this;
}
public function getDateEnvoi(): ?\DateTimeInterface
{
return $this->date_envoi;
}
public function setDateEnvoi(?\DateTimeInterface $date_envoi): self
{
$this->date_envoi = $date_envoi;
return $this;
}
/**
* @return Collection<int, DocumentLigne>
*/
public function getDocumentLignes(): Collection
{
return $this->documentLignes;
}
public function addDocumentLigne(DocumentLigne $documentLigne): self
{
if (!$this->documentLignes->contains($documentLigne)) {
$this->documentLignes[] = $documentLigne;
$documentLigne->setDocument($this);
}
return $this;
}
public function removeDocumentLigne(DocumentLigne $documentLigne): self
{
if ($this->documentLignes->removeElement($documentLigne)) {
// set the owning side to null (unless already changed)
if ($documentLigne->getDocument() === $this) {
$documentLigne->setDocument(null);
}
}
return $this;
}
public function orderDocLigneByTauxTva()
{
$this->documentLignes = new ArrayCollection(
Fnc::array_orderby($this->getDocumentLignes()->toArray(), 'getTauxTva()', SORT_ASC)
);
}
public function getLibelleFirstLigne(): string
{
if(0===$this->getDocumentLignes()->count()) return 'Aucune ligne dans le document';
return array_reduce(
$this->getDocumentLignes()->toArray(),
function ($lib, $ligne){
$libelle = $ligne->getLibelle();
if('Avance'!==$libelle) {
$libelle .= ' - ';
$libelle .= $ligne->getTauxTva() * 100;
$libelle .= '%';
} else {
$libelle .= ' : ';
$libelle .= $ligne->getPuht();
$libelle .= '€';
}
if(empty($lib)) return $libelle;
else return $lib. '<br>' . $libelle;
}
);
}
public function getDatePaiement(): ?\DateTimeInterface
{
return $this->date_paiement;
}
public function setDatePaiement(?\DateTimeInterface $date_paiement): self
{
$this->date_paiement = $date_paiement;
return $this;
}
public function getContact(): ?string
{
return $this->contact;
}
public function setContact(?string $contact): self
{
$this->contact = $contact;
return $this;
}
public function getVentilationTVA(): array
{
$ventilation = array();
$ligneList = $this->getDocumentLignes()->toArray();
foreach ($ligneList as $ligne){
if('Avance'===$ligne->getLibelle()) continue;
$txTva = $ligne->getTauxTva();
if(!isset($ventilation[$txTva])) $ventilation[$txTva] = (object) array(
'taux_tva' => $txTva*100,
'montant_tva' => 0,
);
$ventilation[$txTva]->montant_tva+= $ligne->getMontantTva();
}
ksort($ventilation);
return $ventilation;
}
public function performMontant(): self
{
$montantHT = 0;
$montantTVA = 0;
$ligneList = $this->getDocumentLignes()->toArray();
foreach ($ligneList as $ligne){
$montantHT+= $ligne->getMontantHt();
$montantTVA+= $ligne->getMontantTva();
}
$this->setMontantHt($montantHT);
$this->setMontantTva($montantTVA);
return $this;
}
public function initDocVierge(Projet $projet, string $typeDoc): self
{
switch ($typeDoc){
case 'FACTURE PROFORMA':
$adresseClient = $projet->getSociete()->getAdresses()->last();
$this->setNumPiece($projet->getNumProd() .'-xxxxxxx')
->setTiers( $projet->getSociete() )
->setAdresse($adresseClient)
->setDateBdc($projet->getDateCrea())
;
break;
case 'COMMANDE PRESTATAIRE':
$societe = new Societe();
$societe->setLibSociete('Nouvelle commande prestataire');
$tiers = new Tiers();
$tiers->addSociete($societe)
->setPrenomTiers('-')
->setEmailTiers('-')
->setTelFixeTiers('-')
->setTelPortableTiers('-')
;
$this->setTiers($societe)
->setStatut('Non Payée')
;
$ligne = new DocumentLigne();
$ligne->setTauxTva(0.2);
$this->addDocumentLigne($ligne);
break;
case 'NDF':
break;
default:
break;
}
$this->setProjet($projet)
->setDateCrea(new \DateTime())
->setTypeDocument($typeDoc)
->setMontantHt(0)
;
return $this;
}
public function getIdDocOrigine(): ?int
{
return $this->id_doc_origine;
}
public function setIdDocOrigine(?int $id_doc_origine): self
{
$this->id_doc_origine = $id_doc_origine;
return $this;
}
public function getCommentaire(): ?string
{
return $this->commentaire;
}
public function setCommentaire(?string $commentaire): self
{
$this->commentaire = $commentaire;
return $this;
}
public function getDateBdc(): ?\DateTimeInterface
{
return $this->date_bdc;
}
public function setDateBdc(?\DateTimeInterface $date_bdc): self
{
$this->date_bdc = $date_bdc;
return $this;
}
public function getLibProjet(): ?string
{
return $this->lib_projet;
}
public function setLibProjet(?string $lib_projet): self
{
$this->lib_projet = $lib_projet;
return $this;
}
public function getSalarie(): ?Tiers
{
return $this->salarie;
}
public function setSalarie(?Tiers $salarie): self
{
$this->salarie = $salarie;
return $this;
}
public function getDateNdf(): ?\DateTimeInterface
{
return $this->date_ndf;
}
public function setDateNdf(?\DateTimeInterface $date_ndf): self
{
$this->date_ndf = $date_ndf;
return $this;
}
public function getLibelle(): ?string
{
return $this->libelle;
}
public function setLibelle(?string $libelle): self
{
$this->libelle = $libelle;
return $this;
}
public function groupNdfBySalarie($ndfList): array
{
$salarieList = [];
/** @var Document $ndf */
foreach ($ndfList as $ndf){
$idSalarie = $ndf->getSalarie()->getId();
if(!isset($salarieList[$idSalarie])){
$salarieList[$idSalarie] = (object)[
'id_projet' => $ndf->getProjet()->getId(),
'id_salarie' => $idSalarie,
'salarie' => $ndf->getSalarie(),
'total_ht' => 0,
'total_ttc' => 0,
'select_option' => [],
'ndf_list' => [],
];
}
$salarieList[$idSalarie]->total_ht+= $ndf->getMontantHt();
$salarieList[$idSalarie]->total_ttc+= $ndf->getMontantHt() + $ndf->getMontantTva();
$salarieList[$idSalarie]->ndf_list[]= $ndf;
$mois = $ndf->getDateNdf()->format('Y-m');
if(!isset($salarieList[$idSalarie]->select_option[$mois])){
$annee = $ndf->getDateNdf()->format('Y');
$libMois = Fnc::getLibMois(
$ndf->getDateNdf()->format('m')
);
$salarieList[$idSalarie]->select_option[$mois] = '<option value="'. $mois .'">'. $libMois .' '. $annee.'</option>';
$salarieList[$idSalarie]->select_option[$mois.'-en_GB'] = '<option value="'. $mois .'&lang=en_GB">'. $libMois .' '. $annee.' en 🇬🇧</option>';
}
ksort($salarieList[$idSalarie]->select_option);
}
return $salarieList;
}
}