<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use App\Repository\CompanyRepository;
use App\Entity\Status;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Finder\Finder;
/**
* @ORM\Entity(repositoryClass=CompanyRepository::class)
* @ORM\HasLifecycleCallbacks
*/
class Company
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=69, nullable=true)
*/
private $name;
/**
* @ORM\Column(type="string", length=14, nullable=true)
*/
private $siret;
/**
* @ORM\Column(type="string", length=25, nullable=true)
*/
private $tva;
/**
* @ORM\Column(type="string", length=69, nullable=true)
*/
private $email;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $code_client;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $price_type;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $discount_rate;
/**
* @ORM\Column(type="boolean", nullable=false)
*/
private $grouping;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $grouping_comment;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $shipping_cost_tax_excluded;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $free_shipping_tax_excluded;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $note;
/**
* @ORM\OneToOne(targetEntity=BillingAddress::class, mappedBy="company", cascade={"persist", "remove"})
*/
private $billingAddress;
/**
* @ORM\OneToMany(targetEntity=Contact::class, mappedBy="company", orphanRemoval=true)
*/
private $contacts;
/**
* @ORM\ManyToOne(targetEntity=Status::class, cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private $status;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $date_add;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $date_update;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="companies")
* @ORM\JoinColumn(nullable=true)
*/
private $owner;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="created_companies")
* @ORM\JoinColumn(nullable=false)
*/
private $commercial;
/**
* @ORM\OneToMany(targetEntity=DeliveryAddress::class, mappedBy="company", orphanRemoval=true)
*/
private $deliveryAddresses;
/**
* @ORM\ManyToOne(targetEntity=Job::class, inversedBy="companies")
*/
private $job;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $known_as;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $exception_price;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $departure;
/**
* @ORM\Column(type="boolean")
*/
private $locked;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $billingFrequency;
public function __construct()
{
$this->contacts = new ArrayCollection();
$this->deliveryAddresses = new ArrayCollection();
$this->locked = false;
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getSiret(): ?string
{
return $this->siret;
}
public function setSiret(?string $siret): self
{
$this->siret = $siret;
return $this;
}
public function getTva(): ?string
{
return $this->tva;
}
public function setTva(?string $tva): self
{
$this->tva = $tva;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
public function getCodeClient(): ?string
{
return $this->code_client;
}
public function setCodeClient(?string $code_client): self
{
$this->code_client = $code_client;
return $this;
}
public function getPriceType(): ?string
{
return $this->price_type;
}
public function setPriceType(?string $price_type): self
{
$this->price_type = $price_type;
return $this;
}
public function getDiscountRate(): ?float
{
return $this->discount_rate;
}
public function setDiscountRate(?float $discount_rate): self
{
$this->discount_rate = $discount_rate;
return $this;
}
public function getGrouping(): ?bool
{
return $this->grouping;
}
public function setGrouping(bool $grouping): self
{
$this->grouping = $grouping;
return $this;
}
public function getGroupingComment(): ?string
{
return $this->grouping_comment;
}
public function setGroupingComment(?string $grouping_comment): self
{
$this->grouping_comment = $grouping_comment;
return $this;
}
public function getShippingCostTaxExcluded(): ?float
{
return $this->shipping_cost_tax_excluded;
}
public function setShippingCostTaxExcluded(?float $shipping_cost_tax_excluded): self
{
$this->shipping_cost_tax_excluded = $shipping_cost_tax_excluded;
return $this;
}
public function getFreeShippingTaxExcluded(): ?float
{
return $this->free_shipping_tax_excluded;
}
public function setFreeShippingTaxExcluded(?float $free_shipping_tax_excluded): self
{
$this->free_shipping_tax_excluded = $free_shipping_tax_excluded;
return $this;
}
public function getNote(): ?string
{
return $this->note;
}
public function setNote(?string $note): self
{
$this->note = $note;
return $this;
}
public function getBillingAddress(): ?BillingAddress
{
return $this->billingAddress;
}
public function setBillingAddress(BillingAddress $billingAddress): self
{
$this->billingAddress = $billingAddress;
// set the owning side of the relation if necessary
if ($billingAddress->getCompany() !== $this) {
$billingAddress->setCompany($this);
}
return $this;
}
/**
* @return Collection|Contact[]
*/
public function getContacts(): Collection
{
return $this->contacts;
}
public function addContact(Contact $contact): self
{
if (!$this->contacts->contains($contact)) {
$this->contacts[] = $contact;
$contact->setCompany($this);
}
return $this;
}
public function removeContact(Contact $contact): self
{
if ($this->contacts->contains($contact)) {
$this->contacts->removeElement($contact);
// set the owning side to null (unless already changed)
if ($contact->getCompany() === $this) {
$contact->setCompany(null);
}
}
return $this;
}
public function getStatus(): ?Status
{
return $this->status;
}
public function setStatus(?Status $status): self
{
$this->status = $status;
return $this;
}
public function getDateAdd(): ?\DateTimeInterface
{
return $this->date_add;
}
public function setDateAdd(?\DateTimeInterface $date_add): self
{
$this->date_add = $date_add;
return $this;
}
public function getDateUpdate(): ?\DateTimeInterface
{
return $this->date_update;
}
public function setDateUpdate(?\DateTimeInterface $date_update): self
{
$this->date_update = $date_update;
return $this;
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps()
{
$this->setDateUpdate(new \DateTime('now'));
if ($this->getDateAdd() === null) {
$this->setDateAdd(new \DateTime('now'));
}
}
public function getOwner(): ?User
{
return $this->owner;
}
public function setOwner(?User $owner): self
{
$this->owner = $owner;
return $this;
}
public function getCommercial(): ?User
{
return $this->commercial;
}
public function setCommercial(?User $commercial): self
{
$this->commercial = $commercial;
return $this;
}
public function getFiles()
{
if ((new Finder())->directories()->in("company_documents")->name($this->getId())->hasResults()) {
$finder = new Finder();
if ($finder->files()->in("company_documents/{$this->getId()}")->hasResults()) {
foreach ($finder as $file) {
$files[] = $file;
}
}
}
return isset($files) ? $files : [];
}
/**
* @param string $timestamp
* @param string $inode
*
* @return mixed
*/
public function getFile(string $timestamp, string $inode)
{
$finder = new Finder();
$date = date("Y-m-d H:i:s", $timestamp);
if ((new Finder())->directories()->in("company_documents")->name($this->getId())->hasResults()) {
if ($finder->files()->in("company_documents/{$this->getId()}")->date($date)->hasResults()) {
foreach ($finder as $file) {
if ($file->getInode() == $inode) {
return $file;
}
}
}
}
return [];
}
/**
* @param string $type (kbis|mandat)
*
* @return bool
*/
public function hasFile(string $type)
{
if ((new Finder())->directories()->in("company_documents")->name($this->getId())->hasResults()) {
$finder = new Finder();
if ($finder->files()->in("company_documents/{$this->getId()}")->hasResults()) {
foreach ($finder as $file) {
if (preg_match('/' . $type . '/', $file->getRelativePathName())) {
return true;
}
}
}
}
return false;
}
/**
* @return string
*/
public function generateCodeSage(BillingAddress $billingAddress): string
{
$code_commercial = $this->getCommercial()->getCode();
$prefix = 'L';
if (!empty($code_commercial)) {
switch (substr($code_commercial, 0, 3)) {
case '202':
case '203':
$prefix = 'G';
break;
case '331':
case '351':
case '381':
$prefix = 'P';
break;
case preg_match('/^MA/', $code_commercial) ? $code_commercial : !$code_commercial:
$prefix = 'M';
break;
}
}
if (soundex($this->getPriceType()) == soundex('REVENDEUR')) {
$prefix = 'C';
}
if ($billingAddress != null) {
if (soundex($billingAddress->getCountry()) != soundex('FRANCE')) {
$prefix = 'E';
}
$name = substr($this->getName(), 0, 3);
$postcode = $billingAddress->getPostcode();
return strtoupper(str_replace(' ', '', ($prefix . $name . $postcode)));
}
return $prefix;
}
/**
* @return Collection|DeliveryAddress[]
*/
public function getDeliveryAddresses(): Collection
{
return $this->deliveryAddresses;
}
public function addDeliveryAddress(DeliveryAddress $deliveryAddress): self
{
if (!$this->deliveryAddresses->contains($deliveryAddress)) {
$this->deliveryAddresses[] = $deliveryAddress;
$deliveryAddress->setCompany($this);
}
return $this;
}
public function removeDeliveryAddress(DeliveryAddress $deliveryAddress): self
{
if ($this->deliveryAddresses->contains($deliveryAddress)) {
$this->deliveryAddresses->removeElement($deliveryAddress);
// set the owning side to null (unless already changed)
if ($deliveryAddress->getCompany() === $this) {
$deliveryAddress->setCompany(null);
}
}
return $this;
}
public function getJob(): ?Job
{
return $this->job;
}
public function setJob(?Job $job): self
{
$this->job = $job;
return $this;
}
public function getKnownAs(): ?string
{
return $this->known_as;
}
public function setKnownAs(?string $known_as): self
{
$this->known_as = $known_as;
return $this;
}
public function getExceptionPrice(): ?bool
{
return $this->exception_price;
}
public function setExceptionPrice(?bool $exception_price): self
{
$this->exception_price = $exception_price;
return $this;
}
public function getDeparture(): ?string
{
return $this->departure;
}
public function setDeparture(?string $departure): self
{
$this->departure = $departure;
return $this;
}
public function getRequiredFields()
{
return [
'getName',
'getEmail',
'getSiret',
'getTva',
'getJob',
'getKnownAs'
];
}
public function getLocked(): ?bool
{
return $this->locked;
}
public function setLocked(?bool $locked): self
{
$this->locked = $locked ?? false;
return $this;
}
public function getBillingFrequency(): ?string
{
return $this->billingFrequency;
}
public function setBillingFrequency(string $billingFrequency): self
{
$this->billingFrequency = $billingFrequency;
return $this;
}
}