AnyPriceRoundUpDonationModifier::LiveTableValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @author Nicolaas [at] sunnysideup.co.nz
5
 * @package: ecommerce
6
 * @sub-package: ecommerce_delivery
7
 * @description: allows you to add a modifier at checkout where the customer is prompted to
8
 * add a donation rounding up to the next round number.
9
 *
10
 * The trick here is to work out the value of the donation
11
 * from the total without the donation and then add it to the donation
12
 * to get to a round numer.
13
 *
14
 */
15
class AnyPriceRoundUpDonationModifier extends OrderModifier
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
16
{
17
18
// ######################################## *** model defining static variables (e.g. $db, $has_one)
19
20
    /**
21
     * add extra fields as you need them.
22
     *
23
     **/
24
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
25
        "ModifierTotalExcludingDonation" => "Currency",
26
        "SubTotal" => "Currency",
27
        "OtherValue" => "Currency",
28
        "AddDonation" => "Boolean"
29
    );
30
31
    // ######################################## *** cms variables + functions (e.g. getCMSFields, $searchableFields)
32
33
    public function getCMSFields()
34
    {
35
        $fields = parent::getCMSFields();
36
        return $fields;
37
    }
38
39
    private static $singular_name = "Round Up Donation";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $singular_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
40
    public function i18n_singular_name()
41
    {
42
        return _t("AnyPriceRoundUpDonationModifier.ROUNDUPDONATION", "Round Up Donation");
43
    }
44
45
    private static $plural_name = "Round Up Donations";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $plural_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
46
    public function i18n_plural_name()
47
    {
48
        return _t("AnyPriceRoundUpDonationModifier.ROUNDUPDONATIONS", "Round Up Donations");
49
    }
50
51
    // ######################################## *** other (non) static variables (e.g. private static $special_name_for_something, protected $order)
52
53
54
    /**
55
     * Maximum Round Up
56
     * to which the donation should round.
57
     * +1 = nearest 10, e.g. 73.45 rounds to 80
58
     * 0 = nearest rounded integer - e.g. 73.45 rounds to 74
59
     * -1 = nearest 10 cents - e.g. 73.45 rounds to 73.50
60
     *
61
     * @var Int
62
     */
63
    private static $precision = 1;
0 ignored issues
show
Unused Code introduced by
The property $precision is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
64
65
    /**
66
     * Maximum Round Up - modifier will ensure that the round up is no more
67
     * than the number specified here.
68
     * @var Int
69
     */
70
    private static $maximum_round_up = 5;
0 ignored issues
show
Unused Code introduced by
The property $maximum_round_up is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
71
72
    private static $round_up_even_if_there_is_nothing_to_round = true;
0 ignored issues
show
Unused Code introduced by
The property $round_up_even_if_there_is_nothing_to_round is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
73
74
    private static $include_form_in_order_table = true;
75
76
    // ######################################## *** CRUD functions (e.g. canEdit)
77
    // ######################################## *** init and update functions
78
79
    /**
80
     * For all modifers with their own database fields, we need to include this...
81
     * It will update each of the fields.
82
     * With this, we also need to create the methods
83
     * Live{functionName}
84
     * e.g LiveMyField() and LiveMyReduction() in this case...
85
     * @param Bool $force - run it, even if it has run already
86
     */
87
    public function runUpdate($force = false)
88
    {
89
        $this->checkField("AddDonation");
90
        $this->checkField("OtherValue");
91
        $this->checkField("SubTotal");
92
        $this->checkField("ModifierTotalExcludingDonation");
93
        parent::runUpdate($force);
94
    }
95
96
    /**
97
     * allows you to save a new value AddDonation
98
     * @param Boolean $b
99
     */
100
    public function updateAddDonation($b)
101
    {
102
        $this->AddDonation = $b ? 1 : 0;
0 ignored issues
show
Documentation introduced by
The property AddDonation does not exist on object<AnyPriceRoundUpDonationModifier>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
        $this->write();
104
    }
105
106
    /**
107
     * allows you to save a new value OtherValue
108
     * @param float
109
     */
110
    public function updateOtherValue($f)
111
    {
112
        $this->OtherValue = $f;
0 ignored issues
show
Documentation introduced by
The property OtherValue does not exist on object<AnyPriceRoundUpDonationModifier>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
113
        $this->write();
114
    }
115
116
117
    // ######################################## *** form functions (e. g. Showform and getform)
