Commit b8ddc3d45152c2a92cbc2f35ab01e958e37d8c68
Exists in
master
Merge branch 'master' into 'develop'
Master See merge request !12
Showing
1 changed file
Show diff stats
src/js/controller.js
| 1 | angular.module('focaModalDetalleHojaRuta') | 1 | angular.module('focaModalDetalleHojaRuta') |
| 2 | .controller('focaModalDetalleHojaRutaController', | 2 | .controller('focaModalDetalleHojaRutaController', |
| 3 | [ | 3 | [ |
| 4 | '$filter', | 4 | '$filter', |
| 5 | '$scope', | 5 | '$scope', |
| 6 | '$uibModalInstance', | 6 | '$uibModalInstance', |
| 7 | 'idRemito', | 7 | 'idRemito', |
| 8 | 'focaModalDetalleHojaRutaService', | 8 | 'focaModalDetalleHojaRutaService', |
| 9 | 'focaModalService', | 9 | 'focaModalService', |
| 10 | 'focaSeguimientoService', | 10 | 'focaSeguimientoService', |
| 11 | '$uibModal', | 11 | '$uibModal', |
| 12 | function($filter, $scope, $uibModalInstance, idRemito, focaModalDetalleHojaRutaService, | 12 | function($filter, $scope, $uibModalInstance, idRemito, focaModalDetalleHojaRutaService, |
| 13 | focaModalService, focaSeguimientoService, $uibModal) | 13 | focaModalService, focaSeguimientoService, $uibModal) |
| 14 | { | 14 | { |
| 15 | //Seteo variables | 15 | //Seteo variables |
| 16 | $scope.remito = {}; | 16 | $scope.remito = {}; |
| 17 | $scope.cisternas = []; | 17 | $scope.cisternas = []; |
| 18 | $scope.articuloSeleccionado = {}; | 18 | $scope.articuloSeleccionado = {}; |
| 19 | $scope.aDescargar = []; | 19 | $scope.aDescargar = []; |
| 20 | $scope.cargando = true; | 20 | $scope.cargando = true; |
| 21 | 21 | ||
| 22 | //Datos | 22 | //Datos |
| 23 | var promesaRemito = focaModalDetalleHojaRutaService | 23 | var promesaRemito = focaModalDetalleHojaRutaService |
| 24 | .getRemitoById(idRemito); | 24 | .getRemitoById(idRemito); |
| 25 | var promesaCisternas = focaModalDetalleHojaRutaService | 25 | var promesaCisternas = focaModalDetalleHojaRutaService |
| 26 | .getCisternasByIdRemito(idRemito); | 26 | .getCisternasByIdRemito(idRemito); |
| 27 | 27 | ||
| 28 | Promise.all([promesaRemito, promesaCisternas]).then(function(res) { | 28 | Promise.all([promesaRemito, promesaCisternas]).then(function(res) { |
| 29 | $scope.cargando = false; | 29 | $scope.cargando = false; |
| 30 | $scope.remito = res[0].data; | 30 | $scope.remito = res[0].data; |
| 31 | $scope.cisternas = res[1].data; | 31 | $scope.cisternas = res[1].data; |
| 32 | var articuloAChequear = $scope.remito.articulosRemito.filter( | 32 | var articuloAChequear = $scope.remito.articulosRemito.filter( |
| 33 | function(articulo) { | 33 | function(articulo) { |
| 34 | return !articulo.descargado; | 34 | return !articulo.descargado; |
| 35 | }); | 35 | }); |
| 36 | if(!articuloAChequear.length || $scope.remito.rechazado) { | 36 | if(!articuloAChequear.length || $scope.remito.rechazado) { |
| 37 | $scope.readonly = true; | 37 | $scope.readonly = true; |
| 38 | $scope.cambio($scope.remito.articulosRemito[0]); | 38 | $scope.cambio($scope.remito.articulosRemito[0]); |
| 39 | }else { | 39 | }else { |
| 40 | $scope.cambio(articuloAChequear[0]); | 40 | $scope.cambio(articuloAChequear[0]); |
| 41 | } | 41 | } |
| 42 | $scope.$digest(); | 42 | $scope.$digest(); |
| 43 | }, function() { | 43 | }, function() { |
| 44 | focaModalService.alert('El servicio no responde intente más tarde'); | 44 | focaModalService.alert('El servicio no responde intente más tarde'); |
| 45 | $uibModalInstance.dismiss(); | 45 | $uibModalInstance.dismiss(); |
| 46 | }); | 46 | }); |
| 47 | $scope.cambio = function(articulo) { | 47 | $scope.cambio = function(articulo) { |
| 48 | if(!$scope.articuloSeleccionado.descargado) { | 48 | if(!$scope.articuloSeleccionado.descargado) { |
| 49 | $scope.articuloSeleccionado.cantidadDescargada = 0; | 49 | $scope.articuloSeleccionado.cantidadDescargada = 0; |
| 50 | } | 50 | } |
| 51 | $scope.aDescargar = []; | 51 | $scope.aDescargar = []; |
| 52 | $scope.articuloSeleccionado = articulo; | 52 | $scope.articuloSeleccionado = articulo; |
| 53 | }; | 53 | }; |
| 54 | $scope.descargar = function(key) { | 54 | $scope.descargar = function(key) { |
| 55 | if(key === 13) { | 55 | if(key === 13) { |
| 56 | $scope.cargando = true; | 56 | $scope.cargando = true; |
| 57 | var hojaRutaMovimientos = []; | 57 | var hojaRutaMovimientos = []; |
| 58 | var cisternaMovimientos = []; | 58 | var cisternaMovimientos = []; |
| 59 | var cisternaCargas = []; | 59 | var cisternaCargas = []; |
| 60 | var totalADescargar = 0; | 60 | var totalADescargar = 0; |
| 61 | for(var i = 0; i < $scope.aDescargar.length; i++) { | 61 | for(var i = 0; i < $scope.aDescargar.length; i++) { |
| 62 | totalADescargar += $scope.aDescargar[i] || 0; | 62 | totalADescargar += $scope.aDescargar[i] || 0; |
| 63 | } | 63 | } |
| 64 | focaModalService | 64 | focaModalService |
| 65 | .confirm('¿Desea descargar ' + totalADescargar + ' litros de ' + | 65 | .confirm('¿Desea descargar ' + totalADescargar + ' litros de ' + |
| 66 | $scope.articuloSeleccionado.descripcion + '?') | 66 | $scope.articuloSeleccionado.descripcion + '?') |
| 67 | .then(descargar, function() { | 67 | .then(descargar, function() { |
| 68 | $scope.cargando = false; | 68 | $scope.cargando = false; |
| 69 | }); | 69 | }); |
| 70 | } | 70 | } |
| 71 | function descargar() { | 71 | function descargar() { |
| 72 | for(var i = 0; i < $scope.cisternas.length; i++) { | 72 | for(var i = 0; i < $scope.cisternas.length; i++) { |
| 73 | var descarga = $scope.aDescargar[i]; | 73 | var descarga = $scope.aDescargar[i]; |
| 74 | var cisternaCarga = $scope.cisternas[i].cisternaCarga; | 74 | var cisternaCarga = $scope.cisternas[i].cisternaCarga; |
| 75 | if(!descarga) continue; | 75 | if(!descarga) continue; |
| 76 | if(descarga > cisternaCarga.cantidad) { | 76 | if(descarga > cisternaCarga.cantidad) { |
| 77 | focaModalService.alert('La cantidad a descargar no debe ser ' + | 77 | focaModalService.alert('La cantidad a descargar no debe ser ' + |
| 78 | 'mayor a la cantidad de la cisterna'); | 78 | 'mayor a la cantidad de la cisterna'); |
| 79 | $scope.cargando = false; | 79 | $scope.cargando = false; |
| 80 | return; | 80 | return; |
| 81 | } | 81 | } |
| 82 | cisternaCarga.cantidad -= descarga; | 82 | cisternaCarga.cantidad -= descarga; |
| 83 | 83 | ||
| 84 | //Guardar | 84 | //Guardar |
| 85 | var now = new Date(); | 85 | var now = new Date(); |
| 86 | var cisternaMovimiento = { | 86 | var cisternaMovimiento = { |
| 87 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), | 87 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), |
| 88 | cantidad: descarga, | 88 | cantidad: descarga, |
| 89 | metodo: 'descarga', | 89 | metodo: 'descarga', |
| 90 | idCisternaCarga: cisternaCarga.id, | 90 | idCisternaCarga: cisternaCarga.id, |
| 91 | idRemito: $scope.remito.id | 91 | idRemito: $scope.remito.id |
| 92 | }; | 92 | }; |
| 93 | var hojaRutaMovimiento = { | 93 | var hojaRutaMovimiento = { |
| 94 | reciboDescarga: $scope.numeroRecibo, | 94 | reciboDescarga: $scope.numeroRecibo, |
| 95 | idRemito: $scope.remito.id | 95 | idRemito: $scope.remito.id |
| 96 | }; | 96 | }; |
| 97 | delete cisternaCarga.articulo; | 97 | delete cisternaCarga.articulo; |
| 98 | cisternaCargas.push(cisternaCarga); | 98 | cisternaCargas.push(cisternaCarga); |
| 99 | cisternaMovimientos.push(cisternaMovimiento); | 99 | cisternaMovimientos.push(cisternaMovimiento); |
| 100 | hojaRutaMovimientos.push(hojaRutaMovimiento); | 100 | hojaRutaMovimientos.push(hojaRutaMovimiento); |
| 101 | } | 101 | } |
| 102 | var save = { | 102 | var save = { |
| 103 | cisternaCargas: cisternaCargas, | 103 | cisternaCargas: cisternaCargas, |
| 104 | cisternaMovimientos: cisternaMovimientos, | 104 | cisternaMovimientos: cisternaMovimientos, |
| 105 | hojaRutaMovimientos: hojaRutaMovimientos, | 105 | hojaRutaMovimientos: hojaRutaMovimientos, |
| 106 | articulo: $scope.articuloSeleccionado | 106 | articulo: $scope.articuloSeleccionado |
| 107 | }; | 107 | }; |
| 108 | focaModalDetalleHojaRutaService | 108 | focaModalDetalleHojaRutaService |
| 109 | .postMovimientoHojaRuta(save) | 109 | .postMovimientoHojaRuta(save) |
| 110 | .then(guardarSeguimiento) | 110 | .then(guardarSeguimiento) |
| 111 | .catch(error); | 111 | .catch(error); |
| 112 | function guardarSeguimiento(res) { | 112 | function guardarSeguimiento(res) { |
| 113 | focaSeguimientoService | 113 | focaSeguimientoService |
| 114 | .guardarPosicion( | 114 | .guardarPosicion( |
| 115 | 'Entrega de producto', | 115 | 'Entrega de producto', |
| 116 | res.data[0].id, | 116 | res.data[0].id, |
| 117 | $scope.remito.observaciones); | 117 | $scope.remito.observaciones); |
| 118 | $scope.aDescargar = []; | 118 | $scope.aDescargar = []; |
| 119 | $scope.remito.observaciones = ''; | 119 | $scope.remito.observaciones = ''; |
| 120 | $scope.articuloSeleccionado.descargado = true; | 120 | $scope.articuloSeleccionado.descargado = true; |
| 121 | var siguienteArticulo = $scope.remito.articulosRemito.filter( | 121 | var siguienteArticulo = $scope.remito.articulosRemito.filter( |
| 122 | function(articulo) { | 122 | function(articulo) { |
| 123 | return articulo.id != $scope.articuloSeleccionado.id; | 123 | return articulo.id != $scope.articuloSeleccionado.id; |
| 124 | } | 124 | } |
| 125 | ); | 125 | ); |
| 126 | if(siguienteArticulo.length) { | 126 | if(siguienteArticulo.length) { |
| 127 | $scope.cambio(siguienteArticulo[0]); | 127 | $scope.cambio(siguienteArticulo[0]); |
| 128 | } | 128 | } |
| 129 | $scope.actualizarPuntoDescarga(); | 129 | $scope.actualizarPuntoDescarga(); |
| 130 | } | 130 | } |
| 131 | } | 131 | } |
| 132 | }; | 132 | }; |
| 133 | 133 | ||
| 134 | $scope.cancel = function() { | 134 | $scope.cancel = function() { |
| 135 | $uibModalInstance.dismiss('cancel'); | 135 | $uibModalInstance.dismiss('cancel'); |
| 136 | }; | 136 | }; |
| 137 | 137 | ||
| 138 | $scope.distribucionDisponible = function() { | 138 | $scope.distribucionDisponible = function() { |
| 139 | return $scope.articuloSeleccionado.cantidadDescargada && | 139 | return $scope.articuloSeleccionado.cantidadDescargada && |
| 140 | $scope.articuloSeleccionado.cantidadDescargada <= | 140 | $scope.articuloSeleccionado.cantidadDescargada <= |
| 141 | $scope.articuloSeleccionado.cantidad; | 141 | $scope.articuloSeleccionado.cantidad; |
| 142 | }; | 142 | }; |
| 143 | 143 | ||
| 144 | $scope.actualizarArticulo = function() { | 144 | $scope.actualizarArticulo = function() { |
| 145 | $scope.articuloSeleccionado.cantidadDescargada = 0; | 145 | $scope.articuloSeleccionado.cantidadDescargada = 0; |
| 146 | for(var i = 0; i < $scope.aDescargar.length; i++) { | 146 | for(var i = 0; i < $scope.aDescargar.length; i++) { |
| 147 | $scope.articuloSeleccionado.cantidadDescargada += | 147 | $scope.articuloSeleccionado.cantidadDescargada += |
| 148 | parseFloat($scope.aDescargar[i]) || 0; | 148 | parseFloat($scope.aDescargar[i]) || 0; |
| 149 | } | 149 | } |
| 150 | }; | 150 | }; |
| 151 | 151 | ||
| 152 | $scope.actualizarPuntoDescarga = function() { | 152 | $scope.actualizarPuntoDescarga = function() { |
| 153 | var modalInstance = $uibModal.open( | 153 | var modalInstance = $uibModal.open( |
| 154 | { | 154 | { |
| 155 | ariaLabelledBy: 'Actualizar punto de descarga', | 155 | ariaLabelledBy: 'Actualizar punto de descarga', |
| 156 | templateUrl: 'modal-actualizar-punto-descarga.html', | 156 | templateUrl: 'modal-actualizar-punto-descarga.html', |
| 157 | controller: 'focaModalActualizarPuntoDescargaController', | 157 | controller: 'focaModalActualizarPuntoDescargaController', |
| 158 | resolve: { | 158 | resolve: { |
| 159 | notaPedido: function() { | 159 | notaPedido: function() { |
| 160 | return $scope.remito.notaPedido; | 160 | return $scope.remito.notaPedido; |
| 161 | } | 161 | } |
| 162 | }, | 162 | }, |
| 163 | size: 'lg' | 163 | size: 'lg' |
| 164 | } | 164 | } |
| 165 | ); | 165 | ); |
| 166 | modalInstance.result.then(function() { | 166 | modalInstance.result.then(function() { |
| 167 | success(); | 167 | success(); |
| 168 | }, function() { | 168 | }, function() { |
| 169 | success(); | 169 | success(); |
| 170 | }); | 170 | }); |
| 171 | }; | 171 | }; |
| 172 | 172 | ||
| 173 | $scope.rechazar = function() { | 173 | $scope.rechazar = function() { |
| 174 | focaModalService | 174 | focaModalService |
| 175 | .prompt('Aclare el motivo de rechazo') | 175 | .prompt({ |
| 176 | titulo: 'Aclare el motivo de rechazo' | ||
| 177 | }) | ||
| 176 | .then(function(motivo) { | 178 | .then(function(motivo) { |
| 177 | $scope.cargando = true; | 179 | $scope.cargando = true; |
| 178 | var remitoRechazado = $.extend(true, {}, $scope.remito); | 180 | var remitoRechazado = $.extend(true, {}, $scope.remito); |
| 179 | delete remitoRechazado.articulosRemito; | 181 | delete remitoRechazado.articulosRemito; |
| 180 | delete remitoRechazado.notaPedido; | 182 | delete remitoRechazado.notaPedido; |
| 181 | remitoRechazado.rechazado = true; | 183 | remitoRechazado.rechazado = true; |
| 182 | remitoRechazado.motivoRechazo = motivo; | 184 | remitoRechazado.motivoRechazo = motivo; |
| 183 | remitoRechazado.fechaRemito = | 185 | remitoRechazado.fechaRemito = |
| 184 | remitoRechazado.fechaRemito.slice(0, 19).replace('T', ' '); | 186 | remitoRechazado.fechaRemito.slice(0, 19).replace('T', ' '); |
| 185 | focaModalDetalleHojaRutaService.rechazarRemito(remitoRechazado) | 187 | focaModalDetalleHojaRutaService.rechazarRemito(remitoRechazado) |
| 186 | .then(function(res) { | 188 | .then(function(res) { |
| 187 | focaSeguimientoService.guardarPosicion( | 189 | focaSeguimientoService.guardarPosicion( |
| 188 | 'Entrega de producto', | 190 | 'Entrega de producto', |
| 189 | res.data[1].id, | 191 | res.data[1].id, |
| 190 | motivo); | 192 | motivo); |
| 191 | success(); | 193 | success(); |
| 192 | }) | 194 | }) |
| 193 | .catch(error); | 195 | .catch(error); |
| 194 | $scope.readonly = true; | 196 | $scope.readonly = true; |
| 195 | }); | 197 | }); |
| 196 | }; | 198 | }; |
| 197 | 199 | ||
| 198 | //funciones | 200 | //funciones |
| 199 | function error(error) { | 201 | function error(error) { |
| 200 | focaModalService.alert('Hubo un error ' + error); | 202 | focaModalService.alert('Hubo un error ' + error); |
| 201 | } | 203 | } |
| 202 | function success() { | 204 | function success() { |
| 203 | focaModalService.alert('Operación realizada con éxito'); | 205 | focaModalService.alert('Operación realizada con éxito'); |
| 204 | $scope.cargando = false; | 206 | $scope.cargando = false; |
| 205 | } | 207 | } |
| 206 | } | 208 | } |
| 207 | ] | 209 | ] |
| 208 | ) | 210 | ) |
| 209 | .controller('focaModalActualizarPuntoDescargaController', | 211 | .controller('focaModalActualizarPuntoDescargaController', |
| 210 | [ | 212 | [ |
| 211 | '$scope', | 213 | '$scope', |
| 212 | '$uibModalInstance', | 214 | '$uibModalInstance', |
| 213 | 'focaSeguimientoService', | 215 | 'focaSeguimientoService', |
| 214 | 'focaModalService', | 216 | 'focaModalService', |
| 215 | 'notaPedido', | 217 | 'notaPedido', |
| 216 | 'focaModalDetalleHojaRutaService', | 218 | 'focaModalDetalleHojaRutaService', |
| 217 | function($scope, $uibModalInstance, focaSeguimientoService, | 219 | function($scope, $uibModalInstance, focaSeguimientoService, |
| 218 | focaModalService, notaPedido, focaModalDetalleHojaRutaService) | 220 | focaModalService, notaPedido, focaModalDetalleHojaRutaService) |
| 219 | { | 221 | { |
| 220 | $scope.notaPedido = notaPedido; | 222 | $scope.notaPedido = notaPedido; |
| 221 | $scope.descripcion = ''; | 223 | $scope.descripcion = ''; |
| 222 | focaSeguimientoService.obtenerPosicion(function(res) { | 224 | focaSeguimientoService.obtenerPosicion(function(res) { |
| 223 | $scope.posicion = res.coords; | 225 | $scope.posicion = res.coords; |
| 224 | }); | 226 | }); |
| 225 | 227 | ||
| 226 | $scope.cancel = function() { | 228 | $scope.cancel = function() { |
| 227 | if ($scope.ingreso) { | 229 | if ($scope.ingreso) { |
| 228 | $scope.ingreso = false; | 230 | $scope.ingreso = false; |
| 229 | } else { | 231 | } else { |
| 230 | $uibModalInstance.dismiss(); | 232 | $uibModalInstance.dismiss(); |
| 231 | } | 233 | } |
| 232 | }; | 234 | }; |
| 233 | 235 | ||
| 234 | $scope.select = function(puntoDescarga) { | 236 | $scope.select = function(puntoDescarga) { |
| 235 | if (!$scope.posicion) { | 237 | if (!$scope.posicion) { |
| 236 | focaModalService.alert('No se pudo obtener la ubicación'); | 238 | focaModalService.alert('No se pudo obtener la ubicación'); |
| 237 | return; | 239 | return; |
| 238 | } | 240 | } |
| 239 | puntoDescarga.latitud = $scope.posicion.latitude; | 241 | puntoDescarga.latitud = $scope.posicion.latitude; |
| 240 | puntoDescarga.longitud = $scope.posicion.longitude; | 242 | puntoDescarga.longitud = $scope.posicion.longitude; |
| 241 | focaModalDetalleHojaRutaService | 243 | focaModalDetalleHojaRutaService |
| 242 | .guardarPuntoDescarga(puntoDescarga) | 244 | .guardarPuntoDescarga(puntoDescarga) |
| 243 | .then(function(res) { | 245 | .then(function(res) { |
| 244 | $uibModalInstance.close(res.data); | 246 | $uibModalInstance.close(res.data); |
| 245 | }); | 247 | }); |
| 246 | }; | 248 | }; |
| 247 | 249 | ||
| 248 | $scope.guardar = function() { | 250 | $scope.guardar = function() { |
| 249 | if (!$scope.posicion) { | 251 | if (!$scope.posicion) { |
| 250 | focaModalService.alert('No se pudo obtener la ubicación'); | 252 | focaModalService.alert('No se pudo obtener la ubicación'); |
| 251 | return; | 253 | return; |
| 252 | } | 254 | } |
| 253 | focaModalDetalleHojaRutaService | 255 | focaModalDetalleHojaRutaService |
| 254 | .guardarPuntoDescarga({ | 256 | .guardarPuntoDescarga({ |
| 255 | id: 0, | 257 | id: 0, |
| 256 | id_cliente: $scope.notaPedido.idCliente, | 258 | id_cliente: $scope.notaPedido.idCliente, |
| 257 | id_da_config_0: $scope.notaPedido.idDomicilio, | 259 | id_da_config_0: $scope.notaPedido.idDomicilio, |
| 258 | descripcion: $scope.descripcion, | 260 | descripcion: $scope.descripcion, |
| 259 | latitud: $scope.posicion.latitude, | 261 | latitud: $scope.posicion.latitude, |
| 260 | longitud: $scope.posicion.longitude | 262 | longitud: $scope.posicion.longitude |
| 261 | }) | 263 | }) |
| 262 | .then(function(res) { | 264 | .then(function(res) { |
| 263 | $uibModalInstance.close(res.data); | 265 | $uibModalInstance.close(res.data); |
| 264 | }); | 266 | }); |
| 265 | 267 | ||
| 266 | }; | 268 | }; |
| 267 | 269 | ||
| 268 | } | 270 | } |
| 269 | ] | 271 | ] |
| 270 | ); | 272 | ); |
| 271 | 273 |