Browse Source

[Backend] Produits : duplication sur un autre marchand

packProduct
Guillaume 2 years ago
parent
commit
686057c909
4 changed files with 84 additions and 35 deletions
  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 View File



namespace Lc\CaracoleBundle\Controller; namespace Lc\CaracoleBundle\Controller;


use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection; use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection; use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface; use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
use Lc\CaracoleBundle\Form\Merchant\DuplicateToOtherMerchantFormType; use Lc\CaracoleBundle\Form\Merchant\DuplicateToOtherMerchantFormType;
use Lc\CaracoleBundle\Form\Section\DuplicateToOtherSectionFormType; use Lc\CaracoleBundle\Form\Section\DuplicateToOtherSectionFormType;
use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
use Lc\CaracoleBundle\Resolver\MerchantResolver; use Lc\CaracoleBundle\Resolver\MerchantResolver;
use Lc\CaracoleBundle\Resolver\SectionResolver; use Lc\CaracoleBundle\Resolver\SectionResolver;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
AdminContext $context, AdminContext $context,
EntityComponent $entityComponent, EntityComponent $entityComponent,
TranslatorAdmin $translatorAdmin, TranslatorAdmin $translatorAdmin,
EntityManagerInterface $em
EntityManagerInterface $entityManager
) { ) {
if (!$this->isGranted( if (!$this->isGranted(
Permission::EA_EXECUTE_ACTION, Permission::EA_EXECUTE_ACTION,
throw new InsufficientEntityPermissionException($context); 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.'); throw new \ErrorException('L\entité n\'est pas lié à un merchant.');
} }

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

)
]
); );


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


if ($duplicateOtherMerchantForm->isSubmitted() && $duplicateOtherMerchantForm->isValid()) { if ($duplicateOtherMerchantForm->isSubmitted() && $duplicateOtherMerchantForm->isValid()) {
$newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance()); $newEntity = $entityComponent->duplicateEntity($context->getEntity()->getInstance());
$em->create($newEntity);
$entityManager->create($newEntity);
$merchant = $duplicateOtherMerchantForm->get('merchants')->getData(); $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) $url = $this->get(AdminUrlGenerator::class)
->setAction(ActionDefinition::EDIT)
->setEntityId($newEntity->getId())
->setAction(ActionDefinition::INDEX)
->generateUrl(); ->generateUrl();

$this->addFlashTranslator( $this->addFlashTranslator(
'success', 'success',
'duplicateToOtherMerchant', 'duplicateToOtherMerchant',
['%merchant%' => $merchant->getTitle()] ['%merchant%' => $merchant->getTitle()]
); );


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



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


return new Response(json_encode($response)); return new Response(json_encode($response));
} else {
}
else {
throw new \ErrorException('La requête doit être effectué en ajax'); throw new \ErrorException('La requête doit être effectué en ajax');
} }
} }
public function buildIndexActions(Actions $actions): void public function buildIndexActions(Actions $actions): void
{ {
parent::buildIndexActions($actions); parent::buildIndexActions($actions);

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

+ 21
- 17
EventSubscriber/Product/OnlineOrOfflineProductCategoryAfterProductFamilyEventSubscriber.php View File

protected EntityManagerInterface $entityManager; protected EntityManagerInterface $entityManager;
protected ProductCategoryContainer $productCategoryContainer; protected ProductCategoryContainer $productCategoryContainer;


public function __construct(EntityManagerInterface $entityManager, ProductCategoryContainer $productCategoryContainer)
{
public function __construct(
EntityManagerInterface $entityManager,
ProductCategoryContainer $productCategoryContainer
) {
$this->entityManager = $entityManager; $this->entityManager = $entityManager;
$this->productCategoryContainer = $productCategoryContainer; $this->productCategoryContainer = $productCategoryContainer;
} }
public static function getSubscribedEvents() public static function getSubscribedEvents()
{ {
return [ return [
EntityManagerEvent::PRE_CREATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
EntityManagerEvent::PRE_UPDATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
EntityManagerEvent::PRE_CREATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
EntityManagerEvent::PRE_UPDATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
]; ];
} }


{ {
if ($event->getEntity() instanceof ProductFamilySectionPropertyInterface) { if ($event->getEntity() instanceof ProductFamilySectionPropertyInterface) {
$this->setProductCategoryByProductFamilySectionProperty($event->getEntity()); $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);
}
} }


} }
} }


$this->productCategoryContainer->getBuilder()->setOnlineIfOnlineProductfamily($productCategory); $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 View File

$url .= $section->getSlug(); $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) public function getMerchantUser(UserInterface $user = null)

+ 6
- 1
Resources/translations/admin.fr.yaml View File





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

settings_saved: Paramètres mis à jour 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%" duplicateToOtherSection: "Le document a bien été dupliqué sur la section : %section%"


action: action:
label: Catégorie label: Catégorie
label_plurial: Catégories label_plurial: Catégories
ProductFamily: ProductFamily:
flashes:
success:
duplicateToOtherMerchant: Duplication du produit sur un autre marchand réussie
fields: fields:
priceWithTax: Prix de vente priceWithTax: Prix de vente
sales: Historique des ventes sales: Historique des ventes

Loading…
Cancel
Save