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_rule |
35
|
|
|
* @param CurrencyModel $currency |
36
|
|
|
*/ |
37
|
|
|
public function __construct(PriceRuleModel $price_rule, CurrencyModel $currency) |
38
|
|
|
{ |
39
|
|
|
parent::__construct(); |
40
|
|
|
|
41
|
|
|
$this->price_rule = $price_rule; |
42
|
|
|
$this->currency = $currency; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Callback for "pricerule-get" command |
47
|
|
|
*/ |
48
|
|
|
public function cmdGetPriceRule() |
49
|
|
|
{ |
50
|
|
|
$result = $this->getListPriceRule(); |
51
|
|
|
$this->outputFormat($result); |
52
|
|
|
$this->outputFormatTablePriceRule($result); |
53
|
|
|
$this->output(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Callback for "pricerule-delete" command |
58
|
|
|
*/ |
59
|
|
|
public function cmdDeletePriceRule() |
60
|
|
|
{ |
61
|
|
|
$id = $this->getParam(0); |
62
|
|
|
$all = $this->getParam('all'); |
63
|
|
|
|
64
|
|
|
if (!isset($id) && !$all) { |
65
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$result = $options = null; |
69
|
|
|
|
70
|
|
|
if (isset($id)) { |
71
|
|
|
|
72
|
|
|
if (empty($id) || !is_numeric($id)) { |
73
|
|
|
$this->errorAndExit($this->text('Invalid ID')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($this->getParam('trigger')) { |
77
|
|
|
$options = array('trigger_id' => $id); |
78
|
|
|
} else { |
79
|
|
|
$result = $this->price_rule->delete($id); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} else if ($all) { |
83
|
|
|
$options = array(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (isset($options)) { |
87
|
|
|
|
88
|
|
|
$deleted = $count = 0; |
89
|
|
|
foreach ($this->price_rule->getList($options) as $item) { |
90
|
|
|
$count++; |
91
|
|
|
$deleted += (int) $this->price_rule->delete($item['price_rule_id']); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$result = $count && $count == $deleted; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (empty($result)) { |
98
|
|
|
$this->errorAndExit($this->text('An error occurred')); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->output(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Callback for "pricerule-add" command |
106
|
|
|
*/ |
107
|
|
|
public function cmdAddPriceRule() |
108
|
|
|
{ |
109
|
|
|
if ($this->getParam()) { |
110
|
|
|
$this->submitAddPriceRule(); |
111
|
|
|
} else { |
112
|
|
|
$this->wizardAddPriceRule(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->output(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Callback for "pricerule-update" command |
120
|
|
|
*/ |
121
|
|
|
public function cmdUpdatePriceRule() |
122
|
|
|
{ |
123
|
|
|
$params = $this->getParam(); |
124
|
|
|
|
125
|
|
|
if (empty($params[0]) || count($params) < 2) { |
126
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (!is_numeric($params[0])) { |
130
|
|
|
$this->errorAndExit($this->text('Invalid ID')); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$this->setSubmitted(null, $params); |
134
|
|
|
$this->setSubmitted('update', $params[0]); |
135
|
|
|
$this->validateComponent('price_rule'); |
136
|
|
|
|
137
|
|
|
$this->updatePriceRule($params[0]); |
138
|
|
|
$this->output(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns an array of price rules |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
protected function getListPriceRule() |
146
|
|
|
{ |
147
|
|
|
$id = $this->getParam(0); |
148
|
|
|
|
149
|
|
|
if (!isset($id)) { |
150
|
|
|
return $this->price_rule->getList(array('limit' => $this->getLimit())); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (!is_numeric($id)) { |
154
|
|
|
$this->errorAndExit($this->text('Invalid ID')); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if ($this->getParam('trigger')) { |
158
|
|
|
return $this->price_rule->getList(array('trigger_id' => $id, 'limit' => $this->getLimit())); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$result = $this->price_rule->get($id); |
162
|
|
|
|
163
|
|
|
if (empty($result)) { |
164
|
|
|
$this->errorAndExit($this->text('Invalid ID')); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return array($result); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Output table format |
172
|
|
|
* @param array $items |
173
|
|
|
*/ |
174
|
|
|
protected function outputFormatTablePriceRule(array $items) |
175
|
|
|
{ |
176
|
|
|
$header = array( |
177
|
|
|
$this->text('ID'), |
178
|
|
|
$this->text('Name'), |
179
|
|
|
$this->text('Trigger'), |
180
|
|
|
$this->text('Value'), |
181
|
|
|
$this->text('Value type'), |
182
|
|
|
$this->text('Currency'), |
183
|
|
|
$this->text('Code'), |
184
|
|
|
$this->text('Weight'), |
185
|
|
|
$this->text('Enabled') |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
$rows = array(); |
189
|
|
|
|
190
|
|
|
foreach ($items as $item) { |
191
|
|
|
$rows[] = array( |
192
|
|
|
$item['price_rule_id'], |
193
|
|
|
$item['name'], |
194
|
|
|
$item['trigger_id'], |
195
|
|
|
$item['value'], |
196
|
|
|
$item['value_type'], |
197
|
|
|
$item['currency'], |
198
|
|
|
$item['code'], |
199
|
|
|
$item['weight'], |
200
|
|
|
empty($item['status']) ? $this->text('No') : $this->text('Yes') |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$this->outputFormatTable($rows, $header); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Add a new price rule |
209
|
|
|
*/ |
210
|
|
|
protected function addPriceRule() |
211
|
|
|
{ |
212
|
|
|
if (!$this->isError()) { |
213
|
|
|
$id = $this->price_rule->add($this->getSubmitted()); |
214
|
|
|
if (empty($id)) { |
215
|
|
|
$this->errorAndExit($this->text('An error occurred')); |
216
|
|
|
} |
217
|
|
|
$this->line($id); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Updates a price rule |
223
|
|
|
* @param string $price_rule_id |
224
|
|
|
*/ |
225
|
|
|
protected function updatePriceRule($price_rule_id) |
226
|
|
|
{ |
227
|
|
|
if (!$this->isError() && !$this->price_rule->update($price_rule_id, $this->getSubmitted())) { |
228
|
|
|
$this->errorAndExit($this->text('An error occurred')); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Add a new price rule at once |
234
|
|
|
*/ |
235
|
|
|
protected function submitAddPriceRule() |
236
|
|
|
{ |
237
|
|
|
$this->setSubmitted(null, $this->getParam()); |
238
|
|
|
$this->validateComponent('price_rule'); |
239
|
|
|
$this->addPriceRule(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Add a new price rule step by step |
244
|
|
|
*/ |
245
|
|
|
protected function wizardAddPriceRule() |
246
|
|
|
{ |
247
|
|
|
$this->validatePrompt('name', $this->text('Name'), 'price_rule'); |
248
|
|
|
$this->validatePrompt('trigger_id', $this->text('Trigger ID'), 'price_rule'); |
249
|
|
|
|
250
|
|
|
$types = array(); |
251
|
|
|
foreach ($this->price_rule->getTypes() as $id => $type) { |
252
|
|
|
$types[$id] = $type['title']; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$this->validateMenu('value_type', $this->text('Value type'), 'price_rule', $types); |
256
|
|
|
$this->validatePrompt('value', $this->text('Value'), 'price_rule'); |
257
|
|
|
$this->validatePrompt('currency', $this->text('Currency'), 'price_rule', $this->currency->getDefault()); |
258
|
|
|
$this->validatePrompt('code', $this->text('Code'), 'price_rule', ''); |
259
|
|
|
$this->validatePrompt('status', $this->text('Status'), 'price_rule', 0); |
260
|
|
|
$this->validatePrompt('weight', $this->text('Weight'), 'price_rule', 0); |
261
|
|
|
|
262
|
|
|
$this->validateComponent('price_rule'); |
263
|
|
|
$this->addPriceRule(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
} |
267
|
|
|
|