118
119
    /**
120
     * standard OrderModifier Method
121
     * Should we show a form in the checkout page for this modifier?
122
     */
123
    public function ShowForm()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
124
    {
125
        /*$ajaxObject = $this->AJAXDefinitions();
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
126
        //TableValue is a database value
127
        $tableID = $ajaxObject->TableID();
128
        if(!$this->hasDonation()) {
129
            Requirements::customScript("jQuery(document).ready(function() {jQuery(\"#".$tableID."\").hide();});", "hide$tableID");
130
        }*/
131
        return $this->Order()->Items();
132
    }
133
134
    /**
135
     * Should the form be included in the editable form
136
     * on the checkout page?
137
     * @return Boolean
138
     */
139
    public function ShowFormInEditableOrderTable()
140
    {
141
        return ($this->ShowForm() && self::$include_form_in_order_table) ? true : false;
142
    }
143
144
    /**
145
     * standard OrderModifier Method
146
     * This method returns the form for the checkout page.
147
     * @param Object $controller = Controller object for form
0 ignored issues
show
Documentation introduced by
There is no parameter named $controller. Did you maybe mean $optionalController?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
148
     * @return Object - AnyPriceRoundUpDonationModifier
149
     */
150
    public function getModifierForm(Controller $optionalController = null, Validator $optionalValidator = null)
151
    {
152
        $fields = new FieldList();
153
        $fields->push($this->headingField());
154
        $fields->push($this->descriptionField());
155
        $maxRoundUpObject = DBField::create_field('Currency', Config::inst()->get("AnyPriceRoundUpDonationModifier", 'maximum_round_up'));
156
        $checkFieldTitle = sprintf(
157
            _t("AnyPriceRoundUpDonationModifier.ADDDONATION", "Add round up donation (maximum added %s)?"),
158
            $maxRoundUpObject->Nice()
159
        );
160
        $checkField = new DropdownField(
161
            'AddDonation',
162
            $checkFieldTitle,
163
            array(
164
                _t("AnyPriceRoundUpDonationModifier.NO", 'No'),
165
                _t("AnyPriceRoundUpDonationModifier.YES", 'Yes')
166
            ),
167
            $this->AddDonation
0 ignored issues
show
Documentation introduced by
The property AddDonation does not exist on object<AnyPriceRoundUpDonationModifier>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
168
        );
169
        $fields->push($checkField);
170
        $fields->push(new NumericFIeld('OtherValue', _t("AnyPriceRoundUpDonationModifier.OTHERVALUE", "Other Value"), $this->OtherValue));
0 ignored issues
show
Documentation introduced by
The property OtherValue does not exist on object<AnyPriceRoundUpDonationModifier>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
171
        $actions = new FieldList(
172
            new FormAction('submit', 'Update Order')
173
        );
174
        return new AnyPriceRoundUpDonationModifier_Form($optionalController, 'AnyPriceRoundUpDonationModifier', $fields, $actions, $optionalValidator);
175
    }
176
177
    // ######################################## *** template functions (e.g. ShowInTable, TableTitle, etc...) ... USES DB VALUES
178
179
180
    /**
181
     * This has to be set to true, because it can be added by form using AJAX.
182
     * @return Boolean
183
     */
184
    public function ShowInTable()
185
    {
186
        if ($this->Order()->IsSubmitted()) {
187
            return $this->TableValue > 0;
0 ignored issues
show
Documentation introduced by
The property TableValue does not exist on object<AnyPriceRoundUpDonationModifier>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
188
        }
189
        return true;
190
    }
191
192
    /**
193
     * Removed via form instead.
194
     * @return Boolean
195
     */
196
    public function CanBeRemoved()
197
    {
198
        return false;
199
    }
200
201
    // ######################################## ***  inner calculations.... USES CALCULATED VALUES
202
203
204
    /**
205
     * Works out if there is a donation at all.
206
     *
207
     *@return Boolean
208
     */
209
    protected function hasDonation()
210
    {
211
        if (($this->LiveAddDonation() && Config::inst()->get("AnyPriceRoundUpDonationModifier", 'maximum_round_up') > 0) || $this->OtherValue > 0) {
0 ignored issues
show
Documentation introduced by
The property OtherValue does not exist on object<AnyPriceRoundUpDonationModifier>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $this->LiveAddDon... $this->OtherValue > 0;.
Loading history...
212
            return true;
213
        }
214
        return false;
215
    }
216
    /**
217
     * Works out the total round up amount, using both the
218
     * sub-total and the modifier total.
219
     *
220
     *@return Float
221
     */
