浏览代码

FD3-680 Se agrega mensaje y validación de position en ONU

Espinoza Guillermo 7 年之前
父节点
当前提交
cd3e68923a

+ 29 - 1
src/FTTHBundle/Controller/ONUController.php

@@ -11,7 +11,6 @@ use Symfony\Component\HttpFoundation\Response;
 use MapBundle\Entity\Location;
 use FTTHBundle\Entity\ONU;
 use FTTHBundle\Entity\NAP;
-
 use Symfony\Component\Form\Extension\Core\Type\TextType;
 use Symfony\Component\Form\Extension\Core\Type\DateType;
 use Symfony\Component\Form\Extension\Core\Type\SubmitType;
@@ -279,4 +278,33 @@ class ONUController extends Controller
         }
         return ($a < $b) ? -1 : 1;
     }
+    
+    /**
+     * chequea la position si esta libre, según nap
+     * @param Request $request
+     *
+     * @return JsonResponse
+     */
+    public function ajaxCheckPositionAction(Request $request)
+    {
+        $result = 'ok';
+        $position = $request->get('position');
+        $napId = $request->get('nap');
+        if ($position && $napId) {
+            $em = $this->getDoctrine()->getManager();
+            $nap = $em->getRepository(NAP::class)->find($napId);
+            if ($nap) {
+                $onus = $em->getRepository(ONU::class)->findByOLTSlotLink($nap);
+                foreach ($onus as $onu) {
+                    if ($onu->getPosition() == $position) {
+                        $result = 'error';
+                        break;
+                    }
+                }
+            }
+        }
+        
+        return new JsonResponse(compact('result'));
+    }
+    
 }

+ 12 - 7
src/FTTHBundle/Entity/ONU.php

@@ -2,23 +2,24 @@
 
 namespace FTTHBundle\Entity;
 
+use Base\AdminBundle\Interfaces\SoftDeleteInterface;
 use Base\AdminBundle\Traits\TenancyIdTrait;
 use Base\AdminBundle\Traits\TenancyIdTraitInterface;
-use Doctrine\ORM\Mapping as ORM;
-use ExtraDataBundle\Entity\Traits\ExtraDataTrait;
-use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
-use Symfony\Component\Validator\Constraints as Assert;
-use Symfony\Component\Workflow\Exception\ExceptionInterface;
 use DeviceBundle\Validator\Constraints as ValidatorAssert;
 use DeviceBundle\Interfaces\DeviceInterface;
-use JMS\Serializer\Annotation as JMS;
+use ExtraDataBundle\Entity\Traits\ExtraDataTrait;
+use FTTHBundle\Validator\Constraints as FTTHAssert;
 use MapBundle\Entity\Interfaces\LocationInterface;
 use MapBundle\Entity\Traits\LocationTrait;
 use WorkflowBundle\Entity\Interfaces\WorkflowInterface;
 use WorkflowBundle\Entity\Traits\WorkflowTrait;
+use Doctrine\ORM\Mapping as ORM;
 use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity as SoftDeleteable;
 use Gedmo\Mapping\Annotation as Gedmo;
