angular.module('focaAbmVehiculo') .controller('focaAbmVehiculosController', [ '$scope', 'focaAbmVehiculoService', '$location', 'focaModalService', '$uibModal', 'focaBotoneraLateralService', '$timeout', function($scope, focaAbmVehiculoService, $location, focaModalService, $uibModal, focaBotoneraLateralService, $timeout) { $scope.now = new Date(); $scope.botonera = [{ label: 'Transportista', image: 'cliente.png' }]; focaAbmVehiculoService.cleanCisternas(); //SETEO BOTONERA LATERAL focaBotoneraLateralService.showSalir(false); focaBotoneraLateralService.showPausar(false); focaBotoneraLateralService.showCancelar(false); focaBotoneraLateralService.showGuardar(false); focaBotoneraLateralService.addCustomButton('Salir', salir); if (focaAbmVehiculoService.transportistaSeleccionado.COD) { elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado); } $scope.editar = function(id) { $location.path('/vehiculo/' + id + '/' + $scope.idTransportista); }; $scope.solicitarConfirmacion = function(vehiculo) { focaModalService.confirm('¿Está seguro que desea borrar el vehiculo ' + vehiculo.id + ' ' + vehiculo.tractor + ' ?').then( function(data) { if (data) { focaAbmVehiculoService.deleteVehiculo(vehiculo.id); $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 1); } } ); }; $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) { elegirTransportista(transportista); focaAbmVehiculoService.transportistaSeleccionado = transportista; }, function() { } ); }; function elegirTransportista(transportista) { var codigo = ('00000' + transportista.COD).slice(-5); $scope.idTransportista = transportista.COD; $scope.filtros = transportista.NOM.trim(); $timeout(function() { $scope.$broadcast('addCabecera', { label: 'Transportista:', valor: codigo + ' - ' + transportista.NOM }); }); buscar(transportista.COD); } function buscar(idTransportista) { focaAbmVehiculoService .getVehiculosPorTransportista(idTransportista) .then(function(datos) { $scope.vehiculos = datos.data; }); } function salir() { focaAbmVehiculoService.transportistaSeleccionado = {}; $location.path('/'); } } ]) .controller('focaAbmVehiculoController', [ '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal', 'focaModalService', '$timeout', 'focaBotoneraLateralService', function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal, focaModalService, $timeout, focaBotoneraLateralService) { $scope.nuevo = $routeParams.idVehiculo === '0' ? true : false; $scope.now = new Date(); $scope.focused = 1; $scope.transportistaStamp = ''; $scope.cisternas = []; $timeout(function() { focaBotoneraLateralService.showSalir(false); focaBotoneraLateralService.showPausar(false); focaBotoneraLateralService.showCancelar(false); focaBotoneraLateralService.showGuardar(true, $scope.guardar); focaBotoneraLateralService.addCustomButton('Salir', $scope.cancelar); }); if ($scope.nuevo) { focaAbmVehiculoService .getTransportistaPorId($routeParams.idTransportista) .then(function(res) { var codigo = ('00000' + res.data.COD).slice(-5); $scope.vehiculo.idTransportista = res.data.COD; $scope.vehiculo.transportista = res.data; $scope.$broadcast('addCabecera', { label: 'Transportista:', valor: codigo + ' - ' + res.data.NOM }); }); } $scope.vehiculo = {}; focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) { if (res.data) { $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5); $scope.transportistaStamp += ' - ' + res.data.transportista.NOM; $scope.vehiculo = res.data; $scope.$broadcast('addCabecera', { label: 'Transportista:', valor: $scope.transportistaStamp }); $scope.$broadcast('addCabecera', { label: 'Unidad:', valor: res.data.codigo }); focaAbmVehiculoService .getCisternas($routeParams.idVehiculo) .then(function(res) { $scope.cisternas = res; $scope.$apply(); }); } }); $scope.next = function(key) { if (key === 13) $scope.focused++; }; $scope.cancelar = function() { $location.path('/vehiculo'); }; $scope.editar = function(key) { if (key) { $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/' + key); } else { $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/0/'); } }; $scope.guardar = function(key) { key = (typeof key === 'undefined') ? 13 : key; if (key === 13) { if ($scope.formVehiculo.$pristine) { $scope.cancelar(); return; } //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'); return; } validaCodigoUnidad().then(function() { delete $scope.vehiculo.transportista; delete $scope.vehiculo.cisternas; focaAbmVehiculoService.guardarVehiculo($scope.vehiculo) .then(function(res) { if ($scope.nuevo) { $location.path('/vehiculo/' + res.data.id + '/' + res.data.idTransportista); } else { guardarCisternas().then(function() { $location.path('/vehiculo'); }); } }); }, function() { focaModalService.alert('Código de unidad existente'); }); } }; $scope.solicitarConfirmacionCisterna = function(cisterna, idx) { focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + cisterna.id + ' ' + cisterna.codigo + ' ?').then( function(data) { if (data) { focaAbmVehiculoService.deleteCisterna(idx); focaAbmVehiculoService .getCisternas($routeParams.idVehiculo) .then(function(res) { $scope.cisternas = res; }); } } ); }; function validaCodigoUnidad() { return new Promise(function(resolve, reject) { focaAbmVehiculoService .getVehiculosPorTransportista(parseInt($routeParams.idTransportista)) .then(function(res) { //Valida si existe numero de unidad var existe = res.data.filter(function(vehiculo) { return vehiculo.codigo === $scope.vehiculo.codigo && vehiculo.id !== $scope.vehiculo.id; }); if (existe.length) { reject(existe); } else { resolve(); } }); }); } function validaTotalCargas() { var total = 0; $scope.cisternas.forEach(function(cisterna) { if (!cisterna.desactivado) { total += parseInt(cisterna.capacidad); } }); return $scope.vehiculo.capacidad === total; } function guardarCisternas() { var cisternas = $scope.cisternas.map(function(cisterna) { return { id: cisterna.id, capacidad: parseFloat(cisterna.capacidad), codigo: cisterna.codigo, idUnidadMedida: cisterna.idUnidadMedida, idVehiculo: $routeParams.idVehiculo, desactivado: cisterna.desactivado }; }); return focaAbmVehiculoService.guardarCisternas(cisternas); } } ]);