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 UncleCheese\DisplayLogic\Forms\Wrapper; |
||
12 | |||
13 | /** |
||
14 | * Class FoxyStripeOptionInventoryManager |
||
15 | * @package Dynamic\FoxyStripe\ORM |
||
16 | * |
||
17 | * @property bool $ControlInventory |
||
18 | * @property int $PurchaseLimit |
||
19 | * |
||
20 | * @property-read \Dynamic\FoxyStripe\Model\OptionItem $owner |
||
21 | */ |
||
22 | class FoxyStripeOptionInventoryManager extends DataExtension |
||
23 | { |
||
24 | /** |
||
25 | * @var array |
||
26 | 1 | */ |
|
27 | private static $db = [ |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
28 | 1 | 'ControlInventory' => 'Boolean', |
|
29 | 1 | 'PurchaseLimit' => 'Int', |
|
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * @param FieldList $fields |
||
34 | 1 | */ |
|
35 | 1 | public function updateCMSFields(FieldList $fields) |
|
36 | 1 | { |
|
37 | 1 | $fields->removeByName(array( |
|
38 | 1 | 'PurchaseLimit', |
|
39 | 1 | 'EmbargoLimit', |
|
40 | 1 | 'NumberPurchased', |
|
41 | 1 | )); |
|
42 | 1 | ||
43 | $fields->addFieldsToTab('Root.Inventory', array( |
||
44 | CheckboxField::create('ControlInventory', 'Control Inventory?') |
||
45 | ->setDescription('limit the number of this product available for purchase'), |
||
46 | Wrapper::create( |
||
47 | NumericField::create('PurchaseLimit') |
||
48 | ->setTitle('Number Available') |
||
49 | 1 | ->setDescription('add to cart form will be disabled once number available equals purchased'), |
|
50 | ReadonlyField::create('NumberPurchased', 'Purchased', $this->getNumberPurchased())//, |
||
51 | 1 | )->displayIf('ControlInventory')->isChecked()->end(), |
|
52 | )); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @return bool |
||
57 | */ |
||
58 | public function getHasInventory() |
||
59 | { |
||
60 | return $this->owner->ControlInventory && $this->owner->PurchaseLimit != 0; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return bool |
||
65 | */ |
||
66 | public function getIsOptionAvailable() |
||
67 | { |
||
68 | if ($this->getHasInventory()) { |
||
69 | return $this->owner->PurchaseLimit > $this->getNumberPurchased(); |
||
70 | } |
||
71 | return true; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return int |
||
76 | */ |
||
77 | public function getNumberAvailable() |
||
78 | 1 | { |
|
79 | if ($this->getIsOptionAvailable()) { |
||
80 | 1 | return (int)$this->owner->PurchaseLimit - (int)$this->getNumberPurchased(); |
|
81 | 1 | } |
|
82 | 1 | } |
|
83 | |||
84 | /** |
||
85 | * @return int |
||
86 | 1 | */ |
|
87 | public function getNumberPurchased() |
||
88 | { |
||
89 | $ct = 0; |
||
90 | if ($this->getOrders()) { |
||
91 | foreach ($this->getOrders() as $order) { |
||
92 | 1 | $ct += $order->Quantity; |
|
93 | } |
||
94 | 1 | } |
|
95 | 1 | return $ct; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return DataList |
||
100 | */ |
||
101 | public function getOrders() |
||
102 | { |
||
103 | 1 | if ($this->owner->ID) { |
|
104 | return OrderDetail::get()->filter('OrderOptions.ID', $this->owner->ID); |
||
105 | 1 | } |
|
106 | return false; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param $available |
||
111 | */ |
||
112 | public function updateOptionAvailability(&$available) |
||
113 | { |
||
114 | if ($this->getHasInventory()) { |
||
115 | $available = $this->getIsOptionAvailable(); |
||
116 | } |
||
117 | } |
||
118 | } |
||
119 |