angular.module('focaLogisticaPedidoRuta') .controller('focaLogisticaPedidoRutaController', [ '$scope', 'focaLogisticaPedidoRutaService', '$location', '$uibModal', '$filter', 'focaModalService', 'focaBotoneraLateralService', '$interval', function($scope, focaLogisticaPedidoRutaService, $location, $uibModal, $filter, focaModalService, focaBotoneraLateralService, $interval ) { $scope.actividad = 'Logistica'; //Datos Pantalla $scope.titulo = 'Logistica de Pedidos'; var transportista = { label: 'Vehículo', image: 'abmChofer.png' }; var fecha = { label: 'Fecha Reparto', image: 'fechaDeReparto.png' }; $scope.botonera = [fecha, transportista]; var cabecera = ''; $scope.now = new Date(); $scope.idVendedor = 0; $scope.marcadores = []; $scope.vehiculos = []; getSeguimiento(); $scope.arrastrando = false; $scope.general = function() { $scope.idVendedor = 0; getSeguimiento(); }; //SETEO BOTONERA LATERAL focaBotoneraLateralService.showSalir(true); focaBotoneraLateralService.showPausar(false); focaBotoneraLateralService.showGuardar(false); $scope.general = function() { $scope.idVendedor = 0; getSeguimiento(); $scope.$broadcast('removeCabecera', cabecera); $scope.$broadcast('addCabecera', { label: 'General', valor: '' }); }; $scope.cargar = function(idVehiculo, punto) { if(!eligioFecha()) return; var idRemito; if(punto === -1) { idRemito = -1; }else { idRemito = JSON.parse(punto).notaPedido.remito.id; } var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Vehiculo', templateUrl: 'foca-detalle-vehiculo.html', controller: 'focaDetalleVehiculo', size: 'lg', resolve: { idVehiculo: function() {return idVehiculo;}, idRemito: function() {return idRemito;}, fechaReparto: function() {return $scope.fechaReparto;} } } ); modalInstance.result.then(function() { }, function() { }); }; $scope.quitarVehiculo = function(vehiculo) { if(!eligioFecha() || vehiculoEnUso(vehiculo)) return; focaModalService.confirm('Esta seguro que desea eliminar el vehículo ' + vehiculo.codigo + '?').then(function() { eliminarVehiculo(vehiculo); }); }; $scope.hacerHojaRuta = function(vehiculo) { if(!eligioFecha() || vehiculoEnUso(vehiculo)) return; var modalInstance = $uibModal.open( { ariaLabelledBy: 'Creación hoja ruta', templateUrl: 'foca-modal-cerrar-vehiculo.html', controller: 'focaModalCerrarVehiculo', size: 'lg', resolve: { idVehiculo: function() {return vehiculo.id;}, fechaReparto: function() {return $scope.fechaReparto;} } } ); modalInstance.result.then(function() { }, function() { //usar cuando se cancela el modal }); }; $scope.arrastra = function() { $scope.arrastrando = true; $scope.$digest(); }; $scope.noArrastra = function() { $scope.arrastrando = false; $scope.$digest(); }; $scope.individual = function() { $scope.idVendedor = -1; }; $scope.mostrarDetalle = function() { $scope.detalle = true; }; $scope.salir = function() { $location.path('/'); }; $scope.search = function() { getSeguimiento(); }; $scope.fecha = function() { getSeguimiento(); }; $scope.seleccionarUnidad = function() { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Transportista', templateUrl: 'modal-proveedor.html', controller: 'focaModalProveedorCtrl', size: 'lg', resolve: { transportista: function() { return true; } } } ); modalInstance.result.then(function(transportista) { $scope.seleccionarVehiculo(transportista.COD); }); }; $scope.seleccionarVehiculo = function(idTransportista) { var query = '/vehiculo/transportista/' + idTransportista; var columnas = { nombre: ['Código', 'tractor', 'Semi', 'Capacidad'], propiedad: ['codigo', 'tractor', 'semi', 'capacidadTotalCisternas'] }; var titulo = 'Búsqueda de vehiculos'; focaModalService.modal(columnas, query, titulo).then( function(vehiculo) { var existe = $filter('filter')($scope.vehiculos, {id: vehiculo.id}); if(existe.length) { focaModalService.alert('El vehiculo ya ha sido cargado'); return; } if(!vehiculo.cisternas.length) { focaModalService.alert('El vehiculo no tiene cisternas'); return; } $scope.vehiculos.push(vehiculo); }, function() { // funcion ejecutada cuando se cancela el modal }); }; $scope.seleccionarFechaReparto = function() { focaModalService.modalFecha('Fecha de reparto').then(function(fecha) { $scope.$broadcast('addCabecera',{ label: 'Fecha:', valor: fecha.toLocaleDateString() }); $scope.fechaReparto = fecha; focaLogisticaPedidoRutaService.setFechaReparto(fecha); focaLogisticaPedidoRutaService.getUnidadesByFecha(fecha).then(function(res) { $scope.vehiculos = res.data; }); }); }; function getSeguimiento() { var desde = new Date('1900/01/01'); var hasta = new Date('2099/01/01'); if($scope.fechaDesde) { var fechaDesde = $scope.fechaDesde; desde = new Date(new Date(fechaDesde.setHours(0)).setMinutes(0)); desde = new Date(desde); } if($scope.fechaHasta) { var fechaHasta = $scope.fechaHasta; hasta = new Date(new Date(fechaHasta.setHours(0)).setMinutes(0)); hasta = hasta.setDate(hasta.getDate() + 1); hasta = new Date(hasta); } var datos = { actividad: $scope.actividad, idUsuario: $scope.idVendedor, fechaDesde: desde, fechaHasta: hasta, asignacion: $scope.filtroEstado ? true : ($scope.filtroEstado !== undefined ? false : undefined) }; $scope.datosBuscados = { actividad: $scope.actividad, individual: $scope.idVendedor ? true : false }; focaLogisticaPedidoRutaService.obtenerActividad(datos).then(function(datos) { if(JSON.stringify(datos.data) !== JSON.stringify($scope.marcadores)) { $scope.marcadores = datos.data; } }); } function eliminarVehiculo(vehiculo) { focaLogisticaPedidoRutaService.getRemitos(vehiculo.id).then(function(res) { if(!focaLogisticaPedidoRutaService.obtenerRemitosDeCarga(res.data).length) { $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 1); }else { focaModalService.alert('No ha sido posible eliminar el vehiculo porque ' + 'tiene remitos asociados').then(function() { $scope.hacerHojaRuta(vehiculo); }); } }); } function eligioFecha() { if(!$scope.fechaReparto) { focaModalService.alert('Primero seleccione fecha de reparto'); return false; } return true; } function vehiculoEnUso(vehiculo) { var idUsuario = focaLogisticaPedidoRutaService.idUsuario; for(var i = 0; i < vehiculo.cisternas.length; i++) { for(var j = 0; j < vehiculo.cisternas[i].cisternasCarga.length; j++) { var cisternaCarga = vehiculo.cisternas[i].cisternasCarga[j]; if(cisternaCarga.fechaReparto.substring(0, 10) === $scope.fechaReparto .toISOString().substring(0, 10) && cisternaCarga.idUsuarioProceso && cisternaCarga.idUsuarioProceso !== idUsuario) { focaModalService.alert('El vehículo está siendo usado por otro usuario'); return true; } } } return false; } $interval(function() { getSeguimiento(); }, 5000); } ]);