/
Variant.php
1091 lines (919 loc) · 33.6 KB
/
Variant.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
237
238
239
240
241
242
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Shopware 5
* Copyright (c) shopware AG
*
* According to our dual licensing model, this program can be used either
* under the terms of the GNU Affero General Public License, version 3,
* or under a proprietary license.
*
* The texts of the GNU Affero General Public License with an additional
* permission and of our proprietary license can be found at and
* in the LICENSE file you have received along with this program.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* "Shopware" is a registered trademark of shopware AG.
* The licensing of the program under the AGPLv3 does not imply a
* trademark license. Therefore any rights, title and interest in
* our trademarks remain entirely with us.
*/
namespace Shopware\Components\Api\Resource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Query;
use RuntimeException;
use Shopware\Components\Api\BatchInterface;
use Shopware\Components\Api\Exception\CustomValidationException;
use Shopware\Components\Api\Exception\NotFoundException;
use Shopware\Components\Api\Exception\ParameterMissingException;
use Shopware\Components\Api\Exception\ValidationException;
use Shopware\Models\Article\Article as ProductModel;
use Shopware\Models\Article\Configurator\Group;
use Shopware\Models\Article\Configurator\Option;
use Shopware\Models\Article\Detail;
use Shopware\Models\Article\Esd;
use Shopware\Models\Article\EsdSerial;
use Shopware\Models\Article\Image;
use Shopware\Models\Article\Image\Mapping;
use Shopware\Models\Article\Image\Rule;
use Shopware\Models\Article\Price;
use Shopware\Models\Article\Repository;
use Shopware\Models\Article\Unit;
use Shopware\Models\Attribute\Article as ArticleAttributeModel;
use Shopware\Models\Customer\Group as CustomerGroup;
use Shopware\Models\Media\Media as MediaModel;
use Shopware\Models\Tax\Tax;
/**
* Variant API Resource
*/
class Variant extends Resource implements BatchInterface
{
/**
* @return Repository
*/
public function getRepository()
{
return $this->getManager()->getRepository(Detail::class);
}
/**
* @param string $number
*
* @return array|Detail
*/
public function getOneByNumber($number, array $options = [])
{
$id = $this->getIdFromNumber($number);
return $this->getOne($id, $options);
}
/**
* @param int $id
*
* @throws NotFoundException
* @throws ParameterMissingException
*
* @return array|Detail
*/
public function getOne($id, array $options = [])
{
$this->checkPrivilege('read');
if (empty($id)) {
throw new ParameterMissingException('id');
}
$builder = $this->getRepository()->getVariantDetailQuery();
$builder->addSelect('article')
->andWhere('variants.id = :variantId')
->addOrderBy('variants.id', 'ASC')
->addOrderBy('customerGroup.id', 'ASC')
->addOrderBy('prices.from', 'ASC')
->setParameter('variantId', $id);
$variant = $builder->getQuery()->getOneOrNullResult($this->getResultMode());
if (!$variant) {
throw new NotFoundException(sprintf('Variant by id %d not found', $id));
}
if (\is_array($variant) && !empty($options['considerTaxInput'])) {
$variant = $this->considerTaxInput($variant);
}
return $variant;
}
/**
* @param int $offset
* @param int $limit
*
* @return array
*/
public function getList($offset = 0, $limit = 25, array $criteria = [], array $orderBy = [], array $options = [])
{
$this->checkPrivilege('read');
$builder = $this->getRepository()->createQueryBuilder('detail');
$builder->addSelect(['prices', 'attribute', 'partial article.{id,name,active,taxId}', 'customerGroup'])
->leftJoin('detail.prices', 'prices')
->innerJoin('prices.customerGroup', 'customerGroup')
->leftJoin('detail.attribute', 'attribute')
->innerJoin('detail.article', 'article')
->addFilter($criteria)
->addOrderBy($orderBy)
->setFirstResult($offset)
->setMaxResults($limit);
/** @var Query<Detail|array<string, mixed>> $query */
$query = $builder->getQuery();
$query->setHydrationMode($this->getResultMode());
$paginator = $this->getManager()->createPaginator($query);
// Returns the total count of the query
$totalResult = $paginator->count();
// Returns the product data
$variants = iterator_to_array($paginator);
if ($this->getResultMode() === self::HYDRATE_ARRAY && !empty($options['considerTaxInput'])) {
foreach ($variants as &$variant) {
if (\is_array($variant)) {
$variant = $this->considerTaxInput($variant);
}
}
}
return ['data' => $variants, 'total' => $totalResult];
}
/**
* Little helper function for the ...ByNumber methods
*
* @param string $number
*
* @throws NotFoundException
* @throws ParameterMissingException
*
* @return int
*/
public function getIdFromNumber($number)
{
if (empty($number)) {
throw new ParameterMissingException('id');
}
$productVariant = $this->getRepository()->findOneBy(['number' => $number]);
if (!$productVariant) {
throw new NotFoundException(sprintf('Variant by number %s not found', $number));
}
return $productVariant->getId();
}
/**
* @param string $number
*
* @throws ParameterMissingException
* @throws NotFoundException
*
* @return Detail
*/
public function deleteByNumber($number)
{
$id = $this->getIdFromNumber($number);
return $this->delete($id);
}
/**
* @param int $id
*
* @throws ParameterMissingException
* @throws NotFoundException
*
* @return Detail
*/
public function delete($id)
{
$this->checkPrivilege('delete');
if (empty($id)) {
throw new ParameterMissingException('id');
}
$productVariant = $this->getRepository()->find($id);
if (!$productVariant instanceof Detail) {
throw new NotFoundException(sprintf('Variant by id %d not found', $id));
}
if ($productVariant->getKind() === 1) {
$productVariant->getArticle()->setMainDetail(null);
}
$this->getManager()->remove($productVariant);
$this->flush();
return $productVariant;
}
/**
* Convenience method to update a variant by number
*
* @param string $number
*
* @return Detail
*/
public function updateByNumber($number, array $params)
{
$id = $this->getIdFromNumber($number);
return $this->update($id, $params);
}
/**
* Updates a single variant entity.
*
* @param int $id
*
* @throws ValidationException
* @throws NotFoundException
* @throws ParameterMissingException
*
* @return Detail
*/
public function update($id, array $params)
{
if (empty($id)) {
throw new ParameterMissingException('id');
}
$variant = $this->getRepository()->find($id);
if (!$variant instanceof Detail) {
throw new NotFoundException(sprintf('Variant by id %d not found', $id));
}
$variant = $this->internalUpdate($id, $params, $variant->getArticle());
$violations = $this->getManager()->validate($variant);
if ($violations->count() > 0) {
throw new ValidationException($violations);
}
$this->flush();
return $variant;
}
/**
* Creates a new variant for an product.
* This function requires an articleId in the params parameter.
*
* @throws ValidationException
* @throws NotFoundException
* @throws ParameterMissingException
*
* @return Detail
*/
public function create(array $params)
{
$productId = $params['articleId'];
if (empty($productId)) {
throw new ParameterMissingException('Passed parameter array does not contain an articleId property');
}
$product = $this->getManager()->find(ProductModel::class, $productId);
if (!$product) {
throw new NotFoundException(sprintf('Product by id %d not found', $productId));
}
$variant = $this->internalCreate($params, $product);
$violations = $this->getManager()->validate($variant);
if ($violations->count() > 0) {
throw new ValidationException($violations);
}
$this->getManager()->persist($variant);
$this->flush();
return $variant;
}
/**
* Update function for the internal usage of the rest api.
* Used from the 'Article' resource. This function supports
* to pass an updated product entity which isn't updated in the database.
* Required for the 'Article' resource if the product data is already updated
* in the entity but not in the database.
*
* @param int $id
*
* @throws NotFoundException
* @throws ParameterMissingException
*
* @return Detail
*/
public function internalUpdate($id, array $data, ProductModel $article)
{
if (empty($id)) {
throw new ParameterMissingException('id');
}
$variant = $this->getRepository()->find($id);
if (!$variant instanceof Detail) {
throw new NotFoundException(sprintf('Variant by id %d not found', $id));
}
$variant->setArticle($article);
$data = $this->prepareData($data, $article, $variant);
$variant->fromArray($data);
return $variant;
}
/**
* Create function for the internal usage of the rest api.
* Used from the 'Article' resource. This function supports
* to pass an updated product entity which isn't updated in the database.
* Required for the 'Article' resource if the product data is already updated
* in the entity but not in the database.
*
* @throws ValidationException
*
* @return Detail
*/
public function internalCreate(array $data, ProductModel $article)
{
$variant = new Detail();
$variant->setKind(2);
$variant->setArticle($article);
$data = $this->prepareData($data, $article, $variant);
$variant->fromArray($data);
$this->getManager()->persist($variant);
return $variant;
}
/**
* Interface which allows to use the data preparation in the 'Article' resource for the main variant.
*
* @return array|mixed
*/
public function prepareMainVariantData(array $data, ProductModel $article, Detail $variant)
{
return $this->prepareData($data, $article, $variant);
}
/**
* Helper function which creates a variant image for the passed product image.
*
* @return Image
*/
public function createVariantImage(Image $articleImage, Detail $variant)
{
$variantImage = new Image();
$variantImage->setParent($articleImage);
$variantImage->setArticleDetail($variant);
$variantImage->setPosition($articleImage->getPosition());
$variantImage->setMain($articleImage->getMain());
$variantImage->setExtension($articleImage->getExtension());
return $variantImage;
}
/**
* @param Collection|array $options
*
* @return Mapping
*/
public function createImageMappingForOptions($options, Image $image)
{
$mapping = new Mapping();
$mapping->setImage($image);
foreach ($options as $option) {
$rule = new Rule();
$rule->setMapping($mapping);
$rule->setOption($option);
$mapping->getRules()->add($rule);
}
$image->getMappings()->add($mapping);
return $mapping;
}
public function getIdByData($data)
{
$id = null;
if (isset($data['id'])) {
$id = $data['id'];
} elseif (isset($data['number'])) {
try {
$id = $this->getIdFromNumber($data['number']);
} catch (NotFoundException $e) {
return false;
}
}
if (!$id) {
return false;
}
$model = $this->getManager()->find(Detail::class, $id);
if ($model) {
return $id;
}
return false;
}
/**
* @return Article
*/
protected function getArticleResource()
{
return $this->getContainer()->get(Article::class);
}
/**
* @return Media
*/
protected function getMediaResource()
{
return $this->getContainer()->get(Media::class);
}
/**
* Resolves the association data for a single variant.
*
* @throws CustomValidationException
*
* @return array|mixed
*/
protected function prepareData(array $data, ProductModel $article, Detail $variant)
{
$data = $this->prepareUnitAssociation($data);
if (!empty($data['prices'])) {
$data['prices'] = $this->preparePriceAssociation(
$data,
$article,
$variant,
$article->getTax()
);
}
if (isset($data['purchasePrice']) && \is_string($data['purchasePrice'])) {
$data['purchasePrice'] = (float) str_replace(',', '.', $data['purchasePrice']);
}
$data = $this->prepareAttributeAssociation($data, $article, $variant);
if (isset($data['configuratorOptions'])) {
$data = $this->prepareConfigurator($data, $article, $variant);
}
if (isset($data['images'])) {
$data = $this->prepareImageAssociation($data, $article, $variant);
}
if (isset($data['esd'])) {
$data = $this->prepareEsdAssociation($data, $variant);
}
if (!empty($data['number']) && $data['number'] !== $variant->getNumber()) {
// Number changed, hence make sure it does not already exist in another variant
$exists = $this->getContainer()
->get(Connection::class)
->fetchColumn('SELECT id FROM s_articles_details WHERE ordernumber = ?', [$data['number']]);
if ($exists) {
throw new CustomValidationException(sprintf('A variant with the given order number "%s" already exists.', $data['number']));
}
}
return $data;
}
/**
* Resolves the passed images array for the current variant.
* An image can be assigned to a variant over a media id of an existing product image
* or over the link property which can contain a image link.
* This image will be added automatically to the product.
*
* @param array $data
*
* @throws CustomValidationException
*
* @return array
*/
protected function prepareImageAssociation($data, ProductModel $article, Detail $variant)
{
if (empty($data['images'])) {
if ($variant->getImages()->count() > 0) {
foreach ($variant->getImages() as $image) {
$mapping = $this->getVariantMappingOfImage($image, $variant);
if ($mapping) {
$this->getManager()->remove($mapping);
}
}
}
return $data;
}
$images = $this->checkDataReplacement(
$variant->getImages(),
$data,
'images',
true
);
foreach ($data['images'] as $imageData) {
// Check if a media id was passed.
if (isset($imageData['mediaId'])) {
// First check if the media object is already assigned to the product
$image = $this->getAvailableMediaImage(
$article->getImages(),
(int) $imageData['mediaId']
);
// Media image isn't assigned to the product?
if (!$image) {
// Find the media object and convert it to an product image.
$media = $this->getManager()->find(MediaModel::class, (int) $imageData['mediaId']);
if (!$media) {
throw new CustomValidationException(sprintf('Media by id %s not found', (int) $imageData['mediaId']));
}
$image = $this->getArticleResource()->createNewArticleImage($article, $media);
}
} elseif (isset($imageData['link'])) {
// Check if an url passed and upload the passed image url and create a new product image.
$media = $this->getMediaResource()->internalCreateMediaByFileLink(
$imageData['link']
);
$image = $this->getArticleResource()->createNewArticleImage($article, $media);
} else {
throw new CustomValidationException("One of the passed variant images doesn't contain a mediaId or link property!");
}
$variantImage = $this->createVariantImage(
$image,
$variant
);
$this->createImageMappingForOptions(
$variant->getConfiguratorOptions(),
$image
);
$images->add($variantImage);
}
$data['images'] = $images;
$variant->setImages($images);
return $data;
}
/**
* Helper function which returns a single image mapping
* for the passed variant image and variant model.
*
* @return Mapping|null
*/
protected function getVariantMappingOfImage(Image $image, Detail $variant)
{
$parent = $image->getParent();
foreach ($parent->getMappings() as $mapping) {
$match = true;
foreach ($mapping->getRules() as $rule) {
$option = $this->getCollectionElementByProperty(
$variant->getConfiguratorOptions(),
'id',
$rule->getOption()->getId()
);
if (!$option) {
$match = false;
break;
}
}
if ($match) {
return $mapping;
}
}
return null;
}
/**
* @param array $data
*
* @throws CustomValidationException
*
* @return Collection
*/
protected function preparePriceAssociation($data, ProductModel $article, Detail $variant, Tax $tax)
{
$prices = $this->checkDataReplacement($variant->getPrices(), $data, 'prices', true);
foreach ($data['prices'] as &$priceData) {
$price = $this->getOneToManySubElement(
$prices,
$priceData,
Price::class
);
if (empty($priceData['customerGroupKey'])) {
$priceData['customerGroupKey'] = 'EK';
}
if (empty($priceData['from']) && (int) $price->getFrom() === 0) {
$priceData['from'] = 1;
}
// Load the customer group of the price definition
$customerGroup = $this->getManager()
->getRepository(CustomerGroup::class)
->findOneBy(['key' => $priceData['customerGroupKey']]);
if (!$customerGroup instanceof CustomerGroup) {
throw new CustomValidationException(sprintf('Customer Group by key %s not found', $priceData['customerGroupKey']));
}
$priceData['customerGroup'] = $customerGroup;
$priceData['article'] = $article;
$priceData['detail'] = $variant;
$priceData = $this->mergePriceData($priceData, $tax);
$price->fromArray($priceData);
}
return $prices;
}
/**
* Resolves the passed configuratorOptions parameter for a single variant.
* Each passed configurator option, has to be configured in the product configurator set.
*
* @throws CustomValidationException
*
* @return array
*/
protected function prepareConfigurator(array $data, ProductModel $article, Detail $variant)
{
if (!$article->getConfiguratorSet()) {
throw new CustomValidationException('A configurator set has to be defined');
}
$availableGroups = $article->getConfiguratorSet()->getGroups();
$options = new ArrayCollection();
foreach ($data['configuratorOptions'] as $optionData) {
$availableGroup = $this->getAvailableGroup($availableGroups, [
'id' => $optionData['groupId'] ?? null,
'name' => $optionData['group'] ?? null,
]);
// Group is in the product configurator set configured?
if (!$availableGroup) {
continue;
}
// Check if the option is available in the configured product configurator set.
$option = $this->getAvailableOption($availableGroup->getOptions(), [
'id' => $optionData['optionId'] ?? null,
'name' => $optionData['option'] ?? null,
]);
if (!$option) {
if (!$optionData['option']) {
throw new CustomValidationException('A new configurator option requires a name');
}
$option = new Option();
$option->setPosition(0);
if (\array_key_exists('position', $optionData)) {
$option->setPosition((int) $optionData['position']);
}
$option->setName($optionData['option']);
$option->setGroup($availableGroup);
$this->getManager()->persist($option);
}
$options->add($option);
}
$data['configuratorOptions'] = $options;
$variant->setConfiguratorOptions($options);
return $data;
}
/**
* @param array $data
*
* @return array
*/
protected function prepareAttributeAssociation($data, ProductModel $article, Detail $variant)
{
if (!isset($data['attribute']) && !$variant->getAttribute()) {
// Create empty attributes if none provided
$data['attribute'] = new ArticleAttributeModel();
}
return $data;
}
/**
* Prepares the base variant data to save over doctrine.
* Resolves the foreign keys for the passed unit data.
*
* @param array $data
*
* @throws CustomValidationException
*
* @return array
*/
protected function prepareUnitAssociation($data)
{
// If unit id passed, assign existing unit.
if (!empty($data['unitId'])) {
$data['unit'] = $this->getManager()->find(Unit::class, $data['unitId']);
if (empty($data['unit'])) {
throw new CustomValidationException(sprintf('Unit by id %s not found', $data['unitId']));
}
// New unit data send? create new unit for this variant
} elseif (!empty($data['unit'])) {
$data['unit'] = $this->updateUnitReference($data['unit']);
}
return $data;
}
/**
* Try to find an existing unit by the passed parameters.
* If no unit reference found, the function creates a new Unit entity.
* The passed unit data will be assigned to the created or found Unit entity.
*
* @param array $unitData
*
* @throws CustomValidationException
*
* @return Unit
*/
protected function updateUnitReference($unitData)
{
$unitRepository = $this->getManager()->getRepository(Unit::class);
// Try to find an existing unit by the passed conditions "id", "name" or "unit"
$unit = $unitRepository->findOneBy(
$this->getUnitFindCondition($unitData)
);
// Unit identifier send and unit not found? throw exception => Not allowed to create a new unit in this case
if (!$unit && isset($unitData['id'])) {
throw new CustomValidationException(sprintf('Unit by id %s not found', $unitData['id']));
}
// To create a new unit, the unit name and unit is required. Otherwise we throw an exception
if (!$unit && isset($unitData['name'], $unitData['unit'])) {
$unit = new Unit();
} elseif (!$unit) {
throw new CustomValidationException(sprintf('To create a unit you need to pass `name` and `unit`'));
}
$unit->fromArray($unitData);
return $unit;
}
/**
* @param array<string, mixed> $variant
*
* @throws CustomValidationException
*
* @return array<string, mixed>
*/
private function considerTaxInput(array $variant): array
{
$tax = Shopware()->Db()->fetchOne(
'SELECT tax
FROM s_core_tax
INNER JOIN s_articles
ON s_articles.taxID = s_core_tax.id
AND s_articles.id = :articleId',
[':articleId' => $variant['articleId']]
);
if (empty($tax)) {
throw new CustomValidationException(sprintf('No product tax configured for variant: %s', $variant['id']));
}
$variant['prices'] = $this->getArticleResource()->getTaxPrices(
$variant['prices'],
(float) $tax
);
return $variant;
}
/**
* @param Collection<int, Image> $availableImages
*/
private function getAvailableMediaImage(Collection $availableImages, int $mediaId): ?Image
{
foreach ($availableImages as $image) {
if ($image->getMedia()->getId() === $mediaId) {
return $image;
}
}
return null;
}
/**
* Calculates and merges the numeric values of the Price entity
*
* @param array<string, mixed> $priceData
*
* @throws CustomValidationException
*
* @return array<string, mixed>
*/
private function mergePriceData(array $priceData, Tax $tax): array
{
if (\array_key_exists('from', $priceData)) {
$priceData['from'] = (int) $priceData['from'];
if ($priceData['from'] <= 0) {
throw new CustomValidationException(sprintf('Invalid Price "from" value'));
}
}
if (\array_key_exists('to', $priceData)) {
$priceData['to'] = (int) $priceData['to'];
// if the "to" value isn't numeric, set the place holder "beliebig"
if ($priceData['to'] <= 0) {
$priceData['to'] = 'beliebig';
}
}
foreach (['price', 'pseudoPrice', 'percent'] as $key) {
if (\array_key_exists($key, $priceData)) {
$priceData[$key] = (float) str_replace(',', '.', $priceData[$key]);
}
}
if ($priceData['customerGroup']->getTaxInput()) {
if (\array_key_exists('price', $priceData)) {
$priceData['price'] = $priceData['price'] / (100 + (float) $tax->getTax()) * 100;
}
if (\array_key_exists('pseudoPrice', $priceData)) {
$priceData['pseudoPrice'] = $priceData['pseudoPrice'] / (100 + (float) $tax->getTax()) * 100;
}
if (\array_key_exists('regulationPrice', $priceData)) {
$priceData['regulationPrice'] = $priceData['regulationPrice'] / (100 + (float) $tax->getTax()) * 100;
}
}
return $priceData;
}
/**
* Checks if the passed group data is already existing in the passed array collection.
* The group data are checked for "id" and "name".
*
* @param Collection<int, Group> $availableGroups
* @param array<string, mixed> $groupData
*/
private function getAvailableGroup(Collection $availableGroups, array $groupData): ?Group
{
// Convert string to lower case to avoid problems with case insensitivity in database
// vs case sensitivity in PHP
$groupName = mb_strtolower($groupData['name'] ?? '');
foreach ($availableGroups as $availableGroup) {
if (($groupData['id'] !== null && (int) $availableGroup->getId() === (int) $groupData['id'])
|| (mb_strtolower($availableGroup->getName()) === $groupName && $groupData['name'] !== null)
) {
return $availableGroup;
}
}
return null;
}
/**
* Checks if the passed option data is already existing in the passed array collection.
* The option data are checked for "id" and "name".
*
* @param Collection<int, Option> $availableOptions
* @param array<string, mixed> $optionData
*/
private function getAvailableOption(Collection $availableOptions, array $optionData): ?Option
{
// Convert string to lower case to avoid problems with case insensitivity in database
// vs case sensitivity in PHP
$optionName = mb_strtolower($optionData['name'] ?? '');
foreach ($availableOptions as $availableOption) {
if ((mb_strtolower($availableOption->getName()) === $optionName && $optionData['name'] !== null)
|| ((int) $availableOption->getId() === (int) $optionData['id'] && $optionData['id'] !== null)
) {
return $availableOption;
}
}
return null;
}
/**
* Helper function returns the findOneBy condition
* for the passed unit data.
*
* @throws CustomValidationException
*
* @return array
*/
private function getUnitFindCondition(array $data)
{
if (isset($data['id'])) {
return ['id' => $data['id']];
}
if (isset($data['unit'])) {
return ['unit' => $data['unit']];
}
if (isset($data['name'])) {
return ['name' => $data['name']];
}
throw new CustomValidationException(sprintf('To create a unit you need to pass `name` and `unit`'));
}
/**
* @param array $data
*
* @return array
*/
private function prepareEsdAssociation($data, Detail $variant)
{
if (\is_array($data['esd'])) {
$esd = $variant->getEsd();
// Use already uploaded download file
if (!isset($data['esd']['reuse'])) {
$data['esd']['reuse'] = false;
}
if (!$esd) {
$esd = new Esd();