Completed
Push — master ( a58902...8a2ff5 )
by Alexey
05:01
created

Cart::finalSum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

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