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
|
|
4428da8c
Yarik
Almost all databa...
|
57
58
59
60
61
62
63
64
|
return $this->render(
'index',
[
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'product' => $product,
]
);
|
d55d2fe0
Yarik
Multilanguage
|
65
|
}
|
36d1807a
Yarik
Big commit.
|
66
|
|
d55d2fe0
Yarik
Multilanguage
|
67
68
69
70
71
72
73
74
75
|
/**
* Displays a single ProductVariant model.
*
* @param integer $id
*
* @return mixed
*/
public function actionView($id)
{
|
4e55ce81
Yarik
Another one admin...
|
76
77
|
$model = $this->findModel($id);
$properties = $model->getProperties();
|
4428da8c
Yarik
Almost all databa...
|
78
79
80
81
82
83
84
|
return $this->render(
'view',
[
'model' => $model,
'properties' => $properties,
]
);
|
d55d2fe0
Yarik
Multilanguage
|
85
|
}
|
36d1807a
Yarik
Big commit.
|
86
|
|
d55d2fe0
Yarik
Multilanguage
|
87
88
89
|
/**
* Creates a new ProductVariant model.
* If creation is successful, the browser will be redirected to the 'view' page.
|
5c2eb7c8
Yarik
Big commit almost...
|
90
91
92
|
*
* @param int $product_id
*
|
d55d2fe0
Yarik
Multilanguage
|
93
94
95
96
|
* @return mixed
*/
public function actionCreate($product_id)
{
|
4e55ce81
Yarik
Another one admin...
|
97
|
$product = $this->findProduct($product_id);
|
d55d2fe0
Yarik
Multilanguage
|
98
|
$model = new ProductVariant();
|
8af13427
Yarik
For leha commit.
|
99
|
$model->product_id = $product->id;
|
72a992f5
Yarik
Import browser v1.0
|
100
|
$model->generateLangs();
|
4428da8c
Yarik
Almost all databa...
|
101
|
if ($model->load(Yii::$app->request->post())) {
|
72a992f5
Yarik
Import browser v1.0
|
102
|
$model->loadLangs(\Yii::$app->request);
|
4428da8c
Yarik
Almost all databa...
|
103
|
if ($model->save() && $model->transactionStatus) {
|
c70f24ea
Yarik
For Leha commit.
|
104
105
|
$model->stock = $this->saveStocks($model, Yii::$app->request->post('ProductStock', []));
if ($model->save(true, [ 'stock' ]) && $model->transactionStatus) {
|
4428da8c
Yarik
Almost all databa...
|
106
107
108
109
110
111
|
return $this->redirect(
[
'index',
'product_id' => $product->id,
]
);
|
36d1807a
Yarik
Big commit.
|
112
|
}
|
d8c1a2e0
Yarik
Big commit artbox
|
113
|
}
|
d8c1a2e0
Yarik
Big commit artbox
|
114
|
}
|
d8c1a2e0
Yarik
Big commit artbox
|
115
|
$groups = $model->getTaxGroupsByLevel(1);
|
4428da8c
Yarik
Almost all databa...
|
116
117
118
119
120
121
122
123
124
125
|
return $this->render(
'create',
[
'model' => $model,
'modelLangs' => $model->modelLangs,
'groups' => $groups,
'stocks' => [ new ProductStock() ],
'product' => $product,
]
);
|
d8c1a2e0
Yarik
Big commit artbox
|
126
|
}
|
d55d2fe0
Yarik
Multilanguage
|
127
128
129
130
131
132
133
134
135
136
137
138
|
/**
* 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...
|
139
|
$product = $this->findProduct($product_id);
|
d55d2fe0
Yarik
Multilanguage
|
140
|
$model = $this->findModel($id);
|
72a992f5
Yarik
Import browser v1.0
|
141
|
$model->generateLangs();
|
4428da8c
Yarik
Almost all databa...
|
142
|
if ($model->load(Yii::$app->request->post())) {
|
72a992f5
Yarik
Import browser v1.0
|
143
|
$model->loadLangs(\Yii::$app->request);
|
4428da8c
Yarik
Almost all databa...
|
144
|
if ($model->save() && $model->transactionStatus) {
|
c70f24ea
Yarik
For Leha commit.
|
145
146
|
$model->stock = $this->saveStocks($model, Yii::$app->request->post('ProductStock', []));
if ($model->save(true, [ 'stock' ]) && $model->transactionStatus) {
|
4428da8c
Yarik
Almost all databa...
|
147
148
149
150
151
152
|
return $this->redirect(
[
'index',
'product_id' => $product_id,
]
);
|
d8c1a2e0
Yarik
Big commit artbox
|
153
|
}
|
d8c1a2e0
Yarik
Big commit artbox
|
154
|
}
|
d8c1a2e0
Yarik
Big commit artbox
|
155
|
}
|
d8c1a2e0
Yarik
Big commit artbox
|
156
|
$groups = $model->getTaxGroupsByLevel(1);
|
4428da8c
Yarik
Almost all databa...
|
157
158
159
160
161
162
163
164
165
166
|
return $this->render(
'update',
[
'model' => $model,
'modelLangs' => $model->modelLangs,
'groups' => $groups,
'stocks' => ( !empty( $model->variantStocks ) ) ? $model->variantStocks : [ new ProductStock ],
'product' => $product,
]
);
|
d8c1a2e0
Yarik
Big commit artbox
|
167
|
}
|
d55d2fe0
Yarik
Multilanguage
|
168
169
170
171
172
|
/**
* Deletes an existing ProductVariant model.
* If deletion is successful, the browser will be redirected to the 'index' page.
*
|
72a992f5
Yarik
Import browser v1.0
|
173
|
* @param integer $product_id
|
d55d2fe0
Yarik
Multilanguage
|
174
175
176
177
178
179
180
181
182
183
|
* @param integer $id
*
* @return mixed
*/
public function actionDelete($product_id, $id)
{
$this->findModel($id)
->delete();
|
4428da8c
Yarik
Almost all databa...
|
184
185
186
187
188
189
|
return $this->redirect(
[
'index',
'product_id' => $product_id,
]
);
|
d8c1a2e0
Yarik
Big commit artbox
|
190
|
}
|
d55d2fe0
Yarik
Multilanguage
|
191
|
|
c70f24ea
Yarik
For Leha commit.
|
192
193
194
195
196
197
|
/**
* Deletes an existing ProductImage model.
*
* @param $id
*/
public function actionDeleteImage($id)
|
d55d2fe0
Yarik
Multilanguage
|
198
199
200
|
{
$image = ProductImage::findOne($id);
|
4428da8c
Yarik
Almost all databa...
|
201
|
if ($image) {
|
d55d2fe0
Yarik
Multilanguage
|
202
203
204
205
206
207
208
209
|
$image->delete();
}
print '1';
exit;
}
/**
|
c70f24ea
Yarik
For Leha commit.
|
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
|
* Save ProductStocks for ProductVariant and return total count of products.
*
* @param \common\modules\product\models\ProductVariant $productVariant
* @param array|null $productStocks
*
* @return int
*/
protected function saveStocks(ProductVariant $productVariant, array $productStocks = null)
{
$total_quantity = 0;
if (!empty( $productStocks )) {
$productVariant->unlinkAll('stocks', true);
$sorted_array = [];
foreach ($productStocks as $productStock) {
if (!empty( $productStock[ 'title' ] ) && !empty( $productStock[ 'quantity' ] )) {
if (!empty( $sorted_array[ $productStock[ 'title' ] ] )) {
$sorted_array[ $productStock[ 'title' ] ] += $productStock[ 'quantity' ];
} else {
$sorted_array[ $productStock[ 'title' ] ] = $productStock[ 'quantity' ];
}
}
}
$productStocks = $sorted_array;
$stock_names = array_keys($productStocks);
$stocks = Stock::find()
->joinWith('lang')
->where([ 'stock_lang.title' => $stock_names ])
|
772a3ca4
Yarik
Big commit
|
237
238
239
240
241
242
|
->indexBy(function($row) {
/**
* @var Stock $row
*/
return $row->lang->title;
})
|
c70f24ea
Yarik
For Leha commit.
|
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
|
->all();
foreach ($productStocks as $stockName => $quantity) {
$quantity = (int) $quantity;
if (!array_key_exists($stockName, $stocks)) {
$stock = new Stock();
$stock->generateLangs();
foreach ($stock->modelLangs as $modelLang) {
$modelLang->title = $stockName;
}
if (!$stock->save() || !$stock->transactionStatus) {
continue;
}
} else {
$stock = $stocks[ $stockName ];
}
$psModel = new ProductStock(
[
'product_variant_id' => $productVariant->id,
'stock_id' => $stock->id,
'quantity' => $quantity,
]
);
if ($psModel->save()) {
$total_quantity += $quantity;
}
}
} else {
$productVariant->unlinkAll('stocks', true);
}
return $total_quantity;
}
/**
|
d55d2fe0
Yarik
Multilanguage
|
276
277
278
279
280
281
282
283
284
285
|
* 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)
{
|
4428da8c
Yarik
Almost all databa...
|
286
287
288
289
|
if (( $model = ProductVariant::find()
->where([ 'id' => $id ])
->with('lang')
->one() ) !== null
|
4e55ce81
Yarik
Another one admin...
|
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
) {
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)
{
|
4428da8c
Yarik
Almost all databa...
|
305
306
307
308
|
if (( $model = Product::find()
->with('lang')
->where([ 'id' => $product_id ])
->one() ) !== null
|
4e55ce81
Yarik
Another one admin...
|
309
|
) {
|
d55d2fe0
Yarik
Multilanguage
|
310
311
312
313
|
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
|
d8c1a2e0
Yarik
Big commit artbox
|
314
315
|
}
}
|