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