Browse Source

Merged in FD3-299 (pull request #43)

ONUController, se agrego la direccion de la nap al calculo de distancia
Gabriel Gosparo 7 years ago
parent
commit
d2eea2e2cb

+ 35 - 36
src/FTTHBundle/Controller/ONUController.php

@@ -25,7 +25,7 @@ use FOS\RestBundle\Controller\Annotations\RouteResource;
 class ONUController extends Controller
 {
 
-    public function importAction() 
+    public function importAction()
     {
         $adminPool = $this->get('sonata.admin.pool');
         $webservice = $this->get("webservice");
@@ -34,25 +34,25 @@ class ONUController extends Controller
 
         $urlObjects = $this->container->hasParameter('remote_get_objects_url');
         $urlObjectTypes = $this->container->hasParameter('remote_get_object_type_url');
-        $_types = array(); 
+        $_types = array();
         $disabled = false;
         $objectTypeId = 0;
 
-        if(!$urlObjects) {
-            $flashbag->add("error", $translator->trans("msg_no_defined_ftth_url",array(),"FTTHBundle"));
+        if (!$urlObjects) {
+            $flashbag->add("error", $translator->trans("msg_no_defined_ftth_url", array(), "FTTHBundle"));
             $disabled = true;
         } else {
-            if(!$urlObjectTypes) {
-                $flashbag->add("error", $translator->trans("msg_no_defined_ftth_url",array(),"FTTHBundle"));
+            if (!$urlObjectTypes) {
+                $flashbag->add("error", $translator->trans("msg_no_defined_ftth_url", array(), "FTTHBundle"));
                 $disabled = true;
             } else {
                 $urlObjectTypes = $this->getParameter('remote_get_object_type_url');
                 $filters = array('name' => 'ONU');
                 $_types = $webservice->getData($urlObjectTypes, $filters, array(), null, null);
             }
-            
-            if(empty($_types)) {
-                $flashbag->add("error", $translator->trans("msg_no_onu_in_map",array(),"FTTHBundle"));
+
+            if (empty($_types)) {
+                $flashbag->add("error", $translator->trans("msg_no_onu_in_map", array(), "FTTHBundle"));
                 $disabled = true;
                 $objectTypeId = 0;
             } else {
@@ -60,14 +60,14 @@ class ONUController extends Controller
                 $objectTypeId = $_types[0]['id'];
             }
         }
-        
+
         $form = $this->createFormBuilder()
-        ->add('mapId', RemoteMapType::class, array('label' => $translator->trans("form.label_map",array(),"FTTHBundle"), 'disabled' => $disabled))
-        ->add('clientId', RemoteClientType::class, array('label' => $translator->trans("form.label_client",array(),"FTTHBundle"), 'disabled' => $disabled))
-        ->getForm();
-        
-        $flashbag->add("warning", $translator->trans("msg_select_map",array(),"FTTHBundle"));
-        
+            ->add('mapId', RemoteMapType::class, array('label' => $translator->trans("form.label_map", array(), "FTTHBundle"), 'disabled' => $disabled))
+            ->add('clientId', RemoteClientType::class, array('label' => $translator->trans("form.label_client", array(), "FTTHBundle"), 'disabled' => $disabled))
+            ->getForm();
+
+        $flashbag->add("warning", $translator->trans("msg_select_map", array(), "FTTHBundle"));
+
         return $this->render('FTTHBundle:ONU:onu_import.html.twig', array(
             'base_template' => $adminPool->getTemplate('layout'),
             'admin_pool' => $adminPool,
@@ -83,38 +83,38 @@ class ONUController extends Controller
      * Queda pendiente enviar un comando para actualizar masivamente los devices
      * /admin/ftth/onu/onu_save?_=1508847664462&limit=100&mapId=3&offset=0&clientId=1
      */
-    public function onuSaveAction(Request $request) 
+    public function onuSaveAction(Request $request)
     {
-        $em = $this->get('doctrine')->getManager();        
+        $em = $this->get('doctrine')->getManager();
         $webservice = $this->get("webservice");
         $tenancy_service = $this->get("base_tenancy.tenancy_service");
 
         $mapId = $request->get('mapId');
         $clientId = $request->get('clientId');
-        
-        if(is_null($mapId) || empty($mapId) || is_null($clientId) || empty($clientId)) {
+
+        if (is_null($mapId) || empty($mapId) || is_null($clientId) || empty($clientId)) {
             $response = new Response(
                 json_encode(array()),
                 Response::HTTP_OK,
                 array('content-type' => 'application/json')
             );
-    
+
             return $response;
         }
-        
+
         $limit = $request->get('limit');
         $offset = $request->get('offset');
         $objectTypeId = $request->get('objectTypeId');
 
         $urlObjectTypes = $this->getParameter('remote_get_objects_url');
-        
+
         $filters = array('type' => $objectTypeId, 'map' => $mapId);
         $_onus = $webservice->getData($urlObjectTypes, $filters, array(), $limit, $offset);
 
         $return = array();
-        foreach($_onus as $onu) {
+        foreach ($_onus as $onu) {
             $object = $em->getRepository("FTTHBundle:ONU")->findOneByPonSerialNumber($onu['text']);
-            if(is_null($object)) {
+            if (is_null($object)) {
                 $object = new ONU();
                 $object->setClientId($clientId);
                 $object->setTenancyId($tenancy_service->getTenancyIdCurrent());
@@ -123,30 +123,30 @@ class ONUController extends Controller
                 $em->flush();
                 $return[] = "ONU create {$onu['text']}";
             }
-            
+
             $location = $object->getLocation();
             $action = "update";
-            if(is_null($location)) {
+            if (is_null($location)) {
                 $location = new Location();
                 $em->persist($location);
                 $em->flush();
                 $object->setLocation($location);
                 $action = "create";
             }
-            
-            $data = json_decode($onu['vector']['data'],true);
+
+            $data = json_decode($onu['vector']['data'], true);
             $mapLocation = $data[0];
             $mapLocation['zoom'] = 17;
             $mapLocation['onu'] = "{$object->getPonSerialNumber()}";
             $mapLocation['map_object_id'] = $onu['id'];
-            
+
             $location->setJsonExtraData($mapLocation);
             $location->setObjectTypeId($objectTypeId);
             $location->setMapId($mapId);
-            
+
             $em->persist($location);
-            
-            $return[] = "LOCATION (id {$location->getId()}) {$action} - ONU (id {$object->getId()}) {$onu['text']} - ".json_encode($mapLocation);
+
+            $return[] = "LOCATION (id {$location->getId()}) {$action} - ONU (id {$object->getId()}) {$onu['text']} - " . json_encode($mapLocation);
         }
 
         $em->flush();
@@ -162,7 +162,6 @@ class ONUController extends Controller
     }
 
 
-
     /**
      * @param Request $request
      * @return JsonResponse
@@ -238,9 +237,9 @@ class ONUController extends Controller
                 if (isset($extraData['lat']) && isset($extraData['lng'])) {
                     $distance = $this->distanceGeoPoints($lat, $lng, $extraData['lat'], $extraData['lng'], true);
                     // divido por 1000 para pasarlo de metros a kilometros
-                    $naps[$nap->getId()] = ['id' => $nap->getId(), 'name' => $nap->getName(), 'distance' => $distance, 'freePort' => $nap->getFreePort()];
+                    $naps[$nap->getId()] = ['id' => $nap->getId(), 'name' => $nap->getName(), 'distance' => $distance, 'freePort' => $nap->getFreePort(), 'address' => $extraData['Address']];
                 } else {
-                    $naps[$nap->getId()] = ['id' => $nap->getId(), 'name' => $nap->getName(), 'distance' => -1, 'freePort' => $nap->getFreePort()];
+                    $naps[$nap->getId()] = ['id' => $nap->getId(), 'name' => $nap->getName(), 'distance' => -1, 'freePort' => $nap->getFreePort(), 'address' => ''];
                 }
             }
         }

+ 13 - 10
src/FTTHBundle/Entity/NAP.php

@@ -47,14 +47,14 @@ class NAP implements TenancyIdTraitInterface, LocationInterface
 
     /**
      * @ORM\ManyToOne(targetEntity="NAP", fetch="EXTRA_LAZY")
-     * 
+     *
      * @JMS\MaxDepth(1)
      */
     protected $parent;
 
     /**
      * @ORM\OneToMany(targetEntity="ONU", mappedBy="nap", fetch="EXTRA_LAZY")
-     * 
+     *
      * @JMS\MaxDepth(2)
      */
     protected $onus;
@@ -141,18 +141,21 @@ class NAP implements TenancyIdTraitInterface, LocationInterface
 
 
     /**
-     * @return int Retorna la cantidad de puertos libres.
+     * @return int Retorna la cantidad de puertos libres de la caja nap.
      */
     public function getFreePort()
     {
         $em = $this->getContainer()->get("doctrine.orm.entity_manager");
-        $query = $em->createQuery('SELECT n.id, COUNT(DISTINCT n.id) AS usedPort FROM ' . ONU::class . ' o JOIN o.nap n where n.id = ' . $this->getId());
-        $onusQuery = $query->getResult();
-        $freePort = $this::MAX_NUMBER_ONU;
-        foreach ($onusQuery as $key => $nap) {
-            if (is_numeric($nap['usedPort'])) {
-                $freePort = NAP::MAX_NUMBER_ONU - $nap['usedPort'];
-            }
+        $repo = $em->getRepository(ONU::class);
+        $qb = $repo->createQueryBuilder('o');
+        $onus = $qb->select('count(o.id)')
+            ->where($qb->expr()->eq('o.nap', $this->getId()))
+            ->getQuery()
+            ->getSingleScalarResult();
+        $extraData = json_decode($this->getExtraData(), true);
+        $freePort = $extraData['capacity'];
+        if (is_numeric($onus)) {
+            $freePort = $extraData['capacity'] - $onus;
         }
         return $freePort;
     }

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

@@ -194,4 +194,4 @@ alert_select_map_client: Debe seleccionar un mapa y cliente
 msg_import_onu: Importar ONUs
 msg_init_import: Iniciando importación, espere un momento.
 msg_end_import: FIN :)
-
+Address: Dirección

+ 18 - 10
src/FTTHBundle/Resources/views/ONU/form.html.twig

@@ -1,12 +1,13 @@
 {% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %}
 
 
-{% block javascripts %}
+{% block formactions %}
     {{ parent() }}
     <script type="text/javascript">
         $(function () {
             // buscar la direccion del cliente y la muestra en el mapa.
-            $('select[id$="clientId"]').change(function () {
+            var objSelectClient = $('select[id$="clientId"]');
+            objSelectClient.change(function () {
                 $.ajax({
                     url: '{{ path('ajax_client_data') }}',
                     dataType: 'json',
@@ -15,6 +16,7 @@
                         q: $(this).val()
                     }
                 }).done(function (data) {
+                    console.log(data);
                     var extradata = null;
                     if (data.results[0].location) {
                         extradata = JSON.parse(data.results[0].location.extraData);
@@ -30,6 +32,10 @@
                     }
                 });
             });
+
+            if (objSelectClient.val() != "" && parseInt(objSelectClient.val()) > 0) {
+                calcularDistanciaNap();
+            }
         });
 
         /**
@@ -89,27 +95,29 @@
                     obj = JSON.parse(obj);
                     for (var i = 0; i < obj.length; i++) {
                         if (obj[i].distance != -1) {
+                            var tmp = obj[i].name + " ({{ 'Free Port'|trans({}, 'FTTHBundle') }}: " + obj[i].freePort + " - " +
+                                "{{ 'Distance'|trans({}, 'FTTHBundle') }}: " + obj[i].distance + " KM. - " +
+                                "{{ 'Address'|trans({}, 'FTTHBundle') }}: " + obj[i].address + ")";
                             select.append(
-                                '<option value="' + obj[i].id + '">' +
-                                obj[i].name + " ({{ 'Free Port'|trans({}, 'FTTHBundle') }}: " + obj[i].freePort + " - " +
-                                "{{ 'Distance'|trans({}, 'FTTHBundle') }}: " + obj[i].distance + " KM.)" +
+                                '<option value="' + obj[i].id + '">' + tmp +
                                 '</option>'
                             );
                         }
                     }
                     for (var i = 0; i < obj.length; i++) {
                         if (obj[i].distance == -1) {
+                            var tmp = obj[i].name + " ({{ 'Free Port'|trans({}, 'FTTHBundle') }}: " + obj[i].freePort;
+                            if (obj[i].address != null && obj[i].address.length > 0) {
+                                tmp += " - " + "{{ 'Address'|trans({}, 'FTTHBundle') }}: " + obj[i].address;
+                            }
+                            tmp += ")";
                             select.append(
-                                '<option value="' + obj[i].id + '">' +
-                                obj[i].name + " ({{ 'Free Port'|trans({}, 'FTTHBundle') }}: " + obj[i].freePort + ")" +
+                                '<option value="' + obj[i].id + '">' + tmp +
                                 '</option>'
                             );
                         }
                     }
                 });
-            }).error(function (res) {
-                console.log("ERROR: ");
-                console.log(res);
             });
         }
     </script>