1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FeiMx\Tax\Traits; |
4
|
|
|
|
5
|
|
|
use FeiMx\Tax\Exceptions\TaxErrorException; |
6
|
|
|
use FeiMx\Tax\Models\TaxGroup; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
8
|
|
|
|
9
|
|
|
trait Taxable |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Column name used for get the price of the model. |
13
|
|
|
* |
14
|
|
|
* @return string Column name |
15
|
|
|
*/ |
16
|
|
|
public static function priceColumn() |
17
|
|
|
{ |
18
|
|
|
return 'price'; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* [taxGroups description]. |
23
|
|
|
* |
24
|
|
|
* @return [type] [description] |
|
|
|
|
25
|
|
|
*/ |
26
|
|
|
public function taxGroups(): MorphToMany |
27
|
|
|
{ |
28
|
|
|
return $this->morphToMany( |
|
|
|
|
29
|
|
|
TaxGroup::class, |
30
|
|
|
'model', |
31
|
|
|
'model_has_tax_groups', |
32
|
|
|
'model_id', |
33
|
|
|
'tax_group_id' |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function hasTaxGroups() |
38
|
|
|
{ |
39
|
|
|
return (bool) $this->taxGroups->count(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Determine if the model has (one of) the given tax group(s). |
44
|
|
|
* |
45
|
|
|
* @param string|array|\FeiMx\Tax\Models\TaxGroup|\Illuminate\Support\Collection $taxGroups |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function hasTaxGroup($taxGroups) |
50
|
|
|
{ |
51
|
|
|
if (is_string($taxGroups)) { |
52
|
|
|
return $this->taxGroups->contains('name', $taxGroups); |
53
|
|
|
} |
54
|
|
|
if ($taxGroups instanceof TaxGroup) { |
55
|
|
|
return $this->taxGroups->contains('id', $taxGroups->id); |
56
|
|
|
} |
57
|
|
|
if (is_array($taxGroups)) { |
58
|
|
|
foreach ($taxGroups as $taxGroup) { |
59
|
|
|
if ($this->hasTaxGroup($taxGroup)) { |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $taxGroups->intersect($this->taxGroups)->isNotEmpty(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Assign the given taxGroup to the model. |
72
|
|
|
* |
73
|
|
|
* @param array|string|\FeiMx\Tax\Models\TaxGroup ...$taxGroups |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function assignTaxGroup(...$taxGroups) |
78
|
|
|
{ |
79
|
|
|
if (0 == count($taxGroups)) { |
80
|
|
|
throw new TaxErrorException('You must pass a valid TaxGroup'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$taxGroups = collect($taxGroups) |
84
|
|
|
->flatten() |
85
|
|
|
->map(function ($taxGroup) { |
86
|
|
|
return $this->getStoredTaxGroup($taxGroup); |
87
|
|
|
}) |
88
|
|
|
->all(); |
89
|
|
|
|
90
|
|
|
$this->taxGroups()->saveMany($taxGroups); |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Revoke the given role from the model. |
97
|
|
|
* |
98
|
|
|
* @param string|\FeiMx\Tax\Models\TaxGroup $taxGroup |
99
|
|
|
*/ |
100
|
|
|
public function removeTaxGroup($taxGroup) |
101
|
|
|
{ |
102
|
|
|
$this->taxGroups()->detach($this->getStoredTaxGroup($taxGroup)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Remove all current tax groups and set the given ones. |
107
|
|
|
* |
108
|
|
|
* @param array|\FeiMx\Tax\Models\TaxGroup|string ...$taxGroups |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function syncTaxGroups(...$taxGroups) |
113
|
|
|
{ |
114
|
|
|
$this->taxGroups()->detach(); |
115
|
|
|
|
116
|
|
|
return $this->assignTaxGroup($taxGroups); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get total amount for current TaxGroup. |
121
|
|
|
* |
122
|
|
|
* @param \FeiMx\Tax\Models\TaxGroup|string $taxGroup |
123
|
|
|
* |
124
|
|
|
* @return $total |
|
|
|
|
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function total($taxGroup = null) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
if (null === $taxGroup) { |
129
|
|
|
throw new TaxErrorException('You must pass a valid TaxGroup'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$taxGroup = $this->getStoredTaxGroup($taxGroup); |
133
|
|
|
$column = self::priceColumn(); |
134
|
|
|
|
135
|
|
|
return $total = $taxGroup->taxManager($this->{$column}) |
|
|
|
|
136
|
|
|
->addTaxes( |
137
|
|
|
$taxGroup->taxes->map(function ($tax) { |
138
|
|
|
return $tax->info(); |
139
|
|
|
}) |
140
|
|
|
->all() |
141
|
|
|
) |
142
|
|
|
->total(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get a list of taxes with amount calculated for the given TaxGroup. |
147
|
|
|
* |
148
|
|
|
* @param \FeiMx\Tax\Models\TaxGroup|string $taxGroup |
149
|
|
|
* |
150
|
|
|
* @return $total |
|
|
|
|
151
|
|
|
*/ |
152
|
|
View Code Duplication |
public function getAmounts($taxGroup = null) |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
if (null === $taxGroup) { |
155
|
|
|
throw new TaxErrorException('You must pass a valid TaxGroup'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$taxGroup = $this->getStoredTaxGroup($taxGroup); |
159
|
|
|
$column = self::priceColumn(); |
160
|
|
|
|
161
|
|
|
return $taxGroup->taxManager($this->{$column}) |
162
|
|
|
->addTaxes( |
163
|
|
|
$taxGroup->taxes->map(function ($tax) { |
164
|
|
|
return $tax->info(); |
165
|
|
|
}) |
166
|
|
|
->all() |
167
|
|
|
) |
168
|
|
|
->get(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
protected function getStoredTaxGroup($taxGroup): TaxGroup |
172
|
|
|
{ |
173
|
|
|
if (is_numeric($taxGroup)) { |
174
|
|
|
return TaxGroup::find($taxGroup); |
175
|
|
|
} |
176
|
|
|
if (is_string($taxGroup)) { |
177
|
|
|
return TaxGroup::whereName($taxGroup)->first(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $taxGroup; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.