src/Entity/ProduitDeclinationValue.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ProduitDeclinationValueRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use JsonSerializable;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  9. /**
  10.  * @ORM\Entity(repositoryClass=ProduitDeclinationValueRepository::class)
  11.  */
  12. class ProduitDeclinationValue implements JsonSerializable
  13. {
  14.     private $appUrl;
  15.     /**
  16.      * @ORM\Id
  17.      * @ORM\GeneratedValue
  18.      * @ORM\Column(type="integer")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @ORM\Column(type="string", length=255)
  23.      */
  24.     private $reference;
  25.     /**
  26.      * @ORM\Column(type="string", length=255)
  27.      */
  28.     private $name;
  29.     /**
  30.      * @ORM\Column(type="text", nullable=true)
  31.      */
  32.     private $description;
  33.      /**
  34.      *  @ORM\Column(name="buying_price_ht", type="decimal", precision=12, scale=3, options={"comment":"Prix d'achat HT (référence marge)"})
  35.      */
  36.     private string $buyingPriceHt '0.000';
  37.     /**
  38.      * @ORM\Column(name="buying_price_ttc", type="decimal", precision=12, scale=3, options={"comment":"Prix d'achat TTC (héritage)"})
  39.      */
  40.     private string $buyingPriceTtc '0.000';   
  41.     /**
  42.      * @ORM\Column(type="float", nullable=true)
  43.      */
  44.     private $price_ht;
  45.     /**
  46.      * @ORM\Column(type="datetime")
  47.      */
  48.     private $createdAt;
  49.     /**
  50.      * @ORM\ManyToOne(targetEntity=Produit::class, inversedBy="produitDeclinationValues")
  51.      * @ORM\JoinColumn(nullable=false)
  52.      */
  53.     private $produit;
  54.     
  55.     /**
  56.      * @ORM\ManyToMany(targetEntity=File::class, cascade={"persist"})
  57.      */
  58.     private $picture;
  59.     /**
  60.      * @ORM\OneToMany(targetEntity=DocumentDeclinationProduit::class, mappedBy="produitDeclinationValue")
  61.      */
  62.     private $documentDeclinationProduits;
  63.     /**
  64.      * @ORM\OneToMany(targetEntity=Stock::class, mappedBy="declinationProduit")
  65.      */
  66.     private $stocks;
  67.     /**
  68.      * @ORM\Column(type="integer", nullable=true)
  69.      */
  70.     private $uuid;
  71.     /**
  72.      * @ORM\OneToMany(targetEntity=Activity::class, mappedBy="produitDeclination")
  73.      */
  74.     private $activities;
  75.     /**
  76.      * @ORM\ManyToMany(targetEntity=Pack::class, mappedBy="declinations")
  77.      */
  78.     private $packs;
  79.     private $image;
  80.     
  81.     /**
  82.      * @ORM\OneToMany(targetEntity=GroupDeclinationValue::class, mappedBy="produitDeclination")
  83.      */
  84.     private $groupDeclinationValues;
  85.     public function __construct(ContainerBagInterface $appUrl=NULL)
  86.     {
  87.         $this->groupDeclinationValues = new ArrayCollection();
  88.         $this->picture = new ArrayCollection();
  89.         $this->documentDeclinationProduits = new ArrayCollection();
  90.         $this->stocks = new ArrayCollection();
  91.         $this->activities = new ArrayCollection();
  92.         $this->appUrl $appUrl;
  93.         $this->packs = new ArrayCollection();       
  94.     }
  95.     public function getId(): ?int
  96.     {
  97.         return $this->id;
  98.     }
  99.     public function getReference(): ?string
  100.     {
  101.         return $this->reference;
  102.     }
  103.     public function setReference(string $reference): self
  104.     {
  105.         $this->reference $reference;
  106.         return $this;
  107.     }
  108.     public function getName(): ?string
  109.     {
  110.         return $this->name;
  111.     }
  112.     public function setName(string $name): self
  113.     {
  114.         $this->name $name;
  115.         return $this;
  116.     }
  117.     public function getDescription(): ?string
  118.     {
  119.         return $this->description;
  120.     }
  121.     public function setDescription(?string $description): self
  122.     {
  123.         $this->description $description;
  124.         return $this;
  125.     }
  126.     public function getBuyingPriceHt(): ?float
  127.     {
  128.         return $this->buyingPriceHt;
  129.     }
  130.     public function setBuyingPriceHt(?float $buyingPriceHt): self
  131.     {
  132.         $this->buyingPriceHt $buyingPriceHt;
  133.         return $this;
  134.     }
  135.     public function getBuyingPriceTtc(): ?float
  136.     {
  137.         return $this->buyingPriceTtc;
  138.     }
  139.     public function setBuyingPriceTtc(?float $buyingPriceTtc): self
  140.     {
  141.         $this->buyingPriceTtc $buyingPriceTtc;
  142.         return $this;
  143.     }
  144.     public function getPriceHt(): ?float
  145.     {
  146.         return $this->price_ht;
  147.     }
  148.     public function setPriceHt(?float $price_ht): self
  149.     {
  150.         $this->price_ht $price_ht;
  151.         return $this;
  152.     }
  153.     public function getCreatedAt(): ?\DateTimeInterface
  154.     {
  155.         return $this->createdAt;
  156.     }
  157.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  158.     {
  159.         $this->createdAt $createdAt;
  160.         return $this;
  161.     }
  162.     public function getProduit(): ?Produit
  163.     {
  164.         return $this->produit;
  165.     }
  166.     public function setProduit(?Produit $produit): self
  167.     {
  168.         $this->produit $produit;
  169.         return $this;
  170.     }
  171.    
  172.     /**
  173.      * @return Collection|GroupDeclinationValue[]
  174.      */
  175.     public function getGroupDeclinationValues(): Collection
  176.     {
  177.         return $this->groupDeclinationValues;
  178.     }
  179.     public function addGroupDeclinationValue(GroupDeclinationValue $groupDeclinationValue): self
  180.     {
  181.         if( !$this->groupDeclinationValues->contains($groupDeclinationValue)) {
  182.             $this->groupDeclinationValues[] = $groupDeclinationValue;
  183.             $groupDeclinationValue->setProduitDeclination($this);
  184.         }
  185.         return $this;
  186.     }
  187.     public function removeGroupDeclinationValue(GroupDeclinationValue $groupDeclinationValue): self
  188.     {
  189.         if( $this->groupDeclinationValues->removeElement($groupDeclinationValue)) {
  190.             // set the owning side to null (unless already changed)
  191.             if( $groupDeclinationValue->getProduitDeclination() === $this) {
  192.                 $groupDeclinationValue->setProduitDeclination(null);
  193.             }
  194.         }
  195.         return $this;
  196.     }
  197.     /**
  198.      * @return Collection|File[]
  199.      */
  200.     public function getPicture(): Collection
  201.     {
  202.         return $this->picture;
  203.     }
  204.     public function addPicture(File $picture): self
  205.     {
  206.         if( !$this->picture->contains($picture)) {
  207.             $this->picture[] = $picture;
  208.         }
  209.         return $this;
  210.     }
  211.     public function removePicture(File $picture): self
  212.     {
  213.         $this->picture->removeElement($picture);
  214.         return $this;
  215.     }
  216.     /**
  217.      * @return Collection|DocumentDeclinationProduit[]
  218.      */
  219.     public function getDocumentDeclinationProduits(): Collection
  220.     {
  221.         return $this->documentDeclinationProduits;
  222.     }
  223.     public function addDocumentDeclinationProduit(DocumentDeclinationProduit $documentDeclinationProduit): self
  224.     {
  225.         if( !$this->documentDeclinationProduits->contains($documentDeclinationProduit)) {
  226.             $this->documentDeclinationProduits[] = $documentDeclinationProduit;
  227.             $documentDeclinationProduit->setProduitDeclinationValue($this);
  228.         }
  229.         return $this;
  230.     }
  231.     public function removeDocumentDeclinationProduit(DocumentDeclinationProduit $documentDeclinationProduit): self
  232.     {
  233.         if( $this->documentDeclinationProduits->removeElement($documentDeclinationProduit)) {
  234.             // set the owning side to null (unless already changed)
  235.             if( $documentDeclinationProduit->getProduitDeclinationValue() === $this) {
  236.                 $documentDeclinationProduit->setProduitDeclinationValue(null);
  237.             }
  238.         }
  239.         return $this;
  240.     }
  241.     /**
  242.      * @return Collection|Stock[]
  243.      */
  244.     public function getStocks(): Collection
  245.     {
  246.         return $this->stocks;
  247.     }
  248.     public function addStock(Stock $stock): self
  249.     {
  250.         if( !$this->stocks->contains($stock)) {
  251.             $this->stocks[] = $stock;
  252.             $stock->setDeclinationProduit($this);
  253.         }
  254.         return $this;
  255.     }
  256.     public function removeStock(Stock $stock): self
  257.     {
  258.         if( $this->stocks->removeElement($stock)) {
  259.             // set the owning side to null (unless already changed)
  260.             if( $stock->getDeclinationProduit() === $this) {
  261.                 $stock->setDeclinationProduit(null);
  262.             }
  263.         }
  264.         return $this;
  265.     }
  266.     public function getUuid(): ?int
  267.     {
  268.         return $this->uuid;
  269.     }
  270.     public function setUuid(?int $uuid): self
  271.     {
  272.         $this->uuid $uuid;
  273.         return $this;
  274.     }
  275.     /**
  276.      * @return Collection|Activity[]
  277.      */
  278.     public function getActivities(): Collection
  279.     {
  280.         return $this->activities;
  281.     }
  282.     public function addActivity(Activity $activity): self
  283.     {
  284.         if( !$this->activities->contains($activity)) {
  285.             $this->activities[] = $activity;
  286.             $activity->setProduitDeclination($this);
  287.         }
  288.         return $this;
  289.     }
  290.     public function removeActivity(Activity $activity): self
  291.     {
  292.         if( $this->activities->removeElement($activity)) {
  293.             // set the owning side to null (unless already changed)
  294.             if( $activity->getProduitDeclination() === $this) {
  295.                 $activity->setProduitDeclination(null);
  296.             }
  297.         }
  298.         return $this;
  299.     }
  300.     public function getImage(): ?string
  301.     {
  302.         return $this->image;
  303.     }
  304.     public function setImage(string $image): self
  305.     {
  306.         $this->image $image;
  307.         return $this;
  308.     }
  309.     public function jsonSerialize()
  310.     {
  311.         $tabPictures=[];
  312.         foreach ($this->getPicture() as $picture) {
  313.             array_push($tabPictures$picture->getImageName());
  314.         }
  315.         foreach ($tabPictures as $key => $picture_name){
  316.             $tabPictures[$key] =(empty($tabPictures))?'no-image.png':$picture_name;
  317.             if(!empty($tabPictures) && file_exists($_ENV['APP_URL']."/public/images/".$picture_name) && !file_exists($_ENV['APP_URL']."/public/images/thumbs/".$picture_name))
  318.                 $this->resizeImage($picture_name);
  319.             /*if(!empty($tabPictures) && !file_exists($_ENV['APP_URL']."/public/images/".$picture_name) && !file_exists($_ENV['APP_URL']."/public/images/thumbs/".$picture_name)){
  320.                 $tabPictures[$key] = 'no-image.png';
  321.             }*/
  322.         }
  323.         $quantity 0;
  324.         foreach ($this->getStocks() as $stock)
  325.             $quantity $quantity + ($stock->getQtStock() - $stock->getQtReserved());
  326.         // Récpérer le couleur de la declinaison
  327.         $couleur '';
  328.         foreach ($this->getgroupDeclinationValues() as $dec)
  329.             if ($dec->getDeclination()->getId() == 2$couleur=$dec->getValue()->getName();
  330.         return array(
  331.             'id' => $this->getId(),
  332.             'idProduit' => $this->getProduit()->getId(),
  333.             'name' => $this->getProduit()->getName() .' '.$couleur,
  334.             //'name' => $this->getName().' - '.$this->getProduit()->getId(),
  335.             'picture' => $tabPictures,
  336.             'image' => $this->getImage(),
  337.             'quantity' => $quantity,
  338.             'priceHT' => $this->getPriceHt(),
  339.             'priceTTC' => $this->getProduit()->getPriceTtc(),
  340.             'groupDeclinationValues' => $this->getgroupDeclinationValues()->toArray(),
  341.             'promo' => $this->getProduit()->getPromotion(),
  342.             'isNew' => $this->getProduit()->isNew(),
  343.             'stock' => $this->getQtyAvailableByColor(),
  344.         );
  345.     }
  346.     public function addPack(Pack $pack)
  347.     {
  348.         if (!$this->packs->contains($pack)) {
  349.             $this->packs[] = $pack;
  350.         }
  351.     }
  352.     public function removePack(Pack $pack)
  353.     {
  354.         $this->packs->removeElement($pack);
  355.     }
  356.     public function getPacks(): Collection
  357.     {
  358.         return $this->packs;
  359.     }
  360.     public function getQtyReserved()
  361.     {
  362.         $qtyReserved=0;
  363.         foreach($this->stocks as $stock){
  364.             $qtyReserved+=$stock->getQtReserved();
  365.         }
  366.         return $qtyReserved;
  367.     }
  368.     public function getQtyAvailable()
  369.     {
  370.         $qty=0;
  371.         foreach($this->stocks as $stock){
  372.             $qty+=$stock->getQtStock()-$stock->getQtReserved();
  373.         }
  374.         return $qty;
  375.     }
  376.     public function getQtyAvailableByColor()
  377.     {
  378.         $valDec null;
  379.         foreach ($this->getgroupDeclinationValues() as $dec)
  380.             if ($dec->getDeclination()->getId() == 2$valDec $dec->getValue();
  381.         $qty=0;
  382.         // On cherche toutes les decliaison de meme couleur et calcule le stock
  383.         foreach ($this->getProduit()->getProduitDeclinationValues() as $dec){
  384.             foreach ($dec->getgroupDeclinationValues() as $groupDec)
  385.                 if ($groupDec->getDeclination()->getId() == 2  && $groupDec->getValue() === $valDec$qty+=$dec->getQtyAvailable();
  386.         }
  387.         return $qty;
  388.     }
  389. }