1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FeiMx\Tax; |
4
|
|
|
|
5
|
|
|
use FeiMx\Tax\Exceptions\TaxErrorException; |
6
|
|
|
|
7
|
|
|
class TaxManager |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Amount for use when calculate taxes. |
11
|
|
|
* |
12
|
|
|
* @var int |
13
|
|
|
*/ |
14
|
|
|
public $amount; |
15
|
|
|
/** |
16
|
|
|
* List of taxes for calculate final amount. |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
public $taxes = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param int $amount |
24
|
|
|
*/ |
25
|
|
|
public function __construct($amount = 0) |
26
|
|
|
{ |
27
|
|
|
$this->amount = $amount; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string|FeiMx\Tax\Contracts\TaxContract $tax |
32
|
|
|
* @param bool|string $retention |
33
|
|
|
* |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public function addTax($tax, $retention = false) |
37
|
|
|
{ |
38
|
|
|
if (is_string($tax)) { |
39
|
|
|
$className = $this->stringToClassName($tax); |
40
|
|
|
$tax = new $className($retention); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (!in_array($tax, $this->taxes)) { |
44
|
|
|
$this->taxes[] = $tax; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array|\ArrayAccess |
52
|
|
|
*/ |
53
|
|
|
public function addTaxes(...$taxes) |
54
|
|
|
{ |
55
|
|
|
if (is_array($taxes)) { |
56
|
|
|
$taxes = array_flatten($taxes); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
collect($taxes)->each(function ($tax) { |
60
|
|
|
$this->addTax($tax); |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get total amount after taxes. |
68
|
|
|
* |
69
|
|
|
* @return float Total amount |
70
|
|
|
*/ |
71
|
|
|
public function total() |
72
|
|
|
{ |
73
|
|
|
$total = 0; |
74
|
|
|
foreach ($this->taxes as $tax) { |
75
|
|
|
$total += $tax->calculate($this->amount); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return number_format($this->amount + $total, 6, '.', ''); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get a list of taxes with amount calculated. |
83
|
|
|
* |
84
|
|
|
* @return float Total amount |
85
|
|
|
*/ |
86
|
|
|
public function get() |
87
|
|
|
{ |
88
|
|
|
$taxes = array_map(function ($tax) { |
89
|
|
|
return [ |
90
|
|
|
'tax' => $tax->name, |
91
|
|
|
'amount' => $tax->calculate($this->amount), |
92
|
|
|
]; |
93
|
|
|
}, $this->taxes); |
94
|
|
|
|
95
|
|
|
return array_merge([ |
96
|
|
|
'amount' => $this->amount, |
97
|
|
|
'total' => $this->total, |
|
|
|
|
98
|
|
|
'taxes' => $taxes, |
99
|
|
|
]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $tax |
104
|
|
|
* |
105
|
|
|
* @return string $className |
106
|
|
|
*/ |
107
|
|
|
public function stringToClassName($tax) |
108
|
|
|
{ |
109
|
|
|
$className = 'FeiMx\\Tax\\Taxes\\'.strtoupper($tax); |
110
|
|
|
if (!class_exists($className)) { |
111
|
|
|
throw new TaxErrorException("The tax '{$tax}' is not valid"); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $className; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function __get($property) |
118
|
|
|
{ |
119
|
|
|
if ('total' == $property) { |
120
|
|
|
return $this->total(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (property_exists($this, $property)) { |
124
|
|
|
return $this->{$property}; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
throw new \Exception('Property not exists'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.