|
1
|
|
|
<?php |
|
2
|
|
|
namespace app\modules\shop\actions; |
|
3
|
|
|
|
|
4
|
|
|
use Yii; |
|
5
|
|
|
use yii\base\Action; |
|
6
|
|
|
use yii\web\Response; |
|
7
|
|
|
use yii\web\NotFoundHttpException; |
|
8
|
|
|
use app\modules\shop\models\Category; |
|
9
|
|
|
use app\modules\shop\models\Product; |
|
10
|
|
|
|
|
11
|
|
|
class BatchEditPriceAction extends Action |
|
12
|
|
|
{ |
|
13
|
|
|
const BEP_CONTEXT_PRODUCT = 'backend-product'; |
|
14
|
|
|
const BEP_KIND_FIXED = 'fixed'; |
|
15
|
|
|
const BEP_KIND_PERCENT = 'percentage'; |
|
16
|
|
|
const BEP_TYPE_NORMAL = 'normal'; |
|
17
|
|
|
const BEP_TYPE_RELATIVE = 'relative'; |
|
18
|
|
|
const BEP_FIELD_PRICE = 'price'; |
|
19
|
|
|
const BEP_FIELD_OLDPRICE = 'old_price'; |
|
20
|
|
|
const BEP_FIELD_ALL = 'all'; |
|
21
|
|
|
const BEP_INCREMENT = 'inc'; |
|
22
|
|
|
const BEP_DECREMENT = 'dec'; |
|
23
|
|
|
|
|
24
|
|
|
public $params = []; |
|
25
|
|
|
|
|
26
|
|
|
public function run() |
|
27
|
|
|
{ |
|
28
|
|
|
if (false === Yii::$app->request->isAjax) { |
|
29
|
|
|
throw new NotFoundHttpException(); |
|
30
|
|
|
} |
|
31
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
|
32
|
|
|
|
|
33
|
|
|
$this->params = [ |
|
34
|
|
|
'is_child_inc' => Yii::$app->request->post('is_child_inc', false), |
|
35
|
|
|
'kind' => Yii::$app->request->post('kind', ''), |
|
36
|
|
|
'operation' => Yii::$app->request->post('operation', ''), |
|
37
|
|
|
'is_round' => Yii::$app->request->post('is_round', true), |
|
38
|
|
|
'round_val' => intval(Yii::$app->request->post('round_val', 2)), |
|
39
|
|
|
'sel_field' => Yii::$app->request->post('apply_for', ''), |
|
40
|
|
|
'value' => floatval(Yii::$app->request->post('value', 0)), |
|
41
|
|
|
'type' => Yii::$app->request->post('type', ''), |
|
42
|
|
|
'currency_id' => intval(Yii::$app->request->post('currency_id', 0)), |
|
43
|
|
|
'context' => Yii::$app->request->post('context', ''), |
|
44
|
|
|
'items' => Yii::$app->request->post('items', []) |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
return $this->editPrices(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get list of child categories |
|
52
|
|
|
* @param $list int[] |
|
53
|
|
|
* @return int[] |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function getParentCategories($list) |
|
56
|
|
|
{ |
|
57
|
|
|
if ($this->params['is_child_inc']) { |
|
58
|
|
|
$count = count($list); |
|
59
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
60
|
|
|
$cats = Category::getByParentId($list[$i]); |
|
61
|
|
|
foreach ($cats as $category) { |
|
62
|
|
|
$list[] = $category->id; |
|
63
|
|
|
$count ++; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $list; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get parameters to build queries |
|
73
|
|
|
* @return mixed[] |
|
|
|
|
|
|
74
|
|
|
*/ |
|
75
|
|
|
protected function getSqlStatements() |
|
76
|
|
|
{ |
|
77
|
|
|
$updateRule = []; |
|
78
|
|
|
$condition = []; |
|
79
|
|
|
$args = []; |
|
80
|
|
|
|
|
81
|
|
|
$conditionFormat = '%s %s >= 0'; |
|
|
|
|
|
|
82
|
|
|
if ($this->params['is_round']) { |
|
83
|
|
|
$updateFormat = 'ROUND(%s %s, :round)'; |
|
84
|
|
|
} else { |
|
85
|
|
|
$updateFormat = '%s %s'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// operation |
|
89
|
|
|
if ($this->params['operation'] == self::BEP_INCREMENT) { |
|
90
|
|
|
$operation = '+'; |
|
91
|
|
|
} elseif ($this->params['operation'] == self::BEP_DECREMENT) { |
|
92
|
|
|
$operation = '-'; |
|
93
|
|
|
} |
|
94
|
|
|
if ($this->params['kind'] == self::BEP_KIND_FIXED) { |
|
95
|
|
|
$args['operation'] = $operation . ' :val'; |
|
|
|
|
|
|
96
|
|
|
} elseif ($this->params['kind'] == self::BEP_KIND_PERCENT) { |
|
97
|
|
|
$args['operation'] = " * (1 {$operation} :val / 100)"; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// price | old_price | both of them |
|
101
|
|
|
if ($this->params['type'] == self::BEP_TYPE_NORMAL) { |
|
102
|
|
View Code Duplication |
if ($this->params['sel_field'] == self::BEP_FIELD_PRICE |
|
103
|
|
|
|| $this->params['sel_field'] == self::BEP_FIELD_ALL |
|
104
|
|
|
) { |
|
105
|
|
|
$args['field_to'] = self::BEP_FIELD_PRICE; |
|
106
|
|
|
$args['field_from'] = self::BEP_FIELD_PRICE; |
|
107
|
|
|
} |
|
108
|
|
View Code Duplication |
if ($this->params['sel_field'] == self::BEP_FIELD_OLDPRICE) { |
|
109
|
|
|
$args['field_to'] = self::BEP_FIELD_OLDPRICE; |
|
110
|
|
|
$args['field_from'] = self::BEP_FIELD_OLDPRICE; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if ($this->params['sel_field'] == self::BEP_FIELD_ALL) { |
|
114
|
|
|
$updateRule[$args['field_to']] = new \yii\db\Expression( |
|
115
|
|
|
sprintf( |
|
116
|
|
|
$updateFormat, |
|
117
|
|
|
$args['field_from'], |
|
118
|
|
|
$args['operation'] |
|
119
|
|
|
) |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
$condition[] = $args['field_from'] . ' ' . $args['operation'] . '>= 0'; |
|
123
|
|
|
$args['field_to'] = self::BEP_FIELD_OLDPRICE; |
|
124
|
|
|
$args['field_from'] = self::BEP_FIELD_OLDPRICE; |
|
125
|
|
|
} |
|
126
|
|
|
} elseif ($this->params['type'] == self::BEP_TYPE_RELATIVE) { |
|
127
|
|
View Code Duplication |
if ($this->params['sel_field'] == self::BEP_FIELD_PRICE) { |
|
128
|
|
|
$args['field_to'] = self::BEP_FIELD_OLDPRICE; |
|
129
|
|
|
$args['field_from'] = self::BEP_FIELD_PRICE; |
|
130
|
|
|
} |
|
131
|
|
View Code Duplication |
if ($this->params['sel_field'] == self::BEP_FIELD_OLDPRICE) { |
|
132
|
|
|
$args['field_to'] = self::BEP_FIELD_PRICE; |
|
133
|
|
|
$args['field_from'] = self::BEP_FIELD_OLDPRICE; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$updateRule[$args['field_to']] = new \yii\db\Expression( |
|
138
|
|
|
sprintf( |
|
139
|
|
|
$updateFormat, |
|
140
|
|
|
$args['field_from'], |
|
141
|
|
|
$args['operation'] |
|
142
|
|
|
) |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
$condition[] = $args['field_from'] . ' ' . $args['operation'] . ' >= 0'; |
|
146
|
|
|
$condition[] = 'currency_id = :currency'; |
|
147
|
|
|
if ($this->params['context'] == self::BEP_CONTEXT_PRODUCT) { |
|
148
|
|
|
$data = implode(',', $this->params['items']); |
|
149
|
|
|
$condition['for_count'] = 'id IN (' . $data . ')'; |
|
150
|
|
|
} else { |
|
151
|
|
|
$data = implode(',', $this->getParentCategories($this->params['items'])); |
|
152
|
|
|
$condition['for_count'] = 'main_category_id IN (' . $data . ')'; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return [ |
|
156
|
|
|
'rule' => $updateRule, |
|
157
|
|
|
'condition' => new \yii\db\Expression(implode(' AND ', $condition)), |
|
158
|
|
|
'condition_for_count' => new \yii\db\Expression($condition['for_count']) |
|
159
|
|
|
]; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Editing of prices |
|
164
|
|
|
* @return int[] |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function editPrices() |
|
167
|
|
|
{ |
|
168
|
|
|
$sqlStatements = $this->getSqlStatements(); |
|
169
|
|
|
|
|
170
|
|
|
$result['all'] = Product::find() |
|
|
|
|
|
|
171
|
|
|
->where($sqlStatements['condition_for_count']) |
|
172
|
|
|
->count(); |
|
173
|
|
|
|
|
174
|
|
|
$result['success'] = Product::updateAll( |
|
175
|
|
|
$sqlStatements['rule'], |
|
176
|
|
|
$sqlStatements['condition'], |
|
177
|
|
|
[ |
|
178
|
|
|
':val' => $this->params['value'], |
|
179
|
|
|
':round' => $this->params['round_val'], |
|
180
|
|
|
':currency' => $this->params['currency_id'] |
|
181
|
|
|
] |
|
182
|
|
|
); |
|
183
|
|
|
|
|
184
|
|
|
return $result; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.