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

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\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()
  101.             && $this->getType() === \CDev\Coupons\Model\Coupon::TYPE_PERCENT
  102.             && $this->getOrder()
  103.         ) {
  104.             $percent round($this->getValue() / $this->getOrder()->getSubtotal() * 1002);
  105.             $suffix sprintf('(%s%%)'$percent);
  106.         } elseif ($this->getCoupon()) {
  107.             return $this->getCoupon()->getPublicName();
  108.         }
  109.         return $this->getPublicCode() . ' ' $suffix;
  110.     }
  111.     /**
  112.      * Get actual code
  113.      *
  114.      * @return string
  115.      */
  116.     public function getActualCode()
  117.     {
  118.         return $this->getCoupon() ? $this->getCoupon()->getCode() : $this->getCode();
  119.     }
  120.     /**
  121.      * Check - coupon deleted or not
  122.      *
  123.      * @return boolean
  124.      */
  125.     public function isDeleted()
  126.     {
  127.         return !(bool)$this->getCoupon();
  128.     }
  129.     // }}}
  130.     // {{{ Processing
  131.     /**
  132.      * Mark as used
  133.      *
  134.      * @return void
  135.      */
  136.     public function markAsUsed()
  137.     {
  138.         if ($this->getCoupon()) {
  139.             $this->getCoupon()->setUses($this->getCoupon()->getUses() + 1);
  140.         }
  141.     }
  142.     /**
  143.      * Unmark as used
  144.      *
  145.      * @return void
  146.      */
  147.     public function unmarkAsUsed()
  148.     {
  149.         if ($this->getCoupon() && $this->getCoupon()->getUses()) {
  150.             $this->getCoupon()->setUses($this->getCoupon()->getUses() - 1);
  151.         }
  152.     }
  153.     // }}}
  154.     /**
  155.      * Get id
  156.      *
  157.      * @return integer
  158.      */
  159.     public function getId()
  160.     {
  161.         return $this->id;
  162.     }
  163.     /**
  164.      * Set code
  165.      *
  166.      * @param string $code
  167.      *
  168.      * @return UsedCoupon
  169.      */
  170.     public function setCode($code)
  171.     {
  172.         $this->code $code;
  173.         return $this;
  174.     }
  175.     /**
  176.      * Get code
  177.      *
  178.      * @return string
  179.      */
  180.     public function getCode()
  181.     {
  182.         return $this->code;
  183.     }
  184.     /**
  185.      * Set value
  186.      *
  187.      * @param float $value
  188.      *
  189.      * @return UsedCoupon
  190.      */
  191.     public function setValue($value)
  192.     {
  193.         $this->value $value;
  194.         return $this;
  195.     }
  196.     /**
  197.      * Get value
  198.      *
  199.      * @return float
  200.      */
  201.     public function getValue()
  202.     {
  203.         return $this->value;
  204.     }
  205.     /**
  206.      * Set order
  207.      *
  208.      * @param \XLite\Model\Order $order
  209.      *
  210.      * @return UsedCoupon
  211.      */
  212.     public function setOrder(\XLite\Model\Order $order null)
  213.     {
  214.         $this->order $order;
  215.         return $this;
  216.     }
  217.     /**
  218.      * Get order
  219.      *
  220.      * @return \XLite\Model\Order
  221.      */
  222.     public function getOrder()
  223.     {
  224.         return $this->order;
  225.     }
  226.     /**
  227.      * Get coupon
  228.      *
  229.      * @return \CDev\Coupons\Model\Coupon
  230.      */
  231.     public function getCoupon()
  232.     {
  233.         return $this->coupon;
  234.     }
  235.     /**
  236.      * Return Type
  237.      *
  238.      * @return string
  239.      */
  240.     public function getType()
  241.     {
  242.         return $this->type;
  243.     }
  244.     /**
  245.      * Set Type
  246.      *
  247.      * @param string $type
  248.      *
  249.      * @return $this
  250.      */
  251.     public function setType($type)
  252.     {
  253.         $this->type $type;
  254.         return $this;
  255.     }
  256.     public function isFreeShipping(): bool
  257.     {
  258.         return $this->getType() === Coupon::TYPE_FREESHIP;
  259.     }
  260. }