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