222
    protected function workOutRoundUpAmount()
223
    {
224
        if ($this->hasDonation()) {
225
            if ($this->OtherValue > 0) {
0 ignored issues
show
Documentation introduced by
The property OtherValue does not exist on object<AnyPriceRoundUpDonationModifier>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
226
                $actualAdditionToTotal = $this->OtherValue;
0 ignored issues
show
Documentation introduced by
The property OtherValue does not exist on object<AnyPriceRoundUpDonationModifier>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
227
            } else {
228
                $totalExcludingDonation = $this->LiveSubTotal() + $this->LiveModifierTotalExcludingDonation();
229
                $precisionMultiplier = pow(10, Config::inst()->get('AnyPriceRoundUpDonationModifier', 'precision'));
230
                $totalMultipliedByPrecision = $totalExcludingDonation / $precisionMultiplier;
231
                $roundedTotalMultipliedByPrecision = ceil($totalMultipliedByPrecision);
232
                $actualAdditionToTotal = ($roundedTotalMultipliedByPrecision * $precisionMultiplier) - $totalExcludingDonation;
233
                while ($actualAdditionToTotal > Config::inst()->get("AnyPriceRoundUpDonationModifier", 'maximum_round_up') && $actualAdditionToTotal > 0) {
234
                    $actualAdditionToTotal = $actualAdditionToTotal - Config::inst()->get("AnyPriceRoundUpDonationModifier", 'maximum_round_up');
235
                }
236
                if (Config::inst()->get('AnyPriceRoundUpDonationModifier', 'round_up_even_if_there_is_nothing_to_round') && $actualAdditionToTotal == 0) {
237
                    $actualAdditionToTotal = Config::inst()->get("AnyPriceRoundUpDonationModifier", 'maximum_round_up');
238
                }
239
            }
240
        } else {
241
            $actualAdditionToTotal = 0;
242
        }
243
        return $actualAdditionToTotal;
244
    }
245
246
247
    // ######################################## *** calculate database fields: protected function Live[field name]  ... USES CALCULATED VALUES
248
249
    /**
250
     * if we want to change the default value for the Name field
251
     * (defined in the OrderModifer class) then we can do this
252
     * as shown in the method below.
253
     * You may choose to return an empty string or just a standard message.
254
     *
255
     *
256
     **/
257
    protected function LiveName()
258
    {
259
        if ($this->OtherValue > 0) {
0 ignored issues
show
Documentation introduced by
The property OtherValue does not exist on object<AnyPriceRoundUpDonationModifier>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
260
            return _t("AnyPriceRoundUpDonationModifier.DONATION", "Donation");
261
        } elseif ($this->hasDonation()) {
262
            return _t("AnyPriceRoundUpDonationModifier.ROUNDUPDONATION", "Round up donation");
263
        } else {
264
            return _t("AnyPriceRoundUpDonationModifier.NOROUNDUPDONATION", "No round up donation added");
265
        }
266
    }
267
268
    /**
269
     *
270
     * @return Boolean
271
     **/
272
    protected function LiveAddDonation()
273
    {
274
        return $this->AddDonation;
0 ignored issues
show
Documentation introduced by
The property AddDonation does not exist on object<AnyPriceRoundUpDonationModifier>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
275
    }
276
277
    /**
278
     *
279
     * @return Float
280
     **/
281
    protected function LiveOtherValue()
282
    {
283
        return $this->OtherValue;
0 ignored issues
show
Documentation introduced by
The property OtherValue does not exist on object<AnyPriceRoundUpDonationModifier>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
284
    }
285
286
    /**
287
     * Work out sub total amount for order
288
     * @return float
289
     **/
290
    protected function LiveSubTotal()
291
    {
292
        if ($this->hasDonation()) {
293
            $order = $this->Order();
294
            return $order->SubTotal();
295
        } else {
296
            return 0;
297
        }
298
    }
299
300
    /**
301
     * Work out modifier total excluding donation
302
     * @return float
0 ignored issues
show
Documentation introduced by
Should the return type not be double|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
303
     **/
304
    protected function LiveModifierTotalExcludingDonation()
