|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\shop\models; |
|
4
|
|
|
|
|
5
|
|
|
use app\modules\core\helpers\EventTriggeringHelper; |
|
6
|
|
|
use app\modules\shop\events\OrderCalculateEvent; |
|
7
|
|
|
use app\modules\shop\helpers\PriceHelper; |
|
8
|
|
|
use app\modules\user\models\User; |
|
9
|
|
|
use app\properties\HasProperties; |
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\base\Exception; |
|
12
|
|
|
use yii\behaviors\TimestampBehavior; |
|
13
|
|
|
use yii\caching\TagDependency; |
|
14
|
|
|
use yii\data\ActiveDataProvider; |
|
15
|
|
|
use yii\db\Expression; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* This is the model class for table "{{%order}}". |
|
19
|
|
|
* Model fields: |
|
20
|
|
|
* @property integer $id |
|
21
|
|
|
* @property integer $user_id |
|
22
|
|
|
* @property integer $customer_id |
|
23
|
|
|
* @property integer $contragent_id |
|
24
|
|
|
* @property integer $manager_id |
|
25
|
|
|
* @property string $start_date |
|
26
|
|
|
* @property string $update_date |
|
27
|
|
|
* @property string $end_date |
|
28
|
|
|
* @property integer $cart_forming_time |
|
29
|
|
|
* @property integer $order_stage_id |
|
30
|
|
|
* @property integer $payment_type_id |
|
31
|
|
|
* @property integer $assigned_id |
|
32
|
|
|
* @property integer $tax_id |
|
33
|
|
|
* @property string $external_id |
|
34
|
|
|
* @property integer $items_count |
|
35
|
|
|
* @property double $total_price |
|
36
|
|
|
* @property double $total_payed |
|
37
|
|
|
* @property string $hash |
|
38
|
|
|
* @property bool $is_deleted |
|
39
|
|
|
* @property bool $temporary |
|
40
|
|
|
* @property bool $show_price_changed_notification |
|
41
|
|
|
* @property bool $in_cart |
|
42
|
|
|
* Relations: |
|
43
|
|
|
* @property \app\properties\AbstractModel $abstractModel |
|
44
|
|
|
* @property OrderItem[] $items |
|
45
|
|
|
* @property SpecialPriceObject[] $specialPriceObjects |
|
46
|
|
|
* @property ShippingOption $shippingOption |
|
47
|
|
|
* @property OrderStage $stage |
|
48
|
|
|
* @property PaymentType $paymentType |
|
49
|
|
|
* @property OrderTransaction[] $transactions |
|
50
|
|
|
* @property User $user |
|
51
|
|
|
* @property User $manager |
|
52
|
|
|
* @property float $fullPrice |
|
53
|
|
|
* @property Contragent $contragent |
|
54
|
|
|
* @property DiscountCode $code |
|
55
|
|
|
* @property Customer $customer |
|
56
|
|
|
* @property OrderDeliveryInformation $orderDeliveryInformation |
|
57
|
|
|
*/ |
|
58
|
|
|
class Order extends \yii\db\ActiveRecord |
|
59
|
|
|
{ |
|
60
|
|
|
const IMMUTABLE_NONE = 0; |
|
61
|
|
|
const IMMUTABLE_USER = 1; |
|
62
|
|
|
const IMMUTABLE_MANAGER = 2; |
|
63
|
|
|
const IMMUTABLE_ASSIGNED = 4; |
|
64
|
|
|
const IMMUTABLE_ALL = 128; |
|
65
|
|
|
const ORDER_STATE_FINISH = 0; |
|
66
|
|
|
const ORDER_STATE_IN_PROCESS = 1; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var Order $order |
|
70
|
|
|
*/ |
|
71
|
|
|
protected static $order; |
|
72
|
|
|
/** @var OrderStage $orderStage */ |
|
73
|
|
|
protected $orderStage = null; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @inheritdoc |
|
77
|
|
|
*/ |
|
78
|
|
|
public function behaviors() |
|
79
|
|
|
{ |
|
80
|
|
|
return [ |
|
81
|
|
|
[ |
|
82
|
|
|
'class' => HasProperties::className(), |
|
83
|
|
|
], |
|
84
|
|
|
[ |
|
85
|
|
|
'class' => \devgroup\TagDependencyHelper\ActiveRecordHelper::className(), |
|
86
|
|
|
], |
|
87
|
|
|
[ |
|
88
|
|
|
'class' => TimestampBehavior::className(), |
|
89
|
|
|
'createdAtAttribute' => 'start_date', |
|
90
|
|
|
'updatedAtAttribute' => 'update_date', |
|
91
|
|
|
'value' => new Expression('NOW()'), |
|
92
|
|
|
], |
|
93
|
|
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @inheritdoc |
|
98
|
|
|
*/ |
|
99
|
|
|
public static function tableName() |
|
100
|
|
|
{ |
|
101
|
|
|
return '{{%order}}'; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @inheritdoc |
|
106
|
|
|
*/ |
|
107
|
|
|
public function rules() |
|
108
|
|
|
{ |
|
109
|
|
|
return [ |
|
110
|
|
|
[ |
|
111
|
|
|
[ |
|
112
|
|
|
'user_id', |
|
113
|
|
|
'customer_id', |
|
114
|
|
|
'contragent_id', |
|
115
|
|
|
'order_stage_id', |
|
116
|
|
|
'total_price', |
|
117
|
|
|
'payment_type_id', |
|
118
|
|
|
'in_cart', |
|
119
|
|
|
], |
|
120
|
|
|
'required' |
|
121
|
|
|
], |
|
122
|
|
|
[ |
|
123
|
|
|
[ |
|
124
|
|
|
'user_id', |
|
125
|
|
|
'customer_id', |
|
126
|
|
|
'contragent_id', |
|
127
|
|
|
'order_stage_id', |
|
128
|
|
|
'payment_type_id', |
|
129
|
|
|
'assigned_id', |
|
130
|
|
|
'tax_id' |
|
131
|
|
|
], |
|
132
|
|
|
'integer' |
|
133
|
|
|
], |
|
134
|
|
|
[['start_date', 'end_date', 'update_date'], 'safe'], |
|
135
|
|
|
[['total_price', 'items_count', 'total_payed'], 'number'], |
|
136
|
|
|
[['external_id'], 'string', 'max' => 38], |
|
137
|
|
|
[['is_deleted', 'temporary', 'show_price_changed_notification', 'in_cart'], 'boolean'], |
|
138
|
|
|
[['user_id', 'customer_id', 'contragent_id', 'in_cart'], 'default', 'value' => 0], |
|
139
|
|
|
[['temporary'], 'default', 'value' => 1], |
|
140
|
|
|
]; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @inheritdoc |
|
145
|
|
|
*/ |
|
146
|
|
|
public function scenarios() |
|
147
|
|
|
{ |
|
148
|
|
|
return [ |
|
149
|
|
|
'default' => [ |
|
150
|
|
|
'user_id', |
|
151
|
|
|
'cart_forming_time', |
|
152
|
|
|
'order_stage_id', |
|
153
|
|
|
'items_count', |
|
154
|
|
|
'total_price', |
|
155
|
|
|
'hash', |
|
156
|
|
|
'in_cart', |
|
157
|
|
|
], |
|
158
|
|
|
'search' => [ |
|
159
|
|
|
'id', |
|
160
|
|
|
'user_id', |
|
161
|
|
|
'manager_id', |
|
162
|
|
|
'start_date', |
|
163
|
|
|
'end_date', |
|
164
|
|
|
'order_stage_id', |
|
165
|
|
|
'payment_type_id', |
|
166
|
|
|
'items_count', |
|
167
|
|
|
'total_price', |
|
168
|
|
|
'hash', |
|
169
|
|
|
], |
|
170
|
|
|
'shippingOption' => ['order_stage_id'], |
|
171
|
|
|
'paymentType' => ['payment_type_id', 'order_stage_id'], |
|
172
|
|
|
'changeManager' => ['manager_id'], |
|
173
|
|
|
'backend' => [ |
|
174
|
|
|
'user_id', |
|
175
|
|
|
'customer_id', |
|
176
|
|
|
'contragent_id', |
|
177
|
|
|
], |
|
178
|
|
|
]; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @inheritdoc |
|
183
|
|
|
*/ |
|
184
|
|
|
public function attributeLabels() |
|
185
|
|
|
{ |
|
186
|
|
|
return [ |
|
187
|
|
|
'id' => Yii::t('app', 'ID'), |
|
188
|
|
|
'user_id' => Yii::t('app', 'User'), |
|
189
|
|
|
'customer_id' => Yii::t('app', 'Customer'), |
|
190
|
|
|
'contragent_id' => Yii::t('app', 'Contragent'), |
|
191
|
|
|
'manager_id' => Yii::t('app', 'Manager'), |
|
192
|
|
|
'start_date' => Yii::t('app', 'Start Date'), |
|
193
|
|
|
'update_date' => Yii::t('app', 'Update date'), |
|
194
|
|
|
'end_date' => Yii::t('app', 'End Date'), |
|
195
|
|
|
'cart_forming_time' => Yii::t('app', 'Cart Forming Time'), |
|
196
|
|
|
'order_stage_id' => Yii::t('app', 'Stage'), |
|
197
|
|
|
'payment_type_id' => Yii::t('app', 'Payment Type'), |
|
198
|
|
|
'assigned_id' => Yii::t('app', 'Assigned'), |
|
199
|
|
|
'tax_id' => Yii::t('app', 'Tax'), |
|
200
|
|
|
'external_id' => Yii::t('app', 'External ID'), |
|
201
|
|
|
'items_count' => Yii::t('app', 'Items Count'), |
|
202
|
|
|
'total_price' => Yii::t('app', 'Total Price'), |
|
203
|
|
|
'total_payed' => Yii::t('app', 'Total payed'), |
|
204
|
|
|
'hash' => Yii::t('app', 'Hash'), |
|
205
|
|
|
'is_deleted' => Yii::t('app', 'Is deleted'), |
|
206
|
|
|
'temporary' => Yii::t('app', 'Temporary'), |
|
207
|
|
|
'show_price_changed_notification' => Yii::t('app', 'Show price changed notification'), |
|
208
|
|
|
'in_cart' => Yii::t('app', 'In Cart'), |
|
209
|
|
|
]; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function getItems() |
|
|
|
|
|
|
213
|
|
|
{ |
|
214
|
|
|
return $this->hasMany(OrderItem::className(), ['order_id' => 'id']); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @return OrderStage|null |
|
219
|
|
|
*/ |
|
220
|
|
|
public function getStage() |
|
221
|
|
|
{ |
|
222
|
|
|
if (null === $this->orderStage) { |
|
223
|
|
|
$this->orderStage = $this->hasOne(OrderStage::className(), ['id' => 'order_stage_id']); |
|
224
|
|
|
} |
|
225
|
|
|
return $this->orderStage; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
public function getShippingOption() |
|
229
|
|
|
{ |
|
230
|
|
|
$orderDelivery = OrderDeliveryInformation::getByOrderId($this->id); |
|
231
|
|
|
return empty($orderDelivery) ? null : $orderDelivery->shippingOption; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
public function getPaymentType() |
|
|
|
|
|
|
235
|
|
|
{ |
|
236
|
|
|
return $this->hasOne(PaymentType::className(), ['id' => 'payment_type_id']); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
public function getTransactions() |
|
|
|
|
|
|
240
|
|
|
{ |
|
241
|
|
|
return $this->hasMany(OrderTransaction::className(), ['order_id' => 'id']); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
public function getUser() |
|
|
|
|
|
|
245
|
|
|
{ |
|
246
|
|
|
return $this->hasOne(User::className(), ['id' => 'user_id']); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
public function getCode() |
|
|
|
|
|
|
250
|
|
|
{ |
|
251
|
|
|
return $this->hasOne(OrderCode::className(), ['order_id'=>'id']); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* @return Customer|null |
|
256
|
|
|
*/ |
|
257
|
|
|
public function getCustomer() |
|
258
|
|
|
{ |
|
259
|
|
|
return $this->hasOne(Customer::className(), ['id' => 'customer_id']); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
public function getContragent() |
|
|
|
|
|
|
263
|
|
|
{ |
|
264
|
|
|
return $this->hasOne(Contragent::className(), ['id' => 'contragent_id']); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
public function getManager() |
|
|
|
|
|
|
268
|
|
|
{ |
|
269
|
|
|
return $this->hasOne(User::className(), ['id' => 'manager_id']); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
public function getSpecialPriceObjects() |
|
|
|
|
|
|
273
|
|
|
{ |
|
274
|
|
|
return SpecialPriceObject::find() |
|
275
|
|
|
->leftJoin( |
|
276
|
|
|
SpecialPriceList::tableName(), |
|
277
|
|
|
SpecialPriceList::tableName() . '.id =' . SpecialPriceObject::tableName() . '.special_price_list_id' |
|
278
|
|
|
) |
|
279
|
|
|
->where( |
|
280
|
|
|
[ |
|
281
|
|
|
SpecialPriceObject::tableName() . '.object_model_id' => $this->id, |
|
282
|
|
|
SpecialPriceList::tableName() . '.object_id' => $this->object->id |
|
283
|
|
|
] |
|
284
|
|
|
) |
|
285
|
|
|
->all(); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
public function getFullPrice() |
|
289
|
|
|
{ |
|
290
|
|
|
$fullPrice = $this->total_price; |
|
291
|
|
|
if (!is_null($this->shippingOption)) { |
|
292
|
|
|
$fullPrice += $this->shippingOption->cost; |
|
293
|
|
|
} |
|
294
|
|
|
return $fullPrice; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* @return OrderDeliveryInformation|null |
|
299
|
|
|
*/ |
|
300
|
|
|
public function getOrderDeliveryInformation() |
|
301
|
|
|
{ |
|
302
|
|
|
return $this->hasOne(OrderDeliveryInformation::className(), ['order_id' => 'id']); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Первое удаление в корзину, второе из БД |
|
307
|
|
|
* |
|
308
|
|
|
* @return bool |
|
309
|
|
|
*/ |
|
310
|
|
|
public function beforeDelete() |
|
311
|
|
|
{ |
|
312
|
|
|
if (Yii::$app->getModule('shop')->deleteOrdersAbility === 0) { |
|
313
|
|
|
return false; |
|
314
|
|
|
} |
|
315
|
|
|
if (!parent::beforeDelete()) { |
|
316
|
|
|
return false; |
|
317
|
|
|
} |
|
318
|
|
|
if (0 === intval($this->is_deleted)) { |
|
319
|
|
|
$this->is_deleted = 1; |
|
320
|
|
|
$this->save(); |
|
321
|
|
|
return false; |
|
322
|
|
|
} |
|
323
|
|
|
$customer = $this->getCustomer(); |
|
324
|
|
|
if (0 === intval($customer->user_id)) { |
|
325
|
|
|
$customer->delete(); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
return true; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Search tasks |
|
333
|
|
|
* @param $params |
|
334
|
|
|
* @return ActiveDataProvider |
|
335
|
|
|
*/ |
|
336
|
|
View Code Duplication |
public function search($params) |
|
337
|
|
|
{ |
|
338
|
|
|
/** @var $query \yii\db\ActiveQuery */ |
|
339
|
|
|
$query = self::find(); |
|
340
|
|
|
|
|
341
|
|
|
$dataProvider = new ActiveDataProvider( |
|
342
|
|
|
[ |
|
343
|
|
|
'query' => $query, |
|
344
|
|
|
'pagination' => [ |
|
345
|
|
|
'pageSize' => 10, |
|
346
|
|
|
], |
|
347
|
|
|
] |
|
348
|
|
|
); |
|
349
|
|
|
if (!($this->load($params))) { |
|
350
|
|
|
return $dataProvider; |
|
351
|
|
|
} |
|
352
|
|
|
$query->andFilterWhere(['id' => $this->id]); |
|
353
|
|
|
$query->andFilterWhere(['is_deleted' => $this->is_deleted]); |
|
354
|
|
|
return $dataProvider; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Create a new order. |
|
359
|
|
|
* @param bool $throwException Throw an exception if a order has not been saved |
|
360
|
|
|
* @param bool $assignToUser Assign to a current user |
|
361
|
|
|
* @return Order |
|
|
|
|
|
|
362
|
|
|
* @throws Exception |
|
363
|
|
|
*/ |
|
364
|
|
|
public static function create($throwException = true, $assignToUser = true, $dummyObject = false) |
|
365
|
|
|
{ |
|
366
|
|
|
TagDependency::invalidate(Yii::$app->cache, ['Session:' . Yii::$app->session->id]); |
|
367
|
|
|
$initialOrderStage = OrderStage::getInitialStage(); |
|
368
|
|
|
if (is_null($initialOrderStage)) { |
|
369
|
|
|
throw new Exception('Initial order stage not found'); |
|
370
|
|
|
} |
|
371
|
|
|
$model = new static; |
|
372
|
|
|
$model->loadDefaultValues(false); |
|
373
|
|
|
$model->user_id = !Yii::$app->user->isGuest && $assignToUser ? Yii::$app->user->id : 0; |
|
374
|
|
|
$model->order_stage_id = $initialOrderStage->id; |
|
375
|
|
|
$model->in_cart = 1; |
|
376
|
|
|
$model->customer_id = 0; |
|
377
|
|
|
$model->contragent_id = 0; |
|
378
|
|
|
mt_srand(); |
|
379
|
|
|
$model->hash = md5(mt_rand() . uniqid()); |
|
380
|
|
|
if (false === $dummyObject) { |
|
381
|
|
|
if (!$model->save()) { |
|
382
|
|
|
if ($throwException) { |
|
383
|
|
|
throw new Exception('Cannot create a new order.'); |
|
384
|
|
|
} else { |
|
385
|
|
|
return null; |
|
386
|
|
|
} |
|
387
|
|
|
} |
|
388
|
|
|
} |
|
389
|
|
|
return $model; |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
/** |
|
393
|
|
|
* Get current order. |
|
394
|
|
|
* @param bool $create Create order if it does not exist |
|
395
|
|
|
* @return Order |
|
396
|
|
|
* @throws Exception |
|
397
|
|
|
*/ |
|
398
|
|
|
public static function getOrder($create = false) |
|
399
|
|
|
{ |
|
400
|
|
|
Yii::beginProfile("GetOrder"); |
|
401
|
|
|
if (is_null(self::$order) && Yii::$app->session->has('orderId')) { |
|
402
|
|
|
self::$order = self::find() |
|
403
|
|
|
->where(['id' => Yii::$app->session->get('orderId')]) |
|
404
|
|
|
->one(); |
|
405
|
|
|
} |
|
406
|
|
|
if (is_null(self::$order) && !Yii::$app->user->isGuest) { |
|
407
|
|
|
self::$order = self::find() |
|
408
|
|
|
->where(['user_id' => Yii::$app->user->id, 'in_cart' => 1]) |
|
409
|
|
|
->orderBy(['start_date' => SORT_DESC, 'id' => SORT_DESC]) |
|
410
|
|
|
->one(); |
|
411
|
|
|
} |
|
412
|
|
|
if ((is_null(self::$order) || is_null(self::$order->stage) || self::$order->in_cart == 0) |
|
413
|
|
|
&& $create === true |
|
414
|
|
|
) { |
|
415
|
|
|
$model = self::create(); |
|
416
|
|
|
self::$order = $model; |
|
417
|
|
|
Yii::$app->session->set('orderId', $model->id); |
|
418
|
|
|
|
|
419
|
|
|
$sessionOrders = Yii::$app->session->get('orders', []); |
|
420
|
|
|
$sessionOrders[] = $model->id; |
|
421
|
|
|
Yii::$app->session->set('orders', $sessionOrders); |
|
422
|
|
|
|
|
423
|
|
|
} |
|
424
|
|
|
Yii::endProfile("GetOrder"); |
|
425
|
|
|
return self::$order; |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
/** |
|
429
|
|
|
* |
|
430
|
|
|
*/ |
|
431
|
|
|
public static function clearStaticOrder() |
|
432
|
|
|
{ |
|
433
|
|
|
self::$order = null; |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
/** |
|
437
|
|
|
* Calculate order total price and items count with all additional markups. |
|
438
|
|
|
* @param bool $callSave Call save method after calculating. |
|
439
|
|
|
* @param bool $deleteNotActiveProducts Delete Order Item if product is not active or is not exist. |
|
440
|
|
|
* @return bool |
|
441
|
|
|
*/ |
|
442
|
|
|
public function calculate($callSave = false, $deleteNotActiveProducts = true) |
|
443
|
|
|
{ |
|
444
|
|
|
$itemsCount = 0; |
|
445
|
|
|
foreach ($this->items as $item) { |
|
446
|
|
|
if (null === OrderItem::findOne(['id' => $item->id])) { |
|
447
|
|
|
$item->delete(); |
|
448
|
|
|
continue; |
|
449
|
|
|
} |
|
450
|
|
|
if ($deleteNotActiveProducts && (null === $item->product || $item->product->active == 0)) { |
|
451
|
|
|
$item->delete(); |
|
452
|
|
|
continue; |
|
453
|
|
|
} |
|
454
|
|
|
if (Yii::$app->getModule('shop')->countChildrenProducts == 1) { |
|
455
|
|
|
$itemsCount += Yii::$app->getModule('shop')->countUniqueProductsOnly == 1 ? 1 : $item->quantity; |
|
456
|
|
|
} else { |
|
457
|
|
|
if ($item->parent_id == 0) { |
|
458
|
|
|
$itemsCount += Yii::$app->getModule('shop')->countUniqueProductsOnly == 1 ? 1 : $item->quantity; |
|
459
|
|
|
} |
|
460
|
|
|
} |
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
$event = new OrderCalculateEvent(); |
|
464
|
|
|
$event->order = $this; |
|
465
|
|
|
$event->price = PriceHelper::getOrderPrice($this, SpecialPriceList::TYPE_CORE); |
|
466
|
|
|
EventTriggeringHelper::triggerSpecialEvent($event); |
|
467
|
|
|
|
|
468
|
|
|
$this->items_count = $itemsCount; |
|
469
|
|
|
$this->total_price = PriceHelper::getOrderPrice($this); |
|
470
|
|
|
|
|
471
|
|
|
$event->state = OrderCalculateEvent::AFTER_CALCULATE; |
|
472
|
|
|
EventTriggeringHelper::triggerSpecialEvent($event); |
|
473
|
|
|
|
|
474
|
|
|
TagDependency::invalidate(Yii::$app->cache, ['Session:' . Yii::$app->session->id]); |
|
475
|
|
|
return $callSave ? $this->save(true, ['items_count', 'total_price', 'total_price_with_shipping']) : true; |
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
|
|
/** |
|
479
|
|
|
* @inheritdoc |
|
480
|
|
|
*/ |
|
481
|
|
|
public function beforeSave($insert) |
|
|
|
|
|
|
482
|
|
|
{ |
|
483
|
|
|
$this->calculate(); |
|
484
|
|
|
return parent::beforeSave($insert); |
|
485
|
|
|
} |
|
486
|
|
|
|
|
487
|
|
|
/** |
|
488
|
|
|
* @inheritdoc |
|
489
|
|
|
*/ |
|
490
|
|
|
public function afterSave($insert, $changedAttributes) |
|
491
|
|
|
{ |
|
492
|
|
|
TagDependency::invalidate(Yii::$app->cache, ['Session:' . Yii::$app->session->id]); |
|
493
|
|
|
parent::afterSave($insert, $changedAttributes); |
|
494
|
|
|
|
|
495
|
|
|
if (!$insert && !empty($changedAttributes['user_id']) && 0 === intval($changedAttributes['user_id'])) { |
|
496
|
|
|
if (!empty($this->customer)) { |
|
497
|
|
|
$customer = $this->customer; |
|
498
|
|
|
$customer->user_id = 0 === intval($customer->user_id) ? $this->user_id : $customer->user_id; |
|
499
|
|
|
$customer->save(); |
|
500
|
|
|
} |
|
501
|
|
|
} |
|
502
|
|
|
} |
|
503
|
|
|
|
|
504
|
|
|
public function afterDelete() |
|
|
|
|
|
|
505
|
|
|
{ |
|
506
|
|
|
self::deleteOrderElements($this); |
|
507
|
|
|
return parent::afterDelete(); |
|
508
|
|
|
} |
|
509
|
|
|
|
|
510
|
|
|
public static function deleteOrderElements(Order $order) |
|
511
|
|
|
{ |
|
512
|
|
|
foreach ($order->items as $item) { |
|
513
|
|
|
$item->delete(); |
|
514
|
|
|
} |
|
515
|
|
|
if ($order->code !== null) { |
|
516
|
|
|
$order->code->delete(); |
|
517
|
|
|
} |
|
518
|
|
|
SpecialPriceObject::deleteAllByObject($order); |
|
519
|
|
|
} |
|
520
|
|
|
|
|
521
|
|
|
/** |
|
522
|
|
|
* @param integer|null $checkWith |
|
523
|
|
|
* @return int |
|
|
|
|
|
|
524
|
|
|
*/ |
|
525
|
|
|
public function getImmutability($checkWith = null) |
|
526
|
|
|
{ |
|
527
|
|
|
$stage = $this->stage; |
|
528
|
|
|
$checkWith = intval($checkWith); |
|
529
|
|
|
$flag = intval($stage->immutable_by_user) |
|
530
|
|
|
| (intval($stage->immutable_by_manager) << 1) |
|
531
|
|
|
| (intval($stage->immutable_by_assigned) << 2); |
|
532
|
|
|
return $checkWith > 0 ? $checkWith === ($checkWith & $flag) : $flag; |
|
533
|
|
|
} |
|
534
|
|
|
|
|
535
|
|
|
/** |
|
536
|
|
|
* @return int |
|
537
|
|
|
*/ |
|
538
|
|
|
public function getOrderState() |
|
539
|
|
|
{ |
|
540
|
|
|
$stage = $this->stage; |
|
541
|
|
|
return 1 === ($stage->immutable_by_user & $stage->immutable_by_manager & $stage->immutable_by_assigned) |
|
542
|
|
|
? Order::ORDER_STATE_FINISH |
|
|
|
|
|
|
543
|
|
|
: Order::ORDER_STATE_IN_PROCESS; |
|
|
|
|
|
|
544
|
|
|
} |
|
545
|
|
|
} |
|
546
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.