Issues (42)

src/Model/DiscountTier.php (4 issues)

1
<?php
2
3
namespace Dynamic\Foxy\Discounts\Model;
4
5
use Dynamic\Foxy\Coupons\Model\Coupon;
0 ignored issues
show
The type Dynamic\Foxy\Coupons\Model\Coupon was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\ORM\DataObject;
8
9
/**
10
 * Class DiscountTier
11
 * @package Dynamic\Foxy\Discounts\Model
12
 *
13
 * @property string $ParentType Save the parent Discount's type so we don't need to run a query
14
 * @property int $Quantity
15
 * @property int $Percentage
16
 * @property float $Amount
17
 * @property int $DiscountID
18
 * @method Discount Discount()
19
 */
20
class DiscountTier extends DataObject
21
{
22
    /**
23
     * @var array
24
     */
25
    private static $db = [
26
        'ParentType' => 'Varchar',
27
        'Quantity' => 'Int',
28
        'Percentage' => 'Int',
29
        'Amount' => 'Currency',
30
    ];
31
32
    /**
33
     * @var array
34
     */
35
    private static $has_one = [
36
        'Discount' => Discount::class,
37
    ];
38
39
    /**
40
     * @var array
41
     */
42
    private static $defaults = [
43
        'Quantity' => 1,
44
    ];
45
46
    /**
47
     * @var array
48
     */
49
    private static $summary_fields = [
50
        'DiscountLabel' => [
51
            'title' => 'Discount',
52
        ],
53
        'Quantity',
54
    ];
55
56
    /**
57
     * @var string
58
     */
59
    private static $table_name = 'FoxyDiscountTier';
60
61
    /**
62
     * @var array
63
     */
64
    private static $default_sort = [
65
        'Quantity',
66
    ];
67
68
    /**
69
     * @return FieldList|void
70
     */
71
    public function getCMSFields()
72
    {
73
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
74
            $fields->removeByName([
75
                'DiscountID',
76
                'ParentType',
77
            ]);
78
79
            $quantity = $fields->dataFieldByName('Quantity');
80
            $quantity->setTitle('Quantity to trigger discount');
81
82
            /** @var Discount|Coupon $type */
83
            if ($parent = $this->getParent()) {
84
                $type = $parent->Type;
0 ignored issues
show
The property Type does not exist on string.
Loading history...
85
86
                $percentage = $fields->dataFieldByName('Percentage')
87
                    ->setTitle('Percent discount');
88
                $amount = $fields->dataFieldByName('Amount')
89
                    ->setTitle('Amount to discount');
90
91
                $fields->removeByName([
92
                    'Percentage',
93
                    'Amount',
94
                ]);
95
96
                if ($type == 'Percent') {
97
                    $fields->addFieldToTab(
98
                        'Root.Main',
99
                        $percentage
100
                    );
101
                } elseif ($type == 'Amount') {
102
                    $fields->addFieldToTab(
103
                        'Root.Main',
104
                        $amount
105
                    );
106
                }
107
            }
108
        });
109
110
        return parent::getCMSFields();
111
    }
112
113
    protected function onBeforeWrite()
114
    {
115
        parent::onBeforeWrite();
116
117
        $this->ParentType = $this->Discount()->Type;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    protected function getParent()
124
    {
125
        foreach ($this->hasOne() as $relationName => $className) {
126
            $field = "{$relationName}ID";
127
128
            if ($this->{$field} > 0) {
129
                return $className::get()->byID($this->{$field});
130
            }
131
        }
132
133
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
134
    }
135
136
    /**
137
     * @return \SilverStripe\ORM\ValidationResult|void
138
     */
139
    public function validate()
140
    {
141
        $response = parent::validate();
142
143
        if ($this->exists()) {
144
            $exclude['ID'] = $this->ID;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$exclude was never initialized. Although not strictly required by PHP, it is generally a good practice to add $exclude = array(); before regardless.
Loading history...
145
        }
146
147
        /** @var Discount $discount */
148
        if ($discount = Discount::get()->byID($this->DiscountID)) {
149
            $existing = $discount->DiscountTiers()->filter('Quantity', $this->Quantity);
150
            if (isset($exclude)) {
151
                $existing = $existing->exclude($exclude);
152
            }
153
154
            if ($existing->count() > 0) {
155
                $response->addError("A discount tier already has the quantity {$this->Quantity} set");
156
            }
157
        }
158
159
        return $response;
160
    }
161
162
    /**
163
     * @return string
164
     */
165
    public function getDiscountLabel()
166
    {
167
        $label = '';
168
169
        if ($this->ParentType == 'Percent') {
170
            $label = "{$this->Percentage}%";
171
        } elseif ($this->ParentType == 'Amount') {
172
            $label = $this->dbObject('Amount')->Nice();
173
        }
174
175
        $this->extend('updateDiscountLabel', $label);
176
177
        return $label;
178
    }
179
}
180