Completed
Push — master ( f332d8...569caa )
by Joachim
114:30 queued 99:30
created

Price::getUnitPriceExclVat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 5
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Loevgaard\DandomainFoundation\Entity;
4
5
use Assert\Assert;
6
use Doctrine\ORM\Mapping as ORM;
7
use Loevgaard\DandomainFoundation;
8
use Loevgaard\DandomainFoundation\Entity\Generated\ProductInterface;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...erated\ProductInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Loevgaard\DandomainFoundation\Entity\Generated\PeriodInterface;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...nerated\PeriodInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Loevgaard\DandomainFoundation\Entity\Generated\CurrencyInterface;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...rated\CurrencyInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Loevgaard\DandomainFoundation\Entity\Generated\PriceInterface;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...enerated\PriceInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Loevgaard\DandomainFoundation\Entity\Generated\PriceTrait;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...ty\Generated\PriceTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Money\Money;
14
15
/**
16
 * @ORM\Entity()
17
 * @ORM\Table(name="ldf_prices", uniqueConstraints={@ORM\UniqueConstraint(columns={"amount", "b2b_group_id", "currency_id", "product_id"})})
18
 * @ORM\HasLifecycleCallbacks()
19
 */
20
class Price extends AbstractEntity implements PriceInterface
21
{
22
    use PriceTrait;
23
24
    /**
25
     * @var int
26
     *
27
     * @ORM\Id
28
     * @ORM\GeneratedValue
29
     * @ORM\Column(type="integer")
30
     **/
31
    protected $id;
32
33
    /**
34
     * @var int|null
35
     *
36
     * @ORM\Column(name="amount", type="integer")
37
     */
38
    protected $amount;
39
40
    /**
41
     * @var int|null
42
     *
43
     * @ORM\Column(type="integer")
44
     */
45
    protected $avance;
46
47
    /**
48
     * @var string|null
49
     *
50
     * @ORM\Column(name="b2b_group_id", type="string", length=191)
51
     */
52
    protected $b2bGroupId;
53
54
    /**
55
     * The currency code in the Dandomain API refers in fact to the currencies' field named 'id' or 'code'
56
     * Therefore we don't have a currencyCode and isoCode property, but a currency property
57
     *
58
     * @var CurrencyInterface|null
59
     *
60
     * @ORM\ManyToOne(targetEntity="Currency")
61
     * @ORM\JoinColumn(name="currency_id", nullable=false)
62
     */
63
    protected $currency;
64
65
    /**
66
     * @var int|null
67
     *
68
     * @ORM\Column(type="integer")
69
     */
70
    protected $specialOfferPrice;
71
72
    /**
73
     * @var int|null
74
     *
75
     * @ORM\Column(type="integer")
76
     */
77
    protected $unitPrice;
78
79
    /**
80
     * @var PeriodInterface|null
81
     *
82
     * @ORM\JoinColumn(onDelete="SET NULL", nullable=true)
83
     * @ORM\ManyToOne(targetEntity="Period")
84
     */
85
    protected $period;
86
87
    /**
88
     * @var ProductInterface
89
     *
90
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="prices")
91
     * @ORM\JoinColumn(name="product_id", nullable=false, onDelete="CASCADE")
92
     */
93
    protected $product;
94
95
    /**
96
     * Creates a valid Price
97
     *
98
     * @param int $amount
99
     * @param int $avance
100
     * @param string $b2bGroupId
101
     * @param CurrencyInterface $currency
102
     * @param int $specialOfferPrice In cents/ører (in danish)
103
     * @param int $unitPrice In cents/ører (in danish)
104
     * @return PriceInterface
105
     */
106
    public static function create(int $amount, int $avance, string $b2bGroupId, CurrencyInterface $currency, int $specialOfferPrice, int $unitPrice) : PriceInterface
107
    {
108
        $specialOfferPrice = new Money($specialOfferPrice, new \Money\Currency($currency->getIsoCodeAlpha()));
109
        $unitPrice = new Money($unitPrice, new \Money\Currency($currency->getIsoCodeAlpha()));
110
111
        $price = new Price();
112
        $price
113
            ->setAmount($amount)
114
            ->setAvance($avance)
115
            ->setB2bGroupId($b2bGroupId)
116
            ->setCurrency($currency)
117
            ->setSpecialOfferPrice($specialOfferPrice)
118
            ->setUnitPrice($unitPrice)
119
        ;
120
121
        return $price;
122
    }
123
124
    /**
125
     * @ORM\PreUpdate()
126
     * @ORM\PrePersist()
127
     */
128
    public function validate()
129
    {
130
        Assert::that($this->amount)->integer(null, 'amount')->greaterThan(0);
131
        Assert::that($this->avance)->integer(null, 'avance');
132
        Assert::that($this->b2bGroupId)->string(null, 'b2bGroupId');
133
        Assert::that($this->currency)->isInstanceOf(CurrencyInterface::class, null, 'currency');
134
        Assert::that($this->specialOfferPrice)->integer(null, 'specialOfferPrice')->greaterOrEqualThan(0, null, 'specialOfferPrice');
135
        Assert::that($this->unitPrice)->integer(null, 'unitPrice')->greaterOrEqualThan(0, null, 'unitPrice');
136
        Assert::thatNullOr($this->period)->isInstanceOf(PeriodInterface::class, null, 'period');
137
        Assert::that($this->product)->isInstanceOf(ProductInterface::class);
138
    }
139
140
    /**
141
     * Will copy properties from $price
142
     *
143
     * @param PriceInterface $price
144
     */
145
    public function copyProperties(PriceInterface $price) : void
146
    {
147
        $this->setAmount($price->getAmount());
148
        $this->setAvance($price->getAvance());
149
        $this->setB2bGroupId($price->getB2bGroupId());
150
        $this->setCurrency($price->getCurrency());
151
        $this->setSpecialOfferPrice($price->getSpecialOfferPrice());
152
        $this->setUnitPrice($price->getUnitPrice());
153
        $this->setPeriod($price->getPeriod());
154
        $this->setProduct($price->getProduct());
155
    }
156
157
    /*
158
     * Helper methods
159
     */
160
    public function getUnitPriceExclVat(float $vat) : ?Money
161
    {
162
        $unitPrice = $this->getUnitPrice();
163
164
        if(!$unitPrice) {
165
            return null;
166
        }
167
168
        $multiplier = (100 + $vat) / 100;
169
170
        return $unitPrice->multiply($multiplier);
171
    }
172
173
    public function getSpecialOfferPriceExclVat(float $vat) : ?Money
174
    {
175
        $specialOfferPrice = $this->getSpecialOfferPrice();
176
177
        if(!$specialOfferPrice) {
178
            return null;
179
        }
180
181
        $multiplier = (100 + $vat) / 100;
182
183
        return $specialOfferPrice->multiply($multiplier);
184
    }
185
186
    /**
187
     * @return int
188
     */
189
    public function getId(): int
190
    {
191
        return (int)$this->id;
192
    }
193
194
    /**
195
     * @param int $id
196
     * @return PriceInterface
197
     */
198
    public function setId(int $id) : PriceInterface
199
    {
200
        $this->id = $id;
201
        return $this;
202
    }
203
204
    /**
205
     * @return int|null
206
     */
207
    public function getAmount() : ?int
208
    {
209
        return $this->amount;
210
    }
211
212
    /**
213
     * @param int|null $amount
214
     * @return PriceInterface
215
     */
216
    public function setAmount(int $amount) : PriceInterface
217
    {
218
        $this->amount = $amount;
219
        return $this;
220
    }
221
222
    /**
223
     * @return int|null
224
     */
225
    public function getAvance() : ?int
226
    {
227
        return $this->avance;
228
    }
229
230
    /**
231
     * @param int|null $avance
232
     * @return PriceInterface
233
     */
234
    public function setAvance(int $avance) : PriceInterface
235
    {
236
        $this->avance = $avance;
237
        return $this;
238
    }
239
240
    /**
241
     * @return null|string
242
     */
243
    public function getB2bGroupId() : ?string
244
    {
245
        return $this->b2bGroupId;
246
    }
247
248
    /**
249
     * @param null|string $b2bGroupId
250
     * @return PriceInterface
251
     */
252
    public function setB2bGroupId(string $b2bGroupId) : PriceInterface
253
    {
254
        $this->b2bGroupId = $b2bGroupId;
255
        return $this;
256
    }
257
258
    /**
259
     * @return null|CurrencyInterface
260
     */
261
    public function getCurrency() : ?CurrencyInterface
262
    {
263
        return $this->currency;
264
    }
265
266
    /**
267
     * @param null|CurrencyInterface $currency
268
     * @return PriceInterface
269
     */
270
    public function setCurrency(CurrencyInterface $currency) : PriceInterface
271
    {
272
        $this->currency = $currency;
273
        return $this;
274
    }
275
276
    /**
277
     * @return Money|null
278
     */
279
    public function getSpecialOfferPrice() : ?Money
280
    {
281
        if(!$this->currency) {
282
            return null;
283
        }
284
        return DandomainFoundation\createMoney($this->currency->getIsoCodeAlpha(), (int)$this->specialOfferPrice);
0 ignored issues
show
Bug introduced by
The function createMoney was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

284
        return /** @scrutinizer ignore-call */ DandomainFoundation\createMoney($this->currency->getIsoCodeAlpha(), (int)$this->specialOfferPrice);
Loading history...
285
    }
286
287
    /**
288
     * @param Money|null $specialOfferPrice
289
     * @return PriceInterface
290
     */
291
    public function setSpecialOfferPrice(Money $specialOfferPrice) : PriceInterface
292
    {
293
        // @todo change type from int to string
294
        $this->specialOfferPrice = (int)$specialOfferPrice->getAmount();
295
296
        return $this;
297
    }
298
299
    /**
300
     * @return Money|null
301
     */
302
    public function getUnitPrice() : ?Money
303
    {
304
        if(!$this->currency) {
305
            return null;
306
        }
307
        return DandomainFoundation\createMoney($this->currency->getIsoCodeAlpha(), (int)$this->unitPrice);
0 ignored issues
show
Bug introduced by
The function createMoney was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

307
        return /** @scrutinizer ignore-call */ DandomainFoundation\createMoney($this->currency->getIsoCodeAlpha(), (int)$this->unitPrice);
Loading history...
308
    }
309
310
    /**
311
     * @param Money|null $unitPrice
312
     * @return PriceInterface
313
     */
314
    public function setUnitPrice(Money $unitPrice) : PriceInterface
315
    {
316
        // @todo change type from int to string
317
        $this->unitPrice = (int)$unitPrice->getAmount();
318
319
        return $this;
320
    }
321
322
    /**
323
     * @return Period|null
324
     */
325
    public function getPeriod() : ?PeriodInterface
326
    {
327
        return $this->period;
328
    }
329
330
    /**
331
     * @param PeriodInterface|null $period
332
     * @return PriceInterface
333
     */
334
    public function setPeriod(?PeriodInterface $period) : PriceInterface
335
    {
336
        $this->period = $period;
337
        return $this;
338
    }
339
340
    /**
341
     * @return ProductInterface
342
     */
343
    public function getProduct() : ?ProductInterface
344
    {
345
        return $this->product;
346
    }
347
348
    /**
349
     * @param ProductInterface|null $product
350
     * @return PriceInterface
351
     */
352
    public function setProduct(?ProductInterface $product) : PriceInterface
353
    {
354
        $this->product = $product;
355
        return $this;
356
    }
357
}
358