<?php
namespace App\Entity;
use App\Repository\SocieteRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=SocieteRepository::class)
*/
class Societe
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=20)
*/
private $classe_societe;
/**
* @ORM\Column(type="string", length=255)
*/
private $lib_societe;
/**
* @ORM\Column(type="string", length=255)
*/
private $type_societe;
/**
* @ORM\Column(type="string", length=20)
*/
private $sigle_societe;
/**
* @ORM\Column(type="integer")
*/
private $nb_historique_projet;
/**
* @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\Column(type="boolean")
*/
private $is_active;
/**
* @ORM\ManyToMany(targetEntity=Tiers::class, inversedBy="societes")
*/
private $tiers;
/**
* @ORM\OneToMany(targetEntity=Adresse::class, mappedBy="societe")
*/
private $adresses;
/**
* @var Tiers
*/
private $main_tiers = null;
/**
* @ORM\OneToMany(targetEntity=Projet::class, mappedBy="societe")
*/
private $projets;
/**
* @ORM\OneToMany(targetEntity=Document::class, mappedBy="tiers")
*/
private $documents;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $num_tva;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $condition_reglement;
public function __construct()
{
$this->getMainTiers();
$this->tiers = new ArrayCollection();
$this->adresses = new ArrayCollection();
$this->projets = new ArrayCollection();
$this->documents = new ArrayCollection();
}
public function clearForJsonResp(){
$this->getMainTiers();
$this->tiers = new ArrayCollection();
// $this->adresses = new ArrayCollection();
$this->projets = new ArrayCollection();
$this->documents = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getClasseSociete(): ?string
{
return $this->classe_societe;
}
public function setClasseSociete(string $classe_societe): self
{
$this->classe_societe = $classe_societe;
return $this;
}
public function getLibSociete(): ?string
{
return $this->lib_societe;
}
public function setLibSociete(string $lib_societe): self
{
$this->lib_societe = $lib_societe;
return $this;
}
public function getTypeSociete(): ?string
{
return $this->type_societe;
}
public function setTypeSociete(string $type_societe): self
{
$this->type_societe = $type_societe;
return $this;
}
public function getSigleSociete(): ?string
{
return $this->sigle_societe;
}
public function setSigleSociete(string $sigle_societe): self
{
$this->sigle_societe = $sigle_societe;
return $this;
}
public function getNbHistoriqueProjet(): ?int
{
return $this->nb_historique_projet;
}
public function setNbHistoriqueProjet(int $nb_historique_projet): self
{
$this->nb_historique_projet = $nb_historique_projet;
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 getIsActive(): ?bool
{
return $this->is_active;
}
public function setIsActive(bool $is_active): self
{
$this->is_active = $is_active;
return $this;
}
/**
* @return Collection<int, Tiers>
*/
public function getTiers(): Collection
{
return $this->tiers;
}
public function getMainTiers(): ?Tiers
{
if(null===$this->tiers) return null;
$tiersList = array();
foreach ($this->tiers as $tiers) $tiersList[ $tiers->getId() ] = $tiers;
if(!empty($tiersList)) {
ksort($tiersList);
$this->main_tiers = array_shift($tiersList);
}
return $this->main_tiers;
}
public function addTier(Tiers $tier): self
{
if (!$this->tiers->contains($tier)) {
$this->tiers[] = $tier;
}
return $this;
}
public function removeTier(Tiers $tier): self
{
$this->tiers->removeElement($tier);
return $this;
}
/**
* @return Collection<int, Adresse>
*/
public function getAdresses(): Collection
{
return $this->adresses;
}
public function addAdress(Adresse $adress): self
{
if (!$this->adresses->contains($adress)) {
$this->adresses[] = $adress;
$adress->setSociete($this);
}
return $this;
}
public function removeAdress(Adresse $adress): self
{
if ($this->adresses->removeElement($adress)) {
// set the owning side to null (unless already changed)
if ($adress->getSociete() === $this) {
$adress->setSociete(null);
}
}
return $this;
}
public function serializeAdresse()
{
if(empty($this->adresses)) return array();
$adresseList = array();
foreach ($this->adresses->toArray() as $adresse){
$adresseList[] = array(
'id_adresse' => $adresse->getId(),
'lib_adresse' => $adresse->getLibAdresse(),
'ligne_1' => $adresse->getLigne1(),
'ligne_2' => $adresse->getLigne2(),
'code_postal' => $adresse->getCodePostal(),
'ville' => $adresse->getVille(),
'pays' => $adresse->getPays(),
);
}
return $adresseList;
}
/**
* @return Collection<int, Projet>
*/
public function getProjets(): Collection
{
return $this->projets;
}
public function addProjet(Projet $projet): self
{
if (!$this->projets->contains($projet)) {
$this->projets[] = $projet;
$projet->setSociete($this);
}
return $this;
}
public function removeProjet(Projet $projet): self
{
if ($this->projets->removeElement($projet)) {
// set the owning side to null (unless already changed)
if ($projet->getSociete() === $this) {
$projet->setSociete(null);
}
}
return $this;
}
/**
* 'AO' = Appel d'offre + Appel d'offre annulé
* 'PROD' = Projet en cours + Projet terminé + Projet annulé
* @param string $statut
* Retourne le nombre de projet de la société
* @return int
*/
public function countNbProjet(string $statut): int
{
$projetList = array();
if('AO'===$statut){
$projetList = $this->getProjets()->filter(function (Projet $projet){
return in_array($projet->getStatut()->getId(), [1,2,3,4,5]) && $projet->getIsActive();
});
}
if('PROD'===$statut){
$projetList = $this->getProjets()->filter(function (Projet $projet){
return in_array($projet->getStatut()->getId(), [1,2,3,4,5]) && $projet->getIsActive();
});
}
return count($projetList);
}
/**
* Génère le numéro du prochain appel d'offre de la société
* @param string $prefix correspond à l'entité connectée
* @return string
*/
public function createNumAO(string $prefix): string
{
$nb = 1;
$nb+= $this->countNbProjet('AO');
return $prefix .'-'. $this->getSigleSociete() .'-AO-'. $nb;
}
/**
* Génère le numéro de production du prochain projet de la société
* @param string $prefix correspond à l'entité connectée
* @return string
*/
public function createNumProd(string $prefix): string
{
$nb = 1;
$nb+= $this->getNbHistoriqueProjet();
$nb+= $this->countNbProjet('PROD');
return $prefix .'-'. $this->getSigleSociete() .'-'. $nb;
}
/**
* @return Collection<int, Document>
*/
public function getDocuments(): Collection
{
return $this->documents;
}
public function addDocument(Document $document): self
{
if (!$this->documents->contains($document)) {
$this->documents[] = $document;
$document->setTiers($this);
}
return $this;
}
public function removeDocument(Document $document): self
{
if ($this->documents->removeElement($document)) {
// set the owning side to null (unless already changed)
if ($document->getTiers() === $this) {
$document->setTiers(null);
}
}
return $this;
}
public function getNumTva(): ?string
{
return $this->num_tva;
}
public function setNumTva(?string $num_tva): self
{
$this->num_tva = $num_tva;
return $this;
}
public function getConditionReglement(): ?string
{
return $this->condition_reglement;
}
public function setConditionReglement(?string $condition_reglement): self
{
$this->condition_reglement = $condition_reglement;
return $this;
}
}