|
- <?php
-
- namespace Lc\ShopBundle\Controller\Admin;
-
- use App\Entity\Product;
- use Doctrine\DBAL\Types\FloatType;
- use Doctrine\ORM\EntityRepository;
- use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
- use Lc\ShopBundle\Context\ProductCategoryInterface;
- use Lc\ShopBundle\Context\ProductFamilyInterface;
- use Lc\ShopBundle\Context\ProductInterface;
- use Lc\ShopBundle\Context\TaxRateInterface;
- use Lc\ShopBundle\Context\UnitInterface;
- use Lc\ShopBundle\Form\ProductFamilyCategoriesType;
- use Lc\ShopBundle\Form\ProductType;
- use Symfony\Bridge\Doctrine\Form\Type\EntityType;
- use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
- use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
- use Symfony\Component\Form\Extension\Core\Type\CollectionType;
- use Symfony\Component\Form\Extension\Core\Type\DateType;
- use Symfony\Component\Form\Extension\Core\Type\MoneyType;
- use Symfony\Component\Form\Extension\Core\Type\NumberType;
- use Symfony\Component\HttpFoundation\Response;
-
- class ProductFamilyController extends AdminController
- {
- private $taxRateClass;
- private $choicesTaxRateParam;
- private $choicesSupplierTaxRateParam;
-
- public function createEntityFormBuilder($entity, $view)
- {
- $formBuilder = parent::createEntityFormBuilder($entity, $view);
-
- $class = $this->em->getClassMetadata(ProductCategoryInterface::class);
- $this->taxRateClass = $this->em->getClassMetadata(TaxRateInterface::class);
-
- $formBuilder->add('productCategories', ProductFamilyCategoriesType::class) ;
-
- //CHOICE qui permet de sélectionner une taxe
- //ça c'est du lourd fais très attention faudra refactorer
- $this->getUser()->getMerchant()->getTaxRate();
- $choicesTaxRate['Valeur par défaut'] = 0;
- $choicesSupplierTaxRate['Hérité de la TVA du produit'] = 0;
- foreach ($this->em->getRepository($this->taxRateClass->name)->findAll() as $tax) {
- $choicesTaxRate[$tax->getTitle()] = $tax->getId();
- $choicesSupplierTaxRate[$tax->getTitle()] = $tax->getId();
- $this->choicesTaxRateParam[$tax->getId()] = $tax->getValue();
- $this->choicesSupplierTaxRateParam[$tax->getId()] = $tax->getValue();
- }
-
- //là mon ami je kiffe symfo !!!!!
- $this->choicesTaxRateParam[0] = $this->getUser()->getMerchant()->getTaxRate()->getValue();
-
- // choices unit
- $choicesUnits = [] ;
- $unitClass = $this->em->getClassMetadata(UnitInterface::class)->getName() ;
- foreach ($this->em->getRepository($unitClass)->findAll() as $unit) {
- $choicesUnits[$unit->getWording()] = $unit->getId() ;
- }
-
- $formBuilder->add('taxRate', ChoiceType::class, array(
- 'label' => 'TVA',
- 'choices' => $choicesTaxRate,
- 'mapped' => false,
- 'data'=> 0,
- 'choice_attr' => function ($choice, $key, $value) {
- return ['data-tax-rate-value' => $this->choicesTaxRateParam[$choice]];
- },
- ));
-
- $formBuilder->add('differentSupplierTaxRate', CheckboxType::class, array(
- 'required'=>false,
- 'mapped'=>false
- ));
-
- $this->choicesSupplierTaxRateParam[0] = 'inherited';
-
- $formBuilder->add('supplierTaxRate', ChoiceType::class, array(
- 'label' => 'TVA du fournisseur',
- 'choices' => $choicesSupplierTaxRate,
- 'data'=> 0,
- 'mapped' => false,
- 'choice_attr' => function ($choice, $key, $value) {
- return ['data-tax-rate-value' => $this->choicesSupplierTaxRateParam[$choice]];
- },
- ));
-
- $formBuilder->add('unit', EntityType::class, array(
- 'class' => $unitClass,
- /*'label' => 'Unité',
- 'choices' => $choicesUnits,
- 'data'=> 0,*/
- ));
-
- $formBuilder->add('quantity', NumberType::class, array(
- 'label' => 'Quantité',
- 'attr' => [
- 'append_html' => 'g'
- ]
- ));
-
- $formBuilder->add('price', NumberType::class, array(
- 'label' => 'Prix',
- ));
-
- $formBuilder->add('priceWithTax', NumberType::class, array(
- 'label' => 'Prix TTC',
- 'required' => false,
- 'mapped' => false
- ));
-
- $formBuilder->add('buyingPrice', NumberType::class, array(
- 'label' => 'Prix d\'achat',
- ));
-
- $formBuilder->add('priceByRefUnit', NumberType::class, array(
- 'label' => 'Prix de vente en fonction de l\'unité de référence',
- 'mapped'=>false,
- 'required'=>false
- ));
-
- $formBuilder->add('priceByRefUnitWithTax', NumberType::class, array(
- 'label' => 'Prix',
- 'mapped'=>false,
- 'required'=>false
- ));
-
- $formBuilder->add('buyingPriceWithTax', NumberType::class, array(
- 'label' => 'Prix d\'achat TTC',
- 'required' => false,
- 'mapped' => false
- ));
- $formBuilder->add('multiplyingFactor', NumberType::class, array(
- 'label' => 'Coefficiant de multiplication',
- 'mapped'=>false,
- 'required'=>false
- ));
-
-
- $formBuilder->add('behaviorCountStock', ChoiceType::class, array(
- 'label' => 'Stock',
- 'empty_data' => 'by-product-family',
- 'choices' => array(
- 'Gèrer le stock par quantité' => 'by-quantity',
- 'Gèrer le stock par produit (à la pièce)' => 'by-product-family',
- 'Gèrer le stock par déclinaison' => 'by-product'
- ),
- 'multiple' => false,
- 'expanded' => true
- ));
-
- $formBuilder->add('organicLabel', ChoiceType::class, array(
- 'label' => 'Type de labellisation',
- 'choices' => array(
- 'Agriculture biologique' => 'bio',
- 'Nature & progrès' => 'nature-progres',
- 'Haute valeur environnementale' => 'hve'
- ),
- 'multiple' => false,
- 'expanded' => false,
- 'required' => false
- ));
-
-
- $formBuilder->add('products', CollectionType::class, array(
- 'label' => 'Déclinaisons',
- 'entry_type' => ProductType::class,
- 'entry_options' => ['label' => false],
- 'allow_add' => true,
- 'allow_delete' => true,
- 'required' => true
- )
- );
-
- return $formBuilder;
- }
-
- public function updateEntity($entity)
- {
- $prodductFamilyRequest = $this->request->request->get('productfamily');
-
- if ($taxRateId = intval($prodductFamilyRequest['taxRate']) > 0) {
- $repo = $this->em->getRepository(TaxRateInterface::class);
- $entity->setTaxRate($repo->find($taxRateId));
- }
-
- $this->processCategories($entity) ;
- $this->processProducts($entity) ;
-
- $this->setUpdated($entity);
-
- parent::updateEntity($entity);
- }
-
- public function persistEntity($entity)
- {
- $this->processCategories($entity) ;
- $this->processProducts($entity) ;
-
- parent::persistEntity($entity) ;
- }
-
- protected function processProducts($entity)
- {
- //si il existe un et un seul produit pour ce product family n'ajoute rien supprime rien
- if(count($entity->getProducts()) == 0) {
- $product = new Product();
- $product->setCreatedBy($this->getUser()) ;
- $product->setUpdatedBy($this->getUser()) ;
- $product->setProductFamily($entity) ;
- $this->em->persist($product);
-
- $entity->addProduct($product) ;
- }
- else {
-
- foreach($entity->getProducts() as $product) {
- $product->setProductFamily($entity) ;
- $product->setCreatedBy($this->getUser()) ;
- $product->setUpdatedBy($this->getUser()) ;
- $this->em->persist($product);
- $entity->addProduct($product) ;
-
- // die('ncici');
- }
- }
- }
-
- protected function processCategories(ProductFamilyInterface $entity)
- {
- $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class) ;
- $productCategories = $entity->getProductCategories() ;
-
- $entity->initProductCategories() ;
-
- foreach($productCategories as $key => $bool) {
- if(is_bool($bool) && $bool) {
- if(strpos($key, 'category_children_') !== false) {
- $idCategory = (int) str_replace('category_children_', '', $key) ;
- }
- else {
- $idCategory = (int) str_replace('category_', '', $key) ;
- }
-
- $category = $productCategoryRepository->find($idCategory) ;
- $entity->addProductCategory($category) ;
- }
- }
- }
- protected function editAction()
- {
-
- $this->dispatch(EasyAdminEvents::PRE_EDIT);
-
- $id = $this->request->query->get('id');
- $easyadmin = $this->request->attributes->get('easyadmin');
- $entity = $easyadmin['item'];
-
- if ($this->request->isXmlHttpRequest() && $property = $this->request->query->get('property')) {
- $newValue = 'true' === mb_strtolower($this->request->query->get('newValue'));
- $fieldsMetadata = $this->entity['list']['fields'];
-
- if (!isset($fieldsMetadata[$property]) || 'toggle' !== $fieldsMetadata[$property]['dataType']) {
- throw new \RuntimeException(sprintf('The type of the "%s" property is not "toggle".', $property));
- }
-
- $this->updateEntityProperty($entity, $property, $newValue);
-
- // cast to integer instead of string to avoid sending empty responses for 'false'
- return new Response((int) $newValue);
- }
-
- $fields = $this->entity['edit']['fields'];
-
- $editForm = $this->executeDynamicMethod('create<EntityName>EditForm', [$entity, $fields]);
- $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
-
- $sortableProductsField = array();
- foreach($editForm->get('products')->getData() as $k=>$product){
- $sortableProductsField[$product->getPosition()] = $k;
- }
- ksort($sortableProductsField);
- $editForm->handleRequest($this->request);
- if ($editForm->isSubmitted() && $editForm->isValid()) {
-
- $this->processUploadedFiles($editForm);
-
- $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
- $this->executeDynamicMethod('update<EntityName>Entity', [$entity, $editForm]);
- $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
-
- return $this->redirectToReferrer();
- }
-
- $this->dispatch(EasyAdminEvents::POST_EDIT);
-
- $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
- $categories = $productCategoryRepository->findAllParents();
-
- $parameters = [
- 'form' => $editForm->createView(),
- 'entity_fields' => $fields,
- 'entity' => $entity,
- 'delete_form' => $deleteForm->createView(),
- 'categories' => $categories,
- 'sortableProductsField' => $sortableProductsField
- ];
-
- return $this->executeDynamicMethod('render<EntityName>Template', ['edit', $this->entity['templates']['edit'], $parameters]);
- }
-
- protected function newAction()
- {
- $this->dispatch(EasyAdminEvents::PRE_NEW);
-
- $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
-
- $easyadmin = $this->request->attributes->get('easyadmin');
- $easyadmin['item'] = $entity;
- $this->request->attributes->set('easyadmin', $easyadmin);
-
- $fields = $this->entity['new']['fields'];
-
- $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
-
- $newForm->handleRequest($this->request);
-
- if ($newForm->isSubmitted() && $newForm->isValid()) {
- $this->processUploadedFiles($newForm);
-
- $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
- $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
- $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
-
- return $this->redirectToReferrer();
- }
-
- $this->dispatch(EasyAdminEvents::POST_NEW, [
- 'entity_fields' => $fields,
- 'form' => $newForm,
- 'entity' => $entity
- ]);
-
- $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
- $categories = $productCategoryRepository->findAllParents();
-
- $parameters = [
- 'form' => $newForm->createView(),
- 'entity_fields' => $fields,
- 'entity' => $entity,
- 'categories' => $categories,
- 'sortableProductsField' => array()
- ];
-
- return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
- }
-
- }
|