1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
25
|
|
|
* |
26
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics) |
27
|
|
|
* |
28
|
|
|
* This program is free software; you can redistribute it and/or |
29
|
|
|
* modify it under the terms of the GNU General Public License |
30
|
|
|
* as published by the Free Software Foundation; either version 2 |
31
|
|
|
* of the License, or (at your option) any later version. |
32
|
|
|
* |
33
|
|
|
* This program is distributed in the hope that it will be useful, |
34
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
35
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
36
|
|
|
* GNU General Public License for more details. |
37
|
|
|
* |
38
|
|
|
* You should have received a copy of the GNU General Public License |
39
|
|
|
* along with this program; if not, write to the Free Software |
40
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
41
|
|
|
*/ |
42
|
|
|
|
43
|
|
|
namespace App\Entity\PriceInformations; |
44
|
|
|
|
45
|
|
|
use App\Entity\Attachments\CurrencyAttachment; |
46
|
|
|
use App\Entity\Base\AbstractStructuralDBElement; |
47
|
|
|
use App\Entity\Parameters\CurrencyParameter; |
48
|
|
|
use Doctrine\Common\Collections\Collection; |
49
|
|
|
use Doctrine\ORM\Mapping as ORM; |
50
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
51
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* This entity describes a currency that can be used for price informations. |
55
|
|
|
* |
56
|
|
|
* @UniqueEntity("iso_code") |
57
|
|
|
* @ORM\Entity() |
58
|
|
|
* @ORM\Table(name="currencies") |
59
|
|
|
*/ |
60
|
|
|
class Currency extends AbstractStructuralDBElement |
61
|
|
|
{ |
62
|
|
|
public const PRICE_SCALE = 5; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string|null The exchange rate between this currency and the base currency |
66
|
|
|
* (how many base units the current currency is worth) |
67
|
|
|
* @ORM\Column(type="decimal", precision=11, scale=5, nullable=true) |
68
|
|
|
* @Assert\Positive() |
69
|
|
|
*/ |
70
|
|
|
protected $exchange_rate; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string the 3 letter ISO code of the currency |
74
|
|
|
* @ORM\Column(type="string") |
75
|
|
|
* @Assert\Currency() |
76
|
|
|
*/ |
77
|
|
|
protected $iso_code; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @ORM\OneToMany(targetEntity="Currency", mappedBy="parent", cascade={"persist"}) |
81
|
|
|
* @ORM\OrderBy({"name" = "ASC"}) |
82
|
|
|
*/ |
83
|
|
|
protected $children; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @ORM\ManyToOne(targetEntity="Currency", inversedBy="children") |
87
|
|
|
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id") |
88
|
|
|
*/ |
89
|
|
|
protected $parent; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var Collection<int, CurrencyAttachment> |
93
|
|
|
* @ORM\OneToMany(targetEntity="App\Entity\Attachments\CurrencyAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) |
94
|
|
|
* @ORM\OrderBy({"name" = "ASC"}) |
95
|
|
|
* @Assert\Valid() |
96
|
|
|
*/ |
97
|
|
|
protected $attachments; |
98
|
|
|
|
99
|
|
|
/** @var Collection<int, CurrencyParameter> |
100
|
|
|
* @ORM\OneToMany(targetEntity="App\Entity\Parameters\CurrencyParameter", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) |
101
|
|
|
* @ORM\OrderBy({"group" = "ASC" ,"name" = "ASC"}) |
102
|
|
|
* @Assert\Valid() |
103
|
|
|
*/ |
104
|
|
|
protected $parameters; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the 3 letter ISO code of this currency. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getIsoCode(): ?string |
112
|
|
|
{ |
113
|
|
|
return $this->iso_code; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $iso_code |
118
|
|
|
* |
119
|
|
|
* @return Currency |
120
|
|
|
*/ |
121
|
|
|
public function setIsoCode(?string $iso_code): self |
122
|
|
|
{ |
123
|
|
|
$this->iso_code = $iso_code; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns the inverse exchange rate (how many of the current currency the base unit is worth). |
130
|
|
|
*/ |
131
|
|
|
public function getInverseExchangeRate(): ?string |
132
|
|
|
{ |
133
|
|
|
$tmp = $this->getExchangeRate(); |
134
|
|
|
|
135
|
|
|
if (null === $tmp || '0' === $tmp) { |
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return bcdiv('1', $tmp, static::PRICE_SCALE); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns The exchange rate between this currency and the base currency |
144
|
|
|
* (how many base units the current currency is worth). |
145
|
|
|
*/ |
146
|
|
|
public function getExchangeRate(): ?string |
147
|
|
|
{ |
148
|
|
|
return $this->exchange_rate; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Sets the exchange rate of the currency. |
153
|
|
|
* |
154
|
|
|
* @param string|null $exchange_rate The new exchange rate of the currency. |
155
|
|
|
* Set to null, if the exchange rate is unknown. |
156
|
|
|
* |
157
|
|
|
* @return Currency |
158
|
|
|
*/ |
159
|
|
|
public function setExchangeRate(?string $exchange_rate): self |
160
|
|
|
{ |
161
|
|
|
$this->exchange_rate = $exchange_rate; |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|