|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Ecommerce Cart app controller |
|
5
|
|
|
* |
|
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
|
7
|
|
|
* @link http://inji.ru/ |
|
8
|
|
|
* @copyright 2015 Alexey Krupskiy |
|
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class CartController |
|
14
|
|
|
* @property Ecommerce $ecommerce |
|
15
|
|
|
* @property Ecommerce $module |
|
16
|
|
|
*/ |
|
17
|
|
|
class CartController extends Controller { |
|
18
|
|
|
|
|
19
|
|
|
public function indexAction() { |
|
20
|
|
|
$deliverys = \Ecommerce\Delivery::getList(['where' => ['disabled', 0], 'order' => ['weight', 'ASC']]); |
|
21
|
|
|
$payTypes = \Ecommerce\PayType::getList(['order' => ['weight', 'ASC']]); |
|
22
|
|
|
$cart = $this->ecommerce->getCurCart(false); |
|
23
|
|
|
if ($cart && !empty($_POST)) { |
|
24
|
|
|
$error = false; |
|
25
|
|
|
$user = Users\User::$cur; |
|
26
|
|
|
if (!Users\User::$cur->id) { |
|
27
|
|
|
$user_id = $this->Users->registration($_POST, true); |
|
28
|
|
|
if (!$user_id) { |
|
29
|
|
|
$error = true; |
|
30
|
|
|
} else { |
|
31
|
|
|
$user = Users\User::get($user_id); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
$ids = []; |
|
35
|
|
|
if (!empty($_POST['cartItems'])) { |
|
36
|
|
|
foreach ($_POST['cartItems'] as $cartItemId => $cartItemCont) { |
|
37
|
|
|
$cartItem = \Ecommerce\Cart\Item::get((int) $cartItemId); |
|
38
|
|
|
if (!$cartItem) { |
|
39
|
|
|
continue; |
|
40
|
|
|
} |
|
41
|
|
|
if ($cartItem->cart_id != $cart->id) { |
|
42
|
|
|
continue; |
|
43
|
|
|
} |
|
44
|
|
|
$count = (float) $cartItemCont; |
|
45
|
|
|
if ($count < 0.001) { |
|
46
|
|
|
$count = 1; |
|
47
|
|
|
} |
|
48
|
|
|
$cartItem->count = $count; |
|
49
|
|
|
$cartItem->save(); |
|
50
|
|
|
$ids[] = $cartItemId; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
foreach ($cart->cartItems as $cartItem) { |
|
54
|
|
|
if (!in_array($cartItem->id, $ids)) { |
|
55
|
|
|
$cartItem->delete(); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
$cart = Ecommerce\Cart::get($cart->id); |
|
59
|
|
|
if (!$cart->cartItems) { |
|
60
|
|
|
$error = true; |
|
61
|
|
|
} |
|
62
|
|
|
if (empty($this->module->config['sell_over_warehouse'])) { |
|
63
|
|
|
foreach ($cart->cartItems as $cartitem) { |
|
64
|
|
|
$warecount = $cartitem->price->offer->warehouseCount($cart->id); |
|
65
|
|
|
if ($cartitem->count > $warecount) { |
|
66
|
|
|
$error = true; |
|
67
|
|
|
Msg::add('Вы заказали <b>' . $cartitem->item->name . '</b> больше чем есть на складе. на складе: <b>' . $warecount . '</b>', 'danger'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
if ($deliverys && !$cart->delivery_id && (empty($_POST['delivery']) || empty($deliverys[$_POST['delivery']]))) { |
|
|
|
|
|
|
72
|
|
|
$error = 1; |
|
73
|
|
|
Msg::add('Выберите способ доставки', 'danger'); |
|
74
|
|
|
} elseif ($deliverys && !empty($_POST['delivery']) && !empty($deliverys[$_POST['delivery']])) { |
|
|
|
|
|
|
75
|
|
|
$cart->delivery_id = $_POST['delivery']; |
|
76
|
|
|
} |
|
77
|
|
|
if ($cart->delivery_id) { |
|
78
|
|
|
foreach ($deliverys[$cart->delivery_id]->fields as $field) { |
|
79
|
|
View Code Duplication |
if (empty($_POST['deliveryFields'][$field->id]) && $field->required) { |
|
|
|
|
|
|
80
|
|
|
$error = 1; |
|
81
|
|
|
Msg::add('Вы не указали: ' . $field->name, 'danger'); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
$payType = false; |
|
86
|
|
|
if ($payTypes && (empty($_POST['payType']) || empty($payTypes[$_POST['payType']]))) { |
|
|
|
|
|
|
87
|
|
|
$error = 1; |
|
88
|
|
|
Msg::add('Выберите способ оплаты', 'danger'); |
|
89
|
|
|
} elseif ($payTypes && !empty($payTypes[$_POST['payType']])) { |
|
|
|
|
|
|
90
|
|
|
$payType = $payTypes[$_POST['payType']]; |
|
91
|
|
|
$cart->paytype_id = $payType->id; |
|
92
|
|
|
} |
|
93
|
|
|
foreach (\Ecommerce\UserAdds\Field::getList() as $field) { |
|
94
|
|
View Code Duplication |
if (empty($_POST['userAdds']['fields'][$field->id]) && $field->required) { |
|
|
|
|
|
|
95
|
|
|
$error = 1; |
|
96
|
|
|
Msg::add('Вы не указали: ' . $field->name, 'danger'); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
if (!empty($_POST['discounts']['card_item_id'])) { |
|
100
|
|
|
$userCard = \Ecommerce\Card\Item::get((int) $_POST['discounts']['card_item_id']); |
|
101
|
|
|
if (!$userCard) { |
|
102
|
|
|
$error = true; |
|
103
|
|
|
Msg::add('Такой карты не существует', 'danger'); |
|
104
|
|
|
} elseif ($userCard->user_id != $user->id) { |
|
105
|
|
|
$error = true; |
|
106
|
|
|
Msg::add('Это не ваша карта', 'danger'); |
|
107
|
|
|
} else { |
|
108
|
|
|
$cart->card_item_id = $userCard->id; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
$this->module->parseFields($_POST['userAdds']['fields'], $cart); |
|
|
|
|
|
|
112
|
|
|
if ($deliverys && !empty($deliverys[$cart->delivery_id]) && !empty($_POST['deliveryFields'])) { |
|
|
|
|
|
|
113
|
|
|
$this->module->parseDeliveryFields($_POST['deliveryFields'], $cart, $deliverys[$cart->delivery_id]->fields); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
$cart->save(); |
|
116
|
|
|
if (!$error && !empty($_POST['action']) && $_POST['action'] = 'order') { |
|
117
|
|
|
$cart->user_id = $user->user_id; |
|
118
|
|
|
$cart->cart_status_id = 2; |
|
119
|
|
|
$cart->comment = !empty($_POST['comment']) ? htmlspecialchars($_POST['comment']) : ''; |
|
120
|
|
|
$cart->date_status = date('Y-m-d H:i:s'); |
|
121
|
|
|
$cart->complete_data = date('Y-m-d H:i:s'); |
|
122
|
|
|
$cart->warehouse_block = 1; |
|
123
|
|
|
$cart->save(); |
|
124
|
|
|
|
|
125
|
|
|
$cart = \Ecommerce\Cart::get($cart->id); |
|
126
|
|
|
foreach ($cart->cartItems as $cartItem) { |
|
127
|
|
|
$cartItem->discount = $cartItem->discount(); |
|
128
|
|
|
$cartItem->final_price = $cartItem->price->price - $cartItem->discount; |
|
129
|
|
|
$cartItem->save(); |
|
130
|
|
|
} |
|
131
|
|
|
$cart = \Ecommerce\Cart::get($cart->id); |
|
132
|
|
|
if (!empty(\App::$cur->ecommerce->config['notify_mail'])) { |
|
133
|
|
|
$text = 'Перейдите в админ панель чтобы просмотреть новый заказ <a href = "http://' . idn_to_utf8(INJI_DOMAIN_NAME) . '/admin/ecommerce/Cart">Админ панель</a>'; |
|
134
|
|
|
$title = 'Новый заказ в интернет магазине на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME); |
|
135
|
|
|
\Tools::sendMail('noreply@' . INJI_DOMAIN_NAME, \App::$cur->ecommerce->config['notify_mail'], $title, $text); |
|
136
|
|
|
} |
|
137
|
|
|
if ($this->notifications) { |
|
138
|
|
|
$notification = new Notifications\Notification(); |
|
139
|
|
|
$notification->name = 'Новый заказ в интернет магазине на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME); |
|
140
|
|
|
$notification->text = 'Перейдите в админ панель чтобы просмотреть новый заказ'; |
|
141
|
|
|
$notification->chanel_id = $this->notifications->getChanel('Ecommerce-orders')->id; |
|
142
|
|
|
$notification->save(); |
|
143
|
|
|
} |
|
144
|
|
|
$handlers = $this->ecommerce->getSnippets('payTypeHandler'); |
|
145
|
|
|
$redirect = ['/ecommerce/cart/success']; |
|
146
|
|
|
if ($payType && !empty($handlers[$payType->handler]['handler'])) { |
|
147
|
|
|
$newRedirect = $handlers[$payType->handler]['handler']($cart); |
|
148
|
|
|
if (!empty($newRedirect)) { |
|
149
|
|
|
$redirect = $newRedirect; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
unset($_SESSION['cart']['cart_id']); |
|
153
|
|
|
call_user_func_array(['Tools', 'redirect'], $redirect); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
$this->view->setTitle('Корзина'); |
|
158
|
|
|
$bread = []; |
|
159
|
|
|
$bread[] = [ |
|
160
|
|
|
'text' => 'Каталог', |
|
161
|
|
|
'href' => '/ecommerce' |
|
162
|
|
|
]; |
|
163
|
|
|
$bread[] = [ |
|
164
|
|
|
'text' => 'Корзина', |
|
165
|
|
|
'href' => '/ecommerce/cart' |
|
166
|
|
|
]; |
|
167
|
|
|
$this->view->page(['data' => compact('cart', 'items', 'deliverys', 'payTypes', 'packItem', 'bread')]); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function orderDetailAction($id = 0) { |
|
171
|
|
|
$cart = Ecommerce\Cart::get((int) $id); |
|
172
|
|
|
if ($cart->user_id != Users\User::$cur->id) { |
|
173
|
|
|
$this->url->redirect('/', 'Это не ваша корзина'); |
|
174
|
|
|
} |
|
175
|
|
|
$bread = []; |
|
176
|
|
|
$bread[] = [ |
|
177
|
|
|
'text' => 'Каталог', |
|
178
|
|
|
'href' => '/ecommerce' |
|
179
|
|
|
]; |
|
180
|
|
|
$bread[] = [ |
|
181
|
|
|
'text' => 'Корзина', |
|
182
|
|
|
'href' => '/ecommerce/cart' |
|
183
|
|
|
]; |
|
184
|
|
|
$bread[] = [ |
|
185
|
|
|
'text' => 'Заказ: №' . $cart->id, |
|
186
|
|
|
'href' => '/ecommerce/cart/orderDetail/' . $cart->id |
|
187
|
|
|
]; |
|
188
|
|
|
$this->view->setTitle('Заказ №' . $cart->id); |
|
189
|
|
|
$this->view->page(['data' => compact('cart', 'bread')]); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function continueAction($id = 0) { |
|
193
|
|
|
$cart = \Ecommerce\Cart::get((int) $id); |
|
194
|
|
|
if ($cart->user_id != Users\User::$cur->id) { |
|
195
|
|
|
Tools::redirect('/', 'Это не ваша корзина'); |
|
196
|
|
|
} |
|
197
|
|
|
if ($cart->cart_status_id > 1) { |
|
198
|
|
|
Tools::redirect('/', 'Корзина уже оформлена'); |
|
199
|
|
|
} |
|
200
|
|
|
$_SESSION['cart']['cart_id'] = $cart->id; |
|
201
|
|
|
Tools::redirect('/ecommerce/cart'); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function deleteAction($id = 0) { |
|
205
|
|
|
$cart = \Ecommerce\Cart::get((int) $id); |
|
206
|
|
|
if ($cart->user_id != Users\User::$cur->id) { |
|
207
|
|
|
Tools::redirect('/', 'Это не ваша корзина'); |
|
208
|
|
|
} |
|
209
|
|
|
if ($cart->cart_status_id > 1) { |
|
210
|
|
|
Tools::redirect('/', 'Корзина уже оформлена'); |
|
211
|
|
|
} |
|
212
|
|
|
if (!empty($_SESSION['cart']['cart_id']) && $_SESSION['cart']['cart_id'] == $cart->id) { |
|
213
|
|
|
unset($_SESSION['cart']['cart_id']); |
|
214
|
|
|
} |
|
215
|
|
|
$cart->delete(); |
|
216
|
|
|
Tools::redirect('/users/cabinet/ecommerceOrdersHistory', 'Корзина была удалена', 'success'); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function refillAction($id = 0) { |
|
220
|
|
|
$cart = \Ecommerce\Cart::get((int) $id); |
|
221
|
|
|
if ($cart->user_id != Users\User::$cur->id) { |
|
222
|
|
|
Tools::redirect('/', 'Это не ваша корзина'); |
|
223
|
|
|
} |
|
224
|
|
|
if (!empty($_SESSION['cart']['cart_id'])) { |
|
225
|
|
|
unset($_SESSION['cart']['cart_id']); |
|
226
|
|
|
} |
|
227
|
|
|
$newCart = $this->ecommerce->getCurCart(); |
|
228
|
|
|
foreach ($cart->cartItems as $cartitem) { |
|
229
|
|
|
$newCart->addItem($cartitem->item_offer_price_id, $cartitem->count); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
$newCart->save(); |
|
233
|
|
|
|
|
234
|
|
|
Tools::redirect('/ecommerce/cart/'); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public function successAction() { |
|
238
|
|
|
$bread = []; |
|
239
|
|
|
$bread[] = [ |
|
240
|
|
|
'text' => 'Каталог', |
|
241
|
|
|
'href' => '/ecommerce' |
|
242
|
|
|
]; |
|
243
|
|
|
$bread[] = [ |
|
244
|
|
|
'text' => 'Корзина', |
|
245
|
|
|
'href' => '/ecommerce/cart' |
|
246
|
|
|
]; |
|
247
|
|
|
$bread[] = [ |
|
248
|
|
|
'text' => 'Заказ принят', |
|
249
|
|
|
'href' => '/ecommerce/cart/success' |
|
250
|
|
|
]; |
|
251
|
|
|
$this->view->setTitle('Заказ принят'); |
|
252
|
|
|
$this->view->page(['data' => compact('bread')]); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
public function addAction() { |
|
256
|
|
|
$result = new Server\Result(); |
|
257
|
|
|
if (empty($_GET['itemOfferPriceId'])) { |
|
258
|
|
|
$result->success = false; |
|
259
|
|
|
$result->content = 'Произошла непредвиденная ошибка при добавлении товара'; |
|
260
|
|
|
$result->send(); |
|
261
|
|
|
} |
|
262
|
|
|
$price = \Ecommerce\Item\Offer\Price::get((int) $_GET['itemOfferPriceId']); |
|
263
|
|
|
if (!$price) { |
|
264
|
|
|
$result->success = false; |
|
265
|
|
|
$result->content = 'Такой цены не найдено'; |
|
266
|
|
|
$result->send(); |
|
267
|
|
|
} |
|
268
|
|
|
$item = $price->offer->item; |
|
269
|
|
|
|
|
270
|
|
|
if (!$item) { |
|
271
|
|
|
$result->success = false; |
|
272
|
|
|
$result->content = 'Такого товара не существует'; |
|
273
|
|
|
$result->send(); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
$cart = $this->ecommerce->getCurCart(); |
|
277
|
|
|
if (!empty($this->ecommerce->config['cartAddToggle']) && isset($cart->cartItems(['key' => 'item_id'])[$item->id]) && $cart->cartItems(['key' => 'item_id'])[$item->id]->item_offer_price_id == $price->id) { |
|
278
|
|
|
$cart->cartItems(['key' => 'item_id'])[$item->id]->delete(); |
|
279
|
|
|
$cart = $this->ecommerce->getCurCart(); |
|
280
|
|
|
$cart->date_last_activ = date('Y-m-d H:i:s'); |
|
281
|
|
|
$cart->calc(); |
|
282
|
|
|
$item->sales--; |
|
283
|
|
|
$item->save(); |
|
284
|
|
|
$result->successMsg = '<a href="/ecommerce/view/' . $item->id . '">' . $item->name() . ($price->offer->name() && $price->offer->name() != $item->name() ? ' (' . $price->offer->name() . ')' : '') . '</a> удален <a href="/ecommerce/cart">из корзины покупок</a>!'; |
|
285
|
|
|
$result->content = ['result' => 'toggleDelete']; |
|
286
|
|
|
return $result->send(); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
if (empty($_GET['count'])) { |
|
290
|
|
|
$count = 1; |
|
291
|
|
|
} else { |
|
292
|
|
|
$count = (float) $_GET['count']; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
if (empty($this->module->config['sell_over_warehouse']) && $price->offer->warehouseCount() < $count) { |
|
296
|
|
|
$result->success = false; |
|
297
|
|
|
$result->content = 'На складе недостаточно товара! Доступно: ' . $price->offer->warehouseCount(); |
|
298
|
|
|
$result->send(); |
|
299
|
|
|
} |
|
300
|
|
|
if (!$price->checkUserAccess()) { |
|
301
|
|
|
$price = $price->offer->getPrice(); |
|
302
|
|
|
} |
|
303
|
|
|
if (!isset($cart->cartItems(['key' => 'item_id'])[$item->id]) || $cart->cartItems(['key' => 'item_id'])[$item->id]->item_offer_price_id == $price->id) { |
|
304
|
|
|
$cart->addItem($price->id, $count); |
|
305
|
|
|
$result->content = ['result' => 'addNew']; |
|
306
|
|
|
} else { |
|
307
|
|
|
$cart->cartItems(['key' => 'item_id'])[$item->id]->count += $count; |
|
308
|
|
|
$cart->cartItems(['key' => 'item_id'])[$item->id]->save(); |
|
309
|
|
|
$result->content = ['result' => 'addCount']; |
|
310
|
|
|
} |
|
311
|
|
|
$cart->date_last_activ = date('Y-m-d H:i:s'); |
|
312
|
|
|
$cart->calc(); |
|
313
|
|
|
|
|
314
|
|
|
$item->sales++; |
|
315
|
|
|
$item->save(); |
|
316
|
|
|
|
|
317
|
|
|
$result->successMsg = '<a href="/ecommerce/view/' . $item->id . '">' . $item->name() . ($price->offer->name() && $price->offer->name() != $item->name() ? ' (' . $price->offer->name() . ')' : '') . '</a> добавлен <a href="/ecommerce/cart">в корзину покупок</a>!'; |
|
318
|
|
|
$result->send(); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
public function deleteItemAction() { |
|
322
|
|
|
$result = new Server\Result(); |
|
323
|
|
|
if (empty($_GET['cartItemId'])) { |
|
324
|
|
|
$result->success = false; |
|
325
|
|
|
$result->content = 'Произошла непредвиденная ошибка при добавлении товара'; |
|
326
|
|
|
$result->send(); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
$cart = $this->ecommerce->getCurCart(); |
|
330
|
|
|
if (!isset($cart->cartItems[$_GET['cartItemId']])) { |
|
331
|
|
|
$result->success = false; |
|
332
|
|
|
$result->content = 'Такого товара нет в вашей корзине'; |
|
333
|
|
|
$result->send(); |
|
334
|
|
|
} |
|
335
|
|
|
$cart->cartItems[$_GET['cartItemId']]->delete(); |
|
336
|
|
|
$cart = $this->ecommerce->getCurCart(); |
|
337
|
|
|
$cart->date_last_activ = date('Y-m-d H:i:s'); |
|
338
|
|
|
$cart->calc(); |
|
339
|
|
|
ob_start(); |
|
340
|
|
|
$this->view->widget('Ecommerce\cart'); |
|
341
|
|
|
$result->content = ob_get_contents(); |
|
342
|
|
|
ob_end_clean(); |
|
343
|
|
|
$result->successMsg = 'Товар был удален'; |
|
344
|
|
|
$result->send(); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
public function getcartAction() { |
|
348
|
|
|
$result = new Server\Result(); |
|
349
|
|
|
ob_start(); |
|
350
|
|
|
$this->view->widget('Ecommerce\cart'); |
|
351
|
|
|
$result->content = ob_get_contents(); |
|
352
|
|
|
ob_end_clean(); |
|
353
|
|
|
$result->send(); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
} |
|
357
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.