Explorar el Código

[Backend] Produits : duplication sur un autre marchand

packProduct
Guillaume hace 3 años
padre
commit
686057c909
Se han modificado 4 ficheros con 84 adiciones y 35 borrados
  1. +51
    -16
      Controller/AdminControllerTrait.php
  2. +21
    -17
      EventSubscriber/Product/OnlineOrOfflineProductCategoryAfterProductFamilyEventSubscriber.php
  3. +6
    -1
      Resolver/MerchantResolver.php
  4. +6
    -1
      Resources/translations/admin.fr.yaml

+ 51
- 16
Controller/AdminControllerTrait.php Ver fichero

@@ -2,6 +2,7 @@

namespace Lc\CaracoleBundle\Controller;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
@@ -20,6 +21,7 @@ use Lc\CaracoleBundle\Doctrine\Extension\FilterMultipleMerchantsInterface;
use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
use Lc\CaracoleBundle\Form\Merchant\DuplicateToOtherMerchantFormType;
use Lc\CaracoleBundle\Form\Section\DuplicateToOtherSectionFormType;
use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
use Lc\CaracoleBundle\Resolver\MerchantResolver;
use Lc\CaracoleBundle\Resolver\SectionResolver;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
@@ -93,7 +95,7 @@ trait AdminControllerTrait
AdminContext $context,
EntityComponent $entityComponent,
TranslatorAdmin $translatorAdmin,
EntityManagerInterface $em
EntityManagerInterface $entityManager
) {
if (!$this->isGranted(
Permission::EA_EXECUTE_ACTION,
@@ -106,35 +108,68 @@ trait AdminControllerTrait
throw new InsufficientEntityPermissionException($context);
}

if (!$this->isInstanceOf(FilterMerchantInterface::class)) {
if (!$this->isInstanceOf(FilterMerchantInterface::class)
&& !$this->isInstanceOf(ProductFamilyInterface::class)) {
throw new \ErrorException('L\entité n\'est pas lié à un merchant.');
}

$duplicateOtherMerchantForm = $this->createForm(
DuplicateToOtherMerchantFormType::class,
null,
array(
[
'entityClass' => $context->getEntity()->getFqcn(),
'entityId' => $context->getEntity()->getInstance()->getId(),
'action' => $context->getRequest()->getUri(),
'attr' => ['id' => 'duplicate-other-merchant-form'],

)
]
);

$duplicateOtherMerchantForm->handleRequest($context->getRequest());

if ($duplicateOtherMerchantForm->isSubmitted() && $duplicateOtherMerchantForm->isValid()) {
$newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance());
$em->create($newEntity);
$entityManager->create($newEntity);
$merchant = $duplicateOtherMerchantForm->get('merchants')->getData();
$newEntity->setMerchant($merchant);
$em->update($newEntity);
$em->flush();

if($this->isInstanceOf(ProductFamilyInterface::class)) {
$sectionStore = $this->getSectionContainer()->getStore()->setMerchant($merchant);

// Les ProductFamilySectionproperty sont créées en double, on rectifie ici
// @TODO : j'imagine qu'on peut faire mieux que ça. Résoudre le problème à la base par exemple.
$productFamilySectionPropertyArray = [];
foreach($newEntity->getProductFamilySectionProperties() as $productFamilySectionProperty) {
$productFamilySectionPropertyArray[$productFamilySectionProperty->getId()] = $productFamilySectionProperty;
$newEntity->removeProductFamilySectionProperty($productFamilySectionProperty);
}

foreach($productFamilySectionPropertyArray as $productFamilySectionProperty) {
$oldSection = $productFamilySectionProperty->getSection();
$newSection = $sectionStore->getOneByDevAlias($oldSection->getDevAlias());

if($newSection) {
$productFamilySectionProperty->setProductFamily($newEntity);
$productFamilySectionProperty->setSection($newSection);
$newEntity->addProductFamilySectionProperty($productFamilySectionProperty);
}
else {
$entityManager->remove($productFamilySectionProperty);
$newEntity->removeProductFamilySectionProperty($productFamilySectionProperty);
}
}

$newEntity->initProductCategories();
}
else {
$newEntity->setMerchant($merchant);
}

$entityManager->update($newEntity);
$entityManager->flush();

$url = $this->get(AdminUrlGenerator::class)
->setAction(ActionDefinition::EDIT)
->setEntityId($newEntity->getId())
->setAction(ActionDefinition::INDEX)
->generateUrl();

$this->addFlashTranslator(
'success',
'duplicateToOtherMerchant',
@@ -142,21 +177,20 @@ trait AdminControllerTrait
['%merchant%' => $merchant->getTitle()]
);

//TODO switch merchant route
return $this->redirect($url);
}


if ($context->getRequest()->isXmlHttpRequest()) {
$response['data'] = $this->renderView(
'@LcCaracole/admin/merchant/modal/duplicate_entity_to_other_merchant.html.twig',
array(
[
'form_duplicate_entity_to_other_merchant' => $duplicateOtherMerchantForm->createView(),
)
]
);

return new Response(json_encode($response));
} else {
}
else {
throw new \ErrorException('La requête doit être effectué en ajax');
}
}
@@ -235,6 +269,7 @@ trait AdminControllerTrait
public function buildIndexActions(Actions $actions): void
{
parent::buildIndexActions($actions);

if ($this->isInstanceOf(FilterMerchantInterface::class)) {
$actions->add(Crud::PAGE_INDEX, $this->getDuplicateToOhterMerchantAction());
}

+ 21
- 17
EventSubscriber/Product/OnlineOrOfflineProductCategoryAfterProductFamilyEventSubscriber.php Ver fichero

@@ -16,8 +16,10 @@ class OnlineOrOfflineProductCategoryAfterProductFamilyEventSubscriber implements
protected EntityManagerInterface $entityManager;
protected ProductCategoryContainer $productCategoryContainer;

public function __construct(EntityManagerInterface $entityManager, ProductCategoryContainer $productCategoryContainer)
{
public function __construct(
EntityManagerInterface $entityManager,
ProductCategoryContainer $productCategoryContainer
) {
$this->entityManager = $entityManager;
$this->productCategoryContainer = $productCategoryContainer;
}
@@ -25,8 +27,8 @@ class OnlineOrOfflineProductCategoryAfterProductFamilyEventSubscriber implements
public static function getSubscribedEvents()
{
return [
EntityManagerEvent::PRE_CREATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
EntityManagerEvent::PRE_UPDATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
EntityManagerEvent::PRE_CREATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
EntityManagerEvent::PRE_UPDATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
];
}

@@ -34,13 +36,12 @@ class OnlineOrOfflineProductCategoryAfterProductFamilyEventSubscriber implements
{
if ($event->getEntity() instanceof ProductFamilySectionPropertyInterface) {
$this->setProductCategoryByProductFamilySectionProperty($event->getEntity());
} else if ($event->getEntity() instanceof ProductFamilyInterface) {

foreach ($event->getEntity()->getProductFamilySectionProperties() as $productFamilySectionProperty) {
$this->setProductCategoryByProductFamilySectionProperty($productFamilySectionProperty);
} else {
if ($event->getEntity() instanceof ProductFamilyInterface) {
foreach ($event->getEntity()->getProductFamilySectionProperties() as $productFamilySectionProperty) {
$this->setProductCategoryByProductFamilySectionProperty($productFamilySectionProperty);
}
}


}
}

@@ -56,15 +57,18 @@ class OnlineOrOfflineProductCategoryAfterProductFamilyEventSubscriber implements
$this->productCategoryContainer->getBuilder()->setOnlineIfOnlineProductfamily($productCategory);
}
}
} else if ($productFamilySectionProperty->getStatus() == 0 || $productFamily->getStatus() == 0) {
$section = $productFamilySectionProperty->getSection();
$productCategoryArray = $productFamilySectionProperty->getProductFamily()->getProductCategories();
foreach ($productCategoryArray as $productCategory) {
if ($productCategory->getSection() === $section) {
$this->productCategoryContainer->getBuilder()->setOfflineIfOfflineProductfamily($productCategory);
} else {
if ($productFamilySectionProperty->getStatus() == 0 || $productFamily->getStatus() == 0) {
$section = $productFamilySectionProperty->getSection();
$productCategoryArray = $productFamily->getProductCategories();
foreach ($productCategoryArray as $productCategory) {
if ($productCategory->getSection() === $section) {
$this->productCategoryContainer->getBuilder()->setOfflineIfOfflineProductfamily(
$productCategory
);
}
}
}

}
}
}

+ 6
- 1
Resolver/MerchantResolver.php Ver fichero

@@ -154,7 +154,12 @@ class MerchantResolver
$url .= $section->getSlug();
}

return$url;
return $url;
}

public function getUrlAdmin(MerchantInterface $merchant)
{
return $this->settingSolver->getSettingValue($merchant, MerchantSettingDefinition::SETTING_URL).'admin';
}

public function getMerchantUser(UserInterface $user = null)

+ 6
- 1
Resources/translations/admin.fr.yaml Ver fichero

@@ -15,10 +15,12 @@ menu:


flash_message:
success:
duplicateToOtherMerchant: "Le document a bien été dupliqué sur le marchand : %merchant%"
error:
formValidation: Une erreur est survenue lors de la soumission du formulaire

settings_saved: Paramètres mis à jour
duplicateToOtherMerchant: "Le document a bien été dupliqué sur le marchand : %merchant%"
duplicateToOtherSection: "Le document a bien été dupliqué sur la section : %section%"

action:
@@ -208,6 +210,9 @@ entity:
label: Catégorie
label_plurial: Catégories
ProductFamily:
flashes:
success:
duplicateToOtherMerchant: Duplication du produit sur un autre marchand réussie
fields:
priceWithTax: Prix de vente
sales: Historique des ventes

Cargando…
Cancelar
Guardar