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