cea5c45d
Administrator
21.03.16 Versrka
|
1
2
3
4
|
<?php
namespace frontend\controllers;
|
2e3a817e
Administrator
24.03.16 finish 1
|
5
6
7
8
|
use common\models\Customers;
use common\models\OrderItems;
use common\models\Orders;
use common\modules\product\models\ProductVariant;
|
f7aa643c
Administrator
21.03.16 Versrka
|
9
|
use common\widgets\BasketModal;
|
cea5c45d
Administrator
21.03.16 Versrka
|
10
11
12
13
14
15
16
17
|
use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\data\ArrayDataProvider;
/**
* OrderController implements the CRUD actions for Order model.
*/
|
f7aa643c
Administrator
21.03.16 Versrka
|
18
|
class OrdersController extends Controller
|
cea5c45d
Administrator
21.03.16 Versrka
|
19
20
|
{
|
f7aa643c
Administrator
21.03.16 Versrka
|
21
22
23
24
25
26
27
28
29
30
31
32
|
/**
* @inheritdoc
*/
public function beforeAction($action)
{
if ($action->id == 'buy-items' || $action->id == 'delete') {
Yii::$app->controller->enableCsrfValidation = false;
}
return true;
}
|
cea5c45d
Administrator
21.03.16 Versrka
|
33
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
34
|
public function actionFirst(){
|
cea5c45d
Administrator
21.03.16 Versrka
|
35
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
36
|
$array = Yii::$app->session->get('order');
|
cea5c45d
Administrator
21.03.16 Versrka
|
37
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
38
39
40
41
42
|
if(isset($array['order_id']) ) {
$model = Orders::findOne($array['order_id']);
}else {
$model = new Orders();
}
|
cea5c45d
Administrator
21.03.16 Versrka
|
43
|
|
cea5c45d
Administrator
21.03.16 Versrka
|
44
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
45
|
if(Yii::$app->request->post() && $model->load(Yii::$app->request->post())){
|
cea5c45d
Administrator
21.03.16 Versrka
|
46
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
47
|
if($model->save()){
|
cea5c45d
Administrator
21.03.16 Versrka
|
48
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
49
|
$array['order_id'] = $model->order_id;
|
cea5c45d
Administrator
21.03.16 Versrka
|
50
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
51
|
Yii::$app->session->set('order', $array );
|
cea5c45d
Administrator
21.03.16 Versrka
|
52
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
53
54
|
return $this->redirect(['orders/second']);
}
|
cea5c45d
Administrator
21.03.16 Versrka
|
55
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
56
57
58
59
60
61
|
} else {
if (!Yii::$app->user->isGuest) {
$customer = Yii::$app->user->identity;
$model->name = $customer->name;
$model->email = $customer->email;
$model->phone = $customer->phone;
|
cea5c45d
Administrator
21.03.16 Versrka
|
62
|
}
|
cea5c45d
Administrator
21.03.16 Versrka
|
63
|
}
|
cea5c45d
Administrator
21.03.16 Versrka
|
64
|
|
cea5c45d
Administrator
21.03.16 Versrka
|
65
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
66
67
68
69
|
return $this->render('basket-step-01',[
'model' => $model
]);
}
|
cea5c45d
Administrator
21.03.16 Versrka
|
70
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
71
|
public function actionSecond(){
|
cea5c45d
Administrator
21.03.16 Versrka
|
72
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
73
|
$sessionData = \Yii::$app->session->get('order');
|
cea5c45d
Administrator
21.03.16 Versrka
|
74
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
75
|
$order_id = $sessionData['order_id'];
|
cea5c45d
Administrator
21.03.16 Versrka
|
76
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
77
78
79
80
|
if(!empty($order_id)){
$order_model = Orders::findOne($order_id);
} else{
$order_model = new Orders;
|
cea5c45d
Administrator
21.03.16 Versrka
|
81
82
|
}
|
2e3a817e
Administrator
24.03.16 finish 1
|
83
|
unset($sessionData['order_id']);
|
cea5c45d
Administrator
21.03.16 Versrka
|
84
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
$variant = ProductVariant::find()->where(['product_variant_id'=>array_keys($sessionData)])->indexBy('product_variant_id')->all();
if(Yii::$app->request->post()){
foreach ($sessionData as $k => $item) {
$itemModel = OrderItems::find()->where(['order_id'=>$order_id, 'item_id'=> $variant[$k]->product_variant_id])->one();
if($itemModel instanceof OrderItems){
$itemModel->order_id = $order_id;
$itemModel->item_id = $variant[$k]->product_variant_id;
$itemModel->item_count = $sessionData[$k]['num'];
$itemModel->price = $variant[$k]->price;
$itemModel->save();
} else {
$itemModel = new OrderItems();
$itemModel->order_id = $order_id;
$itemModel->item_id = $variant[$k]->product_variant_id;
$itemModel->item_count = $sessionData[$k]['num'];
$itemModel->price = $variant[$k]->price;
$itemModel->save();
}
|
cea5c45d
Administrator
21.03.16 Versrka
|
106
|
|
2e3a817e
Administrator
24.03.16 finish 1
|
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
}
Yii::$app->session->set('order', [] );
return $this->redirect(['orders/third']);
} else {
$price = 0;
$count = count($sessionData);
foreach ($sessionData as $k => $item) {
$sessionData[$k]['item'] = $variant[$k];
$price += $variant[$k]->price * $sessionData[$k]['num'];
}
}
return $this->render('basket-step-02',[
'items'=>$sessionData,
'count' => $count,
'price' => $price,
'order_id' => $order_id,
'order_model' => $order_model,
|
cea5c45d
Administrator
21.03.16 Versrka
|
130
|
]);
|
2e3a817e
Administrator
24.03.16 finish 1
|
131
|
|
cea5c45d
Administrator
21.03.16 Versrka
|
132
133
|
}
|
2e3a817e
Administrator
24.03.16 finish 1
|
134
135
136
137
138
139
140
141
142
143
144
145
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
|
public function actionThird(){
return $this->render('basket-step-03');
}
// /**
// * Lists all Order models.
// * @return mixed
// */
// public function actionIndex()
// {
//
// if (Yii::$app->request->post()) {
// $item = $items_id = array();
//
// $i = 0;
// $order = Yii::$app->request->post();
//
// $orderData['Order'] = $order['OrderForm'];
//
// foreach($order['OrderForm']['one_item'] as $k => $v ){
// $item[$k]['num'] = $v['num'];
// $items_id[] = $k;
// $i++;
// }
//
// $items = Items::find()->where(['id'=>$items_id])->all();
//
//
// $orderModel = new Order();
// $orderModel->load($orderData);
// $orderModel->save();
//
//
// foreach($items as $one_item){
// $ItemOrderModel = new ItemOrder();
// $ItemOrderModel->order_id = $orderModel->id;
// $ItemOrderModel->num = $item[$one_item->id]['num'];
// $ItemOrderModel->item_id = $one_item->id;
// $ItemOrderModel->price = $one_item->price * $item[$one_item->id]['num'];
// $ItemOrderModel->item_name = $one_item->name;
// $ItemOrderModel->save();
// }
// Yii::$app->session->set('order', [] );
// return $this->redirect(['order/complete']);
// }
// $total_price = 0;
//
// $items_id = [];
//
// $orders = Yii::$app->session->get('order');
//
// if(!empty($orders)){
// foreach($orders as $k => $v) {
// $items_id[] = $k;
// }
// }
//
//
// $items = Items::find()->where(['id'=>$items_id])->all();
//
// foreach($items as $item) {
// $total_price += $orders[$item['id']]['num'] * $item['price'];
// }
//
//
// $dataProvider = new ArrayDataProvider([
// 'allModels' => $items
// ]);
//
// return $this->render('index', [
// 'dataProvider' => $dataProvider,
// 'total_price'=> $total_price,
// 'model' => new OrderForm()
// ]);
// }
|
cea5c45d
Administrator
21.03.16 Versrka
|
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
public function actionComplete()
{
return $this->render('complete', [
]);
}
public function actionBuyItems(){
$data = Yii::$app->request->post();
$sessionData = Yii::$app->session->get('order');
if(isset($sessionData) && !array_search($data['id'],Yii::$app->session->get('order')) ){
$array = Yii::$app->session->get('order');
$array[$data['id']] = $data;
Yii::$app->session->set('order', $array );
} else {
$array[$data['id']] = $data;
Yii::$app->session->set('order', $array );
}
|
f7aa643c
Administrator
21.03.16 Versrka
|
229
|
echo BasketModal::widget([]);
|
cea5c45d
Administrator
21.03.16 Versrka
|
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
}
/**
* Displays a single Order model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Order model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Order();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Order model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Order model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete()
{
$data = Yii::$app->request->post();
$sessionData = Yii::$app->session->get('order');
unset($sessionData[$data['id']]);
Yii::$app->session->set('order', $sessionData);
return count(Yii::$app->session->get('order'));
}
/**
* Finds the Order model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Order the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Order::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
|