@@ -53,14 +53,14 @@ public function store(Request $request)
5353 Log::info ('Product store request received ' , ['request_all ' => $ request ->all ()]);
5454 Log::info ('Files in request ' , ['request_files ' => $ request ->files ->all ()]);
5555
56-
5756 $ user = Auth::guard ('api ' )->user ();
5857 $ shop = Shop::where ('user_id ' , $ user ->id )->first ();
5958
6059 if (!$ shop ) {
6160 throw new \Exception ("Shop not found for the authenticated user. " );
6261 }
6362
63+ // Mise à jour du niveau du shop si c'est le premier produit
6464 if ($ shop ->products ()->count () === 0 ) {
6565 $ shop ->shop_level = "3 " ;
6666 $ shop ->save ();
@@ -70,50 +70,53 @@ public function store(Request $request)
7070 $ product ->product_name = $ request ->product_name ;
7171 $ product ->product_description = $ request ->product_description ;
7272 $ product ->shop_id = $ shop ->id ;
73- $ product ->type = $ request ->type == 'simple ' ? 0 : 1 ;
73+ $ product ->type = $ request ->type == 'simple ' ? 0 : 1 ; // 0 pour simple, 1 pour variable
7474 $ product ->product_gender = $ request ->product_gender ;
7575 $ product ->whatsapp_number = $ request ->whatsapp_number ;
7676 $ product ->product_residence = $ request ->product_residence ;
77- $ product ->status = 0 ; // Default status
78-
77+ $ product ->status = 0 ; // Statut par défaut
7978
80- if ($ product ->type == 0 ) { // Simple product
79+ // Gestion du produit simple
80+ if ($ product ->type == 0 ) {
8181 $ product ->product_price = $ request ->product_price ;
8282 $ product ->product_quantity = $ request ->product_quantity ;
8383 }
8484
85+ // Gestion de l'image principale
8586 if ($ request ->hasFile ('product_profile ' )) {
8687 $ product ->product_profile = $ request ->file ('product_profile ' )->store ('product/profile ' , 'public ' );
8788 Log::info ('Product profile image stored ' , ['path ' => $ product ->product_profile ]);
8889 }
8990
90- $ product ->save ();
91+ $ product ->save (); // Sauvegarde du produit pour obtenir l'ID
9192 Log::info ('Product saved ' , ['product_id ' => $ product ->id ]);
9293
9394 GenerateProductUrlJob::dispatch ($ product ->id );
9495
95-
96+ // Gestion des prix de gros au niveau du produit (pour les produits simples ou les variations 'couleur uniquement' avec prix de gros global)
9697 if ($ request ->is_wholesale == "1 " ) {
9798 $ product ->is_wholesale = true ;
9899 if ($ request ->is_only_wholesale == "1 " ) {
99100 $ product ->is_only_wholesale = true ;
100101 }
101- $ product ->save ();
102+ $ product ->save (); // Sauvegarde pour mettre à jour is_wholesale et is_only_wholesale
103+
102104 if ($ request ->has ('wholesale_prices ' )) {
103- $ wholesalePrices = json_decode ($ request ->wholesale_prices );
104- if ($ wholesalePrices ) {
105- foreach ($ wholesalePrices as $ wholeSalePrice ) {
106- $ newWholeSalePrice = new WholeSalePrice ;
107- $ newWholeSalePrice -> min_quantity = $ wholeSalePrice -> min_quantity ;
108- $ newWholeSalePrice -> wholesale_price = $ wholeSalePrice -> wholesale_price ;
109- $ newWholeSalePrice -> product_id = $ product -> id ;
110- $ newWholeSalePrice -> save ( );
111- Log::info ('Product wholesale price saved ' , ['price_id ' => $ newWholeSalePrice ->id ]);
105+ $ wholesalePricesData = json_decode ($ request ->wholesale_prices , true );
106+ if ($ wholesalePricesData ) {
107+ foreach ($ wholesalePricesData as $ wpData ) {
108+ // Lie les prix de gros au produit directement via la relation polymorphique
109+ $ product -> wholesalePrices ()-> create ([
110+ ' min_quantity ' => $ wpData [ ' min_quantity ' ],
111+ ' wholesale_price ' => $ wpData [ ' wholesale_price ' ],
112+ ] );
113+ Log::info ('Product global wholesale price saved ' , ['product_id ' => $ product ->id , ' min_quantity ' => $ wpData [ ' min_quantity ' ] ]);
112114 }
113115 }
114116 }
115117 }
116118
119+ // Gestion des images du produit simple (non liées aux variations)
117120 if ($ request ->hasFile ('images ' )) {
118121 foreach ($ request ->file ('images ' ) as $ image ) {
119122 $ imagePath = $ image ->store ('product/images ' , 'public ' );
@@ -122,45 +125,62 @@ public function store(Request $request)
122125 }
123126 }
124127
128+ // Gestion des variations pour produit variable
125129 if ($ product ->type == 1 && $ request ->filled ('variations ' )) {
126130 $ variationsData = json_decode ($ request ->variations , true );
127131 Log::info ('Decoded variations data ' , ['variations ' => $ variationsData ]);
128132
129133 foreach ($ variationsData as $ colorGroup ) {
134+ // Vérifie si c'est une variation "couleur uniquement" ou "couleur et attribut"
135+ // En se basant sur la présence des tableaux 'sizes' ou 'shoeSizes'
136+ $ isColorAndAttribute = (isset ($ colorGroup ['sizes ' ]) && is_array ($ colorGroup ['sizes ' ]) && count ($ colorGroup ['sizes ' ]) > 0 ) ||
137+ (isset ($ colorGroup ['shoeSizes ' ]) && is_array ($ colorGroup ['shoeSizes ' ]) && count ($ colorGroup ['shoeSizes ' ]) > 0 );
138+
130139 // Création de la variation principale (couleur)
131140 $ variation = $ product ->variations ()->create ([
132141 'color_id ' => $ colorGroup ['color ' ]['id ' ],
133- 'price ' => $ colorGroup ['price ' ] ?? 0 ,
134- 'quantity ' => $ colorGroup ['quantity ' ] ?? null ,
142+ // Pour 'Couleur uniquement', le prix et la quantité sont directement sur la variation de couleur
143+ 'price ' => !$ isColorAndAttribute && isset ($ colorGroup ['price ' ]) ? $ colorGroup ['price ' ] : 0 ,
144+ 'quantity ' => !$ isColorAndAttribute && isset ($ colorGroup ['quantity ' ]) ? $ colorGroup ['quantity ' ] : null ,
135145 ]);
136146 Log::info ('ProductVariation (color) created ' , ['variation_id ' => $ variation ->id , 'color_id ' => $ colorGroup ['color ' ]['id ' ]]);
137147
138- // Gestion des images pour cette couleur
148+ // Gestion des images pour cette couleur (envoyées sous 'color_{id}_image_{index}')
139149 $ colorImageKeyPrefix = "color_ " . $ colorGroup ['color ' ]['id ' ] . "_image_ " ;
140150 $ imageIndex = 0 ;
141151 while ($ request ->hasFile ($ colorImageKeyPrefix . $ imageIndex )) {
142152 $ imageFile = $ request ->file ($ colorImageKeyPrefix . $ imageIndex );
143153 $ imagePath = $ imageFile ->store ('product/variations ' , 'public ' );
144- $ variation ->images ()->create (['image_path ' => $ imagePath ]);
154+ $ variation ->images ()->create (['image_path ' => $ imagePath ]); // Assurez-vous que ProductVariation a une relation images() morphMany
145155 Log::info ('Variation image stored ' , ['color_id ' => $ colorGroup ['color ' ]['id ' ], 'image_index ' => $ imageIndex , 'path ' => $ imagePath ]);
146156 $ imageIndex ++;
147157 }
148158
149- // Gestion des sous-variations (tailles/pointures)
150- if (isset ($ colorGroup ['sizes ' ]) && is_array ($ colorGroup ['sizes ' ])) {
151- Log::info ('Processing sizes for color group ' , ['color_id ' => $ colorGroup ['color ' ]['id ' ], 'sizes_data ' => $ colorGroup ['sizes ' ]]);
152- foreach ($ colorGroup ['sizes ' ] as $ attributeValue ) {
153- if (!$ variation ->attributesVariation ()->where ('attribute_value_id ' , $ attributeValue ['id ' ])->exists ()) {
154- $ attrVariation = $ variation ->attributesVariation ()->create ([
155- 'attribute_value_id ' => $ attributeValue ['id ' ],
156- 'quantity ' => $ attributeValue ['quantity ' ],
157- 'price ' => $ attributeValue ['price ' ],
158- ]);
159- Log::info ('ProductAttributeVariation (size) created ' , ['id ' => $ attrVariation ->id , 'attribute_value_id ' => $ attributeValue ['id ' ]]);
160-
161- if (isset ($ attributeValue ['is_wholesale ' ]) && $ attributeValue ['is_wholesale ' ] && isset ($ attributeValue ['wholesale_prices ' ]) && is_array ($ attributeValue ['wholesale_prices ' ])) {
159+ // Gestion des sous-variations (tailles/pointures/autres attributs)
160+ if ($ isColorAndAttribute ) {
161+ // Traitement des tailles
162+ if (isset ($ colorGroup ['sizes ' ]) && is_array ($ colorGroup ['sizes ' ])) {
163+ Log::info ('Processing sizes for color group ' , ['color_id ' => $ colorGroup ['color ' ]['id ' ], 'sizes_data ' => $ colorGroup ['sizes ' ]]);
164+ foreach ($ colorGroup ['sizes ' ] as $ attributeValue ) {
165+ // Crée ou met à jour la VariationAttribute
166+ $ attrVariation = $ variation ->attributesVariation ()->updateOrCreate (
167+ ['attribute_value_id ' => $ attributeValue ['id ' ]],
168+ [
169+ 'quantity ' => $ attributeValue ['quantity ' ],
170+ 'price ' => $ attributeValue ['price ' ],
171+ ]
172+ );
173+ Log::info ('VariationAttribute (size) processed ' , ['id ' => $ attrVariation ->id , 'attribute_value_id ' => $ attributeValue ['id ' ]]);
174+
175+ // Gestion des prix de gros pour cette VariationAttribute spécifique
176+ if (isset ($ attributeValue ['is_wholesale ' ]) && $ attributeValue ['is_wholesale ' ] &&
177+ isset ($ attributeValue ['wholesale_prices ' ]) && is_array ($ attributeValue ['wholesale_prices ' ])) {
178+
179+ // Supprime les anciens prix de gros pour éviter les doublons si vous utilisez updateOrCreate
180+ $ attrVariation ->wholesalePrices ()->delete ();
181+
162182 foreach ($ attributeValue ['wholesale_prices ' ] as $ wholesalePriceData ) {
163- $ attrVariation ->wholesalePrices ()->create ([
183+ $ attrVariation ->wholesalePrices ()->create ([ // Lie les prix de gros à VariationAttribute
164184 'min_quantity ' => $ wholesalePriceData ['min_quantity ' ],
165185 'wholesale_price ' => $ wholesalePriceData ['wholesale_price ' ],
166186 ]);
@@ -169,22 +189,30 @@ public function store(Request $request)
169189 }
170190 }
171191 }
172- }
173192
174- if (isset ($ colorGroup ['shoeSizes ' ]) && is_array ($ colorGroup ['shoeSizes ' ])) {
175- Log::info ('Processing shoe sizes for color group ' , ['color_id ' => $ colorGroup ['color ' ]['id ' ], 'shoe_sizes_data ' => $ colorGroup ['shoeSizes ' ]]);
176- foreach ($ colorGroup ['shoeSizes ' ] as $ attributeValue ) {
177- if (!$ variation ->attributesVariation ()->where ('attribute_value_id ' , $ attributeValue ['id ' ])->exists ()) {
178- $ attrVariation = $ variation ->attributesVariation ()->create ([
179- 'attribute_value_id ' => $ attributeValue ['id ' ],
180- 'quantity ' => $ attributeValue ['quantity ' ],
181- 'price ' => $ attributeValue ['price ' ],
182- ]);
183- Log::info ('ProductAttributeVariation (shoe size) created ' , ['id ' => $ attrVariation ->id , 'attribute_value_id ' => $ attributeValue ['id ' ]]);
184-
185- if (isset ($ attributeValue ['is_wholesale ' ]) && $ attributeValue ['is_wholesale ' ] && isset ($ attributeValue ['wholesale_prices ' ]) && is_array ($ attributeValue ['wholesale_prices ' ])) {
193+ // Traitement des pointures
194+ if (isset ($ colorGroup ['shoeSizes ' ]) && is_array ($ colorGroup ['shoeSizes ' ])) {
195+ Log::info ('Processing shoe sizes for color group ' , ['color_id ' => $ colorGroup ['color ' ]['id ' ], 'shoe_sizes_data ' => $ colorGroup ['shoeSizes ' ]]);
196+ foreach ($ colorGroup ['shoeSizes ' ] as $ attributeValue ) {
197+ // Crée ou met à jour la VariationAttribute
198+ $ attrVariation = $ variation ->attributesVariation ()->updateOrCreate (
199+ ['attribute_value_id ' => $ attributeValue ['id ' ]],
200+ [
201+ 'quantity ' => $ attributeValue ['quantity ' ],
202+ 'price ' => $ attributeValue ['price ' ],
203+ ]
204+ );
205+ Log::info ('VariationAttribute (shoe size) processed ' , ['id ' => $ attrVariation ->id , 'attribute_value_id ' => $ attributeValue ['id ' ]]);
206+
207+ // Gestion des prix de gros pour cette VariationAttribute spécifique
208+ if (isset ($ attributeValue ['is_wholesale ' ]) && $ attributeValue ['is_wholesale ' ] &&
209+ isset ($ attributeValue ['wholesale_prices ' ]) && is_array ($ attributeValue ['wholesale_prices ' ])) {
210+
211+ // Supprime les anciens prix de gros pour éviter les doublons si vous utilisez updateOrCreate
212+ $ attrVariation ->wholesalePrices ()->delete ();
213+
186214 foreach ($ attributeValue ['wholesale_prices ' ] as $ wholesalePriceData ) {
187- $ attrVariation ->wholesalePrices ()->create ([
215+ $ attrVariation ->wholesalePrices ()->create ([ // Lie les prix de gros à VariationAttribute
188216 'min_quantity ' => $ wholesalePriceData ['min_quantity ' ],
189217 'wholesale_price ' => $ wholesalePriceData ['wholesale_price ' ],
190218 ]);
@@ -197,6 +225,7 @@ public function store(Request $request)
197225 }
198226 }
199227
228+ // Gestion des catégories et sous-catégories
200229 if ($ request ->has ('categories ' ) && is_array ($ request ->categories )) {
201230 $ product ->categories ()->attach (array_map ('intval ' , $ request ->categories ));
202231 Log::info ('Categories attached ' , ['categories ' => $ request ->categories ]);
@@ -217,8 +246,8 @@ public function store(Request $request)
217246 Log::error ('Product creation failed ' , [
218247 'error ' => $ e ->getMessage (),
219248 'trace ' => $ e ->getTraceAsString (),
220- 'request_all ' => $ request ->all (), // Log all request data on failure
221- 'request_files ' => $ request ->files ->all (), // Log all file data on failure
249+ 'request_all ' => $ request ->all (), // Log toutes les données de la requête en cas d'échec
250+ 'request_files ' => $ request ->files ->all (), // Log tous les fichiers en cas d'échec
222251 ]);
223252 return response ()->json ([
224253 'success ' => false ,
0 commit comments