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\Order as OrderModel; |
13
|
|
|
use gplcart\modules\cli\controllers\Command; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Handles commands related to store orders |
17
|
|
|
*/ |
18
|
|
|
class Order extends Command |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Order model instance |
23
|
|
|
* @var \gplcart\core\models\Order $order |
24
|
|
|
*/ |
25
|
|
|
protected $order; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param OrderModel $order |
29
|
|
|
*/ |
30
|
|
|
public function __construct(OrderModel $order) |
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
|
34
|
|
|
$this->order = $order; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Callback for "order-get" command |
39
|
|
|
*/ |
40
|
|
|
public function cmdGetOrder() |
41
|
|
|
{ |
42
|
|
|
$result = $this->getListOrder(); |
43
|
|
|
$this->outputFormat($result); |
44
|
|
|
$this->outputFormatTableOrder($result); |
45
|
|
|
$this->output(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Callback for "order-delete" command |
50
|
|
|
*/ |
51
|
|
|
public function cmdDeleteOrder() |
52
|
|
|
{ |
53
|
|
|
$id = $this->getParam(0); |
54
|
|
|
|
55
|
|
|
if (empty($id)) { |
56
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$options = null; |
60
|
|
|
|
61
|
|
|
if ($this->getParam('status')) { |
62
|
|
|
$options = array('status' => $id); |
63
|
|
|
} else if ($this->getParam('store')) { |
64
|
|
|
|
65
|
|
|
if (!is_numeric($id)) { |
66
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$options = array('store_id' => $id); |
70
|
|
|
|
71
|
|
|
} else if ($this->getParam('user')) { |
72
|
|
|
$options = array('user_id' => $id); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (isset($options)) { |
76
|
|
|
|
77
|
|
|
$deleted = $count = 0; |
78
|
|
|
|
79
|
|
|
foreach ($this->order->getList($options) as $item) { |
80
|
|
|
$count++; |
81
|
|
|
$deleted += (int) $this->order->delete($item['order_id']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$result = $count && $count == $deleted; |
85
|
|
|
|
86
|
|
|
} else { |
87
|
|
|
|
88
|
|
|
if (!is_numeric($id)) { |
89
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$result = $this->order->delete($id); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (empty($result)) { |
96
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->output(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Callback for "order-add" command |
104
|
|
|
*/ |
105
|
|
|
public function cmdAddOrder() |
106
|
|
|
{ |
107
|
|
|
$submitted = $this->getParam(); |
108
|
|
|
|
109
|
|
|
$this->setSubmitted(null, $submitted); |
110
|
|
|
$this->setSubmittedJson('data'); |
111
|
|
|
|
112
|
|
|
$this->validateComponent('order'); |
113
|
|
|
|
114
|
|
|
if ($this->isError()) { |
115
|
|
|
$this->output(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$id = $this->order->add($this->getSubmitted()); |
119
|
|
|
|
120
|
|
|
if (empty($id)) { |
121
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->line($id); |
125
|
|
|
$this->output(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Callback for "order-update" command |
130
|
|
|
*/ |
131
|
|
|
public function cmdUpdateOrder() |
132
|
|
|
{ |
133
|
|
|
$params = $this->getParam(); |
134
|
|
|
|
135
|
|
|
if (empty($params[0]) || count($params) < 2) { |
136
|
|
|
$this->errorAndExit($this->text('Invalid command')); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (!is_numeric($params[0])) { |
140
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$this->setSubmitted(null, $params); |
144
|
|
|
$this->setSubmitted('update', $params[0]); |
145
|
|
|
$this->setSubmittedJson('data'); |
146
|
|
|
|
147
|
|
|
$this->validateComponent('order'); |
148
|
|
|
|
149
|
|
|
if ($this->isError()) { |
150
|
|
|
$this->output(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (!$this->order->update($params[0], $this->getSubmitted())) { |
154
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$this->output(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns an array of orders |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
protected function getListOrder() |
165
|
|
|
{ |
166
|
|
|
$id = $this->getParam(0); |
167
|
|
|
$limit = $this->getLimit(); |
168
|
|
|
|
169
|
|
|
if (!isset($id)) { |
170
|
|
|
return $this->order->getList(array('limit' => $limit)); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if ($this->getParam('user')) { |
174
|
|
|
return $this->order->getList(array('user_id' => $id, 'limit' => $limit)); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ($this->getParam('status')) { |
178
|
|
|
return $this->order->getList(array('status' => $id, 'limit' => $limit)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if (!is_numeric($id)) { |
182
|
|
|
$this->errorAndExit($this->text('Invalid argument')); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if ($this->getParam('store')) { |
186
|
|
|
return $this->order->getList(array('store_id' => $id, 'limit' => $limit)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if ($this->getParam('shipping-address')) { |
190
|
|
|
return $this->order->getList(array('shipping_address' => $id, 'limit' => $limit)); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if ($this->getParam('payment-address')) { |
194
|
|
|
return $this->order->getList(array('payment_address' => $id, 'limit' => $limit)); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$result = $this->order->get($id); |
198
|
|
|
|
199
|
|
|
if (empty($result)) { |
200
|
|
|
$this->errorAndExit($this->text('Unexpected result')); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return array($result); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Output table format |
208
|
|
|
* @param array $items |
209
|
|
|
*/ |
210
|
|
|
protected function outputFormatTableOrder(array $items) |
211
|
|
|
{ |
212
|
|
|
$header = array( |
213
|
|
|
$this->text('ID'), |
214
|
|
|
$this->text('Store'), |
215
|
|
|
$this->text('Total'), |
216
|
|
|
$this->text('Currency'), |
217
|
|
|
$this->text('User'), |
218
|
|
|
$this->text('Payment'), |
219
|
|
|
$this->text('Shipping'), |
220
|
|
|
$this->text('Payment address'), |
221
|
|
|
$this->text('Shipping address'), |
222
|
|
|
$this->text('Status'), |
223
|
|
|
$this->text('Created') |
224
|
|
|
); |
225
|
|
|
|
226
|
|
|
$rows = array(); |
227
|
|
|
|
228
|
|
|
foreach ($items as $item) { |
229
|
|
|
$rows[] = array( |
230
|
|
|
$item['order_id'], |
231
|
|
|
$item['store_id'], |
232
|
|
|
$item['total'], |
233
|
|
|
$item['currency'], |
234
|
|
|
$item['user_id'], |
235
|
|
|
$item['payment'], |
236
|
|
|
$item['shipping'], |
237
|
|
|
$item['payment_address'], |
238
|
|
|
$item['shipping_address'], |
239
|
|
|
$item['status'], |
240
|
|
|
$this->date($item['created']) |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$this->outputFormatTable($rows, $header); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
} |
248
|
|
|
|