Blame view

frontend/controllers/OrdersController.php 10.8 KB
888851f9   Administrator   16.05.16 ann foto...
1
2
3
4
5
6
7
  <?php

  

  namespace frontend\controllers;

  

  use common\models\Customers;

  use common\models\OrderItems;

  use common\models\Orders;

a4e099fb   Administrator   16.05.16 ann foto...
8
  use common\models\RemoteOrders;

888851f9   Administrator   16.05.16 ann foto...
9
10
11
12
13
14
15
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  use common\modules\product\models\ProductVariant;

  use common\widgets\BasketModal;

  use Yii;

  

  use yii\web\Controller;

  use yii\web\NotFoundHttpException;

  use yii\data\ArrayDataProvider;

  /**

   * OrderController implements the CRUD actions for Order model.

   */

  class OrdersController extends Controller

  {

  

  

      /**

       * @inheritdoc

       */

      public function beforeAction($action)

      {

          if ($action->id == 'buy-items' || $action->id == 'delete') {

              Yii::$app->controller->enableCsrfValidation = false;

          }

  

          return true;

      }

  

      public function actionFirst(){

  

          $array = Yii::$app->session->get('order');

  

          if(isset($array['order_id']) ) {

              $model = Orders::findOne($array['order_id']);

          }else {

              $model = new Orders();

          }

  

  

          if(Yii::$app->request->post() &&  $model->load(Yii::$app->request->post())){

  

              if($model->save()){

  

                  $array['order_id'] =  $model->order_id;

  

                  Yii::$app->session->set('order', $array );

  

                  return $this->redirect(['orders/second']);

              }

  

          } else {

              if (!Yii::$app->user->isGuest) {

                  $customer = Yii::$app->user->identity;

                  $model->name = $customer->name;

                  $model->email = $customer->email;

                  $model->phone = $customer->phone;

              }

          }

  

  

          return $this->render('basket-step-01',[

              'model' => $model

          ]);

      }

  

      public function actionSecond(){

  

          $sessionData = \Yii::$app->session->get('order');

  

          $order_id = $sessionData['order_id'];

  

          if(!empty($order_id)){

              $order_model = Orders::findOne($order_id);

          } else{

a4e099fb   Administrator   16.05.16 ann foto...
81
              $this->redirect(['orders/first']);

888851f9   Administrator   16.05.16 ann foto...
82
83
84
85
86
87
88
89
90
91
92
          }

  

          unset($sessionData['order_id']);

  

          $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();

a4e099fb   Administrator   16.05.16 ann foto...
93
94
95
96
  

                  $remoteItemModel = RemoteOrders::find()->where(['ID'=>$order_id, 'ID_Product'=> $variant[$k]->product_variant_id])->one();

  

  

a4e099fb   Administrator   16.05.16 ann foto...
97
98
99
  

  

                  if($remoteItemModel instanceof RemoteOrders){

b44453de   Administrator   16.05.16 ann foto...
100
101
102
103
104
105
106
107
108
109
                      $remoteItemModel->ID =  (string)$order_id;

                      $remoteItemModel->ID_Client = (string)$order_model->customer_id;

                      $remoteItemModel->ID_Product =  (string)$variant[$k]->product_variant_id;

                      $remoteItemModel->Quantity = $sessionData[$k]['num'];

                      $remoteItemModel->Price_old = $variant[$k]->price;

                      $remoteItemModel->Date = date("Y-m-d H:i:s");

  //                    $remoteItemModel->Rate = '';

                      $remoteItemModel->Sum = $variant[$k]->price * $sessionData[$k]['num'];

                      $remoteItemModel->Payment_type = $order_model->payment;

                      $remoteItemModel->save();

a4e099fb   Administrator   16.05.16 ann foto...
110
                  } else {

b44453de   Administrator   16.05.16 ann foto...
111
112
113
114
115
116
117
118
119
120
121
122
                      $remoteItemModel = new RemoteOrders();

                      $remoteItemModel->ID =  (string)$order_id;

                      $remoteItemModel->ID_Client =  (string)$order_model->customer_id;

                      $remoteItemModel->ID_Product =  (string)$variant[$k]->product_variant_id;

                      $remoteItemModel->Quantity = $sessionData[$k]['num'];

                      $remoteItemModel->Price_old = $variant[$k]->price;

                      $remoteItemModel->Date = date("Y-m-d H:i:s");

  //                    $remoteItemModel->Rate = '';

                      $remoteItemModel->Sum = $variant[$k]->price * $sessionData[$k]['num'];

                      $remoteItemModel->Payment_type = $order_model->payment;

                      $remoteItemModel->save();

  

a4e099fb   Administrator   16.05.16 ann foto...
123
124
                  }

  

888851f9   Administrator   16.05.16 ann foto...
125
126
127
128
129
130
131
132
133
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
211
212
213
214
215
216
217
218
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
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
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
340
341
342
343
344
                  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();

                  }

  

              }

              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,

          ]);

  

      }

  

      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()

  //        ]);

  //    }

  

  

      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 );

          }

          echo BasketModal::widget([]);

  

      }

      /**

       * 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.');

          }

      }

  }