@@ -100,7 +100,8 @@ public function merge(object $target, object $other, array $context = []): Part
100100 return $ target ;
101101 }
102102
103- private function comparePartAssociations (PartAssociation $ t , PartAssociation $ o ): bool {
103+ private function comparePartAssociations (PartAssociation $ t , PartAssociation $ o ): bool
104+ {
104105 //We compare the translation keys, as it contains info about the type and other type info
105106 return $ t ->getOther () === $ o ->getOther ()
106107 && $ t ->getTypeTranslationKey () === $ o ->getTypeTranslationKey ();
@@ -141,40 +142,39 @@ private function mergeCollectionFields(Part $target, Part $other, array $context
141142 $ owner ->addAssociatedPartsAsOwner ($ clone );
142143 }
143144
145+ // Merge orderdetails, considering same supplier+part number as duplicates
144146 $ this ->mergeCollections ($ target , $ other , 'orderdetails ' , function (Orderdetail $ t , Orderdetail $ o ) {
145- //First check that the orderdetails infos are equal
146- $ tmp = $ t ->getSupplier () === $ o ->getSupplier ()
147- && $ t ->getSupplierPartNr () === $ o ->getSupplierPartNr ()
148- && $ t ->getSupplierProductUrl (false ) === $ o ->getSupplierProductUrl (false );
149-
150- if (!$ tmp ) {
151- return false ;
152- }
153-
154- //Check if the pricedetails are equal
155- $ t_pricedetails = $ t ->getPricedetails ();
156- $ o_pricedetails = $ o ->getPricedetails ();
157- //Ensure that both pricedetails have the same length
158- if (count ($ t_pricedetails ) !== count ($ o_pricedetails )) {
159- return false ;
160- }
161-
162- //Check if all pricedetails are equal
163- for ($ n =0 , $ nMax = count ($ t_pricedetails ); $ n < $ nMax ; $ n ++) {
164- $ t_price = $ t_pricedetails ->get ($ n );
165- $ o_price = $ o_pricedetails ->get ($ n );
166-
167- if (!$ t_price ->getPrice ()->isEqualTo ($ o_price ->getPrice ())
168- || $ t_price ->getCurrency () !== $ o_price ->getCurrency ()
169- || $ t_price ->getPriceRelatedQuantity () !== $ o_price ->getPriceRelatedQuantity ()
170- || $ t_price ->getMinDiscountQuantity () !== $ o_price ->getMinDiscountQuantity ()
171- ) {
172- return false ;
147+ // If supplier and part number match, merge the orderdetails
148+ if ($ t ->getSupplier () === $ o ->getSupplier () && $ t ->getSupplierPartNr () === $ o ->getSupplierPartNr ()) {
149+ // Update URL if target doesn't have one
150+ if (empty ($ t ->getSupplierProductUrl (false )) && !empty ($ o ->getSupplierProductUrl (false ))) {
151+ $ t ->setSupplierProductUrl ($ o ->getSupplierProductUrl (false ));
173152 }
153+ // Merge price details: add new ones, update empty ones, keep existing non-empty ones
154+ foreach ($ o ->getPricedetails () as $ otherPrice ) {
155+ $ found = false ;
156+ foreach ($ t ->getPricedetails () as $ targetPrice ) {
157+ if ($ targetPrice ->getMinDiscountQuantity () === $ otherPrice ->getMinDiscountQuantity ()
158+ && $ targetPrice ->getCurrency () === $ otherPrice ->getCurrency ()) {
159+ // Only update price if the existing one is zero/empty (most logical)
160+ if ($ targetPrice ->getPrice ()->isZero ()) {
161+ $ targetPrice ->setPrice ($ otherPrice ->getPrice ());
162+ $ targetPrice ->setPriceRelatedQuantity ($ otherPrice ->getPriceRelatedQuantity ());
163+ }
164+ $ found = true ;
165+ break ;
166+ }
167+ }
168+ // Add completely new price tiers
169+ if (!$ found ) {
170+ $ clonedPrice = clone $ otherPrice ;
171+ $ clonedPrice ->setOrderdetail ($ t );
172+ $ t ->addPricedetail ($ clonedPrice );
173+ }
174+ }
175+ return true ; // Consider them equal so the other one gets skipped
174176 }
175-
176- //If all pricedetails are equal, the orderdetails are equal
177- return true ;
177+ return false ; // Different supplier/part number, add as new
178178 });
179179 //The pricedetails are not correctly assigned to the new orderdetails, so fix that
180180 foreach ($ target ->getOrderdetails () as $ orderdetail ) {
0 commit comments