Commit e4296f6a85d8138ed3f3f0870db191beaa0e3e87
1 parent
4deaf827fb
Exists in
master
espacio
Showing
1 changed file
with
1 additions
and
1 deletions
Show diff stats
src/js/controllerDetalleVehiculo.js
| 1 | angular.module('focaLogisticaPedidoRuta') | 1 | angular.module('focaLogisticaPedidoRuta') |
| 2 | .controller('focaDetalleVehiculo', | 2 | .controller('focaDetalleVehiculo', |
| 3 | ['$scope', | 3 | ['$scope', |
| 4 | '$uibModalInstance', | 4 | '$uibModalInstance', |
| 5 | 'idVehiculo', | 5 | 'idVehiculo', |
| 6 | 'idRemito', | 6 | 'idRemito', |
| 7 | 'focaModalService', | 7 | 'focaModalService', |
| 8 | '$filter', | 8 | '$filter', |
| 9 | 'focaLogisticaPedidoRutaService', | 9 | 'focaLogisticaPedidoRutaService', |
| 10 | function($scope, $uibModalInstance, idVehiculo, idRemito, focaModalService, $filter, | 10 | function($scope, $uibModalInstance, idVehiculo, idRemito, focaModalService, $filter, |
| 11 | focaLogisticaPedidoRutaService | 11 | focaLogisticaPedidoRutaService |
| 12 | ) { | 12 | ) { |
| 13 | //seteo variables | 13 | //seteo variables |
| 14 | $scope.cargandoDatos = true; | 14 | $scope.cargandoDatos = true; |
| 15 | $scope.idRemito = idRemito; | 15 | $scope.idRemito = idRemito; |
| 16 | $scope.articulos = []; | 16 | $scope.articulos = []; |
| 17 | $scope.vehiculo = {}; | 17 | $scope.vehiculo = {}; |
| 18 | $scope.remito = {}; | 18 | $scope.remito = {}; |
| 19 | $scope.aCargar = []; | 19 | $scope.aCargar = []; |
| 20 | var cisternaMovimientos = []; | 20 | var cisternaMovimientos = []; |
| 21 | var promesaVehiculo = focaLogisticaPedidoRutaService.obtenerVehiculoById(idVehiculo); | 21 | var promesaVehiculo = focaLogisticaPedidoRutaService.obtenerVehiculoById(idVehiculo); |
| 22 | var promesaRemito; | 22 | var promesaRemito; |
| 23 | if(idRemito !== -1) { | 23 | if(idRemito !== -1) { |
| 24 | promesaRemito = focaLogisticaPedidoRutaService.obtenerRemitoById(idRemito); | 24 | promesaRemito = focaLogisticaPedidoRutaService.obtenerRemitoById(idRemito); |
| 25 | } | 25 | } |
| 26 | Promise.all([promesaVehiculo, promesaRemito]).then(function(res) { | 26 | Promise.all([promesaVehiculo, promesaRemito]).then(function(res) { |
| 27 | $scope.cargandoDatos = false; | 27 | $scope.cargandoDatos = false; |
| 28 | $scope.vehiculo = res[0].data; | 28 | $scope.vehiculo = res[0].data; |
| 29 | if(!res[1]) return; | 29 | if(!res[1]) return; |
| 30 | $scope.remito = res[1].data; | 30 | $scope.remito = res[1].data; |
| 31 | if($scope.remito.idUsuarioProceso) { | 31 | if($scope.remito.idUsuarioProceso) { |
| 32 | focaModalService.alert('Remito ya asignado'); | 32 | focaModalService.alert('Remito ya asignado'); |
| 33 | $uibModalInstance.close(); | 33 | $uibModalInstance.close(); |
| 34 | } | 34 | } |
| 35 | $scope.articulos = $scope.remito.articulosRemito; | 35 | $scope.articulos = $scope.remito.articulosRemito; |
| 36 | $scope.seleccionarArticulo($scope.articulos[0]); | 36 | $scope.seleccionarArticulo($scope.articulos[0]); |
| 37 | }); | 37 | }); |
| 38 | $scope.aceptar = function() { | 38 | $scope.aceptar = function() { |
| 39 | $scope.cargando = true; | 39 | $scope.cargando = true; |
| 40 | var cisternaCargas = []; | 40 | var cisternaCargas = []; |
| 41 | for (var i = 0; i < $scope.vehiculo.cisternas.length; i++) { | 41 | for (var i = 0; i < $scope.vehiculo.cisternas.length; i++) { |
| 42 | delete $scope.vehiculo.cisternas[i].cisternaCarga.articulo; | 42 | delete $scope.vehiculo.cisternas[i].cisternaCarga.articulo; |
| 43 | cisternaCargas.push($scope.vehiculo.cisternas[i].cisternaCarga); | 43 | cisternaCargas.push($scope.vehiculo.cisternas[i].cisternaCarga); |
| 44 | } | 44 | } |
| 45 | var cisterna = { | 45 | var cisterna = { |
| 46 | cisternaMovimientos: cisternaMovimientos, | 46 | cisternaMovimientos: cisternaMovimientos, |
| 47 | cisternaCargas: cisternaCargas, | 47 | cisternaCargas: cisternaCargas, |
| 48 | idVehiculo: $scope.vehiculo.id | 48 | idVehiculo: $scope.vehiculo.id |
| 49 | }; | 49 | }; |
| 50 | focaLogisticaPedidoRutaService.guardarCisternas(cisterna, $scope.remito.id) | 50 | focaLogisticaPedidoRutaService.guardarCisternas(cisterna, $scope.remito.id) |
| 51 | .then(function() { | 51 | .then(function() { |
| 52 | focaModalService.alert('Cisternas cargadas con éxito').then(function() { | 52 | focaModalService.alert('Cisternas cargadas con éxito').then(function() { |
| 53 | $scope.cargando = false; | 53 | $scope.cargando = false; |
| 54 | $uibModalInstance.close(); | 54 | $uibModalInstance.close(); |
| 55 | }); | 55 | }); |
| 56 | }).catch(function(error) { | 56 | }).catch(function(error) { |
| 57 | $scope.cargando = false; | 57 | $scope.cargando = false; |
| 58 | $uibModalInstance.close(); | 58 | $uibModalInstance.close(); |
| 59 | if (error.status === 403.1) { | 59 | if (error.status === 403.1) { |
| 60 | focaModalService.alert('ERROR: El vehículo esta en uso'); | 60 | focaModalService.alert('ERROR: El vehículo esta en uso'); |
| 61 | } | 61 | } |
| 62 | if(error.status === 403.2) { | 62 | if(error.status === 403.2) { |
| 63 | focaModalService.alert('ERROR: Otro usario ya cargó este remito'); | 63 | focaModalService.alert('ERROR: Otro usario ya cargó este remito'); |
| 64 | return; | 64 | return; |
| 65 | } | 65 | } |
| 66 | focaModalService.alert('Hubo un error al cargar las cisternas'); | 66 | focaModalService.alert('Hubo un error al cargar las cisternas'); |
| 67 | }); | 67 | }); |
| 68 | }; | 68 | }; |
| 69 | $scope.cancelar = function() { | 69 | $scope.cancelar = function() { |
| 70 | $uibModalInstance.close(); | 70 | $uibModalInstance.close(); |
| 71 | }; | 71 | }; |
| 72 | $scope.cargarACisternas = function(vehiculo) { | 72 | $scope.cargarACisternas = function(vehiculo) { |
| 73 | for(var i = 0; i < vehiculo.cisternas.length; i++) { | 73 | for(var i = 0; i < vehiculo.cisternas.length; i++) { |
| 74 | var cisterna = vehiculo.cisternas[i]; | 74 | var cisterna = vehiculo.cisternas[i]; |
| 75 | var aCargar = parseFloat($scope.aCargar[i]); | 75 | var aCargar = parseFloat($scope.aCargar[i]); |
| 76 | //validaciones | 76 | //validaciones |
| 77 | if(!aCargar) { | 77 | if(!aCargar) { |
| 78 | continue; | 78 | continue; |
| 79 | } | 79 | } |
| 80 | if(aCargar > cisterna.disponible) { | 80 | if(aCargar > cisterna.disponible) { |
| 81 | focaModalService.alert('La cantidad cargada supera la capacidad de la' + | 81 | focaModalService.alert('La cantidad cargada supera la capacidad de la' + |
| 82 | 'cisterna ' + cisterna.codigo); | 82 | 'cisterna ' + cisterna.codigo); |
| 83 | return; | 83 | return; |
| 84 | } | 84 | } |
| 85 | //cargar | 85 | //cargar |
| 86 | if(cisterna.cisternaCarga.cantidad) { | 86 | if(cisterna.cisternaCarga.cantidad) { |
| 87 | cisterna.cisternaCarga.cantidad += aCargar; | 87 | cisterna.cisternaCarga.cantidad += aCargar; |
| 88 | }else { | 88 | }else { |
| 89 | cisterna.cisternaCarga.cantidad = aCargar; | 89 | cisterna.cisternaCarga.cantidad = aCargar; |
| 90 | cisterna.cisternaCarga.idProducto = $scope.articuloSeleccionado.idArticulo; | 90 | cisterna.cisternaCarga.idProducto = $scope.articuloSeleccionado.idArticulo; |
| 91 | } | 91 | } |
| 92 | cisterna.disponible = cisterna.capacidad - cisterna.cisternaCarga.cantidad; | 92 | cisterna.disponible = cisterna.capacidad - cisterna.cisternaCarga.cantidad; |
| 93 | 93 | ||
| 94 | cisterna.cisternaCarga.articulo = { | 94 | cisterna.cisternaCarga.articulo = { |
| 95 | DetArt: $scope.articuloSeleccionado.descripcion | 95 | DetArt: $scope.articuloSeleccionado.descripcion |
| 96 | }; | 96 | }; |
| 97 | $filter('filter')($scope.articulos, {id: $scope.articuloSeleccionado.id})[0] | 97 | $filter('filter')($scope.articulos, {id: $scope.articuloSeleccionado.id})[0] |
| 98 | .cargado = true; | 98 | .cargado = true; |
| 99 | 99 | ||
| 100 | $scope.calcularPorcentaje(cisterna); | 100 | $scope.calcularPorcentaje(cisterna); |
| 101 | //Guardar | 101 | //Guardar |
| 102 | var now = new Date(); | 102 | var now = new Date(); |
| 103 | var cisternaMovimiento = { | 103 | var cisternaMovimiento = { |
| 104 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), | 104 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), |
| 105 | cantidad: aCargar, | 105 | cantidad: aCargar, |
| 106 | metodo: 'carga', | 106 | metodo: 'carga', |
| 107 | idCisternaCarga: cisterna.cisternaCarga.id, | 107 | idCisternaCarga: cisterna.cisternaCarga.id, |
| 108 | idRemito: $scope.remito.id | 108 | idRemito: $scope.remito.id |
| 109 | }; | 109 | }; |
| 110 | cisternaMovimientos.push(cisternaMovimiento); | 110 | cisternaMovimientos.push(cisternaMovimiento); |
| 111 | } | 111 | } |
| 112 | var articuloSiguiente = $scope.articulos.filter( | 112 | var articuloSiguiente = $scope.articulos.filter( |
| 113 | function(filter) { | 113 | function(filter) { |
| 114 | return filter.cargado !== true; | 114 | return filter.cargado !== true; |
| 115 | } | 115 | } |
| 116 | ); | 116 | ); |
| 117 | if(articuloSiguiente.length > 0) { | 117 | if(articuloSiguiente.length > 0) { |
| 118 | $scope.seleccionarArticulo(articuloSiguiente[0]); | 118 | $scope.seleccionarArticulo(articuloSiguiente[0]); |
| 119 | } | 119 | } |
| 120 | }; | 120 | }; |
| 121 | $scope.calcularPorcentaje = function(cisterna) { | 121 | $scope.calcularPorcentaje = function(cisterna) { |
| 122 | if(!cisterna.cisternaCarga.cantidad) { | 122 | if(!cisterna.cisternaCarga.cantidad) { |
| 123 | cisterna.cisternaCarga.cantidad = 0; | 123 | cisterna.cisternaCarga.cantidad = 0; |
| 124 | } | 124 | } |
| 125 | var porcentaje = (cisterna.cisternaCarga.cantidad * 100 / | 125 | var porcentaje = (cisterna.cisternaCarga.cantidad * 100 / |
| 126 | cisterna.capacidad) + '%'; | 126 | cisterna.capacidad) + '%'; |
| 127 | var elementHtml = document.getElementById(cisterna.id); | 127 | var elementHtml = document.getElementById(cisterna.id); |
| 128 | if(elementHtml) { | 128 | if(elementHtml) { |
| 129 | elementHtml.style.width = porcentaje; | 129 | elementHtml.style.width = porcentaje; |
| 130 | } | 130 | } |
| 131 | }; | 131 | }; |
| 132 | $scope.seleccionarArticulo = function(articulo) { | 132 | $scope.seleccionarArticulo = function(articulo) { |
| 133 | $scope.articuloSeleccionado = articulo; | 133 | $scope.articuloSeleccionado = articulo; |
| 134 | $scope.cisternaDisabled(); | 134 | $scope.cisternaDisabled(); |
| 135 | $scope.autoCompletar(); | 135 | $scope.autoCompletar(); |
| 136 | $scope.actualizarArticulo(); | 136 | $scope.actualizarArticulo(); |
| 137 | }; | 137 | }; |
| 138 | $scope.actualizarArticulo = function () { | 138 | $scope.actualizarArticulo = function () { |
| 139 | $scope.articuloSeleccionado.cantidadCargada = 0; | 139 | $scope.articuloSeleccionado.cantidadCargada = 0; |
| 140 | for (var i = 0; i < $scope.aCargar.length; i++) { | 140 | for (var i = 0; i < $scope.aCargar.length; i++) { |
| 141 | $scope.articuloSeleccionado.cantidadCargada += | 141 | $scope.articuloSeleccionado.cantidadCargada += |
| 142 | parseFloat($scope.aCargar[i]) || 0; | 142 | parseFloat($scope.aCargar[i]) || 0; |
| 143 | } | 143 | } |
| 144 | }; | 144 | }; |
| 145 | $scope.autoCompletar = function() { | 145 | $scope.autoCompletar = function() { |
| 146 | $scope.aCargar = []; | 146 | $scope.aCargar = []; |
| 147 | var disponible = $filter('filter')($scope.vehiculo.cisternas, {disabled: false}); | 147 | var disponible = $filter('filter')($scope.vehiculo.cisternas, {disabled: false}); |
| 148 | var index = $scope.vehiculo.cisternas.indexOf(disponible[0]); | 148 | var index = $scope.vehiculo.cisternas.indexOf(disponible[0]); |
| 149 | $scope.aCargar[index] = $scope.articuloSeleccionado.cantidad; | 149 | $scope.aCargar[index] = $scope.articuloSeleccionado.cantidad; |
| 150 | }; | 150 | }; |
| 151 | $scope.cisternaDisabled = function() { | 151 | $scope.cisternaDisabled = function() { |
| 152 | for (var i = 0; i < $scope.vehiculo.cisternas.length; i++) { | 152 | for(var i = 0; i < $scope.vehiculo.cisternas.length; i++) { |
| 153 | var cisterna = $scope.vehiculo.cisternas[i]; | 153 | var cisterna = $scope.vehiculo.cisternas[i]; |
| 154 | if(!$scope.articuloSeleccionado || ($scope.articuloSeleccionado.idArticulo !== | 154 | if(!$scope.articuloSeleccionado || ($scope.articuloSeleccionado.idArticulo !== |
| 155 | cisterna.cisternaCarga.idProducto && cisterna.cisternaCarga.idProducto) || | 155 | cisterna.cisternaCarga.idProducto && cisterna.cisternaCarga.idProducto) || |
| 156 | !$scope.tieneArticulosPendientes()|| $scope.articuloSeleccionado.cantidad > | 156 | !$scope.tieneArticulosPendientes()|| $scope.articuloSeleccionado.cantidad > |
| 157 | cisterna.disponible) { | 157 | cisterna.disponible) { |
| 158 | cisterna.disabled = true; | 158 | cisterna.disabled = true; |
| 159 | }else { | 159 | }else { |
| 160 | cisterna.disabled = false; | 160 | cisterna.disabled = false; |
| 161 | } | 161 | } |
| 162 | } | 162 | } |
| 163 | }; | 163 | }; |
| 164 | $scope.rellenarInput = function(input) { | 164 | $scope.rellenarInput = function(input) { |
| 165 | if(!$scope.articuloSeleccionado) return; | 165 | if(!$scope.articuloSeleccionado) return; |
| 166 | if($scope.articuloSeleccionado.cantidad - | 166 | if($scope.articuloSeleccionado.cantidad - |
| 167 | $scope.articuloSeleccionado.cantidadCargada === 0) { | 167 | $scope.articuloSeleccionado.cantidadCargada === 0) { |
| 168 | return input; | 168 | return input; |
| 169 | } | 169 | } |
| 170 | if(!input) input = 0; | 170 | if(!input) input = 0; |
| 171 | input = parseFloat(input); | 171 | input = parseFloat(input); |
| 172 | input += parseFloat($scope.articuloSeleccionado.cantidad - | 172 | input += parseFloat($scope.articuloSeleccionado.cantidad - |
| 173 | $scope.articuloSeleccionado.cantidadCargada); | 173 | $scope.articuloSeleccionado.cantidadCargada); |
| 174 | return input; | 174 | return input; |
| 175 | }; | 175 | }; |
| 176 | $scope.distribucionDisponible = function() { | 176 | $scope.distribucionDisponible = function() { |
| 177 | if(!$scope.articuloSeleccionado || $scope.articuloSeleccionado.cantidad - | 177 | if(!$scope.articuloSeleccionado || $scope.articuloSeleccionado.cantidad - |
| 178 | $scope.articuloSeleccionado.cantidadCargada !== 0 || | 178 | $scope.articuloSeleccionado.cantidadCargada !== 0 || |
| 179 | !$scope.tieneArticulosPendientes()) { | 179 | !$scope.tieneArticulosPendientes()) { |
| 180 | return false; | 180 | return false; |
| 181 | } | 181 | } |
| 182 | return true; | 182 | return true; |
| 183 | }; | 183 | }; |
| 184 | $scope.tieneArticulosPendientes = function() { | 184 | $scope.tieneArticulosPendientes = function() { |
| 185 | var algunValorNegativo = $scope.aCargar.filter(function(p) { | 185 | var algunValorNegativo = $scope.aCargar.filter(function(p) { |
| 186 | return p < 0; | 186 | return p < 0; |
| 187 | }); | 187 | }); |
| 188 | if(algunValorNegativo.length) { | 188 | if(algunValorNegativo.length) { |
| 189 | return false; | 189 | return false; |
| 190 | } | 190 | } |
| 191 | var articulosDescargados = $scope.articulos.filter(function(filter) { | 191 | var articulosDescargados = $scope.articulos.filter(function(filter) { |
| 192 | return filter.cargado === true; | 192 | return filter.cargado === true; |
| 193 | }); | 193 | }); |
| 194 | if(articulosDescargados.length === $scope.articulos.length) { | 194 | if(articulosDescargados.length === $scope.articulos.length) { |
| 195 | return false; | 195 | return false; |
| 196 | } | 196 | } |
| 197 | return true; | 197 | return true; |
| 198 | }; | 198 | }; |
| 199 | }]); | 199 | }]); |
| 200 | 200 |