|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Cart |
|
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
|
|
|
namespace Ecommerce; |
|
13
|
|
|
|
|
14
|
|
|
class Cart extends \Model |
|
15
|
|
|
{ |
|
16
|
|
|
public static $objectName = 'Корзины'; |
|
17
|
|
|
|
|
18
|
|
|
public static function indexes() |
|
19
|
|
|
{ |
|
20
|
|
|
return [ |
|
21
|
|
|
'ecommerce_cartStatusBlock' => [ |
|
22
|
|
|
'type' => 'INDEX', |
|
23
|
|
|
'cols' => [ |
|
24
|
|
|
'cart_cart_status_id', |
|
25
|
|
|
'cart_warehouse_block' |
|
26
|
|
|
] |
|
27
|
|
|
], |
|
28
|
|
|
'ecommerce_cartStats' => [ |
|
29
|
|
|
'type' => 'INDEX', |
|
30
|
|
|
'cols' => [ |
|
31
|
|
|
'cart_cart_status_id', |
|
32
|
|
|
] |
|
33
|
|
|
], |
|
34
|
|
|
'ecommerce_cartBlock' => [ |
|
35
|
|
|
'type' => 'INDEX', |
|
36
|
|
|
'cols' => [ |
|
37
|
|
|
'cart_warehouse_block' |
|
38
|
|
|
] |
|
39
|
|
|
], |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function relations() |
|
44
|
|
|
{ |
|
45
|
|
|
return [ |
|
46
|
|
|
'user' => [ |
|
47
|
|
|
'model' => 'Users\User', |
|
48
|
|
|
'col' => 'user_id' |
|
49
|
|
|
], |
|
50
|
|
|
'cartItems' => [ |
|
51
|
|
|
'type' => 'many', |
|
52
|
|
|
'model' => 'Ecommerce\Cart\Item', |
|
53
|
|
|
'col' => 'cart_id', |
|
54
|
|
|
], |
|
55
|
|
|
'events' => [ |
|
56
|
|
|
'type' => 'many', |
|
57
|
|
|
'model' => 'Ecommerce\Cart\Event', |
|
58
|
|
|
'col' => 'cart_id', |
|
59
|
|
|
], |
|
60
|
|
|
'status' => [ |
|
61
|
|
|
'model' => 'Ecommerce\Cart\Status', |
|
62
|
|
|
'col' => 'cart_status_id' |
|
63
|
|
|
], |
|
64
|
|
|
'delivery' => [ |
|
65
|
|
|
'model' => 'Ecommerce\Delivery', |
|
66
|
|
|
'col' => 'delivery_id' |
|
67
|
|
|
], |
|
68
|
|
|
'payType' => [ |
|
69
|
|
|
'model' => 'Ecommerce\PayType', |
|
70
|
|
|
'col' => 'paytype_id' |
|
71
|
|
|
], |
|
72
|
|
|
'infos' => [ |
|
73
|
|
|
'type' => 'many', |
|
74
|
|
|
'model' => 'Ecommerce\Cart\Info', |
|
75
|
|
|
'col' => 'cart_id' |
|
76
|
|
|
], |
|
77
|
|
|
'extras' => [ |
|
78
|
|
|
'type' => 'many', |
|
79
|
|
|
'model' => 'Ecommerce\Cart\Extra', |
|
80
|
|
|
'col' => 'cart_id' |
|
81
|
|
|
], |
|
82
|
|
|
'card' => [ |
|
83
|
|
|
'model' => 'Ecommerce\Card\Item', |
|
84
|
|
|
'col' => 'card_item_id' |
|
85
|
|
|
], |
|
86
|
|
|
'pays' => [ |
|
87
|
|
|
'type' => 'many', |
|
88
|
|
|
'model' => 'Money\Pay', |
|
89
|
|
|
'col' => 'data' |
|
90
|
|
|
] |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function beforeDelete() |
|
95
|
|
|
{ |
|
96
|
|
|
foreach ($this->cartItems as $cartItem) { |
|
97
|
|
|
$cartItem->delete(); |
|
98
|
|
|
} |
|
99
|
|
|
foreach ($this->infos as $info) { |
|
100
|
|
|
$info->delete(); |
|
101
|
|
|
} |
|
102
|
|
|
foreach ($this->extras as $extra) { |
|
103
|
|
|
$extra->delete(); |
|
104
|
|
|
} |
|
105
|
|
|
foreach ($this->events as $event) { |
|
106
|
|
|
$event->delete(); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public static $labels = [ |
|
111
|
|
|
'user_id' => 'Пользователь', |
|
112
|
|
|
'sum' => 'Сумма', |
|
113
|
|
|
'cart_status_id' => 'Статус', |
|
114
|
|
|
'delivery_id' => 'Доставка', |
|
115
|
|
|
'comment' => 'Комментарий', |
|
116
|
|
|
'bonus_used' => 'Выгодные рубли', |
|
117
|
|
|
'complete_data' => 'Время заказа', |
|
118
|
|
|
'info' => 'Информация', |
|
119
|
|
|
'items' => 'Товары', |
|
120
|
|
|
'paytype_id' => 'Способ оплаты', |
|
121
|
|
|
'payed' => 'Оплачен', |
|
122
|
|
|
'exported' => 'Выгружено', |
|
123
|
|
|
'warehouse_block' => 'Блокировка товаров', |
|
124
|
|
|
'extra' => 'Дополнительно', |
|
125
|
|
|
'card_item_id' => 'Дисконтная карта', |
|
126
|
|
|
'info' => 'Информация', |
|
127
|
|
|
'pay' => 'Счета оплаты', |
|
128
|
|
|
'sums' => 'Суммы', |
|
129
|
|
|
]; |
|
130
|
|
|
public static $cols = [ |
|
131
|
|
|
//Основные параметры |
|
132
|
|
|
'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'], |
|
133
|
|
|
'cart_status_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'status'], |
|
134
|
|
|
'delivery_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'delivery'], |
|
135
|
|
|
'paytype_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'payType'], |
|
136
|
|
|
'card_item_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'card'], |
|
137
|
|
|
'warehouse_block' => ['type' => 'bool'], |
|
138
|
|
|
'payed' => ['type' => 'bool'], |
|
139
|
|
|
'comment' => ['type' => 'textarea'], |
|
140
|
|
|
//Системные |
|
141
|
|
|
'sum' => ['type' => 'text'], |
|
142
|
|
|
'exported' => ['type' => 'bool'], |
|
143
|
|
|
'complete_data' => ['type' => 'dateTime'], |
|
144
|
|
|
'date_status' => ['type' => 'dateTime'], |
|
145
|
|
|
'date_last_activ' => ['type' => 'dateTime'], |
|
146
|
|
|
'date_create' => ['type' => 'dateTime'], |
|
147
|
|
|
//Виджеты |
|
148
|
|
|
'sums' => [ |
|
149
|
|
|
'type' => 'void', |
|
150
|
|
|
'view' => [ |
|
151
|
|
|
'type' => 'widget', |
|
152
|
|
|
'widget' => 'Ecommerce\adminSums', |
|
153
|
|
|
], |
|
154
|
|
|
], |
|
155
|
|
|
//Менеджеры |
|
156
|
|
|
'extra' => ['type' => 'dataManager', 'relation' => 'extras'], |
|
157
|
|
|
'pay' => ['type' => 'dataManager', 'relation' => 'pays'], |
|
158
|
|
|
'items' => ['type' => 'dataManager', 'relation' => 'cartItems'], |
|
159
|
|
|
'info' => ['type' => 'dataManager', 'relation' => 'infos'], |
|
160
|
|
|
]; |
|
161
|
|
|
public static $dataManagers = [ |
|
162
|
|
|
'manager' => [ |
|
163
|
|
|
'cols' => [ |
|
164
|
|
|
'info', |
|
165
|
|
|
'items', |
|
166
|
|
|
'extra', |
|
167
|
|
|
'sums', |
|
168
|
|
|
'cart_status_id', |
|
169
|
|
|
'delivery_id', |
|
170
|
|
|
'payed', |
|
171
|
|
|
'pay', |
|
172
|
|
|
'complete_data', |
|
173
|
|
|
], |
|
174
|
|
|
'sortable' => [ |
|
175
|
|
|
'cart_status_id', |
|
176
|
|
|
'delivery_id', |
|
177
|
|
|
'payed', |
|
178
|
|
|
'complete_data', |
|
179
|
|
|
], |
|
180
|
|
|
'filters' => [ |
|
181
|
|
|
'cart_status_id', |
|
182
|
|
|
'delivery_id', |
|
183
|
|
|
'payed', |
|
184
|
|
|
'complete_data', |
|
185
|
|
|
], |
|
186
|
|
|
'preSort' => [ |
|
187
|
|
|
'complete_data' => 'desc' |
|
188
|
|
|
], |
|
189
|
|
|
'actions' => [ |
|
190
|
|
|
'Ecommerce\CloseCartBtn', 'Open', 'Edit', 'Delete' |
|
191
|
|
|
] |
|
192
|
|
|
] |
|
193
|
|
|
]; |
|
194
|
|
|
|
|
195
|
|
|
public static function itemName($item) |
|
196
|
|
|
{ |
|
197
|
|
|
return $item->pk() . '. ' . $item->name(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public static $forms = [ |
|
201
|
|
|
'manager' => [ |
|
202
|
|
|
'inputs' => [ |
|
203
|
|
|
'userSearch' => [ |
|
204
|
|
|
'type' => 'search', |
|
205
|
|
|
'source' => 'relation', |
|
206
|
|
|
'relation' => 'user', |
|
207
|
|
|
'label' => 'Покупатель', |
|
208
|
|
|
'cols' => [ |
|
209
|
|
|
'info:first_name', |
|
210
|
|
|
'info:last_name', |
|
211
|
|
|
'info:middle_name', |
|
212
|
|
|
'mail' |
|
213
|
|
|
], |
|
214
|
|
|
'col' => 'user_id', |
|
215
|
|
|
'required' => true, |
|
216
|
|
|
'showCol' => [ |
|
217
|
|
|
'type' => 'staticMethod', |
|
218
|
|
|
'class' => 'Ecommerce\Cart', |
|
219
|
|
|
'method' => 'itemName', |
|
220
|
|
|
], |
|
221
|
|
|
], |
|
222
|
|
|
'cardSearch' => [ |
|
223
|
|
|
'type' => 'search', |
|
224
|
|
|
'source' => 'relation', |
|
225
|
|
|
'relation' => 'card', |
|
226
|
|
|
'label' => 'Дисконтная карта', |
|
227
|
|
|
'cols' => [ |
|
228
|
|
|
'code', |
|
229
|
|
|
'user:info:first_name', |
|
230
|
|
|
'user:info:last_name', |
|
231
|
|
|
'user:info:middle_name', |
|
232
|
|
|
'user:mail' |
|
233
|
|
|
], |
|
234
|
|
|
'col' => 'card_item_id', |
|
235
|
|
|
], |
|
236
|
|
|
], |
|
237
|
|
|
'map' => [ |
|
238
|
|
|
['userSearch', 'cart_status_id'], |
|
239
|
|
|
['paytype_id', 'delivery_id'], |
|
240
|
|
|
['cardSearch', 'comment'], |
|
241
|
|
|
['warehouse_block', 'complete_data'], |
|
242
|
|
|
['payed'], |
|
243
|
|
|
['items'], |
|
244
|
|
|
['extra'], |
|
245
|
|
|
['pay'] |
|
246
|
|
|
] |
|
247
|
|
|
], |
|
248
|
|
|
]; |
|
249
|
|
|
|
|
250
|
|
|
public function addPacks($count = 1) |
|
251
|
|
|
{ |
|
252
|
|
|
$this->addItem(Inji::app()->ecommerce->modConf['packItem']['ci_id'], Inji::app()->ecommerce->modConf['packItem']['ciprice_id'], $count); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
public function needDelivery() |
|
256
|
|
|
{ |
|
257
|
|
|
foreach ($this->cartItems as $cartItem) { |
|
258
|
|
|
if (!$cartItem->item->type) { |
|
259
|
|
|
continue; |
|
260
|
|
|
} |
|
261
|
|
|
if ($cartItem->item->type->cit_warehouse) { |
|
262
|
|
|
return true; |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
return false; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
public function deliverySum() |
|
269
|
|
|
{ |
|
270
|
|
|
|
|
271
|
|
|
if ($this->needDelivery() && $this->delivery && $this->sum < $this->delivery->cd_max_cart_price) { |
|
272
|
|
|
return $this->delivery->cd_price; |
|
273
|
|
|
} |
|
274
|
|
|
return 0; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
public function discountSun() |
|
278
|
|
|
{ |
|
279
|
|
|
$discountSum = 0; |
|
280
|
|
|
foreach ($this->cartItems as $cartItem) { |
|
281
|
|
|
$discountSum += $cartItem->discount(); |
|
282
|
|
|
} |
|
283
|
|
|
return $discountSum; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
public function finalSum() |
|
287
|
|
|
{ |
|
288
|
|
|
return $this->sum + $this->deliverySum() - $this->disountSum(); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
public function itemSum() |
|
292
|
|
|
{ |
|
293
|
|
|
return $this->sum; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
public function addItem($item_id, $offer_price_id, $count = 1, $final_price = 0) |
|
297
|
|
|
{ |
|
298
|
|
|
$item = Item::get((int) $item_id); |
|
299
|
|
|
|
|
300
|
|
|
if (!$item) { |
|
301
|
|
|
return false; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
$price = false; |
|
305
|
|
|
foreach ($item->offers as $offer) { |
|
306
|
|
|
if (!empty($offer->prices[(int) $offer_price_id])) { |
|
307
|
|
|
$price = $offer->prices[(int) $offer_price_id]; |
|
308
|
|
|
break; |
|
309
|
|
|
} |
|
310
|
|
|
} |
|
311
|
|
|
if (!$price) |
|
312
|
|
|
return false; |
|
313
|
|
|
|
|
314
|
|
|
if ($count <= 0) { |
|
315
|
|
|
$count = 1; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
$cartItem = new Cart\Item(); |
|
319
|
|
|
$cartItem->cart_id = $this->id; |
|
320
|
|
|
$cartItem->item_id = $item->id; |
|
321
|
|
|
$cartItem->count = $count; |
|
322
|
|
|
$cartItem->item_offer_price_id = $price->id; |
|
323
|
|
|
$cartItem->final_price = $final_price ? $final_price : $price->price; |
|
324
|
|
|
$cartItem->save(); |
|
325
|
|
|
return true; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
public function calc($save = true) |
|
329
|
|
|
{ |
|
330
|
|
|
if (!$this->id) { |
|
331
|
|
|
return; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
$this->sum = 0; |
|
335
|
|
|
$cart = Cart::get($this->id); |
|
336
|
|
|
foreach ($cart->cartItems as $cartItem) { |
|
337
|
|
|
if (!$cartItem->price) { |
|
338
|
|
|
continue; |
|
339
|
|
|
} |
|
340
|
|
|
$this->sum += (float) ($cartItem->price->price - $cartItem->discount()) * (float) $cartItem->count; |
|
341
|
|
|
} |
|
342
|
|
|
foreach ($cart->extras as $extra) { |
|
343
|
|
|
$this->sum += $extra->price * $extra->count; |
|
344
|
|
|
} |
|
345
|
|
|
if ($save) { |
|
346
|
|
|
$this->save(); |
|
347
|
|
|
} |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
public function beforeSave() |
|
351
|
|
|
{ |
|
352
|
|
|
$this->calc(false); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
} |
|
356
|
|
|
|