|
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 SilverStripe\SiteConfig\SiteConfig; |
|
9
|
|
|
use SilverCommerce\Postage\Helpers\Parcel; |
|
10
|
|
|
use SilverCommerce\TaxAdmin\Model\TaxCategory; |
|
11
|
|
|
use SilverCommerce\TaxAdmin\Helpers\MathsHelper; |
|
12
|
|
|
use SilverCommerce\Postage\Helpers\PostageOption; |
|
13
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm; |
|
14
|
|
|
use SilverStripe\Forms\GridField\GridFieldEditButton; |
|
15
|
|
|
use Symbiote\GridFieldExtensions\GridFieldEditableColumns; |
|
16
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns; |
|
17
|
|
|
use SilverStripe\Forms\GridField\GridFieldDeleteAction; |
|
18
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
|
19
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton; |
|
20
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Postage objects list available postage costs and destination locations |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
class PriceBased extends PostageType |
|
27
|
|
|
{ |
|
28
|
|
|
private static $table_name = 'PostageType_PriceBased'; |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
private static $has_many = [ |
|
|
|
|
|
|
31
|
|
|
"Rates" => SinglePostageRate::class |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
private static $many_many = [ |
|
|
|
|
|
|
35
|
|
|
"Locations" => Zone::class |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
public function getCMSFields() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->beforeUpdateCMSFields(function ($fields) { |
|
41
|
|
|
$rates_field = $fields->dataFieldByName("Rates"); |
|
42
|
|
|
|
|
43
|
|
|
if (isset($rates_field)) { |
|
44
|
|
|
$config = $rates_field->getConfig(); |
|
45
|
|
|
$config |
|
46
|
|
|
->removeComponentsByType(GridFieldDetailForm::class) |
|
47
|
|
|
->removeComponentsByType(GridFieldEditButton::class) |
|
48
|
|
|
->removeComponentsByType(GridFieldDataColumns::class) |
|
49
|
|
|
->removeComponentsByType(GridFieldDeleteAction::class) |
|
50
|
|
|
->removeComponentsByType(GridFieldAddNewButton::class) |
|
51
|
|
|
->removeComponentsByType(GridFieldAddExistingAutocompleter::class) |
|
52
|
|
|
->addComponent(new GridFieldEditableColumns()) |
|
53
|
|
|
->addComponent(new GridFieldAddNewInlineButton()) |
|
54
|
|
|
->addComponent(new GridFieldDeleteAction()); |
|
55
|
|
|
} |
|
56
|
|
|
}); |
|
57
|
|
|
|
|
58
|
|
|
return parent::getCMSFields(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* If the current parcel is located in an area that we |
|
63
|
|
|
* allow and has an acceptable weight |
|
64
|
|
|
* |
|
65
|
|
|
* @param Parcel |
|
66
|
|
|
* @return SSList |
|
|
|
|
|
|
67
|
|
|
*/ |
|
68
|
|
|
public function getPossiblePostage(Parcel $parcel) |
|
69
|
|
|
{ |
|
70
|
|
|
$return = ArrayList::create(); |
|
71
|
|
|
$locations = $this->Locations(); |
|
|
|
|
|
|
72
|
|
|
$country = $parcel->getCountry(); |
|
73
|
|
|
$region = $parcel->getRegion(); |
|
74
|
|
|
$value = (float)$parcel->getItems(); |
|
75
|
|
|
$check = false; |
|
76
|
|
|
$tax = null; |
|
77
|
|
|
|
|
78
|
|
|
if ($this->Tax()->exists()) { |
|
|
|
|
|
|
79
|
|
|
$tax = $this->Tax()->ValidTax(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// Should this type filter based on location |
|
83
|
|
|
if (!$locations->exists()) { |
|
84
|
|
|
$check = true; |
|
85
|
|
|
} elseif (isset($country) && isset($region)) { |
|
86
|
|
|
$locations = $locations->filter([ |
|
87
|
|
|
"Regions.CountryCode" => $country, |
|
88
|
|
|
"Regions.Code" => $region |
|
89
|
|
|
]); |
|
90
|
|
|
|
|
91
|
|
|
if ($locations->exists()) { |
|
92
|
|
|
$check = true; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ($check) { |
|
97
|
|
|
$rates = $this->Rates()->filter([ |
|
|
|
|
|
|
98
|
|
|
"Min:LessThanOrEqual" => $value, |
|
99
|
|
|
"Max:GreaterThanOrEqual" => $value |
|
100
|
|
|
]); |
|
101
|
|
|
|
|
102
|
|
|
foreach ($rates as $rate) { |
|
103
|
|
|
$return->add(PostageOption::create( |
|
104
|
|
|
$this->Name, |
|
|
|
|
|
|
105
|
|
|
$rate->Price, |
|
106
|
|
|
$tax |
|
107
|
|
|
)); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|