Test Failed
Push — master ( 23cc6e...476483 )
by Alexey
05:28
created

Offer::getPrice()   B

Complexity

Conditions 8
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 8
nc 3
nop 0
dl 0
loc 12
rs 7.7777
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Item offer
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\Item;
13
/**
14
 * Class Offer
15
 *
16
 * @property int $id
17
 * @property int $item_id
18
 * @property string $name
19
 * @property string $article
20
 * @property int $weight
21
 * @property string $date_create
22
 *
23
 * @property-read \Ecommerce\Item\Offer\Warehouse[] $warehouses
24
 * @property-read \Ecommerce\Item\Offer\Price[] $prices
25
 * @method \Ecommerce\Item\Offer\Price[] prices($options)
26
 * @property-read \Ecommerce\Item\Offer\Bonus[] $bonuses
27
 * @property-read \Ecommerce\Item\Offer\Param[] $options
28
 * @property-read \Ecommerce\Item $item
29
 */
30
class Offer extends \Model {
31
32
    public static $objectName = 'Торговое предложение';
33
    public static $cols = [
34
        //Основные параметры
35
        'item_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'item'],
36
        'name' => ['type' => 'text'],
37
        'article' => ['type' => 'text'],
38
        //Системные
39
        'weight' => ['type' => 'number'],
40
        'date_create' => ['type' => 'dateTime'],
41
        //Менеджеры
42
        'warehouse' => ['type' => 'dataManager', 'relation' => 'warehouses'],
43
        'price' => ['type' => 'dataManager', 'relation' => 'prices'],
44
        'option' => ['type' => 'dataManager', 'relation' => 'options'],
45
    ];
46
    public static $labels = [
47
        'name' => 'Название',
48
        'article' => 'Артикул',
49
        'warehouse' => 'Наличие на складах',
50
        'price' => 'Цены',
51
        'option' => 'Параметры предложения'
52
    ];
53
    public static $dataManagers = [
54
        'manager' => [
55
            'cols' => [
56
                'name', 'article', 'warehouse', 'price', 'option'
57
            ]
58
        ]
59
    ];
60
    public static $forms = [
61
        'manager' => [
62
            'map' => [
63
                ['name', 'article'],
64
                ['warehouse'],
65
                ['price'],
66
                ['option'],
67
            ]
68
        ]
69
    ];
70
71
    public static function relations() {
72
        return [
73
            'warehouses' => [
74
                'type' => 'many',
75
                'model' => 'Ecommerce\Item\Offer\Warehouse',
76
                'col' => 'item_offer_id'
77
            ],
78
            'prices' => [
79
                'type' => 'many',
80
                'model' => 'Ecommerce\Item\Offer\Price',
81
                'col' => 'item_offer_id',
82
            ],
83
            'bonuses' => [
84
                'type' => 'many',
85
                'model' => 'Ecommerce\Item\Offer\Bonus',
86
                'col' => 'item_offer_id',
87
            ],
88
            'options' => [
89
                'type' => 'many',
90
                'model' => 'Ecommerce\Item\Offer\Param',
91
                'col' => 'item_offer_id',
92
                'resultKey' => 'item_offer_option_id',
93
                'join' => [Offer\Option::table(), Offer\Option::index() . ' = ' . Offer\Param::colPrefix() . Offer\Option::index()]
94
            ],
95
            'item' => [
96
                'model' => 'Ecommerce\Item',
97
                'col' => 'item_id'
98
            ]
99
        ];
100
    }
101
102
    public function changeWarehouse($count) {
103
        $warehouse = Offer\Warehouse::get([['count', '0', '>'], ['item_offer_id', $this->id]]);
104
        if ($warehouse) {
105
            $warehouse->count += (float) $count;
106
            $warehouse->save();
107
        } else {
108
            $warehouse = Offer\Warehouse::get([['item_offer_id', $this->id]]);
109
            if ($warehouse) {
110
                $warehouse->count += (float) $count;
111
                $warehouse->save();
112
            }
113
        }
114
    }
115
116
    public function warehouseCount($cart_id = 0) {
117
        $warehouseIds = [];
118
        if (\App::$cur->geography && \Geography\City::$cur) {
119
            $warehouses = \Geography\City\Data::get([['code', 'warehouses'], ['city_id', \Geography\City::$cur->id]]);
120
            if ($warehouses && $warehouses->data) {
121
                foreach (explode(',', $warehouses->data) as $id) {
122
                    $warehouseIds[$id] = $id;
123
                }
124
            }
125
        }
126
        if ($warehouseIds) {
127
            \App::$cur->db->where(\Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Warehouse::index(), $warehouseIds, 'IN');
128
        }
129
        \App::$cur->db->where(\Ecommerce\Item\Offer\Warehouse::colPrefix() . \Ecommerce\Item\Offer::index(), $this->id);
130
        \App::$cur->db->cols = 'COALESCE(sum(' . \Ecommerce\Item\Offer\Warehouse::colPrefix() . 'count),0) as `sum` ';
131
        $warehouse = \App::$cur->db->select(\Ecommerce\Item\Offer\Warehouse::table())->fetch();
132
133
        \App::$cur->db->cols = 'COALESCE(sum(' . \Ecommerce\Warehouse\Block::colPrefix() . 'count) ,0) as `sum` ';
134
        \App::$cur->db->where(\Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Item\Offer::index(), $this->id);
135
        if ($cart_id) {
136
            \App::$cur->db->where(\Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Cart::index(), (int) $cart_id, '!=');
137
        }
138
        $on = '
139
            ' . \Ecommerce\Cart::index() . ' = ' . \Ecommerce\Warehouse\Block::colPrefix() . \Ecommerce\Cart::index() . ' AND (
140
            (`' . \Ecommerce\Cart::colPrefix() . 'warehouse_block` = 1 and `' . \Ecommerce\Cart::colPrefix() . 'cart_status_id` in(2,3,6)) || 
141
            (`' . \Ecommerce\Cart::colPrefix() . 'cart_status_id` in(0,1) and `' . \Ecommerce\Cart::colPrefix() . 'date_last_activ` >=subdate(now(),INTERVAL 30 MINUTE))
142
            )
143
        ';
144
        \App::$cur->db->join(\Ecommerce\Cart::table(), $on, 'inner');
145
146
147
        $blocked = \App::$cur->db->select(\Ecommerce\Warehouse\Block::table())->fetch();
148
        return (float) $warehouse['sum'] - (float) $blocked['sum'];
149
    }
150
151
    public function getPrice() {
152
        $curPrice = null;
153
        foreach ($this->prices as $price) {
154
            if (
155
                (!$curPrice && (!$price->type || !$price->type->roles)) ||
156
                ($price->type->roles && !$curPrice && strpos($price->type->roles, "|" . \Users\User::$cur->role_id . "|") !== false)
157
            ) {
158
                $curPrice = $price;
159
            }
160
        }
161
        return $curPrice;
162
    }
163
164
    public function beforeDelete() {
165
        if ($this->id) {
166
            if ($this->warehouses) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->warehouses of type Ecommerce\Item\Offer\Warehouse[] 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...
167
                foreach ($this->warehouses as $warehouse) {
168
                    $warehouse->delete();
169
                }
170
            }
171
            if ($this->prices) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->prices of type Ecommerce\Item\Offer\Price[] 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...
172
                foreach ($this->prices as $price) {
173
                    $price->delete();
174
                }
175
            }
176
            if ($this->bonuses) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->bonuses of type Ecommerce\Item\Offer\Bonus[] 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...
177
                foreach ($this->bonuses as $bonus) {
178
                    $bonus->delete();
179
                }
180
            }
181
        }
182
    }
183
184
}
185