Issues (174)

src/Entity/Price.php (9 issues)

Labels
Severity
1
<?php
2
3
namespace Loevgaard\DandomainFoundation\Entity;
4
5
use Assert\Assert;
6
use Brick\Math\BigDecimal;
7
use Doctrine\ORM\Mapping as ORM;
8
use Loevgaard\DandomainFoundation;
9
use Loevgaard\DandomainFoundation\Entity\Generated\CurrencyInterface;
0 ignored issues
show
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...
10
use Loevgaard\DandomainFoundation\Entity\Generated\PeriodInterface;
0 ignored issues
show
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...
11
use Loevgaard\DandomainFoundation\Entity\Generated\PriceInterface;
0 ignored issues
show
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
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 Loevgaard\DandomainFoundation\Entity\Generated\ProductInterface;
0 ignored issues
show
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...
14
use Money\Money;
15
16
/**
17
 * @ORM\Entity()
18
 * @ORM\Table(name="ldf_prices", uniqueConstraints={@ORM\UniqueConstraint(columns={"amount", "b2b_group_id", "currency_id", "product_id"})})
19
 * @ORM\HasLifecycleCallbacks()
20
 */
21
class Price extends AbstractEntity implements PriceInterface
22
{
23
    use PriceTrait;
24
25
    /**
26
     * @var int
27
     *
28
     * @ORM\Id
29
     * @ORM\GeneratedValue
30
     * @ORM\Column(type="integer")
31
     **/
32
    protected $id;
33
34
    /**
35
     * @var int|null
36
     *
37
     * @ORM\Column(name="amount", type="integer")
38
     */
39
    protected $amount;
40
41
    /**
42
     * @var int|null
43
     *
44
     * @ORM\Column(type="integer")
45
     */
46
    protected $avance;
47
48
    /**
49
     * @var string|null
50
     *
51
     * @ORM\Column(name="b2b_group_id", type="string", length=191)
52
     */
53
    protected $b2bGroupId;
54
55
    /**
56
     * The currency code in the Dandomain API refers in fact to the currencies' field named 'id' or 'code'
57
     * Therefore we don't have a currencyCode and isoCode property, but a currency property.
58
     *
59
     * @var CurrencyInterface|null
60
     *
61
     * @ORM\ManyToOne(targetEntity="Currency")
62
     * @ORM\JoinColumn(name="currency_id", nullable=false)
63
     */
64
    protected $currency;
65
66
    /**
67
     * The special offer price WITH vat.
68
     *
69
     * @var int|null
70
     *
71
     * @ORM\Column(type="integer")
72
     */
73
    protected $specialOfferPrice;
74
75
    /**
76
     * The unit price WITH vat.
77
     *
78
     * @var int|null
79
     *
80
     * @ORM\Column(type="integer")
81
     */
82
    protected $unitPrice;
83
84
    /**
85
     * @var PeriodInterface|null
86
     *
87
     * @ORM\JoinColumn(onDelete="SET NULL", nullable=true)
88
     * @ORM\ManyToOne(targetEntity="Period")
89
     */
90
    protected $period;
91
92
    /**
93
     * @var ProductInterface
94
     *
95
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="prices")
96
     * @ORM\JoinColumn(name="product_id", nullable=false, onDelete="CASCADE")
97
     */
98
    protected $product;
99
100
    /**
101
     * Creates a valid Price.
102
     *
103
     * @param int               $amount
104
     * @param int               $avance
105
     * @param string            $b2bGroupId
106
     * @param CurrencyInterface $currency
107
     * @param int               $specialOfferPrice In cents/ører (in danish)
108
     * @param int               $unitPrice         In cents/ører (in danish)
109
     *
110
     * @return PriceInterface
111
     */
112
    public static function create(int $amount, int $avance, string $b2bGroupId, CurrencyInterface $currency, int $specialOfferPrice, int $unitPrice): PriceInterface
113
    {
114
        $specialOfferPrice = new Money($specialOfferPrice, new \Money\Currency($currency->getIsoCodeAlpha()));
115
        $unitPrice = new Money($unitPrice, new \Money\Currency($currency->getIsoCodeAlpha()));
116
117
        $price = new self();
118
        $price
119
            ->setAmount($amount)
120
            ->setAvance($avance)
121
            ->setB2bGroupId($b2bGroupId)
122
            ->setCurrency($currency)
123
            ->setSpecialOfferPrice($specialOfferPrice)
124
            ->setUnitPrice($unitPrice)
125
        ;
126
127
        return $price;
128
    }
129
130
    /**
131
     * @ORM\PreUpdate()
132
     * @ORM\PrePersist()
133
     */
134
    public function validate()
135
    {
136
        Assert::that($this->amount)->integer(null, 'amount')->greaterThan(0);
137
        Assert::that($this->avance)->integer(null, 'avance');
138
        Assert::that($this->b2bGroupId)->string(null, 'b2bGroupId');
139
        Assert::that($this->currency)->isInstanceOf(CurrencyInterface::class, null, 'currency');
140
        Assert::that($this->specialOfferPrice)->integer(null, 'specialOfferPrice')->greaterOrEqualThan(0, null, 'specialOfferPrice');
141
        Assert::that($this->unitPrice)->integer(null, 'unitPrice')->greaterOrEqualThan(0, null, 'unitPrice');
142
        Assert::thatNullOr($this->period)->isInstanceOf(PeriodInterface::class, null, 'period');
143
        Assert::that($this->product)->isInstanceOf(ProductInterface::class);
144
    }
145
146
    /**
147
     * Will copy properties from $price.
148
     *
149
     * @param PriceInterface $price
150
     */
151
    public function copyProperties(PriceInterface $price): void
152
    {
153
        $this->setAmount($price->getAmount());
154
        $this->setAvance($price->getAvance());
155
        $this->setB2bGroupId($price->getB2bGroupId());
156
        $this->setCurrency($price->getCurrency());
157
        $this->setSpecialOfferPrice($price->getSpecialOfferPrice());
158
        $this->setUnitPrice($price->getUnitPrice());
159
        $this->setPeriod($price->getPeriod());
160
        $this->setProduct($price->getProduct());
161
    }
162
163
    /*
164
     * Helper methods
165
     */
166
    public function getUnitPriceExclVat(float $vat): ?Money
167
    {
168
        $unitPrice = $this->getUnitPrice();
169
170
        if (!$unitPrice) {
171
            return null;
172
        }
173
174
        $multiplier = BigDecimal::of('100')->exactlyDividedBy(BigDecimal::of('100')->plus($vat));
0 ignored issues
show
The method exactlyDividedBy() does not exist on Brick\Math\BigRational. ( Ignorable by Annotation )

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

174
        $multiplier = BigDecimal::of('100')->/** @scrutinizer ignore-call */ exactlyDividedBy(BigDecimal::of('100')->plus($vat));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
The method exactlyDividedBy() does not exist on Brick\Math\BigInteger. ( Ignorable by Annotation )

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

174
        $multiplier = BigDecimal::of('100')->/** @scrutinizer ignore-call */ exactlyDividedBy(BigDecimal::of('100')->plus($vat));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
175
176
        return $unitPrice->multiply((string) $multiplier);
177
    }
178
179
    public function getSpecialOfferPriceExclVat(float $vat): ?Money
180
    {
181
        $specialOfferPrice = $this->getSpecialOfferPrice();
182
183
        if (!$specialOfferPrice) {
184
            return null;
185
        }
186
187
        $multiplier = BigDecimal::of('100')->exactlyDividedBy(BigDecimal::of('100')->plus($vat));
188
189
        return $specialOfferPrice->multiply((string) $multiplier);
190
    }
191
192
    /**
193
     * @return int
194
     */
195
    public function getId(): int
196
    {
197
        return (int) $this->id;
198
    }
199
200
    /**
201
     * @param int $id
202
     *
203
     * @return PriceInterface
204
     */
205
    public function setId(int $id): PriceInterface
206
    {
207
        $this->id = $id;
208
209
        return $this;
210
    }
211
212
    /**
213
     * @return int|null
214
     */
215
    public function getAmount(): ?int
216
    {
217
        return $this->amount;
218
    }
219
220
    /**
221
     * @param int|null $amount
222
     *
223
     * @return PriceInterface
224
     */
225
    public function setAmount(int $amount): PriceInterface
226
    {
227
        $this->amount = $amount;
228
229
        return $this;
230
    }
231
232
    /**
233
     * @return int|null
234
     */
235
    public function getAvance(): ?int
236
    {
237
        return $this->avance;
238
    }
239
240
    /**
241
     * @param int|null $avance
242
     *
243
     * @return PriceInterface
244
     */
245
    public function setAvance(int $avance): PriceInterface
246
    {
247
        $this->avance = $avance;
248
249
        return $this;
250
    }
251
252
    /**
253
     * @return null|string
254
     */
255
    public function getB2bGroupId(): ?string
256
    {
257
        return $this->b2bGroupId;
258
    }
259
260
    /**
261
     * @param null|string $b2bGroupId
262
     *
263
     * @return PriceInterface
264
     */
265
    public function setB2bGroupId(string $b2bGroupId): PriceInterface
266
    {
267
        $this->b2bGroupId = $b2bGroupId;
268
269
        return $this;
270
    }
271
272
    /**
273
     * @return null|CurrencyInterface
274
     */
275
    public function getCurrency(): ?CurrencyInterface
276
    {
277
        return $this->currency;
278
    }
279
280
    /**
281
     * @param null|CurrencyInterface $currency
282
     *
283
     * @return PriceInterface
284
     */
285
    public function setCurrency(CurrencyInterface $currency): PriceInterface
286
    {
287
        $this->currency = $currency;
288
289
        return $this;
290
    }
291
292
    /**
293
     * @return Money|null
294
     */
295
    public function getSpecialOfferPrice(): ?Money
296
    {
297
        if (!$this->currency) {
298
            return null;
299
        }
300
301
        return DandomainFoundation\createMoney($this->currency->getIsoCodeAlpha(), (int) $this->specialOfferPrice);
0 ignored issues
show
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

301
        return /** @scrutinizer ignore-call */ DandomainFoundation\createMoney($this->currency->getIsoCodeAlpha(), (int) $this->specialOfferPrice);
Loading history...
302
    }
303
304
    /**
305
     * @param Money|null $specialOfferPrice
306
     *
307
     * @return PriceInterface
308
     */
309
    public function setSpecialOfferPrice(Money $specialOfferPrice): PriceInterface
310
    {
311
        // @todo change type from int to string
312
        $this->specialOfferPrice = (int) $specialOfferPrice->getAmount();
313
314
        return $this;
315
    }
316
317
    /**
318
     * @return Money|null
319
     */
320
    public function getUnitPrice(): ?Money
321
    {
322
        if (!$this->currency) {
323
            return null;
324
        }
325
326
        return DandomainFoundation\createMoney($this->currency->getIsoCodeAlpha(), (int) $this->unitPrice);
0 ignored issues
show
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

326
        return /** @scrutinizer ignore-call */ DandomainFoundation\createMoney($this->currency->getIsoCodeAlpha(), (int) $this->unitPrice);
Loading history...
327
    }
328
329
    /**
330
     * @param Money|null $unitPrice
331
     *
332
     * @return PriceInterface
333
     */
334
    public function setUnitPrice(Money $unitPrice): PriceInterface
335
    {
336
        // @todo change type from int to string
337
        $this->unitPrice = (int) $unitPrice->getAmount();
338
339
        return $this;
340
    }
341
342
    /**
343
     * @return Period|null
344
     */
345
    public function getPeriod(): ?PeriodInterface
346
    {
347
        return $this->period;
348
    }
349
350
    /**
351
     * @param PeriodInterface|null $period
352
     *
353
     * @return PriceInterface
354
     */
355
    public function setPeriod(?PeriodInterface $period): PriceInterface
356
    {
357
        $this->period = $period;
358
359
        return $this;
360
    }
361
362
    /**
363
     * @return ProductInterface
364
     */
365
    public function getProduct(): ?ProductInterface
366
    {
367
        return $this->product;
368
    }
369
370
    /**
371
     * @param ProductInterface|null $product
372
     *
373
     * @return PriceInterface
374
     */
375
    public function setProduct(?ProductInterface $product): PriceInterface
376
    {
377
        $this->product = $product;
378
379
        return $this;
380
    }
381
}
382