1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class FoxyStripeOptionInventoryManager extends DataExtension |
4
|
|
|
{ |
5
|
|
|
/** |
6
|
|
|
* @var array |
7
|
|
|
*/ |
8
|
|
|
private static $db = [ |
9
|
|
|
'ControlInventory' => 'Boolean', |
10
|
|
|
'PurchaseLimit' => 'Int', |
11
|
|
|
]; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param FieldList $fields |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
public function updateCMSFields(FieldList $fields) |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$fields->removeByName(array( |
19
|
|
|
'PurchaseLimit', |
20
|
|
|
'EmbargoLimit', |
21
|
|
|
'NumberPurchased', |
22
|
|
|
)); |
23
|
|
|
|
24
|
|
|
$fields->addFieldsToTab('Root.Inventory', array( |
25
|
|
|
CheckboxField::create('ControlInventory', 'Control Inventory?') |
26
|
|
|
->setDescription('limit the number of this product available for purchase'), |
27
|
|
|
DisplayLogicWrapper::create( |
28
|
|
|
NumericField::create('PurchaseLimit') |
29
|
|
|
->setTitle('Number Available') |
30
|
|
|
->setDescription('add to cart form will be disabled once number available equals purchased'), |
31
|
|
|
ReadonlyField::create('NumberPurchased', 'Purchased', $this->getNumberPurchased())//, |
32
|
|
|
)->displayIf('ControlInventory')->isChecked()->end(), |
33
|
|
|
)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
|
|
public function getHasInventory() |
40
|
|
|
{ |
41
|
|
|
return $this->owner->ControlInventory && $this->owner->PurchaseLimit != 0; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function getIsOptionAvailable() |
48
|
|
|
{ |
49
|
|
|
if ($this->getHasInventory()) { |
50
|
|
|
return $this->owner->PurchaseLimit > $this->getNumberPurchased(); |
51
|
|
|
} |
52
|
|
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return int |
57
|
|
|
*/ |
58
|
|
|
public function getNumberAvailable() |
59
|
|
|
{ |
60
|
|
|
if ($this->getIsOptionAvailable()) { |
61
|
|
|
return (int)$this->owner->PurchaseLimit - (int)$this->getNumberPurchased(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function getNumberPurchased() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$ct = 0; |
71
|
|
|
if ($this->getOrders()) { |
72
|
|
|
foreach ($this->getOrders() as $order) { |
73
|
|
|
$ct += $order->Quantity; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
return $ct; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return DataList |
81
|
|
|
*/ |
82
|
|
|
public function getOrders() |
83
|
|
|
{ |
84
|
|
|
if ($this->owner->ID) { |
85
|
|
|
return OrderDetail::get()->filter('Options.ID', $this->owner->ID); |
86
|
|
|
} |
87
|
|
|
return false; |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $available |
92
|
|
|
*/ |
93
|
|
|
public function updateOptionAvailability(&$available) |
94
|
|
|
{ |
95
|
|
|
if ($this->getHasInventory()) { |
96
|
|
|
$available = $this->getIsOptionAvailable(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.