modules/XC/MultiCurrency/src/Model/ActiveCurrency.php line 22

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 XC\MultiCurrency\Model;
  7. use Doctrine\ORM\Mapping as ORM;
  8. /**
  9.  * @ORM\Entity
  10.  * @ORM\Table  (
  11.  *     name="active_currencies",
  12.  *     indexes={
  13.  *         @ORM\Index (name="position", columns={"position"}),
  14.  *         @ORM\Index (name="enabled", columns={"enabled"})
  15.  *     }
  16.  * )
  17.  */
  18. class ActiveCurrency extends \XLite\Model\AEntity
  19. {
  20.     /**
  21.      * @var int
  22.      *
  23.      * @ORM\Id
  24.      * @ORM\GeneratedValue (strategy="AUTO")
  25.      * @ORM\Column (type="integer")
  26.      */
  27.     protected $active_currency_id;
  28.     /**
  29.      * Currency rate
  30.      *
  31.      * @var float
  32.      *
  33.      * @ORM\Column (type="decimal", precision=14, scale=4)
  34.      */
  35.     protected $rate 1;
  36.     /**
  37.      * @var bool
  38.      *
  39.      * @ORM\Column (type="boolean")
  40.      */
  41.     protected $enabled true;
  42.     /**
  43.      * @var int
  44.      *
  45.      * @ORM\Column (type="integer")
  46.      */
  47.     protected $position 0;
  48.     /**
  49.      * Last rate update date
  50.      *
  51.      * @var int
  52.      *
  53.      * @ORM\Column (type="integer")
  54.      */
  55.     protected $rateDate 0;
  56.     /**
  57.      * @var \XLite\Model\Currency
  58.      *
  59.      * @ORM\OneToOne (targetEntity="XLite\Model\Currency", inversedBy="active_currency")
  60.      * @ORM\JoinColumn (name="currency_id", referencedColumnName="currency_id", onDelete="CASCADE")
  61.      */
  62.     protected $currency;
  63.     /**
  64.      * @var \XLite\Model\Country[]
  65.      *
  66.      * @ORM\OneToMany (targetEntity="XLite\Model\Country", mappedBy="active_currency")
  67.      */
  68.     protected $countries;
  69.     /**
  70.      * Default delimiter format
  71.      *
  72.      * @var array
  73.      */
  74.     protected $defaultDelimiterFormat = ['''.'];
  75.     /**
  76.      * Allowed thousands format
  77.      *
  78.      * @var array
  79.      */
  80.     protected $allowedThousandsDelimiter = [' ','.',',',''];
  81.     /**
  82.      * Allowed decimal delimiter
  83.      *
  84.      * @var array
  85.      */
  86.     protected $allowedDecimalDelimiter = ['.',','];
  87.     /**
  88.      * Update entity
  89.      *
  90.      * @return boolean
  91.      */
  92.     public function update()
  93.     {
  94.         $this->getCurrency()->update();
  95.         return parent::update();
  96.     }
  97.     /**
  98.      * Get rate
  99.      *
  100.      * @return integer
  101.      */
  102.     public function getRate()
  103.     {
  104.         return $this->rate;
  105.     }
  106.     /**
  107.      * Get currency name
  108.      *
  109.      * @return string
  110.      */
  111.     public function getName()
  112.     {
  113.         return $this->getCurrency()->getName();
  114.     }
  115.     /**
  116.      * Get currency code
  117.      *
  118.      * @return string
  119.      */
  120.     public function getCode()
  121.     {
  122.         return $this->getCurrency()->getCode();
  123.     }
  124.     /**
  125.      * Get currency prefix
  126.      *
  127.      * @return string
  128.      */
  129.     public function getPrefix()
  130.     {
  131.         return $this->getCurrency()->getPrefix();
  132.     }
  133.     /**
  134.      * Get currency suffix
  135.      *
  136.      * @return string
  137.      */
  138.     public function getSuffix()
  139.     {
  140.         return $this->getCurrency()->getSuffix();
  141.     }
  142.     /**
  143.      * Get currency format
  144.      *
  145.      * @return string
  146.      */
  147.     public function getFormat()
  148.     {
  149.         return $this->getCurrency()->getThousandDelimiter()
  150.         . \XLite\View\FormField\Select\FloatFormat::FORMAT_DELIMITER
  151.         $this->getCurrency()->getDecimalDelimiter();
  152.     }
  153.     /**
  154.      * Get rate update date as string
  155.      *
  156.      * @return string
  157.      */
  158.     public function getRateDateAsString()
  159.     {
  160.         return ($this->getRateDate() == 0)
  161.             ? '-'
  162.             \XLite\Core\Converter::getInstance()->formatDate($this->getRateDate())
  163.             . ' ' \XLite\Core\Converter::getInstance()->formatDayTime($this->getRateDate());
  164.     }
  165.     /**
  166.      * Get countries list
  167.      *
  168.      * @return string
  169.      */
  170.     public function getCountriesList()
  171.     {
  172.         $return = [];
  173.         $countries $this->getCountries();
  174.         if (count($countries) > 0) {
  175.             foreach ($countries as $country) {
  176.                 $return[] = $country->getCode();
  177.             }
  178.         }
  179.         return empty($return) ? '...' implode(', '$return);
  180.     }
  181.     /**
  182.      * Set enabled
  183.      *
  184.      * @param boolean $enabled
  185.      *
  186.      * @return ActiveCurrency
  187.      */
  188.     public function setEnabled($enabled)
  189.     {
  190.         $this->enabled = (bool)$enabled;
  191.         return $this;
  192.     }
  193.     /**
  194.      * Set rate
  195.      *
  196.      * @param float $value Value
  197.      *
  198.      * @return void
  199.      */
  200.     public function setRate($value)
  201.     {
  202.         if ($this->isDefaultCurrency()) {
  203.             $this->setRateDate(0);
  204.         } else {
  205.             $this->rateDate \XLite\Core\Converter::getInstance()->time();
  206.         }
  207.         $this->rate $value;
  208.     }
  209.     /**
  210.      * Set currency prefix
  211.      *
  212.      * @param string $value Value
  213.      *
  214.      * @return void
  215.      */
  216.     public function setPrefix($value)
  217.     {
  218.         $this->getCurrency()->setPrefix($value);
  219.     }
  220.     /**
  221.      * Set currency suffix
  222.      *
  223.      * @param string $value Value
  224.      *
  225.      * @return void
  226.      */
  227.     public function setSuffix($value)
  228.     {
  229.         $this->getCurrency()->setSuffix($value);
  230.     }
  231.     /**
  232.      * Set currency format
  233.      *
  234.      * @param string $value Value
  235.      *
  236.      * @return void
  237.      */
  238.     public function setFormat($value)
  239.     {
  240.         $format $this->getDelimitersFormat($value);
  241.         $this->getCurrency()->setThousandDelimiter($format[0]);
  242.         $this->getCurrency()->setDecimalDelimiter($format[1]);
  243.     }
  244.     /**
  245.      * Get ID
  246.      *
  247.      * @return integer
  248.      */
  249.     public function getId()
  250.     {
  251.         return $this->getActiveCurrencyId();
  252.     }
  253.     /**
  254.      * Is default currency
  255.      *
  256.      * @return boolean
  257.      */
  258.     public function isDefaultCurrency()
  259.     {
  260.         return $this->getCurrency()->getCurrencyId() == \XLite\Core\Config::getInstance()->General->shop_currency;
  261.     }
  262.     /**
  263.      * Get default value
  264.      *
  265.      * @return boolean
  266.      */
  267.     public function getDefaultValue()
  268.     {
  269.         return $this->isDefaultCurrency();
  270.     }
  271.     /**
  272.      * Get delimiters format from string
  273.      *
  274.      * @param string $format Format
  275.      *
  276.      * @return array
  277.      */
  278.     protected function getDelimitersFormat($format)
  279.     {
  280.         $return explode(\XLite\View\FormField\Select\FloatFormat::FORMAT_DELIMITER$format);
  281.         if (!is_array($return)) {
  282.             $return $this->defaultDelimiterFormat;
  283.         } else {
  284.             if (!in_array($return[0], $this->allowedThousandsDelimiter)) {
  285.                 $return[0] = $this->defaultDelimiterFormat[0];
  286.             }
  287.             if (!in_array($return[1], $this->allowedDecimalDelimiter)) {
  288.                 $return[1] = $this->defaultDelimiterFormat[1];
  289.             }
  290.         }
  291.         return $return;
  292.     }
  293.     /**
  294.      * Return first assigned country
  295.      *
  296.      * @return \XLite\Model\Country
  297.      */
  298.     public function getFirstCountry()
  299.     {
  300.         $countries $this->getCountries();
  301.         return $countries[0] ?? null;
  302.     }
  303.     /**
  304.      * Check if currency has assigned countries
  305.      *
  306.      * @return boolean
  307.      */
  308.     public function hasAssignedCountries()
  309.     {
  310.         return \XLite\Core\Database::getRepo('XC\MultiCurrency\Model\ActiveCurrency')
  311.             ->hasAssignedCountries($this->getCode());
  312.     }
  313.     /**
  314.      * Get active_currency_id
  315.      *
  316.      * @return integer
  317.      */
  318.     public function getActiveCurrencyId()
  319.     {
  320.         return $this->active_currency_id;
  321.     }
  322.     /**
  323.      * Get enabled
  324.      *
  325.      * @return boolean
  326.      */
  327.     public function getEnabled()
  328.     {
  329.         return $this->enabled;
  330.     }
  331.     /**
  332.      * Set position
  333.      *
  334.      * @param integer $position
  335.      * @return ActiveCurrency
  336.      */
  337.     public function setPosition($position)
  338.     {
  339.         $this->position $position;
  340.         return $this;
  341.     }
  342.     /**
  343.      * Get position
  344.      *
  345.      * @return integer
  346.      */
  347.     public function getPosition()
  348.     {
  349.         return $this->position;
  350.     }
  351.     /**
  352.      * Set rateDate
  353.      *
  354.      * @param integer $rateDate
  355.      * @return ActiveCurrency
  356.      */
  357.     public function setRateDate($rateDate)
  358.     {
  359.         $this->rateDate $rateDate;
  360.         return $this;
  361.     }
  362.     /**
  363.      * Get rateDate
  364.      *
  365.      * @return integer
  366.      */
  367.     public function getRateDate()
  368.     {
  369.         return $this->rateDate;
  370.     }
  371.     /**
  372.      * Set currency
  373.      *
  374.      * @param \XLite\Model\Currency $currency
  375.      * @return ActiveCurrency
  376.      */
  377.     public function setCurrency(\XLite\Model\Currency $currency null)
  378.     {
  379.         $this->currency $currency;
  380.         return $this;
  381.     }
  382.     /**
  383.      * Get currency
  384.      *
  385.      * @return \XLite\Model\Currency
  386.      */
  387.     public function getCurrency()
  388.     {
  389.         return $this->currency;
  390.     }
  391.     /**
  392.      * Add countries
  393.      *
  394.      * @param \XLite\Model\Country $countries
  395.      * @return ActiveCurrency
  396.      */
  397.     public function addCountries(\XLite\Model\Country $countries)
  398.     {
  399.         $this->countries[] = $countries;
  400.         return $this;
  401.     }
  402.     /**
  403.      * Set Countries
  404.      *
  405.      * @param \XLite\Model\Country[] $countries
  406.      *
  407.      * @return $this
  408.      */
  409.     public function setCountries($countries)
  410.     {
  411.         foreach ($this->getCountries() as $country) {
  412.             if ($country->getActiveCurrency() === $this) {
  413.                 $country->setActiveCurrency(null);
  414.             }
  415.         }
  416.         foreach ($countries as $country) {
  417.             $country->setActiveCurrency($this);
  418.         }
  419.         $this->countries $countries;
  420.         return $this;
  421.     }
  422.     /**
  423.      * Get countries
  424.      *
  425.      * @return \Doctrine\Common\Collections\Collection
  426.      */
  427.     public function getCountries()
  428.     {
  429.         return $this->countries;
  430.     }
  431.     /**
  432.      * Return RoundUp
  433.      *
  434.      * @return string
  435.      */
  436.     public function getRoundUp()
  437.     {
  438.         return $this->getCurrency()
  439.             ? $this->getCurrency()->getRoundUp()
  440.             : \XLite\Model\Currency::ROUNDUP_NONE;
  441.     }
  442.     /**
  443.      * Set RoundUp
  444.      *
  445.      * @param string $roundUp
  446.      *
  447.      * @return $this
  448.      */
  449.     public function setRoundUp($roundUp)
  450.     {
  451.         if ($this->getCurrency()) {
  452.             $this->getCurrency()->setRoundUp($roundUp);
  453.         }
  454.         return $this;
  455.     }
  456. }