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