src/AtechNet/CoreBundle/Entity/Coupon.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\AtechNet\CoreBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping\Column;
  6. use Doctrine\ORM\Mapping\Entity;
  7. use Doctrine\ORM\Mapping\JoinColumn;
  8. use Doctrine\ORM\Mapping\JoinTable;
  9. use Doctrine\ORM\Mapping\ManyToMany;
  10. use Doctrine\ORM\Mapping\ManyToOne;
  11. use Doctrine\ORM\Mapping\Table as Table;
  12. /**
  13.  * Coupon
  14.  *
  15.  * @Table(name="coupons")
  16.  * @Entity(repositoryClass="App\AtechNet\CoreBundle\Repository\CouponRepository")
  17.  */
  18. class Coupon extends AbstractSignedEntity
  19. {
  20.     public const DISCOUNT_TYPE_PERCENT = 1;
  21.     public const DISCOUNT_TYPE_PRICE = 2;
  22.     public const DISCOUNT_TYPE_BONUS_PRODUCT = 3;
  23.     public const COUPON_PRODUCT_ID = 10427;
  24.     public const MAC_MOUSE_COUPON_CODE = 'MACLICK';
  25.     public const SUMMER_2026_COUPON_CODE = 'SUMMER10';
  26.     public static function getDiscountTypes(): array
  27.     {
  28.         return [
  29.             'Price percent' => self::DISCOUNT_TYPE_PERCENT,
  30.             'Price discount' => self::DISCOUNT_TYPE_PRICE,
  31.             //            'Bonus product'  => self::DISCOUNT_TYPE_BONUS_PRODUCT
  32.         ];
  33.     }
  34.     /**
  35.      * @var Domain
  36.      *
  37.      * @ManyToOne(targetEntity="Domain")
  38.      * @JoinColumn(name="domain_id", referencedColumnName="id", nullable=false)
  39.      */
  40.     private $domain;
  41.     /**
  42.      * @var string
  43.      *
  44.      * @Column(name="code", type="string", length=24)
  45.      */
  46.     private $code;
  47.     /**
  48.      * @var bool
  49.      *
  50.      * @Column(name="enabled", type="boolean")
  51.      */
  52.     private $enabled = false;
  53.     /**
  54.      * @var string
  55.      *
  56.      * @Column(name="description", type="text")
  57.      */
  58.     private $description;
  59.     /**
  60.      * @var \DateTime
  61.      *
  62.      * @Column(name="valid_form", type="datetime", nullable=true)
  63.      */
  64.     private $validFrom;
  65.     /**
  66.      * @var \DateTime
  67.      *
  68.      * @Column(name="valid_to", type="datetime", nullable=true)
  69.      */
  70.     private $validTo;
  71.     /**
  72.      * @var int
  73.      *
  74.      * @Column(name="discount_type", type="integer")
  75.      */
  76.     private $discountType;
  77.     /**
  78.      * @var int
  79.      *
  80.      * @Column(name="discount", type="float")
  81.      */
  82.     private $discount;
  83.     /**
  84.      * @var int
  85.      *
  86.      * @Column(name="total", type="float")
  87.      */
  88.     private $total;
  89.     /**
  90.      * @var bool
  91.      *
  92.      * @Column(name="available_for_discounted_products", type="boolean")
  93.      */
  94.     private $availableForDiscountedProducts = false;
  95.     /**
  96.      * @var bool
  97.      *
  98.      * @Column(name="only_once_use", type="boolean")
  99.      */
  100.     private $onlyOnceUse = false;
  101.     /**
  102.      * @var bool
  103.      *
  104.      * @Column(name="only_once_customer_use", type="boolean")
  105.      */
  106.     private $onlyOnceCustomerUse = false;
  107.     /**
  108.      * @var bool
  109.      *
  110.      * @Column(name="free_shipping", type="boolean")
  111.      */
  112.     private $freeShipping = false;
  113.     /**
  114.      * @var ArrayCollection
  115.      *
  116.      * @ManyToMany(targetEntity="ProductType")
  117.      * @JoinTable(name="coupons__product_types",
  118.      *      joinColumns={@JoinColumn(name="coupon_id", referencedColumnName="id")},
  119.      *      inverseJoinColumns={@JoinColumn(name="product_type_id", referencedColumnName="id")}
  120.      * )
  121.      */
  122.     private $productTypes;
  123.     /**
  124.      * @var ArrayCollection
  125.      *
  126.      * @ManyToMany(targetEntity="Vendor")
  127.      * @JoinTable(name="coupons__vendors",
  128.      *      joinColumns={@JoinColumn(name="coupon_id", referencedColumnName="id")},
  129.      *      inverseJoinColumns={@JoinColumn(name="vendor_id", referencedColumnName="id")}
  130.      * )
  131.      */
  132.     private $vendors;
  133.     /**
  134.      * @var ArrayCollection
  135.      *
  136.      * @ManyToMany(targetEntity="Product", inversedBy="coupons")
  137.      * @JoinTable(name="coupons__products",
  138.      *      joinColumns={@JoinColumn(name="coupon_id", referencedColumnName="id")},
  139.      *      inverseJoinColumns={@JoinColumn(name="product_id", referencedColumnName="id")}
  140.      * )
  141.      */
  142.     private $products;
  143.     /**
  144.      * @var string
  145.      *
  146.      * @Column(name="product_tags", type="string", nullable=true)
  147.      */
  148.     private string|null $productTags;
  149.     /**
  150.      * @var bool
  151.      *
  152.      * @Column(name="show_badge_on_website", type="boolean", options={"default": false})
  153.      */
  154.     private bool $showBadgeOnWebsite = false;
  155.     public function __construct()
  156.     {
  157.         parent::__construct();
  158.         $this->productTypes = new ArrayCollection();
  159.         $this->vendors = new ArrayCollection();
  160.         $this->products = new ArrayCollection();
  161.     }
  162.     /**
  163.      * @return Domain
  164.      */
  165.     public function getDomain()
  166.     {
  167.         return $this->domain;
  168.     }
  169.     /**
  170.      * @param Domain $domain
  171.      */
  172.     public function setDomain($domain)
  173.     {
  174.         $this->domain = $domain;
  175.     }
  176.     /**
  177.      * @return string
  178.      */
  179.     public function getCode()
  180.     {
  181.         return $this->code;
  182.     }
  183.     /**
  184.      * @param string $code
  185.      */
  186.     public function setCode($code)
  187.     {
  188.         $this->code = $code;
  189.     }
  190.     /**
  191.      * @return bool
  192.      */
  193.     public function isEnabled()
  194.     {
  195.         return $this->enabled;
  196.     }
  197.     /**
  198.      * @param bool $enabled
  199.      */
  200.     public function setEnabled($enabled)
  201.     {
  202.         $this->enabled = $enabled;
  203.     }
  204.     /**
  205.      * @return string
  206.      */
  207.     public function getDescription()
  208.     {
  209.         return $this->description;
  210.     }
  211.     /**
  212.      * @param string $description
  213.      */
  214.     public function setDescription($description)
  215.     {
  216.         $this->description = $description;
  217.     }
  218.     /**
  219.      * @return \DateTime
  220.      */
  221.     public function getValidFrom()
  222.     {
  223.         return $this->validFrom;
  224.     }
  225.     /**
  226.      * @param \DateTime $validFrom
  227.      */
  228.     public function setValidFrom($validFrom)
  229.     {
  230.         $this->validFrom = $validFrom;
  231.     }
  232.     /**
  233.      * @return \DateTime
  234.      */
  235.     public function getValidTo()
  236.     {
  237.         return $this->validTo;
  238.     }
  239.     /**
  240.      * @param \DateTime $validTo
  241.      */
  242.     public function setValidTo($validTo)
  243.     {
  244.         $this->validTo = $validTo;
  245.     }
  246.     /**
  247.      * @return bool
  248.      */
  249.     public function isActive()
  250.     {
  251.         if (!$this->enabled) {
  252.             return false;
  253.         }
  254.         $currentDateTime = new \DateTime();
  255.         if (!is_null($this->validFrom) && is_null($this->validTo)) {
  256.             return $this->validFrom < $currentDateTime;
  257.         } elseif (is_null($this->validFrom) && !is_null($this->validTo)) {
  258.             return $this->validTo > $currentDateTime;
  259.         } elseif (!is_null($this->validFrom) && !is_null($this->validTo)) {
  260.             return $this->validFrom < $currentDateTime && $this->validTo > $currentDateTime;
  261.         }
  262.         return true;
  263.     }
  264.     /**
  265.      * @return int
  266.      */
  267.     public function getDiscount()
  268.     {
  269.         return $this->discount;
  270.     }
  271.     /**
  272.      * @param int $discount
  273.      */
  274.     public function setDiscount($discount)
  275.     {
  276.         $this->discount = $discount;
  277.     }
  278.     /**
  279.      * @return int
  280.      */
  281.     public function getDiscountType()
  282.     {
  283.         return $this->discountType;
  284.     }
  285.     /**
  286.      * @param int $discountType
  287.      */
  288.     public function setDiscountType($discountType)
  289.     {
  290.         $this->discountType = $discountType;
  291.     }
  292.     /**
  293.      * @return float
  294.      */
  295.     public function getTotal(): ?float
  296.     {
  297.         return $this->total;
  298.     }
  299.     /**
  300.      * @param float $total
  301.      */
  302.     public function setTotal(float $total)
  303.     {
  304.         $this->total = $total;
  305.     }
  306.     /**
  307.      * @return bool
  308.      */
  309.     public function isAvailableForDiscountedProducts()
  310.     {
  311.         return $this->availableForDiscountedProducts;
  312.     }
  313.     /**
  314.      * @param bool $availableForDiscountedProducts
  315.      */
  316.     public function setAvailableForDiscountedProducts($availableForDiscountedProducts)
  317.     {
  318.         $this->availableForDiscountedProducts = $availableForDiscountedProducts;
  319.     }
  320.     /**
  321.      * @return bool
  322.      */
  323.     public function hasFreeShipping(): bool
  324.     {
  325.         return $this->freeShipping;
  326.     }
  327.     /**
  328.      * @param bool $freeShipping
  329.      */
  330.     public function setFreeShipping(bool $freeShipping)
  331.     {
  332.         $this->freeShipping = $freeShipping;
  333.     }
  334.     /**
  335.      * @return bool
  336.      */
  337.     public function isOnlyOnceUse(): bool
  338.     {
  339.         return $this->onlyOnceUse;
  340.     }
  341.     /**
  342.      * @param bool $onlyOnceUse
  343.      */
  344.     public function setOnlyOnceUse(bool $onlyOnceUse)
  345.     {
  346.         $this->onlyOnceUse = $onlyOnceUse;
  347.     }
  348.     /**
  349.      * @return bool
  350.      */
  351.     public function isOnlyOnceCustomerUse(): bool
  352.     {
  353.         return $this->onlyOnceCustomerUse;
  354.     }
  355.     /**
  356.      * @param bool $onlyOnceCustomerUse
  357.      */
  358.     public function setOnlyOnceCustomerUse(bool $onlyOnceCustomerUse)
  359.     {
  360.         $this->onlyOnceCustomerUse = $onlyOnceCustomerUse;
  361.     }
  362.     /**
  363.      * @return Collection
  364.      */
  365.     public function getProductTypes(): Collection
  366.     {
  367.         return $this->productTypes;
  368.     }
  369.     /**
  370.      * @param ArrayCollection $productTypes
  371.      */
  372.     public function setProductTypes(ArrayCollection $productTypes)
  373.     {
  374.         $this->productTypes = $productTypes;
  375.     }
  376.     /**
  377.      * @param ProductType $productType
  378.      */
  379.     public function addProductType(ProductType $productType)
  380.     {
  381.         $this->productTypes->add($productType);
  382.     }
  383.     /**
  384.      * @param ProductType $productType
  385.      */
  386.     public function removeProductType(ProductType $productType)
  387.     {
  388.         $this->productTypes->removeElement($productType);
  389.     }
  390.     /**
  391.      * @return Collection
  392.      */
  393.     public function getVendors(): Collection
  394.     {
  395.         return $this->vendors;
  396.     }
  397.     /**
  398.      * @param ArrayCollection $vendors
  399.      */
  400.     public function setVendors(ArrayCollection $vendors)
  401.     {
  402.         $this->vendors = $vendors;
  403.     }
  404.     /**
  405.      * @param Vendor $vendor
  406.      */
  407.     public function addVendor(Vendor $vendor)
  408.     {
  409.         $this->vendors->add($vendor);
  410.     }
  411.     /**
  412.      * @param Vendor $vendor
  413.      */
  414.     public function removeVendor(Vendor $vendor)
  415.     {
  416.         $this->vendors->removeElement($vendor);
  417.     }
  418.     /**
  419.      * @return Collection
  420.      */
  421.     public function getProducts(): Collection
  422.     {
  423.         return $this->products;
  424.     }
  425.     /**
  426.      * @param ArrayCollection $products
  427.      */
  428.     public function setProducts(ArrayCollection $products)
  429.     {
  430.         $this->products = $products;
  431.     }
  432.     /**
  433.      * @param Product $product
  434.      */
  435.     public function addProduct(Product $product)
  436.     {
  437.         if ( !$this->products->contains($product) ) {
  438.             $this->products->add($product);
  439.         }
  440.     }
  441.     /**
  442.      * @param Product $product
  443.      */
  444.     public function removeProduct(Product $product)
  445.     {
  446.         $this->products->removeElement($product);
  447.     }
  448.     public function clearProducts(): void
  449.     {
  450.         $this->products = new ArrayCollection();
  451.     }
  452.     /**
  453.      * @return string|null
  454.      */
  455.     public function getEmail(): ?string
  456.     {
  457.         return $this->email;
  458.     }
  459.     /**
  460.      * @param string|null $email
  461.      */
  462.     public function setEmail(?string $email)
  463.     {
  464.         $this->email = $email;
  465.     }
  466.     public function getProductTagIds(): array
  467.     {
  468.         if (is_null($this->productTags)) {
  469.             return [];
  470.         }
  471.         return explode(',', trim($this->productTags, ','));
  472.     }
  473.     public function addProductTag(string|int $productTagId): void
  474.     {
  475.         $this->productTags .= $productTagId . ',';
  476.     }
  477.     public function removeProductTag(string|int $productTagId): void
  478.     {
  479.         $productTags = explode(',', trim($this->productTags, ','));
  480.         foreach ($productTags as $key => $tag) {
  481.             if ($tag == (string)$productTagId) {
  482.                 unset($productTags[$key]);
  483.             }
  484.         }
  485.         $this->productTags = implode(',', $productTags);
  486.     }
  487.     /**
  488.      * @return bool
  489.      */
  490.     public function isShowBadgeOnWebsite(): bool
  491.     {
  492.         return $this->showBadgeOnWebsite;
  493.     }
  494.     /**
  495.      * @param bool $showBadgeOnWebsite
  496.      *
  497.      * @return void
  498.      */
  499.     public function setShowBadgeOnWebsite(bool $showBadgeOnWebsite): void
  500.     {
  501.         $this->showBadgeOnWebsite = $showBadgeOnWebsite;
  502.     }
  503. }