Update part merger to consider rows with same supplier and spn duplicates

This commit is contained in:
barisgit 2025-08-01 18:50:19 +02:00 committed by Jan Böhmer
parent a6be786d5d
commit c27f2246a3

View file

@ -100,7 +100,8 @@ class PartMerger implements EntityMergerInterface
return $target;
}
private function comparePartAssociations(PartAssociation $t, PartAssociation $o): bool {
private function comparePartAssociations(PartAssociation $t, PartAssociation $o): bool
{
//We compare the translation keys, as it contains info about the type and other type info
return $t->getOther() === $o->getOther()
&& $t->getTypeTranslationKey() === $o->getTypeTranslationKey();
@ -141,40 +142,39 @@ class PartMerger implements EntityMergerInterface
$owner->addAssociatedPartsAsOwner($clone);
}
// Merge orderdetails, considering same supplier+part number as duplicates
$this->mergeCollections($target, $other, 'orderdetails', function (Orderdetail $t, Orderdetail $o) {
//First check that the orderdetails infos are equal
$tmp = $t->getSupplier() === $o->getSupplier()
&& $t->getSupplierPartNr() === $o->getSupplierPartNr()
&& $t->getSupplierProductUrl(false) === $o->getSupplierProductUrl(false);
if (!$tmp) {
return false;
// If supplier and part number match, merge the orderdetails
if ($t->getSupplier() === $o->getSupplier() && $t->getSupplierPartNr() === $o->getSupplierPartNr()) {
// Update URL if target doesn't have one
if (empty($t->getSupplierProductUrl(false)) && !empty($o->getSupplierProductUrl(false))) {
$t->setSupplierProductUrl($o->getSupplierProductUrl(false));
}
//Check if the pricedetails are equal
$t_pricedetails = $t->getPricedetails();
$o_pricedetails = $o->getPricedetails();
//Ensure that both pricedetails have the same length
if (count($t_pricedetails) !== count($o_pricedetails)) {
return false;
// Merge price details: add new ones, update empty ones, keep existing non-empty ones
foreach ($o->getPricedetails() as $otherPrice) {
$found = false;
foreach ($t->getPricedetails() as $targetPrice) {
if ($targetPrice->getMinDiscountQuantity() === $otherPrice->getMinDiscountQuantity()
&& $targetPrice->getCurrency() === $otherPrice->getCurrency()) {
// Only update price if the existing one is zero/empty (most logical)
if ($targetPrice->getPrice()->isZero()) {
$targetPrice->setPrice($otherPrice->getPrice());
$targetPrice->setPriceRelatedQuantity($otherPrice->getPriceRelatedQuantity());
}
//Check if all pricedetails are equal
for ($n=0, $nMax = count($t_pricedetails); $n< $nMax; $n++) {
$t_price = $t_pricedetails->get($n);
$o_price = $o_pricedetails->get($n);
if (!$t_price->getPrice()->isEqualTo($o_price->getPrice())
|| $t_price->getCurrency() !== $o_price->getCurrency()
|| $t_price->getPriceRelatedQuantity() !== $o_price->getPriceRelatedQuantity()
|| $t_price->getMinDiscountQuantity() !== $o_price->getMinDiscountQuantity()
) {
return false;
$found = true;
break;
}
}
//If all pricedetails are equal, the orderdetails are equal
return true;
// Add completely new price tiers
if (!$found) {
$clonedPrice = clone $otherPrice;
$clonedPrice->setOrderdetail($t);
$t->addPricedetail($clonedPrice);
}
}
return true; // Consider them equal so the other one gets skipped
}
return false; // Different supplier/part number, add as new
});
//The pricedetails are not correctly assigned to the new orderdetails, so fix that
foreach ($target->getOrderdetails() as $orderdetail) {