diff --git a/src/js/controller.js b/src/js/controller.js index 60dfff3..c9c6ea3 100644 --- a/src/js/controller.js +++ b/src/js/controller.js @@ -2,40 +2,137 @@ angular.module('focaAbmChofer') .controller('focaAbmChoferesController', [ '$scope', 'focaAbmChoferService', '$location', '$uibModal', 'focaModalService', 'focaBotoneraLateralService', '$timeout', '$localStorage', + '$routeParams', '$filter', function($scope, focaAbmChoferService, $location, $uibModal, focaModalService, - focaBotoneraLateralService, $timeout, $localStorage) { + focaBotoneraLateralService, $timeout, $localStorage, $routeParams, $filter) { + $scope.focused = 1; $scope.now = new Date(); + $scope.nuevo = $routeParams.id === '0'; $scope.filters = ''; $scope.choferes = []; + $scope.creando = false; + $scope.crear = false; + $scope.transportistas = []; $scope.botonera = [{ label: 'Transportista', image: 'cliente.png' }]; + $scope.next = function(key) { + if (key === 13) $scope.focused++; + }; //SETEO BOTONERA LATERAL $timeout(function() { focaBotoneraLateralService.showSalir(false); focaBotoneraLateralService.showPausar(false); focaBotoneraLateralService.showCancelar(false); - focaBotoneraLateralService.showGuardar(false); + focaBotoneraLateralService.showGuardar(true, $scope.guardar); focaBotoneraLateralService.addCustomButton('Salir', salir); }); - + if (focaAbmChoferService.transportistaSeleccionado.COD) { elegirTransportista(focaAbmChoferService.transportistaSeleccionado); } - $scope.editar = function(id) { - $location.path('/chofer/' + id + '/' + $scope.idTransportista); + focaAbmChoferService.getTiposDocumento().then(function(res) { + $scope.tiposDocumento = res.data; + }); + + $scope.crearChofer = function () { + var chofer = { + id: 0, + nombre: '', + telefono: '', + editando: true, + }; + $scope.choferes.unshift(chofer); + $scope.crear = false; + }; + + $scope.editar = function(chofer) { + $scope.choferes.forEach(function(chofer) { + chofer.editando = false; + $scope.crear = false; + }); + chofer.editando = true; + $scope.inicial = angular.copy(chofer); + }; + + $scope.agregarChofer = function (chofer) { + if (!chofer) { + focaModalService.alert('Ingrese nombre'); + return; + } else if (!chofer.idTipoDocumento) { + focaModalService.alert('Ingrese tipo documento'); + return; + } else if (!chofer.dni) { + focaModalService.alert('Ingrese DNI'); + return; + } else if (!chofer.telefono) { + focaModalService.alert('Ingrese teléfono'); + return; + } + + validaDni(chofer).then(function() { + chofer.idTransportista = focaAbmChoferService.transportistaSeleccionado.COD; + delete chofer.transportista; + }, function() { + focaModalService.alert('Dni existente'); + $scope.editando = true; + }); + $scope.crear = true; + chofer.editando = false; + }; + + $scope.tipoDocumento = function (idTipoDocumento) { + var value = ''; + switch (parseInt(idTipoDocumento)) { + case 96 : + value = 'DNI'; + break; + case 80 : + value = 'CUIT'; + break; + case 86 : + value = 'CUIL'; + break; + default: + value = ''; + break; + } + return value; + }; + + $scope.volver = function (chofer, key) { + if (chofer.idTransportista === undefined) { + $scope.choferes.shift(); + $scope.crear = true; + chofer.editando = false; + return; + } else if (chofer.id !== 0 || !$scope.crear) { + $scope.choferes[key] = $scope.inicial; + $scope.choferes[key].editando = false; + } + $scope.crear = true; + }; + + $scope.guardar = function() { + $scope.choferes.forEach( function (chofer) { + if (chofer.id === 0) { + delete chofer.id; + } + delete chofer.transportista; + delete chofer.editando; + }); + focaAbmChoferService.guardarChoferes($scope.choferes); }; $scope.solicitarConfirmacion = function(chofer) { focaModalService.confirm('¿Está seguro que desea borrar el chofer ' + chofer.nombre + ' ?').then(function(confirmed) { if (confirmed) { - focaAbmChoferService.deleteChofer(chofer.id); - $scope.choferes.splice($scope.choferes.indexOf(chofer), 1); + chofer.desactivado = true; } }); }; @@ -61,6 +158,7 @@ angular.module('focaAbmChofer') }; focaModalService.modal(parametrosModal).then( function(transportista) { + $scope.crear = true; elegirTransportista(transportista); focaAbmChoferService.transportistaSeleccionado = transportista; }, function() {} @@ -90,72 +188,28 @@ angular.module('focaAbmChofer') $location.path('/'); } + function validaDni(chofer) { + return new Promise(function(resolve, reject) { + focaAbmChoferService + .getChoferPorDni(chofer.dni) + .then(function(res) { + if (res.data.id && + chofer.id !== res.data.id) { + reject(res.data); + } else { + resolve(); + } + }); + }); + } + if ($localStorage.chofer) { var chofer = JSON.parse($localStorage.chofer); if (!chofer.id) { chofer.id = 0; } $location.path('/chofer/' + chofer.id + '/' + chofer.idTransportista); } - } - ]) - .controller('focaAbmChoferController', [ - '$scope', 'focaAbmChoferService', '$routeParams', '$localStorage', '$filter', - '$location', 'focaBotoneraLateralService', '$timeout', 'focaModalService', '$window', - function($scope, focaAbmChoferService, $routeParams, $localStorage, $filter, - $location, focaBotoneraLateralService, $timeout, focaModalService){ - $scope.focused = 1; - $scope.nuevo = $routeParams.id === '0'; - $scope.chofer = {}; - $scope.transportistas = []; - $scope.now = new Date(); - $scope.next = function(key) { - if (key === 13) $scope.focused++; - }; - - focaAbmChoferService.getTiposDocumento().then(function(res) { - $scope.tiposDocumento = res.data; - }); - - //SETEO BOTONERA LATERAL - $timeout(function() { - focaBotoneraLateralService.showSalir(false); - focaBotoneraLateralService.showPausar(true); - focaBotoneraLateralService.showCancelar(false); - focaBotoneraLateralService.showGuardar(true, $scope.guardar); - focaBotoneraLateralService.addCustomButton('Salir', salir); - - }); - - $timeout(function() {getLSChofer();}); - - if ($scope.nuevo) { - focaAbmChoferService - .getTransportistaPorId($routeParams.idTransportista) - .then(function(res) { - var codigo = ('00000' + res.data.COD).slice(-5); - $scope.chofer.idTransportista = res.data.COD; - $scope.chofer.transportista = res.data; - $scope.$broadcast('addCabecera', { - label: 'Transportista:', - valor: codigo + ' - ' + res.data.NOM - }); - }); - } - - focaAbmChoferService.getChofer($routeParams.id).then(function(res) { - if (res.data) { - var codigo = ('00000' + res.data.transportista.COD).slice(-5); - $scope.chofer = res.data; - $scope.$broadcast('addCabecera', { - label: 'Transportista:', - valor: codigo + ' - ' + res.data.transportista.NOM - }); - } - }); - - focaAbmChoferService.getTransportistas().then(function(res) { - $scope.transportistas = res.data; - }); + /*$timeout(function() {getLSChofer();}); function setearChofer(chofer) { $scope.chofer = chofer; @@ -164,6 +218,7 @@ angular.module('focaAbmChofer') valor: $filter('rellenarDigitos')(chofer.idTransportista.chofer, 2) + ' - ' }); } + function getLSChofer() { var chofer = JSON.parse($localStorage.chofer || null); @@ -173,40 +228,6 @@ angular.module('focaAbmChofer') } } - $scope.cancelar = function() { - $location.path('/chofer'); - }; - - $scope.guardar = function(key) { - - key = (typeof key === 'undefined') ? 13 : key; - if (key === 13) { - if (!$scope.chofer.nombre) { - focaModalService.alert('Ingrese nombre'); - return; - } else if (!$scope.chofer.idTipoDocumento) { - focaModalService.alert('Seleccione tipo de documento'); - return; - } else if (!$scope.chofer.dni) { - focaModalService.alert('Ingrese DNI'); - return; - } else if (!$scope.chofer.telefono) { - focaModalService.alert('Ingrese número de teléfono'); - return; - } - - validaDni().then(function() { - $scope.chofer.idTransportista = $routeParams.idTransportista; - delete $scope.chofer.transportista; - focaAbmChoferService.guardarChofer($scope.chofer).then(function() { - $location.path('/chofer'); - }); - }, function() { - focaModalService.alert('Dni existente'); - }); - } - }; - $scope.$watch('chofer', function(newValue) { focaBotoneraLateralService.setPausarData({ label: 'chofer', @@ -219,35 +240,7 @@ angular.module('focaAbmChofer') id: newValue.id } }); - }, true); + }, true);*/ - function salir() { - if ($scope.formChofer.$pristine == false) { - focaModalService.confirm( - '¿Está seguro de que desea salir? Se perderán todos los datos cargados.' - ).then(function(data) { - if (data) { - $location.path('/chofer'); - } - }); - } else { - $location.path('/chofer'); - } - } - - function validaDni() { - return new Promise(function(resolve, reject) { - focaAbmChoferService - .getChoferPorDni($scope.chofer.dni) - .then(function(res) { - if (res.data.id && - $scope.chofer.id !== res.data.id) { - reject(res.data); - } else { - resolve(); - } - }); - }); - } } ]); diff --git a/src/js/service.js b/src/js/service.js index 430e061..6f4e51f 100644 --- a/src/js/service.js +++ b/src/js/service.js @@ -16,6 +16,9 @@ angular.module('focaAbmChofer') guardarChofer: function(chofer) { return $http.post(API_ENDPOINT.URL + '/chofer', {chofer: chofer}); }, + guardarChoferes: function(choferes) { + return $http.post(API_ENDPOINT.URL + '/chofer', {choferes: choferes}); + }, getTransportistas: function() { return $http.get(API_ENDPOINT.URL + '/transportista'); }, diff --git a/src/views/foca-abm-choferes-listado.html b/src/views/foca-abm-choferes-listado.html index f660ac8..bce4609 100644 --- a/src/views/foca-abm-choferes-listado.html +++ b/src/views/foca-abm-choferes-listado.html @@ -16,29 +16,145 @@ Nombre + Tipo Documento Teléfono - - - - - + + + + + +
+ +
+ + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + + @@ -50,6 +166,20 @@ + + + +