Issues (1519)

system/modules/Ecommerce/Ecommerce.php (41 issues)

1
<?php
2
namespace Inji;
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
12
class Ecommerce extends Module {
13
14
    public function init() {
15
        App::$primary->view->customAsset('js', '/moduleAsset/Ecommerce/js/cart.js');
0 ignored issues
show
Bug Best Practice introduced by
The property view does not exist on Inji\App. Since you implemented __get, consider adding a @property annotation.
Loading history...
The method customAsset() does not exist on Inji\Module. It seems like you code against a sub-type of Inji\Module such as Inji\Db. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

15
        App::$primary->view->/** @scrutinizer ignore-call */ 
16
                             customAsset('js', '/moduleAsset/Ecommerce/js/cart.js');
Loading history...
The method customAsset() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

15
        App::$primary->view->/** @scrutinizer ignore-call */ 
16
                             customAsset('js', '/moduleAsset/Ecommerce/js/cart.js');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
16
        if (class_exists('Users\User') && Users\User::$cur->id) {
17
            $favs = !empty($_COOKIE['ecommerce_favitems']) ? json_decode($_COOKIE['ecommerce_favitems'], true) : [];
18
            if ($favs) {
19
                foreach ($favs as $itemId) {
20
                    $fav = \Ecommerce\Favorite::get([['user_id', Users\User::$cur->id], ['item_id', (int) $itemId]]);
0 ignored issues
show
The type Ecommerce\Favorite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
                    if (!$fav) {
22
                        $item = \Ecommerce\Item::get((int) $itemId);
0 ignored issues
show
The type Ecommerce\Item was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
                        if ($item) {
24
                            $fav = new \Ecommerce\Favorite([
25
                                'user_id' => Users\User::$cur->id,
26
                                'item_id' => $itemId
27
                            ]);
28
                            $fav->save();
29
                        }
30
                    }
31
                }
32
                if (!headers_sent()) {
33
                    setcookie("ecommerce_favitems", json_encode([]), time() + 360000, "/");
34
                }
35
            }
36
        }
37
    }
38
39
    public function availablePricesTypes() {
40
        $types = [];
41
        $where = [];
42
        if (!empty(\App::$cur->Ecommerce->config['available_price_types'])) {
43
            foreach (\App::$cur->Ecommerce->config['available_price_types'] as $typeId) {
44
                $types[$typeId] = $typeId;
45
            }
46
            $where[] = ['id', array_keys($types), 'IN'];
47
        } else {
48
            return true;
49
        }
50
51
        $dbTypes = \Ecommerce\Item\Offer\Price\Type::getList(['where' => $where, 'order' => ['weight', 'ASC']]);
0 ignored issues
show
The type Ecommerce\Item\Offer\Price\Type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
52
        foreach ($dbTypes as $type) {
53
            if ($type->roles && strpos($type->roles, "|" . \Users\User::$cur->role_id . "|") === false) {
54
                unset($types[$type->id]);
55
            }
56
        }
57
58
        if (\Users\User::$cur->id) {
59
            foreach (Ecommerce\Card\Item::getList(['where' => ['user_id', \Users\User::$cur->id]]) as $cardItem) {
60
                foreach ($cardItem->card->prices as $priceType) {
0 ignored issues
show
Bug Best Practice introduced by
The property card does not exist on Inji\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
61
                    $types[$priceType->id] = $priceType->id;
62
                }
63
            }
64
        }
65
        return $types;
66
    }
67
68
    public function getPayTypeHandlers($forSelect = false) {
69
        if (!$forSelect) {
70
            return $this->getSnippets('payTypeHandler');
71
        }
72
        $handlers = ['' => 'Не выбрано'];
73
        foreach ($this->getSnippets('payTypeHandler') as $key => $handler) {
74
            if (empty($handler)) {
75
                continue;
76
            }
77
            $handlers[$key] = $handler['name'];
78
        }
79
        return $handlers;
80
    }
81
82
    public function cartPayRecive($data) {
83
        $cart = Ecommerce\Cart::get($data['pay']->data);
84
        if ($cart) {
85
            $payed = true;
86
            foreach ($cart->pays as $pay) {
87
                if ($pay->pay_status_id != 2) {
88
                    $payed = false;
89
                    break;
90
                }
91
            }
92
            $cart->payed = $payed;
93
            $cart->save();
94
        }
95
    }
96
97
    /**
98
     * @param array $data
99
     * @param \Ecommerce\Cart $cart
100
     * @return bool|\Ecommerce\UserAdds
0 ignored issues
show
The type Ecommerce\UserAdds was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
101
     */
102
    public function parseFields($data, $cart) {
103
        $user = \Users\User::get($cart->user_id);
104
        $fields = \Ecommerce\UserAdds\Field::getList();
0 ignored issues
show
The type Ecommerce\UserAdds\Field was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
105
        if ($user) {
106
            $name = '';
107
            foreach ($fields as $field) {
108
                if ($field->save && isset($data[$field->id])) {
109
                    $name .= htmlspecialchars($data[$field->id]) . ' ';
110
                }
111
            }
112
            $name = trim($name);
113
114
            $userAdds = Ecommerce\UserAdds::get([['user_id', $cart->user->id], ['name', $name]]);
115
            if (!$userAdds) {
116
                $userAdds = new Ecommerce\UserAdds();
117
                $userAdds->user_id = $cart->user->id;
118
                $userAdds->name = $name;
119
                $userAdds->save();
120
            }
121
            foreach ($fields as $field) {
122
                if (!$field->save) {
123
                    continue;
124
                }
125
                if (isset($data[$field->id])) {
126
                    $value = htmlspecialchars($data[$field->id]);
127
                    if (!isset($userAdds->values[$field->id])) {
128
                        $userAddsValue = new Ecommerce\UserAdds\Value();
129
                        $userAddsValue->value = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property value does not exist on Inji\Ecommerce\UserAdds\Value. Since you implemented __set, consider adding a @property annotation.
Loading history...
130
                        $userAddsValue->useradds_field_id = $field->id;
0 ignored issues
show
Bug Best Practice introduced by
The property useradds_field_id does not exist on Inji\Ecommerce\UserAdds\Value. Since you implemented __set, consider adding a @property annotation.
Loading history...
131
                        $userAddsValue->useradds_id = $userAdds->id;
0 ignored issues
show
Bug Best Practice introduced by
The property useradds_id does not exist on Inji\Ecommerce\UserAdds\Value. Since you implemented __set, consider adding a @property annotation.
Loading history...
132
                        $userAddsValue->save();
133
                    } else {
134
                        $userAddsValue = $userAdds->values[$field->id];
135
                        $userAddsValue->value = $value;
136
                        $userAddsValue->save();
137
                    }
138
                }
139
            }
140
        }
141
142
        foreach ($fields as $field) {
143
            if (isset($data[$field->id])) {
144
                $value = htmlspecialchars($data[$field->id]);
145
                if (!isset($cart->infos[$field->id])) {
146
                    $info = new \Ecommerce\Cart\Info();
0 ignored issues
show
The type Ecommerce\Cart\Info was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
147
                    $info->name = $field->name;
148
                    $info->value = $value;
149
                    $info->useradds_field_id = $field->id;
150
                    $info->cart_id = $cart->id;
151
                    $info->save();
152
                } else {
153
                    $info = $cart->infos[$field->id];
154
                    $info->value = $value;
155
                    $info->save();
156
                }
157
            }
158
            if (isset($info) && $user && $field->userfield) {
159
                $relations = [];
160
                if (strpos($field->userfield, ':')) {
161
                    $path = explode(':', $field->userfield);
162
                    if (!$user->{$path[0]}->{$path[1]}) {
163
                        $user->{$path[0]}->{$path[1]} = $info->value;
164
                        $relations[$path[0]] = $path[0];
165
                    }
166
                } else {
167
                    if (!$user->{$field->userfield}) {
168
                        $user->{$field->userfield} = $info->value;
169
                    }
170
                }
171
172
                foreach ($relations as $rel) {
173
                    $user->$rel->save();
174
                }
175
                $user->save();
176
            }
177
        }
178
        return isset($userAdds) ? $userAdds : false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return IssetNode ? $userAdds : false also could return the type Inji\Ecommerce\UserAdds which is incompatible with the documented return type Ecommerce\UserAdds|boolean.
Loading history...
179
    }
180
181
    /**
182
     * @param array $data
183
     * @param \Ecommerce\Cart $cart
184
     * @param \Ecommerce\Delivery\Field[] $fields
185
     * @return bool|\Ecommerce\Delivery\Save
0 ignored issues
show
The type Ecommerce\Delivery\Save was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
186
     */
187
    public function parseDeliveryFields($data, $cart, $fields) {
188
        $user = \Users\User::get($cart->user_id);
189
        if ($user) {
190
            $name = '';
191
            foreach ($fields as $field) {
192
                if ($field->save && isset($data[$field->id])) {
193
                    $name .= htmlspecialchars($data[$field->id]) . ' ';
194
                }
195
            }
196
            $name = trim($name);
197
198
            $save = Ecommerce\Delivery\Save::get([['user_id', $cart->user->id], ['name', $name]]);
199
            if (!$save) {
200
                $save = new Ecommerce\Delivery\Save();
201
                $save->user_id = $cart->user->id;
202
                $save->name = $name;
203
                $save->save();
204
            }
205
            foreach ($fields as $field) {
206
                if (!$field->save) {
207
                    continue;
208
                }
209
                if (isset($data[$field->id])) {
210
                    $value = htmlspecialchars($data[$field->id]);
211
                    if (!isset($save->values[$field->id])) {
212
                        $saveValue = new Ecommerce\Delivery\Value();
213
                        $saveValue->value = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property value does not exist on Inji\Ecommerce\Delivery\Value. Since you implemented __set, consider adding a @property annotation.
Loading history...
214
                        $saveValue->delivery_field_id = $field->id;
0 ignored issues
show
Bug Best Practice introduced by
The property delivery_field_id does not exist on Inji\Ecommerce\Delivery\Value. Since you implemented __set, consider adding a @property annotation.
Loading history...
215
                        $saveValue->delivery_save_id = $save->id;
0 ignored issues
show
Bug Best Practice introduced by
The property delivery_save_id does not exist on Inji\Ecommerce\Delivery\Value. Since you implemented __set, consider adding a @property annotation.
Loading history...
216
                        $saveValue->save();
217
                    } else {
218
                        $saveValue = $save->values[$field->id];
219
                        $saveValue->value = $value;
220
                        $saveValue->save();
221
                    }
222
                }
223
            }
224
        }
225
        foreach ($fields as $field) {
226
            if (isset($data[$field->id])) {
227
                $value = htmlspecialchars($data[$field->id]);
228
                if (!isset($cart->deliveryInfos[$field->id])) {
229
                    $info = new \Ecommerce\Cart\DeliveryInfo();
0 ignored issues
show
The type Ecommerce\Cart\DeliveryInfo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
230
                    $info->name = $field->name;
231
                    $info->value = $value;
232
                    $info->delivery_field_id = $field->id;
233
                    $info->cart_id = $cart->id;
234
                    $info->save();
235
                } else {
236
                    $info = $cart->deliveryInfos[$field->id];
237
                    $info->value = $value;
238
                    $info->save();
239
                }
240
            }
241
242
            if (isset($info) && $user && $field->userfield) {
243
                $relations = [];
244
                if (strpos($field->userfield, ':')) {
245
                    $path = explode(':', $field->userfield);
246
                    if (!$user->{$path[0]}->{$path[1]}) {
247
                        $user->{$path[0]}->{$path[1]} = $info->value;
248
                        $relations[$path[0]] = $path[0];
249
                    }
250
                } else {
251
                    if (!$user->{$field->userfield}) {
252
                        $user->{$field->userfield} = $info->value;
253
                    }
254
                }
255
                foreach ($relations as $rel) {
256
                    $user->$rel->save();
257
                }
258
                $user->save();
259
            }
260
        }
261
        return isset($save) ? $save : false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return IssetNode ? $save : false also could return the type Inji\Ecommerce\Delivery\Save which is incompatible with the documented return type Ecommerce\Delivery\Save|boolean.
Loading history...
262
    }
263
264
    /**
265
     * @param bool $create
266
     * @return \Ecommerce\Cart
267
     */
268
    public function getCurCart($create = true) {
269
        $cart = false;
270
        if (!empty($_SESSION['cart']['cart_id'])) {
271
            $cart = Ecommerce\Cart::get((int) $_SESSION['cart']['cart_id']);
272
        }
273
        if (!$cart && $create) {
274
            $cart = new Ecommerce\Cart();
275
            $cart->cart_status_id = 1;
276
            $cart->user_id = Users\User::$cur->id;
277
            $userCard = \Ecommerce\Card\Item::get(\Users\User::$cur->id, 'user_id');
0 ignored issues
show
The type Ecommerce\Card\Item was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
278
            if ($userCard) {
279
                $cart->card_item_id = $userCard->id;
280
            }
281
            $cart->save();
282
            $_SESSION['cart']['cart_id'] = $cart->id;
283
        }
284
        $defaultDelivery = \Ecommerce\Delivery::get(1, 'default');
285
        if ($cart && $defaultDelivery && !$cart->delivery_id) {
286
            $cart->delivery_id = $defaultDelivery->id;
287
            $cart->save();
288
        }
289
        return $cart;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $cart returns the type Inji\Ecommerce\Cart|false which is incompatible with the documented return type Ecommerce\Cart.
Loading history...
290
    }
291
292
    /**
293
     * Getting items params with params
294
     *
295
     * @param array $params
296
     * @return array
297
     */
298
    public function getItemsParams($params = [], $saveFilterOptions = []) {
299
        $filtersOptions = !empty($params['filters']['options']) ? $params['filters']['options'] : [];
300
        $filters = $params['filters'];
301
        $params['filters'] = [];
302
        foreach ($filtersOptions as $optionId => $filter) {
303
            if (in_array($optionId, $saveFilterOptions)) {
304
                $params['filters']['options'][$optionId] = $filter;
305
            }
306
        }
307
        if (!empty($filters['best'])) {
308
            $params['filters']['best'] = $filters['best'];
309
        }
310
311
        $selectOptions = Ecommerce\OptionsParser::parse($params);
0 ignored issues
show
The type Inji\Ecommerce\OptionsParser was not found. Did you mean Ecommerce\OptionsParser? If so, make sure to prefix the type with \.
Loading history...
312
        $selectOptions['array'] = true;
313
        $items = Ecommerce\Item::getList($selectOptions);
314
        if (!$items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $items of type array<mixed,Inji\Model> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
315
            return [];
316
        }
317
        $cols = array_keys(App::$cur->db->getTableCols(\Ecommerce\Item\Option::table()));
0 ignored issues
show
The type Ecommerce\Item\Option was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
318
        $cols[] = \Ecommerce\Item\Param::colPrefix() . \Ecommerce\Item::index();
0 ignored issues
show
The type Ecommerce\Item\Param was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
319
        $selectOptions = [
320
            'where' => ['view', 1],
321
            'join' => [
322
                [Ecommerce\Item\Param::table(), \Ecommerce\Item\Option::index() . ' = ' . Ecommerce\Item\Param::colPrefix() . \Ecommerce\Item\Option::index() . ' and ' . Ecommerce\Item\Param::colPrefix() . Ecommerce\Item::index() . ' IN (' . implode(',', array_keys($items)) . ')', 'inner'],
323
            ],
324
            'distinct' => \Ecommerce\Item\Option::index(),
325
            'group' => \Ecommerce\Item\Option::index(),
326
            'cols' => implode(',', $cols)
327
        ];
328
        $options = Ecommerce\Item\Option::getList($selectOptions);
329
        return $options;
330
    }
331
332
    /**
333
     * Getting items with params
334
     *
335
     * @param array $params
336
     * @return array
337
     */
338
    public function getItems($params = []) {
339
        $selectOptions = Ecommerce\OptionsParser::parse($params);
340
        $items = Ecommerce\Item::getList($selectOptions);
341
        return $items;
342
    }
343
344
    /**
345
     * Return count of items with params
346
     *
347
     * @param array $params
348
     * @return int
349
     */
350
    public function getItemsCount($params = []) {
351
        $selectOptions = Ecommerce\OptionsParser::parse($params, true);
352
        $counts = Ecommerce\Item::getCount($selectOptions);
353
        if (is_array($counts)) {
354
            $sum = 0;
355
            foreach ($counts as $count) {
356
                $sum += $count['count'];
357
            }
358
            return $sum;
359
        }
360
        return $counts;
361
    }
362
363
    public function viewsCategoryList($inherit = true) {
364
        $return = [];
365
        if ($inherit) {
366
            $return['inherit'] = 'Как у родителя';
367
        }
368
        $return['itemList'] = 'Список товаров';
369
        $conf = App::$primary->view->template->config;
0 ignored issues
show
Bug Best Practice introduced by
The property view does not exist on Inji\App. Since you implemented __get, consider adding a @property annotation.
Loading history...
370
        if (!empty($conf['files']['modules']['Ecommerce'])) {
371
            foreach ($conf['files']['modules']['Ecommerce'] as $file) {
372
                if ($file['type'] == 'Category') {
373
                    $return[$file['file']] = $file['name'];
374
                }
375
            }
376
        }
377
        return $return;
378
    }
379
380
    public function templatesCategoryList() {
381
        $return = [
382
            'inherit' => 'Как у родителя',
383
            'current' => 'Текущая тема'
384
        ];
385
386
        $conf = App::$primary->view->template->config;
0 ignored issues
show
Bug Best Practice introduced by
The property view does not exist on Inji\App. Since you implemented __get, consider adding a @property annotation.
Loading history...
387
388
        if (!empty($conf['files']['aditionTemplateFiels'])) {
389
            foreach ($conf['files']['aditionTemplateFiels'] as $file) {
390
                $return[$file['file']] = '- ' . $file['name'];
391
            }
392
        }
393
        return $return;
394
    }
395
396
    public function cartStatusDetector($event) {
397
        $cart = $event['eventObject'];
398
        if (!empty($cart->_changedParams['cart_cart_status_id'])) {
399
            $cart->date_status = date('Y-m-d H:i:s');
400
            $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]);
401
            $event->save();
402
403
            $prev_status_id = $cart->_changedParams['cart_cart_status_id'];
404
            $now_status_id = $cart->cart_status_id;
405
406
            $status = Ecommerce\Cart\Status::getList(['where' => ['id', implode(',', [$prev_status_id, $now_status_id]), 'IN']]);
407
408
            $prefix = isset(App::$cur->ecommerce->config['orderPrefix']) ? $config = App::$cur->ecommerce->config['orderPrefix'] : '';
0 ignored issues
show
The assignment to $config is dead and can be removed.
Loading history...
Bug Best Practice introduced by
The property ecommerce does not exist on Inji\App. Since you implemented __get, consider adding a @property annotation.
Loading history...
409
            \App::$cur->users->AddUserActivity($cart->user_id, 3, "Статус вашего заказа номер {$prefix}{$cart->id} изменился с {$status[$prev_status_id]->name} на {$status[$now_status_id]->name}");
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Inji\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
410
411
            if ($cart->cart_status_id == 5) {
412
                Inji::$inst->event('ecommerceCartClosed', $cart);
413
            }
414
        }
