1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package silvershop-shipping |
5
|
|
|
*/ |
6
|
|
|
class DistanceShippingMethod extends ShippingMethod |
7
|
|
|
{ |
8
|
|
|
private static $defaults = array( |
|
|
|
|
9
|
|
|
'Name' => 'Distance Shipping', |
10
|
|
|
'Description' => 'Per product shipping' |
11
|
|
|
); |
12
|
|
|
|
13
|
|
|
private static $has_many = array( |
|
|
|
|
14
|
|
|
"DistanceFares" => "DistanceShippingFare" |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
public function getCMSFields() |
18
|
|
|
{ |
19
|
|
|
$fields = parent::getCMSFields(); |
20
|
|
|
$fields->fieldByName('Root')->removeByName("DistanceFares"); |
21
|
|
|
if ($this->isInDB()) { |
22
|
|
|
$fields->addFieldToTab("Root.Main", $gridfield = GridField::create( |
23
|
|
|
"DistanceFares", "Fares", |
24
|
|
|
$this->DistanceFares(), $config = new GridFieldConfig_RecordEditor() |
|
|
|
|
25
|
|
|
)); |
26
|
|
|
$config->removeComponentsByType("GridFieldDataColumns"); |
27
|
|
|
$config->removeComponentsByType("GridFieldEditButton"); |
28
|
|
|
$config->removeComponentsByType("GridFieldDeleteAction"); |
29
|
|
|
$config->removeComponentsByType("GridFieldAddNewButton"); |
30
|
|
|
$config->addComponent($cols = new GridFieldEditableColumns()); |
31
|
|
|
$config->addComponent(new GridFieldDeleteAction()); |
32
|
|
|
$config->addComponent($addnew = new GridFieldAddNewInlineButton()); |
33
|
|
|
$addnew->setTitle($addnew->getTitle()." Fare"); |
34
|
|
|
if ($greatest = $this->greatestCostDistance()) { |
|
|
|
|
35
|
|
|
$fields->insertAfter( |
36
|
|
|
LiteralField::create("costnote", |
37
|
|
|
"<p class=\"message\">Distances beyond the greatest specified distance will be cost ". |
38
|
|
|
$this->greatestCostDistance()->dbObject("Cost")->Nice(). |
39
|
|
|
" (the most expensive fare)</p>" |
40
|
|
|
), "DistanceFares" |
|
|
|
|
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $fields; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function calculateRate(ShippingPackage $package, Address $address) |
49
|
|
|
{ |
50
|
|
|
$warehouse = Warehouse::closest_to($address); |
51
|
|
|
$distance = $warehouse->Address()->distanceTo($address); |
|
|
|
|
52
|
|
|
|
53
|
|
|
return $this->getDistanceFare($distance); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getDistanceFare($distance) |
57
|
|
|
{ |
58
|
|
|
$cost = 0; |
59
|
|
|
$fare = $this->DistanceFares() |
|
|
|
|
60
|
|
|
->filter("Distance:GreaterThan", 0) |
61
|
|
|
->filter("Distance:GreaterThan", $distance) |
62
|
|
|
->sort("Distance", "ASC") |
63
|
|
|
->first(); |
64
|
|
|
if (!$fare) { |
65
|
|
|
$fare = $this->greatestCostDistance(); |
66
|
|
|
} |
67
|
|
|
if ($fare->exists()) { |
68
|
|
|
$cost = $fare->Cost; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $cost; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function greatestCostDistance() |
75
|
|
|
{ |
76
|
|
|
return $this->DistanceFares() |
|
|
|
|
77
|
|
|
->sort("Cost", "DESC") |
78
|
|
|
->first(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function requiresAddress() |
85
|
|
|
{ |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
class DistanceShippingFare extends DataObject |
91
|
|
|
{ |
92
|
|
|
private static $db = array( |
|
|
|
|
93
|
|
|
'Distance' => 'Float', |
94
|
|
|
'Cost' => 'Currency' |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
private static $has_one = array( |
|
|
|
|
98
|
|
|
'ShippingMethod' => 'DistanceShippingMethod' |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
private static $summary_fields = array( |
|
|
|
|
102
|
|
|
'MinDistance', |
103
|
|
|
'Distance', |
104
|
|
|
'Cost' |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
private static $field_labels = array( |
|
|
|
|
108
|
|
|
'MinDistance' => 'Min Distance (km)', |
109
|
|
|
'Distance' => 'Max Distance (km)', |
110
|
|
|
'Cost' => 'Cost' |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
private static $singular_name = "Fare"; |
|
|
|
|
114
|
|
|
|
115
|
|
|
private static $default_sort = "\"Distance\" ASC"; |
|
|
|
|
116
|
|
|
|
117
|
|
|
public function getMinDistance() |
118
|
|
|
{ |
119
|
|
|
$dist = 0; |
120
|
|
|
if ( |
121
|
|
|
$dfare = self::get() |
122
|
|
|
->filter("Distance:LessThan", $this->Distance) |
|
|
|
|
123
|
|
|
->filter("ShippingMethodID", $this->ShippingMethodID) |
|
|
|
|
124
|
|
|
->sort("Distance", "DESC") |
125
|
|
|
->first() |
126
|
|
|
) { |
127
|
|
|
$dist = $dfare->Distance; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $dist; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|