Part-DB.Part-DB-server/src/Entity/PriceInformations/Currency.php

178 lines
5.5 KiB
PHP
Raw Normal View History

<?php
2020-02-22 18:14:36 +01:00
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
2022-11-29 22:28:53 +01:00
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
2020-02-22 18:14:36 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-01-05 15:46:58 +01:00
declare(strict_types=1);
namespace App\Entity\PriceInformations;
use App\Entity\Attachments\CurrencyAttachment;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\Parameters\CurrencyParameter;
use App\Validator\Constraints\BigDecimal\BigDecimalPositive;
use Brick\Math\BigDecimal;
use Brick\Math\RoundingMode;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
2023-03-12 01:12:35 +01:00
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
2023-02-05 03:01:25 +01:00
* This entity describes a currency that can be used for price information.
*
* @UniqueEntity("iso_code")
* @ORM\Entity()
* @ORM\Table(name="currencies", indexes={
* @ORM\Index(name="currency_idx_name", columns={"name"}),
* @ORM\Index(name="currency_idx_parent_name", columns={"parent_id", "name"}),
* })
*/
class Currency extends AbstractStructuralDBElement
{
public const PRICE_SCALE = 5;
/**
* @var BigDecimal|null The exchange rate between this currency and the base currency
2020-08-21 21:36:22 +02:00
* (how many base units the current currency is worth)
* @ORM\Column(type="big_decimal", precision=11, scale=5, nullable=true)
* @BigDecimalPositive()
*/
2022-09-18 22:59:31 +02:00
protected ?BigDecimal $exchange_rate = null;
/**
2023-02-05 03:01:25 +01:00
* @var string the 3-letter ISO code of the currency
* @ORM\Column(type="string")
* @Assert\Currency()
2023-03-13 00:52:22 +01:00
* @Groups({"extended", "full", "import"})
*/
protected string $iso_code = "";
2019-08-12 23:45:21 +02:00
/**
* @ORM\OneToMany(targetEntity="Currency", mappedBy="parent", cascade={"persist"})
* @ORM\OrderBy({"name" = "ASC"})
2019-08-12 23:45:21 +02:00
*/
protected $children;
/**
* @ORM\ManyToOne(targetEntity="Currency", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
protected $parent;
2020-01-05 22:49:00 +01:00
/**
2020-03-29 23:13:25 +02:00
* @var Collection<int, CurrencyAttachment>
2020-01-05 22:49:00 +01:00
* @ORM\OneToMany(targetEntity="App\Entity\Attachments\CurrencyAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OrderBy({"name" = "ASC"})
* @Assert\Valid()
2020-01-05 22:49:00 +01:00
*/
protected $attachments;
2020-03-29 23:13:25 +02:00
/** @var Collection<int, CurrencyParameter>
* @ORM\OneToMany(targetEntity="App\Entity\Parameters\CurrencyParameter", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OrderBy({"group" = "ASC" ,"name" = "ASC"})
* @Assert\Valid()
*/
protected $parameters;
/** @var Collection<int, Pricedetail>
* @ORM\OneToMany(targetEntity="App\Entity\PriceInformations\Pricedetail", mappedBy="currency")
*/
protected $pricedetails;
public function __construct()
{
$this->pricedetails = new ArrayCollection();
parent::__construct();
}
public function getPricedetails(): Collection
{
return $this->pricedetails;
}
/**
2023-02-05 03:01:25 +01:00
* Returns the 3-letter ISO code of this currency.
*
* @return string
*/
2019-08-12 23:45:21 +02:00
public function getIsoCode(): ?string
{
return $this->iso_code;
}
/**
2022-08-14 19:39:07 +02:00
* @param string|null $iso_code
*
* @return Currency
*/
public function setIsoCode(?string $iso_code): self
{
$this->iso_code = $iso_code;
return $this;
}
/**
* Returns the inverse exchange rate (how many of the current currency the base unit is worth).
*/
public function getInverseExchangeRate(): ?BigDecimal
{
$tmp = $this->getExchangeRate();
if (null === $tmp || $tmp->isZero()) {
return null;
}
return BigDecimal::one()->dividedBy($tmp, $tmp->getScale(), RoundingMode::HALF_UP);
}
/**
* Returns The exchange rate between this currency and the base currency
* (how many base units the current currency is worth).
*/
public function getExchangeRate(): ?BigDecimal
{
return $this->exchange_rate;
}
/**
2019-11-10 14:00:56 +01:00
* Sets the exchange rate of the currency.
2020-01-04 20:24:09 +01:00
*
* @param BigDecimal|null $exchange_rate The new exchange rate of the currency.
2020-08-21 21:36:22 +02:00
* Set to null, if the exchange rate is unknown.
2020-01-04 20:24:09 +01:00
*
* @return Currency
*/
public function setExchangeRate(?BigDecimal $exchange_rate): self
{
2020-08-21 21:36:22 +02:00
if (null === $exchange_rate) {
$this->exchange_rate = null;
}
$tmp = $exchange_rate->toScale(self::PRICE_SCALE, RoundingMode::HALF_UP);
//Only change the object, if the value changes, so that doctrine does not detect it as changed.
if ((string) $tmp !== (string) $this->exchange_rate) {
$this->exchange_rate = $exchange_rate;
}
return $this;
}
}