Blame view

frontend/controllers/OrderController.php 6.64 KB
d09f430f   Administrator   big commti
1
2
3
4
5
6
7
8
9
  <?php
  
  namespace frontend\controllers;
  
  
  use common\models\Customer;
  use common\models\OrdersProducts;
  use common\widgets\Mailer;
  use Yii;
7f87d6f9   Yarik   Modals.
10
11
  use yii\base\InvalidConfigException;
  use yii\base\InvalidParamException;
d09f430f   Administrator   big commti
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
  use yii\helpers\ArrayHelper;
  use yii\web\Controller;
  use common\models\Basket;
  use common\modules\product\models\ProductVariant;
  use common\models\Orders;
  
  
  class OrderController extends Controller
  {
  
  
      public function actionIndex()
      {
          $basket = \Yii::$app->basket;
          $data = $basket->getData();
          $models = $basket->findModels(array_keys($data));
          return $this->render('index', [
              'models' => $models,
              'basket' => $basket,
          ]);
      }
  
  
      /**
       *
       * @return string
       */
      public function actionSave()
      {
          $modelOrder = new Orders;
          /**
           * @var $basket Basket
           */
          $basket = \Yii::$app->basket;
          $productV = $basket->getData();
d09f430f   Administrator   big commti
47
          if(!empty($productV) && $modelOrder->load(Yii::$app->request->post())&& $modelOrder->save()){
d09f430f   Administrator   big commti
48
49
50
              foreach ($productV as $index=>$row) {
                  $modelOrdersProducts = new OrdersProducts();
                  $mod_id = $index;
d09f430f   Administrator   big commti
51
52
53
54
                  $product = ProductVariant::findOne($mod_id);
                  /**
                   * Проверяем товар на наличие
                   */
d09f430f   Administrator   big commti
55
                  if(!$product->stock > 0 || !$product->price > 0 ){
d09f430f   Administrator   big commti
56
57
58
59
                      /**
                       * Добавляем сообщение об ошибке
                       */
                      \Yii::$app->getSession()->setFlash('error', 'К сожалению товара '.$product->name . ' нет в наличии');
8ec69373   Yarik   Orders.
60
61
                      $basket->delete($product->product_variant_id);
                      unset($productV[$index]);
d09f430f   Administrator   big commti
62
                  }else {
d09f430f   Administrator   big commti
63
64
65
                      /**
                       * Удаляем товар с массива и сохраняем в заказ
                       */
d09f430f   Administrator   big commti
66
67
68
69
70
71
72
73
74
75
76
77
78
                      $modelOrdersProducts->order_id =  $modelOrder->id;
                      $modelOrdersProducts->product_name = $product->product->name;
                      $modelOrdersProducts->name = $product->name;
                      $modelOrdersProducts->price = $productV[$index]['price'];
                      $modelOrdersProducts->count= $productV[$index]['count'];
                      $modelOrdersProducts->sum_cost = $productV[$index]['price'] *$productV[$index]['count'] ;
                      $modelOrdersProducts->mod_id = $mod_id;
                      $modelOrdersProducts->sku = $product->sku;
                      $modelOrdersProducts->validate();
                      $modelOrdersProducts->save();
                      $productV[$index] = ArrayHelper::toArray($modelOrdersProducts);
                      $productV[$index]['img'] = \common\components\artboximage\ArtboxImageHelper::getImageSrc($product->image->imageUrl, 'list');
                  }
d09f430f   Administrator   big commti
79
              }
d09f430f   Administrator   big commti
80
81
82
              /**
               * Сохраняем заказ
               */
d09f430f   Administrator   big commti
83
84
85
86
87
88
89
90
91
92
93
94
95
96
              if(!Yii::$app->user->id && !empty($modelOrder->email)){
                  $modelUser = new Customer();
                  $modelUser->role = 'person';
                  $modelUser->username = $modelOrder->email;
                  $modelUser->name = $modelOrder->name;
                  $modelUser->phone = $modelOrder->phone;
                  $modelUser->password = Yii::$app->getSecurity()->generateRandomString(10);
                  $modelUser->group_id = 2;
                  $modelUser->save();
              }
              $order = clone $modelOrder;
              /**
               * Чистим сессию корзины
               */
d09f430f   Administrator   big commti
97
              $modelOrder->clearBasket();
d09f430f   Administrator   big commti
98
99
100
101
102
103
104
105
106
              Mailer::widget(
                  ['type' => 'order',
                      'subject'=> 'Спасибо за покупку',
                      'email' => $modelOrder->email,
                      'params' => [
                          'order' => $order,
                          'variants' => $productV,
                      ]
                  ]);
d09f430f   Administrator   big commti
107
108
              //$text = "# zakaz: ". $order->id .". V blijayshee vremya menedjer svyajetsya s Vami. (044) 303 90 15";
              //Yii::$app->sms->send($order->phone, $text);
d09f430f   Administrator   big commti
109
              Yii::$app->session['order_data']= ['order' => $order,'variants'=>$productV];
8ec69373   Yarik   Orders.
110
              return $this->redirect(['order/success']);
d09f430f   Administrator   big commti
111
          }
d09f430f   Administrator   big commti
112
113
114
115
116
117
118
119
120
          $data = $basket->getData();
          $models = $basket->findModels(array_keys($data));
          return $this->render('index', [
              'models' => $models,
              'basket' => $basket
          ]);
      }
  
      public function actionSuccess(){
8ec69373   Yarik   Orders.
121
          return $this->render('success');
d09f430f   Administrator   big commti
122
      }
7f87d6f9   Yarik   Modals.
123
124
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
      
      public function actionQuick() {
          $response = \Yii::$app->response;
          $request  = \Yii::$app->request;
          $response->format = $response::FORMAT_JSON;
          $product_variant_id = (int) $request->post('product_variant_id');
          $orders = new Orders([
              'scenario' => Orders::SCENARIO_QUICK,
              'name' => 'Покупка в 1 клик',
          ]);
          if(!empty($product_variant_id)) {
              /**
               * @var ProductVariant $product_variant
               */
              $product_variant = ProductVariant::findOne($product_variant_id);
          } else {
              throw new InvalidParamException('Не указан товар');
          }
          if(!empty($product_variant) && $orders->load($request->post()) && $orders->save()) {
              if($product_variant->stock <= 0 || $product_variant->price <= 0) {
                  $orders->delete();
                  return [
                      'error' => 'К сожалению товара '.$product_variant->name.' нет в наличии',
                  ];
              } else {
                  $order_product = new OrdersProducts([
                      'order_id' => $orders->id,
                      'product_name' => $product_variant->product->name,
                      'name' => $product_variant->name,
                      'price' => $product_variant->price,
                      'count' => 1,
                      'sum_cost' => $product_variant->price,
                      'mod_id' => $product_variant->product_variant_id,
                      'sku' => $product_variant->sku,
                  ]);
                  $order_product->save();
                  return [
                      'result' => 'Спасибо за заказ! Наши менеджеры свяжутся с Вами в ближайшее время.'
                  ];
              }
          } else {
              throw new InvalidConfigException('Товар не найден или не удалось загрузить данные о покупателе.');
          }
          
      }
d09f430f   Administrator   big commti
168
169
  
  }