Completed
Push — master ( 3c661d...2a57f9 )
by Will
26s queued 12s
created

src/Model/Modifiers/OrderModifier.php (4 issues)

1
<?php
2
3
namespace SilverShop\Model\Modifiers;
4
5
use SilverShop\Model\Order;
6
use SilverShop\Model\OrderAttribute;
7
use SilverStripe\Forms\TextField;
8
use SilverStripe\ORM\FieldType\DBCurrency;
9
use SilverStripe\ORM\FieldType\DBEnum;
10
11
/**
12
 * The OrderModifier class is a databound object for
13
 * handling the additional charges or deductions of
14
 * an order.
15
 *
16
 * @property DBCurrency $Amount
17
 * @property DBEnum $Type
18
 * @property int $Sort
19
 */
20
class OrderModifier extends OrderAttribute
21
{
22
    private static $db = [
23
        'Amount' => 'Currency',
24
        'Type' => "Enum('Chargable,Deductable,Ignored','Chargable')",
25
        'Sort' => 'Int',
26
    ];
27
28
    private static $defaults = [
29
        'Type' => 'Chargable',
30
    ];
31
32
    private static $casting = [
33
        'TableValue' => 'Currency',
34
    ];
35
36
    private static $searchable_fields = [
37
        'OrderID' => [
38
            'title' => 'Order ID',
39
            'field' => TextField::class,
40
        ],
41
        'Title' => ['filter' => 'PartialMatchFilter'],
42
        'TableTitle' => ['filter' => 'PartialMatchFilter'],
43
        'CartTitle' => ['filter' => 'PartialMatchFilter'],
44
        'Amount',
45
        'Type',
46
    ];
47
48
    private static $summary_fields = [
49
        'Order.ID' => 'Order ID',
50
        'TableTitle' => 'Table Title',
51
        'ClassName' => 'Type',
52
        'Amount' => 'Amount',
53
        'Type' => 'Type',
54
    ];
55
56
    private static $singular_name = 'Modifier';
57
58
    private static $plural_name = 'Modifiers';
59
60
    private static $default_sort = '"Sort" ASC, "Created" ASC';
61
62
    private static $table_name = 'SilverShop_OrderModifier';
63
64
    /**
65
     * Specifies whether this modifier is always required in an order.
66
     */
67
    public function required()
68
    {
69
        return true;
70
    }
71
72
    /**
73
     * Modifies the incoming value by adding,
74
     * subtracting or ignoring the value this modifier calculates.
75
     *
76
     * Sets $this->Amount to the calculated value;
77
     *
78
     * @param $subtotal - running total to be modified
79
     * @param $forcecalculation - force calculating the value, if order isn't in cart
80
     *
81
     * @return $subtotal - updated subtotal
0 ignored issues
show
Documentation Bug introduced by
The doc comment $subtotal at position 0 could not be parsed: Unknown type name '$subtotal' at position 0 in $subtotal.
Loading history...
82
     */
83
    public function modify($subtotal, $forcecalculation = false)
84
    {
85
        $order = $this->Order();
86
        $value = ($order->IsCart() || $forcecalculation) ? $this->value($subtotal) : $this->Amount;
87
        switch ($this->Type) {
88
            case 'Chargable':
89
                $subtotal += $value;
90
                break;
91
            case 'Deductable':
92
                $subtotal -= $value;
93
                break;
94
            case 'Ignored':
95
                break;
96
        }
97
        $value = round($value, Order::config()->rounding_precision);
0 ignored issues
show
It seems like $value can also be of type SilverStripe\ORM\FieldType\DBCurrency; however, parameter $val of round() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        $value = round(/** @scrutinizer ignore-type */ $value, Order::config()->rounding_precision);
Loading history...
98
        $this->Amount = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type double is incompatible with the declared type SilverStripe\ORM\FieldType\DBCurrency of property $Amount.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
99
        return $subtotal;
100
    }
101
102
    /**
103
     * Calculates value to store, based on incoming running total.
104
     *
105
     * @param float $incoming the incoming running total.
106
     */
107
    public function value($incoming)
108
    {
109
        return 0;
110
    }
111
112
    /**
113
     * Check if the modifier should be in the cart.
114
     */
115
    public function valid()
116
    {
117
        $order = $this->Order();
118
        if (!$order) {
119
            return false;
120
        }
121
        return true;
122
    }
123
124
    /**
125
     * This function is always called to determine the
126
     * amount this modifier needs to charge or deduct.
127
     *
128
     * If the modifier exists in the DB, in which case it
129
     * already exists for a given order, we just return
130
     * the Amount data field from the DB. This is for
131
     * existing orders.
132
     *
133
     * If this is a new order, and the modifier doesn't
134
     * exist in the DB ($this->ID is 0), so we return
135
     * the amount from $this->LiveAmount() which is a
136
     * calculation based on the order and it's items.
137
     */
138
    public function Amount()
139
    {
140
        return $this->Amount;
141
    }
142
143
    /**
144
     * Monetary to use in templates.
145
     */
146
    public function TableValue()
147
    {
148
        return $this->Total();
149
    }
150
151
    /**
152
     * Provides a modifier total that is positive or negative, depending on whether the modifier is chargable or not.
153
     *
154
     * @return boolean
155
     */
156
    public function Total()
157
    {
158
        if ($this->Type == 'Deductable') {
159
            return $this->Amount * -1;
160
        }
161
        return $this->Amount;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->Amount returns the type SilverStripe\ORM\FieldType\DBCurrency which is incompatible with the documented return type boolean.
Loading history...
162
    }
163
164
    /**
165
     * Checks if this modifier has type = Chargable
166
     *
167
     * @return boolean
168
     */
169
    public function IsChargable()
170
    {
171
        return $this->Type == 'Chargable';
172
    }
173
174
    /**
175
     * Checks if the modifier can be removed.
176
     *
177
     * @return boolean
178
     */
179
    public function canRemove()
180
    {
181
        return false;
182
    }
183
}
184