-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathSystemTaxDriver.php
More file actions
182 lines (147 loc) · 4.86 KB
/
SystemTaxDriver.php
File metadata and controls
182 lines (147 loc) · 4.86 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
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
<?php
namespace Lunar\Drivers;
use Lunar\Actions\Taxes\GetTaxZone;
use Lunar\Base\Addressable;
use Lunar\Base\Purchasable;
use Lunar\Base\TaxDriver;
use Lunar\Base\ValueObjects\Cart\TaxBreakdown;
use Lunar\Base\ValueObjects\Cart\TaxBreakdownAmount;
use Lunar\DataTypes\Price;
use Lunar\Models\Contracts\CartLine;
use Lunar\Models\Contracts\Currency;
use Lunar\Models\Contracts\TaxZone as TaxZoneContract;
use Lunar\Models\TaxZone;
use Spatie\LaravelBlink\BlinkFacade as Blink;
class SystemTaxDriver implements TaxDriver
{
/**
* The taxable shipping address.
*/
protected ?Addressable $shippingAddress = null;
/**
* The taxable billing address.
*/
protected ?Addressable $billingAddress = null;
/**
* The currency model.
*/
protected Currency $currency;
/**
* The purchasable item.
*/
protected Purchasable $purchasable;
/**
* The cart line model.
*/
protected ?CartLine $cartLine = null;
/**
* An optional tax zone override supplied at the cart level.
* When set this takes precedence over the address-derived zone.
*/
protected ?TaxZoneContract $taxZone = null;
/**
* {@inheritDoc}
*/
public function setShippingAddress(?Addressable $address = null): self
{
$this->shippingAddress = $address;
return $this;
}
/**
* {@inheritDoc}
*/
public function setCurrency(Currency $currency): self
{
$this->currency = $currency;
return $this;
}
/**
* {@inheritDoc}
*/
public function setBillingAddress(?Addressable $address = null): self
{
$this->billingAddress = $address;
return $this;
}
/**
* {@inheritDoc}
*/
public function setPurchasable(Purchasable $purchasable): self
{
$this->purchasable = $purchasable;
return $this;
}
/**
* Set the cart line.
*/
public function setCartLine(CartLine $cartLine): self
{
$this->cartLine = $cartLine;
return $this;
}
/**
* {@inheritDoc}
*/
public function setTaxZone(?TaxZoneContract $taxZone = null): self
{
$this->taxZone = $taxZone;
return $this;
}
/**
* {@inheritDoc}
*/
public function getBreakdown($subTotal): TaxBreakdown
{
$taxZone = $this->taxZone ?? app(GetTaxZone::class)->execute($this->shippingAddress);
$taxClass = $this->purchasable->getTaxClass();
$taxAmounts = Blink::once('tax_zone_rates_'.$taxZone->id.'_'.$taxClass->id, function () use ($taxClass, $taxZone) {
return $taxZone->taxAmounts()->whereTaxClassId($taxClass->id)->get();
});
if (prices_inc_tax()) {
// Remove tax from price
$totalTaxPercentage = $taxAmounts->sum('percentage') / 100; // E.g. 0.2 for 20%
$priceExTax = round($subTotal / (1 + $totalTaxPercentage));
// Check to see if the included tax uses the same tax zone
if ($this->defaultTaxZone()->id === $taxZone->id) {
// Manually return the tax breakdown
$breakdown = new TaxBreakdown;
$taxTally = 0;
foreach ($taxAmounts as $key => $amount) {
if ($taxAmounts->keys()->last() == $key) {
// Ensure the final tax amount adds up to the original price
$result = $subTotal - $priceExTax - $taxTally;
} else {
$result = round($priceExTax * ($amount->percentage / 100));
}
$taxTally += $result;
$amount = new TaxBreakdownAmount(
price: new Price((int) $result, $this->currency, $this->purchasable->getUnitQuantity()),
identifier: "tax_rate_{$amount->taxRate->id}",
description: $amount->taxRate->name,
percentage: $amount->percentage
);
$breakdown->addAmount($amount);
}
return $breakdown;
}
// Set subTotal to ex. tax price
$subTotal = $priceExTax;
}
$breakdown = new TaxBreakdown;
foreach ($taxAmounts as $amount) {
$result = round($subTotal * ($amount->percentage / 100));
$amount = new TaxBreakdownAmount(
price: new Price((int) $result, $this->currency, $this->purchasable->getUnitQuantity()),
identifier: "tax_rate_{$amount->taxRate->id}",
description: $amount->taxRate->name,
percentage: $amount->percentage
);
$breakdown->addAmount($amount);
}
return $breakdown;
}
protected function defaultTaxZone()
{
return TaxZone::where('default', '=', 1)->first();
}
}