From 7e8d4a2a31adcefce66e25965fc1d00271b0e841 Mon Sep 17 00:00:00 2001 From: Luigi Date: Thu, 2 May 2019 16:48:35 -0300 Subject: [PATCH] Alerta de codigo existente --- src/js/controller.js | 143 ++++++++++++++++++++++++++------------------------- 1 file changed, 73 insertions(+), 70 deletions(-) diff --git a/src/js/controller.js b/src/js/controller.js index 909620d..40bb439 100644 --- a/src/js/controller.js +++ b/src/js/controller.js @@ -2,7 +2,7 @@ angular.module('focaAbmVehiculo') .controller('focaAbmVehiculosController', [ '$scope', 'focaAbmVehiculoService', '$location', 'focaModalService', '$uibModal', 'focaBotoneraLateralService', '$timeout', '$localStorage', - function($scope, focaAbmVehiculoService, $location, focaModalService, + function ($scope, focaAbmVehiculoService, $location, focaModalService, $uibModal, focaBotoneraLateralService, $timeout, $localStorage) { $scope.now = new Date(); @@ -23,13 +23,13 @@ angular.module('focaAbmVehiculo') if (focaAbmVehiculoService.transportistaSeleccionado.COD) { elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado); } - $scope.editar = function(id) { + $scope.editar = function (id) { $location.path('/vehiculo/' + id + '/' + $scope.idTransportista); }; - $scope.solicitarConfirmacion = function(vehiculo) { + $scope.solicitarConfirmacion = function (vehiculo) { focaModalService.confirm('¿Está seguro que desea borrar el vehiculo ' + vehiculo.id + ' ' + vehiculo.tractor + ' ?').then( - function(data) { + function (data) { if (data) { focaAbmVehiculoService.deleteVehiculo(vehiculo.id); $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 1); @@ -37,7 +37,7 @@ angular.module('focaAbmVehiculo') } ); }; - $scope.seleccionarTransportista = function() { + $scope.seleccionarTransportista = function () { var parametrosModal = { titulo: 'Búsqueda de Transportista', query: '/transportista', @@ -57,10 +57,10 @@ angular.module('focaAbmVehiculo') ] }; focaModalService.modal(parametrosModal).then( - function(transportista) { + function (transportista) { elegirTransportista(transportista); focaAbmVehiculoService.transportistaSeleccionado = transportista; - }, function() { + }, function () { } ); @@ -69,7 +69,7 @@ angular.module('focaAbmVehiculo') var codigo = ('00000' + transportista.COD).slice(-5); $scope.idTransportista = transportista.COD; $scope.filtros = transportista.NOM.trim(); - $timeout(function() { + $timeout(function () { $scope.$broadcast('addCabecera', { label: 'Transportista:', valor: codigo + ' - ' + transportista.NOM @@ -81,7 +81,7 @@ angular.module('focaAbmVehiculo') function buscar(idTransportista) { focaAbmVehiculoService .getVehiculosPorTransportista(idTransportista) - .then(function(datos) { + .then(function (datos) { $scope.vehiculos = datos.data; }); } @@ -94,8 +94,8 @@ angular.module('focaAbmVehiculo') .controller('focaAbmVehiculoController', [ '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal', 'focaModalService', '$timeout', 'focaBotoneraLateralService', '$localStorage', '$filter', - function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal, - focaModalService, $timeout, focaBotoneraLateralService, $localStorage, $filter) { + function ($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal, + focaModalService, $timeout, focaBotoneraLateralService, $localStorage, $filter) { $scope.nuevo = $routeParams.idVehiculo === '0' ? true : false; $scope.nuevoCisterna = ($routeParams.idx > -1) ? false : true; $scope.now = new Date(); @@ -105,7 +105,7 @@ angular.module('focaAbmVehiculo') $scope.transportistaStamp = ''; $scope.cisternas = []; - $timeout(function() { + $timeout(function () { focaBotoneraLateralService.showSalir(false); focaBotoneraLateralService.showPausar(true); focaBotoneraLateralService.showCancelar(false); @@ -116,7 +116,7 @@ angular.module('focaAbmVehiculo') if ($scope.nuevo) { focaAbmVehiculoService .getTransportistaPorId($routeParams.idTransportista) - .then(function(res) { + .then(function (res) { var codigo = ('00000' + res.data.COD).slice(-5); $scope.vehiculo.idTransportista = res.data.COD; $scope.vehiculo.transportista = res.data; @@ -128,8 +128,8 @@ angular.module('focaAbmVehiculo') } $scope.vehiculo = {}; - focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) { - + focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function (res) { + if (res.data) { var vehiculoSeteado = getLSVehiculo(); if (vehiculoSeteado === false) { @@ -146,25 +146,25 @@ angular.module('focaAbmVehiculo') valor: res.data.codigo }); } - + focaAbmVehiculoService .getCisternas($routeParams.idVehiculo) - .then(function(res) { + .then(function (res) { $scope.cisternas = res; $scope.$apply(); - }); + }); } }); - $scope.next = function(key) { + $scope.next = function (key) { if (key === 13) $scope.focused++; }; - $scope.salir = function() { + $scope.salir = function () { if (!$scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) { focaModalService.confirm( '¿Está seguro de que desea salir? Se perderán todos los datos cargados.' - ).then(function(data) { + ).then(function (data) { if (data) { $location.path('/vehiculo'); } @@ -174,8 +174,8 @@ angular.module('focaAbmVehiculo') } }; - $scope.editar = function(cisterna) { - $scope.cisternas.forEach(function(cisterna) { + $scope.editar = function (cisterna) { + $scope.cisternas.forEach(function (cisterna) { cisterna.editando = false; $scope.crear = true; }); @@ -183,7 +183,7 @@ angular.module('focaAbmVehiculo') $scope.inicial = angular.copy(cisterna); }; - $scope.volver = function(cisterna, key) { + $scope.volver = function (cisterna, key) { if (cisterna.id === undefined || !$scope.crear) { $scope.cisternas.shift(cisterna); $scope.crear = false; @@ -196,7 +196,7 @@ angular.module('focaAbmVehiculo') $scope.crear = false; }; - $scope.crearCisterna = function() { + $scope.crearCisterna = function () { var cisterna = { codigo: '', capacidad: '', @@ -208,7 +208,7 @@ angular.module('focaAbmVehiculo') $scope.crear = true; }; - $scope.seleccionarUnidadMedida = function(cisterna) { + $scope.seleccionarUnidadMedida = function (cisterna) { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Unidades de medida', @@ -217,14 +217,14 @@ angular.module('focaAbmVehiculo') size: 'lg' } ); - modalInstance.result.then(function(unidaMedida) { + modalInstance.result.then(function (unidaMedida) { cisterna.idUnidadMedida = unidaMedida.ID; cisterna.unidadMedida = unidaMedida; }); }; - $scope.guardar = function(key) { - key = (typeof key === 'undefined') ? 13 : key; + $scope.guardar = function (key) { + key = (typeof key === 'undefined') ? 13 : key; if (key === 13) { @@ -232,7 +232,7 @@ angular.module('focaAbmVehiculo') $scope.salir(); return; } - $scope.cisternas.forEach( function(cisterna) { + $scope.cisternas.forEach(function (cisterna) { if (cisterna.id === 0) cisterna.id = undefined; }); @@ -252,33 +252,33 @@ angular.module('focaAbmVehiculo') //Valida si existe numero de unidad if (!validaTotalCargas() && !$scope.nuevo) { focaModalService.alert('La suma de las capacidades de las cisternas' + - ' debe ser igual a la capacidad total del vehículo'); + ' debe ser igual a la capacidad total del vehículo'); return; } - validaCodigoUnidad().then(function() { + validaCodigoUnidad().then(function () { delete $scope.vehiculo.transportista; delete $scope.vehiculo.cisternas; focaAbmVehiculoService.guardarVehiculo($scope.vehiculo) - .then(function(res) { + .then(function (res) { if ($scope.nuevo) { $location.path('/vehiculo/' + res.data.id + '/' + res.data.idTransportista); } else { - guardarCisternas($scope.cisternas).then(function() { + guardarCisternas($scope.cisternas).then(function () { $location.path('/vehiculo'); }); } }); - }, function() { + }, function () { focaModalService.alert('Código de unidad existente'); }); } }; //Agregar propiedades de cisterna - $scope.$watch('vehiculo', function(newValue) { + $scope.$watch('vehiculo', function (newValue) { focaBotoneraLateralService.setPausarData({ - label:'vehiculo', + label: 'vehiculo', val: { codigo: newValue.codigo, tractor: newValue.tractor, @@ -291,7 +291,7 @@ angular.module('focaAbmVehiculo') }, true); function getLSVehiculo() { - var vehiculo = JSON.parse($localStorage.vehiculo|| null); + var vehiculo = JSON.parse($localStorage.vehiculo || null); if (vehiculo) { setearVehiculo(vehiculo); delete $localStorage.vehiculo; @@ -308,9 +308,9 @@ angular.module('focaAbmVehiculo') }); } - $scope.solicitarConfirmacionTabla = function(cisterna) { + $scope.solicitarConfirmacionTabla = function (cisterna) { focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + cisterna.id + ' ' + cisterna.codigo + ' ?') - .then( function(data) { + .then(function (data) { if (data) { cisterna.desactivado = true; } @@ -319,12 +319,12 @@ angular.module('focaAbmVehiculo') }; function validaCodigoUnidad() { - return new Promise(function(resolve, reject) { + return new Promise(function (resolve, reject) { focaAbmVehiculoService .getVehiculosPorTransportista(parseInt($routeParams.idTransportista)) - .then(function(res) { + .then(function (res) { //Valida si existe numero de unidad - var existe = res.data.filter(function(vehiculo) { + var existe = res.data.filter(function (vehiculo) { return vehiculo.codigo === $scope.vehiculo.codigo && vehiculo.id !== $scope.vehiculo.id; }); @@ -340,7 +340,7 @@ angular.module('focaAbmVehiculo') function validaTotalCargas() { var total = 0; - $scope.cisternas.forEach(function(cisterna) { + $scope.cisternas.forEach(function (cisterna) { if (!cisterna.desactivado) { total += parseInt(cisterna.capacidad); } @@ -348,7 +348,7 @@ angular.module('focaAbmVehiculo') return $scope.vehiculo.capacidad >= total; } function guardarCisternas() { - var cisternas = $scope.cisternas.map(function(cisterna) { + var cisternas = $scope.cisternas.map(function (cisterna) { return { id: cisterna.id, capacidad: parseFloat(cisterna.capacidad), @@ -362,7 +362,7 @@ angular.module('focaAbmVehiculo') return focaAbmVehiculoService.guardarCisternas(cisternas); } - $scope.agregarCisterna = function(cisterna) { + $scope.agregarCisterna = function (cisterna) { if (!cisterna) { focaModalService.alert('Ingrese valores'); return; @@ -375,47 +375,50 @@ angular.module('focaAbmVehiculo') } else if (!cisterna.idUnidadMedida) { focaModalService.alert('Ingrese unidad de medida'); return; + } else if (cisterna.id === undefined) { + cisterna.id = 0; } validaCodigo(cisterna); - cisterna.id = 0; }; if ($routeParams.idx !== -1) { $scope.cisterna = [$routeParams.idx]; focaAbmVehiculoService .getCisternas($routeParams.idVehiculo) - .then(function(res) { + .then(function (res) { $scope.cisterna = res[$routeParams.idx]; }); } function validaCodigo(cisterna) { focaAbmVehiculoService - .getCisternas($routeParams.idVehiculo) - .then(function(res) { - var cisternas = res; - var totalCargado = 0; - cisternas.forEach(function(cisternaBase, idx) { - //SI EL CODIGO YA EXISTE - if (cisternaBase.codigo === cisterna.codigo && - idx !== $routeParams.idx && - cisternaBase.desactivado === false && - cisternaBase.id !== cisterna.id - ) - { + .getCisternas($routeParams.idVehiculo) + .then(function (res) { + var cisternas = res; + var totalCargado = 0; + + for (var i = 0; i < cisternas.length; i++) { + //SI EL CODIGO YA EXISTE + if (cisternas[i].codigo === cisterna.codigo && + i !== $routeParams.idx && + cisternas[i].desactivado === false && + cisternas[i].id !== cisterna.id + ) { focaModalService.alert('Código de cisterna existente'); + cisterna.editando = true; + return; } - if (idx !== $routeParams.idx && - !cisternaBase.desactivado) { - totalCargado += cisternaBase.capacidad; - } - }); - $timeout( function(){ - cisterna.editando = false; - $scope.crear = false; - }); + if (i !== $routeParams.idx && + !cisternas[i].desactivado) { + totalCargado += cisternas[i].capacidad; + } + } + $timeout(function () { + cisterna.editando = false; + $scope.crear = false; }); - } + }); + } } ]); -- 1.9.1