Completed
Push — master ( 10d1ae...5e8b4d )
by Alexey
05:42
created

Ecommerce::getItemsParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 8
c 2
b 0
f 1
nc 1
nop 1
dl 0
loc 11
rs 9.4285
1
<?php
2
3
/**
4
 * Ecommerce module
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
class Ecommerce extends Module
12
{
13
    public function init()
14
    {
15
        App::$primary->view->customAsset('js', '/moduleAsset/Ecommerce/js/cart.js');
16
    }
17
18
    public function getPayTypeHandlers($forSelect = false)
19
    {
20
        if (!$forSelect) {
21
            return $this->getSnippets('payTypeHandler');
22
        }
23
        $handlers = ['' => 'Не выбрано'];
24
        foreach ($this->getSnippets('payTypeHandler') as $key => $handler) {
25
            if (empty($handler)) {
26
                continue;
27
            }
28
            $handlers[$key] = $handler['name'];
29
        }
30
        return $handlers;
31
    }
32
33
    public function cartPayRecive($data)
34
    {
35
        $cart = Ecommerce\Cart::get($data['pay']->data);
36
        if ($cart) {
37
            $payed = true;
38
            foreach ($cart->pays as $pay) {
39
                if ($pay->pay_status_id != 2) {
40
                    $payed = false;
41
                    break;
42
                }
43
            }
44
            $cart->payed = $payed;
45
            $cart->save();
46
        }
47
    }
48
49 View Code Duplication
    public function parseFields($data, $cart)
50
    {
51
        $fields = \Ecommerce\UserAdds\Field::getList();
52
        $name = '';
53
        foreach ($fields as $field) {
54
            if ($field->save && !empty($data[$field->id])) {
55
                $name .= htmlspecialchars($data[$field->id]) . ' ';
56
            }
57
        }
58
        $name = trim($name);
59
60
        $userAdds = Ecommerce\UserAdds::get([['user_id', $cart->user->id], ['name', $name]]);
61
        if (!$userAdds) {
62
            $userAdds = new Ecommerce\UserAdds();
63
            $userAdds->user_id = $cart->user->id;
64
            $userAdds->name = $name;
65
            $userAdds->save();
66
            foreach ($fields as $field) {
67
                if (!$field->save) {
68
                    continue;
69
                }
70
                $userAddsValue = new Ecommerce\UserAdds\Value();
71
                $userAddsValue->value = htmlspecialchars($data[$field->id]);
72
                $userAddsValue->useradds_field_id = $field->id;
73
                $userAddsValue->useradds_id = $userAdds->id;
74
                $userAddsValue->save();
75
            }
76
        }
77
        $user = \Users\User::get($cart->user_id);
78
        foreach ($fields as $field) {
79
            $info = new \Ecommerce\Cart\Info();
80
            $info->name = $field->name;
81
            $info->value = htmlspecialchars($data[$field->id]);
82
            $info->useradds_field_id = $field->id;
83
            $info->cart_id = $cart->id;
84
            $info->save();
85
            $relations = [];
86
            if ($field->userfield) {
87
                if (strpos($field->userfield, ':')) {
88
                    $path = explode(':', $field->userfield);
89
                    if (!$user->{$path[0]}->{$path[1]}) {
90
                        $user->{$path[0]}->{$path[1]} = $info->value;
91
                        $relations[$path[0]] = $path[0];
92
                    }
93
                } else {
94
                    if (!$user->{$field->userfield}) {
95
                        $user->{$field->userfield} = $info->value;
96
                    }
97
                }
98
            }
99
            foreach ($relations as $rel) {
100
                $user->$rel->save();
101
            }
102
            $user->save();
103
        }
104
        return $userAdds;
105
    }
106
107 View Code Duplication
    public function parseDeliveryFields($data, $cart, $fields)
108
    {
109
        $name = '';
110
        foreach ($fields as $field) {
111
            if ($field->save && !empty($data[$field->id])) {
112
                $name .= htmlspecialchars($data[$field->id]) . ' ';
113
            }
114
        }
115
        $name = trim($name);
116
117
        $save = Ecommerce\Delivery\Save::get([['user_id', $cart->user->id], ['name', $name]]);
118
        if (!$save) {
119
            $save = new Ecommerce\Delivery\Save();
120
            $save->user_id = $cart->user->id;
121
            $save->name = $name;
122
            $save->save();
123
            foreach ($fields as $field) {
124
                if (!$field->save) {
125
                    continue;
126
                }
127
                $saveValue = new Ecommerce\Delivery\Value();
128
                $saveValue->value = htmlspecialchars($data[$field->id]);
129
                $saveValue->delivery_field_id = $field->id;
130
                $saveValue->delivery_save_id = $save->id;
131
                $saveValue->save();
132
            }
133
        }
134
        $user = \Users\User::get($cart->user_id);
135
        foreach ($fields as $field) {
136
            $info = new \Ecommerce\Cart\DeliveryInfo();
137
            $info->name = $field->name;
138
            $info->value = htmlspecialchars($data[$field->id]);
139
            $info->delivery_field_id = $field->id;
140
            $info->cart_id = $cart->id;
141
            $info->save();
142
            $relations = [];
143
            if ($field->userfield) {
144
                if (strpos($field->userfield, ':')) {
145
                    $path = explode(':', $field->userfield);
146
                    if (!$user->{$path[0]}->{$path[1]}) {
147
                        $user->{$path[0]}->{$path[1]} = $info->value;
148
                        $relations[$path[0]] = $path[0];
149
                    }
150
                } else {
151
                    if (!$user->{$field->userfield}) {
152
                        $user->{$field->userfield} = $info->value;
153
                    }
154
                }
155
            }
156
            foreach ($relations as $rel) {
157
                $user->$rel->save();
158
            }
159
            $user->save();
160
        }
161
        return $save;
162
    }
163
164
    public function getCurCart($create = true)
1 ignored issue
show
Coding Style introduced by
getCurCart uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
165
    {
166
        $cart = false;
167
        if (!empty($_SESSION['cart']['cart_id'])) {
168
            $cart = Ecommerce\Cart::get((int) $_SESSION['cart']['cart_id']);
169
        }
170
        if (!$cart && $create) {
171
            $cart = new Ecommerce\Cart();
172
            $cart->cart_status_id = 1;
173
            $cart->user_id = Users\User::$cur->id;
174
            $userCard = \Ecommerce\Card\Item::get(\Users\User::$cur->id, 'user_id');
175
            if ($userCard) {
176
                $cart->card_item_id = $userCard->id;
177
            }
178
            $cart->save();
179
            $_SESSION['cart']['cart_id'] = $cart->id;
180
        }
181
        return $cart;
182
    }
183
184
    public function parseOptions($options = [])
185
    {
186
        $selectOptions = [
187
            'where' => !empty($options['where']) ? $options['where'] : [],
188
            'distinct' => false,
189
            'join' => [],
190
            'order' => [],
191
            'start' => isset($options['start']) ? (int) $options['start'] : 0,
192
            'key' => isset($options['key']) ? $options['key'] : null,
193
            'limit' => !empty($options['count']) ? (int) $options['count'] : 0,
194
        ];
195
        if (!empty($options['sort']) && is_array($options['sort'])) {
196
            foreach ($options['sort'] as $col => $direction) {
197
                switch ($col) {
198
                    case 'price':
199
                        $selectOptions['order'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', strtolower($direction) == 'desc' ? 'desc' : 'asc'];
200
                        break;
201 View Code Duplication
                    case 'name':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
                        $selectOptions['order'][] = ['name', strtolower($direction) == 'desc' ? 'desc' : 'asc'];
203
                        break;
204 View Code Duplication
                    case 'sales':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205
                        $selectOptions['order'][] = ['sales', strtolower($direction) == 'desc' ? 'desc' : 'asc'];
206
                        break;
207 View Code Duplication
                    case 'weight':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
                        $selectOptions['order'][] = ['weight', strtolower($direction) == 'desc' ? 'desc' : 'asc'];
209
                        break;
210
                }
211
            }
212
        }
213
        $selectOptions['where'][] = ['deleted', 0];
214
        if (empty($this->config['view_empty_image'])) {
215
            $selectOptions['where'][] = ['image_file_id', 0, '!='];
216
        }
217
218
        $selectOptions['join'][] = [Ecommerce\Item\Offer::table(), Ecommerce\Item::index() . ' = ' . Ecommerce\Item\Offer::colPrefix() . Ecommerce\Item::index(), 'inner'];
219
220
        $selectOptions['join'][] = [Ecommerce\Item\Offer\Price::table(),
221
            Ecommerce\Item\Offer::index() . ' = ' . Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer::index() .
222
            (empty($this->config['show_zero_price']) ? ' and ' . Ecommerce\Item\Offer\Price::colPrefix() . 'price>0' : ''),
223
            empty($this->config['show_without_price']) ? 'inner' : 'left'];
224
225
        $selectOptions['join'][] = [
226
            Ecommerce\Item\Offer\Price\Type::table(), Ecommerce\Item\Offer\Price::colPrefix() . Ecommerce\Item\Offer\Price\Type::index() . ' = ' . Ecommerce\Item\Offer\Price\Type::index()
227
        ];
228
229
        $selectOptions['where'][] = [
230
            [Ecommerce\Item\Offer\Price\Type::index(), NULL, 'is'],
231
            [
232
                [Ecommerce\Item\Offer\Price\Type::colPrefix() . 'roles', '', '=', 'OR'],
233
                [Ecommerce\Item\Offer\Price\Type::colPrefix() . 'roles', '%|' . \Users\User::$cur->role_id . '|%', 'LIKE', 'OR'],
234
            ],
235
        ];
236
237
238
        if (!empty($this->config['view_filter'])) {
239
            if (!empty($this->config['view_filter']['options'])) {
240
                foreach ($this->config['view_filter']['options'] as $optionId => $optionValue) {
241
                    $selectOptions['join'][] = [Ecommerce\Item\Param::table(), Ecommerce\Item::index() . ' = ' . 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item::index() . ' AND ' .
242
                        'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' = "' . (int) $optionId . '" AND ' .
243
                        'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . 'value = "' . (int) $optionValue . '"',
244
                        'inner', 'option' . $optionId];
245
                }
246
            }
247
        }
248
        //filters
249
        if (!empty($options['filters'])) {
250
            foreach ($options['filters'] as $col => $filter) {
251
                switch ($col) {
252
                    case 'price':
253 View Code Duplication
                        if (!empty($filter['min'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
254
                            $selectOptions['where'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', (float) $filter['min'], '>='];
255
                        }
256 View Code Duplication
                        if (!empty($filter['max'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
257
                            $selectOptions['where'][] = [Ecommerce\Item\Offer\Price::colPrefix() . 'price', (float) $filter['max'], '<='];
258
                        }
259
                        break;
260 View Code Duplication
                    case 'options':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
261
                        foreach ($filter as $optionId => $optionValue) {
262
                            $optionId = (int) $optionId;
263
                            $selectOptions['join'][] = [Ecommerce\Item\Param::table(), Ecommerce\Item::index() . ' = ' . 'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item::index() . ' AND ' .
264
                                'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' = "' . (int) $optionId . '" AND ' .
265
                                'option' . $optionId . '.' . Ecommerce\Item\Param::colPrefix() . 'value = ' . \App::$cur->db->connection->pdo->quote($optionValue) . '',
266
                                'inner', 'option' . $optionId];
267
                        }
268
                        break;
269 View Code Duplication
                    case 'offerOptions':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
270
                        //$selectOptions['join'][] = [Ecommerce\Item\Offer::table(), Ecommerce\Item::index() . ' = offer.' . Ecommerce\Item\Offer::colPrefix() . Ecommerce\Item::index(), 'left', 'offer'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
271
                        foreach ($filter as $optionId => $optionValue) {
272
                            $optionId = (int) $optionId;
273
                            $selectOptions['join'][] = [Ecommerce\Item\Offer\Param::table(), Ecommerce\Item\Offer::index() . ' = ' . 'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . Ecommerce\Item\Offer::index() . ' AND ' .
274
                                'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . Ecommerce\Item\Offer\Option::index() . ' = "' . (int) $optionId . '" AND ' .
275
                                'offerOption' . $optionId . '.' . Ecommerce\Item\Offer\Param::colPrefix() . 'value = ' . \App::$cur->db->connection->pdo->quote($optionValue) . '',
276
                                'inner', 'offerOption' . $optionId];
277
                        }
278
                        break;
279
                }
280
            }
281
        }
282
        //parents
283
        if (!empty($options['parent']) && strpos($options['parent'], ',') !== false) {
284
            $first = true;
285
            $where = [];
286
            foreach (explode(',', $options['parent']) as $categoryId) {
287
                if (!$categoryId) {
288
                    continue;
289
                }
290
                $category = \Ecommerce\Category::get($categoryId);
291
                $where[] = ['tree_path', $category->tree_path . (int) $categoryId . '/%', 'LIKE', $first ? 'AND' : 'OR'];
292
                $first = false;
293
            }
294
            $selectOptions['where'][] = $where;
295
        } elseif (!empty($options['parent'])) {
296
            $category = \Ecommerce\Category::get($options['parent']);
297
            $selectOptions['where'][] = ['tree_path', $category->tree_path . (int) $options['parent'] . '/%', 'LIKE'];
298
        }
299
300
        //search
301
        if (!empty($options['search'])) {
302
            $searchStr = preg_replace('![^A-zА-я0-9 ]!iSu', ' ', $options['search']);
303
            $searchArr = [];
304
            foreach (explode(' ', $searchStr) as $part) {
305
                $part = trim($part);
306
                if ($part && strlen($part) > 2) {
307
                    $searchArr[] = ['search_index', '%' . $part . '%', 'LIKE'];
308
                }
309
            }
310
            if (!empty($searchArr)) {
311
                $selectOptions['where'][] = $searchArr;
312
            }
313
        }
314
        if (empty($this->config['view_empty_warehouse'])) {
315
            $warehouseIds = [];
316 View Code Duplication
            if (class_exists('Geography\City\Data')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
317
                $warehouses = \Geography\City\Data::get([['code', 'warehouses'], ['city_id', \Geography\City::$cur->id]]);
318
                if ($warehouses && $warehouses->data) {
319
                    foreach (explode(',', $warehouses->data) as $id) {
320
                        $warehouseIds[$id] = $id;
321
                    }
322
                }
323
            }
324
            $selectOptions['where'][] = [
325
                '(
326
          (SELECT COALESCE(sum(`' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . 'count`),0) 
327
            FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Item\Offer\Warehouse::table() . ' iciw 
328
            WHERE iciw.' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . '
329
                ' . ($warehouseIds ? ' AND iciw.' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Warehouse::index() . ' IN(' . implode(',', $warehouseIds) . ')' : '') . '
330
            )
331
          -
332
          (SELECT COALESCE(sum(' . \Ecommerce\Warehouse\Block::colPrefix() . 'count) ,0)
333
            FROM ' . \App::$cur->db->table_prefix . \Ecommerce\Warehouse\Block::table() . ' iewb
334
            inner JOIN ' . \App::$cur->db->table_prefix . \Ecommerce\Cart::table() . ' icc ON icc.' . \Ecommerce\Cart::index() . ' = iewb.' . \Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Cart::index() . ' AND (
335
                (`' . \Ecommerce\Cart::colPrefix() . 'warehouse_block` = 1 and `' . \Ecommerce\Cart::colPrefix() . 'cart_status_id` in(2,3,6)) ||
336
                (`' . \Ecommerce\Cart::colPrefix() . \Ecommerce\Cart\Status::index() . '` in(0,1) and `' . \Ecommerce\Cart::colPrefix() . 'date_last_activ` >=subdate(now(),INTERVAL 30 MINUTE))
337
            )
338
            WHERE iewb.' . \Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Item\Offer::index() . ' = ' . \Ecommerce\Item\Offer::index() . ')
339
          )',
340
                0,
341
                '>'
342
            ];
343
        }
344
345
346
347
348
349
350
        $selectOptions['group'] = Ecommerce\Item::index();
351
352
        return $selectOptions;
353
    }
354
355
    /**
356
     * Getting items params with params
357
     * 
358
     * @param array $params
359
     * @return array
360
     */
