|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\ORM; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\Model\OrderDetail; |
|
6
|
|
|
use SilverStripe\Forms\FieldList; |
|
7
|
|
|
use SilverStripe\Forms\CheckboxField; |
|
8
|
|
|
use SilverStripe\Forms\NumericField; |
|
9
|
|
|
use SilverStripe\Forms\ReadonlyField; |
|
10
|
|
|
use SilverStripe\ORM\DataExtension; |
|
11
|
|
|
use SilverStripe\Forms\DropdownField; |
|
12
|
|
|
use SilverStripe\Core\Extension; |
|
13
|
|
|
use UncleCheese\DisplayLogic\Forms\Wrapper; |
|
14
|
|
|
|
|
15
|
|
|
class FoxyStripeInventoryManager extends DataExtension |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private static $db = [ |
|
|
|
|
|
|
21
|
|
|
'ControlInventory' => 'Boolean', |
|
22
|
|
|
'PurchaseLimit' => 'Int', |
|
23
|
|
|
'EmbargoLimit' => 'Int', |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param FieldList $fields |
|
28
|
|
|
*/ |
|
29
|
1 |
|
public function updateCMSFields(FieldList $fields) |
|
30
|
|
|
{ |
|
31
|
1 |
|
$fields->removeByName(array( |
|
32
|
1 |
|
'PurchaseLimit', |
|
33
|
|
|
'EmbargoLimit', |
|
34
|
|
|
'NumberPurchased', |
|
35
|
|
|
)); |
|
36
|
|
|
|
|
37
|
1 |
|
$fields->addFieldsToTab('Root.Inventory', array( |
|
38
|
1 |
|
CheckboxField::create('ControlInventory', 'Control Inventory?') |
|
39
|
1 |
|
->setDescription('limit the number of this product available for purchase'), |
|
40
|
1 |
|
Wrapper::create( |
|
41
|
1 |
|
NumericField::create('PurchaseLimit') |
|
42
|
1 |
|
->setTitle('Number Available') |
|
43
|
1 |
|
->setDescription('add to cart form will be disabled once number available equals purchased'), |
|
44
|
1 |
|
ReadonlyField::create('NumberPurchased', 'Purchased', $this->getNumberPurchased())//, |
|
45
|
|
|
/* |
|
|
|
|
|
|
46
|
|
|
NumericField::create('EmbargoLimit') |
|
47
|
|
|
->setTitle('Embargo Time') |
|
48
|
|
|
->setDescription('time in seconds to reserve an item once added to cart') |
|
49
|
|
|
*/ |
|
50
|
1 |
|
)->displayIf('ControlInventory')->isChecked()->end(), |
|
51
|
|
|
)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function getHasInventory() |
|
58
|
|
|
{ |
|
59
|
1 |
|
return $this->owner->ControlInventory && $this->owner->PurchaseLimit != 0; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function getIsProductAvailable() |
|
66
|
|
|
{ |
|
67
|
1 |
|
if ($this->owner->getHasInventory()) { |
|
68
|
1 |
|
return $this->owner->PurchaseLimit > $this->getNumberPurchased(); |
|
69
|
|
|
} |
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return int |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getNumberAvailable() |
|
77
|
|
|
{ |
|
78
|
|
|
if ($this->getIsProductAvailable()) { |
|
79
|
|
|
return (int)$this->owner->PurchaseLimit - (int)$this->getNumberPurchased(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return int |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function getNumberPurchased() |
|
87
|
|
|
{ |
|
88
|
2 |
|
$ct = 0; |
|
89
|
2 |
|
if ($this->getOrders()) { |
|
90
|
2 |
|
foreach ($this->getOrders() as $order) { |
|
91
|
2 |
|
$ct += $order->Quantity; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
return $ct; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return DataList |
|
|
|
|
|
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function getOrders() |
|
102
|
|
|
{ |
|
103
|
2 |
|
if ($this->owner->ID) { |
|
104
|
2 |
|
return OrderDetail::get()->filter('ProductID', $this->owner->ID); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
return false; |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|