Commit 0fd397057c75448a9820d778864405aa5f7fe67c
1 parent
51c9800474
Exists in
master
Arreglo en alerta
Showing
1 changed file
with
3 additions
and
9 deletions
Show diff stats
src/js/controller.js
1 | angular.module('focaAbmVehiculo') | 1 | angular.module('focaAbmVehiculo') |
2 | .controller('focaAbmVehiculosController', [ | 2 | .controller('focaAbmVehiculosController', [ |
3 | '$scope', 'focaAbmVehiculoService', '$location', 'focaModalService', | 3 | '$scope', 'focaAbmVehiculoService', '$location', 'focaModalService', |
4 | '$uibModal', 'focaBotoneraLateralService', '$timeout', '$localStorage', | 4 | '$uibModal', 'focaBotoneraLateralService', '$timeout', '$localStorage', |
5 | function ($scope, focaAbmVehiculoService, $location, focaModalService, | 5 | function ($scope, focaAbmVehiculoService, $location, focaModalService, |
6 | $uibModal, focaBotoneraLateralService, $timeout, $localStorage) { | 6 | $uibModal, focaBotoneraLateralService, $timeout, $localStorage) { |
7 | 7 | ||
8 | $scope.now = new Date(); | 8 | $scope.now = new Date(); |
9 | $scope.tipo = ''; | 9 | $scope.tipo = ''; |
10 | $scope.botonera = [{ | 10 | $scope.botonera = [{ |
11 | label: 'Transportista', | 11 | label: 'Transportista', |
12 | image: 'cliente.png' | 12 | image: 'cliente.png' |
13 | }]; | 13 | }]; |
14 | 14 | ||
15 | focaAbmVehiculoService.cleanCisternas(); | 15 | focaAbmVehiculoService.cleanCisternas(); |
16 | 16 | ||
17 | //SETEO BOTONERA LATERAL | 17 | //SETEO BOTONERA LATERAL |
18 | focaBotoneraLateralService.showSalir(false); | 18 | focaBotoneraLateralService.showSalir(false); |
19 | focaBotoneraLateralService.showPausar(false); | 19 | focaBotoneraLateralService.showPausar(false); |
20 | focaBotoneraLateralService.showCancelar(false); | 20 | focaBotoneraLateralService.showCancelar(false); |
21 | focaBotoneraLateralService.showGuardar(false); | 21 | focaBotoneraLateralService.showGuardar(false); |
22 | focaBotoneraLateralService.addCustomButton('Salir', salir); | 22 | focaBotoneraLateralService.addCustomButton('Salir', salir); |
23 | 23 | ||
24 | if (focaAbmVehiculoService.transportistaSeleccionado.COD) { | 24 | if (focaAbmVehiculoService.transportistaSeleccionado.COD) { |
25 | elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado); | 25 | elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado); |
26 | } | 26 | } |
27 | $scope.editar = function (id) { | 27 | $scope.editar = function (id) { |
28 | $location.path('/vehiculo/' + id + '/' + $scope.idTransportista); | 28 | $location.path('/vehiculo/' + id + '/' + $scope.idTransportista); |
29 | }; | 29 | }; |
30 | 30 | ||
31 | $scope.solicitarConfirmacion = function (vehiculo) { | 31 | $scope.solicitarConfirmacion = function (vehiculo) { |
32 | focaModalService.confirm('¿Está seguro que desea borrar el vehiculo ' + | 32 | focaModalService.confirm('¿Está seguro que desea borrar el vehiculo ' + |
33 | vehiculo.id + ' ' + vehiculo.tractor + ' ?').then( | 33 | vehiculo.id + ' ' + vehiculo.tractor + ' ?').then( |
34 | function (data) { | 34 | function (data) { |
35 | if (data) { | 35 | if (data) { |
36 | focaAbmVehiculoService.deleteVehiculo(vehiculo.id); | 36 | focaAbmVehiculoService.deleteVehiculo(vehiculo.id); |
37 | $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 1); | 37 | $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 1); |
38 | } | 38 | } |
39 | } | 39 | } |
40 | ); | 40 | ); |
41 | }; | 41 | }; |
42 | $scope.seleccionarTransportista = function () { | 42 | $scope.seleccionarTransportista = function () { |
43 | var parametrosModal = { | 43 | var parametrosModal = { |
44 | titulo: 'Búsqueda de Transportista', | 44 | titulo: 'Búsqueda de Transportista', |
45 | query: '/transportista', | 45 | query: '/transportista', |
46 | columnas: [ | 46 | columnas: [ |
47 | { | 47 | { |
48 | nombre: 'Código', | 48 | nombre: 'Código', |
49 | propiedad: 'COD' | 49 | propiedad: 'COD' |
50 | }, | 50 | }, |
51 | { | 51 | { |
52 | nombre: 'Nombre', | 52 | nombre: 'Nombre', |
53 | propiedad: 'NOM' | 53 | propiedad: 'NOM' |
54 | }, | 54 | }, |
55 | { | 55 | { |
56 | nombre: 'CUIT', | 56 | nombre: 'CUIT', |
57 | propiedad: 'CUIT' | 57 | propiedad: 'CUIT' |
58 | } | 58 | } |
59 | ] | 59 | ] |
60 | }; | 60 | }; |
61 | focaModalService.modal(parametrosModal).then( | 61 | focaModalService.modal(parametrosModal).then( |
62 | function (transportista) { | 62 | function (transportista) { |
63 | elegirTransportista(transportista); | 63 | elegirTransportista(transportista); |
64 | focaAbmVehiculoService.transportistaSeleccionado = transportista; | 64 | focaAbmVehiculoService.transportistaSeleccionado = transportista; |
65 | }, function () { | 65 | }, function () { |
66 | 66 | ||
67 | } | 67 | } |
68 | ); | 68 | ); |
69 | }; | 69 | }; |
70 | function elegirTransportista(transportista) { | 70 | function elegirTransportista(transportista) { |
71 | var codigo = ('00000' + transportista.COD).slice(-5); | 71 | var codigo = ('00000' + transportista.COD).slice(-5); |
72 | $scope.idTransportista = transportista.COD; | 72 | $scope.idTransportista = transportista.COD; |
73 | $scope.filtros = transportista.NOM.trim(); | 73 | $scope.filtros = transportista.NOM.trim(); |
74 | $timeout(function () { | 74 | $timeout(function () { |
75 | $scope.$broadcast('addCabecera', { | 75 | $scope.$broadcast('addCabecera', { |
76 | label: 'Transportista:', | 76 | label: 'Transportista:', |
77 | valor: codigo + ' - ' + transportista.NOM | 77 | valor: codigo + ' - ' + transportista.NOM |
78 | }); | 78 | }); |
79 | }); | 79 | }); |
80 | buscar(transportista.COD); | 80 | buscar(transportista.COD); |
81 | } | 81 | } |
82 | 82 | ||
83 | function buscar(idTransportista) { | 83 | function buscar(idTransportista) { |
84 | focaAbmVehiculoService | 84 | focaAbmVehiculoService |
85 | .getVehiculosPorTransportista(idTransportista) | 85 | .getVehiculosPorTransportista(idTransportista) |
86 | .then(function (datos) { | 86 | .then(function (datos) { |
87 | $scope.vehiculos = datos.data; | 87 | $scope.vehiculos = datos.data; |
88 | }); | 88 | }); |
89 | } | 89 | } |
90 | function salir() { | 90 | function salir() { |
91 | focaAbmVehiculoService.transportistaSeleccionado = {}; | 91 | focaAbmVehiculoService.transportistaSeleccionado = {}; |
92 | $location.path('/'); | 92 | $location.path('/'); |
93 | } | 93 | } |
94 | } | 94 | } |
95 | ]) | 95 | ]) |
96 | .controller('focaAbmVehiculoController', [ | 96 | .controller('focaAbmVehiculoController', [ |
97 | '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal', | 97 | '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal', |
98 | 'focaModalService', '$timeout', 'focaBotoneraLateralService', '$localStorage', '$filter', | 98 | 'focaModalService', '$timeout', 'focaBotoneraLateralService', '$localStorage', '$filter', |
99 | function ($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal, | 99 | function ($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal, |
100 | focaModalService, $timeout, focaBotoneraLateralService, $localStorage, $filter) { | 100 | focaModalService, $timeout, focaBotoneraLateralService, $localStorage, $filter) { |
101 | $scope.nuevo = $routeParams.idVehiculo === '0' ? true : false; | 101 | $scope.nuevo = $routeParams.idVehiculo === '0' ? true : false; |
102 | $scope.nuevoCisterna = ($routeParams.idx > -1) ? false : true; | 102 | $scope.nuevoCisterna = ($routeParams.idx > -1) ? false : true; |
103 | $scope.now = new Date(); | 103 | $scope.now = new Date(); |
104 | $scope.focused = 1; | 104 | $scope.focused = 1; |
105 | $scope.tipo = ''; | 105 | $scope.tipo = ''; |
106 | $scope.creando = false; | 106 | $scope.creando = false; |
107 | $scope.crear = false; | 107 | $scope.crear = false; |
108 | $scope.transportistaStamp = ''; | 108 | $scope.transportistaStamp = ''; |
109 | $scope.cisternas = []; | 109 | $scope.cisternas = []; |
110 | $scope.cisternasInicial = []; | ||
110 | 111 | ||
111 | $timeout(function () { | 112 | $timeout(function () { |
112 | focaBotoneraLateralService.showSalir(false); | 113 | focaBotoneraLateralService.showSalir(false); |
113 | focaBotoneraLateralService.showPausar(true); | 114 | focaBotoneraLateralService.showPausar(true); |
114 | focaBotoneraLateralService.showCancelar(false); | 115 | focaBotoneraLateralService.showCancelar(false); |
115 | focaBotoneraLateralService.showGuardar(true, $scope.guardar); | 116 | focaBotoneraLateralService.showGuardar(true, $scope.guardar); |
116 | focaBotoneraLateralService.addCustomButton('Salir', $scope.salir); | 117 | focaBotoneraLateralService.addCustomButton('Salir', $scope.salir); |
117 | }); | 118 | }); |
118 | 119 | ||
119 | if ($scope.nuevo) { | 120 | if ($scope.nuevo) { |
120 | $scope.tipo = 'Crear Vehiculo'; | 121 | $scope.tipo = 'Crear Vehiculo'; |
121 | focaAbmVehiculoService | 122 | focaAbmVehiculoService |
122 | .getTransportistaPorId($routeParams.idTransportista) | 123 | .getTransportistaPorId($routeParams.idTransportista) |
123 | .then(function (res) { | 124 | .then(function (res) { |
124 | var codigo = ('00000' + res.data[0].COD).slice(-5); | 125 | var codigo = ('00000' + res.data[0].COD).slice(-5); |
125 | $scope.vehiculo.idTransportista = res.data[0].COD; | 126 | $scope.vehiculo.idTransportista = res.data[0].COD; |
126 | $scope.vehiculo.transportista = res.data; | 127 | $scope.vehiculo.transportista = res.data; |
127 | $scope.vehiculo.capacidad = 0; | 128 | $scope.vehiculo.capacidad = 0; |
128 | $scope.$broadcast('addCabecera', { | 129 | $scope.$broadcast('addCabecera', { |
129 | label: 'Transportista:', | 130 | label: 'Transportista:', |
130 | valor: codigo + ' - ' + res.data[0].ALI | 131 | valor: codigo + ' - ' + res.data[0].ALI |
131 | }); | 132 | }); |
132 | $scope.$broadcast('addCabecera', { | 133 | $scope.$broadcast('addCabecera', { |
133 | label: 'Unidad:', | 134 | label: 'Unidad:', |
134 | valor: 'NUEVO' | 135 | valor: 'NUEVO' |
135 | }); | 136 | }); |
136 | }); | 137 | }); |
137 | } else { | 138 | } else { |
138 | $scope.tipo = 'Editar Vehiculo'; | 139 | $scope.tipo = 'Editar Vehiculo'; |
139 | } | 140 | } |
140 | $scope.vehiculo = {}; | 141 | $scope.vehiculo = {}; |
141 | 142 | ||
142 | focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function (res) { | 143 | focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function (res) { |
143 | if (res.data) { | 144 | if (res.data) { |
144 | var vehiculoSeteado = getLSVehiculo(); | 145 | var vehiculoSeteado = getLSVehiculo(); |
145 | if (vehiculoSeteado === false) { | 146 | if (vehiculoSeteado === false) { |
146 | $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5); | 147 | $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5); |
147 | $scope.transportistaStamp += ' - ' + res.data.transportista.NOM; | 148 | $scope.transportistaStamp += ' - ' + res.data.transportista.NOM; |
148 | 149 | ||
149 | $scope.vehiculo = res.data; | 150 | $scope.vehiculo = res.data; |
150 | $scope.$broadcast('addCabecera', { | 151 | $scope.$broadcast('addCabecera', { |
151 | label: 'Transportista:', | 152 | label: 'Transportista:', |
152 | valor: $scope.transportistaStamp | 153 | valor: $scope.transportistaStamp |
153 | }); | 154 | }); |
154 | $scope.$broadcast('addCabecera', { | 155 | $scope.$broadcast('addCabecera', { |
155 | label: 'Unidad:', | 156 | label: 'Unidad:', |
156 | valor: res.data.codigo | 157 | valor: res.data.codigo |
157 | }); | 158 | }); |
158 | } | 159 | } |
159 | 160 | ||
160 | focaAbmVehiculoService | 161 | focaAbmVehiculoService |
161 | .getCisternas($routeParams.idVehiculo) | 162 | .getCisternas($routeParams.idVehiculo) |
162 | .then(function (res) { | 163 | .then(function (res) { |
163 | $scope.cisternas = res; | 164 | $scope.cisternas = res; |
165 | $scope.cisternasInicial = angular.copy($scope.cisternas); | ||
164 | $scope.$apply(); | 166 | $scope.$apply(); |
165 | }); | 167 | }); |
166 | } | 168 | } |
167 | }); | 169 | }); |
168 | 170 | ||
169 | $scope.next = function (key) { | 171 | $scope.next = function (key) { |
170 | if (key === 13) $scope.focused++; | 172 | if (key === 13) $scope.focused++; |
171 | }; | 173 | }; |
172 | 174 | ||
173 | $scope.salir = function () { | 175 | $scope.salir = function () { |
174 | var cisternaEdit = []; | 176 | if (!$scope.formVehiculo.$pristine || !angular.equals($scope.cisternasInicial, $scope.cisternas)) { |
175 | $scope.cisternas.forEach( function (cisterna) { | ||
176 | if (cisterna.editando === true) { | ||
177 | cisternaEdit.push(cisterna); | ||
178 | } | ||
179 | }); | ||
180 | if (!$scope.formVehiculo.$pristine || cisternaEdit.length > 0) { | ||
181 | focaModalService.confirm('¿Está seguro de que desea salir? Se perderán todos los datos cargados.').then(function (data) { | 177 | focaModalService.confirm('¿Está seguro de que desea salir? Se perderán todos los datos cargados.').then(function (data) { |
182 | if (data) { | 178 | if (data) { |
183 | $location.path('/vehiculo'); | 179 | $location.path('/vehiculo'); |
184 | } | 180 | } |
185 | }); | 181 | }); |
186 | } else { | 182 | } else { |
187 | $location.path('/vehiculo'); | 183 | $location.path('/vehiculo'); |
188 | } | 184 | } |
189 | }; | 185 | }; |
190 | 186 | ||
191 | $scope.editar = function (cisterna) { | 187 | $scope.editar = function (cisterna) { |
192 | $scope.cisternas.forEach(function (cisterna) { | 188 | $scope.cisternas.forEach(function (cisterna) { |
193 | cisterna.editando = false; | 189 | cisterna.editando = false; |
194 | $scope.crear = true; | 190 | $scope.crear = true; |
195 | }); | 191 | }); |
196 | cisterna.editando = true; | 192 | cisterna.editando = true; |
197 | $scope.inicial = angular.copy(cisterna); | 193 | $scope.inicial = angular.copy(cisterna); |
198 | }; | 194 | }; |
199 | 195 | ||
200 | $scope.volver = function (cisterna, key) { | 196 | $scope.volver = function (cisterna, key) { |
201 | if (cisterna.id === undefined || !$scope.crear) { | 197 | if (cisterna.id === undefined || !$scope.crear) { |
202 | $scope.cisternas.shift(cisterna); | 198 | $scope.cisternas.shift(cisterna); |
203 | $scope.crear = false; | 199 | $scope.crear = false; |
204 | return; | 200 | return; |
205 | } | 201 | } |
206 | if (cisterna.id !== undefined) { | 202 | if (cisterna.id !== undefined) { |
207 | $scope.cisternas[key] = $scope.inicial; | 203 | $scope.cisternas[key] = $scope.inicial; |
208 | $scope.cisternas[key].editando = false; | 204 | $scope.cisternas[key].editando = false; |
209 | } | 205 | } |
210 | $scope.crear = false; | 206 | $scope.crear = false; |
211 | }; | 207 | }; |
212 | 208 | ||
213 | $scope.crearCisterna = function () { | 209 | $scope.crearCisterna = function () { |
214 | var cisterna = { | 210 | var cisterna = { |
215 | codigo: '', | 211 | codigo: '', |
216 | capacidad: '', | 212 | capacidad: '', |
217 | idUnidadMedida: 0, | 213 | idUnidadMedida: 0, |
218 | unidadMedida: {}, | 214 | unidadMedida: {}, |
219 | editando: true, | 215 | editando: true, |
220 | }; | 216 | }; |
221 | $scope.cisternas.unshift(cisterna); | 217 | $scope.cisternas.unshift(cisterna); |
222 | $scope.crear = true; | 218 | $scope.crear = true; |
223 | }; | 219 | }; |
224 | 220 | ||
225 | $scope.seleccionarUnidadMedida = function (key,cisterna) { | 221 | $scope.seleccionarUnidadMedida = function (key,cisterna) { |
226 | 222 | ||
227 | if (key === 13) { | 223 | if (key === 13) { |
228 | var modalInstance = $uibModal.open( | 224 | var modalInstance = $uibModal.open( |
229 | { | 225 | { |
230 | ariaLabelledBy: 'Busqueda de Unidades de medida', | 226 | ariaLabelledBy: 'Busqueda de Unidades de medida', |
231 | templateUrl: 'modal-unidad-medida.html', | 227 | templateUrl: 'modal-unidad-medida.html', |
232 | controller: 'focaModalUnidadMedidaCtrl', | 228 | controller: 'focaModalUnidadMedidaCtrl', |
233 | size: 'lg' | 229 | size: 'lg' |
234 | } | 230 | } |
235 | ); | 231 | ); |
236 | modalInstance.result.then(function (unidaMedida) { | 232 | modalInstance.result.then(function (unidaMedida) { |
237 | cisterna.idUnidadMedida = unidaMedida.ID; | 233 | cisterna.idUnidadMedida = unidaMedida.ID; |
238 | cisterna.unidadMedida = unidaMedida; | 234 | cisterna.unidadMedida = unidaMedida; |
239 | }); | 235 | }); |
240 | } | 236 | } |
241 | }; | 237 | }; |
242 | 238 | ||
243 | $scope.guardar = function (key) { | 239 | $scope.guardar = function (key) { |
244 | key = (typeof key === 'undefined') ? 13 : key; | 240 | key = (typeof key === 'undefined') ? 13 : key; |
245 | 241 | ||
246 | if (key === 13) { | 242 | if (key === 13) { |
247 | 243 | ||
248 | if ($scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) { | 244 | if ($scope.formVehiculo.$pristine && focaAbmVehiculoService.cisternasPristine) { |
249 | $scope.salir(); | 245 | $scope.salir(); |
250 | return; | 246 | return; |
251 | } | 247 | } |
252 | |||
253 | for (var i = 0; i < $scope.cisternas.length; i++) { | 248 | for (var i = 0; i < $scope.cisternas.length; i++) { |
254 | if (!validarCisterna($scope.cisternas[i])){ | 249 | if (!validarCisterna($scope.cisternas[i])){ |
255 | return; | 250 | return; |
256 | } | 251 | } |
257 | if ($scope.cisternas[i].id === 0) $scope.cisternas[i].id = undefined; | 252 | if ($scope.cisternas[i].id === 0) $scope.cisternas[i].id = undefined; |
258 | } | 253 | } |
259 | 254 | ||
260 | if (!$scope.vehiculo.codigo) { | 255 | if (!$scope.vehiculo.codigo) { |
261 | focaModalService.alert('Ingrese unidad'); | 256 | focaModalService.alert('Ingrese unidad'); |
262 | return; | 257 | return; |
263 | } else if (!$scope.vehiculo.tractor) { | 258 | } else if (!$scope.vehiculo.tractor) { |
264 | focaModalService.alert('Ingrese dominio tractor'); | 259 | focaModalService.alert('Ingrese dominio tractor'); |
265 | return; | 260 | return; |
266 | } else if (!$scope.vehiculo.semi) { | 261 | } else if (!$scope.vehiculo.semi) { |
267 | focaModalService.alert('Ingrese dominio semi'); | 262 | focaModalService.alert('Ingrese dominio semi'); |
268 | return; | 263 | return; |
269 | } | 264 | } |
270 | 265 | ||
271 | if ($scope.nuevo) { | 266 | if ($scope.nuevo) { |
272 | validaCodigoUnidad().then(function () { | 267 | validaCodigoUnidad().then(function () { |
273 | delete $scope.vehiculo.transportista; | 268 | delete $scope.vehiculo.transportista; |
274 | focaAbmVehiculoService.guardarVehiculo($scope.vehiculo) | 269 | focaAbmVehiculoService.guardarVehiculo($scope.vehiculo) |
275 | .then(function (res) { | 270 | .then(function (res) { |
276 | $routeParams.idVehiculo = res.data.id; | 271 | $routeParams.idVehiculo = res.data.id; |
277 | guardarCisternas($scope.cisternas).then(function () { | 272 | guardarCisternas($scope.cisternas).then(function () { |
278 | angular.element('#guardar').addClass('guardado'); | 273 | angular.element('#guardar').addClass('guardado'); |
279 | $timeout(function () {$location.path('/vehiculo');},300); | 274 | $timeout(function () {$location.path('/vehiculo');},300); |
280 | }); | 275 | }); |
281 | }); | 276 | }); |
282 | }, function () { | 277 | }, function () { |
283 | focaModalService.alert('Código de unidad existente'); | 278 | focaModalService.alert('Código de unidad existente'); |
284 | }); | 279 | }); |
285 | } else { | 280 | } else { |
286 | delete $scope.vehiculo.cisternas; | 281 | delete $scope.vehiculo.cisternas; |
287 | delete $scope.vehiculo.transportista; | 282 | delete $scope.vehiculo.transportista; |
288 | focaAbmVehiculoService.guardarVehiculo($scope.vehiculo); | 283 | focaAbmVehiculoService.guardarVehiculo($scope.vehiculo); |
289 | guardarCisternas($scope.cisternas).then(function () { | 284 | guardarCisternas($scope.cisternas).then(function () { |
290 | angular.element('#guardar').addClass('guardado'); | 285 | angular.element('#guardar').addClass('guardado'); |
291 | $timeout(function () {$location.path('/vehiculo');},500); | 286 | $timeout(function () {$location.path('/vehiculo');},500); |
292 | }); | 287 | }); |
293 | } | 288 | } |
294 | } | 289 | } |
295 | }; | 290 | }; |
296 | //Agregar propiedades de cisterna | 291 | //Agregar propiedades de cisterna |
297 | $scope.$watch('vehiculo', function (newValue) { | 292 | $scope.$watch('vehiculo', function (newValue) { |
298 | focaBotoneraLateralService.setPausarData({ | 293 | focaBotoneraLateralService.setPausarData({ |
299 | label: 'vehiculo', | 294 | label: 'vehiculo', |
300 | val: { | 295 | val: { |
301 | codigo: newValue.codigo, | 296 | codigo: newValue.codigo, |
302 | tractor: newValue.tractor, | 297 | tractor: newValue.tractor, |
303 | semi: newValue.semi, | 298 | semi: newValue.semi, |
304 | capacidad: newValue.capacidad, | 299 | capacidad: newValue.capacidad, |
305 | idVehiculo: newValue.idVehiculo, | 300 | idVehiculo: newValue.idVehiculo, |
306 | idTransportista: newValue.idTransportista | 301 | idTransportista: newValue.idTransportista |
307 | } | 302 | } |
308 | }); | 303 | }); |
309 | }, true); | 304 | }, true); |
310 | 305 | ||
311 | function getLSVehiculo() { | 306 | function getLSVehiculo() { |
312 | var vehiculo = JSON.parse($localStorage.vehiculo || null); | 307 | var vehiculo = JSON.parse($localStorage.vehiculo || null); |
313 | if (vehiculo) { | 308 | if (vehiculo) { |
314 | setearVehiculo(vehiculo); | 309 | setearVehiculo(vehiculo); |
315 | delete $localStorage.vehiculo; | 310 | delete $localStorage.vehiculo; |
316 | return true; | 311 | return true; |
317 | } | 312 | } |
318 | return false; | 313 | return false; |
319 | } | 314 | } |
320 | 315 | ||
321 | function setearVehiculo(vehiculo) { | 316 | function setearVehiculo(vehiculo) { |
322 | $scope.vehiculo = vehiculo; | 317 | $scope.vehiculo = vehiculo; |
323 | $scope.$broadcast('addCabecera', { | 318 | $scope.$broadcast('addCabecera', { |
324 | label: 'Vehiculo:', | 319 | label: 'Vehiculo:', |
325 | valor: $filter('rellenarDigitos')(vehiculo.codigo) + ' - ' | 320 | valor: $filter('rellenarDigitos')(vehiculo.codigo) + ' - ' |
326 | }); | 321 | }); |
327 | } | 322 | } |
328 | 323 | ||
329 | $scope.solicitarConfirmacionTabla = function (cisterna) { | 324 | $scope.solicitarConfirmacionTabla = function (cisterna) { |
330 | focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + cisterna.id + ' ' + cisterna.codigo + ' ?') | 325 | focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + cisterna.id + ' ' + cisterna.codigo + ' ?') |
331 | .then(function (data) { | 326 | .then(function (data) { |
332 | if (data) { | 327 | if (data) { |
333 | cisterna.desactivado = true; | 328 | cisterna.desactivado = true; |
334 | validaTotalCargas(); | 329 | validaTotalCargas(); |
335 | } | 330 | } |
336 | }); | 331 | }); |
337 | return; | 332 | return; |
338 | }; | 333 | }; |
339 | 334 | ||
340 | function validaCodigoUnidad() { | 335 | function validaCodigoUnidad() { |
341 | return new Promise(function (resolve, reject) { | 336 | return new Promise(function (resolve, reject) { |
342 | focaAbmVehiculoService | 337 | focaAbmVehiculoService |
343 | .getVehiculosPorTransportista(parseInt($routeParams.idTransportista)) | 338 | .getVehiculosPorTransportista(parseInt($routeParams.idTransportista)) |
344 | .then(function (res) { | 339 | .then(function (res) { |
345 | //Valida si existe numero de unidad | 340 | //Valida si existe numero de unidad |
346 | var existe = res.data.filter(function (vehiculo) { | 341 | var existe = res.data.filter(function (vehiculo) { |
347 | return vehiculo.codigo === $scope.vehiculo.codigo && | 342 | return vehiculo.codigo === $scope.vehiculo.codigo && |
348 | vehiculo.id !== $scope.vehiculo.id; | 343 | vehiculo.id !== $scope.vehiculo.id; |
349 | }); | 344 | }); |
350 | 345 | ||
351 | if (existe.length) { | 346 | if (existe.length) { |
352 | reject(existe); | 347 | reject(existe); |
353 | } else { | 348 | } else { |
354 | resolve(); | 349 | resolve(); |
355 | } | 350 | } |
356 | }); | 351 | }); |
357 | }); | 352 | }); |
358 | } | 353 | } |
359 | |||
360 | function validaTotalCargas() { | 354 | function validaTotalCargas() { |
361 | var total = 0; | 355 | var total = 0; |
362 | $scope.cisternas.forEach(function (cisterna) { | 356 | $scope.cisternas.forEach(function (cisterna) { |
363 | if (!cisterna.desactivado) { | 357 | if (!cisterna.desactivado) { |
364 | total += parseInt(cisterna.capacidad); | 358 | total += parseInt(cisterna.capacidad); |
365 | } else { | 359 | } else { |
366 | $scope.vehiculo.capacidad -= parseInt(cisterna.capacidad); | 360 | $scope.vehiculo.capacidad -= parseInt(cisterna.capacidad); |
367 | } | 361 | } |
368 | }); | 362 | }); |
369 | return $scope.vehiculo.capacidad = total; | 363 | return $scope.vehiculo.capacidad = total; |
370 | } | 364 | } |
371 | function guardarCisternas() { | 365 | function guardarCisternas() { |
372 | 366 | ||
373 | var cisternas = $scope.cisternas.map(function (cisterna) { | 367 | var cisternas = $scope.cisternas.map(function (cisterna) { |
374 | return { | 368 | return { |
375 | id: cisterna.id, | 369 | id: cisterna.id, |
376 | capacidad: parseFloat(cisterna.capacidad), | 370 | capacidad: parseFloat(cisterna.capacidad), |
377 | codigo: cisterna.codigo, | 371 | codigo: cisterna.codigo, |
378 | idUnidadMedida: cisterna.idUnidadMedida, | 372 | idUnidadMedida: cisterna.idUnidadMedida, |
379 | idVehiculo: $routeParams.idVehiculo, | 373 | idVehiculo: $routeParams.idVehiculo, |
380 | desactivado: cisterna.desactivado | 374 | desactivado: cisterna.desactivado |
381 | }; | 375 | }; |
382 | }); | 376 | }); |
383 | 377 | ||
384 | return focaAbmVehiculoService.guardarCisternas(cisternas); | 378 | return focaAbmVehiculoService.guardarCisternas(cisternas); |
385 | } | 379 | } |
386 | 380 | ||
387 | function validarCisterna(cisterna) { | 381 | function validarCisterna(cisterna) { |
388 | if (!cisterna) { | 382 | if (!cisterna) { |
389 | focaModalService.alert('Ingrese valores'); | 383 | focaModalService.alert('Ingrese valores'); |
390 | return false; | 384 | return false; |
391 | } else if (!cisterna.codigo) { | 385 | } else if (!cisterna.codigo) { |
392 | focaModalService.alert('Ingrese codigo de cisterna'); | 386 | focaModalService.alert('Ingrese codigo de cisterna'); |
393 | return false; | 387 | return false; |
394 | } else if (!cisterna.capacidad) { | 388 | } else if (!cisterna.capacidad) { |
395 | focaModalService.alert('Ingrese capacidad'); | 389 | focaModalService.alert('Ingrese capacidad'); |
396 | return false; | 390 | return false; |
397 | } else if (!cisterna.idUnidadMedida) { | 391 | } else if (!cisterna.idUnidadMedida) { |
398 | focaModalService.alert('Ingrese unidad de medida'); | 392 | focaModalService.alert('Ingrese unidad de medida'); |
399 | return false; | 393 | return false; |
400 | } else if (cisterna.id === undefined) { | 394 | } else if (cisterna.id === undefined) { |
401 | cisterna.id = 0; | 395 | cisterna.id = 0; |
402 | } | 396 | } |
403 | return true; | 397 | return true; |
404 | } | 398 | } |
405 | 399 | ||
406 | $scope.agregarCisterna = function (cisterna) { | 400 | $scope.agregarCisterna = function (cisterna) { |
407 | if(!validarCisterna(cisterna)) { | 401 | if(!validarCisterna(cisterna)) { |
408 | return; | 402 | return; |
409 | } | 403 | } |
410 | validaTotalCargas(); | 404 | validaTotalCargas(); |
411 | validaCodigo(cisterna); | 405 | validaCodigo(cisterna); |
412 | }; | 406 | }; |
413 | 407 | ||
414 | if ($routeParams.idx !== -1) { | 408 | if ($routeParams.idx !== -1) { |
415 | $scope.cisterna = [$routeParams.idx]; | 409 | $scope.cisterna = [$routeParams.idx]; |
416 | focaAbmVehiculoService | 410 | focaAbmVehiculoService |
417 | .getCisternas($routeParams.idVehiculo) | 411 | .getCisternas($routeParams.idVehiculo) |
418 | .then(function (res) { | 412 | .then(function (res) { |
419 | $scope.cisterna = res[$routeParams.idx]; | 413 | $scope.cisterna = res[$routeParams.idx]; |
420 | }); | 414 | }); |
421 | } | 415 | } |
422 | 416 | ||
423 | function validaCodigo(cisterna) { | 417 | function validaCodigo(cisterna) { |
424 | focaAbmVehiculoService | 418 | focaAbmVehiculoService |
425 | .getCisternas($routeParams.idVehiculo) | 419 | .getCisternas($routeParams.idVehiculo) |
426 | .then(function (res) { | 420 | .then(function (res) { |
427 | var cisternas = res; | 421 | var cisternas = res; |
428 | var totalCargado = 0; | 422 | var totalCargado = 0; |
429 | 423 | ||
430 | for (var i = 0; i < cisternas.length; i++) { | 424 | for (var i = 0; i < cisternas.length; i++) { |
431 | //SI EL CODIGO YA EXISTE | 425 | //SI EL CODIGO YA EXISTE |
432 | if (cisternas[i].codigo === cisterna.codigo && | 426 | if (cisternas[i].codigo === cisterna.codigo && |
433 | i !== $routeParams.idx && | 427 | i !== $routeParams.idx && |
434 | cisternas[i].desactivado === false && | 428 | cisternas[i].desactivado === false && |
435 | cisternas[i].id !== cisterna.id | 429 | cisternas[i].id !== cisterna.id |
436 | ) { | 430 | ) { |
437 | focaModalService.alert('Código de cisterna existente'); | 431 | focaModalService.alert('Código de cisterna existente'); |
438 | cisterna.editando = true; | 432 | cisterna.editando = true; |
439 | return; | 433 | return; |
440 | } | 434 | } |
441 | if (i !== $routeParams.idx && | 435 | if (i !== $routeParams.idx && |
442 | !cisternas[i].desactivado) { | 436 | !cisternas[i].desactivado) { |
443 | totalCargado += cisternas[i].capacidad; | 437 | totalCargado += cisternas[i].capacidad; |
444 | } | 438 | } |
445 | } | 439 | } |
446 | $timeout(function () { | 440 | $timeout(function () { |
447 | cisterna.editando = false; | 441 | cisterna.editando = false; |
448 | $scope.crear = false; | 442 | $scope.crear = false; |
449 | }); | 443 | }); |
450 | }); | 444 | }); |
451 | } | 445 | } |
452 | } | 446 | } |