1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* BuyableStockDecorator |
5
|
|
|
* Extension for any buyable - adding stock level capabilities. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
class BuyableStockDecorator extends DataExtension |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Array of Class Names of classes that are buyables |
13
|
|
|
* @to do, do we need this here, why not use the original buyable dod. |
14
|
|
|
* @var Array |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
private static $buyables = array(); |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Selector for field in the HTML where quantities are set |
21
|
|
|
* @to do - move this out to template... |
22
|
|
|
* @var String |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
private static $quantity_field_selector = ""; |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Standard SS method |
29
|
|
|
* |
30
|
|
|
* @return Array |
31
|
|
|
*/ |
32
|
|
|
private static $db = array( |
|
|
|
|
33
|
|
|
'MinQuantity' => 'Int', |
34
|
|
|
'MaxQuantity' => 'Int', |
35
|
|
|
'UnlimitedStock' => 'Boolean' |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
private static $casting = array( |
|
|
|
|
39
|
|
|
'ActualQuantity' => 'Int' |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
private static $defaults = array( |
|
|
|
|
43
|
|
|
'UnlimitedStock' => 1, |
44
|
|
|
'MinQuantity' => 0, |
45
|
|
|
'MaxQuantity' => 0 |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
/* |
49
|
|
|
* Allow setting stock level in CMS |
50
|
|
|
*/ |
51
|
|
|
public function updateCMSFields(FieldList $fields) |
52
|
|
|
{ |
53
|
|
|
$tabName = 'Root.Stock'; |
54
|
|
|
$fields->addFieldsToTab( |
55
|
|
|
$tabName, |
56
|
|
|
array( |
57
|
|
|
new HeaderField('MinMaxHeader', 'Minimum and Maximum Quantities per Order', 3), |
58
|
|
|
new NumericField('MinQuantity', 'Min. Qty per order'), |
59
|
|
|
new NumericField('MaxQuantity', 'Max. Qty per order'), |
60
|
|
|
new HeaderField('ActualQuantityHeader', 'Stock available', 3), |
61
|
|
|
new CheckboxField('UnlimitedStock', 'Unlimited Stock'), |
62
|
|
|
new NumericField('ActualQuantity', 'Actual Stock Available', $this->getActualQuantity()), |
63
|
|
|
new HeaderField('ActualQuantityAdjustmentHeader', 'Adjust all stock', 3), |
64
|
|
|
new LiteralField('ActualQuantityAdjustmentLink', 'This CMS also provides a <a href="/update-stock/" target="_blank">quick stock adjuster</a>.') |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/* |
70
|
|
|
* Getter for stock level |
71
|
|
|
*/ |
72
|
|
|
public function ActualQuantity() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
return $this->getActualQuantity(); |
75
|
|
|
} |
76
|
|
|
public function getActualQuantity() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
return BuyableStockCalculatedQuantity::get_quantity_by_buyable($this->owner); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/* |
82
|
|
|
* Setter for stock level |
83
|
|
|
* @param int $value |
84
|
|
|
*/ |
85
|
|
|
public function setActualQuantity($value) |
86
|
|
|
{ |
87
|
|
|
if (!$this->owner->ID) { |
88
|
|
|
$this->owner->write(); |
89
|
|
|
} |
90
|
|
|
//only set stock level if it differs from previous |
91
|
|
|
$shopAdminCode = EcommerceConfig::get("EcommerceRole", "admin_permission_code"); |
92
|
|
|
if ($this->owner->ID) { |
93
|
|
|
if ($shopAdminCode && Permission::check($shopAdminCode)) { |
94
|
|
|
if ($value != $this->owner->getActualQuantity()) { |
95
|
|
|
$parent = BuyableStockCalculatedQuantity::get_by_buyable($this->owner); |
96
|
|
|
if ($parent) { |
97
|
|
|
$member = Member::currentUser(); |
98
|
|
|
$obj = new BuyableStockManualUpdate(); |
99
|
|
|
$obj->ParentID = $parent->ID; |
|
|
|
|
100
|
|
|
$obj->Quantity = (int)$value; |
|
|
|
|
101
|
|
|
$obj->MemberID = $member->ID; |
|
|
|
|
102
|
|
|
$obj->write(); |
103
|
|
|
} else { |
104
|
|
|
user_error("Could not write BuyableStockCalculatedQuantity Object because there was no parent ".$this->owner->Title); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
|
|
user_error("Could not write BuyableStockCalculatedQuantity Object because you do not have permissions ".$this->owner->Title); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* This is a pivotal method. |
116
|
|
|
* Only allow purchase if stock levels allow |
117
|
|
|
* TODO: customise this to a certain stock level, on, or off |
118
|
|
|
* @return null (equals TRUE!), true or false |
|
|
|
|
119
|
|
|
*/ |
120
|
|
|
public function canPurchase($member = null) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
if ($this->owner->UnlimitedStock) { |
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
if ($this->owner->getActualQuantity() < $this->owner->MinQuantity) { |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
if ($this->owner->getActualQuantity() <= 0) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
return null; //returning null ensures that the value from this method is ignored. |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* stanard SS metehod |
136
|
|
|
*/ |
137
|
|
|
public function onAfterWrite() |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
BuyableStockCalculatedQuantity::get_by_buyable($this->owner); |
140
|
|
|
if (isset($_REQUEST["ActualQuantity"])) { |
141
|
|
|
$actualQuantity = intval($_REQUEST["ActualQuantity"]); |
142
|
|
|
if ($actualQuantity != $this->owner->getActualQuantity() && ($actualQuantity === 0 || $actualQuantity)) { |
143
|
|
|
$this->owner->setActualQuantity($actualQuantity); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
class BuyableStockDecorator_Extension extends Extension |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
/** |
152
|
|
|
* TO DO: review method below |
153
|
|
|
* - move to init??? |
154
|
|
|
* - decorate Ecommerce Quantity Field? |
155
|
|
|
* |
156
|
|
|
**/ |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
public function index() |
160
|
|
|
{ |
161
|
|
|
$min = 0; |
162
|
|
|
$max = 0; |
163
|
|
|
$msg = Config::inst()->get("MinMaxModifier", "sorry_message"); |
164
|
|
|
$fieldSelector = Config::inst()->get("BuyableStockDecorator", "quantity_field_selector"); |
165
|
|
|
if ($minField = Config::inst()->get("MinMaxModifier", "min_field")) { |
166
|
|
|
$min = $this->owner->$minField; |
167
|
|
|
} |
168
|
|
|
if ($maxField = Config::inst()->get("MinMaxModifier", "max_field")) { |
169
|
|
|
$max = $this->owner->$maxField; |
170
|
|
|
} |
171
|
|
|
$js = ' |
172
|
|
|
MinMaxModifierData = []; |
173
|
|
|
MinMaxModifierData.push( |
174
|
|
|
{ |
175
|
|
|
selector: "'.$fieldSelector.'", |
176
|
|
|
min: '.intval($min).', |
177
|
|
|
max: '.intval($max).', |
178
|
|
|
msg: "'.addslashes($msg).'" |
179
|
|
|
} |
180
|
|
|
);'; |
181
|
|
|
Requirements::javascript("ecommerce_stockcontrol/javascript/MinMaxModifier.js"); |
182
|
|
|
Requirements::customScript($js, $fieldSelector); |
183
|
|
|
return array(); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.