361
    public function getItemsParams($params = [])
362
    {
363
        $selectOptions = $this->parseOptions($params);
364
        $items = Ecommerce\Item::getList($selectOptions);
365
        $items = Ecommerce\Item\Param::getList([
366
                    'where' => ['item_id', array_keys($items), 'IN'],
367
                    'join' => [[Ecommerce\Item\Option::table(), Ecommerce\Item\Option::index() . ' = ' . \Ecommerce\Item\Param::colPrefix() . Ecommerce\Item\Option::index() . ' and ' . \Ecommerce\Item\Option::colPrefix() . 'searchable = 1', 'inner']],
368
                    'distinct' => \Ecommerce\Item\Option::index()
369
        ]);
370
        return $items;
371
    }
372
373
    /**
374
     * Getting items with params
375
     * 
376
     * @param array $params
377
     * @return array
378
     */
379
    public function getItems($params = [])
380
    {
381
        $selectOptions = $this->parseOptions($params);
382
        $items = Ecommerce\Item::getList($selectOptions);
383
        return $items;
384
    }
385
386
    /**
387
     * Return count of items with params
388
     * 
389
     * @param array $params
390
     * @return int
391
     */
392
    public function getItemsCount($params = [])
393
    {
394
        $selectOptions = $this->parseOptions($params);
395
        $selectOptions['distinct'] = \Ecommerce\Item::index();
396
        $counts = Ecommerce\Item::getCount($selectOptions);
397
        if (is_array($counts)) {
398
            $sum = 0;
399
            foreach ($counts as $count) {
400
                $sum +=$count['count'];
401
            }
402
            return $sum;
403
        }
404
        return $counts;
405
    }