305
    {
306
        if ($this->hasDonation()) {
307
            $modifiersTotal = 0;
308
            $order = $this->Order();
309
            if ($order) {
310
                if ($modifiers = $order->Modifiers()) {
311
                    foreach ($modifiers as $modifier) {
312
                        if (!$modifier->IsRemoved()) { //we just doubledouble-check this...
313
                            if ($modifier instanceof $this->ClassName) {
314
                                $totalForModifier = 0;
315
                            } else {
316
                                $totalForModifier = $modifier->CalculationTotal();
317
                            }
318
                            $modifiersTotal += floatval($totalForModifier);
319
                        }
320
                    }
321
                }
322
            }
323
            return $modifiersTotal;
324
        } else {
325
            return 0;
326
        }
327
    }
328
329
    protected function LiveCalculatedTotal()
330
    {
331
        if ($this->hasDonation()) {
332
            return $this->workOutRoundUpAmount();
333
        } else {
334
            return 0;
335
        }
336
    }
337
338
    public function LiveTableValue()
339
    {
340
        return $this->LiveCalculatedTotal();
341
    }
342
343
344
    // ######################################## *** Type Functions (IsChargeable, IsDeductable, IsNoChange, IsRemoved)
345
346
    private static $table_sub_title;
0 ignored issues
show
Unused Code introduced by
The property $table_sub_title is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
347
348
    public function getTableSubTitle()
349
    {
350
        return _t('AnyPriceRoundUpDonationModifier.TABLESUBTITLE', $this->stat('table_sub_title'));
351
    }
352
353
    // ######################################## *** standard database related functions (e.g. onBeforeWrite, onAfterWrite, etc...)
354
355
    public function onBeforeWrite()
356
    {
357
        parent::onBeforeWrite();
358
    }
359
360
361
    // ######################################## *** AJAX related functions
362
    /**
363
    * some modifiers can be hidden after an ajax update (e.g. if someone enters a discount coupon and it does not exist).
364
    * There might be instances where ShowInTable (the starting point) is TRUE and HideInAjaxUpdate return false.
365
    *@return Boolean
366
    **/
367
    public function HideInAjaxUpdate()
368
    {
369
        //we check if the parent wants to hide it...
370
        //we need to do this first in case it is being removed.
371
        if (parent::HideInAjaxUpdate()) {
372
            return true;
373
        }
374
        // we do NOT hide it if values have been entered
375
        if ($this->hasDonation() || $this->ShowFormInEditableOrderTable()) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !($this->hasDonat...nEditableOrderTable());.
Loading history...
376
            return false;
377
        }
378
        return true;
379
    }
380
    // ######################################## *** debug functions
381
}
382
383
class AnyPriceRoundUpDonationModifier_Form extends OrderModifierForm
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
384
{
385
    public function __construct($optionalController = null, $name, $fields, $actions, $optionalValidator = null)
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
386
    {
387
        parent::__construct($optionalController, $name, $fields, $actions, $optionalValidator);
388
        Requirements::javascript("ecommerce_anypriceproduct/javascript/AnyPriceRoundUpDonationModifier.js");
389
    }
390
391
    public function submit(array $data, Form $form, $message = "order updated", $status = "good")
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
392
    {
393
        $order = ShoppingCart::current_order();
394
        if ($order) {
395
            if ($modifiers = $order->Modifiers("AnyPriceRoundUpDonationModifier")) {
396
                $msg = "";
397
                foreach ($modifiers as $modifier) {
398
                    if (isset($data['AddDonation']) && $data['AddDonation']) {
399
                        $modifier->updateAddDonation(true);
400
                        $msg .= _t("AnyPriceRoundUpDonationModifier.UPDATED", "Round up donation added - THANK YOU.");
401
                    } else {
402
                        $modifier->updateAddDonation(false);
403
                        $msg .= _t("AnyPriceRoundUpDonationModifier.REMOVED", "Round up donation removed.");
404
                    }
405 View Code Duplication
                    if (isset($data['OtherValue'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
406
                        $modifier->updateOtherValue(floatval($data['OtherValue']));
407
                        if (floatval($data['OtherValue']) > 0) {
408
                            //here we replace the message!
409
                            $msg = _t("AnyPriceRoundUpDonationModifier.UPDATED_OTHER", "Added donation - THANK YOU.");
410
                        }
411
                    } else {
412
                        $modifier->updateOtherValue(0);
413
                    }
414
                    $modifier->write();
415
                }
416
                return ShoppingCart::singleton()->setMessageAndReturn($msg, "good");
417
            }
418
        }
419
        return ShoppingCart::singleton()->setMessageAndReturn(_t("AnyPriceRoundUpDonationModifier.NOTUPDATED", "Could not update the round up donation status.", "bad"));
420
    }
421
}
422