Blame view

frontend/controllers/OrderController.php 10.1 KB
d09f430f   Administrator   big commti
1
  <?php
0fc3bf10   Yarik   Quickbuy modal
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
      
      namespace frontend\controllers;
      
      use common\models\Customer;
      use common\models\OrdersProducts;
      use common\widgets\Mailer;
      use Yii;
      use yii\base\InvalidConfigException;
      use yii\base\InvalidParamException;
      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
d09f430f   Administrator   big commti
18
      {
0fc3bf10   Yarik   Quickbuy modal
19
          
076b9c96   Administrator   big commti
20
          public function actionMain()
0fc3bf10   Yarik   Quickbuy modal
21
22
23
24
25
26
27
28
29
30
          {
              $basket = \Yii::$app->basket;
              $data = $basket->getData();
              $models = $basket->findModels(array_keys($data));
              return $this->render('index', [
                  'models' => $models,
                  'basket' => $basket,
              ]);
          }
          
d09f430f   Administrator   big commti
31
          /**
0fc3bf10   Yarik   Quickbuy modal
32
           * @return string
d09f430f   Administrator   big commti
33
           */
0fc3bf10   Yarik   Quickbuy modal
34
35
36
37
38
39
40
41
42
43
44
45
46
          public function actionSave()
          {
              $modelOrder = new Orders;
              /**
               * @var $basket Basket
               */
              $basket = \Yii::$app->basket;
              $productV = $basket->getData();
              if(!empty( $productV ) && $modelOrder->load(Yii::$app->request->post()) && $modelOrder->save()) {
                  foreach($productV as $index => $row) {
                      $modelOrdersProducts = new OrdersProducts();
                      $mod_id = $index;
                      $product = ProductVariant::findOne($mod_id);
d09f430f   Administrator   big commti
47
                      /**
0fc3bf10   Yarik   Quickbuy modal
48
                       * Проверяем товар на наличие
d09f430f   Administrator   big commti
49
                       */
5181a91a   Administrator   big commti
50
                      if( !$product->price > 0) {
0fc3bf10   Yarik   Quickbuy modal
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
                          /**
                           * Добавляем сообщение об ошибке
                           */
                          \Yii::$app->getSession()
                                    ->setFlash('error', 'К сожалению товара ' . $product->name . ' нет в наличии');
                          $basket->delete($product->product_variant_id);
                          unset( $productV[ $index ] );
                      } else {
                          /**
                           * Удаляем товар с массива и сохраняем в заказ
                           */
                          $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);
37415bee   Administrator   big commti
73
74
75
76
                          if(isset($product->image)){
                              $productV[ $index ][ 'img' ] = \common\components\artboximage\ArtboxImageHelper::getImageSrc($product->image->imageUrl, 'list');
                          }
  
0fc3bf10   Yarik   Quickbuy modal
77
                      }
d09f430f   Administrator   big commti
78
                  }
0fc3bf10   Yarik   Quickbuy modal
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
                  /**
                   * Сохраняем заказ
                   */
                  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;
                  /**
                   * Чистим сессию корзины
                   */
                  $modelOrder->clearBasket();
                  Mailer::widget([
                      'type'    => 'order',
df819475   Alex Savenko   mail
100
                      'subject' => 'Спасибо за Ваш заказ',
0fc3bf10   Yarik   Quickbuy modal
101
102
103
                      'email'   => $modelOrder->email,
                      'params'  => [
                          'order'    => $order,
d09f430f   Administrator   big commti
104
                          'variants' => $productV,
0fc3bf10   Yarik   Quickbuy modal
105
                      ],
d09f430f   Administrator   big commti
106
                  ]);
0fc3bf10   Yarik   Quickbuy modal
107
108
109
110
111
112
113
114
115
116
117
118
119
                  //$text = "# zakaz: ". $order->id .". V blijayshee vremya menedjer svyajetsya s Vami. (044) 303 90 15";
                  //Yii::$app->sms->send($order->phone, $text);
                  Yii::$app->session[ 'order_data' ] = [ 'order'    => $order,
                                                         'variants' => $productV,
                  ];
                  return $this->redirect([ 'order/success' ]);
              }
              $data = $basket->getData();
              $models = $basket->findModels(array_keys($data));
              return $this->render('index', [
                  'models' => $models,
                  'basket' => $basket,
              ]);
d09f430f   Administrator   big commti
120
          }
b905419d   Administrator   big commti
121
  
