src/Entity/Produit.php line 16

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