-use Base\AdminBundle\Interfaces\SoftDeleteInterface;
+use JMS\Serializer\Annotation as JMS;
+use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
+use Symfony\Component\Validator\Constraints as Assert;
+use Symfony\Component\Workflow\Exception\ExceptionInterface;
 
 /**
  * ONU
@@ -31,6 +32,8 @@ use Base\AdminBundle\Interfaces\SoftDeleteInterface;
  * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=true)
  *
  * @ValidatorAssert\Device
+ *
+ * @FTTHAssert\Position
  */
 class ONU implements DeviceInterface, TenancyIdTraitInterface, LocationInterface, WorkflowInterface, SoftDeleteInterface
 {
@@ -172,6 +175,8 @@ class ONU implements DeviceInterface, TenancyIdTraitInterface, LocationInterface
      * @var int
      *
      * @ORM\Column(type="integer", nullable=false, options={"default": "0"})
+     *
+     * @Assert\Range(min = 0)
      */
     private $position = 0;
 

+ 5 - 1
src/FTTHBundle/Resources/config/routing.yml

@@ -1,3 +1,7 @@
 ajax_distance_nap_onu:
     path:      /ajax_distance_nap_onu
-    defaults:  { _controller: FTTHBundle:ONU:getDistanceNapOnu }
+    defaults:  { _controller: FTTHBundle:ONU:getDistanceNapOnu }
+    
+ajax_check_position:
+    path:      /ajax_check_position
+    defaults:  { _controller: FTTHBundle:ONU:ajaxCheckPosition }

+ 7 - 1
src/FTTHBundle/Resources/config/services.yml

@@ -108,4 +108,10 @@ services:
        class: FTTHBundle\Twig\ShowFileSystemExtension
        arguments: ["@service_container"]
        tags:
-           - { name: twig.extension } 
+           - { name: twig.extension } 
+           
+    ftth.position_validator:
+       class: FTTHBundle\Validator\Constraints\PositionValidator
+       arguments: ["@doctrine.orm.entity_manager"]
+       tags:
+           - { name: validator.constraint_validator }       

+ 2 - 1
src/FTTHBundle/Resources/translations/FTTHBundle.es.yml

@@ -331,4 +331,5 @@ action_use_template: Usar
 action_no_use_template: Deshabilitar base
 msg_use_template_onu_in_create_onu_ftth: Se encuentra en uso una plantilla de ONU y algunos campos se encuentran preseleccionados. Para crear una ONU sin campos preseleccionados acceda a FTTH > Plantilla ONU > Acciones > Deshabilitar Base.
 
-client_filter_help: Ingresar nombre de cliente, ID o externalID
+client_filter_help: Ingresar nombre de cliente, ID o externalID
+error_onu_position: La posición dentro del puerto seleccionada ya se encuentra asignada a otra ONU

+ 2 - 1
src/FTTHBundle/Resources/translations/validators.es.yml

@@ -1 +1,2 @@
-error.max_number_onu_invalid: Máxima cantidad de ONU por NAP. Seleccione otro NAP de la lista
+error.max_number_onu_invalid: Máxima cantidad de ONU por NAP. Seleccione otro NAP de la lista
+error_onu_position: La posición dentro del puerto seleccionada ya se encuentra asignada a otra ONU

+ 31 - 0
src/FTTHBundle/Resources/views/ONU/form.html.twig

@@ -63,6 +63,37 @@
                 }
             });
         });
+        
+        {# Se chequea la position si esta libre, según el nap seleccionado #}
+        
+        $('#{{ admin.uniqid ~ "_position" }}').on('change', checkPosition);
+        
+        function checkPosition()
+        {
+            $('#{{ admin.uniqid ~ "_position" }}').parent().find('#error_position').remove();
+            var $position = $('#{{ admin.uniqid ~ "_position" }}').val();
+            var $nap = $('#{{ admin.uniqid ~ "_nap" }}').val();
+            if ($position && $nap) {
+                $.ajax({
+                    url: '{{ path('ajax_check_position') }}',
+                    type: 'POST',
+                    data: {
+                        position: $position,
+                        nap: $nap,
+                    },
+                    async: false,
+                    success: function (data) {
+                        if (data.result === 'error') {
+                            message = '{{ 'error_onu_position'|trans({}, 'FTTHBundle') }}';
+                            div = '<div id="error_position" class="alert alert-danger alert-dismissable">\
+                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>'
+                            + message + '</div>'
+                            $('#{{ admin.uniqid ~ "_position" }}').parent().append(div);
+                        }
+                    }
+                });
+            }
+        }
 
         function callbackClientId(clientId) {
             $.ajax({

+ 24 - 0
src/FTTHBundle/Validator/Constraints/Position.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace FTTHBundle\Validator\Constraints;
+
+use Symfony\Component\Validator\Constraint;
+
+/**
+ * @Annotation
+ */
+class Position extends Constraint
+{
+
+    public $message = 'error_onu_position';
+
+
+    /**
+     * @return string
+     */
+    public function getTargets()
+    {
+        return self::CLASS_CONSTRAINT;
+    }
+
+}

+ 44 - 0
src/FTTHBundle/Validator/Constraints/PositionValidator.php

@@ -0,0 +1,44 @@
+<?php
+
+namespace FTTHBundle\Validator\Constraints;
+
+use FTTHBundle\Entity\ONU;
+use Doctrine\ORM\EntityManager;
+use Symfony\Component\Validator\Constraint;
+use Symfony\Component\Validator\ConstraintValidator;
+
+class PositionValidator extends ConstraintValidator
+{
+    
+    /**
+     * @var EntityManager
+     */
+    private $entityManager;
+
+    /**
+     * @param EntityManager $entityManager
+     */
+    public function __construct(EntityManager $entityManager)
+    {
+        $this->entityManager = $entityManager;
+    }
+
+    /**
+     * @param object $entity
+     * @param Constraint $constraint
+     */
+    public function validate($entity, Constraint $constraint)
+    {
+        if ($entity->getPosition() && $entity->getNap()) {
+            $onus = $this->entityManager->getRepository(ONU::class)
+                ->findByOLTSlotLink($entity->getNap(), $entity->getId());
+            foreach ($onus as $onu) {
+                if ($onu->getPosition() == $entity->getPosition()) {
+                    $this->context->buildViolation($constraint->message)
+                        ->addViolation();
+                }
+            }
+        }
+    }
+
+}