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