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