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