angular.module('focaAbmChofer') .controller('focaAbmChoferesController', [ '$scope', 'focaAbmChoferService', '$location', '$uibModal', 'focaModalService', 'focaBotoneraLateralService', '$timeout', '$localStorage', '$routeParams', '$filter', function($scope, focaAbmChoferService, $location, $uibModal, focaModalService, 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(true, $scope.guardar); focaBotoneraLateralService.addCustomButton('Salir', salir); }); if (focaAbmChoferService.transportistaSeleccionado.COD) { elegirTransportista(focaAbmChoferService.transportistaSeleccionado); } 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.nombre) { focaModalService.alert('Ingrese nombre'); return; } else if (!chofer.idTipoDocumento) { focaModalService.alert('Ingrese tipo documento'); return; } validaDni(chofer); }; $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) { chofer.desactivado = true; } }); }; $scope.seleccionarTransportista = function() { var parametrosModal = { titulo: 'Búsqueda de Transportista', query: '/transportista', columnas: [ { nombre: 'Código', propiedad: 'COD' }, { nombre: 'Nombre', propiedad: 'NOM' }, { nombre: 'CUIT', propiedad: 'CUIT' } ] }; focaModalService.modal(parametrosModal).then( function(transportista) { $scope.crear = true; elegirTransportista(transportista); focaAbmChoferService.transportistaSeleccionado = transportista; }, function() {} ); }; function elegirTransportista(transportista) { buscar(transportista.COD); var codigo = ('00000' + transportista.COD).slice(-5); $scope.idTransportista = transportista.COD; $timeout(function() { $scope.$broadcast('addCabecera', { label: 'Transportista:', valor: codigo + ' - ' + transportista.NOM }); }); } function buscar(id) { focaAbmChoferService.getChoferPorTransportista(id).then(function(res) { $scope.choferes = res.data; }); } function salir() { focaAbmChoferService.transportistaSeleccionado = {}; $location.path('/'); } function validaDni(chofer) { if (!chofer.dni) { focaModalService.alert('Ingrese DNI'); return; } else if (!chofer.telefono) { focaModalService.alert('Ingrese teléfono'); return; } 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(); } }) .then(function() { chofer.idTransportista = focaAbmChoferService.transportistaSeleccionado.COD; delete chofer.transportista; }, function() { focaModalService.alert('Dni existente'); $scope.editando = true; }); $scope.crear = true; chofer.editando = false; }); } if ($localStorage.chofer) { var chofer = JSON.parse($localStorage.chofer); if (!chofer.id) { chofer.id = 0; } $location.path('/chofer/' + chofer.id + '/' + chofer.idTransportista); } } ]);