-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
msorderhandler.class.php
389 lines (327 loc) · 10.5 KB
/
msorderhandler.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
interface msOrderInterface {
/* Initializes order to context
* Here you can load custom javascript or styles
*
* @param string $ctx Context for initialization
*
* @return boolean
* */
public function initialize($ctx = 'web');
/* Add one field to order
*
* @param string $key Name of the field
* @param string $value.Value of the field
*
* @return boolean
* */
public function add($key, $value);
/* Validates field before it set
*
* @param string $key The key of the field
* @param string $value.Value of the field
*
* @return boolean|mixed
* */
public function validate($key, $value);
/* Removes field from order
*
* @param string $key The key of the field
*
* @return boolean
* */
public function remove($key);
/* Returns the whole order
*
* @return array $order
* */
public function get();
/* Returns the one field of order
*
* @param array $order Whole order at one time
* @return array $order
* */
public function set(array $order);
/* Submit the order. It will create record in database and redirect user to payment, if set.
*
* @return array $status Array with order status
* */
public function submit();
/* Cleans the order
*
* @return boolean
* */
public function clean();
/* Returns the cost of delivery depending on its settings and the goods in a cart
*
* @return array $response
* */
public function getcost();
}
class msOrderHandler implements msOrderInterface {
/* @var modX $modx */
public $modx;
protected $order;
/* @var miniShop2 $ms2 */
public $ms2;
function __construct(miniShop2 & $ms2, array $config = array()) {
$this->ms2 = & $ms2;
$this->modx = & $ms2->modx;
$this->config = array_merge(array(
'order' => & $_SESSION['minishop2']['order']
,'json_response' => false
),$config);
$this->order = & $this->config['order'];
$this->modx->lexicon->load('minishop2:order');
if (empty($this->order) || !is_array($this->order)) {
$this->order = array();
}
}
/* @inheritdoc} */
public function initialize($ctx = 'web') {
return true;
}
/* @inheritdoc} */
public function add($key, $value) {
$this->modx->invokeEvent('msOnBeforeAddToOrder', array('key' => & $key, 'value' => & $value, 'order' => $this));
if (empty($key)) {
return $this->error('');
}
else if (empty($value)) {
$this->order[$key] = $validated = '';
}
else {
$validated = $this->validate($key, $value);
if ($validated !== false) {
$this->order[$key] = $validated;
$this->modx->invokeEvent('msOnAddToOrder', array('key' => & $key, 'value' => & $validated, 'order' => $this));
}
}
return $this->success('', array($key => $validated));
}
/* @inheritdoc} */
public function validate($key, $value) {
if ($key != 'comment') {
$value = preg_replace('/\s+/',' ', trim($value));
}
switch ($key) {
case 'email': $value = preg_match('/.+@.+..+/i', $value) ? $value : @$this->order[$key]; break;
case 'receiver':
$value = preg_replace('/[^a-zа-я\s]/iu','',$value);
$tmp = explode(' ',$value);
$value = array();
for ($i=0;$i<=2;$i++) {
if (!empty($tmp[$i])) {
$value[] = $this->ucfirst($tmp[$i]);
}
}
$value = implode(' ', $value);
break;
case 'phone': $value = substr(preg_replace('/[^-+0-9]/iu','',$value),0,15); break;
case 'delivery':
/* @var msDelivery $delivery */
if (!$delivery = $this->modx->getObject('msDelivery',array('id' => $value, 'active' => 1))) {
$value = @$this->order['delivery'];
}
else if (!empty($this->order['payment'])) {
if (!$this->hasPayment($value, $this->order['payment'])) {
$this->order['payment'] = $delivery->getFirstPayment();
};
}
break;
case 'payment':
if (!empty($this->order['delivery'])) {
$value = $this->hasPayment($this->order['delivery'], $value) ? $value : @$this->order['payment'];
}
break;
case 'index': $value = substr(preg_replace('/[^-0-9]/iu', '',$value),0,10); break;
default: break;
}
if ($value === false) {$value = '';}
return $value;
}
/* Checks accordance of payment and delivery
* */
public function hasPayment($delivery, $payment) {
$q = $this->modx->newQuery('msPayment', array('id' => $payment, 'active' => 1));
$q->innerJoin('msDeliveryMember','Member','Member.payment_id = msPayment.id AND Member.delivery_id = '.$delivery);
return $this->modx->getCount('msPayment', $q) ? true : false;
}
/* @inheritdoc} */
public function remove($key) {
if ($exists = array_key_exists($key, $this->order)) {
$this->modx->invokeEvent('msOnBeforeRemoveFromOrder', array('key' => $key, 'order' => $this));
unset($this->order[$key]);
$this->modx->invokeEvent('msOnRemoveFromOrder', array('key' => $key, 'order' => $this));
}
return $exists;
}
/* @inheritdoc} */
public function get() {
return $this->order;
}
/* @inheritdoc} */
public function set(array $order) {
foreach ($order as $key => $value) {
$this->add($key, $value);
}
return $this->order;
}
/* @inheritdoc} */
public function submit($data = array()) {
$this->modx->invokeEvent('msOnSubmitOrder', array('data' => & $data, 'order' => $this));
if (!empty($data)) {
$this->set($data);
}
/* @var msDelivery $delivery */
if (!$delivery = $this->modx->getObject('msDelivery', array('id' => $this->order['delivery'], 'active' => 1))) {
return $this->error('ms2_order_err_delivery', array('delivery'));
}
$requires = array_map('trim', explode(',',$delivery->get('requires')));
$requires[] = 'email';
$errors = array();
foreach ($requires as $v) {
if (!empty($v) && empty($this->order[$v])) {
$errors[] = $v;
}
}
if (!empty($errors)) {
return $this->error('ms2_order_err_requires', $errors);
}
$user_id = $this->ms2->getCustomerId();
$cart_status = $this->ms2->cart->status();
$delivery_cost = $this->getcost(false, true);
$createdon = date('Y-m-d H:i:s');
/* @var msOrder $order */
$order = $this->modx->newObject('msOrder');
$order->fromArray(array(
'user_id' => $user_id
,'createdon' => $createdon
,'num' => $this->getnum()
,'delivery' => $this->order['delivery']
,'payment' => $this->order['payment']
,'cart_cost' => $cart_status['total_cost']
,'weight' => $cart_status['total_weight']
,'delivery_cost' => $delivery_cost
,'cost' => $cart_status['total_cost'] + $delivery_cost
,'status' => 0
,'context' => $this->ms2->config['ctx']
));
// Adding address
/* @var msOrderAddress $address */
$address = $this->modx->newObject('msOrderAddress');
$address->fromArray(array_merge($this->order,array(
'user_id' => $user_id
,'createdon' => $createdon
)));
$order->addOne($address);
// Adding products
$cart = $this->ms2->cart->get();
$products = array();
foreach ($cart as $v) {
/* @var msOrderProduct $product */
$product = $this->modx->newObject('msOrderProduct');
$product->fromArray(array_merge($v, array(
'product_id' => $v['id']
,'cost' => $v['price'] * $v['count']
)));
$products[] = $product;
}
$order->addMany($products);
$this->modx->invokeEvent('msOnBeforeCreateOrder', array('msOrder' => & $order, 'order' => $this));
if ($order->save()) {
$this->modx->invokeEvent('msOnCreateOrder', array('msOrder' => & $order, 'order' => $this));
$this->ms2->cart->clean();
$this->clean();
if (empty($_SESSION['minishop2']['orders'])) {
$_SESSION['minishop2']['orders'] = array();
}
$_SESSION['minishop2']['orders'][] = $order->get('id');
$this->ms2->changeOrderStatus($order->get('id'), 1); // set status "new"
/* @var msPayment $payment*/
if ($payment = $this->modx->getObject('msPayment', array('id' => $order->get('payment'), 'active' => 1))) {
$response = $payment->send($order);
exit(is_array($response) ? $this->modx->toJSON($response) : $response);
}
else {
return $this->success('', array('msorder' => $order->get('id')));
}
}
return $this->error();
}
/* @inheritdoc} */
public function clean() {
$this->modx->invokeEvent('msOnBeforeEmptyOrder', array('order' => $this));
$this->order = array();
$this->modx->invokeEvent('msOnEmptyOrder', array('order' => $this));
return $this->success('', array());
}
/* @inheritdoc} */
public function getcost($with_cart = true, $only_cost = false) {
$cost = 0;
$cart = $this->ms2->cart->status();
/* @var msDelivery $delivery */
if ($delivery = $this->modx->getObject('msDelivery', $this->order['delivery'])) {
$cost = $delivery->getcost($this);
}
if ($with_cart) {
$cost += $cart['total_cost'];
}
return $only_cost ? $cost : $this->success('', array('cost' => $cost));
}
/* Return current number of order
*
* */
public function getnum() {
$table = $this->modx->getTableName('msOrder');
$cur = date('ym');
$sql = $this->modx->query("SELECT `num` FROM {$table} WHERE `num` LIKE '{$cur}%' ORDER BY `id` DESC LIMIT 1");
$num = $sql->fetch(PDO::FETCH_COLUMN);
if (empty($num)) {$num = date('ym').'/0';}
$num = explode('/', $num);
$num = $cur.'/'.($num[1] + 1);
return $num;
}
/* This method returns an error of the order
*
* @param string $message A lexicon key for error message
* @param array $data.Additional data, for example cart status
* @param array $placeholders Array with placeholders for lexicon entry
*
* @return array|string $response
* */
public function error($message = '', $data = array(), $placeholders = array()) {
$response = array(
'success' => false
,'message' => $this->modx->lexicon($message, $placeholders)
,'data' => $data
);
return $this->config['json_response'] ? $this->modx->toJSON($response) : $response;
}
/* This method returns an success of the order
*
* @param string $message A lexicon key for success message
* @param array $data.Additional data, for example cart status
* @param array $placeholders Array with placeholders for lexicon entry
*
* @return array|string $response
* */
public function success($message = '', $data = array(), $placeholders = array()) {
$response = array(
'success' => true
,'message' => $this->modx->lexicon($message, $placeholders)
,'data' => $data
);
return $this->config['json_response'] ? $this->modx->toJSON($response) : $response;
}
public function ucfirst($str = '') {
if (function_exists('mb_substr') && preg_match('/[а-я]/iu',$str)) {
$tmp = mb_strtolower($str, 'utf-8');
$str = mb_substr(mb_strtoupper($tmp, 'utf-8'), 0, 1, 'utf-8') . mb_substr($tmp, 1, mb_strlen($tmp)-1, 'utf-8');
}
else {
$str = ucfirst(strtolower($str));
}
return $str;
}
}