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