1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PriceModifier.php |
4
|
|
|
* |
5
|
|
|
* @author Bram de Leeuw |
6
|
|
|
* Date: 31/03/17 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Broarm\EventTickets; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class TaxModifier |
13
|
|
|
* |
14
|
|
|
* Adds a configurable tax rate on the receipt |
15
|
|
|
* |
16
|
|
|
* @package Broarm\EventTickets |
17
|
|
|
*/ |
18
|
|
|
class TaxModifier extends PriceModifier |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Set the tax rate as a percentage |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private static $tax_rate = 21; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set if the tax rate is inclusive or exclusive |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private static $inclusive = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set the default sort value to a large int so it always shows and calculates as last |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private static $defaults = array( |
|
|
|
|
40
|
|
|
'Title' => 'Tax modifier', |
41
|
|
|
'Sort' => 9999 |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Update the total, if the tax is not inclusive the total gets altered |
46
|
|
|
* |
47
|
|
|
* @param float $total |
48
|
|
|
* @param Reservation $reservation |
49
|
|
|
*/ |
50
|
|
|
public function updateTotal(&$total, Reservation $reservation) { |
51
|
|
|
$rate = (float)self::config()->get('tax_rate') / 100; |
52
|
|
|
$tax = $total * $rate; |
53
|
|
|
$this->setPriceModification($tax); |
54
|
|
|
if (!(bool)self::config()->get('inclusive')) { |
55
|
|
|
$total += $tax; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Show the used tax rate in the table title |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getTableTitle() |
65
|
|
|
{ |
66
|
|
|
$rate = _t( |
67
|
|
|
'TaxModifier.TABLE_TITLE', |
68
|
|
|
'{rate}% BTW', |
69
|
|
|
null, |
70
|
|
|
array( |
|
|
|
|
71
|
|
|
'rate' => (float)self::config()->get('tax_rate') |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
if ((bool)self::config()->get('inclusive')) { |
76
|
|
|
$inc = _t('TaxModifier.INCLUSIVE', '(Incl.)'); |
77
|
|
|
$rate .= " $inc"; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $rate; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Show the calculated tax value as a positive value |
85
|
|
|
* |
86
|
|
|
* @return float |
87
|
|
|
*/ |
88
|
|
|
public function getTableValue() |
89
|
|
|
{ |
90
|
|
|
return $this->PriceModification; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Create a tax modifier if it does not already exists |
95
|
|
|
* |
96
|
|
|
* @param Reservation $reservation |
97
|
|
|
* |
98
|
|
|
* @return TaxModifier|\DataObject|null|static |
99
|
|
|
*/ |
100
|
|
|
public static function findOrMake(Reservation $reservation) |
101
|
|
|
{ |
102
|
|
|
if (!$modifier = $reservation->PriceModifiers()->find('ClassName', self::class)) { |
103
|
|
|
$modifier = self::create(); |
104
|
|
|
$modifier->write(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $modifier; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|