1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\Postage\Helpers; |
4
|
|
|
|
5
|
|
|
use SilverCommerce\TaxAdmin\Model\TaxRate; |
6
|
|
|
use SilverStripe\Core\Injector\Injectable; |
7
|
|
|
use SilverStripe\ORM\FieldType\DBCurrency; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Generic container for postage data, PossiblePostage objects need to be |
11
|
|
|
* created by PostageTypes and then returned as part of the ArrayList |
12
|
|
|
* |
13
|
|
|
* This object contains 3 generic params: |
14
|
|
|
* |
15
|
|
|
* - name (the name of the postage) |
16
|
|
|
* - price (the cost of this object) |
17
|
|
|
* - tax (the TaxCategory assigned to this object) |
18
|
|
|
*/ |
19
|
|
|
class PostageOption |
20
|
|
|
{ |
21
|
|
|
use Injectable; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The Name of the current postage item |
25
|
|
|
*/ |
26
|
|
|
protected $name = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get the value of name |
30
|
|
|
*/ |
31
|
|
|
public function getName() |
32
|
|
|
{ |
33
|
|
|
return $this->name; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set the value of name |
38
|
|
|
* |
39
|
|
|
* @return self |
40
|
|
|
*/ |
41
|
|
|
public function setName($name) |
42
|
|
|
{ |
43
|
|
|
$this->name = $name; |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The price of this current shipping option |
49
|
|
|
* |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
protected $price = 0; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the value of price |
56
|
|
|
*/ |
57
|
|
|
public function getPrice() |
58
|
|
|
{ |
59
|
|
|
return $this->price; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the value of price |
64
|
|
|
* |
65
|
|
|
* @return self |
66
|
|
|
*/ |
67
|
|
|
public function setPrice($price) |
68
|
|
|
{ |
69
|
|
|
$this->price = $price; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Assign tax to this item, this should be an instance of |
76
|
|
|
* TaxCategory |
77
|
|
|
* |
78
|
|
|
* @var TaxRate |
79
|
|
|
*/ |
80
|
|
|
protected $tax = null; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the value of tax |
84
|
|
|
*/ |
85
|
|
|
public function getTax() |
86
|
|
|
{ |
87
|
|
|
return $this->tax; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set the value of tax |
92
|
|
|
* |
93
|
|
|
* @return self |
94
|
|
|
*/ |
95
|
|
|
public function setTax(TaxRate $tax) |
96
|
|
|
{ |
97
|
|
|
$this->tax = $tax; |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the monitary value of tax for this option |
103
|
|
|
* |
104
|
|
|
* @return float |
105
|
|
|
*/ |
106
|
|
|
public function getTaxPrice() |
107
|
|
|
{ |
108
|
|
|
$price = $this->getPrice(); |
109
|
|
|
$tax = $this->getTax(); |
110
|
|
|
$rate = ($tax && $tax->exists()) ? $tax->Rate : 0; |
111
|
|
|
|
112
|
|
|
return ($price / 100 * $rate); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the total monitary value of this option |
117
|
|
|
* |
118
|
|
|
* @return float |
119
|
|
|
*/ |
120
|
|
|
public function getTotalPrice() |
121
|
|
|
{ |
122
|
|
|
return $this->getPrice() + $this->getTaxPrice(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Generate a summary of this postage option |
127
|
|
|
* |
128
|
|
|
* @var string |
129
|
|
|
*/ |
130
|
|
|
public function getSummary() |
131
|
|
|
{ |
132
|
|
|
$area_currency = new DBCurrency("Cost"); |
133
|
|
|
$area_currency->setValue($this->getTotalPrice()); |
134
|
|
|
|
135
|
|
|
return $this->getName() . " (" . $area_currency->Nice() . ")"; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Generate a unique key for this parcel |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getKey() |
144
|
|
|
{ |
145
|
|
|
return base64_encode(json_encode((array)$this)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function __construct($name, $price, TaxRate $tax = null) |
149
|
|
|
{ |
150
|
|
|
$this->name = $name; |
151
|
|
|
$this->price = $price; |
152
|
|
|
$this->tax = $tax; |
153
|
|
|
} |
154
|
|
|
} |