2023-11-19 23:17:48 +01:00
|
|
|
<?php
|
|
|
|
|
/*
|
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Services\EntityMergers\Mergers;
|
|
|
|
|
|
2023-11-21 19:41:18 +01:00
|
|
|
use App\Entity\Parts\InfoProviderReference;
|
|
|
|
|
use App\Entity\Parts\ManufacturingStatus;
|
2023-11-19 23:17:48 +01:00
|
|
|
use App\Entity\Parts\Part;
|
2023-11-21 00:02:17 +01:00
|
|
|
use App\Entity\Parts\PartAssociation;
|
|
|
|
|
use App\Entity\PriceInformations\Orderdetail;
|
2023-11-19 23:17:48 +01:00
|
|
|
|
2023-11-22 17:14:24 +01:00
|
|
|
/**
|
2023-11-24 23:48:39 +01:00
|
|
|
* This class merges two parts together.
|
|
|
|
|
*
|
2023-11-22 17:14:24 +01:00
|
|
|
* @implements EntityMergerInterface<Part>
|
2024-06-22 00:31:43 +02:00
|
|
|
* @see \App\Tests\Services\EntityMergers\Mergers\PartMergerTest
|
2023-11-22 17:14:24 +01:00
|
|
|
*/
|
2023-11-19 23:17:48 +01:00
|
|
|
class PartMerger implements EntityMergerInterface
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
use EntityMergerHelperTrait;
|
|
|
|
|
|
|
|
|
|
public function supports(object $target, object $other, array $context = []): bool
|
|
|
|
|
{
|
|
|
|
|
return $target instanceof Part && $other instanceof Part;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-19 23:47:46 +01:00
|
|
|
public function merge(object $target, object $other, array $context = []): Part
|
2023-11-19 23:17:48 +01:00
|
|
|
{
|
|
|
|
|
if (!$target instanceof Part || !$other instanceof Part) {
|
|
|
|
|
throw new \InvalidArgumentException('The target and the other entity must be instances of Part');
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 19:41:18 +01:00
|
|
|
//Merge basic fields
|
|
|
|
|
$this->mergeTextWithSeparator($target, $other, 'name');
|
|
|
|
|
$this->mergeTextWithSeparator($target, $other, 'description');
|
|
|
|
|
$this->mergeComment($target, $other);
|
|
|
|
|
$this->useOtherValueIfNotEmtpy($target, $other, 'manufacturer_product_url');
|
|
|
|
|
$this->useOtherValueIfNotEmtpy($target, $other, 'manufacturer_product_number');
|
|
|
|
|
$this->useOtherValueIfNotEmtpy($target, $other, 'mass');
|
|
|
|
|
$this->useOtherValueIfNotEmtpy($target, $other, 'ipn');
|
|
|
|
|
|
|
|
|
|
//Merge relations to other entities
|
2023-11-19 23:47:46 +01:00
|
|
|
$this->useOtherValueIfNotNull($target, $other, 'manufacturer');
|
2023-11-21 00:02:17 +01:00
|
|
|
$this->useOtherValueIfNotNull($target, $other, 'footprint');
|
|
|
|
|
$this->useOtherValueIfNotNull($target, $other, 'category');
|
|
|
|
|
$this->useOtherValueIfNotNull($target, $other, 'partUnit');
|
2025-03-24 15:15:15 +01:00
|
|
|
$this->useOtherValueIfNotNull($target, $other, 'partCustomState');
|
2023-11-21 00:02:17 +01:00
|
|
|
|
|
|
|
|
//We assume that the higher value is the correct one for minimum instock
|
|
|
|
|
$this->useLargerValue($target, $other, 'minamount');
|
|
|
|
|
|
2023-11-21 00:18:10 +01:00
|
|
|
//We assume that a part needs review and is a favorite if one of the parts is
|
|
|
|
|
$this->useTrueValue($target, $other, 'needs_review');
|
|
|
|
|
$this->useTrueValue($target, $other, 'favorite');
|
2023-11-19 23:47:46 +01:00
|
|
|
|
2023-11-21 00:18:10 +01:00
|
|
|
//Merge the tags using the tag merger
|
|
|
|
|
$this->mergeTags($target, $other, 'tags');
|
2023-11-19 23:17:48 +01:00
|
|
|
|
2023-11-21 19:41:18 +01:00
|
|
|
//Merge manufacturing status
|
2023-11-22 17:14:24 +01:00
|
|
|
$this->useCallback(function (?ManufacturingStatus $t, ?ManufacturingStatus $o): ManufacturingStatus {
|
2023-11-21 19:41:18 +01:00
|
|
|
//Use the other value, if the target value is not set
|
|
|
|
|
if ($t === ManufacturingStatus::NOT_SET || $t === null) {
|
|
|
|
|
return $o ?? ManufacturingStatus::NOT_SET;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $t;
|
|
|
|
|
}, $target, $other, 'manufacturing_status');
|
|
|
|
|
|
|
|
|
|
//Merge provider reference
|
|
|
|
|
$this->useCallback(function (InfoProviderReference $t, InfoProviderReference $o): InfoProviderReference {
|
|
|
|
|
if (!$t->isProviderCreated() && $o->isProviderCreated()) {
|
|
|
|
|
return $o;
|
|
|
|
|
}
|
|
|
|
|
return $t;
|
|
|
|
|
}, $target, $other, 'providerReference');
|
|
|
|
|
|
2023-11-22 17:14:24 +01:00
|
|
|
//Merge the collections
|
|
|
|
|
$this->mergeCollectionFields($target, $other, $context);
|
|
|
|
|
|
2023-11-19 23:17:48 +01:00
|
|
|
return $target;
|
|
|
|
|
}
|
2023-11-21 00:02:17 +01:00
|
|
|
|
2025-08-01 18:50:19 +02:00
|
|
|
private function comparePartAssociations(PartAssociation $t, PartAssociation $o): bool
|
|
|
|
|
{
|
2023-11-24 23:36:09 +01:00
|
|
|
//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();
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 00:02:17 +01:00
|
|
|
private function mergeCollectionFields(Part $target, Part $other, array $context): void
|
|
|
|
|
{
|
|
|
|
|
/********************************************************************************
|
|
|
|
|
* Merge collections
|
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
|
|
//Lots from different parts are never considered equal, so we just merge them together
|
|
|
|
|
$this->mergeCollections($target, $other, 'partLots');
|
|
|
|
|
$this->mergeAttachments($target, $other);
|
|
|
|
|
$this->mergeParameters($target, $other);
|
|
|
|
|
|
2023-11-24 23:36:09 +01:00
|
|
|
//Merge the associations
|
2024-06-22 00:31:43 +02:00
|
|
|
$this->mergeCollections($target, $other, 'associated_parts_as_owner', $this->comparePartAssociations(...));
|
2023-11-24 23:36:09 +01:00
|
|
|
|
|
|
|
|
//We have to recreate the associations towards the other part, as they are not created by the merger
|
|
|
|
|
foreach ($other->getAssociatedPartsAsOther() as $association) {
|
|
|
|
|
//Clone the association
|
|
|
|
|
$clone = clone $association;
|
|
|
|
|
//Set the target part as the other part
|
|
|
|
|
$clone->setOther($target);
|
|
|
|
|
$owner = $clone->getOwner();
|
|
|
|
|
if (!$owner) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//Ensure that the association is not already present
|
|
|
|
|
foreach ($owner->getAssociatedPartsAsOwner() as $existing_association) {
|
2024-06-22 00:31:43 +02:00
|
|
|
if ($this->comparePartAssociations($existing_association, $clone)) {
|
2023-11-24 23:36:09 +01:00
|
|
|
continue 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Add the association to the owner
|
|
|
|
|
$owner->addAssociatedPartsAsOwner($clone);
|
|
|
|
|
}
|
2023-11-21 00:02:17 +01:00
|
|
|
|
2025-08-01 18:50:19 +02:00
|
|
|
// Merge orderdetails, considering same supplier+part number as duplicates
|
2023-11-21 00:02:17 +01:00
|
|
|
$this->mergeCollections($target, $other, 'orderdetails', function (Orderdetail $t, Orderdetail $o) {
|
2025-08-01 18:50:19 +02:00
|
|
|
// 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));
|
2023-11-21 00:02:17 +01:00
|
|
|
}
|
2025-08-01 18:50:19 +02:00
|
|
|
// 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());
|
|
|
|
|
}
|
|
|
|
|
$found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 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
|
2023-11-21 00:02:17 +01:00
|
|
|
}
|
2025-08-01 18:50:19 +02:00
|
|
|
return false; // Different supplier/part number, add as new
|
2023-11-21 00:02:17 +01:00
|
|
|
});
|
|
|
|
|
//The pricedetails are not correctly assigned to the new orderdetails, so fix that
|
|
|
|
|
foreach ($target->getOrderdetails() as $orderdetail) {
|
|
|
|
|
foreach ($orderdetail->getPricedetails() as $pricedetail) {
|
|
|
|
|
$pricedetail->setOrderdetail($orderdetail);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-19 23:17:48 +01:00
|
|
|
}
|