Benjamin Roth authored on07/11/2024 13:34:07
Showing1 changed files
... ...
@@ -26,6 +26,7 @@ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
26 26
 use Symfony\Component\Serializer\Serializer;
27 27
 use Symfony\Component\Serializer\SerializerInterface;
28 28
 use vonRotenberg\ShopwareApiBundle\API\Shopware;
29
+use vonRotenberg\ShopwareApiBundle\Helper\ShopwareMappings;
29 30
 use vonRotenberg\WmfgoCevisioBundle\Model\Import\ProductModel;
30 31
 
31 32
 class ShopwareImportProductsJob extends AbstractController
... ...
@@ -36,9 +37,12 @@ class ShopwareImportProductsJob extends AbstractController
36 37
     /** @var SerializerInterface */
37 38
     private $serializer;
38 39
 
39
-    /** @var SerializerInterface */
40
+    /** @var Shopware */
40 41
     private $shopware;
41 42
 
43
+    /** @var ShopwareMappings */
44
+    private $mappings;
45
+
42 46
     private $csvContext = [];
43 47
 
44 48
     /** @var Finder */
... ...
@@ -53,12 +57,13 @@ class ShopwareImportProductsJob extends AbstractController
53 57
      */
54 58
     private $rdb;
55 59
 
56
-    public function __construct(Finder $finder, Shopware $shopware, Connection $db, LoggerInterface $logger)
60
+    public function __construct(Finder $finder, Shopware $shopware, ShopwareMappings $mappings, Connection $db, LoggerInterface $logger)
57 61
     {
58 62
         $this->finder = $finder;
59 63
         $this->db = $db;
60 64
         $this->logger = $logger;
61 65
         $this->shopware = $shopware;
66
+        $this->mappings = $mappings;
62 67
 
63 68
         $encoder      = [new CsvEncoder()];
64 69
         $normalizer   = [new ObjectNormalizer()];
... ...
@@ -102,7 +107,149 @@ class ShopwareImportProductsJob extends AbstractController
102 107
                 $Product = $this->serializer->denormalize($row,ProductModel::class);
103 108
 
104 109
                 $arrData = [
110
+                    'productNumber' => $Product->getSku(),
105 111
                     'taxId' => '018e65c0485071508949c072f8dc18bd',
112
+                    'stock' => 999999,
113
+                    'name' => 'vrImport2__'.$Product->getName(),
114
+                    'active' => (bool) $Product->getPublished(),
115
+                    'isCloseout' => true,
116
+                    'purchaseUnit' => (float) $Product->getBaseprice(),
117
+                    'referenceUnit' => 1,
118
+                    'unitId' => $this->mappings->getUnitIdByName('Liter')
119
+                ];
120
+
121
+                if ($Product->getEan()) $arrData['ean'] = $Product->getEan();
122
+                if ($Product->getShipping_weight()) $arrData['weight'] = $Product->getShipping_weight();
123
+                if ($Product->getBesonderheit()) $arrData['description'] = $Product->getBesonderheit();
124
+
125
+                // Custom fields handling
126
+                $arrCustomFields = [];
127
+                if (($customFieldVal = $Product->GetJahrgang())) {
128
+                    $arrCustomFields['custom_wine_attributes_jahrgang'] = $customFieldVal;
129
+                }
130
+
131
+                if (($customFieldVal = $Product->getAlkohol())) {
132
+                    $arrCustomFields['custom_wine_attributes_alkohol'] = $customFieldVal;
133
+                }
134
+
135
+                if (($customFieldVal = $Product->getSaeure())) {
136
+                    $arrCustomFields['custom_wine_attributes_saeure'] = $customFieldVal;
137
+                }
138
+
139
+                if (($customFieldVal = $Product->getrestzucker())) {
140
+                    $arrCustomFields['custom_wine_attributes_restzucker'] = $customFieldVal;
141
+                }
142
+
143
+                if (($customFieldVal = $Product->getZusatz())) {
144
+                    $arrCustomFields['custom_wine_attributes_zusatz'] = $customFieldVal;
145
+                }
146
+
147
+                if (($customFieldVal = $Product->getAllergene())) {
148
+                    $arrCustomFields['custom_wine_attributes_allergene'] = $customFieldVal;
149
+                }
150
+
151
+                if (($customFieldVal = $Product->getTrinktemperatur())) {
152
+                    $arrCustomFields['custom_wine_attributes_trinktemperatur'] = $customFieldVal;
153
+                }
154
+
155
+                if (($customFieldVal = $Product->getSpeiseempfehlung())) {
156
+                    $arrCustomFields['custom_wine_attributes_speiseempfehlung'] = $customFieldVal;
157
+                }
158
+
159
+                if (stripos($Product->getErzeuger(),'weinmanufaktur')) {
160
+                    $arrCustomFields['custom_wine_attributes_erzeuger'] = 'WMFGO';
161
+                }
162
+
163
+                if (stripos($Product->getAnbauregion(),'Baden')) {
164
+                    $arrCustomFields['custom_wine_attributes_anbauregion'] = 'Baden';
165
+                }
166
+
167
+                if (stripos($Product->getUrsprungsland(),'weinmanufaktur')) {
168
+                    $arrCustomFields['custom_wine_attributes_ursprungsland'] = 'DE';
169
+                }
170
+
171
+                if (count($arrCustomFields))
172
+                {
173
+                    $arrData['customFields'] = $arrCustomFields;
174
+                }
175
+
176
+                // Properties handling
177
+                $arrProperties = [];
178
+
179
+                if (
180
+                    ($propertyVal = $Product->getLage())
181
+                    && ($propertyGroupId = $this->mappings->getPropertyGroupIdByName('Einzellage')) !== null)
182
+                {
183
+                    $arrProperties[] = [
184
+                        'id' => $this->mappings->getPropertyIdByNameAndGroup($propertyVal,$propertyGroupId, true),
185
+                        'groupId' => $propertyGroupId,
186
+                        'name' => $propertyVal
187
+                    ];
188
+                }
189
+
190
+                if (
191
+                    ($propertyVal = $Product->getQualitaet())
192
+                    && ($propertyGroupId = $this->mappings->getPropertyGroupIdByName('Qualität')) !== null
193
+                    && ($propertyId = $this->mappings->getPropertyIdByNameAndGroup($propertyVal,$propertyGroupId, true)) !== null)
194
+                {
195
+                    $arrProperties[] = [
196
+                        'id' => $propertyId,
197
+                        'groupId' => $propertyGroupId,
198
+                        'name' => $propertyVal
199
+                    ];
200
+                }
201
+
202
+                if (
203
+                    ($propertyVal = $Product->getFarbe())
204
+                    && ($propertyGroupId = $this->mappings->getPropertyGroupIdByName('Farbe')) !== null)
205
+                {
206
+                    $arrProperties[] = [
207
+                        'id' => $this->mappings->getPropertyIdByNameAndGroup($propertyVal,$propertyGroupId, true),
208
+                        'groupId' => $propertyGroupId,
209
+                        'name' => $propertyVal
210
+                    ];
211
+                }
212
+
213
+                if (
214
+                    ($propertyVal = $Product->getGeschmack())
215
+                    && ($propertyGroupId = $this->mappings->getPropertyGroupIdByName('Geschmack')) !== null)
216
+                {
217
+                    $arrProperties[] = [
218
+                        'id' => $this->mappings->getPropertyIdByNameAndGroup($propertyVal,$propertyGroupId, true),
219
+                        'groupId' => $propertyGroupId,
220
+                        'name' => $propertyVal
221
+                    ];
222
+                }
223
+
224
+                if (
225
+                    ($propertyVal = $Product->getRebsorte())
226
+                    && ($propertyGroupId = $this->mappings->getPropertyGroupIdByName('Rebsorte')) !== null)
227
+                {
228
+                    $arrProperties[] = [
229
+                        'id' => $this->mappings->getPropertyIdByNameAndGroup($propertyVal,$propertyGroupId, true),
230
+                        'groupId' => $propertyGroupId,
231
+                        'name' => $propertyVal
232
+                    ];
233
+                }
234
+
235
+                if (
236
+                    ($propertyVal = $Product->getWeinlinie())
237
+                    && ($propertyGroupId = $this->mappings->getPropertyGroupIdByName('Weinlinie')) !== null)
238
+                {
239
+                    $arrProperties[] = [
240
+                        'id' => $this->mappings->getPropertyIdByNameAndGroup($propertyVal,$propertyGroupId, true),
241
+                        'groupId' => $propertyGroupId,
242
+                        'name' => $propertyVal
243
+                    ];
244
+                }
245
+
246
+                if (count($arrProperties))
247
+                {
248
+                    $arrData['properties'] = $arrProperties;
249
+                }
250
+
251
+
252
+/*
106 253
                     'ean' => $Product->getEan(),
107 254
                     'price' => [
108 255
                         [
... ...
@@ -112,22 +259,29 @@ class ShopwareImportProductsJob extends AbstractController
112 259
                             'linked' => true
113 260
                         ]
114 261
                     ],
115
-                    'stock' => 9999999,
116
-                    'name' => 'vrImport__'.$Product->getName(),
117 262
                     'customFields' => [
118 263
                         'custom_wine_attributes_jahrgang' => $Product->getJahrgang()
264
+                    ],
265
+                    'properties' => [
266
+                        [
267
+                            'id' => $this->mappings->getPropertyIdByNameAndGroup($Product->getLage(),'Einzellage'),
268
+                            'groupId' => $this->mappings->getPropertyGroupIdByName('Einzellage'),
269
+                            'name' => $Product->getLage()
270
+                        ]
119 271
                     ]
120 272
                 ];
121
-
273
+*/
122 274
                 if (!$this->shopware->addOrUpdateProductBySku($Product->getSku(), $arrData))
123 275
                 {
124 276
                     if ($isCli) $io->error('Could not update/import Product ' . $Product->getSku());
277
+                    dump($arrData);
125 278
                 }
126 279
 
127 280
                 if ($isCli)
128 281
                 {
129 282
                     $io->progressAdvance();
130 283
                 }
284
+                return;
131 285
             }
132 286
             if ($isCli) $io->progressFinish();
133 287
         }