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