Passed
Push — master ( be4768...895194 )
by Jan
09:55
created

Currency::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\ArrayCollection;
49
use Doctrine\Common\Collections\Collection;
50
use Doctrine\ORM\Mapping as ORM;
51
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
52
use Symfony\Component\Validator\Constraints as Assert;
53
54
/**
55
 * This entity describes a currency that can be used for price informations.
56
 *
57
 * @UniqueEntity("iso_code")
58
 * @ORM\Entity()
59
 * @ORM\Table(name="currencies")
60
 */
61
class Currency extends AbstractStructuralDBElement
62
{
63
    public const PRICE_SCALE = 5;
64
65
    /**
66
     * @var string|null The exchange rate between this currency and the base currency
67
     *                  (how many base units the current currency is worth)
68
     * @ORM\Column(type="decimal", precision=11, scale=5, nullable=true)
69
     * @Assert\Positive()
70
     */
71
    protected $exchange_rate;
72
73
    /**
74
     * @var string the 3 letter ISO code of the currency
75
     * @ORM\Column(type="string")
76
     * @Assert\Currency()
77
     */
78
    protected $iso_code;
79
80
    /**
81
     * @ORM\OneToMany(targetEntity="Currency", mappedBy="parent", cascade={"persist"})
82
     * @ORM\OrderBy({"name" = "ASC"})
83
     */
84
    protected $children;
85
86
    /**
87
     * @ORM\ManyToOne(targetEntity="Currency", inversedBy="children")
88
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
89
     */
90
    protected $parent;
91
92
    /**
93
     * @var Collection<int, CurrencyAttachment>
94
     * @ORM\OneToMany(targetEntity="App\Entity\Attachments\CurrencyAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true)
95
     * @ORM\OrderBy({"name" = "ASC"})
96
     * @Assert\Valid()
97
     */
98
    protected $attachments;
99
100
    /** @var Collection<int, CurrencyParameter>
101
     * @ORM\OneToMany(targetEntity="App\Entity\Parameters\CurrencyParameter", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true)
102
     * @ORM\OrderBy({"group" = "ASC" ,"name" = "ASC"})
103
     * @Assert\Valid()
104
     */
105
    protected $parameters;
106
107
    /** @var Collection<int, Pricedetail>
108
     * @ORM\OneToMany(targetEntity="App\Entity\PriceInformations\Pricedetail", mappedBy="currency")
109
     */
110
    protected $pricedetails;
111
112
    public function __construct()
113
    {
114
        $this->pricedetails = new ArrayCollection();
115
        parent::__construct();
116
    }
117
118
    /**
119
     * @return Collection
120
     */
121
    public function getPricedetails(): Collection
122
    {
123
        return $this->pricedetails;
124
    }
125
126
    /**
127
     * Returns the 3 letter ISO code of this currency.
128
     *
129
     * @return string
130
     */
131
    public function getIsoCode(): ?string
132
    {
133
        return $this->iso_code;
134
    }
135
136
    /**
137
     * @param string $iso_code
138
     *
139
     * @return Currency
140
     */
141
    public function setIsoCode(?string $iso_code): self
142
    {
143
        $this->iso_code = $iso_code;
144
145
        return $this;
146
    }
147
148
    /**
149
     * Returns the inverse exchange rate (how many of the current currency the base unit is worth).
150
     */
151
    public function getInverseExchangeRate(): ?string
152
    {
153
        $tmp = $this->getExchangeRate();
154
155
        if (null === $tmp || '0' === $tmp) {
156
            return null;
157
        }
158
159
        return bcdiv('1', $tmp, static::PRICE_SCALE);
160
    }
161
162
    /**
163
     * Returns The exchange rate between this currency and the base currency
164
     * (how many base units the current currency is worth).
165
     */
166
    public function getExchangeRate(): ?string
167
    {
168
        return $this->exchange_rate;
169
    }
170
171
    /**
172
     * Sets the exchange rate of the currency.
173
     *
174
     * @param string|null $exchange_rate The new exchange rate of the currency.
175
     *                                   Set to null, if the exchange rate is unknown.
176
     *
177
     * @return Currency
178
     */
179
    public function setExchangeRate(?string $exchange_rate): self
180
    {
181
        $this->exchange_rate = $exchange_rate;
182
183
        return $this;
184
    }
185
}
186