Passed
Push — master ( d83585...c5f1b0 )
by Jason
10:27
created

ProductInventoryManager::validate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 3
nc 2
nop 1
1
<?php
2
3
namespace Dynamic\Foxy\Inventory\Extension;
4
5
use Dynamic\Foxy\Inventory\Model\CartReservation;
6
use Dynamic\Foxy\Orders\Model\OrderDetail;
7
use SilverStripe\Forms\CheckboxField;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\NumericField;
10
use SilverStripe\Forms\ReadonlyField;
11
use SilverStripe\ORM\DataExtension;
12
use SilverStripe\ORM\DataList;
13
use SilverStripe\ORM\ValidationResult;
14
use UncleCheese\DisplayLogic\Forms\Wrapper;
15
16
class ProductInventoryManager extends DataExtension
17
{
18
    /**
19
     * @var array
20
     */
21
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
22
        'ControlInventory' => 'Boolean',
23
        'PurchaseLimit' => 'Int',
24
    ];
25
26
    /**
27
     * @param FieldList $fields
28
     */
29
    public function updateCMSFields(FieldList $fields)
30
    {
31
        $fields->removeByName([
32
            'PurchaseLimit',
33
            'EmbargoLimit',
34
            'NumberPurchased',
35
        ]);
36
37
        $fields->addFieldsToTab('Root.Inventory', [
38
            Wrapper::create(
39
                CheckboxField::create('ControlInventory', 'Control Inventory?')
40
                    ->setDescription('limit the number of this product available for purchase'),
41
                Wrapper::create(
42
                    NumericField::create('PurchaseLimit')
43
                        ->setTitle('Number Available')
44
                        ->setDescription('add to cart form will be disabled once number available equals purchased'),
45
                    ReadonlyField::create('NumberAvailable', 'Remaining Available', $this->getNumberAvailable())
46
                        ->setDescription('This takes into account products added to the cart. Products removed from the cart may persist in the "Cart Reservations" until the expiration time.')//,
47
                )->displayIf('ControlInventory')->isChecked()->end()
48
            )->displayIf('Available')->isChecked()->end(),
49
        ]);
50
    }
51
52
    /**
53
     * @param ValidationResult $validationResult
54
     * @throws \SilverStripe\ORM\ValidationException
55
     */
56
    public function validate(ValidationResult $validationResult)
57
    {
58
        parent::validate($validationResult);
59
60
        if ($this->owner->ControlInventory && $this->owner->PurchaseLimit == 0) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->owner->PurchaseLimit of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
61
            $validationResult->addFieldError('PurchaseLimit', 'You must specify a purchase limit more than 0');
62
        }
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function getHasInventory()
69
    {
70
        return $this->owner->ControlInventory && $this->owner->PurchaseLimit != 0;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->owner->PurchaseLimit of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
71
    }
72
73
    /**
74
     * @return bool
75
     */
76
    public function getIsProductAvailable()
77
    {
78
        if ($this->owner->getHasInventory()) {
79
            return $this->getNumberAvailable() > 0;
80
        }
81
82
        return true;
83
    }
84
85
    /**
86
     * @return int|void
87
     */
88
    public function getNumberAvailable()
89
    {
90
        return (int)$this->owner->PurchaseLimit - (int)$this->getNumberPurchased() - (int)$this->getCartReservations()->count();
91
    }
92
93
    /**
94
     * @return int
95
     */
96
    public function getNumberPurchased()
97
    {
98
        return OrderDetail::get()->filter('ProductID', $this->owner->ID)->sum('Quantity');
0 ignored issues
show
Bug Best Practice introduced by
The expression return Dynamic\Foxy\Orde...r->ID)->sum('Quantity') returns the type string which is incompatible with the documented return type integer.
Loading history...
99
    }
100
101
    /**
102
     * @return DataList|bool
103
     */
104
    public function getOrders()
105
    {
106
        if ($this->owner->ID) {
107
            return OrderDetail::get()->filter('ProductID', $this->owner->ID);
108
        }
109
110
        return false;
111
    }
112
113
    /**
114
     * @return DataList
115
     */
116
    public function getCartReservations()
117
    {
118
        return CartReservation::get()
119
            ->filter([
120
                'ProductID' => $this->owner->ID,
121
                'Expires:GreaterThan' => date('Y-m-d H:i:s', strtotime('now')),
122
            ]);
123
    }
124
}
125