Blame view

common/modules/product/controllers/VariantController.php 12 KB
d8c1a2e0   Yarik   Big commit artbox
1
  <?php
d55d2fe0   Yarik   Multilanguage
2
3
4
5
6
7
8
      
      namespace common\modules\product\controllers;
      
      use common\modules\product\models\Product;
      use common\modules\product\models\ProductImage;
      use common\modules\product\models\ProductStock;
      use common\modules\product\models\ProductVariant;
4e55ce81   Yarik   Another one admin...
9
      use common\modules\product\models\ProductVariantSearch;
d55d2fe0   Yarik   Multilanguage
10
11
      use common\modules\product\models\Stock;
      use Yii;
d55d2fe0   Yarik   Multilanguage
12
13
14
      use yii\web\Controller;
      use yii\web\NotFoundHttpException;
      use yii\filters\VerbFilter;
d55d2fe0   Yarik   Multilanguage
15
      
d8c1a2e0   Yarik   Big commit artbox
16
      /**
4e55ce81   Yarik   Another one admin...
17
       * VartiantController implements the CRUD actions for ProductVariant model.
d8c1a2e0   Yarik   Big commit artbox
18
       */
d55d2fe0   Yarik   Multilanguage
19
      class VariantController extends Controller
d8c1a2e0   Yarik   Big commit artbox
20
      {
d55d2fe0   Yarik   Multilanguage
21
22
23
24
25
26
27
28
29
30
31
32
          
          /**
           * @inheritdoc
           */
          public function behaviors()
          {
              return [
                  'verbs' => [
                      'class'   => VerbFilter::className(),
                      'actions' => [
                          'delete' => [ 'POST' ],
                      ],
d8c1a2e0   Yarik   Big commit artbox
33
                  ],
d55d2fe0   Yarik   Multilanguage
34
              ];
d8c1a2e0   Yarik   Big commit artbox
35
          }
36d1807a   Yarik   Big commit.
36
          
d55d2fe0   Yarik   Multilanguage
37
38
39
40
41
42
          /**
           * Lists all ProductVariant models.
           * @return mixed
           */
          public function actionIndex($product_id)
          {
4e55ce81   Yarik   Another one admin...
43
44
45
46
47
              $product = $this->findProduct($product_id);
              $searchModel = new ProductVariantSearch();
              $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
              $dataProvider->query->andWhere([ 'product_id' => $product->product_id ])
                                  ->with('image');
d55d2fe0   Yarik   Multilanguage
48
49
50
51
52
              
              return $this->render('index', [
                  'searchModel'  => $searchModel,
                  'dataProvider' => $dataProvider,
                  'product'      => $product,
d55d2fe0   Yarik   Multilanguage
53
54
              ]);
          }
36d1807a   Yarik   Big commit.
55
          
d55d2fe0   Yarik   Multilanguage
56
57
58
59
60
61
62
63
64
          /**
           * Displays a single ProductVariant model.
           *
           * @param integer $id
           *
           * @return mixed
           */
          public function actionView($id)
          {
4e55ce81   Yarik   Another one admin...
65
66
              $model = $this->findModel($id);
              $properties = $model->getProperties();
d55d2fe0   Yarik   Multilanguage
67
              return $this->render('view', [
4e55ce81   Yarik   Another one admin...
68
69
                  'model'      => $model,
                  'properties' => $properties,
d55d2fe0   Yarik   Multilanguage
70
71
              ]);
          }
36d1807a   Yarik   Big commit.
72
          
d55d2fe0   Yarik   Multilanguage
73
74
75
76
77
78
79
          /**
           * Creates a new ProductVariant model.
           * If creation is successful, the browser will be redirected to the 'view' page.
           * @return mixed
           */
          public function actionCreate($product_id)
          {
4e55ce81   Yarik   Another one admin...
80
              $product = $this->findProduct($product_id);
d55d2fe0   Yarik   Multilanguage
81
              $model = new ProductVariant();
4e55ce81   Yarik   Another one admin...
82
              $model->product_id = $product->product_id;
72a992f5   Yarik   Import browser v1.0
83
              $model->generateLangs();
d55d2fe0   Yarik   Multilanguage
84
              if($model->load(Yii::$app->request->post())) {
72a992f5   Yarik   Import browser v1.0
85
                  $model->loadLangs(\Yii::$app->request);
72a992f5   Yarik   Import browser v1.0
86
                  if($model->save() && $model->transactionStatus) {
d55d2fe0   Yarik   Multilanguage
87
                      $ProductStocks = Yii::$app->request->post('ProductStock');
d55d2fe0   Yarik   Multilanguage
88
                      $total_quantity = 0;
d55d2fe0   Yarik   Multilanguage
89
90
91
92
93
94
95
96
97
98
                      if(!empty( $ProductStocks ) && is_array($ProductStocks)) {
                          $model->unlinkAll('stocks', true);
                          $sorted_array = [];
                          foreach($ProductStocks as $subArray) {
                              if(!empty( $subArray[ 'name' ] ) && !empty( $subArray[ 'quantity' ] )) {
                                  if(!empty( $sorted_array[ $subArray[ 'name' ] ] )) {
                                      $sorted_array[ $subArray[ 'name' ] ] += $subArray[ 'quantity' ];
                                  } else {
                                      $sorted_array[ $subArray[ 'name' ] ] = $subArray[ 'quantity' ];
                                  }
36d1807a   Yarik   Big commit.
99
100
                              }
                          }
d55d2fe0   Yarik   Multilanguage
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
                          $ProductStocks = $sorted_array;
                          $stock_names = array_keys($ProductStocks);
                          $stocks = Stock::find()
                                         ->where([ 'name' => $stock_names ])
                                         ->indexBy('name')
                                         ->all();
                          foreach($ProductStocks as $name => $quantity) {
                              $quantity = (int) $quantity;
                              if(!array_key_exists($name, $stocks)) {
                                  $stock = new Stock([
                                      'name' => $name,
                                  ]);
                                  if(!$stock->save()) {
                                      continue;
                                  }
                              } else {
                                  $stock = $stocks[ $name ];
                              }
                              $psModel = new ProductStock([
                                  'product_id'         => $model->product_id,
                                  'product_variant_id' => $model->product_variant_id,
                                  'stock_id'           => $stock->stock_id,
                                  'quantity'           => $quantity,
36d1807a   Yarik   Big commit.
124
                              ]);
d55d2fe0   Yarik   Multilanguage
125
126
                              if($psModel->save()) {
                                  $total_quantity += $quantity;
36d1807a   Yarik   Big commit.
127
                              }
36d1807a   Yarik   Big commit.
128
                          }
d55d2fe0   Yarik   Multilanguage
129
130
131
132
133
                      } else {
                          $model->unlinkAll('stocks', true);
                      }
                      
                      $model->stock = $total_quantity;
72a992f5   Yarik   Import browser v1.0
134
                      if($model->save() && $model->transactionStatus) {
d55d2fe0   Yarik   Multilanguage
135
136
                          return $this->redirect([
                              'index',
4e55ce81   Yarik   Another one admin...
137
                              'product_id' => $product->product_id,
d55d2fe0   Yarik   Multilanguage
138
                          ]);
36d1807a   Yarik   Big commit.
139
                      }
d8c1a2e0   Yarik   Big commit artbox
140
                  }
d8c1a2e0   Yarik   Big commit artbox
141
              }
d8c1a2e0   Yarik   Big commit artbox
142
              $groups = $model->getTaxGroupsByLevel(1);
d8c1a2e0   Yarik   Big commit artbox
143
              return $this->render('create', [
d55d2fe0   Yarik   Multilanguage
144
                  'model'       => $model,
72a992f5   Yarik   Import browser v1.0
145
                  'model_langs' => $model->model_langs,
d55d2fe0   Yarik   Multilanguage
146
147
                  'groups'      => $groups,
                  'stocks'      => [ new ProductStock() ],
4e55ce81   Yarik   Another one admin...
148
                  'product'     => $product,
d8c1a2e0   Yarik   Big commit artbox
149
150
              ]);
          }
d55d2fe0   Yarik   Multilanguage
151
152
153
154
155
156
157
158
159
160
161
162
          
          /**
           * Updates an existing ProductVariant model.
           * If update is successful, the browser will be redirected to the 'view' page.
           *
           * @param integer $product_id
           * @param integer $id
           *
           * @return mixed
           */
          public function actionUpdate($product_id, $id)
          {
4e55ce81   Yarik   Another one admin...
163
              $product = $this->findProduct($product_id);
d55d2fe0   Yarik   Multilanguage
164
              $model = $this->findModel($id);
72a992f5   Yarik   Import browser v1.0
165
              $model->generateLangs();
d55d2fe0   Yarik   Multilanguage
166
              if($model->load(Yii::$app->request->post())) {
72a992f5   Yarik   Import browser v1.0
167
168
                  $model->loadLangs(\Yii::$app->request);
                  if($model->save() && $model->transactionStatus) {
d55d2fe0   Yarik   Multilanguage
169
                      $ProductStocks = Yii::$app->request->post('ProductStock');
d55d2fe0   Yarik   Multilanguage
170
                      $total_quantity = 0;
d55d2fe0   Yarik   Multilanguage
171
172
173
174
175
176
177
178
179
180
                      if(!empty( $ProductStocks ) && is_array($ProductStocks)) {
                          $model->unlinkAll('stocks', true);
                          $sorted_array = [];
                          foreach($ProductStocks as $subArray) {
                              if(!empty( $subArray[ 'name' ] ) && !empty( $subArray[ 'quantity' ] )) {
                                  if(!empty( $sorted_array[ $subArray[ 'name' ] ] )) {
                                      $sorted_array[ $subArray[ 'name' ] ] += $subArray[ 'quantity' ];
                                  } else {
                                      $sorted_array[ $subArray[ 'name' ] ] = $subArray[ 'quantity' ];
                                  }
36d1807a   Yarik   Big commit.
181
                              }
d8c1a2e0   Yarik   Big commit artbox
182
                          }
d55d2fe0   Yarik   Multilanguage
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
                          $ProductStocks = $sorted_array;
                          $stock_names = array_keys($ProductStocks);
                          $stocks = Stock::find()
                                         ->where([ 'name' => $stock_names ])
                                         ->indexBy('name')
                                         ->all();
                          foreach($ProductStocks as $name => $quantity) {
                              $quantity = (int) $quantity;
                              if(!array_key_exists($name, $stocks)) {
                                  $stock = new Stock([
                                      'name' => $name,
                                  ]);
                                  if(!$stock->save()) {
                                      continue;
                                  }
                              } else {
                                  $stock = $stocks[ $name ];
                              }
                              $psModel = new ProductStock([
                                  'product_id'         => $model->product_id,
                                  'product_variant_id' => $model->product_variant_id,
                                  'stock_id'           => $stock->stock_id,
                                  'quantity'           => $quantity,
36d1807a   Yarik   Big commit.
206
                              ]);
d55d2fe0   Yarik   Multilanguage
207
208
                              if($psModel->save()) {
                                  $total_quantity += $quantity;
36d1807a   Yarik   Big commit.
209
                              }
36d1807a   Yarik   Big commit.
210
                          }
d55d2fe0   Yarik   Multilanguage
211
212
213
                      } else {
                          $model->unlinkAll('stocks', true);
                      }
d55d2fe0   Yarik   Multilanguage
214
                      $model->stock = $total_quantity;
72a992f5   Yarik   Import browser v1.0
215
                      if($model->save() && $model->transactionStatus) {
d55d2fe0   Yarik   Multilanguage
216
217
218
                          return $this->redirect([
                              'index',
                              'product_id' => $product_id,
36d1807a   Yarik   Big commit.
219
                          ]);
d8c1a2e0   Yarik   Big commit artbox
220
                      }
d8c1a2e0   Yarik   Big commit artbox
221
                  }
d8c1a2e0   Yarik   Big commit artbox
222
              }
d8c1a2e0   Yarik   Big commit artbox
223
              $groups = $model->getTaxGroupsByLevel(1);
d8c1a2e0   Yarik   Big commit artbox
224
              return $this->render('update', [
d55d2fe0   Yarik   Multilanguage
225
                  'model'       => $model,
72a992f5   Yarik   Import browser v1.0
226
                  'model_langs' => $model->model_langs,
d55d2fe0   Yarik   Multilanguage
227
228
                  'groups'      => $groups,
                  'stocks'      => ( !empty( $model->variantStocks ) ) ? $model->variantStocks : [ new ProductStock ],
4e55ce81   Yarik   Another one admin...
229
                  'product'     => $product,
d8c1a2e0   Yarik   Big commit artbox
230
              ]);
d8c1a2e0   Yarik   Big commit artbox
231
          }
d55d2fe0   Yarik   Multilanguage
232
233
234
235
236
          
          /**
           * Deletes an existing ProductVariant model.
           * If deletion is successful, the browser will be redirected to the 'index' page.
           *
72a992f5   Yarik   Import browser v1.0
237
           * @param integer $product_id
d55d2fe0   Yarik   Multilanguage
238
239
240
241
242
243
244
245
246
247
248
249
250
251
           * @param integer $id
           *
           * @return mixed
           */
          public function actionDelete($product_id, $id)
          {
              
              $this->findModel($id)
                   ->delete();
              
              return $this->redirect([
                  'index',
                  'product_id' => $product_id,
              ]);
d8c1a2e0   Yarik   Big commit artbox
252
          }
d55d2fe0   Yarik   Multilanguage
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
          
          public function actionDelimg($id)
          {
              $image = ProductImage::findOne($id);
              
              if($image) {
                  $image->delete();
              }
              
              print '1';
              exit;
          }
          
          /**
           * Finds the ProductVariant model based on its primary key value.
           * If the model is not found, a 404 HTTP exception will be thrown.
           *
           * @param integer $id
           *
           * @return ProductVariant the loaded model
           * @throws NotFoundHttpException if the model cannot be found
           */
          protected function findModel($id)
          {
4e55ce81   Yarik   Another one admin...
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
              if(( $model = ProductVariant::find()
                                          ->where([ 'product_variant_id' => $id ])
                                          ->with('lang')
                                          ->one() ) !== NULL
              ) {
                  return $model;
              } else {
                  throw new NotFoundHttpException('The requested page does not exist.');
              }
          }
          
          /**
           * @param int $product_id
           *
           * @return Product
           * @throws NotFoundHttpException
           */
          protected function findProduct($product_id)
          {
              if(( $model = Product::find()
                                   ->with('lang')
                                   ->where([ 'product_id' => $product_id ])
                                   ->one() ) !== NULL
              ) {
d55d2fe0   Yarik   Multilanguage
301
302
303
304
                  return $model;
              } else {
                  throw new NotFoundHttpException('The requested page does not exist.');
              }
d8c1a2e0   Yarik   Big commit artbox
305
306
          }
      }