/
Tax.php
218 lines (187 loc) · 4.68 KB
/
Tax.php
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
namespace Duit\Concerns;
use Money\Money;
use Money\MoneyFormatter;
use Duit\Contracts\Taxable;
trait Tax
{
use Cash;
/**
* Enable Tax calculation.
*
* @var \Duit\Contracts\Taxable|null
*/
protected $taxable;
/**
* Make object with Tax.
*
* @param int|numeric-string $amount
* @param \Duit\Contracts\Taxable $taxable
*
* @return static
*/
public static function afterTax($amount, Taxable $taxable)
{
return static::beforeTax(
$taxable->getAmountWithoutTax(static::asMoney($amount)), $taxable
);
}
/**
* Make object before applying Tax.
*
* @param int|numeric-string $amount
* @param \Duit\Contracts\Taxable $taxable
*
* @return static
*/
public static function beforeTax($amount, Taxable $taxable)
{
return (new static($amount))->enableTax($taxable);
}
/**
* Make object without Tax.
*
* @param int|numeric-string $amount
*
* @return static
*/
public static function withoutTax($amount)
{
return (new static($amount))->disableTax();
}
/**
* Get formatted amount with GST.
*
* @return numeric-string|non-empty-string
*/
public function amountWithTax(): string
{
return $this->getFormatter()->format(
static::asMoney($this->getAmountWithTax())
);
}
/**
* Get formatted cash with GST.
*
* @return numeric-string|non-empty-string
*/
public function cashAmountWithTax(): string
{
return $this->getFormatter()->format(
static::asMoney($this->getCashAmountWithTax())
);
}
/**
* Enable Tax for calculation.
*
* @return $this
*/
final public function enableTax(Taxable $taxable): self
{
$this->taxable = $taxable;
return $this;
}
/**
* Disable Tax for calculation.
*
* @return $this
*/
final public function disableTax(): self
{
$this->taxable = null;
return $this;
}
/**
* Check if the object has Tax.
*/
final public function hasTax(): bool
{
return $this->getTax() instanceof Taxable;
}
/**
* Get GST amount.
*
* @return numeric-string
*/
public function getTaxAmount(): string
{
if (is_null($this->taxable)) {
return '0';
}
return $this->taxable->getTaxAmount($this->getMoney());
}
/**
* Returns the value represented by this object with Tax.
*
* @return numeric-string
*/
public function getAmountWithTax(): string
{
if (is_null($this->taxable)) {
return $this->getMoney()->getAmount();
}
return $this->taxable->getAmountWithTax($this->getMoney());
}
/**
* Get amount for cash with Tax.
*
* @return numeric-string
*/
public function getCashAmountWithTax(): string
{
return (string) $this->getClosestAcceptedCashAmount(
$this->getAmountWithTax()
);
}
/**
* Allocate the money according to a list of ratios with Tax.
*
* @return array<int, \Money\Money>
*/
public function allocateWithTax(array $ratios): array
{
$method = $this->hasTax() ? 'afterTax' : 'withoutTax';
$results = [];
$allocates = static::asMoney($this->getAmountWithTax())->allocate($ratios);
foreach ($allocates as $allocate) {
$results[] = static::{$method}($allocate->getAmount(), $this->getTax());
}
return $results;
}
/**
* Allocate the money among N targets with GST.
*
* @param int<1, max> $n
* @throws \InvalidArgumentException If number of targets is not an integer
*
* @return array<int, \Money\Money>
*/
public function allocateWithTaxTo(int $n): array
{
$method = $this->hasTax() ? 'afterTax' : 'withoutTax';
$results = [];
$allocates = static::asMoney($this->getAmountWithTax())->allocateTo($n);
foreach ($allocates as $allocate) {
$results[] = static::{$method}($allocate->getAmount(), $this->getTax());
}
return $results;
}
/**
* Get applied tax.
*/
abstract public function getTax(): ?Taxable;
/**
* Get the money object.
*/
abstract public function getMoney(): Money;
/**
* Build money object.
*
* @param int|numeric-string $amount
*/
abstract protected static function asMoney($amount): Money;
/**
* Get money formatter.
*/
abstract protected function getFormatter(): MoneyFormatter;
}