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

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. 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->checkCategoryForProduct($product)) {
  243.                 return false;
  244.             }
  245.             if (!$this->checkProductClassForProduct($product)) {
  246.                 return false;
  247.             }
  248.         }
  249.         return true;
  250.     }
  251.     /**
  252.      * @param \XLite\Model\Category $category
  253.      * @return bool
  254.      */
  255.     public function isApplicableForCategory(\XLite\Model\Category $category)
  256.     {
  257.         if ($this->getSpecificProducts()) {
  258.             return false;
  259.         }
  260.         if ($this->getCategories()->count()) {
  261.              return $this->getCategories()->contains($category);
  262.         }
  263.         return false;
  264.     }
  265.     /**
  266.      * @param \XLite\Model\Product $product
  267.      * @return bool
  268.      */
  269.     protected function checkCategoryForProduct(\XLite\Model\Product $product)
  270.     {
  271.         if ($this->getCategories()->count()) {
  272.             foreach ($product->getCategories() as $category) {
  273.                 if ($this->getCategories()->contains($category)) {
  274.                     return true;
  275.                 }
  276.             }
  277.             return false;
  278.         }
  279.         return true;
  280.     }
  281.     /**
  282.      * @param \XLite\Model\Product $product
  283.      * @return bool
  284.      */
  285.     protected function checkProductClassForProduct(\XLite\Model\Product $product)
  286.     {
  287.         if ($this->getProductClasses()->count()) {
  288.             return $this->getProductClasses()->contains($product->getProductClass());
  289.         }
  290.         return true;
  291.     }
  292.     /**
  293.      * @param \XLite\Model\Product $product
  294.      * @return bool
  295.      */
  296.     protected function checkSpecificProduct(\XLite\Model\Product $product)
  297.     {
  298.         return in_array($product->getProductId(), $this->getApplicableProductIds());
  299.     }
  300.     /**
  301.      * @param \XLite\Model\Profile $profile
  302.      * @return bool
  303.      */
  304.     public function isApplicableForProfile(\XLite\Model\Profile $profile)
  305.     {
  306.         if (!$this->checkMembershipForProfile($profile)) {
  307.             return false;
  308.         }
  309.         return true;
  310.     }
  311.     /**
  312.      * @param \XLite\Model\Profile $profile
  313.      * @return bool
  314.      */
  315.     protected function checkMembershipForProfile(\XLite\Model\Profile $profile)
  316.     {
  317.         if ($this->getMemberships()->count()) {
  318.             return $this->getMemberships()->contains($profile->getMembership());
  319.         }
  320.         return true;
  321.     }
  322.     /**
  323.      * Returns meta description
  324.      *
  325.      * @return string
  326.      */
  327.     public function getMetaDesc()
  328.     {
  329.         return $this->getMetaDescType() === 'A'
  330.             ? static::postprocessMetaDescription($this->getName())
  331.             : $this->getSoftTranslation()->getMetaDesc();
  332.     }
  333.     /**
  334.      * Returns meta description type
  335.      *
  336.      * @return string
  337.      */
  338.     public function getMetaDescType()
  339.     {
  340.         $result $this->metaDescType;
  341.         if (!$result) {
  342.             $metaDescPresent array_reduce($this->getTranslations()->toArray(), static function ($carry$item) {
  343.                 return $carry ?: (bool) $item->getMetaDesc();
  344.             }, false);
  345.             $result $metaDescPresent 'C' 'A';
  346.         }
  347.         return $result;
  348.     }
  349.     /**
  350.      * Set metaDescType
  351.      *
  352.      * @param string $metaDescType
  353.      */
  354.     public function setMetaDescType($metaDescType)
  355.     {
  356.         $this->metaDescType $metaDescType;
  357.     }
  358.     /**
  359.      * Get id
  360.      *
  361.      * @return integer
  362.      */
  363.     public function getId()
  364.     {
  365.         return $this->id;
  366.     }
  367.     /**
  368.      * Set enabled
  369.      *
  370.      * @param boolean $enabled
  371.      */
  372.     public function setEnabled($enabled)
  373.     {
  374.         $this->enabled = (bool)$enabled;
  375.     }
  376.     /**
  377.      * Get enabled
  378.      *
  379.      * @return boolean
  380.      */
  381.     public function getEnabled()
  382.     {
  383.         return $this->enabled;
  384.     }
  385.     /**
  386.      * Set value
  387.      *
  388.      * @param float $value
  389.      */
  390.     public function setValue($value)
  391.     {
  392.         $this->value $value;
  393.     }
  394.     /**
  395.      * Get value
  396.      *
  397.      * @return float
  398.      */
  399.     public function getValue()
  400.     {
  401.         return $this->value;
  402.     }
  403.     /**
  404.      * Set dateRangeBegin
  405.      *
  406.      * @param integer $dateRangeBegin
  407.      */
  408.     public function setDateRangeBegin($dateRangeBegin)
  409.     {
  410.         $this->dateRangeBegin $dateRangeBegin;
  411.     }
  412.     /**
  413.      * Get dateRangeBegin
  414.      *
  415.      * @return integer
  416.      */
  417.     public function getDateRangeBegin()
  418.     {
  419.         return $this->dateRangeBegin;
  420.     }
  421.     /**
  422.      * Set dateRangeEnd
  423.      *
  424.      * @param integer $dateRangeEnd
  425.      */
  426.     public function setDateRangeEnd($dateRangeEnd)
  427.     {
  428.         $this->dateRangeEnd $dateRangeEnd;
  429.     }
  430.     /**
  431.      * Get dateRangeEnd
  432.      *
  433.      * @return integer
  434.      */
  435.     public function getDateRangeEnd()
  436.     {
  437.         return $this->dateRangeEnd;
  438.     }
  439.     /**
  440.      * Set showInSeparateSection
  441.      *
  442.      * @param boolean $showInSeparateSection
  443.      */
  444.     public function setShowInSeparateSection($showInSeparateSection)
  445.     {
  446.         $this->showInSeparateSection $showInSeparateSection;
  447.     }
  448.     /**
  449.      * Get showInSeparateSection
  450.      *
  451.      * @return boolean
  452.      */
  453.     public function getShowInSeparateSection()
  454.     {
  455.         return $this->showInSeparateSection;
  456.     }
  457.     /**
  458.      * Set specificProducts
  459.      *
  460.      * @param boolean $specificProducts
  461.      */
  462.     public function setSpecificProducts($specificProducts)
  463.     {
  464.         $this->specificProducts $specificProducts;
  465.     }
  466.     /**
  467.      * Get specificProducts
  468.      *
  469.      * @return boolean
  470.      */
  471.     public function getSpecificProducts()
  472.     {
  473.         return $this->specificProducts;
  474.     }
  475.     /**
  476.      * Add productClasses
  477.      *
  478.      * @param \XLite\Model\ProductClass $productClasses
  479.      */
  480.     public function addProductClasses(\XLite\Model\ProductClass $productClasses)
  481.     {
  482.         $this->productClasses[] = $productClasses;
  483.     }
  484.     /**
  485.      * Get productClasses
  486.      *
  487.      * @return \Doctrine\Common\Collections\Collection
  488.      */
  489.     public function getProductClasses()
  490.     {
  491.         return $this->productClasses;
  492.     }
  493.     /**
  494.      * Clear product classes
  495.      */
  496.     public function clearProductClasses()
  497.     {
  498.         foreach ($this->getProductClasses()->getKeys() as $key) {
  499.             $this->getProductClasses()->remove($key);
  500.         }
  501.     }
  502.     /**
  503.      * Add memberships
  504.      *
  505.      * @param \XLite\Model\Membership $memberships
  506.      */
  507.     public function addMemberships(\XLite\Model\Membership $memberships)
  508.     {
  509.         $this->memberships[] = $memberships;
  510.     }
  511.     /**
  512.      * Get memberships
  513.      *
  514.      * @return \Doctrine\Common\Collections\Collection
  515.      */
  516.     public function getMemberships()
  517.     {
  518.         return $this->memberships;
  519.     }
  520.     /**
  521.      * Clear memberships
  522.      */
  523.     public function clearMemberships()
  524.     {
  525.         foreach ($this->getMemberships()->getKeys() as $key) {
  526.             $this->getMemberships()->remove($key);
  527.         }
  528.     }
  529.     /**
  530.      * Add categories
  531.      *
  532.      * @param \XLite\Model\Category $categories
  533.      */
  534.     public function addCategories(\XLite\Model\Category $categories)
  535.     {
  536.         $this->getCategories()->add($categories);
  537.     }
  538.     /**
  539.      * Get categories
  540.      *
  541.      * @return \Doctrine\Common\Collections\Collection
  542.      */
  543.     public function getCategories()
  544.     {
  545.         return $this->categories;
  546.     }
  547.     /**
  548.      * Clear categories
  549.      */
  550.     public function clearCategories()
  551.     {
  552.         foreach ($this->getCategories()->getKeys() as $key) {
  553.             $this->getCategories()->remove($key);
  554.         }
  555.     }
  556.     /**
  557.      * Add sale discount products
  558.      *
  559.      * @param \CDev\Sale\Model\SaleDiscountProduct $saleDiscountProduct
  560.      */
  561.     public function addSaleDiscountProducts(\CDev\Sale\Model\SaleDiscountProduct $saleDiscountProduct)
  562.     {
  563.         $this->saleDiscountProducts[] = $saleDiscountProduct;
  564.     }
  565.     /**
  566.      * Get product ids if sale discount is specificProducts
  567.      *
  568.      * @return array
  569.      */
  570.     public function getApplicableProductIds()
  571.     {
  572.         $ids = [];
  573.         if ($this->isPersistent() && $this->getSpecificProducts()) {
  574.             $ids \XLite\Core\Database::getRepo('CDev\Sale\Model\SaleDiscountProduct')
  575.                 ->getSaleDiscountProductIds($this->getId());
  576.         }
  577.         return $ids;
  578.     }
  579.     /**
  580.      * Get sale products
  581.      *
  582.      * @return \Doctrine\Common\Collections\Collection
  583.      */
  584.     public function getSaleDiscountProducts()
  585.     {
  586.         return $this->saleDiscountProducts;
  587.     }
  588.     /**
  589.      * @return \Doctrine\Common\Collections\Collection
  590.      */
  591.     public function getCleanURLs()
  592.     {
  593.         return $this->cleanURLs;
  594.     }
  595.     /**
  596.      * Add cleanURLs
  597.      *
  598.      * @param \XLite\Model\CleanURL $cleanURLs
  599.      * @return SaleDiscount
  600.      */
  601.     public function addCleanURLs(\XLite\Model\CleanURL $cleanURLs)
  602.     {
  603.         $this->cleanURLs[] = $cleanURLs;
  604.         return $this;
  605.     }
  606.     // {{{ Translation Getters / setters
  607.     /**
  608.      * @return string
  609.      */
  610.     public function getMetaTags()
  611.     {
  612.         return $this->getTranslationField(__FUNCTION__);
  613.     }
  614.     /**
  615.      * @param string $metaTags
  616.      *
  617.      * @return \XLite\Model\Base\Translation
  618.      */
  619.     public function setMetaTags($metaTags)
  620.     {
  621.         return $this->setTranslationField(__FUNCTION__$metaTags);
  622.     }
  623.     /**
  624.      * @param string $metaDesc
  625.      *
  626.      * @return \XLite\Model\Base\Translation
  627.      */
  628.     public function setMetaDesc($metaDesc)
  629.     {
  630.         return $this->setTranslationField(__FUNCTION__$metaDesc);
  631.     }
  632.     /**
  633.      * @return string
  634.      */
  635.     public function getMetaTitle()
  636.     {
  637.         return $this->getTranslationField(__FUNCTION__);
  638.     }
  639.     /**
  640.      * @param string $metaTitle
  641.      *
  642.      * @return \XLite\Model\Base\Translation
  643.      */
  644.     public function setMetaTitle($metaTitle)
  645.     {
  646.         return $this->setTranslationField(__FUNCTION__$metaTitle);
  647.     }
  648.     // }}}
  649. }