406
407 View Code Duplication
    public function viewsCategoryList($inherit = true)
408
    {
409
        $return = [];
410
        if ($inherit) {
411
            $return['inherit'] = 'Как у родителя';
412
        }
413
        $return['itemList'] = 'Список товаров';
414
        $conf = App::$primary->view->template->config;
415
        if (!empty($conf['files']['modules']['Ecommerce'])) {
416
            foreach ($conf['files']['modules']['Ecommerce'] as $file) {
417
                if ($file['type'] == 'Category') {
418
                    $return[$file['file']] = $file['name'];
419
                }
420
            }
421
        }
422
        return $return;
423
    }
424
425 View Code Duplication
    public function templatesCategoryList()
426
    {
427
        $return = [
428
            'inherit' => 'Как у родителя',
429
            'current' => 'Текущая тема'
430
        ];
431
432
        $conf = App::$primary->view->template->config;
433
434
        if (!empty($conf['files']['aditionTemplateFiels'])) {
435
            foreach ($conf['files']['aditionTemplateFiels'] as $file) {
436
                $return[$file['file']] = '- ' . $file['name'];
437
            }
438
        }
439
        return $return;
440
    }
441
442
    public function cartStatusDetector($event)
443
    {
444
        $cart = $event['eventObject'];
445
        if (!empty($cart->_changedParams['cart_cart_status_id'])) {
446
            $cart->date_status = date('Y-m-d H:i:s');
447
            $event = new Ecommerce\Cart\Event(['cart_id' => $cart->id, 'user_id' => \Users\User::$cur->id, 'cart_event_type_id' => 5, 'info' => $cart->cart_status_id]);
448
            $event->save();
449
450
            $prev_status_id = $cart->_changedParams['cart_cart_status_id'];
451
            $now_status_id = $cart->cart_status_id;
452
453
            $status = Ecommerce\Cart\Status::getList(['where' => ['id', implode(',', [$prev_status_id, $now_status_id]), 'IN']]);
454
455
            $prefix = isset(App::$cur->ecommerce->config['orderPrefix']) ? $config = App::$cur->ecommerce->config['orderPrefix'] : '';
456
            \App::$cur->users->AddUserActivity($cart->user_id, 3, "Статус вашего заказа номер {$prefix}{$cart->id} изменился с {$status[$prev_status_id]->name} на {$status[$now_status_id]->name}");
457
458
            if ($cart->cart_status_id == 5) {
459
                Inji::$inst->event('ecommerceCartClosed', $cart);
460
            }
461
        }
462
        return $cart;
463
    }
