Commit acb9dc7e2c45dcbdfac36e9f30c700c722f06343
1 parent
aaf537312e
Exists in
master
vehiculo y remito en promesa, cisternas no disponibles en funcion, auto completa…
…r cantidad en cisternas
Showing
1 changed file
with
63 additions
and
17 deletions
Show diff stats
src/js/controllerDetalleVehiculo.js
1 | angular.module('focaLogisticaPedidoRuta') | 1 | angular.module('focaLogisticaPedidoRuta') |
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 | 'focaLogisticaPedidoRutaService', | 9 | 'focaLogisticaPedidoRutaService', |
10 | function($scope, $uibModalInstance, idVehiculo, idRemito, focaModalService, $filter, | 10 | function($scope, $uibModalInstance, idVehiculo, idRemito, focaModalService, $filter, |
11 | focaLogisticaPedidoRutaService | 11 | focaLogisticaPedidoRutaService |
12 | ) { | 12 | ) { |
13 | //seteo variables | 13 | //seteo variables |
14 | $scope.cargandoDatos = true; | ||
14 | $scope.idRemito = idRemito; | 15 | $scope.idRemito = idRemito; |
15 | $scope.articulos = []; | 16 | $scope.articulos = []; |
16 | $scope.vehiculo = {}; | 17 | $scope.vehiculo = {}; |
17 | $scope.remito = {}; | 18 | $scope.remito = {}; |
18 | $scope.aCargar = []; | 19 | $scope.aCargar = []; |
19 | var cisternaMovimientos = []; | 20 | var cisternaMovimientos = []; |
20 | focaLogisticaPedidoRutaService.obtenerVehiculoById(idVehiculo).then( | 21 | var promesaVehiculo = focaLogisticaPedidoRutaService.obtenerVehiculoById(idVehiculo); |
21 | function(res) { | 22 | var promesaRemito; |
22 | $scope.vehiculo = res.data; | ||
23 | } | ||
24 | ); | ||
25 | if(idRemito !== -1) { | 23 | if(idRemito !== -1) { |
26 | focaLogisticaPedidoRutaService.obtenerRemitoById(idRemito).then( | 24 | promesaRemito = focaLogisticaPedidoRutaService.obtenerRemitoById(idRemito); |
27 | function(res) { | ||
28 | $scope.remito = res.data; | ||
29 | if($scope.remito.idUsuarioProceso) { | ||
30 | focaModalService.alert('Remito ya asignado'); | ||
31 | $uibModalInstance.close(); | ||
32 | } | ||
33 | $scope.articulos = res.data.articulosRemito; | ||
34 | $scope.cambioArticulo($scope.articulos[0]); | ||
35 | } | ||
36 | ); | ||
37 | } | 25 | } |
26 | Promise.all([promesaVehiculo, promesaRemito]).then(function(res) { | ||
27 | $scope.cargandoDatos = false; | ||
28 | $scope.vehiculo = res[0].data; | ||
29 | if(!res[1]) return; | ||
30 | $scope.remito = res[1].data; | ||
31 | if($scope.remito.idUsuarioProceso) { | ||
32 | focaModalService.alert('Remito ya asignado'); | ||
33 | $uibModalInstance.close(); | ||
34 | } | ||
35 | $scope.articulos = $scope.remito.articulosRemito; | ||
36 | $scope.cambioArticulo($scope.articulos[0]); | ||
37 | }); | ||
38 | $scope.aceptar = function() { | 38 | $scope.aceptar = function() { |
39 | $scope.cargando = true; | 39 | $scope.cargando = true; |
40 | var cisternaCargas = []; | 40 | var cisternaCargas = []; |
41 | for (var i = 0; i < $scope.vehiculo.cisternas.length; i++) { | 41 | for (var i = 0; i < $scope.vehiculo.cisternas.length; i++) { |
42 | delete $scope.vehiculo.cisternas[i].cisternaCarga.articulo; | 42 | delete $scope.vehiculo.cisternas[i].cisternaCarga.articulo; |
43 | cisternaCargas.push($scope.vehiculo.cisternas[i].cisternaCarga); | 43 | cisternaCargas.push($scope.vehiculo.cisternas[i].cisternaCarga); |
44 | } | 44 | } |
45 | var cisterna = { | 45 | var cisterna = { |
46 | cisternaMovimientos: cisternaMovimientos, | 46 | cisternaMovimientos: cisternaMovimientos, |
47 | cisternaCargas: cisternaCargas, | 47 | cisternaCargas: cisternaCargas, |
48 | idVehiculo: $scope.vehiculo.id | 48 | idVehiculo: $scope.vehiculo.id |
49 | }; | 49 | }; |
50 | focaLogisticaPedidoRutaService.guardarCisternas(cisterna, $scope.remito.id) | 50 | focaLogisticaPedidoRutaService.guardarCisternas(cisterna, $scope.remito.id) |
51 | .then(function() { | 51 | .then(function() { |
52 | focaModalService.alert('Cisternas cargadas con éxito').then(function() { | 52 | focaModalService.alert('Cisternas cargadas con éxito').then(function() { |
53 | $scope.cargando = false; | 53 | $scope.cargando = false; |
54 | $uibModalInstance.close(); | 54 | $uibModalInstance.close(); |
55 | }); | 55 | }); |
56 | }).catch(function(error) { | 56 | }).catch(function(error) { |
57 | $scope.cargando = false; | 57 | $scope.cargando = false; |
58 | $uibModalInstance.close(); | 58 | $uibModalInstance.close(); |
59 | if (error.status === 403.1) { | 59 | if (error.status === 403.1) { |
60 | focaModalService.alert('ERROR: El vehículo esta en uso'); | 60 | focaModalService.alert('ERROR: El vehículo esta en uso'); |
61 | } | 61 | } |
62 | if(error.status === 403.2) { | 62 | if(error.status === 403.2) { |
63 | focaModalService.alert('ERROR: Otro usario ya cargó este remito'); | 63 | focaModalService.alert('ERROR: Otro usario ya cargó este remito'); |
64 | return; | 64 | return; |
65 | } | 65 | } |
66 | focaModalService.alert('Hubo un error al cargar las cisternas'); | 66 | focaModalService.alert('Hubo un error al cargar las cisternas'); |
67 | }); | 67 | }); |
68 | }; | 68 | }; |
69 | 69 | ||
70 | $scope.cancelar = function() { | 70 | $scope.cancelar = function() { |
71 | $uibModalInstance.close(); | 71 | $uibModalInstance.close(); |
72 | }; | 72 | }; |
73 | 73 | ||
74 | $scope.cargarACisternas = function(vehiculo) { | 74 | $scope.cargarACisternas = function(vehiculo) { |
75 | for(var i = 0; i < vehiculo.cisternas.length; i++) { | 75 | for(var i = 0; i < vehiculo.cisternas.length; i++) { |
76 | var cisterna = vehiculo.cisternas[i]; | 76 | var cisterna = vehiculo.cisternas[i]; |
77 | var aCargar = parseFloat($scope.aCargar[i]); | 77 | var aCargar = parseFloat($scope.aCargar[i]); |
78 | //validaciones | 78 | //validaciones |
79 | if(!aCargar) { | 79 | if(!aCargar) { |
80 | continue; | 80 | continue; |
81 | } | 81 | } |
82 | if(aCargar > cisterna.disponible) { | 82 | if(aCargar > cisterna.disponible) { |
83 | focaModalService.alert('La cantidad cargada supera la capacidad de la' + | 83 | focaModalService.alert('La cantidad cargada supera la capacidad de la' + |
84 | 'cisterna ' + cisterna.codigo); | 84 | 'cisterna ' + cisterna.codigo); |
85 | return; | 85 | return; |
86 | } | 86 | } |
87 | //cargar | 87 | //cargar |
88 | if(cisterna.cisternaCarga.cantidad) { | 88 | if(cisterna.cisternaCarga.cantidad) { |
89 | cisterna.cisternaCarga.cantidad += aCargar; | 89 | cisterna.cisternaCarga.cantidad += aCargar; |
90 | }else { | 90 | }else { |
91 | cisterna.cisternaCarga.cantidad = aCargar; | 91 | cisterna.cisternaCarga.cantidad = aCargar; |
92 | cisterna.cisternaCarga.idProducto = $scope.articuloSeleccionado.idArticulo; | 92 | cisterna.cisternaCarga.idProducto = $scope.articuloSeleccionado.idArticulo; |
93 | } | 93 | } |
94 | cisterna.disponible = cisterna.capacidad - cisterna.cisternaCarga.cantidad; | 94 | cisterna.disponible = cisterna.capacidad - cisterna.cisternaCarga.cantidad; |
95 | 95 | ||
96 | cisterna.cisternaCarga.articulo = { | 96 | cisterna.cisternaCarga.articulo = { |
97 | DetArt: $scope.articuloSeleccionado.descripcion | 97 | DetArt: $scope.articuloSeleccionado.descripcion |
98 | }; | 98 | }; |
99 | $filter('filter')($scope.articulos, {id: $scope.articuloSeleccionado.id})[0] | 99 | $filter('filter')($scope.articulos, {id: $scope.articuloSeleccionado.id})[0] |
100 | .cargado = true; | 100 | .cargado = true; |
101 | 101 | ||
102 | $scope.calcularPorcentaje(cisterna); | 102 | $scope.calcularPorcentaje(cisterna); |
103 | //Guardar | 103 | //Guardar |
104 | var now = new Date(); | 104 | var now = new Date(); |
105 | var cisternaMovimiento = { | 105 | var cisternaMovimiento = { |
106 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), | 106 | fecha: now.toISOString().slice(0, 19).replace('T', ' '), |
107 | cantidad: aCargar, | 107 | cantidad: aCargar, |
108 | metodo: 'carga', | 108 | metodo: 'carga', |
109 | idCisternaCarga: cisterna.cisternaCarga.id, | 109 | idCisternaCarga: cisterna.cisternaCarga.id, |
110 | idRemito: $scope.remito.id | 110 | idRemito: $scope.remito.id |
111 | }; | 111 | }; |
112 | cisternaMovimientos.push(cisternaMovimiento); | 112 | cisternaMovimientos.push(cisternaMovimiento); |
113 | } | 113 | } |
114 | var articuloSiguiente = $scope.articulos.filter( | 114 | var articuloSiguiente = $scope.articulos.filter( |
115 | function(filter) { | 115 | function(filter) { |
116 | return filter.cargado !== true; | 116 | return filter.cargado !== true; |
117 | } | 117 | } |
118 | ); | 118 | ); |
119 | if(articuloSiguiente.length > 0) { | 119 | if(articuloSiguiente.length > 0) { |
120 | $scope.cambioArticulo(articuloSiguiente[0]); | 120 | $scope.cambioArticulo(articuloSiguiente[0]); |
121 | } | 121 | } |
122 | $scope.aCargar = []; | 122 | $scope.aCargar = []; |
123 | }; | 123 | }; |
124 | $scope.calcularPorcentaje = function(cisterna) { | 124 | $scope.calcularPorcentaje = function(cisterna) { |
125 | if(!cisterna.cisternaCarga.cantidad) { | 125 | if(!cisterna.cisternaCarga.cantidad) { |
126 | cisterna.cisternaCarga.cantidad = 0; | 126 | cisterna.cisternaCarga.cantidad = 0; |
127 | } | 127 | } |
128 | var porcentaje = (cisterna.cisternaCarga.cantidad * 100 / | 128 | var porcentaje = (cisterna.cisternaCarga.cantidad * 100 / |
129 | cisterna.capacidad) + '%'; | 129 | cisterna.capacidad) + '%'; |
130 | var elementHtml = document.getElementById(cisterna.id); | 130 | var elementHtml = document.getElementById(cisterna.id); |
131 | if(elementHtml) { | 131 | if(elementHtml) { |
132 | elementHtml.style.width = porcentaje; | 132 | elementHtml.style.width = porcentaje; |
133 | } | 133 | } |
134 | }; | 134 | }; |
135 | $scope.cambioArticulo = function(articulo) { | 135 | $scope.cambioArticulo = function(articulo) { |
136 | articulo.checked = true; | 136 | $scope.aCargar = []; |
137 | $filter('filter')($scope.articulos, {id: articulo.id})[0].checked = true; | ||
137 | $scope.articuloSeleccionado = articulo; | 138 | $scope.articuloSeleccionado = articulo; |
139 | for (var i = 0; i < $scope.vehiculo.cisternas.length; i++) { | ||
1 |
|
||
140 | $scope.vehiculo.cisternas[i] = | ||
141 | $scope.cisternaDisabled($scope.vehiculo.cisternas[i]); | ||
142 | } | ||
143 | var disponible = $filter('filter')($scope.vehiculo.cisternas, {disabled: false}); | ||
144 | var index = $scope.vehiculo.cisternas.indexOf(disponible[0]); | ||
145 | $scope.aCargar[index] = $scope.articuloSeleccionado.cantidad; | ||
146 | $scope.actualizarArticulo(); | ||
138 | }; | 147 | }; |
139 | $scope.actualizarArticulo = function () { | 148 | $scope.actualizarArticulo = function () { |
140 | $scope.articuloSeleccionado.cantidadCargada = 0; | 149 | $scope.articuloSeleccionado.cantidadCargada = 0; |
141 | for (var i = 0; i < $scope.aCargar.length; i++) { | 150 | for (var i = 0; i < $scope.aCargar.length; i++) { |
142 | $scope.articuloSeleccionado.cantidadCargada += | 151 | $scope.articuloSeleccionado.cantidadCargada += |
143 | parseFloat($scope.aCargar[i]) || 0; | 152 | parseFloat($scope.aCargar[i]) || 0; |
144 | } | 153 | } |
145 | }; | 154 | }; |
155 | $scope.cisternaDisabled = function(cisterna) { | ||
156 | if(!$scope.articuloSeleccionado || ($scope.articuloSeleccionado.idArticulo !== | ||
157 | cisterna.cisternaCarga.idProducto && cisterna.cisternaCarga.idProducto) || | ||
158 | !$scope.tieneArticulosPendientes()|| $scope.articuloSeleccionado.cantidad > | ||
159 | cisterna.disponible) { | ||
160 | cisterna.disabled = true; | ||
161 | }else { | ||
162 | cisterna.disabled = false; | ||
163 | } | ||
164 | return cisterna; | ||
165 | }; | ||
166 | $scope.rellenarInput = function(input = 0) { | ||
167 | if(!$scope.articuloSeleccionado) return; | ||
168 | input = parseFloat(input); | ||
169 | if(input === $scope.articuloSeleccionado.cantidad || | ||
170 | $scope.articuloSeleccionado.cantidad - | ||
171 | $scope.articuloSeleccionado.cantidadCargada === 0) { | ||
172 | return input; | ||
173 | } | ||
174 | input += parseFloat($scope.articuloSeleccionado.cantidad - | ||
175 | $scope.articuloSeleccionado.cantidadCargada); | ||
176 | return input; | ||
177 | }; | ||
178 | $scope.distribucionDisponible = function() { | ||
179 | if(!$scope.articuloSeleccionado || $scope.articuloSeleccionado.cantidad - | ||
180 | $scope.articuloSeleccionado.cantidadCargada !== 0 || | ||
181 | !$scope.tieneArticulosPendientes()) { | ||
182 | return false; | ||
183 | } | ||
184 | return true; | ||
185 | }; | ||
146 | $scope.tieneArticulosPendientes = function() { | 186 | $scope.tieneArticulosPendientes = function() { |
187 | var algunValorNegativo = $scope.aCargar.filter(function(p) { | ||
188 | return p < 0; | ||
189 | }); |