Completed
Push — master ( 496f6c...fa7820 )
by Iurii
01:48
created

PriceRule::cmdOnPriceRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package CLI
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\cli\controllers;
11
12
use gplcart\core\models\Currency as CurrencyModel;
13
use gplcart\core\models\PriceRule as PriceRuleModel;
14
15
/**
16
 * Handles commands related to price rules
17
 */
18
class PriceRule extends Base
19
{
20
21
    /**
22
     * Price rule model instance
23
     * @var \gplcart\core\models\PriceRule $price_rule
24
     */
25
    protected $price_rule;
26
27
    /**
28
     * Currency model class instance
29
     * @var \gplcart\core\models\Currency $currency
30
     */
31
    protected $currency;
32
33
    /**
34
     * @param PriceRuleModel $price_alias
35
     * @param CurrencyModel $currency
36
     */
37
    public function __construct(PriceRuleModel $price_alias, CurrencyModel $currency)
38
    {
39
        parent::__construct();
40
41
        $this->price_rule = $price_alias;
42
        $this->currency = $currency;
43
    }
44
45
    /**
46
     * Callback for "pricerule-on" command
47
     */
48
    public function cmdOnPriceRule()
49
    {
50
        $this->setStatusPriceRule(true);
51
        $this->output();
52
    }
53
54
    /**
55
     * Callback for "pricerule-off" command
56
     */
57
    public function cmdOffPriceRule()
58
    {
59
        $this->setStatusPriceRule(false);
60
        $this->output();
61
    }
62
63
    /**
64
     * Callback for "pricerule-get" command
65
     */
66
    public function cmdGetPriceRule()
67
    {
68
        $result = $this->getListPriceRule();
69
        $this->outputFormat($result);
70
        $this->outputFormatTablePriceRule($result);
71
        $this->output();
72
    }
73
74
    /**
75
     * Callback for "pricerule-delete" command
76
     */
77
    public function cmdDeletePriceRule()
78
    {
79
        $id = $this->getParam(0);
80
        $all = $this->getParam('all');
81
82
        if (!isset($id) && !$all) {
83
            $this->errorAndExit($this->text('Invalid command'));
84
        }
85
86
        $result = $options = null;
87
88
        if (isset($id)) {
89
90
            if (empty($id) || !is_numeric($id)) {
91
                $this->errorAndExit($this->text('Invalid argument'));
92
            }
93
94
            if ($this->getParam('trigger')) {
95
                $options = array('trigger_id' => $id);
96
            } else {
97
                $result = $this->price_rule->delete($id);
98
            }
99
100
        } else if ($all) {
101
            $options = array();
102
        }
103
104
        if (isset($options)) {
105
106
            $deleted = $count = 0;
107
            foreach ($this->price_rule->getList($options) as $item) {
108
                $count++;
109
                $deleted += (int) $this->price_rule->delete($item['price_rule_id']);
110
            }
111
112
            $result = $count && $count == $deleted;
113
        }
114
115
        if (empty($result)) {
116
            $this->errorAndExit($this->text('Unexpected result'));
117
        }
118
119
        $this->output();
120
    }
121
122
    /**
123
     * Callback for "pricerule-add" command
124
     */
125
    public function cmdAddPriceRule()
126
    {
127
        if ($this->getParam()) {
128
            $this->submitAddPriceRule();
129
        } else {
130
            $this->wizardAddPriceRule();
131
        }
132
133
        $this->output();
134
    }
135
136
    /**
137
     * Callback for "pricerule-update" command
138
     */
139
    public function cmdUpdatePriceRule()
140
    {
141
        $params = $this->getParam();
142
143
        if (empty($params[0]) || count($params) < 2) {
144
            $this->errorAndExit($this->text('Invalid command'));
145
        }
146
147
        if (!is_numeric($params[0])) {
148
            $this->errorAndExit($this->text('Invalid argument'));
149
        }
150
151
        $this->setSubmitted(null, $params);
152
        $this->setSubmitted('update', $params[0]);
153
        $this->validateComponent('price_rule');
154
155
        $this->updatePriceRule($params[0]);
156
        $this->output();
157
    }
158
159
    /**
160
     * Returns an array of price rules
161
     * @return array
162
     */
163
    protected function getListPriceRule()
164
    {
165
        $id = $this->getParam(0);
166
167
        if (!isset($id)) {
168
            return $this->price_rule->getList(array('limit' => $this->getLimit()));
169
        }
170
171
        if (!is_numeric($id)) {
172
            $this->errorAndExit($this->text('Invalid argument'));
173
        }
174
175
        if ($this->getParam('trigger')) {
176
            return $this->price_rule->getList(array('trigger_id' => $id, 'limit' => $this->getLimit()));
177
        }
178
179
        $result = $this->price_rule->get($id);
180
181
        if (empty($result)) {
182
            $this->errorAndExit($this->text('Unexpected result'));
183
        }
184
185
        return array($result);
186
    }
187
188
    /**
189
     * Output table format
190
     * @param array $items
191
     */
192
    protected function outputFormatTablePriceRule(array $items)
193
    {
194
        $header = array(
195
            $this->text('ID'),
196
            $this->text('Name'),
197
            $this->text('Trigger'),
198
            $this->text('Value'),
199
            $this->text('Value type'),
200
            $this->text('Currency'),
201
            $this->text('Code'),
202
            $this->text('Weight'),
203
            $this->text('Enabled')
204
        );
205
206
        $rows = array();
207
208
        foreach ($items as $item) {
209
            $rows[] = array(
210
                $item['price_rule_id'],
211
                $item['name'],
212
                $item['trigger_id'],
213
                $item['value'],
214
                $item['value_type'],
215
                $item['currency'],
216
                $item['code'],
217
                $item['weight'],
218
                empty($item['status']) ? $this->text('No') : $this->text('Yes')
219
            );
220
        }
221
222
        $this->outputFormatTable($rows, $header);
223
    }
224
225
    /**
226
     * Add a new price rule
227
     */
228
    protected function addPriceRule()
229
    {
230
        if (!$this->isError()) {
231
            $id = $this->price_rule->add($this->getSubmitted());
232
            if (empty($id)) {
233
                $this->errorAndExit($this->text('Unexpected result'));
234
            }
235
            $this->line($id);
236
        }
237
    }
238
239
    /**
240
     * Updates a price rule
241
     * @param string $price_rule_id
242
     */
243
    protected function updatePriceRule($price_rule_id)
244
    {
245
        if (!$this->isError() && !$this->price_rule->update($price_rule_id, $this->getSubmitted())) {
246
            $this->errorAndExit($this->text('Unexpected result'));
247
        }
248
    }
249
250
    /**
251
     * Add a new price rule at once
252
     */
253
    protected function submitAddPriceRule()
254
    {
255
        $this->setSubmitted(null, $this->getParam());
256
        $this->validateComponent('price_rule');
257
        $this->addPriceRule();
258
    }
259
260
    /**
261
     * Add a new price rule step by step
262
     */
263
    protected function wizardAddPriceRule()
264
    {
265
        $this->validatePrompt('name', $this->text('Name'), 'price_rule');
266
        $this->validatePrompt('trigger_id', $this->text('Trigger ID'), 'price_rule');
267
268
        $types = array();
269
        foreach ($this->price_rule->getTypes() as $id => $type) {
270
            $types[$id] = $type['title'];
271
        }
272
273
        $this->validateMenu('value_type', $this->text('Value type'), 'price_rule', $types, 'percent');
274
        $this->validatePrompt('value', $this->text('Value'), 'price_rule');
275
        $this->validatePrompt('currency', $this->text('Currency'), 'price_rule', $this->currency->getDefault());
276
        $this->validatePrompt('code', $this->text('Code'), 'price_rule', '');
277
        $this->validatePrompt('status', $this->text('Status'), 'price_rule', 0);
278
        $this->validatePrompt('weight', $this->text('Weight'), 'price_rule', 0);
279
280
        $this->validateComponent('price_rule');
281
        $this->addPriceRule();
282
    }
283
284
    /**
285
     * Sets status for one or several price rules
286
     * @param bool $status
287
     */
288
    protected function setStatusPriceRule($status)
289
    {
290
        $id = $this->getParam(0);
291
        $all = $this->getParam('all');
292
293
        if (!isset($id) && !$all) {
294
            $this->errorAndExit($this->text('Invalid command'));
295
        }
296
297
        $result = $options = null;
298
299
        if (isset($id)) {
300
301
            if (empty($id) || !is_numeric($id)) {
302
                $this->errorAndExit($this->text('Invalid argument'));
303
            }
304
305
            if ($this->getParam('trigger')) {
306
                $options = array('trigger_id' => $id);
307
            } else {
308
                $result = $this->price_rule->update($id, array('status' => $status));
309
            }
310
311
        } else if ($all) {
312
            $options = array();
313
        }
314
315
        if (isset($options)) {
316
317
            $updated = $count = 0;
318
            foreach ($this->price_rule->getList($options) as $item) {
319
                $count++;
320
                $updated += (int) $this->price_rule->update($item['price_rule_id'], array('status' => $status));
321
            }
322
323
            $result = $count && $count == $updated;
324
        }
325
326
        if (empty($result)) {
327
            $this->errorAndExit($this->text('Unexpected result'));
328
        }
329
330
        $this->output();
331
    }
332
333
}
334