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=100, nullable=true)
  27.      */
  28.     private $barcode;
  29.     /**
  30.      * @ORM\Column(type="string", length=255)
  31.      */
  32.     private $name;
  33.     /**
  34.      * @ORM\Column(type="text", nullable=true)
  35.      */
  36.     private $description;
  37.      /**
  38.      *  @ORM\Column(name="buying_price_ht", type="decimal", precision=12, scale=3, options={"comment":"Prix d'achat HT (référence marge)"})
  39.      */
  40.     private string $buyingPriceHt '0.000';
  41.     /**
  42.      * @ORM\Column(name="buying_price_ttc", type="decimal", precision=12, scale=3, options={"comment":"Prix d'achat TTC (héritage)"})
  43.      */
  44.     private string $buyingPriceTtc '0.000';
  45.     /**
  46.      * @ORM\Column(type="float", nullable=true)
  47.      */
  48.     private $price_ht;
  49.     /**
  50.      * @ORM\Column(type="datetime")
  51.      */
  52.     private $createdAt;
  53.     /**
  54.      * @ORM\Column(type="integer", options={"default": 3})
  55.      */
  56.     private $alertStockMin 3;
  57.     /**
  58.      * @ORM\ManyToOne(targetEntity=Produit::class, inversedBy="produitDeclinationValues")
  59.      * @ORM\JoinColumn(nullable=false)
  60.      */
  61.     private $produit;
  62.     /**
  63.      * @ORM\ManyToMany(targetEntity=File::class, cascade={"persist"})
  64.      */
  65.     private $picture;
  66.     /**
  67.      * @ORM\OneToMany(targetEntity=DocumentDeclinationProduit::class, mappedBy="produitDeclinationValue")
  68.      */
  69.     private $documentDeclinationProduits;
  70.     /**
  71.      * @ORM\OneToMany(targetEntity=Stock::class, mappedBy="declinationProduit")
  72.      */
  73.     private $stocks;
  74.     /**
  75.      * @ORM\Column(type="integer", nullable=true)
  76.      */
  77.     private $uuid;
  78.     /**
  79.      * @ORM\OneToMany(targetEntity=Activity::class, mappedBy="produitDeclination")
  80.      */
  81.     private $activities;
  82.     /**
  83.      * @ORM\ManyToMany(targetEntity=Pack::class, mappedBy="declinations")
  84.      */
  85.     private $packs;
  86.     private $image;
  87.     /**
  88.      * @ORM\OneToMany(targetEntity=GroupDeclinationValue::class, mappedBy="produitDeclination")
  89.      */
  90.     private $groupDeclinationValues;
  91.     public function __construct(ContainerBagInterface $appUrl=NULL)
  92.     {
  93.         $this->groupDeclinationValues = new ArrayCollection();
  94.         $this->picture = new ArrayCollection();
  95.         $this->documentDeclinationProduits = new ArrayCollection();
  96.         $this->stocks = new ArrayCollection();
  97.         $this->activities = new ArrayCollection();
  98.         $this->appUrl $appUrl;
  99.         $this->packs = new ArrayCollection();
  100.     }
  101.     public function getId(): ?int
  102.     {
  103.         return $this->id;
  104.     }
  105.     public function getReference(): ?string
  106.     {
  107.         return $this->reference;
  108.     }
  109.     public function setReference(string $reference): self
  110.     {
  111.         $this->reference $reference;
  112.         return $this;
  113.     }
  114.     public function getBarcode(): ?string
  115.     {
  116.         return $this->barcode;
  117.     }
  118.     public function setBarcode(?string $barcode): self
  119.     {
  120.         $this->barcode $barcode;
  121.         return $this;
  122.     }
  123.     public function getName(): ?string
  124.     {
  125.         return $this->name;
  126.     }
  127.     public function setName(string $name): self
  128.     {
  129.         $this->name $name;
  130.         return $this;
  131.     }
  132.     public function getDescription(): ?string
  133.     {
  134.         return $this->description;
  135.     }
  136.     public function setDescription(?string $description): self
  137.     {
  138.         $this->description $description;
  139.         return $this;
  140.     }
  141.     public function getBuyingPriceHt(): ?float
  142.     {
  143.         return $this->buyingPriceHt;
  144.     }
  145.     public function setBuyingPriceHt(?float $buyingPriceHt): self
  146.     {
  147.         $this->buyingPriceHt $buyingPriceHt;
  148.         return $this;
  149.     }
  150.     public function getBuyingPriceTtc(): ?float
  151.     {
  152.         return $this->buyingPriceTtc;
  153.     }
  154.     public function setBuyingPriceTtc(?float $buyingPriceTtc): self
  155.     {
  156.         $this->buyingPriceTtc $buyingPriceTtc;
  157.         return $this;
  158.     }
  159.     public function getPriceHt(): ?float
  160.     {
  161.         return $this->price_ht;
  162.     }
  163.     public function setPriceHt(?float $price_ht): self
  164.     {
  165.         $this->price_ht $price_ht;
  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 getAlertStockMin(): ?int
  178.     {
  179.         return $this->alertStockMin;
  180.     }
  181.     public function setAlertStockMin(int $alertStockMin): self
  182.     {
  183.         $this->alertStockMin max(0$alertStockMin);
  184.         return $this;
  185.     }
  186.     public function getProduit(): ?Produit
  187.     {
  188.         return $this->produit;
  189.     }
  190.     public function setProduit(?Produit $produit): self
  191.     {
  192.         $this->produit $produit;
  193.         return $this;
  194.     }
  195.     /**
  196.      * @return Collection|GroupDeclinationValue[]
  197.      */
  198.     public function getGroupDeclinationValues(): Collection
  199.     {
  200.         return $this->groupDeclinationValues;
  201.     }
  202.     public function addGroupDeclinationValue(GroupDeclinationValue $groupDeclinationValue): self
  203.     {
  204.         if( !$this->groupDeclinationValues->contains($groupDeclinationValue)) {
  205.             $this->groupDeclinationValues[] = $groupDeclinationValue;
  206.             $groupDeclinationValue->setProduitDeclination($this);
  207.         }
  208.         return $this;
  209.     }
  210.     public function removeGroupDeclinationValue(GroupDeclinationValue $groupDeclinationValue): self
  211.     {
  212.         if( $this->groupDeclinationValues->removeElement($groupDeclinationValue)) {
  213.             // set the owning side to null (unless already changed)
  214.             if( $groupDeclinationValue->getProduitDeclination() === $this) {
  215.                 $groupDeclinationValue->setProduitDeclination(null);
  216.             }
  217.         }
  218.         return $this;
  219.     }
  220.     /**
  221.      * @return Collection|File[]
  222.      */
  223.     public function getPicture(): Collection
  224.     {
  225.         return $this->picture;
  226.     }
  227.     public function addPicture(File $picture): self
  228.     {
  229.         if( !$this->picture->contains($picture)) {
  230.             $this->picture[] = $picture;
  231.         }
  232.         return $this;
  233.     }
  234.     public function removePicture(File $picture): self
  235.     {
  236.         $this->picture->removeElement($picture);
  237.         return $this;
  238.     }
  239.     /**
  240.      * @return Collection|DocumentDeclinationProduit[]
  241.      */
  242.     public function getDocumentDeclinationProduits(): Collection
  243.     {
  244.         return $this->documentDeclinationProduits;
  245.     }
  246.     public function addDocumentDeclinationProduit(DocumentDeclinationProduit $documentDeclinationProduit): self
  247.     {
  248.         if( !$this->documentDeclinationProduits->contains($documentDeclinationProduit)) {
  249.             $this->documentDeclinationProduits[] = $documentDeclinationProduit;
  250.             $documentDeclinationProduit->setProduitDeclinationValue($this);
  251.         }
  252.         return $this;
  253.     }
  254.     public function removeDocumentDeclinationProduit(DocumentDeclinationProduit $documentDeclinationProduit): self
  255.     {
  256.         if( $this->documentDeclinationProduits->removeElement($documentDeclinationProduit)) {
  257.             // set the owning side to null (unless already changed)
  258.             if( $documentDeclinationProduit->getProduitDeclinationValue() === $this) {
  259.                 $documentDeclinationProduit->setProduitDeclinationValue(null);
  260.             }
  261.         }
  262.         return $this;
  263.     }
  264.     /**
  265.      * @return Collection|Stock[]
  266.      */
  267.     public function getStocks(): Collection
  268.     {
  269.         return $this->stocks;
  270.     }
  271.     public function addStock(Stock $stock): self
  272.     {
  273.         if( !$this->stocks->contains($stock)) {
  274.             $this->stocks[] = $stock;
  275.             $stock->setDeclinationProduit($this);
  276.         }
  277.         return $this;
  278.     }
  279.     public function removeStock(Stock $stock): self
  280.     {
  281.         if( $this->stocks->removeElement($stock)) {
  282.             // set the owning side to null (unless already changed)
  283.             if( $stock->getDeclinationProduit() === $this) {
  284.                 $stock->setDeclinationProduit(null);
  285.             }
  286.         }
  287.         return $this;
  288.     }
  289.     public function getUuid(): ?int
  290.     {
  291.         return $this->uuid;
  292.     }
  293.     public function setUuid(?int $uuid): self
  294.     {
  295.         $this->uuid $uuid;
  296.         return $this;
  297.     }
  298.     /**
  299.      * @return Collection|Activity[]
  300.      */
  301.     public function getActivities(): Collection
  302.     {
  303.         return $this->activities;
  304.     }
  305.     public function addActivity(Activity $activity): self
  306.     {
  307.         if( !$this->activities->contains($activity)) {
  308.             $this->activities[] = $activity;
  309.             $activity->setProduitDeclination($this);
  310.         }
  311.         return $this;
  312.     }
  313.     public function removeActivity(Activity $activity): self
  314.     {
  315.         if( $this->activities->removeElement($activity)) {
  316.             // set the owning side to null (unless already changed)
  317.             if( $activity->getProduitDeclination() === $this) {
  318.                 $activity->setProduitDeclination(null);
  319.             }
  320.         }
  321.         return $this;
  322.     }
  323.     public function getImage(): ?string
  324.     {
  325.         return $this->image;
  326.     }
  327.     public function setImage(string $image): self
  328.     {
  329.         $this->image $image;
  330.         return $this;
  331.     }
  332.     #[\ReturnTypeWillChange]
  333.     public function jsonSerialize()
  334.     {
  335.         $tabPictures=[];
  336.         foreach ($this->getPicture() as $picture) {
  337.             array_push($tabPictures$picture->getImageName());
  338.         }
  339.         foreach ($tabPictures as $key => $picture_name){
  340.             $tabPictures[$key] =(empty($tabPictures))?'no-image.jpg':$picture_name;
  341.             if(!empty($tabPictures) && file_exists($_ENV['APP_URL']."/public/images/".$picture_name) && !file_exists($_ENV['APP_URL']."/public/images/web/".$picture_name))
  342.                 $this->resizeImage($picture_name);
  343.             /*if(!empty($tabPictures) && !file_exists($_ENV['APP_URL']."/public/images/".$picture_name) && !file_exists($_ENV['APP_URL']."/public/images/web/".$picture_name)){
  344.                 $tabPictures[$key] = 'no-image.jpg';
  345.             }*/
  346.         }
  347.         $quantity 0;
  348.         foreach ($this->getStocks() as $stock)
  349.             $quantity $quantity + ($stock->getQtStock() - $stock->getQtReserved());
  350.         // Récpérer le couleur de la declinaison
  351.         $couleur '';
  352.         foreach ($this->getgroupDeclinationValues() as $dec)
  353.             if ($dec->getDeclination()->getId() == 2$couleur=$dec->getValue()->getName();
  354.         return array(
  355.             'id' => $this->getId(),
  356.             'idProduit' => $this->getProduit()->getId(),
  357.             'name' => $this->getProduit()->getName() .' '.$couleur,
  358.             //'name' => $this->getName().' - '.$this->getProduit()->getId(),
  359.             'picture' => $tabPictures,
  360.             'image' => $this->getImage(),
  361.             'quantity' => $quantity,
  362.             'priceHT' => $this->getPriceHt(),
  363.             'priceTTC' => $this->getProduit()->getPriceTtc(),
  364.             'groupDeclinationValues' => $this->getgroupDeclinationValues()->toArray(),
  365.             'promo' => $this->getProduit()->getPromotion(),
  366.             'isNew' => $this->getProduit()->isNew(),
  367.             'stock' => $this->getQtyAvailableByColor(),
  368.         );
  369.     }
  370.     public function addPack(Pack $pack)
  371.     {
  372.         if (!$this->packs->contains($pack)) {
  373.             $this->packs[] = $pack;
  374.         }
  375.     }
  376.     public function removePack(Pack $pack)
  377.     {
  378.         $this->packs->removeElement($pack);
  379.     }
  380.     public function getPacks(): Collection
  381.     {
  382.         return $this->packs;
  383.     }
  384.     public function getQtyReserved()
  385.     {
  386.         $qtyReserved=0;
  387.         foreach($this->stocks as $stock){
  388.             $qtyReserved+=$stock->getQtReserved();
  389.         }
  390.         return $qtyReserved;
  391.     }
  392.     public function getQtyAvailable()
  393.     {
  394.         $qty=0;
  395.         foreach($this->stocks as $stock){
  396.             $qty+=$stock->getQtStock()-$stock->getQtReserved();
  397.         }
  398.         return $qty;
  399.     }
  400.     public function getQtyIncoming(): int
  401.     {
  402.         $qty 0;
  403.         foreach ($this->stocks as $stock) {
  404.             $qty += (int) $stock->getIncomingQuantity();
  405.         }
  406.         return $qty;
  407.     }
  408.     public function getQtyIncomingRemaining(): int
  409.     {
  410.         $qty 0;
  411.         foreach ($this->stocks as $stock) {
  412.             $qty += (int) $stock->getIncomingRemainingQuantity();
  413.         }
  414.         return $qty;
  415.     }
  416.     public function getIncomingDateEstimated(): ?\DateTimeInterface
  417.     {
  418.         $nearestDate null;
  419.         foreach ($this->stocks as $stock) {
  420.             $candidateDate $stock->getIncomingDateEstimated();
  421.             if (!$candidateDate) {
  422.                 continue;
  423.             }
  424.             if ($nearestDate === null || $candidateDate $nearestDate) {
  425.                 $nearestDate $candidateDate;
  426.             }
  427.         }
  428.         return $nearestDate;
  429.     }
  430.     public function getExchangeIncomingQuantity(): int
  431.     {
  432.         $qty 0;
  433.         foreach ($this->stocks as $stock) {
  434.             $qty += (int) $stock->getExchangeIncomingQuantity();
  435.         }
  436.         return $qty;
  437.     }
  438.     public function getQtyAvailableForWebsite(): int
  439.     {
  440.         $qty 0;
  441.         foreach ($this->stocks as $stock) {
  442.             $qty += (int) $stock->getAvailableQuantityForWebsite();
  443.         }
  444.         return $qty;
  445.     }
  446.     public function getQtyAvailableByColor()
  447.     {
  448.         $valDec null;
  449.         foreach ($this->getgroupDeclinationValues() as $dec)
  450.             if ($dec->getDeclination()->getId() == 2$valDec $dec->getValue();
  451.         $qty=0;
  452.         // On cherche toutes les decliaison de meme couleur et calcule le stock
  453.         foreach ($this->getProduit()->getProduitDeclinationValues() as $dec){
  454.             foreach ($dec->getgroupDeclinationValues() as $groupDec)
  455.                 if ($groupDec->getDeclination()->getId() == 2  && $groupDec->getValue() === $valDec$qty += $dec->getQtyAvailableForWebsite();
  456.         }
  457.         return $qty;
  458.     }
  459. }