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