modules/CDev/Coupons/src/Model/UsedCoupon.php line 18

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\Coupons\Model;
  7. use Doctrine\ORM\Mapping as ORM;
  8. /**
  9.  * Used coupon
  10.  *
  11.  * @ORM\Entity
  12.  * @ORM\Table  (name="order_coupons")
  13.  */
  14. class UsedCoupon extends \XLite\Model\AEntity
  15. {
  16.     /**
  17.      * Product unique ID
  18.      *
  19.      * @var   integer
  20.      *
  21.      * @ORM\Id
  22.      * @ORM\GeneratedValue (strategy="AUTO")
  23.      * @ORM\Column         (type="integer", options={ "unsigned": true })
  24.      */
  25.     protected $id;
  26.     /**
  27.      * Code
  28.      *
  29.      * @var   string
  30.      *
  31.      * @ORM\Column (type="string", options={ "fixed": true }, length=16)
  32.      */
  33.     protected $code;
  34.     /**
  35.      * Value
  36.      *
  37.      * @var   float
  38.      *
  39.      * @ORM\Column (type="decimal", precision=14, scale=4)
  40.      */
  41.     protected $value 0.0000;
  42.     /**
  43.      * Order
  44.      *
  45.      * @var   \XLite\Model\Order
  46.      *
  47.      * @ORM\ManyToOne  (targetEntity="XLite\Model\Order", inversedBy="usedCoupons")
  48.      * @ORM\JoinColumn (name="order_id", referencedColumnName="order_id", onDelete="CASCADE")
  49.      */
  50.     protected $order;
  51.     /**
  52.      * Coupon
  53.      *
  54.      * @var   \CDev\Coupons\Model\Coupon
  55.      *
  56.      * @ORM\ManyToOne  (targetEntity="CDev\Coupons\Model\Coupon", inversedBy="usedCoupons", cascade={"persist"})
  57.      * @ORM\JoinColumn (name="coupon_id", referencedColumnName="id", onDelete="SET NULL")
  58.      */
  59.     protected $coupon;
  60.     /**
  61.      * Type
  62.      *
  63.      * @var   string
  64.      *
  65.      * @ORM\Column (type="string", options={ "fixed": true }, length=1, nullable=true)
  66.      */
  67.     protected $type;
  68.     // {{{ Getters / setters
  69.     /**
  70.      * setCoupon
  71.      *
  72.      * @param \CDev\Coupons\Model\Coupon $coupon ____param_comment____
  73.      *
  74.      * @return void
  75.      */
  76.     public function setCoupon(\CDev\Coupons\Model\Coupon $coupon)
  77.     {
  78.         $this->coupon $coupon;
  79.         $this->setCode($coupon->getCode());
  80.         $this->setType($coupon->getType());
  81.     }
  82.     /**
  83.      * Get public code (masked)
  84.      *
  85.      * @return string
  86.      */
  87.     public function getPublicCode()
  88.     {
  89.         return $this->getActualCode();
  90.     }
  91.     /**
  92.      * Get coupon public name
  93.      *
  94.      * @return string
  95.      */
  96.     public function getPublicName()
  97.     {
  98.         $suffix '';
  99.         if (
  100.             $this->getType() === \CDev\Coupons\Model\Coupon::TYPE_PERCENT
  101.             && $this->getOrder()?->getSubtotal()
  102.         ) {
  103.             $percent round($this->getValue() / $this->getOrder()->getSubtotal() * 1002);
  104.             $suffix sprintf('(%s%%)'$percent);
  105.         } elseif ($this->getCoupon()) {
  106.             return $this->getCoupon()->getPublicName();
  107.         }
  108.         return $this->getPublicCode() . ' ' $suffix;
  109.     }
  110.     /**
  111.      * Get actual code
  112.      *
  113.      * @return string
  114.      */
  115.     public function getActualCode()
  116.     {
  117.         return $this->getCoupon() ? $this->getCoupon()->getCode() : $this->getCode();
  118.     }
  119.     /**
  120.      * Check - coupon deleted or not
  121.      *
  122.      * @return boolean
  123.      */
  124.     public function isDeleted()
  125.     {
  126.         return !(bool)$this->getCoupon();
  127.     }
  128.     // }}}
  129.     // {{{ Processing
  130.     /**
  131.      * Mark as used
  132.      *
  133.      * @return void
  134.      */
  135.     public function markAsUsed()
  136.     {
  137.         if ($this->getCoupon()) {
  138.             $this->getCoupon()->setUses($this->getCoupon()->getUses() + 1);
  139.         }
  140.     }
  141.     /**
  142.      * Unmark as used
  143.      *
  144.      * @return void
  145.      */
  146.     public function unmarkAsUsed()
  147.     {
  148.         if ($this->getCoupon() && $this->getCoupon()->getUses()) {
  149.             $this->getCoupon()->setUses($this->getCoupon()->getUses() - 1);
  150.         }
  151.     }
  152.     // }}}
  153.     /**
  154.      * Get id
  155.      *
  156.      * @return integer
  157.      */
  158.     public function getId()
  159.     {
  160.         return $this->id;
  161.     }
  162.     /**
  163.      * Set code
  164.      *
  165.      * @param string $code
  166.      *
  167.      * @return UsedCoupon
  168.      */
  169.     public function setCode($code)
  170.     {
  171.         $this->code $code;
  172.         return $this;
  173.     }
  174.     /**
  175.      * Get code
  176.      *
  177.      * @return string
  178.      */
  179.     public function getCode()
  180.     {
  181.         return $this->code;
  182.     }
  183.     /**
  184.      * Set value
  185.      *
  186.      * @param float $value
  187.      *
  188.      * @return UsedCoupon
  189.      */
  190.     public function setValue($value)
  191.     {
  192.         $this->value $value;
  193.         return $this;
  194.     }
  195.     /**
  196.      * Get value
  197.      *
  198.      * @return float
  199.      */
  200.     public function getValue()
  201.     {
  202.         return $this->value;
  203.     }
  204.     /**
  205.      * Set order
  206.      *
  207.      * @param \XLite\Model\Order $order
  208.      *
  209.      * @return UsedCoupon
  210.      */
  211.     public function setOrder(\XLite\Model\Order $order null)
  212.     {
  213.         $this->order $order;
  214.         return $this;
  215.     }
  216.     /**
  217.      * Get order
  218.      *
  219.      * @return \XLite\Model\Order
  220.      */
  221.     public function getOrder()
  222.     {
  223.         return $this->order;
  224.     }
  225.     /**
  226.      * Get coupon
  227.      *
  228.      * @return \CDev\Coupons\Model\Coupon
  229.      */
  230.     public function getCoupon()
  231.     {
  232.         return $this->coupon;
  233.     }
  234.     /**
  235.      * Return Type
  236.      *
  237.      * @return string
  238.      */
  239.     public function getType()
  240.     {
  241.         return $this->type;
  242.     }
  243.     /**
  244.      * Set Type
  245.      *
  246.      * @param string $type
  247.      *
  248.      * @return $this
  249.      */
  250.     public function setType($type)
  251.     {
  252.         $this->type $type;
  253.         return $this;
  254.     }
  255.     public function isFreeShipping(): bool
  256.     {
  257.         return $this->getType() === Coupon::TYPE_FREESHIP;
  258.     }
  259. }