Completed
Push — master ( 8717df...fdbe7f )
by Alexey
05:44
created

Item   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 22
lcom 0
cbo 4
dl 0
loc 156
rs 10
c 3
b 1
f 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
B beforeSave() 0 24 5
D afterSave() 0 32 9
A afterDelete() 0 10 2
A discount() 0 7 4
A indexes() 0 11 1
A relations() 0 17 1
1
<?php
2
3
/**
4
 * Cart item
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\Cart;
13
14
class Item extends \Model
15
{
16
    public function beforeSave()
17
    {
18
        if (!$this->id) {
19
            $event = new Event(['cart_id' => $this->cart_id, 'user_id' => \Users\User::$cur->id, 'cart_event_type_id' => 1, 'info' => $this->item_offer_price_id]);
20
            $event->save();
21
        } else {
22
            $cur = Item::get($this->id);
23
            if ($cur->item_id != $this->item_id) {
24
                $event = new Event(['cart_id' => $this->cart->cart_id, 'user_id' => \Users\User::$cur->id, 'cart_event_type_id' => 2, 'info' => $cur->item_offer_price_id]);
25
                $event->save();
26
                $event = new Event(['cart_id' => $this->cart->cart_id, 'user_id' => \Users\User::$cur->id, 'cart_event_type_id' => 1, 'info' => $this->item_offer_price_id]);
27
                $event->save();
28
            } else {
29
                if ($cur->item_offer_price_id != $this->item_offer_price_id) {
30
                    $event = new Event(['cart_id' => $this->cart->cart_id, 'user_id' => \Users\User::$cur->id, 'cart_event_type_id' => 3, 'info' => $this->item_offer_price_id]);
31
                    $event->save();
32
                }
33
                if ($cur->count != $this->count) {
34
                    $event = new Event(['cart_id' => $this->cart->cart_id, 'user_id' => \Users\User::$cur->id, 'cart_event_type_id' => 4, 'info' => $this->item_offer_price_id . "|" . ($this->count - $cur->count)]);
35
                    $event->save();
36
                }
37
            }
38
        }
39
    }
40
41
    public function afterSave()
42
    {
43
        $block = \Ecommerce\Warehouse\Block::get([['cart_id', $this->cart->id], ['item_offer_id', $this->price->item_offer_id]]);
44
        if (in_array($this->cart_status_id, [0, 1, 2, 3, 6])) {
45
            if (in_array($this->cart_status_id, [0, 1])) {
46
                $cur = new \DateTime();
47
                $lastActive = new \DateTime($this->date_last_activ);
48
                $interval = $cur->diff($lastActive);
49
                if ($interval->days || $interval->h || $interval->i >= 30) {
50
                    if ($block) {
51
                        $block->delete();
52
                    }
53
                    $this->cart->save();
54
                    return;
55
                }
56
            }
57
            $block = \Ecommerce\Warehouse\Block::get([['cart_id', $this->cart_id], ['item_offer_id', $this->price->item_offer_id]]);
58
            if ($block) {
59
                $block->count = $this->count;
60
                $block->save();
61
            } else {
62
                $block = new \Ecommerce\Warehouse\Block();
63
                $block->item_offer_id = $this->price->item_offer_id;
64
                $block->cart_id = $this->cart_id;
65
                $block->count = $this->count;
66
                $block->save();
67
            }
68
        } elseif ($block) {
69
            $block->delete();
70
        }
71
        $this->cart->save();
72
    }
73
74
    public function afterDelete()
75
    {
76
        $event = new Event(['cart_id' => $this->cart_id, 'user_id' => \Users\User::$cur->id, 'cart_event_type_id' => 2, 'info' => $this->item_offer_price_id]);
77
        $event->save();
78
        $block = \Ecommerce\Warehouse\Block::get([['cart_id', $this->cart->id], ['item_offer_id', $this->price->item_offer_id]]);
79
        if ($block) {
80
            $block->delete();
81
        }
82
        $this->cart->save();
83
    }
84
85
    public function discount()
86
    {
87
        if ($this->cart->card && $this->price->offer->item->type && $this->price->offer->item->type->discount) {
88
            return round($this->price->price * $this->cart->card->level->discount->amount, 2);
89
        }
90
        return 0;
91
    }
92
93
    public static $labels = [
94
        'item_id' => 'Товар',
95
        'item_offer_price_id' => 'Цена в каталоге',
96
        'count' => 'Количество',
97
        'cart_id' => 'Корзина',
98
        'final_price' => 'Итоговая цена за единицу',
99
    ];
100
    public static $cols = [
101
        //Основные параметры
102
        'cart_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'cart'],
103
        'count' => ['type' => 'text'],
104
        'item_offer_price_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'price', 'showCol' => 'price'],
105
        'item_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'item'],
106
        'final_price' => ['type' => 'decimal'],
107
        'discount' => ['type' => 'decimal'],
108
        //Системные
109
        'date_create' => ['type' => 'dateTime'],
110
    ];
111
112
    public static function indexes()
113
    {
114
        return [
115
            'ecommerce_cartItemCart' => [
116
                'type' => 'INDEX',
117
                'cols' => [
118
                    'cart_item_cart_id'
119
                ]
120
            ]
121
        ];
122
    }
123
124
    public static $dataManagers = [
125
        'manager' => [
126
            'name' => 'Покупки',
127
            'cols' => [
128
                'item_id',
129
                'item_offer_price_id',
130
                'price:currency' => ['label' => 'Валюта'],
131
                'count',
132
            ],
133
        ],
134
    ];
135
    public static $forms = [
136
        'manager' => [
137
            'relations' => [
138
                'item_id' => [
139
                    'col' => 'item_offer_price_id',
140
                    'model' => 'Item',
141
                    'relation' => 'prices'
142
                ]
143
            ],
144
            'map' => [
145
                ['cart_id', 'item_id', 'item_offer_price_id'],
146
                ['final_price', 'count'],
147
            ]
148
        ]
149
    ];
150
151
    public static function relations()
152
    {
153
        return [
154
            'item' => [
155
                'model' => 'Ecommerce\Item',
156
                'col' => 'item_id'
157
            ],
158
            'price' => [
159
                'model' => 'Ecommerce\Item\Offer\Price',
160
                'col' => 'item_offer_price_id'
161
            ],
162
            'cart' => [
163
                'model' => 'Ecommerce\Cart',
164
                'col' => 'cart_id'
165
            ]
166
        ];
167
    }
168
169
}
170