src/Entity/Produit.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\DBAL\Types\Types;
  4. use App\Entity\ProduitPromotion;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use App\Repository\ProduitRepository;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use DateTime;
  10. #[ORM\Entity(repositoryClassProduitRepository::class)]
  11. class Produit
  12. {
  13.     #[ORM\Id]
  14.     #[ORM\GeneratedValue]
  15.     #[ORM\Column]
  16.     private ?int $id null;
  17.     #[ORM\Column(length255)]
  18.     private ?string $product_name null;
  19.     #[ORM\Column]
  20.     private ?int $qtt null;
  21.     #[ORM\Column(typeTypes::DECIMALprecision10scale'2')]
  22.     private ?string $prix null;
  23.     #[ORM\ManyToOne(inversedBy'produits')]
  24.     #[ORM\JoinColumn(nullablefalse)]
  25.     private ?Category $category null;
  26.     #[ORM\Column(nullabletrue)]
  27.     private ?bool $publishItem null;
  28.     #[ORM\ManyToOne(inversedBy'produits')]
  29.     #[ORM\JoinColumn(nullablefalse)]
  30.     private ?User $user null;
  31.     #[ORM\Column(nullabletrue)]
  32.     private ?bool $promo null;
  33.     #[ORM\OneToMany(mappedBy'produit'targetEntityCommandeDetails::class, orphanRemovaltrue)]
  34.     private Collection $commandeDetails;
  35.     #[ORM\Column(length300)]
  36.     private ?string $Description null;
  37.     #[ORM\OneToMany(mappedBy'produit'targetEntityImage::class,
  38.      orphanRemovaltrue,cascade:['persist'])]
  39.     private Collection $images;
  40.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  41.     private ?\DateTimeInterface $publish_date null;
  42.     #[ORM\OneToMany(mappedBy'produit'targetEntityProduitPromotion::class, orphanRemovaltrue)]
  43.     private Collection $promotions;
  44.     #[ORM\OneToMany(mappedBy'produit'targetEntityProduitVariant::class, orphanRemovaltrue,cascade:['persist'])]
  45.     private Collection $produitVariants;
  46.     #[ORM\Column(typeTypes::DECIMALprecision10scale2nullabletrue)]
  47.     private ?string $sale_price null;
  48.     #[ORM\Column(typeTypes::DECIMALprecision10scale2nullabletrue)]
  49.     private ?string $cost_price null;
  50.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  51.     private ?\DateTimeInterface $date_production null;
  52.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  53.     private ?\DateTimeInterface $date_expiration null;
  54.     #[ORM\ManyToOne(inversedBy'produits')]
  55.     #[ORM\JoinColumn(nullablefalse)]
  56.     private ?Magasin $magasin null;
  57.     // ── Up to 3 named variant dimensions (e.g. "Scent", "Size", "Color") ──
  58.     #[ORM\Column(length255nullabletrue)]
  59.     private ?string $variant_type null;
  60.     #[ORM\Column(length255nullabletrue)]
  61.     private ?string $variant_type2 null;
  62.     #[ORM\Column(length255nullabletrue)]
  63.     private ?string $variant_type3 null;
  64.     
  65.     public function __construct()
  66.     {
  67.         $this->commandeDetails = new ArrayCollection();
  68.         $this->images = new ArrayCollection();
  69.         $this->promotions = new ArrayCollection();
  70.         $this->produitVariants = new ArrayCollection();
  71.     }
  72.     public function getId(): ?int
  73.     {
  74.         return $this->id;
  75.     }
  76.     public function getProductName(): ?string
  77.     {
  78.         return $this->product_name;
  79.     }
  80.     
  81.     public function setProductName(string $product_name): self
  82.     {
  83.         $this->product_name $product_name;
  84.         
  85.         return $this;
  86.     }
  87.     public function getQtt(): ?int
  88.     {
  89.         return $this->qtt;
  90.     }
  91.     public function setQtt(?int $qtt): self
  92.     {
  93.         $this->qtt $qtt;
  94.         return $this;
  95.     }
  96.     // normale price without promotion 
  97.     public function getPrix(): ?string
  98.     {
  99.         return $this->prix;
  100.     }
  101.     public function setPrix(?string $prix): self
  102.     {
  103.         $this->prix $prix;
  104.         return $this;
  105.     }
  106.     public function getCategory(): ?Category
  107.     {
  108.         return $this->category;
  109.     }
  110.     public function setCategory(?Category $category): self
  111.     {
  112.         $this->category $category;
  113.         return $this;
  114.     }
  115.     public function getPublishItem(): ?bool
  116.     {
  117.         return $this->publishItem;
  118.     }
  119.     public function setPublishItem(?bool $publishItem): self
  120.     {
  121.         $this->publishItem $publishItem;
  122.         return $this;
  123.     }
  124.     public function getUser(): ?User
  125.     {
  126.         return $this->user;
  127.     }
  128.     public function setUser(?User $user): self
  129.     {
  130.         $this->user $user;
  131.         return $this;
  132.     }
  133.     public function isPromo(): ?bool
  134.     {
  135.         return $this->promo;
  136.     }
  137.     public function setPromo(?bool $promo): self
  138.     {
  139.         $this->promo $promo;
  140.         return $this;
  141.     }
  142.     /**
  143.      * @return Collection<int, CommandeDetails>
  144.      */
  145.     public function getCommandeDetails(): Collection
  146.     {
  147.         return $this->commandeDetails;
  148.     }
  149.     public function addCommandeDetail(CommandeDetails $commandeDetail): self
  150.     {
  151.         if (!$this->commandeDetails->contains($commandeDetail)) {
  152.             $this->commandeDetails->add($commandeDetail);
  153.             $commandeDetail->setProduit($this);
  154.         }
  155.         return $this;
  156.     }
  157.     public function removeCommandeDetail(CommandeDetails $commandeDetail): self
  158.     {
  159.         if ($this->commandeDetails->removeElement($commandeDetail)) {
  160.             // set the owning side to null (unless already changed)
  161.             if ($commandeDetail->getProduit() === $this) {
  162.                 $commandeDetail->setProduit(null);
  163.             }
  164.         }
  165.         return $this;
  166.     }
  167.     public function __toString(): string{
  168.         return $this->product_name;
  169.     }
  170.     public function getDescription(): ?string
  171.     {
  172.         return $this->Description;
  173.     }
  174.     public function setDescription(string $Description): self
  175.     {
  176.         $this->Description $Description;
  177.         return $this;
  178.     }
  179.     /**
  180.      * @return Collection<int, Image>
  181.      */
  182.     public function getImages(): Collection
  183.     {
  184.         return $this->images;
  185.     }
  186.     public function addImage(Image $image): self
  187.     {
  188.         if (!$this->images->contains($image)) {
  189.             $this->images->add($image);
  190.             $image->setProduit($this);
  191.         }
  192.         return $this;
  193.     }
  194.     public function removeImage(Image $image): self
  195.     {
  196.         if ($this->images->removeElement($image)) {
  197.             // set the owning side to null (unless already changed)
  198.             if ($image->getProduit() === $this) {
  199.                 $image->setProduit(null);
  200.             }
  201.         }
  202.         return $this;
  203.     }
  204.     public function getPublishDate(): ?\DateTimeInterface
  205.     {
  206.         return $this->publish_date;
  207.     }
  208.     public function setPublishDate(?\DateTimeInterface $publish_date): self
  209.     {
  210.         $this->publish_date $publish_date;
  211.         return $this;
  212.     }
  213.     /**
  214.      * @return Collection<int, ProduitPromotion>
  215.      */
  216.     public function getPromotions(): Collection
  217.     {
  218.         return $this->promotions;
  219.     }
  220.     
  221.     public function getCurrentPromoRate(): float
  222.     {
  223.         $promorate 0// Default rate if no promotion found
  224.         $promotions $this->promotions// Get the promotions collection
  225.         $dateins = new \DateTime();
  226.         try {
  227.             // Check if there are promotions
  228.             if ($promotions->count() > 0) {
  229.                 // Filtrer les promotions en fonction des dates de début et de fin
  230.                 $filteredPromotions $promotions->filter(function ($promotion) use ($dateins) {
  231.                     $debutPromo $promotion->getDateDebutPromo();
  232.                     $finPromo $promotion->getDateFinPromo();
  233.                     // Make sure both dates are valid DateTime objects
  234.                     if ($debutPromo instanceof DateTime && $finPromo instanceof DateTime) {
  235.                         return $debutPromo <= $dateins && $finPromo $dateins;
  236.                     }
  237.                     // Skip promotion if dates are invalid
  238.                     return false;
  239.                 });
  240.                 $firstPromotion $filteredPromotions->first();
  241.                 // If there is at least one active promotion
  242.                 if ($firstPromotion) {
  243.                     // Get the rate of the first active promotion
  244.                     $promo $firstPromotion->getPromotion();
  245.                     //  Vérifiez si la première promotion existe et si la méthode `getRate()` est accessible
  246.                     if ($promo && method_exists($promo'getRate')) {
  247.                         $promorate = (float) $promo->getRate();
  248.                     } else {
  249.                         $promorate 0// Ensure the rate is set to 0 in case of error
  250.                     }
  251.                 }
  252.             }
  253.         } catch (\Exception $e) {
  254.             // Catch any exception and log or handle it
  255.             error_log('Error fetching promotion: ' $e->getMessage());
  256.             $promorate 0// Ensure the rate is set to 0 in case of error
  257.         }
  258.             // Return the promotion rate (0 if no promotion is active)
  259.             return $promorate;
  260.             
  261.     }
  262.      
  263.     public function addPromotion(ProduitPromotion $promotion): self
  264.     {
  265.         if (!$this->promotions->contains($promotion)) {
  266.             $this->promotions->add($promotion);
  267.             $promotion->setProduit($this);
  268.         }
  269.         return $this;
  270.     }
  271.     public function removePromotion(ProduitPromotion $promotion): self
  272.     {
  273.         if ($this->promotions->removeElement($promotion)) {
  274.             // set the owning side to null (unless already changed)
  275.             if ($promotion->getProduit() === $this) {
  276.                 $promotion->setProduit(null);
  277.             }
  278.         }
  279.         return $this;
  280.     }
  281.     /**
  282.      * @return Collection<int, ProduitVariant>
  283.      */
  284.     public function getProduitVariants(): Collection
  285.     {
  286.         return $this->produitVariants;
  287.     }
  288.     public function addProduitVariant(ProduitVariant $produitVariant): static
  289.     {
  290.         if (!$this->produitVariants->contains($produitVariant)) {
  291.             $this->produitVariants->add($produitVariant);
  292.             $produitVariant->setProduit($this);
  293.         }
  294.         return $this;
  295.     }
  296.     public function removeProduitVariant(ProduitVariant $produitVariant): static
  297.     {
  298.         if ($this->produitVariants->removeElement($produitVariant)) {
  299.             // set the owning side to null (unless already changed)
  300.             if ($produitVariant->getProduit() === $this) {
  301.                 $produitVariant->setProduit(null);
  302.             }
  303.         }
  304.         return $this;
  305.     }
  306. // after promo price 
  307.     public function getSalePrice(): ?string
  308.     {
  309.         $rate $this->getCurrentPromoRate();
  310.         if ($rate 0){
  311.             //calculer le prix 
  312.             $discount $this->prix $rate/100 ;
  313.             $this->sale_price $this->prix $discount ;
  314.         
  315.         }
  316.         else{
  317.             $this->sale_price =null;
  318.         }
  319.         return $this->sale_price ?? $this->getPrix();
  320.     }
  321.     public function setSalePrice(string $sale_price): static
  322.     {
  323.         $this->sale_price $sale_price;
  324.         return $this;
  325.     }
  326.     public function getCostPrice(): ?string
  327.     {
  328.         return $this->cost_price;
  329.     }
  330.     public function setCostPrice(?string $cost_price): static
  331.     {
  332.         $this->cost_price $cost_price;
  333.         return $this;
  334.     }
  335.     public function getDateProduction(): ?\DateTimeInterface
  336.     {
  337.         return $this->date_production;
  338.     }
  339.     public function setDateProduction(?\DateTimeInterface $date_production): static
  340.     {
  341.         $this->date_production $date_production;
  342.         return $this;
  343.     }
  344.     public function getDateExpiration(): ?\DateTimeInterface
  345.     {
  346.         return $this->date_expiration;
  347.     }
  348.     public function setDateExpiration(?\DateTimeInterface $date_expiration): static
  349.     {
  350.         $this->date_expiration $date_expiration;
  351.         return $this;
  352.     }
  353.     public function getMagasin(): ?Magasin
  354.     {
  355.         return $this->magasin;
  356.     }
  357.     public function setMagasin(?Magasin $magasin): static
  358.     {
  359.         $this->magasin $magasin;
  360.         return $this;
  361.     }
  362.     public function getVariantType(): ?string
  363.     {
  364.         return $this->variant_type;
  365.     }
  366.     public function setVariantType(?string $variant_type): self
  367.     {
  368.         $this->variant_type $variant_type;
  369.         return $this;
  370.     }
  371.     public function getVariantType2(): ?string
  372.     {
  373.         return $this->variant_type2;
  374.     }
  375.     public function setVariantType2(?string $variant_type2): self
  376.     {
  377.         $this->variant_type2 $variant_type2;
  378.         return $this;
  379.     }
  380.     public function getVariantType3(): ?string
  381.     {
  382.         return $this->variant_type3;
  383.     }
  384.     public function setVariantType3(?string $variant_type3): self
  385.     {
  386.         $this->variant_type3 $variant_type3;
  387.         return $this;
  388.     }
  389. }