2019-08-12 15:47:57 +02:00
|
|
|
<?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);
|
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
namespace App\Entity\PriceInformations;
|
|
|
|
|
2023-07-14 00:09:22 +02:00
|
|
|
use App\Repository\CurrencyRepository;
|
2023-06-11 14:55:06 +02:00
|
|
|
use Doctrine\DBAL\Types\Types;
|
2019-09-24 13:39:49 +02:00
|
|
|
use App\Entity\Attachments\CurrencyAttachment;
|
2020-02-01 19:48:07 +01:00
|
|
|
use App\Entity\Base\AbstractStructuralDBElement;
|
2020-03-11 21:48:47 +01:00
|
|
|
use App\Entity\Parameters\CurrencyParameter;
|
2020-05-20 20:57:16 +02:00
|
|
|
use App\Validator\Constraints\BigDecimal\BigDecimalPositive;
|
|
|
|
use Brick\Math\BigDecimal;
|
|
|
|
use Brick\Math\RoundingMode;
|
2020-05-17 21:52:47 +02:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2019-09-24 13:39:49 +02:00
|
|
|
use Doctrine\Common\Collections\Collection;
|
2019-08-12 15:47:57 +02:00
|
|
|
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;
|
2019-08-12 15:47:57 +02:00
|
|
|
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.
|
2023-06-13 10:36:34 +02:00
|
|
|
*
|
|
|
|
* @extends AbstractStructuralDBElement<CurrencyAttachment, CurrencyParameter>
|
2019-08-12 15:47:57 +02:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[UniqueEntity('iso_code')]
|
2023-07-14 00:09:22 +02:00
|
|
|
#[ORM\Entity(repositoryClass: CurrencyRepository::class)]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\Table(name: 'currencies')]
|
|
|
|
#[ORM\Index(name: 'currency_idx_name', columns: ['name'])]
|
|
|
|
#[ORM\Index(name: 'currency_idx_parent_name', columns: ['parent_id', 'name'])]
|
2020-02-01 19:48:07 +01:00
|
|
|
class Currency extends AbstractStructuralDBElement
|
2019-08-12 15:47:57 +02:00
|
|
|
{
|
2023-06-11 14:15:46 +02:00
|
|
|
final public const PRICE_SCALE = 5;
|
2019-09-01 14:37:53 +02:00
|
|
|
|
2019-09-24 13:39:49 +02:00
|
|
|
/**
|
2020-05-20 20:57:16 +02:00
|
|
|
* @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)
|
2019-09-24 13:39:49 +02:00
|
|
|
*/
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\Column(type: 'big_decimal', precision: 11, scale: 5, nullable: true)]
|
2023-06-11 19:32:15 +02:00
|
|
|
#[BigDecimalPositive()]
|
2022-09-18 22:59:31 +02:00
|
|
|
protected ?BigDecimal $exchange_rate = null;
|
2019-09-24 13:39:49 +02:00
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
/**
|
2023-02-05 03:01:25 +01:00
|
|
|
* @var string the 3-letter ISO code of the currency
|
2019-08-12 15:47:57 +02:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Assert\Currency]
|
2023-07-03 22:11:12 +02:00
|
|
|
#[Assert\NotBlank]
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Groups(['extended', 'full', 'import'])]
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\Column(type: Types::STRING)]
|
2022-09-21 12:31:32 +02:00
|
|
|
protected string $iso_code = "";
|
2019-08-12 15:47:57 +02:00
|
|
|
|
2023-06-13 20:24:54 +02:00
|
|
|
#[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent', cascade: ['persist'])]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\OrderBy(['name' => 'ASC'])]
|
2023-04-15 22:25:03 +02:00
|
|
|
protected Collection $children;
|
2019-08-12 23:45:21 +02:00
|
|
|
|
2023-06-13 20:24:54 +02:00
|
|
|
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\JoinColumn(name: 'parent_id')]
|
2023-06-11 14:15:46 +02:00
|
|
|
protected ?AbstractStructuralDBElement $parent = null;
|
2019-08-12 23:45:21 +02:00
|
|
|
|
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
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Assert\Valid]
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\OneToMany(targetEntity: CurrencyAttachment::class, mappedBy: 'element', cascade: ['persist', 'remove'], orphanRemoval: true)]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\OrderBy(['name' => 'ASC'])]
|
2023-04-15 22:25:03 +02:00
|
|
|
protected Collection $attachments;
|
2020-01-05 22:49:00 +01:00
|
|
|
|
2020-03-29 23:13:25 +02:00
|
|
|
/** @var Collection<int, CurrencyParameter>
|
2020-03-11 21:48:47 +01:00
|
|
|
*/
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Assert\Valid]
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\OneToMany(targetEntity: CurrencyParameter::class, mappedBy: 'element', cascade: ['persist', 'remove'], orphanRemoval: true)]
|
2023-05-28 01:33:45 +02:00
|
|
|
#[ORM\OrderBy(['group' => 'ASC', 'name' => 'ASC'])]
|
2023-04-15 22:25:03 +02:00
|
|
|
protected Collection $parameters;
|
2020-03-11 21:48:47 +01:00
|
|
|
|
2020-05-17 21:52:47 +02:00
|
|
|
/** @var Collection<int, Pricedetail>
|
|
|
|
*/
|
2023-06-11 14:55:06 +02:00
|
|
|
#[ORM\OneToMany(targetEntity: Pricedetail::class, mappedBy: 'currency')]
|
2023-04-15 22:25:03 +02:00
|
|
|
protected Collection $pricedetails;
|
2020-05-17 21:52:47 +02:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
2023-06-11 14:55:06 +02:00
|
|
|
$this->children = new ArrayCollection();
|
|
|
|
$this->attachments = new ArrayCollection();
|
|
|
|
$this->parameters = new ArrayCollection();
|
2020-05-17 21:52:47 +02:00
|
|
|
$this->pricedetails = new ArrayCollection();
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPricedetails(): Collection
|
|
|
|
{
|
|
|
|
return $this->pricedetails;
|
|
|
|
}
|
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
/**
|
2023-02-05 03:01:25 +01:00
|
|
|
* Returns the 3-letter ISO code of this currency.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-08-12 15:47:57 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2023-07-03 22:11:12 +02:00
|
|
|
public function getIsoCode(): string
|
2019-08-12 15:47:57 +02:00
|
|
|
{
|
|
|
|
return $this->iso_code;
|
|
|
|
}
|
|
|
|
|
2023-07-03 22:11:12 +02:00
|
|
|
public function setIsoCode(string $iso_code): self
|
2019-08-12 15:47:57 +02:00
|
|
|
{
|
|
|
|
$this->iso_code = $iso_code;
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* Returns the inverse exchange rate (how many of the current currency the base unit is worth).
|
2019-08-12 15:47:57 +02:00
|
|
|
*/
|
2020-05-20 20:57:16 +02:00
|
|
|
public function getInverseExchangeRate(): ?BigDecimal
|
2019-08-12 15:47:57 +02:00
|
|
|
{
|
|
|
|
$tmp = $this->getExchangeRate();
|
|
|
|
|
2023-06-11 14:55:06 +02:00
|
|
|
if (!$tmp instanceof BigDecimal || $tmp->isZero()) {
|
2019-08-12 15:47:57 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-20 20:57:16 +02:00
|
|
|
return BigDecimal::one()->dividedBy($tmp, $tmp->getScale(), RoundingMode::HALF_UP);
|
2019-08-12 15:47:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns The exchange rate between this currency and the base currency
|
2019-11-09 00:47:20 +01:00
|
|
|
* (how many base units the current currency is worth).
|
2019-08-12 15:47:57 +02:00
|
|
|
*/
|
2020-05-20 20:57:16 +02:00
|
|
|
public function getExchangeRate(): ?BigDecimal
|
2019-08-12 15:47:57 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
*
|
2020-05-20 20:57:16 +02: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.
|
2019-08-12 15:47:57 +02:00
|
|
|
*/
|
2020-05-20 20:57:16 +02:00
|
|
|
public function setExchangeRate(?BigDecimal $exchange_rate): self
|
2019-08-12 15:47:57 +02:00
|
|
|
{
|
2023-07-03 22:15:58 +02:00
|
|
|
//If the exchange rate is null, set it to null and return.
|
|
|
|
if ($exchange_rate === null) {
|
2020-05-20 22:45:41 +02:00
|
|
|
$this->exchange_rate = null;
|
2023-07-03 22:15:58 +02:00
|
|
|
return $this;
|
2020-05-20 22:45:41 +02:00
|
|
|
}
|
2023-07-03 22:15:58 +02:00
|
|
|
|
2020-05-20 22:45:41 +02:00
|
|
|
//Only change the object, if the value changes, so that doctrine does not detect it as changed.
|
2023-07-03 22:15:58 +02:00
|
|
|
//Or if the current exchange rate is currently null, as we can not compare it then
|
|
|
|
if ($this->exchange_rate === null || $exchange_rate->compareTo($this->exchange_rate) !== 0) {
|
2020-05-20 22:45:41 +02:00
|
|
|
$this->exchange_rate = $exchange_rate;
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|