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