Completed
Push — master ( faf894...f88301 )
by Alexey
05:18
created

Item::indexes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
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->checkStage();
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
        \Ecommerce\Warehouse\Block::deleteList([['cart_id', $this->cart->id], ['item_offer_id', $this->price->item_offer_id]]);
79
        $this->cart->checkStage();
80
    }
81
82
    public function discount()
83
    {
84
        $discountSum = 0;
85
        if ($this->item->type && $this->item->type->discount) {
86
            foreach ($this->cart->discounts as $discount) {
87
                switch ($discount->type) {
88
                    case 'procent':
89
                        $discountSum += round($this->price->price * ($discount->amount / 100), 2);
90
                        break;
91
                    case 'amount':
92
                        $discountSum += round($this->price->price + $discount->amount, 2);
93
                        break;
94
                }
95
            }
96
        }
97
        return $discountSum;
98
    }
99
100
    public static $labels = [
101
        'item_id' => 'Товар',
102
        'item_offer_price_id' => 'Цена в каталоге',
103
        'count' => 'Количество',
104
        'cart_id' => 'Корзина',
105
        'final_price' => 'Итоговая цена за единицу',
106
    ];
107
    public static $cols = [
108
        //Основные параметры
109
        'cart_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'cart'],
110
        'count' => ['type' => 'decimal'],
111
        'item_offer_price_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'price', 'showCol' => 'price'],
112
        'item_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'item'],
113
        'final_price' => ['type' => 'decimal'],
114
        'discount' => ['type' => 'decimal'],
115
        //Системные
116
        'date_create' => ['type' => 'dateTime'],
117
    ];
118
119
    public static function indexes()
120
    {
121
        return [
122
            'ecommerce_cartItemCart' => [
123
                'type' => 'INDEX',
124
                'cols' => [
125
                    'cart_item_cart_id'
126
                ]
127
            ]
128
        ];
129
    }
130
131
    public static $dataManagers = [
132
        'manager' => [
133
            'name' => 'Покупки',
134
            'cols' => [
135
                'item_id',
136
                'item_offer_price_id',
137
                'price:currency' => ['label' => 'Валюта'],
138
                'count',
139
            ],
140
        ],
141
    ];
142
    public static $forms = [
143
        'manager' => [
144
            'relations' => [
145
                'item_id' => [
146
                    'col' => 'item_offer_price_id',
147
                    'model' => 'Item',
148
                    'relation' => 'prices'
149
                ]
150
            ],
151
            'map' => [
152
                ['cart_id', 'item_id', 'item_offer_price_id'],
153
                ['final_price', 'count'],
154
            ]
155
        ]
156
    ];
157
158
    public static function relations()
159
    {
160
        return [
161
            'item' => [
162
                'model' => 'Ecommerce\Item',
163
                'col' => 'item_id'
164
            ],
165
            'price' => [
166
                'model' => 'Ecommerce\Item\Offer\Price',
167
                'col' => 'item_offer_price_id'
168
            ],
169
            'cart' => [
170
                'model' => 'Ecommerce\Cart',
171
                'col' => 'cart_id'
172
            ]
173
        ];
174
    }
175
176
}
177