modules/RedSqui/GiftCertificates/src/Model/GiftCerts.php line 53

Open in your IDE?
  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4.  * X-Cart
  5.  *
  6.  * NOTICE OF LICENSE
  7.  *
  8.  * This source file is subject to the software license agreement
  9.  * that is bundled with this package in the file LICENSE.txt.
  10.  * It is also available through the world-wide-web at this URL:
  11.  * http://www.litecommerce.com/module-eula.html
  12.  * If you did not receive a copy of the license and are unable to
  13.  * obtain it through the world-wide-web, please send an email
  14.  * to redsqui@gmail.com so we can send you a copy immediately.
  15.  *
  16.  * DISCLAIMER
  17.  *
  18.  * Do not modify this file if you wish to upgrade X-Cart to newer versions
  19.  * in the future. If you wish to customize X-Cart for your needs please
  20.  * refer to http://www.x-cart.com/ for more information.
  21.  *
  22.  *
  23.  * @category  X-Cart Next
  24.  * @author    Vladimir Serov <redsqui@gmail.com>
  25.  * @copyright Copyright (c) 2012-present Vladimir Serov <redsqui@gmail.com>. All rights reserved
  26.  * @license   http://www.litecommerce.com/module-eula.html LiteCommerce Module License Agreement
  27.  */
  28. namespace RedSqui\GiftCertificates\Model;
  29. use Doctrine\ORM\Mapping as ORM;
  30. use Doctrine\Common\Collections\ArrayCollection;
  31. use XLite\Core\Config;
  32. use XLite\Core\Database;
  33. use XLite\Model\TaxClass;
  34. use RedSqui\GiftCertificates\View\FormField\Select\CardTypesMenu;
  35. use RedSqui\GiftCertificates\Model\UseGiftCard;
  36. /**
  37.  * Gift Certificate model
  38.  *
  39.  * @ORM\Entity (repositoryClass="\RedSqui\GiftCertificates\Model\Repo\GiftCerts")
  40.  * @ORM\Table  (name="gift_certificates",
  41.  *           indexes={
  42.  *           @ORM\Index (name="status", columns={"status"}),
  43.  *           @ORM\Index (name="add_date", columns={"add_date"}),
  44.  *           @ORM\Index (name="gcid", columns={"gcid"})
  45.  *           }
  46.  * )
  47.  * @ORM\HasLifecycleCallbacks
  48.  */
  49. class GiftCerts extends \XLite\Model\AEntity implements \XLite\Model\Base\IOrderItem
  50. {
  51.     /*
  52.      * Gift card statuses
  53.      */
  54.     public const STATUS_ACTIVE        'A';
  55.     public const STATUS_NOT_PAYED_YET 'N';
  56.     public const STATUS_REDEEMED      'R';
  57.     public const STATUS_EXPIRED       'E';
  58.     /*
  59.      * Gift card types
  60.      */
  61.     public const TYPE_VIRTUAL  'V';
  62.     public const TYPE_TANGIBLE 'T';
  63.     /*
  64.      * Fields length limits
  65.      */
  66.     public const LENGTH_LIMIT_ADDRESS 550;
  67.     public const LENGTH_LIMIT_NAME 128;
  68.     public const LENGTH_LIMIT_MESSAGE 300;
  69.     public const LENGTH_LIMIT_SIGNATURE 128;
  70.     /*
  71.      * template card code
  72.      *
  73.      */
  74.     public const TEMPLATE_GCID '/^[A-Z0-9\-]+$/';
  75.     /**
  76.      * Field const for sku
  77.      *
  78.      */
  79.     public const GC_SKU_PREFIX "gc-";
  80.     /**
  81.      * Validation fields
  82.      *
  83.      * @var array
  84.      */
  85.     protected static $validationFields = [
  86.         'gcid'              => 'Gcid',
  87.         'recipient_email'   => 'RecipientEmail',
  88.         'recipient_address' => 'RecipientAddress',
  89.         'recipient_name'    => 'RecipientName',
  90.         'sender_signature'  => 'SenderSignature',
  91.         'amount'            => 'Amount',
  92.         'message'           => 'Message',
  93.         'delivery_date'     => 'DeliveryDate',
  94.     ];
  95.     /**
  96.      * Consecutive number
  97.      *
  98.      * @ORM\Id
  99.      * @ORM\GeneratedValue (strategy="AUTO")
  100.      * @ORM\Column         (type="integer")
  101.      */
  102.     protected $card_id;
  103.     /**
  104.      * Card code
  105.      *
  106.      * @var string
  107.      *
  108.      * @ORM\Column (type="string", length=255)
  109.      */
  110.     protected $gcid '';
  111.     /**
  112.      * Card type (Virtual, Tangible)
  113.      *
  114.      * @var string
  115.      *
  116.      * @ORM\Column (type="string", length=1)
  117.      */
  118.     protected $card_type 'V';
  119.     /**
  120.      * Is card available or not
  121.      *
  122.      * @var boolean
  123.      *
  124.      * @ORM\Column (type="boolean")
  125.      */
  126.     protected $enabled true;
  127.     /**
  128.      * Buy in order
  129.      *
  130.      * @var \XLite\Model\Order
  131.      *
  132.      * @ORM\ManyToOne  (targetEntity="XLite\Model\Order", cascade={"persist"})
  133.      * @ORM\JoinColumn (name="order_id", referencedColumnName="order_id", onDelete="SET NULL")
  134.      */
  135.     protected $order;
  136.     /**
  137.      * Order item where buy card
  138.      *
  139.      * @var \XLite\Model\OrderItem
  140.      *
  141.      * @ORM\OneToOne  (targetEntity="XLite\Model\OrderItem", inversedBy="gift_card", cascade={"persist"})
  142.      * @ORM\JoinColumn (name="order_item_id", referencedColumnName="item_id", onDelete="SET NULL")
  143.      */
  144.     protected $order_item;
  145.     /**
  146.      * recipient
  147.      *
  148.      * @var string
  149.      *
  150.      * @ORM\Column (type="string", length=64)
  151.      */
  152.     protected $recipient_email '';
  153.     /**
  154.      * recipient_address
  155.      *
  156.      * @var string
  157.      *
  158.      * @ORM\Column (type="string", length=550)
  159.      */
  160.     protected $recipient_address '';
  161.     /**
  162.      * recipient_name
  163.      *
  164.      * @var string
  165.      *
  166.      * @ORM\Column (type="string", length=128)
  167.      */
  168.     protected $recipient_name '';
  169.     /**
  170.      * message
  171.      *
  172.      * @var string
  173.      *
  174.      * @ORM\Column (type="string", length=300)
  175.      */
  176.     protected $message '';
  177.     /**
  178.      * sender_signature
  179.      *
  180.      * @var string
  181.      *
  182.      * @ORM\Column (type="string", length=128)
  183.      */
  184.     protected $sender_signature '';
  185.     /**
  186.      * amount
  187.      *
  188.      * @var float
  189.      *
  190.      * @ORM\Column (type="decimal", precision=14, scale=4)
  191.      */
  192.     protected $amount 0.0000;
  193.     /**
  194.      * balance
  195.      *
  196.      * @var float
  197.      *
  198.      * @ORM\Column (type="decimal", precision=14, scale=4)
  199.      */
  200.     protected $balance 0.0000;
  201.     /**
  202.      * status (Not payment yet, active, Redeemed)
  203.      *
  204.      * @var string
  205.      *
  206.      * @ORM\Column (type="string", length=1)
  207.      */
  208.     protected $status 'A';
  209.     /**
  210.      * add_date
  211.      *
  212.      * @var integer
  213.      *
  214.      * @ORM\Column (type="integer")
  215.      */
  216.     protected $add_date 0;
  217.     /**
  218.      * delivery date
  219.      *
  220.      * @var integer
  221.      *
  222.      * @ORM\Column (type="integer")
  223.      */
  224.     protected $delivery_date 0;
  225.     /**
  226.      * used
  227.      *
  228.      * @var boolean
  229.      *
  230.      * @ORM\Column (type="boolean")
  231.      */
  232.     protected $used 0;
  233.     /**
  234.      * Categories
  235.      *
  236.      * @var   ArrayCollection
  237.      *
  238.      * @ORM\ManyToMany (targetEntity="XLite\Model\Category", inversedBy="gift_certs")
  239.      * @ORM\JoinTable (name="gift_certificates_categories",
  240.      *      joinColumns={@ORM\JoinColumn (name="card_id", referencedColumnName="card_id", onDelete="CASCADE")},
  241.      *      inverseJoinColumns={@ORM\JoinColumn (name="category_id", referencedColumnName="category_id", onDelete="CASCADE")}
  242.      * )
  243.      */
  244.     protected $categories;
  245.     /**
  246.      * Expiration date
  247.      *
  248.      * @var   integer
  249.      *
  250.      * @ORM\Column (type="integer", options={ "unsigned": true })
  251.      */
  252.     protected $expiration_date 0;
  253.     /**
  254.      * Constructor
  255.      *
  256.      * @param array $data Entity properties OPTIONAL
  257.      */
  258.     public function __construct(array $data = [])
  259.     {
  260.         $this->categories = new ArrayCollection();
  261.         parent::__construct($data);
  262.     }
  263.     /**
  264.      * Check if gift card with this code is exist
  265.      *
  266.      * @param string $cardCode Gift card's code
  267.      *
  268.      * @return boolean
  269.      */
  270.     public static function isExistGiftCard($cardCode)
  271.     {
  272.         $card null;
  273.         $cardCode trim($cardCode);
  274.         $card Database::getRepo(static::class)
  275.             ->getCardByCode($cardCode);
  276.         return !empty($card);
  277.     }
  278.     protected static function getRedSquiGCConfig()
  279.     {
  280.         return Config::getInstance()->RedSqui->GiftCertificates;
  281.     }
  282.     /**
  283.      * Check is set option use available code
  284.      *
  285.      * @return boolean
  286.      */
  287.     public static function isUseAvailableCode()
  288.     {
  289.         return (self::getRedSquiGCConfig()->the_codes_for_tangible_cards == '0');
  290.     }
  291.     /**
  292.      * Check is set option use available code
  293.      *
  294.      * @return boolean
  295.      */
  296.     public static function isUseVirtualAvailableCode()
  297.     {
  298.         return (self::getRedSquiGCConfig()->the_codes_for_virtual_cards == '0');
  299.     }
  300.     /**
  301.      * Config show tangible card type
  302.      *
  303.      * @return boolean
  304.      */
  305.     public static function isShowTangible()
  306.     {
  307.         return self::getRedSquiGCConfig()->card_types != CardTypesMenu::TYPE_VIRTUAL;
  308.     }
  309.     /**
  310.      * Config show virtual card type
  311.      *
  312.      * @return boolean
  313.      */
  314.     public static function isShowVirtual()
  315.     {
  316.         return self::getRedSquiGCConfig()->card_types != CardTypesMenu::TYPE_TANGIBLE;
  317.     }
  318.     /**
  319.      * Check is set gift_message option in config
  320.      *
  321.      * @return boolean
  322.      */
  323.     public static function isShowMessage()
  324.     {
  325.         return self::getRedSquiGCConfig()->gift_message == 'Y';
  326.     }
  327.     /**
  328.      * Check is set delivery_date option in config
  329.      *
  330.      * @return boolean
  331.      */
  332.     public static function isShowDeliveryDate()
  333.     {
  334.         return self::getRedSquiGCConfig()->delivery_date == 'Y';
  335.     }
  336.     /**
  337.      * Check is exists available codes
  338.      *
  339.      * @return boolean
  340.      */
  341.     public static function isExistsAvailableCodes()
  342.     {
  343.         $result self::isShowTangible();
  344.         if ($result && self::isUseAvailableCode()) {
  345.             $code Database::getRepo(static::class)->getFirstAvailableCode();
  346.             $result $result && !empty($code);
  347.         }
  348.         return $result;
  349.     }
  350.     /**
  351.      * Validate fields
  352.      *
  353.      * @param array $values Value fields
  354.      * @param int $cardId excluding card
  355.      *
  356.      * @return string
  357.      */
  358.     public static function validateFields($values$cardId 0)
  359.     {
  360.         $errors '';
  361.         foreach ($values as $key => $value) {
  362.             if (isset(static::$validationFields[$key])) {
  363.                 $fValid 'valid' ucfirst(static::$validationFields[$key]);
  364.                 if ($fValid == 'validGcid') {
  365.                     $error = static::$fValid($valuefalse$cardId);
  366.                 } else {
  367.                     $error = static::$fValid($value);
  368.                 }
  369.                 if ($error) {
  370.                     $errors $errors $error '. ';
  371.                 }
  372.             }
  373.         }
  374.         return $errors;
  375.     }
  376.     /**
  377.      * Validate field "gcid"
  378.      *
  379.      * @param string $value Field value
  380.      *
  381.      * @return string
  382.      */
  383.     public static function validGcid($value$key false$cardId 0)
  384.     {
  385.         $error '';
  386.         $giftCardsRepo Database::getRepo(static::class);
  387.         if (!empty($value)) {
  388.             $codeLength strlen($value);
  389.             if (
  390.                 $codeLength $giftCardsRepo::PARAM_MIN_CODE_LENGTH
  391.                 && $codeLength $giftCardsRepo::PARAM_MAX_CODE_LENGTH
  392.             ) {
  393.                 $error = ($key === false)
  394.                     ? static::t('There is incorrect code')
  395.                     : static::t('There is incorrect code on X string', ['string' => $key 1]);
  396.             } elseif (preg_match(self::TEMPLATE_GCID$value)) {
  397.                 $isNoSameCode $giftCardsRepo->isNoSameCode($value$cardId);
  398.                 if (!$isNoSameCode) {
  399.                     $error = ($key === false)
  400.                         ? static::t('Code already exists')
  401.                         : static::t('Code already exists on X string', ['string' => $key 1]);
  402.                 }
  403.             } else {
  404.                 $error = ($key === false)
  405.                     ? static::t('There is incorrect code')
  406.                     : static::t('There is incorrect code on X string', ['string' => $key 1]);
  407.             }
  408.         }
  409.         return $error;
  410.     }
  411.     /**
  412.      * Validate field "recipient's email"
  413.      *
  414.      * @param string $value Field value
  415.      *
  416.      * @return string
  417.      */
  418.     protected static function validRecipientEmail($value)
  419.     {
  420.         $error '';
  421.         if (empty($value)) {
  422.             $error = static::t(
  423.                 'Field X is not defined or empty',
  424.                 ['name' => 'recipient\'s email']
  425.             );
  426.         } else {
  427.             $error = (filter_var($valueFILTER_VALIDATE_EMAIL) === false)
  428.                 ? static::t('Not an email address')
  429.                 : '';
  430.         }
  431.         return $error;
  432.     }
  433.     /**
  434.      * Validate field "recipient's address"
  435.      *
  436.      * @param string $value Field value
  437.      *
  438.      * @return string
  439.      */
  440.     protected static function validRecipientAddress($value)
  441.     {
  442.         $error '';
  443.         if (empty($value)) {
  444.             $error = static::t(
  445.                 'Field X is not defined or empty',
  446.                 ['name' => 'recipient\'s address']
  447.             );
  448.         } else {
  449.             $error = (strlen($value) > self::LENGTH_LIMIT_ADDRESS)
  450.                 ? static::t(
  451.                     'The field X should contain no more than Y characters',
  452.                     [
  453.                         'name'  => 'recipient\'s address',
  454.                         'count' => self::LENGTH_LIMIT_ADDRESS
  455.                     ]
  456.                 )
  457.                 : '';
  458.         }
  459.         return $error;
  460.     }
  461.     /**
  462.      * Validate field "amount"
  463.      *
  464.      * @param string $value Field value
  465.      *
  466.      * @return string
  467.      */
  468.     protected static function validAmount($value)
  469.     {
  470.         $error '';
  471.         if (empty($value)) {
  472.             $error = static::t(
  473.                 'Field X is not defined or empty',
  474.                 ['name' => 'amount']
  475.             );
  476.         } elseif (!is_numeric($value)) {
  477.             $error = static::t(
  478.                 'Field X must be numeric',
  479.                 ['name' => 'amount']
  480.             );
  481.         } else {
  482.             $cardAmount self::getRedSquiGCConfig()->card_amount;
  483.             if ($cardAmount == '1') {
  484.                 //open_amount
  485.                 $range self::getRedSquiGCConfig()->range;
  486.                 if ($range[0] > $value || $value $range[1]) {
  487.                     $error = static::t(
  488.                         'Value of the field X should be in the range [Y, Z]',
  489.                         [
  490.                             'name'        => 'amount',
  491.                             'range_left'  => $range[0],
  492.                             'range_right' => $range[1]
  493.                         ]
  494.                     );
  495.                 }
  496.             } else {
  497.                 $amountOptions self::getRedSquiGCConfig()->amount_options;
  498.                 if (!in_array($value$amountOptions)) {
  499.                     $error = static::t(
  500.                         'Incorrect value of the field X',
  501.                         ['name' => 'amount']
  502.                     );
  503.                 }
  504.             }
  505.         }
  506.         return $error;
  507.     }
  508.     /**
  509.      * Validate field "message"
  510.      *
  511.      * @param string $value Field value
  512.      *
  513.      * @return string
  514.      */
  515.     protected static function validMessage($value)
  516.     {
  517.         $error '';
  518.         if (!empty($value)) {
  519.             if (self::LENGTH_LIMIT_MESSAGE strlen($value)) {
  520.                 $error = static::t(
  521.                     'The field X should contain no more than Y characters',
  522.                     ['name' => 'message''count' => self::LENGTH_LIMIT_MESSAGE]
  523.                 );
  524.             }
  525.         }
  526.         return $error;
  527.     }
  528.     /**
  529.      * Validate field "recipient's name"
  530.      *
  531.      * @param string $value Field value
  532.      *
  533.      * @return string
  534.      */
  535.     protected static function validRecipientName($value)
  536.     {
  537.         $error '';
  538.         if (!empty($value)) {
  539.             if (self::LENGTH_LIMIT_NAME strlen($value)) {
  540.                 $error = static::t(
  541.                     'The field X should contain no more than Y characters',
  542.                     [
  543.                         'name' => 'recipient\'s name',
  544.                         'count' => self::LENGTH_LIMIT_NAME
  545.                     ]
  546.                 );
  547.             }
  548.         }
  549.         return $error;
  550.     }
  551.     /**
  552.      * Validate field "SenderSignature"
  553.      *
  554.      * @param string $value Field value
  555.      *
  556.      * @return string
  557.      */
  558.     protected static function validSenderSignature($value)
  559.     {
  560.         $error '';
  561.         if (!empty($value)) {
  562.             if (self::LENGTH_LIMIT_SIGNATURE strlen($value)) {
  563.                 $error = static::t(
  564.                     'The field X should contain no more than Y characters',
  565.                     [
  566.                         'name' => 'sender\'s signature',
  567.                         'count' => self::LENGTH_LIMIT_SIGNATURE
  568.                     ]
  569.                 );
  570.             }
  571.         }
  572.         return $error;
  573.     }
  574.     /**
  575.      * Validate field "Delivery date"
  576.      *
  577.      * @param string $value Field value
  578.      *
  579.      * @return string
  580.      */
  581.     protected static function validDeliveryDate($value)
  582.     {
  583.         $error '';
  584.         if ($value) {
  585.             $date strtotime(date('Y-m-d'));
  586.             if ($value $date) {
  587.                 $error = static::t('Delivery date shall be not earlier than the current date');
  588.             }
  589.         }
  590.         return $error;
  591.     }
  592.     /**
  593.      * Get gift card code for list cards
  594.      *
  595.      * @return string
  596.      */
  597.     public function getCode()
  598.     {
  599.         return ($this->getGcid())
  600.             ? $this->getGcid()
  601.             : static::t('Not setted');
  602.     }
  603.     /**
  604.      * Get value for 'created' field in card table
  605.      *
  606.      * @param boolean $withTime Date or datetime created OPTIONAL
  607.      *
  608.      * @return string
  609.      */
  610.     public function getCreated($withTime false)
  611.     {
  612.         return ($withTime)
  613.             ? date('M d, Y, H:i'$this->getAddDate())
  614.             : date('M d, Y'$this->getAddDate());
  615.     }
  616.     /**
  617.      * Get order's number for 'created' field in card table
  618.      *
  619.      * @return integer|null
  620.      */
  621.     public function getNumberCreatedOrder()
  622.     {
  623.         $order $this->getOrder();
  624.         return (!empty($order))
  625.             ? $order->getOrderNumber()
  626.             : null;
  627.     }
  628.     /**
  629.      * Get format gift card balance
  630.      *
  631.      * @return string
  632.      */
  633.     public function getFormatBalance()
  634.     {
  635.         $currency $this->getCurrency();
  636.         $balance $currency->formatParts($this->getBalance());
  637.         return implode(''$balance);
  638.     }
  639.     /**
  640.      * Get use in orders
  641.      *
  642.      * @return array
  643.      */
  644.     public function getUsedIn()
  645.     {
  646.         $records Database::getRepo(UseGiftCard::class)
  647.             ->findByParams(['card' => $this->getCardId()]);
  648.         $ordersId = [];
  649.         if ($records) {
  650.             foreach ($records as $record) {
  651.                 $ordersId[] = $record->getOrder()->getOrderNumber();
  652.             }
  653.         }
  654.         return $ordersId;
  655.     }
  656.     /**
  657.      * Get gift card status by user role
  658.      *
  659.      * @return string
  660.      */
  661.     public function getStatus()
  662.     {
  663.         $status '';
  664.         if ($this->getEnabled()) {
  665.             $status = (!\XLite::isAdminZone() && $this->status == self::STATUS_NOT_PAYED_YET)
  666.                 ? static::t('Inactive')
  667.                 : $this->getStatusFull();
  668.         } else {
  669.             $status = (\XLite::isAdminZone())
  670.                 ? $this->getStatusFull()
  671.                 : static::t('Inactive');
  672.         }
  673.         return $status;
  674.     }
  675.     /**
  676.      * Get gift card type
  677.      *
  678.      * @return string
  679.      */
  680.     public function getFullType()
  681.     {
  682.         $type '';
  683.         if ($this->isVirtual()) {
  684.             $type = static::t('Virtual');
  685.         } else {
  686.             $type = ($this->isTangible())
  687.                 ? static::t('Tangible')
  688.                 : '';
  689.         }
  690.         return $type;
  691.     }
  692.     /**
  693.      * Check if gift card is tangible
  694.      *
  695.      * @return boolean
  696.      */
  697.     public function isTangible()
  698.     {
  699.         return ($this->getCardType() == self::TYPE_TANGIBLE);
  700.     }
  701.     /**
  702.      * Check if gift card is virtual
  703.      *
  704.      * @return boolean
  705.      */
  706.     public function isVirtual()
  707.     {
  708.         return ($this->getCardType() == self::TYPE_VIRTUAL);
  709.     }
  710.     /**
  711.      * Check gift card for use in order
  712.      *
  713.      * @return boolean
  714.      */
  715.     public function checkAvailability()
  716.     {
  717.         return $this->getEnabled()
  718.             && $this->isActive()
  719.             && !$this->checkExpired();
  720.     }
  721.     /**
  722.      * Check if card has status "active"
  723.      *
  724.      * @return boolean
  725.      */
  726.     public function isActive()
  727.     {
  728.         return $this->status == self::STATUS_ACTIVE;
  729.     }
  730.     /**
  731.      * Check if card has status "redeemed"
  732.      *
  733.      * @return boolean
  734.      */
  735.     public function isRedeemed()
  736.     {
  737.         return $this->status == self::STATUS_REDEEMED;
  738.     }
  739.     /**
  740.      * Check if card has status "not payed yet"
  741.      *
  742.      * @return boolean
  743.      */
  744.     public function isNotPayedYet()
  745.     {
  746.         return $this->status == self::STATUS_NOT_PAYED_YET;
  747.     }
  748.     /**
  749.      * Check if card has status "expired"
  750.      *
  751.      * @return boolean
  752.      */
  753.     public function isExpired()
  754.     {
  755.         return $this->status == self::STATUS_EXPIRED;
  756.     }
  757.     /**
  758.      * Use card in order
  759.      *
  760.      * @return boolean
  761.      */
  762.     public function useAmount($cost)
  763.     {
  764.         $isSuccess true;
  765.         $newBalance $this->getBalance() - $cost;
  766.         if ($newBalance 0) {
  767.             $isSuccess false;
  768.         } else {
  769.             $this->setBalance($newBalance);
  770.             if ($this->getBalance() == 0) {
  771.                 $this->setStatus(self::STATUS_REDEEMED);
  772.             }
  773.             Database::getEM()->persist($this);
  774.             Database::getEM()->flush();
  775.         }
  776.         return $isSuccess;
  777.     }
  778.     /**
  779.      * Return amount for card in order
  780.      *
  781.      * @return boolean
  782.      */
  783.     public function returnAmount($cost)
  784.     {
  785.         $isSuccess true;
  786.         $newBalance $this->getBalance() + $cost;
  787.         if ($newBalance $this->getAmount() || $newBalance) {
  788.             $isSuccess false;
  789.         } else {
  790.             $this->setBalance($newBalance);
  791.             if ($this->isRedeemed()) {
  792.                 $this->setStatus(self::STATUS_ACTIVE);
  793.             }
  794.             Database::getEM()->persist($this);
  795.             Database::getEM()->flush();
  796.         }
  797.         return $isSuccess;
  798.     }
  799.     /**
  800.      * Since Doctrine lifecycle callbacks do not allow to modify associations, we've added this method
  801.      *
  802.      * @param string $type Type of current operation
  803.      *
  804.      * @return void
  805.      */
  806.     public function prepareEntityBeforeCommit($type)
  807.     {
  808.         if ($type == self::ACTION_INSERT) {
  809.             $this->setBalance($this->getAmount());
  810.             $this->setAddDate(time());
  811.             if (!\XLite::isAdminZone()) {
  812.                 $this->setUsed(1);
  813.             }
  814.         } elseif ($type == self::ACTION_UPDATE) {
  815.             if (\XLite::isAdminZone()) {
  816.                 if ($this->getUsed() == 0) {
  817.                     $this->setUsed(1);
  818.                     $this->setAddDate(time());
  819.                     $this->setBalance($this->getAmount());
  820.                 }
  821.             } else {
  822.                 $this->setBalance($this->getAmount());
  823.             }
  824.         }
  825.     }
  826.     /**
  827.      * Get recipient address with <br/>
  828.      *
  829.      * @return string
  830.      */
  831.     public function getRecipientAddressBreak()
  832.     {
  833.         $address $this->getRecipientAddress();
  834.         if ($address) {
  835.             $explSymbols '';
  836.             if (strpos($address"\r") !== false) {
  837.                 $explSymbols $explSymbols "\r";
  838.             }
  839.             if (strpos($address"\n") !== false) {
  840.                 $explSymbols $explSymbols "\n";
  841.             }
  842.             if ($explSymbols) {
  843.                 $address str_replace($explSymbols'<br/>'$address);
  844.             }
  845.         }
  846.         return $address;
  847.     }
  848.     /**
  849.      * Check if exist gift_message
  850.      *
  851.      * @return boolean
  852.      */
  853.     public function isExistMessage()
  854.     {
  855.         return $this->getMessage()
  856.             || $this->getRecipientName()
  857.             || $this->getSenderSignature();
  858.     }
  859.     /**
  860.      * 'Type' card will be sent to 'person'
  861.      *
  862.      * @return string
  863.      */
  864.     public function getTypeInfo()
  865.     {
  866.         $typeMessage 'card will be sent to X';
  867.         $sentTo '';
  868.         if ($this->isSentToFriend()) {
  869.             $sentTo = static::t('your friend');
  870.         } else {
  871.             $sentTo = ($this->isTangible())
  872.                 ? static::t('your address')
  873.                 : static::t('your e-mail');
  874.         }
  875.         return $this->getFullType() . ' ' . static::t($typeMessage, ['whom' => $sentTo]);
  876.     }
  877.     /**
  878.      * Check if card will be sent to friend
  879.      *
  880.      * @return boolean
  881.      */
  882.     public function isSentToFriend()
  883.     {
  884.         return ($this->isTangible())
  885.             ? ((bool)$this->getRecipientAddress())
  886.             : ((bool)$this->getRecipientEmail());
  887.     }
  888.     /************ IOrderItem *************/
  889.     /**
  890.      * Get free shipping flag
  891.      *
  892.      * @return boolean
  893.      */
  894.     public function getFreeShipping()
  895.     {
  896.         return true;
  897.     }
  898.     /**
  899.      * Get image
  900.      *
  901.      * @return \XLite\Model\Base\Image|null
  902.      */
  903.     public function getImage()
  904.     {
  905.         return null;
  906.     }
  907.     /**
  908.      * Get image URL
  909.      *
  910.      * @return string
  911.      */
  912.     public function getImageURL()
  913.     {
  914.         return \XLite\Core\Layout::getInstance()->getResourceWebPath(
  915.             'modules/RedSqui/GiftCertificates/img/gift_card_photo_small.png',
  916.             \XLite\Core\Layout::WEB_PATH_OUTPUT_URL,
  917.             \XLite::INTERFACE_WEB,
  918.             \XLite::ZONE_CUSTOMER
  919.         );
  920.     }
  921.     /**
  922.      * Get image URL
  923.      *
  924.      * @return string
  925.      */
  926.     public function getImagePath()
  927.     {
  928.         return \XLite\Core\Layout::getInstance()->getResourceFullPath(
  929.             'modules/RedSqui/GiftCertificates/img/gift_card_photo_small.png',
  930.             \XLite::INTERFACE_WEB,
  931.             \XLite::ZONE_CUSTOMER
  932.         );
  933.     }
  934.     /**
  935.      * @return string
  936.      */
  937.     public function getImageRelativeURL()
  938.     {
  939.         return \Includes\Utils\FileManager::getRelativePath($this->getImagePath(), LC_DIR_ROOT);
  940.     }
  941.     /**
  942.      * Maximal available amount
  943.      *
  944.      * @return integer
  945.      */
  946.     public function getMaxPurchaseLimit()
  947.     {
  948.         return 1;
  949.     }
  950.     /**
  951.      * Minimal available amount
  952.      *
  953.      * @return integer
  954.      */
  955.     public function getMinPurchaseLimit()
  956.     {
  957.         return 1;
  958.     }
  959.     /**
  960.      * Get name
  961.      *
  962.      * @return string
  963.      */
  964.     public function getName()
  965.     {
  966.         $currency $this->getCurrency();
  967.         $amount $currency->formatParts($this->getAmount());
  968.         $result = static::t('gift card (X)', ['amount' => implode(''$amount)]);
  969.         return $result;
  970.     }
  971.     /**
  972.      * Get currency symbol for card
  973.      *
  974.      * @return string
  975.      */
  976.     public function getCurrencySymbol()
  977.     {
  978.         return $this->getCurrency()->getSymbol();
  979.     }
  980.     /**
  981.      * Get currency for card
  982.      *
  983.      * @return \XLite\Model\Currency
  984.      */
  985.     public function getCurrency()
  986.     {
  987.         return ($this->getOrder())
  988.             ? $this->getOrder()->getCurrency()
  989.             : \XLite::getInstance()->getCurrency();
  990.     }
  991.     /**
  992.      * Get currency for card
  993.      *
  994.      * @return \XLite\Model\Currency
  995.      */
  996.     public function getEditCurrency()
  997.     {
  998.         return $this->getCurrency();
  999.     }
  1000.     /**
  1001.      * Get currency for card
  1002.      *
  1003.      * @return string
  1004.      */
  1005.     public function getEditCurrencySymbol()
  1006.     {
  1007.         return $this->getEditCurrency()->getSymbol();
  1008.     }
  1009.     /**
  1010.      * Get price: modules should never overwrite this method
  1011.      *
  1012.      * @return float
  1013.      */
  1014.     public function getPrice()
  1015.     {
  1016.         $price $this->getAmount();
  1017.         if ($this->isTangible()) {
  1018.             $price $price self::getRedSquiGCConfig()->shipping_surcharge;
  1019.         }
  1020.         return $price;
  1021.     }
  1022.     /**
  1023.      * Get SKU
  1024.      *
  1025.      * @return string
  1026.      */
  1027.     public function getSku()
  1028.     {
  1029.         return self::GC_SKU_PREFIX $this->getId();
  1030.     }
  1031.     /**
  1032.      * Get  Url
  1033.      *
  1034.      * @return string
  1035.      */
  1036.     public function getURL()
  1037.     {
  1038.         return ($this->getCardId())
  1039.             ? \XLite\Core\Converter::buildURL('gift_certs''', ['card_id' => $this->getCardId()])
  1040.             : null;
  1041.     }
  1042.     /**
  1043.      * getWeight
  1044.      *
  1045.      * @return float
  1046.      */
  1047.     public function getWeight()
  1048.     {
  1049.         return 0;
  1050.     }
  1051.     /**
  1052.      * Get object unique id
  1053.      *
  1054.      * @return integer
  1055.      */
  1056.     public function getId()
  1057.     {
  1058.         return $this->getCardId();
  1059.     }
  1060.     /**
  1061.      * Check if card is accessible (for order)
  1062.      *
  1063.      * @return boolean
  1064.      */
  1065.     public function isAvailable()
  1066.     {
  1067.         return true;
  1068.     }
  1069.     /**
  1070.      * Price not modificators
  1071.      *
  1072.      * @return float
  1073.      */
  1074.     public function getNetPrice()
  1075.     {
  1076.         return $this->getAmount();
  1077.     }
  1078.     /**
  1079.      * Get taxable basis
  1080.      *
  1081.      * @return float
  1082.      */
  1083.     public function getTaxableBasis()
  1084.     {
  1085.         return $this->getNetPrice();
  1086.     }
  1087.     /**
  1088.      * Price not modificators
  1089.      *
  1090.      * @return float
  1091.      */
  1092.     public function getDisplayPrice()
  1093.     {
  1094.         return $this->getAmount();
  1095.     }
  1096.     /**
  1097.      * Price not modificators
  1098.      *
  1099.      * @return float
  1100.      */
  1101.     public function getClearPrice()
  1102.     {
  1103.         return $this->getAmount();
  1104.     }
  1105.     /**
  1106.      * Get memberships
  1107.      *
  1108.      * @return ArrayCollection
  1109.      */
  1110.     public function getMemberships()
  1111.     {
  1112.         $result = new ArrayCollection();
  1113.         return $result;
  1114.     }
  1115.     /**************************/
  1116.     /**
  1117.      * Get gift card status
  1118.      *
  1119.      * @return string
  1120.      */
  1121.     protected function getStatusFull()
  1122.     {
  1123.         switch ($this->status) {
  1124.             case self::STATUS_ACTIVE:
  1125.                 $status = static::t('Active');
  1126.                 break;
  1127.             case self::STATUS_REDEEMED:
  1128.                 $status = static::t('Redeemed');
  1129.                 break;
  1130.             case self::STATUS_NOT_PAYED_YET:
  1131.                 $status = static::t('Not payed yet');
  1132.                 break;
  1133.             case self::STATUS_EXPIRED:
  1134.                 $status = static::t('Expired');
  1135.                 break;
  1136.             default:
  1137.                 $status '';
  1138.                 break;
  1139.         }
  1140.         return $status;
  1141.     }
  1142.     /**
  1143.      * Get card_id
  1144.      *
  1145.      * @return int
  1146.      */
  1147.     public function getCardId()
  1148.     {
  1149.         return $this->card_id;
  1150.     }
  1151.     /**
  1152.      * Set gcid
  1153.      *
  1154.      * @param string $gcid
  1155.      *
  1156.      * @return GiftCerts
  1157.      */
  1158.     public function setGcid($gcid)
  1159.     {
  1160.         $this->gcid $gcid;
  1161.         return $this;
  1162.     }
  1163.     /**
  1164.      * Get gcid
  1165.      *
  1166.      * @return string
  1167.      */
  1168.     public function getGcid()
  1169.     {
  1170.         return $this->gcid;
  1171.     }
  1172.     /**
  1173.      * Set card_type
  1174.      *
  1175.      * @param string $cardType
  1176.      *
  1177.      * @return GiftCerts
  1178.      */
  1179.     public function setCardType($cardType)
  1180.     {
  1181.         $this->card_type $cardType;
  1182.         return $this;
  1183.     }
  1184.     /**
  1185.      * Get card_type
  1186.      *
  1187.      * @return string
  1188.      */
  1189.     public function getCardType()
  1190.     {
  1191.         return $this->card_type;
  1192.     }
  1193.     /**
  1194.      * Set enabled
  1195.      *
  1196.      * @param boolean $enabled
  1197.      *
  1198.      * @return GiftCerts
  1199.      */
  1200.     public function setEnabled($enabled)
  1201.     {
  1202.         $this->enabled $enabled;
  1203.         return $this;
  1204.     }
  1205.     /**
  1206.      * Get enabled
  1207.      *
  1208.      * @return boolean
  1209.      */
  1210.     public function getEnabled()
  1211.     {
  1212.         return $this->enabled;
  1213.     }
  1214.     /**
  1215.      * Set recipient_email
  1216.      *
  1217.      * @param string $recipientEmail
  1218.      *
  1219.      * @return GiftCerts
  1220.      */
  1221.     public function setRecipientEmail($recipientEmail)
  1222.     {
  1223.         $this->recipient_email $recipientEmail;
  1224.         return $this;
  1225.     }
  1226.     /**
  1227.      * Get recipient_email
  1228.      *
  1229.      * @return string
  1230.      */
  1231.     public function getRecipientEmail()
  1232.     {
  1233.         return $this->recipient_email;
  1234.     }
  1235.     /**
  1236.      * Set recipient_address
  1237.      *
  1238.      * @param string $recipientAddress
  1239.      *
  1240.      * @return GiftCerts
  1241.      */
  1242.     public function setRecipientAddress($recipientAddress)
  1243.     {
  1244.         $this->recipient_address $recipientAddress;
  1245.         return $this;
  1246.     }
  1247.     /**
  1248.      * Get recipient_address
  1249.      *
  1250.      * @return string
  1251.      */
  1252.     public function getRecipientAddress()
  1253.     {
  1254.         return $this->recipient_address;
  1255.     }
  1256.     /**
  1257.      * Set recipient_name
  1258.      *
  1259.      * @param string $recipientName
  1260.      *
  1261.      * @return GiftCerts
  1262.      */
  1263.     public function setRecipientName($recipientName)
  1264.     {
  1265.         $this->recipient_name $recipientName;
  1266.         return $this;
  1267.     }
  1268.     /**
  1269.      * Get recipient_name
  1270.      *
  1271.      * @return string
  1272.      */
  1273.     public function getRecipientName()
  1274.     {
  1275.         return $this->recipient_name;
  1276.     }
  1277.     /**
  1278.      * Set message
  1279.      *
  1280.      * @param string $message
  1281.      *
  1282.      * @return GiftCerts
  1283.      */
  1284.     public function setMessage($message)
  1285.     {
  1286.         $this->message $message;
  1287.         return $this;
  1288.     }
  1289.     /**
  1290.      * Get message
  1291.      *
  1292.      * @return string
  1293.      */
  1294.     public function getMessage()
  1295.     {
  1296.         return $this->message;
  1297.     }
  1298.     /**
  1299.      * Set sender_signature
  1300.      *
  1301.      * @param string $senderSignature
  1302.      *
  1303.      * @return GiftCerts
  1304.      */
  1305.     public function setSenderSignature($senderSignature)
  1306.     {
  1307.         $this->sender_signature $senderSignature;
  1308.         return $this;
  1309.     }
  1310.     /**
  1311.      * Get sender_signature
  1312.      *
  1313.      * @return string
  1314.      */
  1315.     public function getSenderSignature()
  1316.     {
  1317.         return $this->sender_signature;
  1318.     }
  1319.     /**
  1320.      * Set amount
  1321.      *
  1322.      * @param float $amount
  1323.      *
  1324.      * @return GiftCerts
  1325.      */
  1326.     public function setAmount($amount)
  1327.     {
  1328.         $this->amount $amount;
  1329.         return $this;
  1330.     }
  1331.     /**
  1332.      * Get amount
  1333.      *
  1334.      * @return float
  1335.      */
  1336.     public function getAmount()
  1337.     {
  1338.         return $this->amount;
  1339.     }
  1340.     /**
  1341.      * Get amount
  1342.      *
  1343.      * @return float
  1344.      */
  1345.     public function getEditAmount()
  1346.     {
  1347.         return $this->getAmount();
  1348.     }
  1349.     /**
  1350.      * Set balance
  1351.      *
  1352.      * @param float $balance
  1353.      *
  1354.      * @return GiftCerts
  1355.      */
  1356.     public function setBalance($balance)
  1357.     {
  1358.         $this->balance $balance;
  1359.         return $this;
  1360.     }
  1361.     /**
  1362.      * Get balance
  1363.      *
  1364.      * @return float
  1365.      */
  1366.     public function getBalance()
  1367.     {
  1368.         return $this->balance;
  1369.     }
  1370.     /**
  1371.      * Set status
  1372.      *
  1373.      * @param string $status
  1374.      *
  1375.      * @return GiftCerts
  1376.      */
  1377.     public function setStatus($status)
  1378.     {
  1379.         $this->status $status;
  1380.         return $this;
  1381.     }
  1382.     /**
  1383.      * Set add_date
  1384.      *
  1385.      * @param integer $addDate
  1386.      *
  1387.      * @return GiftCerts
  1388.      */
  1389.     public function setAddDate($addDate)
  1390.     {
  1391.         $this->add_date $addDate;
  1392.         return $this;
  1393.     }
  1394.     /**
  1395.      * Get add_date
  1396.      *
  1397.      * @return integer
  1398.      */
  1399.     public function getAddDate()
  1400.     {
  1401.         return $this->add_date;
  1402.     }
  1403.     /**
  1404.      * Set delivery_date
  1405.      *
  1406.      * @param integer $deliveryDate
  1407.      *
  1408.      * @return GiftCerts
  1409.      */
  1410.     public function setDeliveryDate($deliveryDate)
  1411.     {
  1412.         $this->delivery_date $deliveryDate;
  1413.         return $this;
  1414.     }
  1415.     /**
  1416.      * Get delivery_date
  1417.      *
  1418.      * @return integer
  1419.      */
  1420.     public function getDeliveryDate()
  1421.     {
  1422.         return $this->delivery_date;
  1423.     }
  1424.     /**
  1425.      * Set used
  1426.      *
  1427.      * @param boolean $used
  1428.      *
  1429.      * @return GiftCerts
  1430.      */
  1431.     public function setUsed($used)
  1432.     {
  1433.         $this->used $used;
  1434.         return $this;
  1435.     }
  1436.     /**
  1437.      * Get used
  1438.      *
  1439.      * @return boolean
  1440.      */
  1441.     public function getUsed()
  1442.     {
  1443.         return $this->used;
  1444.     }
  1445.     /**
  1446.      * Set expiration_date
  1447.      *
  1448.      * @param integer $expirationDate
  1449.      *
  1450.      * @return GiftCerts
  1451.      */
  1452.     public function setExpirationDate($expirationDate)
  1453.     {
  1454.         $this->expiration_date $expirationDate;
  1455.         return $this;
  1456.     }
  1457.     /**
  1458.      * Get expiration_date
  1459.      *
  1460.      * @return integer
  1461.      */
  1462.     public function getExpirationDate()
  1463.     {
  1464.         return $this->expiration_date;
  1465.     }
  1466.     /**
  1467.      * Set order
  1468.      *
  1469.      * @param \XLite\Model\Order $order
  1470.      *
  1471.      * @return GiftCerts
  1472.      */
  1473.     public function setOrder(\XLite\Model\Order $order null)
  1474.     {
  1475.         $this->order $order;
  1476.         return $this;
  1477.     }
  1478.     /**
  1479.      * Get order
  1480.      *
  1481.      * @return \XLite\Model\Order
  1482.      */
  1483.     public function getOrder()
  1484.     {
  1485.         return $this->order;
  1486.     }
  1487.     /**
  1488.      * Set order_item
  1489.      *
  1490.      * @param \XLite\Model\OrderItem $orderItem
  1491.      * @return GiftCerts
  1492.      */
  1493.     public function setOrderItem(\XLite\Model\OrderItem $orderItem null)
  1494.     {
  1495.         $this->order_item $orderItem;
  1496.         return $this;
  1497.     }
  1498.     /**
  1499.      * Get order_item
  1500.      *
  1501.      * @return \XLite\Model\OrderItem
  1502.      */
  1503.     public function getOrderItem()
  1504.     {
  1505.         return $this->order_item;
  1506.     }
  1507.     /**
  1508.      * Get coe tax class
  1509.      *
  1510.      * @return int
  1511.      */
  1512.     public function getCodeTaxClass()
  1513.     {
  1514.         $codeTaxClass = ($this->isVirtual())
  1515.             ? self::getRedSquiGCConfig()->gc_virtual_tax_class
  1516.             self::getRedSquiGCConfig()->gc_tangible_tax_class;
  1517.         return $codeTaxClass;
  1518.     }
  1519.     /**
  1520.      * getTaxClass
  1521.      *
  1522.      * @return TaxClass
  1523.      */
  1524.     public function getTaxClass()
  1525.     {
  1526.         $codeTaxClass $this->getCodeTaxClass();
  1527.         $taxClass Database::getRepo(TaxClass::class)->find($codeTaxClass);
  1528.         return $taxClass;
  1529.     }
  1530.     /**
  1531.      * Is certificate taxable
  1532.      *
  1533.      * @return bool
  1534.      */
  1535.     public function getTaxable()
  1536.     {
  1537.         $codeTaxClass $this->getCodeTaxClass();
  1538.         return (!empty($codeTaxClass) && $codeTaxClass > -2);
  1539.     }
  1540.     /**
  1541.      * Add categories
  1542.      *
  1543.      * @param \XLite\Model\Category $categories
  1544.      *
  1545.      * @return GiftCerts
  1546.      */
  1547.     public function addCategories(\XLite\Model\Category $categories)
  1548.     {
  1549.         $this->getCategories()->add($categories);
  1550.         return $this;
  1551.     }
  1552.     /**
  1553.      * Get categories
  1554.      *
  1555.      * @return \Doctrine\Common\Collections\Collection
  1556.      */
  1557.     public function getCategories()
  1558.     {
  1559.         return $this->categories;
  1560.     }
  1561.     /**
  1562.      * Clear categories
  1563.      */
  1564.     public function clearCategories()
  1565.     {
  1566.         foreach ($this->getCategories()->getKeys() as $key) {
  1567.             $this->getCategories()->remove($key);
  1568.         }
  1569.     }
  1570.     /**
  1571.      * Is gift cert valid for product
  1572.      *
  1573.      * @param \XLite\Model\Product $product Product
  1574.      *
  1575.      * @return boolean
  1576.      */
  1577.     public function isValidForProduct(\XLite\Model\Product $product)
  1578.     {
  1579.         $result true;
  1580.         if ($this->getCategories()->count()) {
  1581.             $result false;
  1582.             foreach ($product->getCategories() as $category) {
  1583.                 if ($this->getCategories()->contains($category)) {
  1584.                     $result true;
  1585.                     break;
  1586.                 }
  1587.             }
  1588.         }
  1589.         return $result;
  1590.     }
  1591.     /**
  1592.      * Check gift card category
  1593.      *
  1594.      * @param \XLite\Model\Order $order Order
  1595.      *
  1596.      * @return bool
  1597.      */
  1598.     public function checkCategory(\XLite\Model\Order $order)
  1599.     {
  1600.         $found true;
  1601.         if ($this->getCategories()->count()) {
  1602.             $found false;
  1603.             foreach ($order->getItems() as $item) {
  1604.                 foreach ($item->getProduct()->getCategories() as $category) {
  1605.                     if ($this->getCategories()->contains($category)) {
  1606.                         $found true;
  1607.                         break;
  1608.                     }
  1609.                 }
  1610.                 if ($found) {
  1611.                     break;
  1612.                 }
  1613.             }
  1614.         }
  1615.         return $found;
  1616.     }
  1617.     /**
  1618.      * Get items for gift card with categories
  1619.      *
  1620.      * @param \XLite\Model\Order $order Order
  1621.      *
  1622.      * @return \XLite\Model\OrderItem[]
  1623.      */
  1624.     public function getValidOrderItems(\XLite\Model\Order $order)
  1625.     {
  1626.         $items = [];
  1627.         foreach ($order->getItems() as $item) {
  1628.             if ($this->isValidForProduct($item->getProduct())) {
  1629.                 $items[] = $item;
  1630.             }
  1631.         }
  1632.         return $items;
  1633.     }
  1634.     /**
  1635.      * Get order total
  1636.      *
  1637.      * @param \XLite\Model\Order $order Order
  1638.      *
  1639.      * @return float
  1640.      */
  1641.     public function getOrderTotalForProducts(\XLite\Model\Order $order)
  1642.     {
  1643.         return array_reduce($this->getValidOrderItems($order), static function ($carry$item) {
  1644.             return $carry $item->getSubtotal();
  1645.         }, 0);
  1646.     }
  1647.     /**
  1648.      * Check - gift card is expired or not
  1649.      *
  1650.      * @return boolean
  1651.      */
  1652.     public function checkExpired()
  1653.     {
  1654.         $expirationDate $this->getExpirationDate();
  1655.         if (>= $expirationDate) {
  1656.             $expirationPeriod self::getRedSquiGCConfig()->gc_expiration_period_days;
  1657.             if ($expirationPeriod) {
  1658.                 $addDate $this->getAddDate();
  1659.                 $expirationDate $addDate $expirationPeriod 86400;
  1660.             }
  1661.         }
  1662.         $date getdate(\XLite\Core\Converter::time());
  1663.         $today mktime(000$date['mon'], $date['mday'], $date['year']);
  1664.         $expired $expirationDate && $expirationDate $today;
  1665.         if ($expired && $this->isActive()) {
  1666.             $this->setStatus(self::STATUS_EXPIRED);
  1667.         }
  1668.         if (!$expired && $this->isExpired()) {
  1669.             if ($this->getBalance() == 0) {
  1670.                 $this->setStatus(self::STATUS_REDEEMED);
  1671.             } else {
  1672.                 if (empty($this->getCode())) {
  1673.                     $this->setStatus(self::STATUS_NOT_PAYED_YET);
  1674.                 } else {
  1675.                     $this->setStatus(self::STATUS_ACTIVE);
  1676.                 }
  1677.             }
  1678.         }
  1679.         return $expired;
  1680.     }
  1681.     /**
  1682.      * get statuses
  1683.      *
  1684.      * @return array
  1685.      */
  1686.     public static function getListStatuses()
  1687.     {
  1688.         $result = [
  1689.             self::STATUS_ACTIVE        => static::t('Active'),
  1690.             self::STATUS_REDEEMED      => static::t('Redeemed'),
  1691.             self::STATUS_NOT_PAYED_YET => static::t('Not paid yet'),
  1692.             self::STATUS_EXPIRED       => static::t('Expired'),
  1693.         ];
  1694.         return $result;
  1695.     }
  1696.     /**
  1697.      * check if products from gift card categories is discounted all
  1698.      *
  1699.      * @param \XLite\Model\Order $order Order
  1700.      *
  1701.      * @return bool
  1702.      */
  1703.     public function isPaidItemsForGiftCard(\XLite\Model\Order $order)
  1704.     {
  1705.         $result false;
  1706.         $validItems $this->getValidOrderItems($order);
  1707.         if ($validItems) {
  1708.             $subtotal 0;
  1709.             foreach ($validItems as $key => $item) {
  1710.                 $subtotal += $item->getDiscountedSubtotal();
  1711.             }
  1712.             $result = ($subtotal <= 0);
  1713.         }
  1714.         return $result;
  1715.     }
  1716. }