Commit a15e2cf0697fd6a8bbc52cf2c99b41fa1fa869b6
1 parent
dca2ba1af7
Exists in
master
Espacios
Showing
2 changed files
with
4 additions
and
3 deletions
 
Show diff stats
src/js/controller.js
| 1 | angular.module('focaModalPuntoDescarga') | 1 | angular.module('focaModalPuntoDescarga') | 
| 2 | .controller('focaModalPuntoDescargaController', [ | 2 | .controller('focaModalPuntoDescargaController', [ | 
| 3 | '$timeout', | 3 | '$timeout', | 
| 4 | '$filter', | 4 | '$filter', | 
| 5 | '$scope', | 5 | '$scope', | 
| 6 | '$uibModalInstance', | 6 | '$uibModalInstance', | 
| 7 | 'focaModalPuntoDescargaService', | 7 | 'focaModalPuntoDescargaService', | 
| 8 | 'filters', | 8 | 'filters', | 
| 9 | 'focaModalService', | 9 | 'focaModalService', | 
| 10 | function($timeout, $filter, $scope, $uibModalInstance, | 10 | function($timeout, $filter, $scope, $uibModalInstance, | 
| 11 | focaModalPuntoDescargaService, filters, focaModalService) { | 11 | focaModalPuntoDescargaService, filters, focaModalService) { | 
| 12 | 12 | ||
| 13 | $scope.cantidadArticulo = 0; | 13 | $scope.cantidadArticulo = 0; | 
| 14 | $scope.articuloSeleccionado = 0; | 14 | $scope.articuloSeleccionado = 0; | 
| 15 | $scope.ivas = []; | 15 | $scope.ivas = []; | 
| 16 | $scope.puntosSeleccionados = []; | 16 | $scope.puntosSeleccionados = []; | 
| 17 | $scope.editando = false; | 17 | $scope.editando = false; | 
| 18 | $scope.puntoDescarga = { | 18 | $scope.puntoDescarga = { | 
| 19 | id: 0, | 19 | id: 0, | 
| 20 | id_cliente: filters.idCliente, | 20 | id_cliente: filters.idCliente, | 
| 21 | id_da_config_0: filters.idDomicilio, | 21 | id_da_config_0: filters.idDomicilio, | 
| 22 | latitud: filters.domicilio.Latitud, | 22 | latitud: filters.domicilio.Latitud, | 
| 23 | longitud: filters.domicilio.Longitud | 23 | longitud: filters.domicilio.Longitud | 
| 24 | }; | 24 | }; | 
| 25 | 25 | ||
| 26 | $scope.articulos = angular.copy(filters.articulos); | 26 | $scope.articulos = angular.copy(filters.articulos); | 
| 27 | $scope.articulos.map(function(articulo) { | 27 | $scope.articulos.map(function(articulo) { | 
| 28 | articulo.restante = articulo.cantidad; | 28 | articulo.restante = articulo.cantidad; | 
| 29 | }); | 29 | }); | 
| 30 | actualizarTabla(); | 30 | actualizarTabla(); | 
| 31 | cargarPuntos(filters.puntosDescarga); | 31 | cargarPuntos(filters.puntosDescarga); | 
| 32 | 32 | ||
| 33 | $scope.cancel = function() { | 33 | $scope.cancel = function() { | 
| 34 | if ($scope.ingreso) { | 34 | if ($scope.ingreso) { | 
| 35 | $scope.ingreso = false; | 35 | $scope.ingreso = false; | 
| 36 | $scope.puntoDescarga = { | 36 | $scope.puntoDescarga = { | 
| 37 | id: 0, | 37 | id: 0, | 
| 38 | id_cliente: filters.idCliente, | 38 | id_cliente: filters.idCliente, | 
| 39 | id_da_config_0: filters.idDomicilio, | 39 | id_da_config_0: filters.idDomicilio, | 
| 40 | latitud: filters.domicilio.Latitud, | 40 | latitud: filters.domicilio.Latitud, | 
| 41 | longitud: filters.domicilio.Longitud | 41 | longitud: filters.domicilio.Longitud | 
| 42 | }; | 42 | }; | 
| 43 | $scope.editando = false; | 43 | $scope.editando = false; | 
| 44 | } else { | 44 | } else { | 
| 45 | $uibModalInstance.dismiss('cancel'); | 45 | $uibModalInstance.dismiss('cancel'); | 
| 46 | } | 46 | } | 
| 47 | }; | 47 | }; | 
| 48 | 48 | ||
| 49 | $scope.aceptar = function() { | 49 | $scope.aceptar = function() { | 
| 50 | if ($scope.cargaArticulos) { | 50 | if ($scope.cargaArticulos) { | 
| 51 | cargarArticulos(); | 51 | cargarArticulos(); | 
| 52 | } else if(!$scope.puntosSeleccionados.length) { | 52 | } else if(!$scope.puntosSeleccionados.length) { | 
| 53 | $uibModalInstance.dismiss('cancel'); | 53 | $uibModalInstance.dismiss('cancel'); | 
| 54 | } else { | 54 | } else { | 
| 55 | enviarPuntos(); | 55 | enviarPuntos(); | 
| 56 | } | 56 | } | 
| 57 | }; | 57 | }; | 
| 58 | 58 | ||
| 59 | $scope.guardar = function(key) { | 59 | $scope.guardar = function(key) { | 
| 60 | if(key === 13) { | 60 | if(key === 13) { | 
| 61 | focaModalPuntoDescargaService | 61 | focaModalPuntoDescargaService | 
| 62 | .guardarPuntoDescarga($scope.puntoDescarga) | 62 | .guardarPuntoDescarga($scope.puntoDescarga) | 
| 63 | .then(function() { | 63 | .then(function() { | 
| 64 | actualizarTabla(); | 64 | actualizarTabla(); | 
| 65 | $scope.ingreso = false; | 65 | $scope.ingreso = false; | 
| 66 | $scope.puntoDescarga = { | 66 | $scope.puntoDescarga = { | 
| 67 | id: 0, | 67 | id: 0, | 
| 68 | id_cliente: filters.idCliente, | 68 | id_cliente: filters.idCliente, | 
| 69 | id_da_config_0: filters.idDomicilio, | 69 | id_da_config_0: filters.idDomicilio, | 
| 70 | latitud: filters.domicilio.Latitud, | 70 | latitud: filters.domicilio.Latitud, | 
| 71 | longitud: filters.domicilio.Longitud | 71 | longitud: filters.domicilio.Longitud | 
| 72 | }; | 72 | }; | 
| 73 | $scope.editando = false; | 73 | $scope.editando = false; | 
| 74 | }); | 74 | }); | 
| 75 | } | 75 | } | 
| 76 | }; | 76 | }; | 
| 77 | 77 | ||
| 78 | $scope.editar = function(id) { | 78 | $scope.editar = function(id) { | 
| 79 | focaModalPuntoDescargaService.getPuntoDescargaById(id).then(function(res) { | 79 | focaModalPuntoDescargaService.getPuntoDescargaById(id).then(function(res) { | 
| 80 | $scope.puntoDescarga = res.data; | 80 | $scope.puntoDescarga = res.data; | 
| 81 | $scope.ingreso = true; | 81 | $scope.ingreso = true; | 
| 82 | $scope.editando = true; | 82 | $scope.editando = true; | 
| 83 | }); | 83 | }); | 
| 84 | }; | 84 | }; | 
| 85 | 85 | ||
| 86 | $scope.eliminar = function(idx, puntoDescarga) { | 86 | $scope.eliminar = function(idx, puntoDescarga) { | 
| 87 | focaModalService.confirm('¿Está seguro que desea borrar el punto de descarga '+ | 87 | focaModalService.confirm('¿Está seguro que desea borrar el punto de descarga '+ | 
| 88 | puntoDescarga.descripcion + '?').then(function(data) { | 88 | puntoDescarga.descripcion + '?').then(function(data) { | 
| 89 | if (data) { | 89 | if (data) { | 
| 90 | focaModalPuntoDescargaService | 90 | focaModalPuntoDescargaService | 
| 91 | .eliminarPuntoDescarga(puntoDescarga.id) | 91 | .eliminarPuntoDescarga(puntoDescarga.id) | 
| 92 | .then(function() { | 92 | .then(function() { | 
| 93 | $scope.puntosDescarga.splice(idx, 1); | 93 | $scope.puntosDescarga.splice(idx, 1); | 
| 94 | }); | 94 | }); | 
| 95 | } | 95 | } | 
| 96 | }); | 96 | }); | 
| 97 | }; | 97 | }; | 
| 98 | 98 | ||
| 99 | $scope.seleccionarPunto = function(idx, esCheckbox) { | 99 | $scope.seleccionarPunto = function(idx, esCheckbox) { | 
| 100 | var indexPunto = $scope.puntosSeleccionados.indexOf(idx); | 100 | var indexPunto = $scope.puntosSeleccionados.indexOf(idx); | 
| 101 | if (indexPunto !== -1) { | 101 | if (indexPunto !== -1) { | 
| 102 | if (!esCheckbox) { | 102 | if (!esCheckbox) { | 
| 103 | $scope.puntosDescarga[idx].seleccionado = false; | 103 | $scope.puntosDescarga[idx].seleccionado = false; | 
| 104 | } | 104 | } | 
| 105 | $scope.puntosSeleccionados.splice(indexPunto, 1); | 105 | $scope.puntosSeleccionados.splice(indexPunto, 1); | 
| 106 | } else { | 106 | } else { | 
| 107 | if (!esCheckbox) { | 107 | if (!esCheckbox) { | 
| 108 | $scope.puntosDescarga[idx].seleccionado = true; | 108 | $scope.puntosDescarga[idx].seleccionado = true; | 
| 109 | } | 109 | } | 
| 110 | $scope.puntosSeleccionados.push(idx); | 110 | $scope.puntosSeleccionados.push(idx); | 
| 111 | } | 111 | } | 
| 112 | }; | 112 | }; | 
| 113 | 113 | ||
| 114 | $scope.agregarArticulo = function(punto, key) { | 114 | $scope.agregarArticulo = function(punto, key) { | 
| 115 | key = (typeof key === 'undefined') ? 13 : key; | 115 | key = (typeof key === 'undefined') ? 13 : key; | 
| 116 | 116 | ||
| 117 | if (key === 13) { | 117 | if (key === 13) { | 
| 118 | var articulo = $scope.articulos[$scope.articuloSeleccionado], | 118 | var articulo = $scope.articulos[$scope.articuloSeleccionado], | 
| 119 | cantidadRestante = articulo.restante - punto.cantidadACargar; | 119 | cantidadRestante = articulo.restante - punto.cantidadACargar; | 
| 120 | 120 | ||
| 121 | if (cantidadRestante < 0) { | 121 | if (cantidadRestante < 0) { | 
| 122 | focaModalService | 122 | focaModalService | 
| 123 | .alert('La cantidad a cargar debe ser menor o igual al restante'); | 123 | .alert('La cantidad a cargar debe ser menor o igual al restante'); | 
| 124 | } else if (punto.cantidadACargar <= 0) { | 124 | } else if (punto.cantidadACargar <= 0) { | 
| 125 | focaModalService | 125 | focaModalService | 
| 126 | .alert('La cantidad a cargar debe ser mayor que cero'); | 126 | .alert('La cantidad a cargar debe ser mayor que cero'); | 
| 127 | } else { | 127 | } else { | 
| 128 | punto.cargado += parseInt(punto.cantidadACargar); | 128 | punto.cargado += parseInt(punto.cantidadACargar); | 
| 129 | articulo.restante = cantidadRestante; | 129 | articulo.restante = cantidadRestante; | 
| 130 | var existeArticulo = punto.articulosAgregados.filter( | 130 | var existeArticulo = punto.articulosAgregados.filter( | 
| 131 | function (articuloAAgregar) { | 131 | function (articuloAAgregar) { | 
| 132 | return articuloAAgregar.id === articulo.idArticulo; | 132 | return articuloAAgregar.id === articulo.idArticulo; | 
| 133 | }); | 133 | }); | 
| 134 | //Si el articulo ya fue agregado | 134 | //Si el articulo ya fue agregado | 
| 135 | if (existeArticulo.length) { | 135 | if (existeArticulo.length) { | 
| 136 | //Solo sumo cantidad | 136 | //Solo sumo cantidad | 
| 137 | var total = parseInt(existeArticulo[0].cantidad) + | 137 | var total = parseInt(existeArticulo[0].cantidad) + | 
| 138 | parseInt(punto.cantidadACargar); | 138 | parseInt(punto.cantidadACargar); | 
| 139 | existeArticulo[0].cantidad = total; | 139 | existeArticulo[0].cantidad = total; | 
| 140 | } else { | 140 | } else { | 
| 141 | //Agrego el articulo con la cantidad | 141 | //Agrego el articulo con la cantidad | 
| 142 | punto.articulosAgregados.push({ | 142 | punto.articulosAgregados.push({ | 
| 143 | id: articulo.idArticulo, | 143 | id: articulo.idArticulo, | 
| 144 | descripcion: articulo.descripcion, | 144 | descripcion: articulo.descripcion, | 
| 145 | cantidad: punto.cantidadACargar, | 145 | cantidad: punto.cantidadACargar, | 
| 146 | index: $scope.articuloSeleccionado | 146 | index: $scope.articuloSeleccionado | 
| 147 | }); | 147 | }); | 
| 148 | } | 148 | } | 
| 149 | punto.cantidadACargar = 0; | 149 | punto.cantidadACargar = 0; | 
| 150 | } | 150 | } | 
| 151 | } | 151 | } | 
| 152 | }; | 152 | }; | 
| 153 | 153 | ||
| 154 | $scope.quitarArticulo = function(articulo, idx, punto) { | 154 | $scope.quitarArticulo = function(articulo, idx, punto) { | 
| 155 | var articuloAEliminar = $scope.articulos.filter(function(art) { | 155 | var articuloAEliminar = $scope.articulos.filter(function(art) { | 
| 156 | return art.id === articulo.id; | 156 | return art.id === articulo.id; | 
| 157 | }); | 157 | }); | 
| 158 | var restante = parseInt(articuloAEliminar[0].restante); | 158 | var restante = parseInt(articuloAEliminar[0].restante); | 
| 159 | restante += parseInt(articulo.cantidad); | 159 | restante += parseInt(articulo.cantidad); | 
| 160 | articuloAEliminar[0].restante = restante; | 160 | articuloAEliminar[0].restante = restante; | 
| 161 | 161 | ||
| 162 | punto.cargado -= parseInt(punto.articulosAgregados[idx].cantidad); | 162 | punto.cargado -= parseInt(punto.articulosAgregados[idx].cantidad); | 
| 163 | punto.articulosAgregados.splice(idx, 1); | 163 | punto.articulosAgregados.splice(idx, 1); | 
| 164 | }; | 164 | }; | 
| 165 | 165 | ||
| 166 | function actualizarTabla() { | 166 | function actualizarTabla() { | 
| 167 | focaModalPuntoDescargaService | 167 | focaModalPuntoDescargaService | 
| 168 | .getPuntosDescargaByClienDom(filters.idDomicilio, filters.idCliente) | 168 | .getPuntosDescargaByClienDom(filters.idDomicilio, filters.idCliente) | 
| 169 | .then(function(res) { | 169 | .then(function(res) { | 
| 170 | $scope.puntosDescarga = res.data; | 170 | $scope.puntosDescarga = res.data; | 
| 171 | }); | 171 | }); | 
| 172 | } | 172 | } | 
| 173 | function verCargaArticulos() { | 173 | function verCargaArticulos() { | 
| 174 | $scope.puntosACargar = []; | 174 | $scope.puntosACargar = []; | 
| 175 | $scope.cargaArticulos = true; | 175 | $scope.cargaArticulos = true; | 
| 176 | $scope.puntosSeleccionados.forEach(function(idx) { | 176 | $scope.puntosSeleccionados.forEach(function(idx) { | 
| 177 | $scope.puntosACargar.push($scope.puntosDescarga[idx]); | 177 | $scope.puntosACargar.push($scope.puntosDescarga[idx]); | 
| 178 | }); | 178 | }); | 
| 179 | 179 | ||
| 180 | $scope.puntosACargar.map(function(punto) { | 180 | $scope.puntosACargar.map(function(punto) { | 
| 181 | punto.articulosAgregados = []; | 181 | punto.articulosAgregados = []; | 
| 182 | punto.cantidadACargar = 0; | 182 | punto.cantidadACargar = 0; | 
| 183 | punto.cargado = 0; | 183 | punto.cargado = 0; | 
| 184 | }); | 184 | }); | 
| 185 | } | 185 | } | 
| 186 | function cargarArticulos() { | 186 | function cargarArticulos() { | 
| 187 | $uibModalInstance.close($scope.puntosACargar); | 187 | $uibModalInstance.close($scope.puntosACargar); | 
| 188 | } | 188 | } | 
| 189 | function cargarPuntos(puntosDescarga) { | 189 | function cargarPuntos(puntosDescarga) { | 
| 190 | //Si existen puntos ya cargados | 190 | //Si existen puntos ya cargados | 
| 191 | if (puntosDescarga) { | 191 | if (puntosDescarga) { | 
| 192 | if (!puntosDescarga[0].cargado) { | 192 | if (!puntosDescarga[0].cargado) { | 
| 193 | agregarTotalCargado(puntosDescarga); | 193 | agregarTotalCargado(puntosDescarga); | 
| 194 | } | 194 | } | 
| 195 | $scope.puntosACargar = puntosDescarga; | 195 | $scope.puntosACargar = puntosDescarga; | 
| 196 | $scope.cargaArticulos = true; | 196 | $scope.cargaArticulos = true; | 
| 197 | //Recorro los puntos | 197 | //Recorro los puntos | 
| 198 | puntosDescarga.forEach(function(punto) { | 198 | puntosDescarga.forEach(function(punto) { | 
| 199 | //Recorro los articulos cargados en cada punto | 199 | //Recorro los articulos cargados en cada punto | 
| 200 | punto.articulosAgregados.forEach(function(articulo) { | 200 | punto.articulosAgregados.forEach(function(articulo) { | 
| 201 | var articuloARestar = $scope.articulos.filter(function(art) { | 201 | var articuloARestar = $scope.articulos.filter(function(art) { | 
| 202 | return art.idArticulo === articulo.id; | 202 | return art.idArticulo === articulo.id; | 
| 203 | }); | 203 | }); | 
| 204 | articuloARestar[0].restante -= articulo.cantidad; | 204 | articuloARestar[0].restante -= articulo.cantidad; | 
| 205 | }); | 205 | }); | 
| 206 | }); | 206 | }); | 
| 207 | } | 207 | } | 
| 208 | } | 208 | } | 
| 209 | function agregarTotalCargado(puntosDescarga) { | 209 | function agregarTotalCargado(puntosDescarga) { | 
| 210 | puntosDescarga.map(function(punto) { | 210 | puntosDescarga.map(function(punto) { | 
| 211 | punto.cantidadACargar = 0; | 211 | punto.cantidadACargar = 0; | 
| 212 | punto.cargado = 0; | 212 | punto.cargado = 0; | 
| 213 | }); | 213 | }); | 
| 214 | //Agrego cantidad de combustible cargada en los puntos de descarga | 214 | //Agrego cantidad de combustible cargada en los puntos de descarga | 
| 215 | puntosDescarga.forEach(function(punto) { | 215 | puntosDescarga.forEach(function(punto) { | 
| 216 | punto.articulosAgregados.forEach(function(articulo) { | 216 | punto.articulosAgregados.forEach(function(articulo) { | 
| 217 | punto.cargado += articulo.cantidad; | 217 | punto.cargado += articulo.cantidad; | 
| 218 | }); | 218 | }); | 
| 219 | }); | 219 | }); | 
| 220 | } | 220 | } | 
| 221 | function enviarPuntos() { | 221 | function enviarPuntos() { | 
| 222 | var result = []; | 222 | var result = []; | 
| 223 | $scope.puntosSeleccionados.forEach(function(idx) { | 223 | $scope.puntosSeleccionados.forEach(function(idx) { | 
| 224 | result.push($scope.puntosDescarga[idx]); | 224 | result.push($scope.puntosDescarga[idx]); | 
| 225 | }); | 225 | }); | 
| 226 | $uibModalInstance.close(result); | 226 | $uibModalInstance.close(result); | 
| 227 | } | 227 | } | 
| 228 | }] | 228 | }] | 
| 229 | ); | 229 | ); | 
| 230 | 230 | 
src/js/osm-directive.js
| 1 | angular.module('focaModalPuntoDescarga').directive('osmPuntoDescarga', function() { | 1 | angular.module('focaModalPuntoDescarga').directive('osmPuntoDescarga', function() { | 
| 2 | return { | 2 | return { | 
| 3 | restrict: 'E', | 3 | restrict: 'E', | 
| 4 | link: function(scope, el, attrs) { | 4 | link: function(scope, el, attrs) { | 
| 5 | var contenedor = document.createElement('div'); | 5 | var contenedor = document.createElement('div'); | 
| 6 | contenedor.className = 'w-100 h-50 mt-3'; | 6 | contenedor.className = 'w-100 h-50 mt-3'; | 
| 7 | el.append(contenedor); | 7 | el.append(contenedor); | 
| 8 | scope.map = L.map(contenedor); | 8 | scope.map = L.map(contenedor); | 
| 9 | L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(scope.map); | 9 | L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(scope.map); | 
| 10 | }, | 10 | }, | 
| 11 | controller: ['$scope', '$timeout', function($scope, $timeout) { | 11 | controller: ['$scope', '$timeout', function($scope, $timeout) { | 
| 12 | //resuelve bug mapa gris en modales | 12 | //resuelve bug mapa gris en modales | 
| 13 | $timeout(function() { | 13 | $timeout(function() { | 
| 14 | $scope.map.invalidateSize(); | 14 | $scope.map.invalidateSize(); | 
| 15 | }, 100); | 15 | }, 100); | 
| 16 | 16 | ||
| 17 | $scope.markers = []; | 17 | $scope.markers = []; | 
| 18 | $scope.$watchGroup(['latitud', 'longitud'], function() { | 18 | $scope.$watchGroup(['latitud', 'longitud'], function() { | 
| 19 | for(var i in $scope.markers) { | 19 | for(var i in $scope.markers) { | 
| 20 | $scope.map.removeLayer($scope.markers[i]); | 20 | $scope.map.removeLayer($scope.markers[i]); | 
| 21 | } | 21 | } | 
| 22 | $scope.map.setView([$scope.latitud, $scope.longitud], 14); | 22 | $scope.map.setView([$scope.latitud, $scope.longitud], 14); | 
| 23 | $scope.markers.push( | 23 | $scope.markers.push( | 
| 24 | L.marker([$scope.latitud, $scope.longitud], {draggable: true}) | 24 | L.marker([$scope.latitud, $scope.longitud], {draggable: true}) | 
| 25 | .addTo($scope.map) | 25 | .addTo($scope.map) | 
| 26 | .on('dragend', function() { | 26 | .on('dragend', function() { | 
| 27 | $scope.latitud = this.getLatLng().lat; | 27 | $scope.latitud = this.getLatLng().lat; | 
| 28 | $scope.longitud = this.getLatLng().lng; | 28 | $scope.longitud = this.getLatLng().lng; | 
| 29 | $scope.$apply(); | 29 | $scope.$apply(); | 
| 30 | }) | 30 | }) | 
| 31 | ); | 31 | ); | 
| 32 | }); | 32 | }); | 
| 33 | }], | 33 | }], | 
| 34 | |||
| 34 | scope: { | 35 | scope: { | 
| 35 | latitud: '=', | 36 | latitud: '=', | 
| 36 | longitud: '=', | 37 | longitud: '=', | 
| 37 | zoom: '=' | 38 | zoom: '=' | 
| 38 | } | 39 | } | 
| 39 | }; | 40 | }; | 
| 40 | }); | 41 | }); | 
| 41 | 42 |