415
        return $cart;
416
    }
417
418
    public function cardTrigger($event) {
419
        $cart = $event['eventObject'];
420
        if ($cart->card) {
421
            $sum = 0;
422
            foreach ($cart->cartItems as $cartItem) {
423
                $sum += $cartItem->final_price * $cartItem->count;
424
            }
425
            $cardItemHistory = new Ecommerce\Card\Item\History();
426
            $cardItemHistory->amount = $sum;
0 ignored issues
show
Bug Best Practice introduced by
The property amount does not exist on Inji\Ecommerce\Card\Item\History. Since you implemented __set, consider adding a @property annotation.
Loading history...
427
            $cardItemHistory->card_item_id = $cart->card_item_id;
0 ignored issues
show
Bug Best Practice introduced by
The property card_item_id does not exist on Inji\Ecommerce\Card\Item\History. Since you implemented __set, consider adding a @property annotation.
Loading history...
428
            $cardItemHistory->save();
429
            $cart->card->sum += $sum;
430
            $cart->card->save();
431
        }
432
        return $cart;
433
    }
434
435
    public function bonusTrigger($event) {
436
        $cart = $event['eventObject'];
437
        foreach ($cart->cartItems as $cartItem) {
438
            foreach ($cartItem->price->offer->bonuses as $bonus) {
439
                if ($bonus->limited && $bonus->left <= 0) {
440
                    continue;
441
                } elseif ($bonus->limited && $bonus->left > 0) {
442
                    $bonus->left -= 1;
443
                    $bonus->save();
444
                }
445
                switch ($bonus->type) {
446
                    case 'currency':
447
                        $currency = \Money\Currency::get($bonus->value);
448
                        $wallets = App::$cur->money->getUserWallets($cart->user->id);
0 ignored issues
show
The method getUserWallets() does not exist on Inji\Module. It seems like you code against a sub-type of Inji\Module such as Inji\Db. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

448
                        /** @scrutinizer ignore-call */ 
449
                        $wallets = App::$cur->money->getUserWallets($cart->user->id);
Loading history...
Bug Best Practice introduced by
The property money does not exist on Inji\App. Since you implemented __get, consider adding a @property annotation.
Loading history...
The method getUserWallets() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

448
                        /** @scrutinizer ignore-call */ 
449
                        $wallets = App::$cur->money->getUserWallets($cart->user->id);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
449
                        $wallets[$currency->id]->diff($bonus->count, 'Бонус за покупку');
0 ignored issues
show
The property id does not exist on false.
Loading history...
450
                        break;
451
                }
452
            }
453
        }
454
        return $cart;
455
    }
