src/Entity/Website/Product/Product.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Website\Product;
  3. use App\Entity\BaseEntity;
  4. use App\Entity\Website\Cart\CartItem;
  5. use App\Entity\Website\Order\Discount;
  6. use App\Entity\Website\Order\OrderItem;
  7. use App\Entity\Website\Website\Review;
  8. use App\Entity\Website\Website\Website;
  9. use App\Entity\SeoFieldsTrait;
  10. use App\Repository\Website\Product\ProductRepository;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\DBAL\Types\Types;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
  16. #[ORM\Entity(repositoryClassProductRepository::class)]
  17. class Product extends BaseEntity
  18. {
  19.     use SeoFieldsTrait;
  20.     #[ORM\Id]
  21.     #[ORM\Column(type'guid'uniquetrue)]
  22.     #[ORM\GeneratedValue(strategy'CUSTOM')]
  23.     #[ORM\CustomIdGenerator(class: UuidGenerator::class)]
  24.     private ?string $id;
  25.     #[ORM\Column(typeTypes::BIGINTnullabletrue)]
  26.     private ?string $stock null;
  27.     #[ORM\OneToMany(mappedBy'product'targetEntityProductPrice::class, cascade: ['persist'], orphanRemovaltrue)]
  28.     private Collection $productPrices;
  29.     #[ORM\OneToMany(mappedBy'product'targetEntityProductAttribute::class, cascade: ['persist'], orphanRemovaltrue)]
  30.     private Collection $productAttributes;
  31.     #[ORM\Column(length255)]
  32.     private ?string $name null;
  33.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  34.     private ?string $description null;
  35.     #[ORM\Column(typeTypes::BIGINT)]
  36.     private ?string $price null;
  37.     #[ORM\Column(length255nullabletrue)]
  38.     private ?string $imageUrl null;
  39.     #[ORM\ManyToOne(inversedBy'products')]
  40.     #[ORM\JoinColumn(nullablefalse)]
  41.     private ?Website $website null;
  42.     #[ORM\OneToMany(mappedBy'product'targetEntityDiscount::class)]
  43.     private Collection $discounts;
  44.     #[ORM\OneToMany(mappedBy'product'targetEntityProductImage::class, cascade: ['persist'], orphanRemovaltrue)]
  45.     private Collection $productImages;
  46.     #[ORM\OneToMany(mappedBy'product'targetEntityOrderItem::class)]
  47.     private Collection $orderItems;
  48.     #[ORM\OneToMany(mappedBy'product'targetEntityCartItem::class)]
  49.     private Collection $cartItems;
  50.     #[ORM\OneToMany(mappedBy'product'targetEntityReview::class)]
  51.     private Collection $reviews;
  52.     #[ORM\Column(length255nullabletrue)]
  53.     private ?string $subName null;
  54.     #[ORM\ManyToMany(targetEntityProductCategory::class, mappedBy'products'cascade: ['persist'])]
  55.     private Collection $productCategories;
  56.     #[ORM\ManyToMany(targetEntityProductTag::class, mappedBy'products'cascade: ['persist'])]
  57.     private Collection $productTags;
  58.     #[ORM\OneToMany(mappedBy'product'targetEntityProductReview::class)]
  59.     private Collection $productReviews;
  60.     #[ORM\Column(nullabletrue)]
  61.     private ?bool $isSelling null;
  62.     #[ORM\Column(length255nullabletrue)]
  63.     private ?string $publishStatus null;
  64.     #[ORM\Column(length255nullabletrue)]
  65.     private ?string $sku null;
  66.     public function calculatePrice($data)
  67.     {
  68.         var_dump($data);die;
  69.     }
  70.     public function __construct()
  71.     {
  72.         parent::__construct();
  73.         $this->setPrice(0);
  74.         $this->productAttributes = new ArrayCollection();
  75.         $this->discounts = new ArrayCollection();
  76.         $this->productImages = new ArrayCollection();
  77.         $this->orderItems = new ArrayCollection();
  78.         $this->cartItems = new ArrayCollection();
  79.         $this->reviews = new ArrayCollection();
  80.         $this->productPrices = new ArrayCollection();
  81.         $this->productCategories = new ArrayCollection();
  82.         $this->productTags = new ArrayCollection();
  83.         $this->productReviews = new ArrayCollection();
  84.     }
  85.     public function __toString()
  86.     {
  87.         return $this->getName();
  88.     }
  89.     public function getMinPrice(): ?int
  90.     {
  91.         if (!$this->isVariablePriced()) {
  92.             return $this->getPrice(); // قیمت ساده
  93.         }
  94.         $prices $this->getProductPrices()->map(fn($p) => $p->getPrice())->toArray();
  95.         return !empty($prices) ? min($prices) : null;
  96.     }
  97.     public function isVariablePriced(): bool
  98.     {
  99.         return !$this->productPrices->isEmpty();
  100.     }
  101.     public function getDisplayPrice(): ?int
  102.     {
  103.         return $this->isVariablePriced()
  104.             ? $this->getMinPrice()
  105.             : $this->getPrice();
  106.     }
  107.     public function getId(): ?string
  108.     {
  109.         return $this->id;
  110.     }
  111.     /**
  112.      * @return Collection<int, ProductAttribute>
  113.      */
  114.     public function getProductAttributes(): Collection
  115.     {
  116.         return $this->productAttributes;
  117.     }
  118.     public function addProductAttribute(ProductAttribute $productAttribute): static
  119.     {
  120.         if (!$this->productAttributes->contains($productAttribute)) {
  121.             $this->productAttributes->add($productAttribute);
  122.             $productAttribute->setProduct($this);
  123.         }
  124.         return $this;
  125.     }
  126.     public function removeProductAttribute(ProductAttribute $productAttribute): static
  127.     {
  128.         if ($this->productAttributes->removeElement($productAttribute)) {
  129.             // set the owning side to null (unless already changed)
  130.             if ($productAttribute->getProduct() === $this) {
  131.                 $productAttribute->setProduct(null);
  132.             }
  133.         }
  134.         return $this;
  135.     }
  136.     public function getName(): ?string
  137.     {
  138.         return $this->name;
  139.     }
  140.     public function setName(string $name): static
  141.     {
  142.         $this->name $name;
  143.         return $this;
  144.     }
  145.     public function getDescription(): ?string
  146.     {
  147.         return $this->description;
  148.     }
  149.     public function setDescription(?string $description): static
  150.     {
  151.         $this->description $description;
  152.         return $this;
  153.     }
  154.     public function getPrice(): ?string
  155.     {
  156.         return $this->price;
  157.     }
  158.     public function setPrice(string $price): static
  159.     {
  160.         $this->price $price;
  161.         return $this;
  162.     }
  163.     public function getStock(): ?string
  164.     {
  165.         return $this->stock;
  166.     }
  167.     public function setStock(string $stock): static
  168.     {
  169.         $this->stock $stock;
  170.         return $this;
  171.     }
  172.     public function getImageUrl(): ?string
  173.     {
  174.         return $this->imageUrl;
  175.     }
  176.     public function setImageUrl(?string $imageUrl): static
  177.     {
  178.         $this->imageUrl $imageUrl;
  179.         return $this;
  180.     }
  181.     public function getWebsite(): ?Website
  182.     {
  183.         return $this->website;
  184.     }
  185.     public function setWebsite(?Website $shop): static
  186.     {
  187.         $this->website $shop;
  188.         return $this;
  189.     }
  190.     /**
  191.      * @return Collection<int, Discount>
  192.      */
  193.     public function getDiscounts(): Collection
  194.     {
  195.         return $this->discounts;
  196.     }
  197.     public function addDiscount(Discount $discount): static
  198.     {
  199.         if (!$this->discounts->contains($discount)) {
  200.             $this->discounts->add($discount);
  201.             $discount->setProduct($this);
  202.         }
  203.         return $this;
  204.     }
  205.     public function removeDiscount(Discount $discount): static
  206.     {
  207.         if ($this->discounts->removeElement($discount)) {
  208.             // set the owning side to null (unless already changed)
  209.             if ($discount->getProduct() === $this) {
  210.                 $discount->setProduct(null);
  211.             }
  212.         }
  213.         return $this;
  214.     }
  215.     public function getCategory()
  216.     {
  217.         return $this->productCategories->first();
  218.     }
  219.     public function setCategory(?ProductCategory $category): static
  220.     {
  221.         $this->category $category;
  222.         return $this;
  223.     }
  224.     /**
  225.      * @return Collection<int, ProductImage>
  226.      */
  227.     public function getProductImages(): Collection
  228.     {
  229.         return $this->productImages;
  230.     }
  231.     public function addProductImage(ProductImage $productImage): static
  232.     {
  233.         if (!$this->productImages->contains($productImage)) {
  234.             $this->productImages->add($productImage);
  235.             $productImage->setProduct($this);
  236.         }
  237.         return $this;
  238.     }
  239.     public function removeProductImage(ProductImage $productImage): static
  240.     {
  241.         if ($this->productImages->removeElement($productImage)) {
  242.             // set the owning side to null (unless already changed)
  243.             if ($productImage->getProduct() === $this) {
  244.                 $productImage->setProduct(null);
  245.             }
  246.         }
  247.         return $this;
  248.     }
  249.     /**
  250.      * @return Collection<int, OrderItem>
  251.      */
  252.     public function getOrderItems(): Collection
  253.     {
  254.         return $this->orderItems;
  255.     }
  256.     public function addOrderItem(OrderItem $orderItem): static
  257.     {
  258.         if (!$this->orderItems->contains($orderItem)) {
  259.             $this->orderItems->add($orderItem);
  260.             $orderItem->setProduct($this);
  261.         }
  262.         return $this;
  263.     }
  264.     public function removeOrderItem(OrderItem $orderItem): static
  265.     {
  266.         if ($this->orderItems->removeElement($orderItem)) {
  267.             // set the owning side to null (unless already changed)
  268.             if ($orderItem->getProduct() === $this) {
  269.                 $orderItem->setProduct(null);
  270.             }
  271.         }
  272.         return $this;
  273.     }
  274.     /**
  275.      * @return Collection<int, CartItem>
  276.      */
  277.     public function getCartItems(): Collection
  278.     {
  279.         return $this->cartItems;
  280.     }
  281.     public function addCartItem(CartItem $cartItem): static
  282.     {
  283.         if (!$this->cartItems->contains($cartItem)) {
  284.             $this->cartItems->add($cartItem);
  285.             $cartItem->setProduct($this);
  286.         }
  287.         return $this;
  288.     }
  289.     public function removeCartItem(CartItem $cartItem): static
  290.     {
  291.         if ($this->cartItems->removeElement($cartItem)) {
  292.             // set the owning side to null (unless already changed)
  293.             if ($cartItem->getProduct() === $this) {
  294.                 $cartItem->setProduct(null);
  295.             }
  296.         }
  297.         return $this;
  298.     }
  299.     /**
  300.      * @return Collection<int, Review>
  301.      */
  302.     public function getReviews(): Collection
  303.     {
  304.         return $this->reviews;
  305.     }
  306.     public function addReview(Review $review): static
  307.     {
  308.         if (!$this->reviews->contains($review)) {
  309.             $this->reviews->add($review);
  310.             $review->setProduct($this);
  311.         }
  312.         return $this;
  313.     }
  314.     public function removeReview(Review $review): static
  315.     {
  316.         if ($this->reviews->removeElement($review)) {
  317.             // set the owning side to null (unless already changed)
  318.             if ($review->getProduct() === $this) {
  319.                 $review->setProduct(null);
  320.             }
  321.         }
  322.         return $this;
  323.     }
  324.     /**
  325.      * @return Collection<int, ProductPrice>
  326.      */
  327.     public function getProductPrices(): Collection
  328.     {
  329.         return $this->productPrices;
  330.     }
  331.     public function addProductPrice(ProductPrice $productPrice): static
  332.     {
  333.         if (!$this->productPrices->contains($productPrice)) {
  334.             $this->productPrices->add($productPrice);
  335.             $productPrice->setProduct($this);
  336.         }
  337.         return $this;
  338.     }
  339.     public function removeProductPrice(ProductPrice $productPrice): static
  340.     {
  341.         if ($this->productPrices->removeElement($productPrice)) {
  342.             // set the owning side to null (unless already changed)
  343.             if ($productPrice->getProduct() === $this) {
  344.                 $productPrice->setProduct(null);
  345.             }
  346.         }
  347.         return $this;
  348.     }
  349.     public function getSubName(): ?string
  350.     {
  351.         return $this->subName;
  352.     }
  353.     public function setSubName(?string $subName): static
  354.     {
  355.         $this->subName $subName;
  356.         return $this;
  357.     }
  358.     /**
  359.      * @return Collection<int, ProductCategory>
  360.      */
  361.     public function getProductCategories(): Collection
  362.     {
  363.         return $this->productCategories;
  364.     }
  365.     public function addProductCategory(ProductCategory $productCategory): static
  366.     {
  367.         if (!$this->productCategories->contains($productCategory)) {
  368.             $this->productCategories->add($productCategory);
  369.             $productCategory->addProduct($this);
  370.         }
  371.         return $this;
  372.     }
  373.     public function removeProductCategory(ProductCategory $productCategory): static
  374.     {
  375.         if ($this->productCategories->removeElement($productCategory)) {
  376.             $productCategory->removeProduct($this);
  377.         }
  378.         return $this;
  379.     }
  380.     /**
  381.      * @return Collection<int, ProductTag>
  382.      */
  383.     public function getProductTags(): Collection
  384.     {
  385.         return $this->productTags;
  386.     }
  387.     public function addProductTag(ProductTag $productTag): static
  388.     {
  389.         if (!$this->productTags->contains($productTag)) {
  390.             $this->productTags->add($productTag);
  391.             $productTag->addProduct($this);
  392.         }
  393.         return $this;
  394.     }
  395.     public function removeProductTag(ProductTag $productTag): static
  396.     {
  397.         if ($this->productTags->removeElement($productTag)) {
  398.             $productTag->removeProduct($this);
  399.         }
  400.         return $this;
  401.     }
  402.     /**
  403.      * @return Collection<int, ProductReview>
  404.      */
  405.     public function getProductReviews(): Collection
  406.     {
  407.         return $this->productReviews;
  408.     }
  409.     public function addProductReview(ProductReview $productReview): static
  410.     {
  411.         if (!$this->productReviews->contains($productReview)) {
  412.             $this->productReviews->add($productReview);
  413.             $productReview->setProduct($this);
  414.         }
  415.         return $this;
  416.     }
  417.     public function removeProductReview(ProductReview $productReview): static
  418.     {
  419.         if ($this->productReviews->removeElement($productReview)) {
  420.             // set the owning side to null (unless already changed)
  421.             if ($productReview->getProduct() === $this) {
  422.                 $productReview->setProduct(null);
  423.             }
  424.         }
  425.         return $this;
  426.     }
  427.     public function isIsSelling(): ?bool
  428.     {
  429.         return $this->isSelling;
  430.     }
  431.     public function setIsSelling(?bool $isSelling): static
  432.     {
  433.         $this->isSelling $isSelling;
  434.         return $this;
  435.     }
  436.     public function getPublishStatus(): ?string
  437.     {
  438.         return $this->publishStatus;
  439.     }
  440.     public function setPublishStatus(?string $publishStatus): static
  441.     {
  442.         $this->publishStatus $publishStatus;
  443.         return $this;
  444.     }
  445.     public function getSku(): ?string
  446.     {
  447.         return $this->sku;
  448.     }
  449.     public function setSku(?string $sku): static
  450.     {
  451.         $this->sku $sku;
  452.         return $this;
  453.     }
  454. }