1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverShop\Model\Modifiers\Tax; |
4
|
|
|
|
5
|
|
|
use SilverShop\Model\Order; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Handles calculation of sales tax on Orders on |
9
|
|
|
* a per-country basis. |
10
|
|
|
* |
11
|
|
|
* @property string $Country |
12
|
|
|
*/ |
13
|
|
|
class GlobalTax extends Base |
14
|
|
|
{ |
15
|
|
|
private static $db = [ |
|
|
|
|
16
|
|
|
'Country' => 'Varchar', |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
private static $table_name = 'SilverShop_GlobalTaxModifier'; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Tax rates per country |
23
|
|
|
* |
24
|
|
|
* @config |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private static $country_rates = []; |
28
|
|
|
|
29
|
|
|
public function value($incoming) |
30
|
|
|
{ |
31
|
|
|
$rate = $this->Type == 'Chargable' |
|
|
|
|
32
|
|
|
? |
33
|
|
|
$this->Rate() |
34
|
|
|
: |
35
|
|
|
round(1 - (1 / (1 + $this->Rate())), Order::config()->rounding_precision); |
36
|
|
|
return $incoming * $rate; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function Rate() |
40
|
|
|
{ |
41
|
|
|
// If the order is no longer in cart, rely on the saved data |
42
|
|
|
if ($this->OrderID && !$this->Order()->IsCart()) { |
43
|
|
|
return $this->getField('Rate'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$rates = self::config()->country_rates; |
47
|
|
|
$country = $this->Country(); |
48
|
|
|
if ($country && isset($rates[$country])) { |
49
|
|
|
return $this->Rate = $rates[$country]['rate']; |
50
|
|
|
} |
51
|
|
|
$defaults = self::config()->defaults; |
52
|
|
|
return $this->Rate = $defaults['Rate']; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getTableTitle() |
56
|
|
|
{ |
57
|
|
|
$country = $this->Country() ? ' (' . $this->Country() . ') ' : ''; |
58
|
|
|
|
59
|
|
|
return parent::getTableTitle() . $country . |
60
|
|
|
($this->Type == 'Chargable' ? '' : _t(__CLASS__ . '.Included', ' (included in the above price)')); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function Country() |
64
|
|
|
{ |
65
|
|
|
if ($this->OrderID && $address = $this->Order()->getBillingAddress()) { |
66
|
|
|
return $address->Country; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function onBeforeWrite() |
73
|
|
|
{ |
74
|
|
|
parent::onBeforeWrite(); |
75
|
|
|
// While the order is still in "Cart" status, persist country code to DB |
76
|
|
|
if ($this->OrderID && $this->Order()->IsCart()) { |
77
|
|
|
$this->setField('Country', $this->Country()); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|