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