modules/CDev/Sale/src/Model/SaleDiscount.php line 52

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2001-present X-Cart Holdings LLC. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace CDev\Sale\Model;
  7. use ApiPlatform\Core\Annotation as ApiPlatform;
  8. use CDev\Sale\API\Endpoint\SaleDiscount\DTO\SaleDiscountInput;
  9. use CDev\Sale\API\Endpoint\SaleDiscount\DTO\SaleDiscountOutput;
  10. use Doctrine\ORM\Mapping as ORM;
  11. /**
  12.  * Sale
  13.  *
  14.  * @ORM\Entity
  15.  * @ORM\Table  (name="sale_discounts")
  16.  *
  17.  * @ApiPlatform\ApiResource(
  18.  *     shortName="Sale Discount",
  19.  *     input=SaleDiscountInput::class,
  20.  *     output=SaleDiscountOutput::class,
  21.  *     itemOperations={
  22.  *          "get"={
  23.  *              "method"="GET",
  24.  *              "path"="/sale_discounts/{id}.{_format}",
  25.  *              "identifiers"={"id"},
  26.  *              "requirements"={"id"="\d+"}
  27.  *          },
  28.  *          "put"={
  29.  *              "method"="PUT",
  30.  *              "path"="/sale_discounts/{id}.{_format}",
  31.  *              "identifiers"={"id"},
  32.  *              "requirements"={"id"="\d+"}
  33.  *          },
  34.  *          "delete"={
  35.  *              "method"="DELETE",
  36.  *              "path"="/sale_discounts/{id}.{_format}",
  37.  *              "identifiers"={"id"},
  38.  *              "requirements"={"id"="\d+"}
  39.  *          }
  40.  *     },
  41.  *     collectionOperations={
  42.  *          "get"={
  43.  *              "method"="GET",
  44.  *              "identifiers"={},
  45.  *              "path"="/sale_discounts.{_format}"
  46.  *          },
  47.  *          "post"={
  48.  *              "method"="POST",
  49.  *              "identifiers"={},
  50.  *              "path"="/sale_discounts.{_format}"
  51.  *          }
  52.  *     }
  53.  * )
  54.  */
  55. class SaleDiscount extends \XLite\Model\Base\Catalog
  56. {
  57.     /**
  58.      * Unique ID
  59.      *
  60.      * @var   integer
  61.      *
  62.      * @ORM\Id
  63.      * @ORM\GeneratedValue (strategy="AUTO")
  64.      * @ORM\Column         (type="integer", options={ "unsigned": true })
  65.      */
  66.     protected $id;
  67.     /**
  68.      * Enabled status
  69.      *
  70.      * @var   boolean
  71.      *
  72.      * @ORM\Column (type="boolean")
  73.      */
  74.     protected $enabled true;
  75.     /**
  76.      * Value
  77.      *
  78.      * @var   float
  79.      *
  80.      * @ORM\Column (type="decimal", precision=14, scale=4)
  81.      */
  82.     protected $value 0.0000;
  83.     /**
  84.      * Date range (begin)
  85.      *
  86.      * @var   integer
  87.      *
  88.      * @ORM\Column (type="integer", options={ "unsigned": true })
  89.      */
  90.     protected $dateRangeBegin 0;
  91.     /**
  92.      * Date range (end)
  93.      *
  94.      * @var   integer
  95.      *
  96.      * @ORM\Column (type="integer", options={ "unsigned": true })
  97.      */
  98.     protected $dateRangeEnd 0;
  99.     /**
  100.      * Flag: Sale products is shown in separate section
  101.      *
  102.      * @var boolean
  103.      *
  104.      * @ORM\Column (type="boolean")
  105.      */
  106.     protected $showInSeparateSection false;
  107.     /**
  108.      * Meta description type
  109.      *
  110.      * @var string
  111.      *
  112.      * @ORM\Column (type="string", length=1)
  113.      */
  114.     protected $metaDescType 'A';
  115.     /**
  116.      * Flag: Sale is used for specific products or not
  117.      *
  118.      * @var boolean
  119.      *
  120.      * @ORM\Column (type="boolean")
  121.      */
  122.     protected $specificProducts false;
  123.     /**
  124.      * Product classes
  125.      *
  126.      * @var   \Doctrine\Common\Collections\ArrayCollection
  127.      *
  128.      * @ORM\ManyToMany (targetEntity="XLite\Model\ProductClass", inversedBy="saleDiscounts")
  129.      * @ORM\JoinTable (name="product_class_sale_discounts",
  130.      *      joinColumns={@ORM\JoinColumn (name="sale_id", referencedColumnName="id", onDelete="CASCADE")},
  131.      *      inverseJoinColumns={@ORM\JoinColumn (name="class_id", referencedColumnName="id", onDelete="CASCADE")}
  132.      * )
  133.      */
  134.     protected $productClasses;
  135.     /**
  136.      * Memberships
  137.      *
  138.      * @var   \Doctrine\Common\Collections\ArrayCollection
  139.      *
  140.      * @ORM\ManyToMany (targetEntity="XLite\Model\Membership", inversedBy="saleDiscounts")
  141.      * @ORM\JoinTable (name="membership_sale_discounts",
  142.      *      joinColumns={@ORM\JoinColumn (name="sale_id", referencedColumnName="id", onDelete="CASCADE")},
  143.      *      inverseJoinColumns={@ORM\JoinColumn (name="membership_id", referencedColumnName="membership_id", onDelete="CASCADE")}
  144.      * )
  145.      */
  146.     protected $memberships;
  147.     /**
  148.      * Categories
  149.      *
  150.      * @var   \Doctrine\Common\Collections\ArrayCollection
  151.      *
  152.      * @ORM\ManyToMany (targetEntity="XLite\Model\Category", inversedBy="saleDiscounts")
  153.      * @ORM\JoinTable (name="category_sale_discounts",
  154.      *      joinColumns={@ORM\JoinColumn (name="sale_id", referencedColumnName="id", onDelete="CASCADE")},
  155.      *      inverseJoinColumns={@ORM\JoinColumn (name="category_id", referencedColumnName="category_id", onDelete="CASCADE")}
  156.      * )
  157.      */
  158.     protected $categories;
  159.     /**
  160.      * Sale products
  161.      *
  162.      * @var   \Doctrine\Common\Collections\ArrayCollection
  163.      *
  164.      * @ORM\OneToMany (targetEntity="CDev\Sale\Model\SaleDiscountProduct", mappedBy="saleDiscount")
  165.      */
  166.     protected $saleDiscountProducts;
  167.     /**
  168.      * Clean URLs
  169.      *
  170.      * @var \Doctrine\Common\Collections\Collection
  171.      *
  172.      * @ORM\OneToMany (targetEntity="XLite\Model\CleanURL", mappedBy="sale_discount", cascade={"all"})
  173.      * @ORM\OrderBy   ({"id" = "ASC"})
  174.      */
  175.     protected $cleanURLs;
  176.     /**
  177.      * @var \Doctrine\Common\Collections\Collection
  178.      *
  179.      * @ORM\OneToMany (targetEntity="CDev\Sale\Model\SaleDiscountTranslation", mappedBy="owner", cascade={"all"})
  180.      */
  181.     protected $translations;
  182.     /**
  183.      * Constructor
  184.      *
  185.      * @param array $data Entity properties OPTIONAL
  186.      */
  187.     public function __construct(array $data = [])
  188.     {
  189.         $this->productClasses = new \Doctrine\Common\Collections\ArrayCollection();
  190.         $this->memberships    = new \Doctrine\Common\Collections\ArrayCollection();
  191.         $this->categories     = new \Doctrine\Common\Collections\ArrayCollection();
  192.         $this->saleDiscountProducts   = new \Doctrine\Common\Collections\ArrayCollection();
  193.         parent::__construct($data);
  194.     }
  195.     /**
  196.      * Check discount activity
  197.      *
  198.      * @return boolean
  199.      */
  200.     public function isActive()
  201.     {
  202.         if (!$this->getEnabled()) {
  203.             return false;
  204.         }
  205.         if (!$this->isStarted()) {
  206.             return false;
  207.         }
  208.         if ($this->isExpired()) {
  209.             return false;
  210.         }
  211.         return true;
  212.     }
  213.     /**
  214.      * Check - discount is started
  215.      *
  216.      * @return boolean
  217.      */
  218.     public function isStarted()
  219.     {
  220.         return $this->getDateRangeBegin() === || $this->getDateRangeBegin() < \XLite\Core\Converter::time();
  221.     }
  222.     /**
  223.      * Check - discount is expired or not
  224.      *
  225.      * @return boolean
  226.      */
  227.     public function isExpired()
  228.     {
  229.         return $this->getDateRangeEnd() && $this->getDateRangeEnd() < \XLite\Core\Converter::time();
  230.     }
  231.     /**
  232.      * @param \XLite\Model\Product $product
  233.      * @return bool
  234.      */
  235.     public function isApplicableForProduct(\XLite\Model\Product $product)
  236.     {
  237.         if ($this->getSpecificProducts()) {
  238.             if (!$this->checkSpecificProduct($product)) {
  239.                 return false;
  240.             }
  241.         } else {
  242.             if ($this->isExcludedForProduct($product)) {
  243.                 return false;
  244.             }
  245.             if (!$this->checkProductClassForProduct($product)) {
  246.                 return false;
  247.             }
  248.         }
  249.         return true;
  250.     }
  251.     public function isExcludedForProduct($product)
  252.     {
  253.         return !$this->checkCategoryForProduct($product);
  254.     }
  255.     /**
  256.      * @param \XLite\Model\Category $category
  257.      * @return bool
  258.      */
  259.     public function isApplicableForCategory(\XLite\Model\Category $category)
  260.     {
  261.         if ($this->getSpecificProducts()) {
  262.             return false;
  263.         }
  264.         if ($this->getCategories()->count()) {
  265.              return $this->getCategories()->contains($category);
  266.         }
  267.         return false;
  268.     }
  269.     /**
  270.      * @param \XLite\Model\Product $product
  271.      * @return bool
  272.      */
  273.     protected function checkCategoryForProduct(\XLite\Model\Product $product)
  274.     {
  275.         if ($this->getCategories()->count()) {
  276.             foreach ($product->getCategories() as $category) {
  277.                 if ($this->getCategories()->contains($category)) {
  278.                     return true;
  279.                 }
  280.             }
  281.             return false;
  282.         }
  283.         return true;
  284.     }
  285.     /**
  286.      * @param \XLite\Model\Product $product
  287.      * @return bool
  288.      */
  289.     protected function checkProductClassForProduct(\XLite\Model\Product $product)
  290.     {
  291.         if ($this->getProductClasses()->count()) {
  292.             return $this->getProductClasses()->contains($product->getProductClass());
  293.         }
  294.         return true;
  295.     }
  296.     /**
  297.      * @param \XLite\Model\Product $product
  298.      * @return bool
  299.      */
  300.     protected function checkSpecificProduct(\XLite\Model\Product $product)
  301.     {
  302.         return in_array($product->getProductId(), $this->getApplicableProductIds());
  303.     }
  304.     /**
  305.      * @param \XLite\Model\Profile $profile
  306.      * @return bool
  307.      */
  308.     public function isApplicableForProfile(\XLite\Model\Profile $profile)
  309.     {
  310.         if (!$this->checkMembershipForProfile($profile)) {
  311.             return false;
  312.         }
  313.         return true;
  314.     }
  315.     /**
  316.      * @param \XLite\Model\Profile $profile
  317.      * @return bool
  318.      */
  319.     protected function checkMembershipForProfile(\XLite\Model\Profile $profile)
  320.     {
  321.         if ($this->getMemberships()->count()) {
  322.             return $this->getMemberships()->contains($profile->getMembership());
  323.         }
  324.         return true;
  325.     }
  326.     /**
  327.      * Returns meta description
  328.      *
  329.      * @return string
  330.      */
  331.     public function getMetaDesc()
  332.     {
  333.         return $this->getMetaDescType() === 'A'
  334.             ? static::postprocessMetaDescription($this->getName())
  335.             : $this->getSoftTranslation()->getMetaDesc();
  336.     }
  337.     /**
  338.      * Returns meta description type
  339.      *
  340.      * @return string
  341.      */
  342.     public function getMetaDescType()
  343.     {
  344.         $result $this->metaDescType;
  345.         if (!$result) {
  346.             $metaDescPresent array_reduce($this->getTranslations()->toArray(), static function ($carry$item) {
  347.                 return $carry ?: (bool) $item->getMetaDesc();
  348.             }, false);
  349.             $result $metaDescPresent 'C' 'A';
  350.         }
  351.         return $result;
  352.     }
  353.     /**
  354.      * Set metaDescType
  355.      *
  356.      * @param string $metaDescType
  357.      */
  358.     public function setMetaDescType($metaDescType)
  359.     {
  360.         $this->metaDescType $metaDescType;
  361.     }
  362.     /**
  363.      * Get id
  364.      *
  365.      * @return integer
  366.      */
  367.     public function getId()
  368.     {
  369.         return $this->id;
  370.     }
  371.     /**
  372.      * Set enabled
  373.      *
  374.      * @param boolean $enabled
  375.      */
  376.     public function setEnabled($enabled)
  377.     {
  378.         $this->enabled = (bool)$enabled;
  379.     }
  380.     /**
  381.      * Get enabled
  382.      *
  383.      * @return boolean
  384.      */
  385.     public function getEnabled()
  386.     {
  387.         return $this->enabled;
  388.     }
  389.     /**
  390.      * Set value
  391.      *
  392.      * @param float $value
  393.      */
  394.     public function setValue($value)
  395.     {
  396.         $this->value $value;
  397.     }
  398.     /**
  399.      * Get value
  400.      *
  401.      * @return float
  402.      */
  403.     public function getValue()
  404.     {
  405.         return $this->value;
  406.     }
  407.     /**
  408.      * Set dateRangeBegin
  409.      *
  410.      * @param integer $dateRangeBegin
  411.      */
  412.     public function setDateRangeBegin($dateRangeBegin)
  413.     {
  414.         $this->dateRangeBegin $dateRangeBegin;
  415.     }
  416.     /**
  417.      * Get dateRangeBegin
  418.      *
  419.      * @return integer
  420.      */
  421.     public function getDateRangeBegin()
  422.     {
  423.         return $this->dateRangeBegin;
  424.     }
  425.     /**
  426.      * Set dateRangeEnd
  427.      *
  428.      * @param integer $dateRangeEnd
  429.      */
  430.     public function setDateRangeEnd($dateRangeEnd)
  431.     {
  432.         $this->dateRangeEnd $dateRangeEnd;
  433.     }
  434.     /**
  435.      * Get dateRangeEnd
  436.      *
  437.      * @return integer
  438.      */
  439.     public function getDateRangeEnd()
  440.     {
  441.         return $this->dateRangeEnd;
  442.     }
  443.     /**
  444.      * Set showInSeparateSection
  445.      *
  446.      * @param boolean $showInSeparateSection
  447.      */
  448.     public function setShowInSeparateSection($showInSeparateSection)
  449.     {
  450.         $this->showInSeparateSection $showInSeparateSection;
  451.     }
  452.     /**
  453.      * Get showInSeparateSection
  454.      *
  455.      * @return boolean
  456.      */
  457.     public function getShowInSeparateSection()
  458.     {
  459.         return $this->showInSeparateSection;
  460.     }
  461.     /**
  462.      * Set specificProducts
  463.      *
  464.      * @param boolean $specificProducts
  465.      */
  466.     public function setSpecificProducts($specificProducts)
  467.     {
  468.         $this->specificProducts $specificProducts;
  469.     }
  470.     /**
  471.      * Get specificProducts
  472.      *
  473.      * @return boolean
  474.      */
  475.     public function getSpecificProducts()
  476.     {
  477.         return $this->specificProducts;
  478.     }
  479.     /**
  480.      * Add productClasses
  481.      *
  482.      * @param \XLite\Model\ProductClass $productClasses
  483.      */
  484.     public function addProductClasses(\XLite\Model\ProductClass $productClasses)
  485.     {
  486.         $this->productClasses[] = $productClasses;
  487.     }
  488.     /**
  489.      * Get productClasses
  490.      *
  491.      * @return \Doctrine\Common\Collections\Collection
  492.      */
  493.     public function getProductClasses()
  494.     {
  495.         return $this->productClasses;
  496.     }
  497.     /**
  498.      * Clear product classes
  499.      */
  500.     public function clearProductClasses()
  501.     {
  502.         foreach ($this->getProductClasses()->getKeys() as $key) {
  503.             $this->getProductClasses()->remove($key);
  504.         }
  505.     }
  506.     /**
  507.      * Add memberships
  508.      *
  509.      * @param \XLite\Model\Membership $memberships
  510.      */
  511.     public function addMemberships(\XLite\Model\Membership $memberships)
  512.     {
  513.         $this->memberships[] = $memberships;
  514.     }
  515.     /**
  516.      * Get memberships
  517.      *
  518.      * @return \Doctrine\Common\Collections\Collection
  519.      */
  520.     public function getMemberships()
  521.     {
  522.         return $this->memberships;
  523.     }
  524.     /**
  525.      * Clear memberships
  526.      */
  527.     public function clearMemberships()
  528.     {
  529.         foreach ($this->getMemberships()->getKeys() as $key) {
  530.             $this->getMemberships()->remove($key);
  531.         }
  532.     }
  533.     /**
  534.      * Add categories
  535.      *
  536.      * @param \XLite\Model\Category $categories
  537.      */
  538.     public function addCategories(\XLite\Model\Category $categories)
  539.     {
  540.         $this->getCategories()->add($categories);
  541.     }
  542.     /**
  543.      * Get categories
  544.      *
  545.      * @return \Doctrine\Common\Collections\Collection
  546.      */
  547.     public function getCategories()
  548.     {
  549.         return $this->categories;
  550.     }
  551.     /**
  552.      * Clear categories
  553.      */
  554.     public function clearCategories()
  555.     {
  556.         foreach ($this->getCategories()->getKeys() as $key) {
  557.             $this->getCategories()->remove($key);
  558.         }
  559.     }
  560.     /**
  561.      * Add sale discount products
  562.      *
  563.      * @param \CDev\Sale\Model\SaleDiscountProduct $saleDiscountProduct
  564.      */
  565.     public function addSaleDiscountProducts(\CDev\Sale\Model\SaleDiscountProduct $saleDiscountProduct)
  566.     {
  567.         $this->saleDiscountProducts[] = $saleDiscountProduct;
  568.     }
  569.     /**
  570.      * Get product ids if sale discount is specificProducts
  571.      *
  572.      * @return array
  573.      */
  574.     public function getApplicableProductIds()
  575.     {
  576.         $ids = [];
  577.         if ($this->isPersistent() && $this->getSpecificProducts()) {
  578.             $ids \XLite\Core\Database::getRepo('CDev\Sale\Model\SaleDiscountProduct')
  579.                 ->getSaleDiscountProductIds($this->getId());
  580.         }
  581.         return $ids;
  582.     }
  583.     /**
  584.      * Get sale products
  585.      *
  586.      * @return \Doctrine\Common\Collections\Collection
  587.      */
  588.     public function getSaleDiscountProducts()
  589.     {
  590.         return $this->saleDiscountProducts;
  591.     }
  592.     /**
  593.      * @return \Doctrine\Common\Collections\Collection
  594.      */
  595.     public function getCleanURLs()
  596.     {
  597.         return $this->cleanURLs;
  598.     }
  599.     /**
  600.      * Add cleanURLs
  601.      *
  602.      * @param \XLite\Model\CleanURL $cleanURLs
  603.      * @return SaleDiscount
  604.      */
  605.     public function addCleanURLs(\XLite\Model\CleanURL $cleanURLs)
  606.     {
  607.         $this->cleanURLs[] = $cleanURLs;
  608.         return $this;
  609.     }
  610.     // {{{ Translation Getters / setters
  611.     /**
  612.      * @return string
  613.      */
  614.     public function getMetaTags()
  615.     {
  616.         return $this->getTranslationField(__FUNCTION__);
  617.     }
  618.     /**
  619.      * @param string $metaTags
  620.      *
  621.      * @return \XLite\Model\Base\Translation
  622.      */
  623.     public function setMetaTags($metaTags)
  624.     {
  625.         return $this->setTranslationField(__FUNCTION__$metaTags);
  626.     }
  627.     /**
  628.      * @param string $metaDesc
  629.      *
  630.      * @return \XLite\Model\Base\Translation
  631.      */
  632.     public function setMetaDesc($metaDesc)
  633.     {
  634.         return $this->setTranslationField(__FUNCTION__$metaDesc);
  635.     }
  636.     /**
  637.      * @return string
  638.      */
  639.     public function getMetaTitle()
  640.     {
  641.         return $this->getTranslationField(__FUNCTION__);
  642.     }
  643.     /**
  644.      * @param string $metaTitle
  645.      *
  646.      * @return \XLite\Model\Base\Translation
  647.      */
  648.     public function setMetaTitle($metaTitle)
  649.     {
  650.         return $this->setTranslationField(__FUNCTION__$metaTitle);
  651.     }
  652.     // }}}
  653. }