|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverCommerce\Postage\Model; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\ORM\ArrayList; |
|
6
|
|
|
use SilverStripe\ORM\DataObject; |
|
7
|
|
|
use SilverCommerce\Postage\Helpers\Parcel; |
|
8
|
|
|
use SilverCommerce\Postage\Model\PostageType; |
|
9
|
|
|
use SilverCommerce\Postage\Helpers\PostageOption; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Represents a flat shipping cost, based on the selected regions. |
|
13
|
|
|
* |
|
14
|
|
|
* NOTE If you dont select any regions, this rate will be applied to |
|
15
|
|
|
* ALL regions |
|
16
|
|
|
*/ |
|
17
|
|
|
class FlatRate extends PostageType |
|
18
|
|
|
{ |
|
19
|
|
|
private static $table_name = 'PostageType_FlatRate'; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
private static $db = [ |
|
|
|
|
|
|
22
|
|
|
"Price" => "Currency" |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* If the current parcel is located in an area that we |
|
27
|
|
|
* allow flat rate |
|
28
|
|
|
* |
|
29
|
|
|
* @param Parcel |
|
30
|
|
|
* @return SSList |
|
|
|
|
|
|
31
|
|
|
*/ |
|
32
|
|
|
public function getPossiblePostage(Parcel $parcel) |
|
33
|
|
|
{ |
|
34
|
|
|
$return = ArrayList::create(); |
|
35
|
|
|
$locations = $this->Locations(); |
|
|
|
|
|
|
36
|
|
|
$exclude = $this->Exclusions(); |
|
|
|
|
|
|
37
|
|
|
$country = $parcel->getCountry(); |
|
38
|
|
|
$region = $parcel->getRegion(); |
|
39
|
|
|
$tax = null; |
|
40
|
|
|
|
|
41
|
|
|
if ($this->Tax()->exists()) { |
|
|
|
|
|
|
42
|
|
|
$tax = $this->Tax()->ValidTax(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$postage = PostageOption::create( |
|
46
|
|
|
$this->Name, |
|
|
|
|
|
|
47
|
|
|
$this->Price, |
|
|
|
|
|
|
48
|
|
|
$tax |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
if ($exclude->exists()) { |
|
52
|
|
|
$exclude = $exclude->filter( |
|
53
|
|
|
[ |
|
54
|
|
|
"Regions.CountryCode" => $country, |
|
55
|
|
|
"Regions.Code" => $region |
|
56
|
|
|
] |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (!$exclude->exists()) { |
|
61
|
|
|
if ($locations->exists()) { |
|
62
|
|
|
$locations = $locations->filter( |
|
63
|
|
|
[ |
|
64
|
|
|
"Regions.CountryCode" => $country, |
|
65
|
|
|
"Regions.Code" => $region |
|
66
|
|
|
] |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
if ($locations->exists()) { |
|
70
|
|
|
$return->add($postage); |
|
71
|
|
|
} |
|
72
|
|
|
} else { |
|
73
|
|
|
$return->add($postage); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $return; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|