-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathHasModifiers.php
More file actions
130 lines (106 loc) · 3.62 KB
/
HasModifiers.php
File metadata and controls
130 lines (106 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Whitecube\Price\Concerns;
use Whitecube\Price\Price;
use Whitecube\Price\Modifier;
use Whitecube\Price\PriceAmendable;
use Brick\Money\AbstractMoney;
use Brick\Money\RationalMoney;
trait HasModifiers
{
/**
* Add a tax modifier
*/
public function addTax(string|callable|AbstractMoney|Price|PriceAmendable $modifier, ...$arguments): static
{
return $this->addModifier(Modifier::TYPE_TAX, $modifier, ...$arguments);
}
/**
* Add a discount modifier
*/
public function addDiscount(string|callable|AbstractMoney|Price|PriceAmendable $modifier, ...$arguments): static
{
return $this->addModifier(Modifier::TYPE_DISCOUNT, $modifier, ...$arguments);
}
/**
* Add a price modifier
*/
public function addModifier(string $type, string|callable|AbstractMoney|Price|PriceAmendable $modifier, ...$arguments): static
{
$this->modifiers[] = $this->makeModifier($type, $modifier, $arguments);
$this->invalidate();
return $this;
}
/**
* Create a usable modifier instance
* @throws \InvalidArgumentException
*/
protected function makeModifier(string $type, string|callable|AbstractMoney|Price|PriceAmendable $modifier, array $arguments = []): PriceAmendable
{
if(is_string($modifier) && class_exists($modifier)) {
$modifier = new $modifier(...$arguments);
}
if(is_a($modifier, PriceAmendable::class)) {
return $modifier->setType($type);
}
if($modifier === '') {
throw new \InvalidArgumentException('Price modifier cannot be empty.');
}
$instance = (new Modifier)->setType($type);
if (is_callable($modifier)) {
$modifier($instance);
return $instance;
}
if(is_a($modifier, Price::class)) {
$modifier = $modifier->inclusive();
}
return $instance->add($modifier, Price::getRounding('exclusive'));
}
/**
* Get the defined modifiers from before or after the
* VAT value should have been applied
*/
public function getVatModifiers(bool $postVat): array
{
return array_filter($this->modifiers, function($modifier) use ($postVat) {
return $modifier->appliesAfterVat() === $postVat;
});
}
/**
* Return the current modifications history
*/
public function modifications(bool $perUnit = false, ?string $type = null): array
{
$result = $this->build()->inclusive($perUnit ? true : false);
if(is_null($type)) {
return array_values($result['modifications']);
}
return array_values(array_filter($result['modifications'], function($modification) use ($type) {
return $modification['type'] === $type;
}));
}
/**
* Return the modification total for all discounts
*/
public function discounts(bool $perUnit = false): RationalMoney
{
return $this->modifiers($perUnit, Modifier::TYPE_DISCOUNT);
}
/**
* Return the modification total for all taxes
*/
public function taxes(bool $perUnit = false): RationalMoney
{
return $this->modifiers($perUnit, Modifier::TYPE_TAX);
}
/**
* Return the modification total for a given type
*/
public function modifiers(bool $perUnit = false, ?string $type = null): RationalMoney
{
$amount = RationalMoney::of(0, $this->currency());
foreach ($this->modifications($perUnit, $type) as $modification) {
$amount = $amount->plus($modification['amount']);
}
return $amount;
}
}