1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\Postage\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\FieldList; |
6
|
|
|
use SilverStripe\ORM\DataExtension; |
7
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
8
|
|
|
use SilverCommerce\TaxAdmin\Model\TaxRate; |
9
|
|
|
use SilverStripe\Forms\ReadonlyField; |
10
|
|
|
use SilverCommerce\Postage\Helpers\PostageOption; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Add extra postage options to a DataObject (for example and Invoice or |
14
|
|
|
* Estimate) |
15
|
|
|
* |
16
|
|
|
* This function will attempt to autocalculate tax for an extended object, |
17
|
|
|
* and will add look for the following extension hooks to update the prices: |
18
|
|
|
* |
19
|
|
|
* - `updateTaxTotal` (to add the postage tax to the current tax value) |
20
|
|
|
* - `updateTotal` (to add the current postage price to the total value) |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
class PostageExtension extends DataExtension |
24
|
|
|
{ |
25
|
|
|
private static $db = [ |
|
|
|
|
26
|
|
|
"PostageTitle" => "Varchar", |
27
|
|
|
"PostagePrice" => "Currency" |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
private static $has_one = [ |
|
|
|
|
31
|
|
|
"PostageTax" => TaxRate::class |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
private static $casting = [ |
|
|
|
|
35
|
|
|
"PostageDetails" => "Varchar", |
36
|
|
|
"PostageTaxPrice" => "Currency", |
37
|
|
|
"PostageTotal" => "Currency" |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set the postage settings on this object based on the provided |
42
|
|
|
* PostageOption. |
43
|
|
|
* |
44
|
|
|
* @return self |
45
|
|
|
*/ |
46
|
|
|
public function setPostage(PostageOption $postage) |
47
|
|
|
{ |
48
|
|
|
$this->owner->PostageTitle = $postage->getName(); |
49
|
|
|
$this->owner->PostagePrice = $postage->getPrice(); |
50
|
|
|
$this->owner->PostageTax = $postage->getTax(); |
51
|
|
|
|
52
|
|
|
return $this->owner; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Generate a PostageOption based on this object's details |
57
|
|
|
* |
58
|
|
|
* @return PostageOption |
59
|
|
|
*/ |
60
|
|
|
public function getPostage() |
61
|
|
|
{ |
62
|
|
|
return PostageOption::create( |
63
|
|
|
$this->owner->PostageTitle, |
64
|
|
|
$this->owner->PostagePrice, |
65
|
|
|
$this->owner->PostageTax |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Remove all postage settings from this object |
71
|
|
|
* |
72
|
|
|
* @return self |
73
|
|
|
*/ |
74
|
|
|
public function clearPostage() |
75
|
|
|
{ |
76
|
|
|
$this->owner->PostageTitle = ""; |
77
|
|
|
$this->owner->PostagePrice = 0; |
78
|
|
|
$this->owner->PostageTaxID = null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Generate a string outlining the details of selected postage |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getPostageDetails() |
87
|
|
|
{ |
88
|
|
|
return $this->owner->PostageTitle . " (" . $this->owner->obj("PostageTotal")->Nice() . ")"; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the total value of postage (including tax) |
93
|
|
|
* |
94
|
|
|
* @return float |
95
|
|
|
*/ |
96
|
|
|
public function getPostageTotal() |
97
|
|
|
{ |
98
|
|
|
$price = $this->owner->PostagePrice; |
99
|
|
|
|
100
|
|
|
return $price + $this->owner->PostageTaxPrice; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get the total value of postage (including tax) |
105
|
|
|
* |
106
|
|
|
* @return float |
107
|
|
|
*/ |
108
|
|
|
public function getPostageTaxPrice() |
109
|
|
|
{ |
110
|
|
|
$price = $this->owner->PostagePrice; |
111
|
|
|
$tax = $this->owner->PostageTax(); |
112
|
|
|
$rate = ($tax->exists()) ? $tax->Rate : 0; |
113
|
|
|
|
114
|
|
|
return ($price / 100 * $rate); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Add the current postage tax top the total tax |
119
|
|
|
* on this object |
120
|
|
|
*/ |
121
|
|
|
public function updateTaxTotal(&$total) |
122
|
|
|
{ |
123
|
|
|
$total = $total + $this->owner->PostageTaxPrice; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Attempt to add the postage price to current totals |
128
|
|
|
* |
129
|
|
|
*/ |
130
|
|
|
public function updateTotal(&$total) |
131
|
|
|
{ |
132
|
|
|
$total = $total + $this->owner->PostagePrice; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function updateCMSFields(FieldList $fields) |
136
|
|
|
{ |
137
|
|
|
$config = SiteConfig::current_site_config(); |
|
|
|
|
138
|
|
|
|
139
|
|
|
// Move postage selection fields |
140
|
|
|
$title = $fields->dataFieldByName("PostageTitle"); |
141
|
|
|
$price = $fields->dataFieldByName("PostagePrice"); |
142
|
|
|
$tax = $fields->dataFieldByName("PostageTaxID"); |
143
|
|
|
|
144
|
|
|
$fields->addFieldsToTab( |
145
|
|
|
"Root.Delivery", |
146
|
|
|
[ |
147
|
|
|
$title, |
148
|
|
|
$price, |
149
|
|
|
$tax |
150
|
|
|
] |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
} |