0fc3bf10   Yarik   Quickbuy modal
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
          
          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()) {
5181a91a   Administrator   big commti
142
                  if( $product_variant->price <= 0) {
0fc3bf10   Yarik   Quickbuy modal
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
                      $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('Товар не найден или не удалось загрузить данные о покупателе.');
              }
          }
b905419d   Administrator   big commti
167
168
169
170
171
172
173
174
  
  
          public function actionSuccess()
          {
              return $this->render('success');
          }
  
  
0fc3bf10   Yarik   Quickbuy modal
175
176
          public function actionQuickBasket()
          {
7c3211cc   Administrator   big commti
177
  
0fc3bf10   Yarik   Quickbuy modal
178
179
180
              $response = \Yii::$app->response;
              $request = \Yii::$app->request;
              $response->format = $response::FORMAT_JSON;
7f87d6f9   Yarik   Modals.
181
              /**
0fc3bf10   Yarik   Quickbuy modal
182
               * @var array $data
7f87d6f9   Yarik   Modals.
183
               */
0fc3bf10   Yarik   Quickbuy modal
184
              $data = \Yii::$app->basket->getData();
7c3211cc   Administrator   big commti
185
  
0fc3bf10   Yarik   Quickbuy modal
186
187
188
189
              $orders = new Orders([
                  'scenario' => Orders::SCENARIO_QUICK,
                  'name'     => 'Покупка в 1 клик',
              ]);
7c3211cc   Administrator   big commti
190
191
192
  
  
  
0fc3bf10   Yarik   Quickbuy modal
193
              if(!empty( $data )) {
7c3211cc   Administrator   big commti
194
  
0fc3bf10   Yarik   Quickbuy modal
195
196
197
198
199
200
201
                  /**
                   * @var ProductVariant[] $product_variants
                   * @var OrdersProducts[] $order_products
                   */
                  $product_variants = [];
                  $order_products = [];
                  foreach($data as $product_variant_id => $item) {
7c3211cc   Administrator   big commti
202
  
0fc3bf10   Yarik   Quickbuy modal
203
                      $product_variant = ProductVariant::findOne($product_variant_id);
5181a91a   Administrator   big commti
204
                      if(!empty($product_variant)  && $product_variant->price > 0) {
0fc3bf10   Yarik   Quickbuy modal
205
206
207
208
209
210
211
212
213
214
215
216
                          $product_variants[$product_variant_id] = $product_variant;
                          $order_products[$product_variant_id] = new OrdersProducts([
                              'product_name' => $product_variant->product->name,
                              'name'         => $product_variant->name,
                              'price'        => $product_variant->price,
                              'count'        => $item['count'],
                              'sum_cost'     => $item['count'] * $product_variant->price,
                              'mod_id'       => $product_variant->product_variant_id,
                              'sku'          => $product_variant->sku,
                          ]);
                      }
                  }
7c3211cc   Administrator   big commti
217
  
7f87d6f9   Yarik   Modals.
218
              } else {
7c3211cc   Administrator   big commti
219
  
0fc3bf10   Yarik   Quickbuy modal
220
221
                  throw new InvalidParamException('Не указан товар');
              }
7c3211cc   Administrator   big commti
222
223
224
  
  
  
0fc3bf10   Yarik   Quickbuy modal
225
              if(!empty( $order_products ) && $orders->load($request->post()) && $orders->save()) {
7c3211cc   Administrator   big commti
226
  
0fc3bf10   Yarik   Quickbuy modal
227
228
229
230
231
                  foreach($order_products as $order_product) {
                      $order_product->order_id = $orders->id;
                      $order_product->save();
                  }
                  \Yii::$app->basket->clear();
7f87d6f9   Yarik   Modals.
232
                  return [
0fc3bf10   Yarik   Quickbuy modal
233
                      'result' => 'Спасибо за заказ! Наши менеджеры свяжутся с Вами в ближайшее время.',
7f87d6f9   Yarik   Modals.
234
                  ];
0fc3bf10   Yarik   Quickbuy modal
235
              } else {
7c3211cc   Administrator   big commti
236
  
0fc3bf10   Yarik   Quickbuy modal
237
                  throw new InvalidConfigException('Товар не найден или не удалось загрузить данные о покупателе.');
7f87d6f9   Yarik   Modals.
238
              }
7f87d6f9   Yarik   Modals.
239
          }
7f87d6f9   Yarik   Modals.
240
      }