src/Entity/Company.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use App\Repository\CompanyRepository;
  6. use App\Entity\Status;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Finder\Finder;
  9. /**
  10.  * @ORM\Entity(repositoryClass=CompanyRepository::class)
  11.  * @ORM\HasLifecycleCallbacks
  12.  */
  13. class Company
  14. {
  15.     /**
  16.      * @ORM\Id()
  17.      * @ORM\GeneratedValue()
  18.      * @ORM\Column(type="integer")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @ORM\Column(type="string", length=69, nullable=true)
  23.      */
  24.     private $name;
  25.     /**
  26.      * @ORM\Column(type="string", length=14, nullable=true)
  27.      */
  28.     private $siret;
  29.     /**
  30.      * @ORM\Column(type="string", length=25, nullable=true)
  31.      */
  32.     private $tva;
  33.     /**
  34.      * @ORM\Column(type="string", length=69, nullable=true)
  35.      */
  36.     private $email;
  37.     /**
  38.      * @ORM\Column(type="string", length=255, nullable=true)
  39.      */
  40.     private $code_client;
  41.     /**
  42.      * @ORM\Column(type="string", length=255, nullable=true)
  43.      */
  44.     private $price_type;
  45.     /**
  46.      * @ORM\Column(type="float", nullable=true)
  47.      */
  48.     private $discount_rate;
  49.     /**
  50.      * @ORM\Column(type="boolean", nullable=false)
  51.      */
  52.     private $grouping;
  53.     /**
  54.      * @ORM\Column(type="text", nullable=true)
  55.      */
  56.     private $grouping_comment;
  57.     /**
  58.      * @ORM\Column(type="float", nullable=true)
  59.      */
  60.     private $shipping_cost_tax_excluded;
  61.     /**
  62.      * @ORM\Column(type="float", nullable=true)
  63.      */
  64.     private $free_shipping_tax_excluded;
  65.     /**
  66.      * @ORM\Column(type="text", nullable=true)
  67.      */
  68.     private $note;
  69.     /**
  70.      * @ORM\OneToOne(targetEntity=BillingAddress::class, mappedBy="company", cascade={"persist", "remove"})
  71.      */
  72.     private $billingAddress;
  73.     /**
  74.      * @ORM\OneToMany(targetEntity=Contact::class, mappedBy="company", orphanRemoval=true)
  75.      */
  76.     private $contacts;
  77.     /**
  78.      * @ORM\ManyToOne(targetEntity=Status::class, cascade={"persist"})
  79.      * @ORM\JoinColumn(nullable=false)
  80.      */
  81.     private $status;
  82.     /**
  83.      * @ORM\Column(type="datetime", nullable=true)
  84.      */
  85.     private $date_add;
  86.     /**
  87.      * @ORM\Column(type="datetime", nullable=true)
  88.      */
  89.     private $date_update;
  90.     /**
  91.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="companies")
  92.      * @ORM\JoinColumn(nullable=true)
  93.      */
  94.     private $owner;
  95.     /**
  96.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="created_companies")
  97.      * @ORM\JoinColumn(nullable=false)
  98.      */
  99.     private $commercial;
  100.     /**
  101.      * @ORM\OneToMany(targetEntity=DeliveryAddress::class, mappedBy="company", orphanRemoval=true)
  102.      */
  103.     private $deliveryAddresses;
  104.     /**
  105.      * @ORM\ManyToOne(targetEntity=Job::class, inversedBy="companies")
  106.      */
  107.     private $job;
  108.     /**
  109.      * @ORM\Column(type="string", length=255, nullable=true)
  110.      */
  111.     private $known_as;
  112.     /**
  113.      * @ORM\Column(type="boolean", nullable=true)
  114.      */
  115.     private $exception_price;
  116.     /**
  117.      * @ORM\Column(type="string", length=255, nullable=true)
  118.      */
  119.     private $departure;
  120.     /**
  121.      * @ORM\Column(type="boolean")
  122.      */
  123.     private $locked;
  124.     /**
  125.      * @ORM\Column(type="string", length=255, nullable=true)
  126.      */
  127.     private $billingFrequency;
  128.     public function __construct()
  129.     {
  130.         $this->contacts = new ArrayCollection();
  131.         $this->deliveryAddresses = new ArrayCollection();
  132.         $this->locked false;
  133.     }
  134.     public function getId(): ?int
  135.     {
  136.         return $this->id;
  137.     }
  138.     public function getName(): ?string
  139.     {
  140.         return $this->name;
  141.     }
  142.     public function setName(?string $name): self
  143.     {
  144.         $this->name $name;
  145.         return $this;
  146.     }
  147.     public function getSiret(): ?string
  148.     {
  149.         return $this->siret;
  150.     }
  151.     public function setSiret(?string $siret): self
  152.     {
  153.         $this->siret $siret;
  154.         return $this;
  155.     }
  156.     public function getTva(): ?string
  157.     {
  158.         return $this->tva;
  159.     }
  160.     public function setTva(?string $tva): self
  161.     {
  162.         $this->tva $tva;
  163.         return $this;
  164.     }
  165.     public function getEmail(): ?string
  166.     {
  167.         return $this->email;
  168.     }
  169.     public function setEmail(?string $email): self
  170.     {
  171.         $this->email $email;
  172.         return $this;
  173.     }
  174.     public function getCodeClient(): ?string
  175.     {
  176.         return $this->code_client;
  177.     }
  178.     public function setCodeClient(?string $code_client): self
  179.     {
  180.         $this->code_client $code_client;
  181.         return $this;
  182.     }
  183.     public function getPriceType(): ?string
  184.     {
  185.         return $this->price_type;
  186.     }
  187.     public function setPriceType(?string $price_type): self
  188.     {
  189.         $this->price_type $price_type;
  190.         return $this;
  191.     }
  192.     public function getDiscountRate(): ?float
  193.     {
  194.         return $this->discount_rate;
  195.     }
  196.     public function setDiscountRate(?float $discount_rate): self
  197.     {
  198.         $this->discount_rate $discount_rate;
  199.         return $this;
  200.     }
  201.     public function getGrouping(): ?bool
  202.     {
  203.         return $this->grouping;
  204.     }
  205.     public function setGrouping(bool $grouping): self
  206.     {
  207.         $this->grouping $grouping;
  208.         return $this;
  209.     }
  210.     public function getGroupingComment(): ?string
  211.     {
  212.         return $this->grouping_comment;
  213.     }
  214.     public function setGroupingComment(?string $grouping_comment): self
  215.     {
  216.         $this->grouping_comment $grouping_comment;
  217.         return $this;
  218.     }
  219.     public function getShippingCostTaxExcluded(): ?float
  220.     {
  221.         return $this->shipping_cost_tax_excluded;
  222.     }
  223.     public function setShippingCostTaxExcluded(?float $shipping_cost_tax_excluded): self
  224.     {
  225.         $this->shipping_cost_tax_excluded $shipping_cost_tax_excluded;
  226.         return $this;
  227.     }
  228.     public function getFreeShippingTaxExcluded(): ?float
  229.     {
  230.         return $this->free_shipping_tax_excluded;
  231.     }
  232.     public function setFreeShippingTaxExcluded(?float $free_shipping_tax_excluded): self
  233.     {
  234.         $this->free_shipping_tax_excluded $free_shipping_tax_excluded;
  235.         return $this;
  236.     }
  237.     public function getNote(): ?string
  238.     {
  239.         return $this->note;
  240.     }
  241.     public function setNote(?string $note): self
  242.     {
  243.         $this->note $note;
  244.         return $this;
  245.     }
  246.     public function getBillingAddress(): ?BillingAddress
  247.     {
  248.         return $this->billingAddress;
  249.     }
  250.     public function setBillingAddress(BillingAddress $billingAddress): self
  251.     {
  252.         $this->billingAddress $billingAddress;
  253.         // set the owning side of the relation if necessary
  254.         if ($billingAddress->getCompany() !== $this) {
  255.             $billingAddress->setCompany($this);
  256.         }
  257.         return $this;
  258.     }
  259.     /**
  260.      * @return Collection|Contact[]
  261.      */
  262.     public function getContacts(): Collection
  263.     {
  264.         return $this->contacts;
  265.     }
  266.     public function addContact(Contact $contact): self
  267.     {
  268.         if (!$this->contacts->contains($contact)) {
  269.             $this->contacts[] = $contact;
  270.             $contact->setCompany($this);
  271.         }
  272.         return $this;
  273.     }
  274.     public function removeContact(Contact $contact): self
  275.     {
  276.         if ($this->contacts->contains($contact)) {
  277.             $this->contacts->removeElement($contact);
  278.             // set the owning side to null (unless already changed)
  279.             if ($contact->getCompany() === $this) {
  280.                 $contact->setCompany(null);
  281.             }
  282.         }
  283.         return $this;
  284.     }
  285.     public function getStatus(): ?Status
  286.     {
  287.         return $this->status;
  288.     }
  289.     public function setStatus(?Status $status): self
  290.     {
  291.         $this->status $status;
  292.         return $this;
  293.     }
  294.     public function getDateAdd(): ?\DateTimeInterface
  295.     {
  296.         return $this->date_add;
  297.     }
  298.     public function setDateAdd(?\DateTimeInterface $date_add): self
  299.     {
  300.         $this->date_add $date_add;
  301.         return $this;
  302.     }
  303.     public function getDateUpdate(): ?\DateTimeInterface
  304.     {
  305.         return $this->date_update;
  306.     }
  307.     public function setDateUpdate(?\DateTimeInterface $date_update): self
  308.     {
  309.         $this->date_update $date_update;
  310.         return $this;
  311.     }
  312.     /**
  313.      * @ORM\PrePersist
  314.      * @ORM\PreUpdate
  315.      */
  316.     public function updatedTimestamps()
  317.     {
  318.         $this->setDateUpdate(new \DateTime('now'));
  319.         if ($this->getDateAdd() === null) {
  320.             $this->setDateAdd(new \DateTime('now'));
  321.         }
  322.     }
  323.     public function getOwner(): ?User
  324.     {
  325.         return $this->owner;
  326.     }
  327.     public function setOwner(?User $owner): self
  328.     {
  329.         $this->owner $owner;
  330.         return $this;
  331.     }
  332.     public function getCommercial(): ?User
  333.     {
  334.         return $this->commercial;
  335.     }
  336.     public function setCommercial(?User $commercial): self
  337.     {
  338.         $this->commercial $commercial;
  339.         return $this;
  340.     }
  341.     public function getFiles()
  342.     {
  343.         if ((new Finder())->directories()->in("company_documents")->name($this->getId())->hasResults()) {
  344.             $finder = new Finder();
  345.             if ($finder->files()->in("company_documents/{$this->getId()}")->hasResults()) {
  346.                 foreach ($finder as $file) {
  347.                     $files[] = $file;
  348.                 }
  349.             }
  350.         }
  351.         return isset($files) ? $files : [];
  352.     }
  353.     /**
  354.      * @param string $timestamp
  355.      * @param string $inode
  356.      *
  357.      * @return mixed
  358.      */
  359.     public function getFile(string $timestampstring $inode)
  360.     {
  361.         $finder = new Finder();
  362.         $date date("Y-m-d H:i:s"$timestamp);
  363.         if ((new Finder())->directories()->in("company_documents")->name($this->getId())->hasResults()) {
  364.             if ($finder->files()->in("company_documents/{$this->getId()}")->date($date)->hasResults()) {
  365.                 foreach ($finder as $file) {
  366.                     if ($file->getInode() == $inode) {
  367.                         return $file;
  368.                     }
  369.                 }
  370.             }
  371.         }
  372.         return [];
  373.     }
  374.     /**
  375.      * @param string $type (kbis|mandat)
  376.      *
  377.      * @return bool
  378.      */
  379.     public function hasFile(string $type)
  380.     {
  381.         if ((new Finder())->directories()->in("company_documents")->name($this->getId())->hasResults()) {
  382.             $finder = new Finder();
  383.             if ($finder->files()->in("company_documents/{$this->getId()}")->hasResults()) {
  384.                 foreach ($finder as $file) {
  385.                     if (preg_match('/' $type '/'$file->getRelativePathName())) {
  386.                         return true;
  387.                     }
  388.                 }
  389.             }
  390.         }
  391.         return false;
  392.     }
  393.     /**
  394.      * @return string
  395.      */
  396.     public function generateCodeSage(BillingAddress $billingAddress): string
  397.     {
  398.         $code_commercial $this->getCommercial()->getCode();
  399.         $prefix 'L';
  400.         if (!empty($code_commercial)) {
  401.             switch (substr($code_commercial03)) {
  402.                 case '202':
  403.                 case '203':
  404.                     $prefix 'G';
  405.                     break;
  406.                 case '331':
  407.                 case '351':
  408.                 case '381':
  409.                     $prefix 'P';
  410.                     break;
  411.                 case preg_match('/^MA/'$code_commercial) ? $code_commercial : !$code_commercial:
  412.                     $prefix 'M';
  413.                     break;
  414.             }
  415.         }
  416.         if (soundex($this->getPriceType()) == soundex('REVENDEUR')) {
  417.             $prefix 'C';
  418.         }
  419.         if ($billingAddress != null) {
  420.             if (soundex($billingAddress->getCountry()) != soundex('FRANCE')) {
  421.                 $prefix 'E';
  422.             }
  423.             $name substr($this->getName(), 03);
  424.             $postcode $billingAddress->getPostcode();
  425.             return strtoupper(str_replace(' ''', ($prefix $name $postcode)));
  426.         }
  427.         return $prefix;
  428.     }
  429.     /**
  430.      * @return Collection|DeliveryAddress[]
  431.      */
  432.     public function getDeliveryAddresses(): Collection
  433.     {
  434.         return $this->deliveryAddresses;
  435.     }
  436.     public function addDeliveryAddress(DeliveryAddress $deliveryAddress): self
  437.     {
  438.         if (!$this->deliveryAddresses->contains($deliveryAddress)) {
  439.             $this->deliveryAddresses[] = $deliveryAddress;
  440.             $deliveryAddress->setCompany($this);
  441.         }
  442.         return $this;
  443.     }
  444.     public function removeDeliveryAddress(DeliveryAddress $deliveryAddress): self
  445.     {
  446.         if ($this->deliveryAddresses->contains($deliveryAddress)) {
  447.             $this->deliveryAddresses->removeElement($deliveryAddress);
  448.             // set the owning side to null (unless already changed)
  449.             if ($deliveryAddress->getCompany() === $this) {
  450.                 $deliveryAddress->setCompany(null);
  451.             }
  452.         }
  453.         return $this;
  454.     }
  455.     public function getJob(): ?Job
  456.     {
  457.         return $this->job;
  458.     }
  459.     public function setJob(?Job $job): self
  460.     {
  461.         $this->job $job;
  462.         return $this;
  463.     }
  464.     public function getKnownAs(): ?string
  465.     {
  466.         return $this->known_as;
  467.     }
  468.     public function setKnownAs(?string $known_as): self
  469.     {
  470.         $this->known_as $known_as;
  471.         return $this;
  472.     }
  473.     public function getExceptionPrice(): ?bool
  474.     {
  475.         return $this->exception_price;
  476.     }
  477.     public function setExceptionPrice(?bool $exception_price): self
  478.     {
  479.         $this->exception_price $exception_price;
  480.         return $this;
  481.     }
  482.     public function getDeparture(): ?string
  483.     {
  484.         return $this->departure;
  485.     }
  486.     public function setDeparture(?string $departure): self
  487.     {
  488.         $this->departure $departure;
  489.         return $this;
  490.     }
  491.     public function getRequiredFields()
  492.     {
  493.         return [
  494.             'getName',
  495.             'getEmail',
  496.             'getSiret',
  497.             'getTva',
  498.             'getJob',
  499.             'getKnownAs'
  500.         ];
  501.     }
  502.     public function getLocked(): ?bool
  503.     {
  504.         return $this->locked;
  505.     }
  506.     public function setLocked(?bool $locked): self
  507.     {
  508.         $this->locked $locked ?? false;
  509.         return $this;
  510.     }
  511.     public function getBillingFrequency(): ?string
  512.     {
  513.         return $this->billingFrequency;
  514.     }
  515.     public function setBillingFrequency(string $billingFrequency): self
  516.     {
  517.         $this->billingFrequency $billingFrequency;
  518.         return $this;
  519.     }
  520. }