Commit 189481565ee82e5de0bfd95a7832d1b43b30e73f
1 parent
28d144ab4e
Exists in
master
no rellena si es menor a 0
Showing
1 changed file
with
1 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) { | 54 | if(!$scope.articulos.length) { |
| 55 | focaModalService.alert('El remito no tiene articulos'); | 55 | focaModalService.alert('El remito no tiene articulos'); |
| 56 | $uibModalInstance.dismiss(); | 56 | $uibModalInstance.dismiss(); |
| 57 | return; | 57 | return; |
| 58 | } | 58 | } |
| 59 | $scope.seleccionarArticulo($scope.articulos[0]); | 59 | $scope.seleccionarArticulo($scope.articulos[0]); |
| 60 | var tieneUsuario = $scope.cisternas.filter(function(cisterna) { | 60 | var tieneUsuario = $scope.cisternas.filter(function(cisterna) { |
| 61 | if(cisterna.cisternaCarga && cisterna.cisternaCarga.idUsuarioProceso) { | 61 | if(cisterna.cisternaCarga && cisterna.cisternaCarga.idUsuarioProceso) { |
| 62 | return cisterna.cisternaCarga.idUsuarioProceso !== | 62 | return cisterna.cisternaCarga.idUsuarioProceso !== |
| 63 | focaModalDetalleCisternasService.idUsuario; | 63 | focaModalDetalleCisternasService.idUsuario; |
| 64 | } | 64 | } |
| 65 | }); | 65 | }); |
| 66 | if(tieneUsuario.length) { | 66 | if(tieneUsuario.length) { |
| 67 | focaModalService.alert('Otro usario esta usando este vehículo'); | 67 | focaModalService.alert('Otro usario esta usando este vehículo'); |
| 68 | $uibModalInstance.close(); | 68 | $uibModalInstance.close(); |
| 69 | } | 69 | } |
| 70 | $scope.$digest(); | 70 | $scope.$digest(); |
| 71 | }); | 71 | }); |
| 72 | $scope.aceptar = function() { | 72 | $scope.aceptar = function() { |
| 73 | $scope.cargando = true; | 73 | $scope.cargando = true; |
| 74 | for(var i = 0; i < $scope.cisternasCarga.length; i++) { | 74 | for(var i = 0; i < $scope.cisternasCarga.length; i++) { |
| 75 | $scope.cisternasCarga[i].idUsuarioProceso = | 75 | $scope.cisternasCarga[i].idUsuarioProceso = |
| 76 | focaModalDetalleCisternasService.idUsuario; | 76 | focaModalDetalleCisternasService.idUsuario; |
| 77 | delete $scope.cisternasCarga[i].articulo; | 77 | delete $scope.cisternasCarga[i].articulo; |
| 78 | } | 78 | } |
| 79 | var cisterna = { | 79 | var cisterna = { |
| 80 | cisternaMovimientos: cisternaMovimientos, | 80 | cisternaMovimientos: cisternaMovimientos, |
| 81 | cisternaCargas: $scope.cisternasCarga, | 81 | cisternaCargas: $scope.cisternasCarga, |
| 82 | idVehiculo: $scope.vehiculo.id, | 82 | idVehiculo: $scope.vehiculo.id, |
| 83 | fechaReparto: focaModalDetalleCisternasService.fecha | 83 | fechaReparto: focaModalDetalleCisternasService.fecha |
| 84 | }; | 84 | }; |
| 85 | if(!focaModalDetalleCisternasService.idUsuario) { | 85 | if(!focaModalDetalleCisternasService.idUsuario) { |
| 86 | focaModalService.alert('No logeado como vendedor'); | 86 | focaModalService.alert('No logeado como vendedor'); |
| 87 | $scope.cargando = false; | 87 | $scope.cargando = false; |
| 88 | return; | 88 | return; |
| 89 | } | 89 | } |
| 90 | focaModalDetalleCisternasService.guardarCisternas(cisterna, $scope.remito.id) | 90 | focaModalDetalleCisternasService.guardarCisternas(cisterna, $scope.remito.id) |
| 91 | .then(function() { | 91 | .then(function() { |
| 92 | focaModalService.alert('Cisternas cargadas con éxito').then(function() { | 92 | focaModalService.alert('Cisternas cargadas con éxito').then(function() { |
| 93 | $scope.cargando = false; | 93 | $scope.cargando = false; |
| 94 | $uibModalInstance.close(); | 94 | $uibModalInstance.close(); |
| 95 | }); | 95 | }); |
| 96 | }).catch(function(error) { | 96 | }).catch(function(error) { |
| 97 | $scope.cargando = false; | 97 | $scope.cargando = false; |
| 98 | $uibModalInstance.close(); | 98 | $uibModalInstance.close(); |
| 99 | if (error.status === 403) { | 99 | if (error.status === 403) { |
| 100 | focaModalService.alert('ERROR: ' + error.data); | 100 | focaModalService.alert('ERROR: ' + error.data); |
| 101 | return; | 101 | return; |
| 102 | } | 102 | } |
| 103 | focaModalService.alert('Hubo un error al cargar las cisternas'); | 103 | focaModalService.alert('Hubo un error al cargar las cisternas'); |
| 104 | }); | 104 | }); |
| 105 | }; | 105 | }; |
| 106 | $scope.cancelar = function() { | 106 | $scope.cancelar = function() { |
| 107 | $uibModalInstance.dismiss(); | 107 | $uibModalInstance.dismiss(); |
| 108 | }; | 108 | }; |
| 109 | $scope.cargarACisternas = function() { | 109 | $scope.cargarACisternas = function() { |
| 110 | for(var i = 0; i < $scope.cisternas.length; i++) { | 110 | for(var i = 0; i < $scope.cisternas.length; i++) { |
| 111 | var cisterna = $scope.cisternas[i]; | 111 | var cisterna = $scope.cisternas[i]; |
| 112 | var aCargar = parseFloat($scope.aCargar[i]); | 112 | var aCargar = parseFloat($scope.aCargar[i]); |
| 113 | var fechaReparto = focaModalDetalleCisternasService.fecha; | 113 | var fechaReparto = focaModalDetalleCisternasService.fecha; |
| 114 | //validaciones | 114 | //validaciones |
| 115 | if(!aCargar) { | 115 | if(!aCargar) { |
| 116 | continue; | 116 | continue; |
| 117 | } | 117 | } |
| 118 | //cargar | 118 | //cargar |
| 119 | if(cisterna.cisternaCarga.cantidad) { | 119 | if(cisterna.cisternaCarga.cantidad) { |
| 120 | cisterna.cisternaCarga.cantidad += aCargar; | 120 | cisterna.cisternaCarga.cantidad += aCargar; |
| 121 | }else { | 121 | }else { |
| 122 | cisterna.cisternaCarga.cantidad = aCargar; | 122 | cisterna.cisternaCarga.cantidad = aCargar; |
| 123 | cisterna.cisternaCarga.idProducto = $scope.articuloSeleccionado.idArticulo; | 123 | cisterna.cisternaCarga.idProducto = $scope.articuloSeleccionado.idArticulo; |
| 124 | } | 124 | } |
| 125 | cisterna.disponible = cisterna.capacidad - cisterna.cisternaCarga.cantidad; | 125 | cisterna.disponible = cisterna.capacidad - cisterna.cisternaCarga.cantidad; |
| 126 | 126 | ||
| 127 | cisterna.cisternaCarga.articulo = { | 127 | cisterna.cisternaCarga.articulo = { |
| 128 | DetArt: $scope.articuloSeleccionado.descripcion | 128 | DetArt: $scope.articuloSeleccionado.descripcion |
| 129 | }; | 129 | }; |
| 130 | $filter('filter')($scope.articulos, {id: $scope.articuloSeleccionado.id})[0] | 130 | $filter('filter')($scope.articulos, {id: $scope.articuloSeleccionado.id})[0] |
| 131 | .cargado = true; | 131 | .cargado = true; |
| 132 | 132 | ||
| 133 | $scope.calcularPorcentaje(cisterna); | 133 | $scope.calcularPorcentaje(cisterna); |
| 134 | //Guardar | 134 | //Guardar |
| 135 | var now = new Date(); | 135 | var now = new Date(); |
| 136 | var cisternaMovimiento = { | 136 | var cisternaMovimiento = { |
| 137 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), | 137 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), |
| 138 | cantidad: aCargar, | 138 | cantidad: aCargar, |
| 139 | metodo: 'carga', | 139 | metodo: 'carga', |
| 140 | idCisternaCarga: cisterna.cisternaCarga.id, | 140 | idCisternaCarga: cisterna.cisternaCarga.id, |
| 141 | idRemito: $scope.remito.id | 141 | idRemito: $scope.remito.id |
| 142 | }; | 142 | }; |
| 143 | cisterna.cisternaCarga.fechaReparto = fechaReparto; | 143 | cisterna.cisternaCarga.fechaReparto = fechaReparto; |
| 144 | cisterna.cisternaCarga.idCisterna = cisterna.id; | 144 | cisterna.cisternaCarga.idCisterna = cisterna.id; |
| 145 | $scope.cisternasCarga.push(cisterna.cisternaCarga); | 145 | $scope.cisternasCarga.push(cisterna.cisternaCarga); |
| 146 | cisternaMovimientos.push(cisternaMovimiento); | 146 | cisternaMovimientos.push(cisternaMovimiento); |
| 147 | } | 147 | } |
| 148 | var articuloSiguiente = $scope.articulos.filter( | 148 | var articuloSiguiente = $scope.articulos.filter( |
| 149 | function(filter) { | 149 | function(filter) { |
| 150 | return filter.cargado !== true; | 150 | return filter.cargado !== true; |
| 151 | } | 151 | } |
| 152 | ); | 152 | ); |
| 153 | if(articuloSiguiente.length > 0) { | 153 | if(articuloSiguiente.length > 0) { |
| 154 | $scope.seleccionarArticulo(articuloSiguiente[0]); | 154 | $scope.seleccionarArticulo(articuloSiguiente[0]); |
| 155 | } | 155 | } |
| 156 | }; | 156 | }; |
| 157 | $scope.calcularPorcentaje = function(cisterna) { | 157 | $scope.calcularPorcentaje = function(cisterna) { |
| 158 | if(!cisterna.cisternaCarga) { | 158 | if(!cisterna.cisternaCarga) { |
| 159 | cisterna.cisternaCarga = { | 159 | cisterna.cisternaCarga = { |
| 160 | cantidad: 0 | 160 | cantidad: 0 |
| 161 | }; | 161 | }; |
| 162 | } | 162 | } |
| 163 | var porcentaje = (cisterna.cisternaCarga.cantidad * 100 / | 163 | var porcentaje = (cisterna.cisternaCarga.cantidad * 100 / |
| 164 | cisterna.capacidad) + '%'; | 164 | cisterna.capacidad) + '%'; |
| 165 | var elementHtml = document.getElementById(cisterna.id); | 165 | var elementHtml = document.getElementById(cisterna.id); |
| 166 | if(elementHtml) { | 166 | if(elementHtml) { |
| 167 | elementHtml.style.width = porcentaje; | 167 | elementHtml.style.width = porcentaje; |
| 168 | } | 168 | } |
| 169 | }; | 169 | }; |
| 170 | $scope.seleccionarArticulo = function(articulo) { | 170 | $scope.seleccionarArticulo = function(articulo) { |
| 171 | $scope.articuloSeleccionado = articulo; | 171 | $scope.articuloSeleccionado = articulo; |
| 172 | $scope.cisternaDisponible(); | 172 | $scope.cisternaDisponible(); |
| 173 | $scope.autoCompletar(); | 173 | $scope.autoCompletar(); |
| 174 | $scope.actualizarArticulo(); | 174 | $scope.actualizarArticulo(); |
| 175 | }; | 175 | }; |
| 176 | $scope.actualizarArticulo = function() { | 176 | $scope.actualizarArticulo = function() { |
| 177 | $scope.articuloSeleccionado.cantidadCargada = 0; | 177 | $scope.articuloSeleccionado.cantidadCargada = 0; |
| 178 | for(var i = 0; i < $scope.aCargar.length; i++) { | 178 | for(var i = 0; i < $scope.aCargar.length; i++) { |
| 179 | $scope.articuloSeleccionado.cantidadCargada += | 179 | $scope.articuloSeleccionado.cantidadCargada += |
| 180 | parseFloat($scope.aCargar[i]) || 0; | 180 | parseFloat($scope.aCargar[i]) || 0; |
| 181 | } | 181 | } |
| 182 | }; | 182 | }; |
| 183 | $scope.autoCompletar = function() { | 183 | $scope.autoCompletar = function() { |
| 184 | for(var i = 0; i < $scope.cisternas.length; i++) { | 184 | for(var i = 0; i < $scope.cisternas.length; i++) { |
| 185 | if($scope.cisternas[i].disabled) { | 185 | if($scope.cisternas[i].disabled) { |
| 186 | continue; | 186 | continue; |
| 187 | } | 187 | } |
| 188 | var aCargar = $scope.articuloSeleccionado.cantidad - | 188 | var aCargar = $scope.articuloSeleccionado.cantidad - |
| 189 | ($scope.articuloSeleccionado.cantidadCargada || 0); | 189 | ($scope.articuloSeleccionado.cantidadCargada || 0); |
| 190 | if(aCargar > $scope.cisternas[i].disponible) { | 190 | if(aCargar > $scope.cisternas[i].disponible) { |
| 191 | aCargar = $scope.cisternas[i].disponible; | 191 | aCargar = $scope.cisternas[i].disponible; |
| 192 | } | 192 | } |
| 193 | if(aCargar) { | 193 | if(aCargar) { |
| 194 | $scope.aCargar[i] = aCargar; | 194 | $scope.aCargar[i] = aCargar; |
| 195 | $scope.actualizarArticulo(); | 195 | $scope.actualizarArticulo(); |
| 196 | } | 196 | } |
| 197 | } | 197 | } |
| 198 | }; | 198 | }; |
| 199 | $scope.cisternaDisponible = function() { | 199 | $scope.cisternaDisponible = function() { |
| 200 | for(var i = 0; i < $scope.cisternas.length; i++) { | 200 | for(var i = 0; i < $scope.cisternas.length; i++) { |
| 201 | //Puede meter un porcentaje del total | 201 | //Puede meter un porcentaje del total |
| 202 | // if($scope.articuloSeleccionado.cantidad > $scope.cisternas[i].disponible) { | 202 | // if($scope.articuloSeleccionado.cantidad > $scope.cisternas[i].disponible) { |
| 203 | // $scope.cisternas[i].disabled = true; | 203 | // $scope.cisternas[i].disabled = true; |
| 204 | // continue; | 204 | // continue; |
| 205 | // } | 205 | // } |
| 206 | if($scope.cisternas[i].cisternaCarga && | 206 | if($scope.cisternas[i].cisternaCarga && |
| 207 | $scope.cisternas[i].cisternaCarga.idProducto && | 207 | $scope.cisternas[i].cisternaCarga.idProducto && |
| 208 | $scope.articuloSeleccionado.idArticulo !== | 208 | $scope.articuloSeleccionado.idArticulo !== |
| 209 | $scope.cisternas[i].cisternaCarga.idProducto) | 209 | $scope.cisternas[i].cisternaCarga.idProducto) |
| 210 | { | 210 | { |
| 211 | $scope.cisternas[i].disabled = true; | 211 | $scope.cisternas[i].disabled = true; |
| 212 | continue; | 212 | continue; |
| 213 | } | 213 | } |
| 214 | $scope.cisternas[i].disabled = false; | 214 | $scope.cisternas[i].disabled = false; |
| 215 | } | 215 | } |
| 216 | }; | 216 | }; |
| 217 | $scope.rellenarInput = function(input, cisterna) { | 217 | $scope.rellenarInput = function(input, cisterna) { |
| 218 | if(!$scope.articuloSeleccionado) return; | 218 | if(!$scope.articuloSeleccionado) return; |
| 219 | if($scope.articuloSeleccionado.cantidad - | 219 | if($scope.articuloSeleccionado.cantidad - |
| 220 | $scope.articuloSeleccionado.cantidadCargada === 0) { | 220 | $scope.articuloSeleccionado.cantidadCargada === 0) { |
| 221 | return input; | 221 | return input; |
| 222 | } | 222 | } |
| 223 | if(!input) input = 0; | 223 | if(!input) input = 0; |
| 224 | input = parseFloat(input); | 224 | input = parseFloat(input); |
| 225 | input += parseFloat($scope.articuloSeleccionado.cantidad - | 225 | input += parseFloat($scope.articuloSeleccionado.cantidad - |
| 226 | $scope.articuloSeleccionado.cantidadCargada); | 226 | $scope.articuloSeleccionado.cantidadCargada); |
| 227 | if(input <= 0) return; | ||
| 227 | if(input > cisterna.disponible) { | 228 | if(input > cisterna.disponible) { |
| 228 | input = cisterna.disponible; | 229 | input = cisterna.disponible; |
| 229 | } | 230 | } |
| 230 | return input; | 231 | return input; |
| 231 | }; | 232 | }; |
| 232 | $scope.distribucionDisponible = function() { | 233 | $scope.distribucionDisponible = function() { |
| 233 | if(!$scope.articuloSeleccionado || $scope.articuloSeleccionado.cantidad - | 234 | if(!$scope.articuloSeleccionado || $scope.articuloSeleccionado.cantidad - |
| 234 | $scope.articuloSeleccionado.cantidadCargada !== 0 || | 235 | $scope.articuloSeleccionado.cantidadCargada !== 0 || |
| 235 | !$scope.tieneArticulosPendientes()) { | 236 | !$scope.tieneArticulosPendientes()) { |
| 236 | return false; | 237 | return false; |
| 237 | } | 238 | } |
| 238 | for(var i = 0; i < $scope.cisternas.length; i++) { | 239 | for(var i = 0; i < $scope.cisternas.length; i++) { |
| 239 | if($scope.aCargar[i] > $scope.cisternas[i].disponible) { | 240 | if($scope.aCargar[i] > $scope.cisternas[i].disponible) { |
| 240 | return false; | 241 | return false; |
| 241 | } | 242 | } |
| 242 | } | 243 | } |
| 243 | return true; | 244 | return true; |
| 244 | }; | 245 | }; |
| 245 | $scope.tieneArticulosPendientes = function() { | 246 | $scope.tieneArticulosPendientes = function() { |
| 246 | var algunValorNegativo = $scope.aCargar.filter(function(p) { | 247 | var algunValorNegativo = $scope.aCargar.filter(function(p) { |
| 247 | return p < 0; | 248 | return p < 0; |
| 248 | }); | 249 | }); |
| 249 | if(algunValorNegativo.length) { | 250 | if(algunValorNegativo.length) { |
| 250 | return false; | 251 | return false; |
| 251 | } | 252 | } |
| 252 | var articulosDescargados = $scope.articulos.filter(function(filter) { | 253 | var articulosDescargados = $scope.articulos.filter(function(filter) { |
| 253 | return filter.cargado === true; | 254 | return filter.cargado === true; |
| 254 | }); | 255 | }); |
| 255 | if(articulosDescargados.length === $scope.articulos.length) { | 256 | if(articulosDescargados.length === $scope.articulos.length) { |
| 256 | $scope.aCargar = []; | 257 | $scope.aCargar = []; |
| 257 | return false; | 258 | return false; |
| 258 | } | 259 | } |
| 259 | return true; | 260 | return true; |
| 260 | }; | 261 | }; |
| 261 | }]); | 262 | }]); |
| 262 | 263 |