464
465
    public function cardTrigger($event)
466
    {
467
        $cart = $event['eventObject'];
468
        if ($cart->card) {
469
            $sum = 0;
470
            foreach ($cart->cartItems as $cartItem) {
471
                $sum += $cartItem->final_price * $cartItem->count;
472
            }
473
            $cardItemHistory = new Ecommerce\Card\Item\History();
474
            $cardItemHistory->amount = $sum;
475
            $cardItemHistory->card_item_id = $cart->card_item_id;
476
            $cardItemHistory->save();
477
            $cart->card->sum += $sum;
478
            $cart->card->save();
479
        }
480
        return $cart;
481
    }
482
483
    public function bonusTrigger($event)
484
    {
485
        $cart = $event['eventObject'];
486
        foreach ($cart->cartItems as $cartItem) {
487
            foreach ($cartItem->price->offer->bonuses as $bonus) {
488
                if ($bonus->limited && $bonus->left <= 0) {
489
                    continue;
490
                } elseif ($bonus->limited && $bonus->left > 0) {
491
                    $bonus->left -= 1;
492
                    $bonus->save();
493
                }
494
                switch ($bonus->type) {
495
                    case'currency':
0 ignored issues
show
Coding Style introduced by
As per coding-style, case should be followed by a single space.

As per the PSR-2 coding standard, there must be a space after the case keyword, instead of the test immediately following it.

switch (true) {
    case!isset($a):  //wrong
        doSomething();
        break;
    case !isset($b):  //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
496
                        $currency = \Money\Currency::get($bonus->value);
497
                        $wallets = App::$cur->money->getUserWallets($cart->user->id);
498
                        $wallets[$currency->id]->diff($bonus->count, 'Бонус за покупку');
499
                        break;
500
                }
501
            }
502
        }
503
        return $cart;
504
    }
505
506
}
507