456
457
    public function sitemap() {
458
        $map = [];
459
        $zeroItems = \Ecommerce\Item::getList(['where' => ['category_id', 0], 'array' => true, 'cols' => ['item_id', 'item_name']]);
460
        foreach ($zeroItems as $item) {
461
            $map[] = [
462
                'name' => $item['item_name'],
463
                'url' => [
464
                    'loc' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . INJI_DOMAIN_NAME . '/ecommerce/view/' . $item['item_id']
465
                ],
466
            ];
467
        }
468
469
        $categorys = \Ecommerce\Category::getList(['where' => ['parent_id', 0]]);
0 ignored issues
show
The type Ecommerce\Category was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
470
        $scan = function ($category, $scan) {
471
            $map = [];
472
473
            foreach ($category->items(['array' => true, 'cols' => ['item_id', 'item_name']]) as $item) {
474
                $map[] = [
475
                    'name' => $item['item_name'],
476
                    'url' => [
477
                        'loc' => (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . INJI_DOMAIN_NAME . '/ecommerce/view/' . $item['item_id']
478
                    ],
479
                ];
480
            }
481
            foreach ($category->catalogs as $child) {
482
                $map = array_merge($map, $scan($child, $scan));
483
            }
484
            return $map;
485
        };
486
        foreach ($categorys as $category) {
487
            $map = array_merge($map, $scan($category, $scan));
488
        }
489
        return $map;
490
    }
491
492
    public function getFavoriteCount() {
493
        if (Inji\Users\User::$cur->id) {
0 ignored issues
show
The type Inji\Inji\Users\User was not found. Did you mean Inji\Users\User? If so, make sure to prefix the type with \.
Loading history...
494
            return Favorite::getCount(['user_id', Inji\Users\User::$cur->id]);
0 ignored issues
show
The type Inji\Favorite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
495
        } else {
496
            $favs = !empty($_COOKIE['ecommerce_favitems']) ? json_decode($_COOKIE['ecommerce_favitems'], true) : [];
497
            return count($favs);
498
        }
499
    }
500
501
    public function siteSearch($search) {
502
        //items pages
503
        $count = $this->getItemsCount([
504
            'search' => trim($search),
505
        ]);
506
        //items
507
        $items = $this->getItems([
508
            'start' => 0,
509
            'count' => 10,
510
            'search' => trim($search),
511
        ]);
512
        $searchResult = [];
513
        foreach ($items as $item) {
514
            $details = '<div>';
515
            if ($item->image) {
516
                $details .= "<img style='margin-right:10px;margin-bottom:10px;' class='pull-left' src ='" . Inji\Statics::file($item->image->path, '70x70', 'q') . "' />";
0 ignored issues
show
The type Inji\Inji\Statics was not found. Did you mean Inji\Statics? If so, make sure to prefix the type with \.
Loading history...
517
            }
518
            $details .= '<b>' . $item->category->name . '</b><br />';
519
            $shortdes = mb_substr($item->description, 0, 200);
520
            $shortdes = mb_substr($shortdes, 0, mb_strrpos($shortdes, ' '));
521
            $details .= $shortdes;
522
            if (mb_strlen($item->description) > $shortdes) {
523
                $details .= '...';
524
            }
525
            $details .= '<div class="clearfix"></div> </div>';
526
            $searchResult[] = [
527
                'title' => $item->name(),
528
                'details' => $details,
529
                'href' => '/ecommerce/view/' . $item->id
530
            ];
531
        }
532
        return ['name' => 'Онлайн магазин', 'count' => $count, 'result' => $searchResult, 'detailSearch' => '/ecommerce/itemList?search=' . $search];
533
    }
534
535
    public function priceTypes() {
536
        $payTypes = ['custom' => 'Настраиваемая стоимость'];
537
        foreach ($this->getObjects('Ecommerce\\DeliveryHelper') as $className) {
538
            $payTypes[$className] = $className::$name;
539
        }
540
        return $payTypes;
541
    }
542
}