|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hideyo\Ecommerce\Framework\Services\Product; |
|
4
|
|
|
|
|
5
|
|
|
use App\Product; |
|
|
|
|
|
|
6
|
|
|
use Hideyo\Ecommerce\Framework\Services\Product\Entity\ProductAttributeCombination; |
|
7
|
|
|
use Hideyo\Ecommerce\Framework\Services\Product\Entity\ProductCombinationRepository; |
|
8
|
|
|
use Hideyo\Ecommerce\Framework\Services\BaseService; |
|
9
|
|
|
|
|
10
|
|
|
class ProductCombinationService extends BaseService |
|
11
|
|
|
{ |
|
12
|
|
|
public function __construct(ProductCombinationRepository $productTagGroup) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->repo = $productTagGroup; |
|
|
|
|
|
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function create(array $attributes, $productId) |
|
18
|
|
|
{ |
|
19
|
|
|
if (isset($attributes['selected_attribute_ids'])) { |
|
20
|
|
|
$check = $this->repo->getModelAttributeCombination()->leftJoin($this->repo->getModel()->getTable(), $this->repo->getModel()->getTable().'.id', '=', $this->repo->getModelAttributeCombination()->getTable().'.product_attribute_id') |
|
21
|
|
|
->where($this->repo->getModel()->getTable().'.product_id', '=', $productId); |
|
22
|
|
|
|
|
23
|
|
|
if (isset($attributes['selected_attribute_ids'])) { |
|
24
|
|
|
$check->where(function ($query) use ($attributes) { |
|
25
|
|
|
$query->whereIn($this->repo->getModelAttributeCombination()->getTable().'.attribute_id', $attributes['selected_attribute_ids']); |
|
26
|
|
|
}); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if ($check->get()->count()) { |
|
30
|
|
|
$newData = array(); |
|
31
|
|
|
foreach ($check->get() as $row) { |
|
32
|
|
|
$newData[$row['product_attribute_id']] = $row->productAttribute->combinations->toArray(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
foreach ($newData as $row) { |
|
36
|
|
|
if (count($row) == count($attributes['selected_attribute_ids'])) { |
|
37
|
|
|
$i = 0; |
|
38
|
|
|
foreach ($row as $newRow) { |
|
39
|
|
|
if (in_array($newRow['attribute_id'], $attributes['selected_attribute_ids'])) { |
|
40
|
|
|
$i++; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if (count($row) == $i) { |
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$data = $attributes; |
|
52
|
|
|
$data['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
|
|
53
|
|
|
$data['product_id'] = $productId; |
|
54
|
|
|
|
|
55
|
|
|
$new = $this->repo->getModel(); |
|
56
|
|
|
$new->fill($data); |
|
57
|
|
|
$new->save(); |
|
58
|
|
|
|
|
59
|
|
|
if (isset($attributes['selected_attribute_ids'])) { |
|
60
|
|
|
foreach ($attributes['selected_attribute_ids'] as $row) { |
|
61
|
|
|
$newData = array( |
|
62
|
|
|
'attribute_id' => $row, |
|
63
|
|
|
'product_attribute_id' => $new->id, |
|
64
|
|
|
|
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
$ok = new ProductAttributeCombination; |
|
68
|
|
|
$ok->fill($newData); |
|
69
|
|
|
$ok->save(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $new; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function updateById(array $attributes, $productId, $productAttributeId) |
|
78
|
|
|
{ |
|
79
|
|
|
|
|
80
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
|
|
|
|
|
|
81
|
|
|
$attributes['product_id'] = $productId; |
|
82
|
|
|
$model = $this->find($productAttributeId); |
|
83
|
|
|
|
|
84
|
|
|
if (count($attributes) > 0) { |
|
85
|
|
|
$model->fill($attributes); |
|
86
|
|
|
$model->save(); |
|
87
|
|
|
|
|
88
|
|
|
$model->combinations()->delete(); |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
$check = $this->repo->getModelAttributeCombination()->leftJoin($this->repo->getModel()->getTable(), $this->repo->getModel()->getTable().'.id', '=', $this->repo->getModelAttributeCombination()->getTable().'.product_attribute_id') |
|
92
|
|
|
->where($this->repo->getModel()->getTable().'.product_id', '=', $attributes['product_id']); |
|
93
|
|
|
|
|
94
|
|
|
if (isset($attributes['selected_attribute_ids'])) { |
|
95
|
|
|
$check->where(function ($query) use ($attributes) { |
|
96
|
|
|
$query->whereIn($this->repo->getModelAttributeCombination()->getTable().'.attribute_id', $attributes['selected_attribute_ids']); |
|
97
|
|
|
}); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($check->get()->count()) { |
|
101
|
|
|
$newData = array(); |
|
102
|
|
|
|
|
103
|
|
|
foreach ($check->get() as $row) { |
|
104
|
|
|
$newData[$row['product_attribute_id']] = $row->productAttribute->combinations->toArray(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if(isset($attributes['selected_attribute_ids'])) { |
|
108
|
|
|
foreach ($newData as $row) { |
|
109
|
|
|
if (count($row) == count($attributes['selected_attribute_ids'])) { |
|
110
|
|
|
$i = 0; |
|
111
|
|
|
foreach ($row as $newRow) { |
|
112
|
|
|
if (in_array($newRow['attribute_id'], $attributes['selected_attribute_ids'])) { |
|
113
|
|
|
$i++; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if (count($row) == $i) { |
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
if (isset($attributes['selected_attribute_ids'])) { |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
foreach ($attributes['selected_attribute_ids'] as $row) { |
|
130
|
|
|
$newData = array( |
|
131
|
|
|
'attribute_id' => $row, |
|
132
|
|
|
'product_attribute_id' => $model->id, |
|
133
|
|
|
|
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
|
|
$ok = new ProductAttributeCombination; |
|
137
|
|
|
$ok->fill($newData); |
|
138
|
|
|
$ok->save(); |
|
139
|
|
|
$ok->refresh(); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $model; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
public function increaseAmounts($products) |
|
149
|
|
|
{ |
|
150
|
|
|
if ($products->count()) { |
|
151
|
|
|
foreach ($products as $product) { |
|
152
|
|
|
if ($product->product_attribute_id) { |
|
153
|
|
|
$model = $this->find($product->product_attribute_id); |
|
154
|
|
|
$attributes = array( |
|
155
|
|
|
'amount' => $model->amount + $product->amount |
|
156
|
|
|
); |
|
157
|
|
|
|
|
158
|
|
|
$model->fill($attributes); |
|
159
|
|
|
$model->save(); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function reduceAmounts($products) |
|
166
|
|
|
{ |
|
167
|
|
|
if ($products->count()) { |
|
168
|
|
|
foreach ($products as $product) { |
|
169
|
|
|
if ($product->product_attribute_id) { |
|
170
|
|
|
$model = $this->find($product->product_attribute_id); |
|
171
|
|
|
$attributes = array( |
|
172
|
|
|
'amount' => $model->amount - $product->amount |
|
173
|
|
|
); |
|
174
|
|
|
|
|
175
|
|
|
$model->fill($attributes); |
|
176
|
|
|
$model->save(); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
public function selectAllByProductCategoryId($productCategoryId, $shopId) { |
|
184
|
|
|
return $this->repo->selectAllByProductCategoryId($productCategoryId, $shopId); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function generatePulldowns($product, $productAttributeId, $attributeLeadingGroup = false, $secondAttributeId = false) |
|
188
|
|
|
{ |
|
189
|
|
|
$defaultOption = array(); |
|
190
|
|
|
$check = array(); |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
//create all pulldowns |
|
193
|
|
|
foreach ($product->attributes as $row) { |
|
194
|
|
|
if ($row['combinations']) { |
|
195
|
|
|
foreach ($row['combinations'] as $key => $value) { |
|
196
|
|
|
$newPullDowns[$value->attribute->attributeGroup->title][$value->attribute->id] = $value->attribute->value; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
if(!$productAttributeId AND $attributeLeadingGroup AND isset($newPullDowns[$attributeLeadingGroup->title])) { |
|
202
|
|
|
$productAttributeId = key($newPullDowns[$attributeLeadingGroup->title]); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
$productAttributeResultWithAttributeId = $this->repo->getProductAttribute($product, $productAttributeId); |
|
206
|
|
|
|
|
207
|
|
|
if ($productAttributeResultWithAttributeId->get()->first()) { |
|
208
|
|
|
foreach ($productAttributeResultWithAttributeId->get()->first()->combinations as $combination) { |
|
209
|
|
|
$defaultOption[$combination->attribute->attributeGroup->title][$combination->attribute->id] = $combination->attribute->value; |
|
210
|
|
|
} |
|
211
|
|
|
} else { |
|
212
|
|
|
$productAttributeId = false; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$productAttributeResultWithAttributeId = $productAttributeResultWithAttributeId->get(); |
|
216
|
|
|
|
|
217
|
|
|
if ($productAttributeResultWithAttributeId) { |
|
218
|
|
|
|
|
219
|
|
|
foreach ($productAttributeResultWithAttributeId as $row) { |
|
220
|
|
|
if ($row['combinations']) { |
|
221
|
|
|
foreach ($row['combinations'] as $key => $value) { |
|
222
|
|
|
$defaultOption[$value->attribute->attributeGroup->title][$value->attribute->id] = $value->attribute->value; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
$defaultPulldown = array(); |
|
|
|
|
|
|
229
|
|
|
if ($attributeLeadingGroup AND isset($newPullDowns[$attributeLeadingGroup->title])) { |
|
230
|
|
|
$defaultOption[$attributeLeadingGroup->title] = $newPullDowns[$attributeLeadingGroup->title]; |
|
231
|
|
|
$newPullDowns = $defaultOption; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
if ($attributeLeadingGroup AND isset($defaultOption[$attributeLeadingGroup->title])) { |
|
235
|
|
|
$defaultPulldown = $newPullDowns[$attributeLeadingGroup->title]; |
|
|
|
|
|
|
236
|
|
|
$defaultPulldownFirstKey = key($newPullDowns[$attributeLeadingGroup->title]); |
|
|
|
|
|
|
237
|
|
|
unset($newPullDowns[$attributeLeadingGroup->title]); |
|
238
|
|
|
$newPullDowns = array_merge(array($attributeLeadingGroup->title => $defaultPulldown), $newPullDowns); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
$productAttribute = $this->repo->getProductAttribute($product, $productAttributeId, $secondAttributeId)->first(); |
|
242
|
|
|
|
|
243
|
|
|
return array('productAttribute' => $productAttribute, 'productAttributeId' => $productAttributeId, 'defaultOption' => $defaultOption, 'newPullDowns' => $newPullDowns); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths