Commit 52d99a2f7b8e02deb72df20272bc12b6729144e6
1 parent
9ada384d5c
Exists in
master
guardo seguimiento al rechazar remito
Showing
1 changed file
with
8 additions
and
4 deletions
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 | function($filter, $scope, $uibModalInstance, idRemito, focaModalDetalleHojaRutaService, | 11 | function($filter, $scope, $uibModalInstance, idRemito, focaModalDetalleHojaRutaService, |
| 12 | focaModalService, focaSeguimientoService) | 12 | focaModalService, focaSeguimientoService) |
| 13 | { | 13 | { |
| 14 | //Seteo variables | 14 | //Seteo variables |
| 15 | $scope.remito = {}; | 15 | $scope.remito = {}; |
| 16 | $scope.cisternas = []; | 16 | $scope.cisternas = []; |
| 17 | $scope.articuloSeleccionado = {}; | 17 | $scope.articuloSeleccionado = {}; |
| 18 | $scope.aDescargar = []; | 18 | $scope.aDescargar = []; |
| 19 | $scope.cargando = true; | 19 | $scope.cargando = true; |
| 20 | 20 | ||
| 21 | //Datos | 21 | //Datos |
| 22 | var promesaRemito = focaModalDetalleHojaRutaService | 22 | var promesaRemito = focaModalDetalleHojaRutaService |
| 23 | .getRemitoById(idRemito); | 23 | .getRemitoById(idRemito); |
| 24 | var promesaCisternas = focaModalDetalleHojaRutaService | 24 | var promesaCisternas = focaModalDetalleHojaRutaService |
| 25 | .getCisternasByIdRemito(idRemito); | 25 | .getCisternasByIdRemito(idRemito); |
| 26 | 26 | ||
| 27 | Promise.all([promesaRemito, promesaCisternas]).then(function(res) { | 27 | Promise.all([promesaRemito, promesaCisternas]).then(function(res) { |
| 28 | $scope.cargando = false; | 28 | $scope.cargando = false; |
| 29 | $scope.remito = res[0].data; | 29 | $scope.remito = res[0].data; |
| 30 | $scope.cisternas = res[1].data; | 30 | $scope.cisternas = res[1].data; |
| 31 | var articuloAChequear = $scope.remito.articulosRemito.filter( | 31 | var articuloAChequear = $scope.remito.articulosRemito.filter( |
| 32 | function(articulo) { | 32 | function(articulo) { |
| 33 | return !articulo.descargado; | 33 | return !articulo.descargado; |
| 34 | }); | 34 | }); |
| 35 | if(!articuloAChequear.length || $scope.remito.rechazado) { | 35 | if(!articuloAChequear.length || $scope.remito.rechazado) { |
| 36 | $scope.readonly = true; | 36 | $scope.readonly = true; |
| 37 | $scope.cambio($scope.remito.articulosRemito[0]); | 37 | $scope.cambio($scope.remito.articulosRemito[0]); |
| 38 | }else { | 38 | }else { |
| 39 | $scope.cambio(articuloAChequear[0]); | 39 | $scope.cambio(articuloAChequear[0]); |
| 40 | } | 40 | } |
| 41 | $scope.$digest(); | 41 | $scope.$digest(); |
| 42 | }, function() { | 42 | }, function() { |
| 43 | focaModalService.alert('El servicio no responde intente más tarde'); | 43 | focaModalService.alert('El servicio no responde intente más tarde'); |
| 44 | $uibModalInstance.dismiss(); | 44 | $uibModalInstance.dismiss(); |
| 45 | }); | 45 | }); |
| 46 | $scope.cambio = function(articulo) { | 46 | $scope.cambio = function(articulo) { |
| 47 | if(!$scope.articuloSeleccionado.descargado) { | 47 | if(!$scope.articuloSeleccionado.descargado) { |
| 48 | $scope.articuloSeleccionado.cantidadDescargada = 0; | 48 | $scope.articuloSeleccionado.cantidadDescargada = 0; |
| 49 | } | 49 | } |
| 50 | $scope.aDescargar = []; | 50 | $scope.aDescargar = []; |
| 51 | $scope.articuloSeleccionado = articulo; | 51 | $scope.articuloSeleccionado = articulo; |
| 52 | }; | 52 | }; |
| 53 | $scope.descargar = function(key) { | 53 | $scope.descargar = function(key) { |
| 54 | if(key === 13) { | 54 | if(key === 13) { |
| 55 | $scope.cargando = true; | 55 | $scope.cargando = true; |
| 56 | var hojaRutaMovimientos = []; | 56 | var hojaRutaMovimientos = []; |
| 57 | var cisternaMovimientos = []; | 57 | var cisternaMovimientos = []; |
| 58 | var cisternaCargas = []; | 58 | var cisternaCargas = []; |
| 59 | var totalADescargar = 0; | 59 | var totalADescargar = 0; |
| 60 | for(var i = 0; i < $scope.aDescargar.length; i++) { | 60 | for(var i = 0; i < $scope.aDescargar.length; i++) { |
| 61 | totalADescargar += $scope.aDescargar[i] || 0; | 61 | totalADescargar += $scope.aDescargar[i] || 0; |
| 62 | } | 62 | } |
| 63 | focaModalService | 63 | focaModalService |
| 64 | .confirm('¿Desea descargar ' + totalADescargar + ' litros de ' + | 64 | .confirm('¿Desea descargar ' + totalADescargar + ' litros de ' + |
| 65 | $scope.articuloSeleccionado.descripcion + '?') | 65 | $scope.articuloSeleccionado.descripcion + '?') |
| 66 | .then(descargar, function() { | 66 | .then(descargar, function() { |
| 67 | $scope.cargando = false; | 67 | $scope.cargando = false; |
| 68 | }); | 68 | }); |
| 69 | } | 69 | } |
| 70 | function descargar() { | 70 | function descargar() { |
| 71 | for(var i = 0; i < $scope.cisternas.length; i++) { | 71 | for(var i = 0; i < $scope.cisternas.length; i++) { |
| 72 | var descarga = $scope.aDescargar[i]; | 72 | var descarga = $scope.aDescargar[i]; |
| 73 | var cisternaCarga = $scope.cisternas[i].cisternaCarga; | 73 | var cisternaCarga = $scope.cisternas[i].cisternaCarga; |
| 74 | if(!descarga) continue; | 74 | if(!descarga) continue; |
| 75 | if(descarga > cisternaCarga.cantidad) { | 75 | if(descarga > cisternaCarga.cantidad) { |
| 76 | focaModalService.alert('La cantidad a descargar no debe ser ' + | 76 | focaModalService.alert('La cantidad a descargar no debe ser ' + |
| 77 | 'mayor a la cantidad de la cisterna'); | 77 | 'mayor a la cantidad de la cisterna'); |
| 78 | $scope.cargando = false; | 78 | $scope.cargando = false; |
| 79 | return; | 79 | return; |
| 80 | } | 80 | } |
| 81 | cisternaCarga.cantidad -= descarga; | 81 | cisternaCarga.cantidad -= descarga; |
| 82 | 82 | ||
| 83 | //Guardar | 83 | //Guardar |
| 84 | var now = new Date(); | 84 | var now = new Date(); |
| 85 | var cisternaMovimiento = { | 85 | var cisternaMovimiento = { |
| 86 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), | 86 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), |
| 87 | cantidad: descarga, | 87 | cantidad: descarga, |
| 88 | metodo: 'descarga', | 88 | metodo: 'descarga', |
| 89 | idCisternaCarga: cisternaCarga.id, | 89 | idCisternaCarga: cisternaCarga.id, |
| 90 | idRemito: $scope.remito.id | 90 | idRemito: $scope.remito.id |
| 91 | }; | 91 | }; |
| 92 | var hojaRutaMovimiento = { | 92 | var hojaRutaMovimiento = { |
| 93 | reciboDescarga: $scope.numeroRecibo, | 93 | reciboDescarga: $scope.numeroRecibo, |
| 94 | idRemito: $scope.remito.id | 94 | idRemito: $scope.remito.id |
| 95 | }; | 95 | }; |
| 96 | delete cisternaCarga.articulo; | 96 | delete cisternaCarga.articulo; |
| 97 | cisternaCargas.push(cisternaCarga); | 97 | cisternaCargas.push(cisternaCarga); |
| 98 | cisternaMovimientos.push(cisternaMovimiento); | 98 | cisternaMovimientos.push(cisternaMovimiento); |
| 99 | hojaRutaMovimientos.push(hojaRutaMovimiento); | 99 | hojaRutaMovimientos.push(hojaRutaMovimiento); |
| 100 | } | 100 | } |
| 101 | var save = { | 101 | var save = { |
| 102 | cisternaCargas: cisternaCargas, | 102 | cisternaCargas: cisternaCargas, |
| 103 | cisternaMovimientos: cisternaMovimientos, | 103 | cisternaMovimientos: cisternaMovimientos, |
| 104 | hojaRutaMovimientos: hojaRutaMovimientos, | 104 | hojaRutaMovimientos: hojaRutaMovimientos, |
| 105 | articulo: $scope.articuloSeleccionado | 105 | articulo: $scope.articuloSeleccionado |
| 106 | }; | 106 | }; |
| 107 | focaModalDetalleHojaRutaService | 107 | focaModalDetalleHojaRutaService |
| 108 | .postMovimientoHojaRuta(save) | 108 | .postMovimientoHojaRuta(save) |
| 109 | .then(guardarSeguimiento) | 109 | .then(guardarSeguimiento) |
| 110 | .catch(error); | 110 | .catch(error); |
| 111 | function guardarSeguimiento(res) { | 111 | function guardarSeguimiento(res) { |
| 112 | focaSeguimientoService | 112 | focaSeguimientoService |
| 113 | .guardarPosicion( | 113 | .guardarPosicion( |
| 114 | 'Entrega de producto', | 114 | 'Entrega de producto', |
| 115 | res.data[0].id, | 115 | res.data[0].id, |
| 116 | $scope.remito.observaciones); | 116 | $scope.remito.observaciones); |
| 117 | $scope.aDescargar = []; | 117 | $scope.aDescargar = []; |
| 118 | $scope.remito.observaciones = ''; | 118 | $scope.remito.observaciones = ''; |
| 119 | $scope.articuloSeleccionado.descargado = true; | 119 | $scope.articuloSeleccionado.descargado = true; |
| 120 | var siguienteArticulo = $scope.remito.articulosRemito.filter( | 120 | var siguienteArticulo = $scope.remito.articulosRemito.filter( |
| 121 | function(articulo) { | 121 | function(articulo) { |
| 122 | return articulo.id != $scope.articuloSeleccionado.id; | 122 | return articulo.id != $scope.articuloSeleccionado.id; |
| 123 | } | 123 | } |
| 124 | ); | 124 | ); |
| 125 | if(siguienteArticulo.length) { | 125 | if(siguienteArticulo.length) { |
| 126 | $scope.cambio(siguienteArticulo[0]); | 126 | $scope.cambio(siguienteArticulo[0]); |
| 127 | } | 127 | } |
| 128 | success(); | 128 | success(); |
| 129 | } | 129 | } |
| 130 | } | 130 | } |
| 131 | }; | 131 | }; |
| 132 | 132 | ||
| 133 | $scope.cancel = function() { | 133 | $scope.cancel = function() { |
| 134 | $uibModalInstance.dismiss('cancel'); | 134 | $uibModalInstance.dismiss('cancel'); |
| 135 | }; | 135 | }; |
| 136 | 136 | ||
| 137 | $scope.distribucionDisponible = function() { | 137 | $scope.distribucionDisponible = function() { |
| 138 | return $scope.articuloSeleccionado.cantidadDescargada && | 138 | return $scope.articuloSeleccionado.cantidadDescargada && |
| 139 | $scope.articuloSeleccionado.cantidadDescargada <= | 139 | $scope.articuloSeleccionado.cantidadDescargada <= |
| 140 | $scope.articuloSeleccionado.cantidad; | 140 | $scope.articuloSeleccionado.cantidad; |
| 141 | }; | 141 | }; |
| 142 | 142 | ||
| 143 | $scope.actualizarArticulo = function() { | 143 | $scope.actualizarArticulo = function() { |
| 144 | $scope.articuloSeleccionado.cantidadDescargada = 0; | 144 | $scope.articuloSeleccionado.cantidadDescargada = 0; |
| 145 | for(var i = 0; i < $scope.aDescargar.length; i++) { | 145 | for(var i = 0; i < $scope.aDescargar.length; i++) { |
| 146 | $scope.articuloSeleccionado.cantidadDescargada += | 146 | $scope.articuloSeleccionado.cantidadDescargada += |
| 147 | parseFloat($scope.aDescargar[i]) || 0; | 147 | parseFloat($scope.aDescargar[i]) || 0; |
| 148 | } | 148 | } |
| 149 | }; | 149 | }; |
| 150 | 150 | ||
| 151 | $scope.rechazar = function() { | 151 | $scope.rechazar = function() { |
| 152 | focaModalService | 152 | focaModalService |
| 153 | .prompt('Aclare el motivo de rechazo') | 153 | .prompt('Aclare el motivo de rechazo') |
| 154 | .then(function(motivo) { | 154 | .then(function(motivo) { |
| 155 | $scope.cargando = true; | 155 | $scope.cargando = true; |
| 156 | var remitoRechazado = $.extend(true, {}, $scope.remito); | 156 | var remitoRechazado = $.extend(true, {}, $scope.remito); |
| 157 | delete remitoRechazado.articulosRemito; | 157 | delete remitoRechazado.articulosRemito; |
| 158 | delete remitoRechazado.notaPedido; | 158 | delete remitoRechazado.notaPedido; |
| 159 | remitoRechazado.rechazado = true; | 159 | remitoRechazado.rechazado = true; |
| 160 | remitoRechazado.motivoRechazo = motivo; | 160 | remitoRechazado.motivoRechazo = motivo; |
| 161 | remitoRechazado.fechaRemito = | 161 | remitoRechazado.fechaRemito = |
| 162 | remitoRechazado.fechaRemito.slice(0, 19).replace('T', ' '); | 162 | remitoRechazado.fechaRemito.slice(0, 19).replace('T', ' '); |
| 163 | focaModalDetalleHojaRutaService | 163 | Promise.all([ |
| 164 | .rechazarRemito(remitoRechazado) | 164 | focaModalDetalleHojaRutaService |
| 165 | .then(success) | 165 | .rechazarRemito(remitoRechazado), |
| 166 | .catch(error) | 166 | focaSeguimientoService.guardarPosicion( |
| 167 | 'Entrega de producto', | ||
| 168 | remitoRechazado.id, | ||
| 169 | motivo) | ||
| 170 | ]).then(success).catch(error); | ||
| 167 | $scope.readonly = true; | 171 | $scope.readonly = true; |
| 168 | }); | 172 | }); |
| 169 | }; | 173 | }; |
| 170 | 174 | ||
| 171 | //funciones | 175 | //funciones |
| 172 | function error(error) { | 176 | function error(error) { |
| 173 | focaModalService.alert('Hubo un error ' + error); | 177 | focaModalService.alert('Hubo un error ' + error); |
| 174 | } | 178 | } |
| 175 | function success() { | 179 | function success() { |
| 176 | focaModalService.alert('Operación realizada con éxito'); | 180 | focaModalService.alert('Operación realizada con éxito'); |
| 177 | $scope.cargando = false; | 181 | $scope.cargando = false; |
| 178 | } | 182 | } |
| 179 | } | 183 | } |
| 180 | ] | 184 | ] |
| 181 | ); | 185 | ); |
| 182 | 186 |