8a7e6ecf
Yarik
Namespaces
|
1
2
3
4
5
|
<?php
namespace artweb\artbox\ecommerce\models;
use artweb\artbox\models\Customer;
use Yii;
|
2b29877e
Alexey Boroda
-Added time colum...
|
6
|
use yii\behaviors\TimestampBehavior;
|
8a7e6ecf
Yarik
Namespaces
|
7
8
9
10
11
12
13
14
15
|
use yii\db\ActiveRecord;
use yii\db\Expression;
use yii\web\Session;
/**
* Class Order
*
* @todo Write docs and refactor
* @package artweb\artbox\ecommerce\models
|
2b29877e
Alexey Boroda
-Added time colum...
|
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
* @property integer $created_at
* @property integer $updated_at
* @property integer $deleted_at
* @property int $id
* @property int $user_id
* @property string $name
* @property string $phone
* @property string $phone2
* @property string $email
* @property string $adress
* @property string $body
* @property double $total
* @property string $date_time
* @property string $date_dedline
* @property string $reserve
* @property string $status
* @property string $comment
* @property int $label
* @property int $pay
* @property int $numbercard
* @property int $delivery
* @property string $declaration
* @property string $stock
* @property string $consignment
* @property string $payment
* @property string $insurance
* @property double $amount_imposed
* @property string $shipping_by
* @property string $city
|
25b21043
Administrator
add create_item t...
|
45
|
* @property string $deliveryString
|
8a7e6ecf
Yarik
Namespaces
|
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
*/
class Order extends ActiveRecord
{
const SCENARIO_QUICK = 'quick';
private $data;
public static function tableName()
{
return 'order';
}
public function scenarios()
{
$scenarios = array_merge(
parent::scenarios(),
[
self::SCENARIO_QUICK => [ 'phone' ],
]
);
return $scenarios;
}
|
2b29877e
Alexey Boroda
-Added time colum...
|
70
71
72
73
74
75
76
77
78
|
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
],
];
}
|
8a7e6ecf
Yarik
Namespaces
|
79
80
81
82
83
|
public function rules()
{
return [
[
[
|
2b29877e
Alexey Boroda
-Added time colum...
|
84
85
86
87
88
89
90
91
|
'created_at',
'updated_at',
'deleted_at',
],
'integer',
],
[
[
|
8a7e6ecf
Yarik
Namespaces
|
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
'phone',
],
'required',
],
[
[ 'comment' ],
'safe',
],
[
[ 'email' ],
'email',
],
[
[ 'phone' ],
'match',
'pattern' => '/^\+38\(\d{3}\)\d{3}-\d{2}-\d{2}$/',
'on' => self::SCENARIO_QUICK,
],
[
[
'name',
'phone2',
'numbercard',
'body',
'declaration',
'stock',
'consignment',
|
8a7e6ecf
Yarik
Namespaces
|
119
120
121
122
123
124
125
126
127
128
129
|
'insurance',
'amount_imposed',
'shipping_by',
'city',
'adress',
'total',
'status',
],
'string',
'max' => 255,
],
|
8a7e6ecf
Yarik
Namespaces
|
130
131
132
133
134
135
|
];
}
public function attributeLabels()
{
return [
|
2b29877e
Alexey Boroda
-Added time colum...
|
136
137
138
139
140
141
142
|
'name' => Yii::t('app', 'order_name'),
'phone' => Yii::t('app', 'order_phone'),
'email' => Yii::t('app', 'order_email'),
'comment' => Yii::t('app', 'order_comment'),
'created_at' => Yii::t('app', 'Дата добавления'),
'updated_at' => Yii::t('app', 'Дата обновления'),
'deleted_at' => Yii::t('app', 'Дата удаления'),
|
8a7e6ecf
Yarik
Namespaces
|
143
144
|
];
}
|
25b21043
Administrator
add create_item t...
|
145
|
|
8a7e6ecf
Yarik
Namespaces
|
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
public function addBasket($product_variant_id, $count)
{
$session = new Session;
$session->open();
$data = $session[ 'basket' ];
$i = 0;
if (isset( $session[ 'basket' ] )) {
foreach ($session[ 'basket' ] as $key => $basket) {
if ($product_variant_id == $basket[ 'id' ]) {
$data[ $key ][ 'count' ] += $count;
$session[ 'basket' ] = $data;
$i++;
}
}
}
if ($i == 0) {
$data[] = [
'id' => $product_variant_id,
'count' => $count,
];
$session[ 'basket' ] = $data;
}
}
public function rowBasket()
{
$session = new Session;
$session->open();
$cost = 0;
$count = 0;
if (isset( $session[ 'basket' ] ) && count($session[ 'basket' ])) {
foreach ($session[ 'basket' ] as $product) {
$count += $product[ 'count' ];
}
}
return (object) [
'cost' => $cost,
'count' => $count,
];
}
public function deleteBasketMod($id)
{
$session = new Session;
$session->open();
$basket = $session[ 'basket' ];
foreach ($basket as $key => $product) {
if ($id == $product[ 'id' ]) {
unset( $basket[ $key ] );
}
}
$session[ 'basket' ] = $basket;
}
public function updateBasket($row)
{
$session = new Session;
$session->open();
//$data = array();
if ($row[ 'count' ] > 0) {
$this->data[] = [
'id' => $row[ 'id' ],
'count' => $row[ 'count' ],
];
}
$session[ 'basket' ] = $this->data;
}
|
25b21043
Administrator
add create_item t...
|
214
215
216
217
218
|
/**
* @return array
*/
|
8a7e6ecf
Yarik
Namespaces
|
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
public function getBasketMods()
{
$session = new Session;
$session->open();
$products = [];
if (empty( $session[ 'basket' ] )) {
return [];
}
foreach ($session[ 'basket' ] as $product) {
$row = ProductVariant::find()
->select(
[
'product_variant.*',
'product.name as productName',
'product.alias',
]
)
->where([ 'product_variant.id' => $product[ 'id' ] ])
->leftJoin('product', 'product.id = product_variant.product_id')
->one();
$row->count = $product[ 'count' ];
$row->sum_cost = $product[ 'count' ] * $row->price;
$products[] = $row;
}
return $products;
}
public function getSumCost()
{
$session = new Session;
$session->open();
$cost = 0;
if (empty( $session[ 'basket' ] )) {
return false;
}
foreach ($session[ 'basket' ] as $product) {
$cost += ( $this->getModCost($product[ 'id' ]) * $product[ 'count' ] );
}
return $cost;
}
private function getModCost($product_variant_id)
{
/**
* @var ProductVariant $mod
*/
$mod = ProductVariant::find()
->where([ 'id' => $product_variant_id ])
->one();
return $mod->price;
}
|
25b21043
Administrator
add create_item t...
|
273
274
|
|
8a7e6ecf
Yarik
Namespaces
|
275
276
277
278
|
public function clearBasket()
{
$session = new Session;
$session->open();
|
7520dc0e
Alexey Boroda
-Grid with input ...
|
279
|
$session[ 'basket' ] = NULL;
|
8a7e6ecf
Yarik
Namespaces
|
280
|
}
|
25b21043
Administrator
add create_item t...
|
281
282
283
284
285
|
/**
* @return \yii\db\ActiveQuery
*/
|
8a7e6ecf
Yarik
Namespaces
|
286
287
288
289
|
public function getUser()
{
return $this->hasOne(Customer::className(), [ 'id' => 'user_id' ]);
}
|
25b21043
Administrator
add create_item t...
|
290
291
292
293
|
/**
* @return \yii\db\ActiveQuery
*/
|
8a7e6ecf
Yarik
Namespaces
|
294
295
296
297
|
public function getProducts()
{
return $this->hasMany(OrderProduct::className(), [ 'order_id' => 'id' ]);
}
|
25b21043
Administrator
add create_item t...
|
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/**
* @return \yii\db\ActiveQuery
*/
public function getOrderDelivery(){
return $this->hasOne(Delivery::className(), ['id'=> 'delivery']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getOrderLabel(){
return $this->hasOne(Label::className(),['id'=> 'status']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getOrderPayment(){
return $this->hasOne(OrderPayment::className(),['id'=> 'payment']);
}
/**
* @return string
*/
public function getDeliveryString(){
if(!empty($this->deliveryModel)){
if(!empty($this->deliveryModel->parent)){
return $this->deliveryModel->parent->lang->title .': '.$this->deliveryModel->lang->title;
} else {
return $this->deliveryModel->lang->title;
}
} else {
return '';
}
}
|
8a7e6ecf
Yarik
Namespaces
|
340
|
}
|