diff --git a/src/Bundle/ChillThirdPartyBundle/Controller/ThirdpartyDuplicateController.php b/src/Bundle/ChillThirdPartyBundle/Controller/ThirdpartyDuplicateController.php index 78f99e8a9..56967a9ce 100644 --- a/src/Bundle/ChillThirdPartyBundle/Controller/ThirdpartyDuplicateController.php +++ b/src/Bundle/ChillThirdPartyBundle/Controller/ThirdpartyDuplicateController.php @@ -14,16 +14,16 @@ namespace Chill\ThirdPartyBundle\Controller; use Chill\PersonBundle\Form\PersonConfimDuplicateType; use Chill\ThirdPartyBundle\Entity\ThirdParty; use Chill\ThirdPartyBundle\Form\ThirdpartyFindDuplicateType; -use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository; use Chill\ThirdPartyBundle\Service\ThirdpartyMergeService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; +use Symfony\Contracts\Translation\TranslatorInterface; class ThirdpartyDuplicateController extends AbstractController { - public function __construct(private readonly ThirdPartyRepository $thirdPartyRepository, private readonly ThirdpartyMergeService $thirdPartyMergeService) {} + public function __construct(private readonly ThirdpartyMergeService $thirdPartyMergeService, private readonly TranslatorInterface $translator) {} /** * @ParamConverter("thirdparty", options={"id": "thirdparty_id"}) @@ -74,25 +74,45 @@ class ThirdpartyDuplicateController extends AbstractController #[Route(path: '/{_locale}/3party/{thirdparty1_id}/duplicate/{thirdparty2_id}/confirm', name: 'chill_thirdparty_duplicate_confirm')] public function confirmAction(ThirdParty $thirdparty1, ThirdParty $thirdparty2, Request $request) { - if ($thirdparty1 === $thirdparty2) { - throw new \InvalidArgumentException('Can not merge same thirdparty'); + try { + $this->validateThirdpartyMerge($thirdparty1, $thirdparty2); + $form = $this->createForm(PersonConfimDuplicateType::class); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + + $this->thirdPartyMergeService->merge($thirdparty1, $thirdparty2); + + return $this->redirectToRoute('chill_crud_3party_3party_view', ['id' => $thirdparty1->getId()]); + } + + return $this->render('@ChillThirdParty/ThirdPartyDuplicate/confirm.html.twig', [ + 'thirdparty' => $thirdparty1, + 'thirdparty2' => $thirdparty2, + 'form' => $form->createView(), + ]); + } catch (\InvalidArgumentException $e) { + $this->addFlash('error', $this->translator->trans($e->getMessage())); + + return $this->redirectToRoute('chill_thirdparty_find_duplicate', [ + 'thirdparty_id' => $thirdparty1->getId(), + ]); } + } - $form = $this->createForm(PersonConfimDuplicateType::class); + private function validateThirdpartyMerge(Thirdparty $thirdparty1, Thirdparty $thirdparty2): void + { + $constraints = [ + [$thirdparty1 === $thirdparty2, 'thirdparty_duplicate.You cannot merge a thirdparty with itself. Please choose a different thirdparty'], + [$thirdparty1->getKind() !== $thirdparty2->getKind(), 'thirdparty_duplicate.A thirdparty can only be merged with a thirdparty of the same kind'], + [$thirdparty1->getParent() !== $thirdparty2->getParent(), 'thirdparty_duplicate.Two child thirdparties must have the same parent'], + ]; - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - - $this->thirdPartyMergeService->merge($thirdparty1, $thirdparty2); - - return $this->redirectToRoute('chill_crud_3party_3party_view', ['id' => $thirdparty1->getId()]); + foreach ($constraints as [$condition, $message]) { + if ($condition) { + throw new \InvalidArgumentException($message); + } } - - return $this->render('@ChillThirdParty/ThirdPartyDuplicate/confirm.html.twig', [ - 'thirdparty' => $thirdparty1, - 'thirdparty2' => $thirdparty2, - 'form' => $form->createView(), - ]); } } diff --git a/src/Bundle/ChillThirdPartyBundle/Service/ThirdpartyMergeService.php b/src/Bundle/ChillThirdPartyBundle/Service/ThirdpartyMergeService.php index 0c1d88e52..89412e5f6 100644 --- a/src/Bundle/ChillThirdPartyBundle/Service/ThirdpartyMergeService.php +++ b/src/Bundle/ChillThirdPartyBundle/Service/ThirdpartyMergeService.php @@ -90,7 +90,6 @@ class ThirdpartyMergeService $joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']); if (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' !== $joinColumn) { - // TODO what with 'address_id' and 'civility_id'? This condition also contains columns like updatedBy_id which we want to ignore... continue; } @@ -102,9 +101,7 @@ class ThirdpartyMergeService 'sql' => "UPDATE {$schemaPrefix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete", 'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()], ]; - } - - if ($assoc['type'] === 8 && isset($assoc['joinTable'])) { + } elseif (8 === $assoc['type'] && isset($assoc['joinTable'])) { $joinTable = $assoc['joinTable']['name']; $joinColumn = $assoc['joinTable']['joinColumns'][0]['name']; $queries[] = [ diff --git a/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml b/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml index b4340bde1..85f7b2405 100644 --- a/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml @@ -141,3 +141,6 @@ thirdparty_duplicate: Thirdparty to delete explanation: Ce tiers sera supprimé. Seuls les contacts de ce tiers, énumérés ci-dessous, seront transférés. Thirdparty to keep explanation: Ce tiers sera conserver Data to keep: Données conservées + You cannot merge a thirdparty with itself. Please choose a different thirdparty: Vous ne pouvez pas fusionner un tiers avec lui-même. Veuillez choisir un autre tiers. + A thirdparty can only be merged with a thirdparty of the same kind: Un tiers ne peut être fusionné qu'avec un tiers de même type. + Two child thirdparties must have the same parent: Deux tiers de type « contact » doivent avoir le même tiers parent.