src/Entity/Product.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ProductRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Symfony\Component\HttpFoundation\File\File;
  11. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  12. /**
  13.  * @ORM\Entity(repositoryClass=ProductRepository::class)
  14.  * @ORM\HasLifecycleCallbacks()
  15.  * @UniqueEntity("code",message="Le code produit existe déjà.")
  16.  * @Vich\Uploadable
  17.  */
  18. class Product
  19. {
  20.     /**
  21.      * @ORM\Id
  22.      * @ORM\GeneratedValue
  23.      * @ORM\Column(type="integer")
  24.      */
  25.     private $id;
  26.     /**
  27.      * @ORM\Column(type="string", length=255, nullable=true)
  28.      * @Assert\NotBlank(message="Le code produit est obligatoire")
  29.      */
  30.     private $code;
  31.     /**
  32.      * @ORM\Column(type="string", length=255, nullable=true)
  33.      * @Assert\NotBlank(message="Le nom est obligatoire")
  34.      */
  35.     private $name;
  36.     /**
  37.      * @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
  38.      * @Assert\NotBlank(message="Le prix de vente est obligatoire")
  39.      */
  40.     private $sellHT;
  41.     /**
  42.      * @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
  43.      * @Assert\NotBlank(message="Le prix d'achat est obligatoire")
  44.      */
  45.     private $buyHT;
  46.     /**
  47.      * @ORM\Column(type="float", nullable=true)
  48.      */
  49.     private $stock;
  50.     /**
  51.      * @ORM\Column(type="datetime", nullable=true)
  52.      */
  53.     private $createdAt;
  54.     /**
  55.      * @ORM\Column(type="datetime", nullable=true)
  56.      */
  57.     private $updatedAt;
  58.     /**
  59.      * @ORM\OneToMany(targetEntity=PieceLine::class, mappedBy="product")
  60.      */
  61.     private $pieceLines;
  62.     private $codeName;
  63.     private $nomComplet;
  64.     /**
  65.      * @ORM\Column(type="string", length=255, nullable=true)
  66.      */
  67.     private $photo;
  68.     /**
  69.      * @var File
  70.      * @Vich\UploadableField(mapping="photos", fileNameProperty="photo")
  71.      */
  72.     private $photoFile;
  73.     /**
  74.      * @ORM\Column(type="string", length=255, nullable=true)
  75.      */
  76.     private $taillePoids;
  77.     /**
  78.      * @ORM\Column(type="string", length=255, nullable=true)
  79.      */
  80.     private $marque;
  81.     /**
  82.      * @ORM\Column(type="date", nullable=true)
  83.      */
  84.     private $dateExpiration;
  85.     /**
  86.      * @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
  87.      */
  88.     private $marge;
  89.     /**
  90.      * @ORM\OneToMany(targetEntity=Lot::class, mappedBy="product", orphanRemoval=true, cascade={"persist"})
  91.      */
  92.     private $lots;
  93.     /**
  94.      * @ORM\Column(type="string", length=255, nullable=true)
  95.      */
  96.     private $barecode;
  97.     /**
  98.      * @ORM\PrePersist
  99.      */
  100.     public function createDate()
  101.     {
  102.         $this->marge floatval($this->sellHT) - floatval($this->buyHT);
  103.         $this->createdAt = new \DateTime();
  104.         $this->updatedAt = new \DateTime();
  105.     }
  106.     /**
  107.      * @ORM\PreUpdate
  108.      */
  109.     public function updateDate()
  110.     {
  111.         $this->marge $this->sellHT $this->getBuyHT();
  112.         $this->updatedAt = new \DateTime();
  113.     }
  114.     public function __construct()
  115.     {
  116.         $this->pieceLines = new ArrayCollection();
  117.         $this->lots = new ArrayCollection();
  118.     }
  119.     public function getId(): ?int
  120.     {
  121.         return $this->id;
  122.     }
  123.     public function getCode(): ?string
  124.     {
  125.         return $this->code;
  126.     }
  127.     public function setCode(?string $code): self
  128.     {
  129.         $this->code $code;
  130.         return $this;
  131.     }
  132.     public function getName(): ?string
  133.     {
  134.         return $this->name;
  135.     }
  136.     public function setName(?string $name): self
  137.     {
  138.         $this->name $name;
  139.         return $this;
  140.     }
  141.     public function getSellHT(): ?string
  142.     {
  143.         return $this->sellHT;
  144.     }
  145.     public function setSellHT(?string $sellHT): self
  146.     {
  147.         $this->sellHT $sellHT;
  148.         return $this;
  149.     }
  150.     public function getBuyHT(): ?string
  151.     {
  152.         return $this->buyHT;
  153.     }
  154.     public function setBuyHT(?string $buyHT): self
  155.     {
  156.         $this->buyHT $buyHT;
  157.         return $this;
  158.     }
  159.     public function getStock(): ?float
  160.     {
  161.         return $this->stock;
  162.     }
  163.     public function setStock(?float $stock): self
  164.     {
  165.         $this->stock $stock;
  166.         return $this;
  167.     }
  168.     public function getCreatedAt(): ?\DateTimeInterface
  169.     {
  170.         return $this->createdAt;
  171.     }
  172.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  173.     {
  174.         $this->createdAt $createdAt;
  175.         return $this;
  176.     }
  177.     public function getUpdatedAt(): ?\DateTimeInterface
  178.     {
  179.         return $this->updatedAt;
  180.     }
  181.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  182.     {
  183.         $this->updatedAt $updatedAt;
  184.         return $this;
  185.     }
  186.     /**
  187.      * @return Collection|PieceLine[]
  188.      */
  189.     public function getPieceLines(): Collection
  190.     {
  191.         return $this->pieceLines;
  192.     }
  193.     public function addPieceLine(PieceLine $pieceLine): self
  194.     {
  195.         if (!$this->pieceLines->contains($pieceLine)) {
  196.             $this->pieceLines[] = $pieceLine;
  197.             $pieceLine->setProduct($this);
  198.         }
  199.         return $this;
  200.     }
  201.     public function removePieceLine(PieceLine $pieceLine): self
  202.     {
  203.         if ($this->pieceLines->contains($pieceLine)) {
  204.             $this->pieceLines->removeElement($pieceLine);
  205.             // set the owning side to null (unless already changed)
  206.             if ($pieceLine->getProduct() === $this) {
  207.                 $pieceLine->setProduct(null);
  208.             }
  209.         }
  210.         return $this;
  211.     }
  212.     public function getCodeName(): ?string
  213.     {
  214.         $nom '';
  215.         if ($this->barecode != '') {
  216.             $nom $this->barecode ' - ';
  217.         }
  218.         if ($this->marque != '') {
  219.             $nom .= $this->marque ' ';
  220.         }
  221.         $nom .= $this->code " - " $this->name " - " $this->getTaillePoids().' - (Stock : '.$this->getStockProduct().')';
  222.         return $nom;
  223.     }
  224.     public function getNomComplet(): ?string
  225.     {
  226.         $nom_complet $this->name;
  227.         if ($this->marque != '') {
  228.             $nom_complet .= ' ' $this->marque;
  229.         }
  230.         if ($this->taillePoids != '') {
  231.             $nom_complet .= ' ' $this->taillePoids;
  232.         }
  233.         return $nom_complet;
  234.     }
  235.     public function getPhoto(): ?string
  236.     {
  237.         return $this->photo;
  238.     }
  239.     public function setPhoto(?string $photo): self
  240.     {
  241.         $this->photo $photo;
  242.         return $this;
  243.     }
  244.     /**
  245.      * Set photoFile.
  246.      *
  247.      * @param string $photoFile
  248.      *
  249.      * @return PhotoFile
  250.      */
  251.     public function setPhotoFile(File $photoFile null)
  252.     {
  253.         $this->photoFile $photoFile;
  254.         if ($photoFile)
  255.             $this->updatedAt = new \DateTimeImmutable();
  256.         return $this;
  257.     }
  258.     /**
  259.      * Get photoFile.
  260.      *
  261.      * @return string
  262.      */
  263.     public function getPhotoFile()
  264.     {
  265.         return $this->photoFile;
  266.     }
  267.     public function getTaillePoids(): ?string
  268.     {
  269.         return $this->taillePoids;
  270.     }
  271.     public function setTaillePoids(?string $taillePoids): self
  272.     {
  273.         $this->taillePoids $taillePoids;
  274.         return $this;
  275.     }
  276.     public function getMarque(): ?string
  277.     {
  278.         return $this->marque;
  279.     }
  280.     public function setMarque(?string $marque): self
  281.     {
  282.         $this->marque $marque;
  283.         return $this;
  284.     }
  285.     public function getDateExpiration(): ?\DateTimeInterface
  286.     {
  287.         return $this->dateExpiration;
  288.     }
  289.     public function setDateExpiration(?\DateTimeInterface $dateExpiration): self
  290.     {
  291.         $this->dateExpiration $dateExpiration;
  292.         return $this;
  293.     }
  294.     public function getMarge(): ?string
  295.     {
  296.         return $this->marge;
  297.     }
  298.     public function setMarge(?string $marge): self
  299.     {
  300.         $this->marge $marge;
  301.         return $this;
  302.     }
  303.     /**
  304.      * @return Collection|Lot[]
  305.      */
  306.     public function getLots(): Collection
  307.     {
  308.         return $this->lots;
  309.     }
  310.     public function addLot(Lot $lot): self
  311.     {
  312.         if (!$this->lots->contains($lot)) {
  313.             $this->lots[] = $lot;
  314.             $lot->setProduct($this);
  315.         }
  316.         return $this;
  317.     }
  318.     public function removeLot(Lot $lot): self
  319.     {
  320.         if ($this->lots->contains($lot)) {
  321.             $this->lots->removeElement($lot);
  322.             // set the owning side to null (unless already changed)
  323.             if ($lot->getProduct() === $this) {
  324.                 $lot->setProduct(null);
  325.             }
  326.         }
  327.         return $this;
  328.     }
  329.     public function getStockProduct(): ?string
  330.     {
  331.         $stock_product 0;
  332.         foreach ($this->getLots() as $lot) {
  333.             $stock_product += $lot->getStock();
  334.         }
  335.         return $stock_product;
  336.     }
  337.     public function getBarecode(): ?string
  338.     {
  339.         return $this->barecode;
  340.     }
  341.     public function setBarecode(?string $barecode): self
  342.     {
  343.         $this->barecode $barecode;
  344.         return $this;
  345.     }
  346.     public function hasStock(){
  347.         foreach($this->getLots() as $lot){
  348.             if($lot->getStock() > 0){
  349.                 return true;
  350.             }
  351.         }
  352.         return false;
  353.     }
  354. }