Commit 6a96ee6ad345cdcd131ecf46fbad7473f3514e80

Authored by Jose Pinto
1 parent 5cd82a974e
Exists in master

espacio

Showing 1 changed file with 1 additions and 1 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', 4 '$uibModal', 'focaBotoneraLateralService', '$timeout',
5 function($scope, focaAbmVehiculoService, $location, focaModalService, 5 function($scope, focaAbmVehiculoService, $location, focaModalService,
6 $uibModal, focaBotoneraLateralService, $timeout) { 6 $uibModal, focaBotoneraLateralService, $timeout) {
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 //SETEO BOTONERA LATERAL 14 //SETEO BOTONERA LATERAL
15 focaBotoneraLateralService.showSalir(true); 15 focaBotoneraLateralService.showSalir(true);
16 focaBotoneraLateralService.showPausar(false); 16 focaBotoneraLateralService.showPausar(false);
17 focaBotoneraLateralService.showCancelar(false); 17 focaBotoneraLateralService.showCancelar(false);
18 focaBotoneraLateralService.showGuardar(false); 18 focaBotoneraLateralService.showGuardar(false);
19 19
20 if(focaAbmVehiculoService.transportistaSeleccionado.COD) { 20 if(focaAbmVehiculoService.transportistaSeleccionado.COD) {
21 elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado); 21 elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado);
22 } 22 }
23 focaAbmVehiculoService.cleanCisternasLocal(); 23 focaAbmVehiculoService.cleanCisternasLocal();
24 $scope.editar = function(id) { 24 $scope.editar = function(id) {
25 $location.path('/vehiculo/' + id + '/' + $scope.idTransportista); 25 $location.path('/vehiculo/' + id + '/' + $scope.idTransportista);
26 }; 26 };
27 $scope.solicitarConfirmacion = function(vehiculo) { 27 $scope.solicitarConfirmacion = function(vehiculo) {
28 focaModalService.confirm('¿Está seguro que desea borrar el vehiculo ' + 28 focaModalService.confirm('¿Está seguro que desea borrar el vehiculo ' +
29 vehiculo.id + ' ' + vehiculo.tractor + ' ?').then( 29 vehiculo.id + ' ' + vehiculo.tractor + ' ?').then(
30 function(data) { 30 function(data) {
31 if(data) { 31 if(data) {
32 focaAbmVehiculoService.deleteVehiculo(vehiculo.id); 32 focaAbmVehiculoService.deleteVehiculo(vehiculo.id);
33 $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 1); 33 $scope.vehiculos.splice($scope.vehiculos.indexOf(vehiculo), 1);
34 } 34 }
35 } 35 }
36 ); 36 );
37 }; 37 };
38 $scope.seleccionarTransportista = function() { 38 $scope.seleccionarTransportista = function() {
39 var modalInstance = $uibModal.open( 39 var modalInstance = $uibModal.open(
40 { 40 {
41 ariaLabelledBy: 'Busqueda de Transportista', 41 ariaLabelledBy: 'Busqueda de Transportista',
42 templateUrl: 'modal-proveedor.html', 42 templateUrl: 'modal-proveedor.html',
43 controller: 'focaModalProveedorCtrl', 43 controller: 'focaModalProveedorCtrl',
44 size: 'lg', 44 size: 'lg',
45 resolve: { 45 resolve: {
46 transportista: function() { 46 transportista: function() {
47 return true; 47 return true;
48 } 48 }
49 } 49 }
50 } 50 }
51 ); 51 );
52 modalInstance.result.then( 52 modalInstance.result.then(
53 function(transportista) { 53 function(transportista) {
54 elegirTransportista(transportista); 54 elegirTransportista(transportista);
55 focaAbmVehiculoService.transportistaSeleccionado = transportista; 55 focaAbmVehiculoService.transportistaSeleccionado = transportista;
56 }, function() { 56 }, function() {
57 57
58 } 58 }
59 ); 59 );
60 }; 60 };
61 61
62 function elegirTransportista(transportista) { 62 function elegirTransportista(transportista) {
63 var codigo = ('00000' + transportista.COD).slice(-5); 63 var codigo = ('00000' + transportista.COD).slice(-5);
64 $scope.idTransportista = transportista.COD; 64 $scope.idTransportista = transportista.COD;
65 $scope.filtros = transportista.NOM.trim(); 65 $scope.filtros = transportista.NOM.trim();
66 $timeout(function() { 66 $timeout(function() {
67 $scope.$broadcast('addCabecera', { 67 $scope.$broadcast('addCabecera', {
68 label: 'Transportista:', 68 label: 'Transportista:',
69 valor: codigo + ' - ' + transportista.NOM 69 valor: codigo + ' - ' + transportista.NOM
70 }); 70 });
71 }); 71 });
72 buscar(transportista.COD); 72 buscar(transportista.COD);
73 } 73 }
74 74
75 function buscar(idTransportista) { 75 function buscar(idTransportista) {
76 focaAbmVehiculoService 76 focaAbmVehiculoService
77 .getVehiculosPorTransportista(idTransportista) 77 .getVehiculosPorTransportista(idTransportista)
78 .then(function(datos) { 78 .then(function(datos) {
79 $scope.vehiculos = datos.data; 79 $scope.vehiculos = datos.data;
80 }); 80 });
81 } 81 }
82 } 82 }
83 ]) 83 ])
84 .controller('focaAbmVehiculoController', [ 84 .controller('focaAbmVehiculoController', [
85 '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal', 85 '$scope', 'focaAbmVehiculoService', '$routeParams', '$location', '$uibModal',
86 'focaModalService', '$timeout', 'focaBotoneraLateralService', '$window', 86 'focaModalService', '$timeout', 'focaBotoneraLateralService', '$window',
87 function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal, 87 function($scope, focaAbmVehiculoService, $routeParams, $location, $uibModal,
88 focaModalService, $timeout, focaBotoneraLateralService, $window) { 88 focaModalService, $timeout, focaBotoneraLateralService, $window) {
89 $scope.nuevo = $routeParams.idVehiculo === '0' ? true : false; 89 $scope.nuevo = $routeParams.idVehiculo === '0' ? true : false;
90 $scope.now = new Date(); 90 $scope.now = new Date();
91 $scope.focused = 1; 91 $scope.focused = 1;
92 $scope.transportistaStamp = ''; 92 $scope.transportistaStamp = '';
93 $scope.cisternas = []; 93 $scope.cisternas = [];
94 94
95 $timeout(function() { 95 $timeout(function() {
96 focaBotoneraLateralService.showSalir(true); 96 focaBotoneraLateralService.showSalir(true);
97 focaBotoneraLateralService.showPausar(false); 97 focaBotoneraLateralService.showPausar(false);
98 focaBotoneraLateralService.showCancelar(); 98 focaBotoneraLateralService.showCancelar();
99 focaBotoneraLateralService.showGuardar(true, $scope.guardar); 99 focaBotoneraLateralService.showGuardar(true, $scope.guardar);
100 }); 100 });
101 101
102 if($scope.nuevo) { 102 if($scope.nuevo) {
103 focaAbmVehiculoService 103 focaAbmVehiculoService
104 .getTransportistaPorId($routeParams.idTransportista) 104 .getTransportistaPorId($routeParams.idTransportista)
105 .then(function(res) { 105 .then(function(res) {
106 var codigo = ('00000' + res.data.COD).slice(-5); 106 var codigo = ('00000' + res.data.COD).slice(-5);
107 $scope.vehiculo.idTransportista = res.data.COD; 107 $scope.vehiculo.idTransportista = res.data.COD;
108 $scope.vehiculo.transportista = res.data; 108 $scope.vehiculo.transportista = res.data;
109 $scope.$broadcast('addCabecera', { 109 $scope.$broadcast('addCabecera', {
110 label: 'Transportista:', 110 label: 'Transportista:',
111 valor: codigo + ' - ' + res.data.NOM 111 valor: codigo + ' - ' + res.data.NOM
112 }); 112 });
113 }); 113 });
114 } 114 }
115 $scope.vehiculo = {}; 115 $scope.vehiculo = {};
116 focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) { 116 focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) {
117 if(res.data) { 117 if(res.data) {
118 $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5); 118 $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5);
119 $scope.transportistaStamp += ' - ' + res.data.transportista.NOM 119 $scope.transportistaStamp += ' - ' + res.data.transportista.NOM
120 120
121 $scope.vehiculo = res.data; 121 $scope.vehiculo = res.data;
122 $scope.$broadcast('addCabecera', { 122 $scope.$broadcast('addCabecera', {
123 label: 'Transportista:', 123 label: 'Transportista:',
124 valor: $scope.transportistaStamp 124 valor: $scope.transportistaStamp
125 }); 125 });
126 $scope.$broadcast('addCabecera', { 126 $scope.$broadcast('addCabecera', {
127 label: 'Unidad:', 127 label: 'Unidad:',
128 valor: res.data.codigo 128 valor: res.data.codigo
129 }); 129 });
130 } 130 }
131 }); 131 });
132 focaAbmVehiculoService.getCisternadoPorVehiculo($routeParams.idVehiculo) 132 focaAbmVehiculoService.getCisternadoPorVehiculo($routeParams.idVehiculo)
133 .then(function(res) { 133 .then(function(res) {
134 //CONCATENA LAS CISTERNAS DEL SERVICIO Y LAS DE LA BASE DE DATOS 134 //CONCATENA LAS CISTERNAS DEL SERVICIO Y LAS DE LA BASE DE DATOS
135 $scope.cisternas = focaAbmVehiculoService.getCisternasLocal().concat(res.data); 135 $scope.cisternas = focaAbmVehiculoService.getCisternasLocal().concat(res.data);
136 }); 136 });
137 $scope.next = function(key) { 137 $scope.next = function(key) {
138 if (key === 13) $scope.focused++; 138 if (key === 13) $scope.focused++;
139 }; 139 };
140 $scope.cancelar = function() { 140 $scope.cancelar = function() {
141 $location.path('/vehiculo'); 141 $location.path('/vehiculo');
142 }; 142 };
143 $scope.editar = function(cisterna) { 143 $scope.editar = function(cisterna) {
144 if(cisterna) { 144 if(cisterna) {
145 if(cisterna.idTemp != undefined) { 145 if(cisterna.idTemp != undefined) {
146 $location.path('/vehiculo/' + $routeParams.idVehiculo + 146 $location.path('/vehiculo/' + $routeParams.idVehiculo +
147 '/cisterna/0/' + cisterna.idTemp); 147 '/cisterna/0/' + cisterna.idTemp);
148 }else { 148 }else {
149 $location.path('/vehiculo/' + $routeParams.idVehiculo + 149 $location.path('/vehiculo/' + $routeParams.idVehiculo +
150 '/cisterna/' + cisterna.id); 150 '/cisterna/' + cisterna.id);
151 } 151 }
152 }else { 152 }else {
153 $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/0/'); 153 $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/0/');
154 } 154 }
155 }; 155 };
156 $scope.guardar = function() { 156 $scope.guardar = function() {
157 //Valida si existe numero de unidad 157 //Valida si existe numero de unidad
158 if(!validaTotalCargas()) { 158 if(!validaTotalCargas()) {
159 focaModalService.alert('La suma de las capacidades de las cisternas' + 159 focaModalService.alert('La suma de las capacidades de las cisternas' +
160 ' debe ser igual a la capacidad total del vehículo'); 160 ' debe ser igual a la capacidad total del vehículo');
161 return; 161 return;
162 } 162 }
163 validaCodigoUnidad().then(function() { 163 validaCodigoUnidad().then(function() {
164 delete $scope.vehiculo.transportista; 164 delete $scope.vehiculo.transportista;
165 delete $scope.vehiculo.cisternas; 165 delete $scope.vehiculo.cisternas;
166 focaAbmVehiculoService.guardarVehiculo($scope.vehiculo) 166 focaAbmVehiculoService.guardarVehiculo($scope.vehiculo)
167 .then(function(res) { 167 .then(function(res) {
168 if($scope.nuevo) { 168 if($scope.nuevo) {
169 $location.path('/vehiculo/' + res.data.id + 169 $location.path('/vehiculo/' + res.data.id +
170 '/' + res.data.idTransportista); 170 '/' + res.data.idTransportista);
171 }else { 171 }else {
172 guardarCisternas().then(function() { 172 guardarCisternas().then(function() {
173 $window.location.assign('/#!/vehiculo'); 173 $window.location.assign('/#!/vehiculo');
174 }); 174 });
175 } 175 }
176 }); 176 });
177 }, function(){ 177 }, function(){
178 focaModalService.alert('Código de unidad existente'); 178 focaModalService.alert('Código de unidad existente');
179 }); 179 });
180 180
181 }; 181 };
182 $scope.solicitarConfirmacionCisterna = function(cisterna) { 182 $scope.solicitarConfirmacionCisterna = function(cisterna) {
183 focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' + 183 focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' +
184 cisterna.id + ' ' + cisterna.codigo + ' ?').then( 184 cisterna.id + ' ' + cisterna.codigo + ' ?').then(
185 function(data) { 185 function(data) {
186 if(data) { 186 if(data) {
187 if(cisterna.id){ 187 if(cisterna.id) {
188 focaAbmVehiculoService.deleteCisterna(cisterna.id); 188 focaAbmVehiculoService.deleteCisterna(cisterna.id);
189 } 189 }
190 $scope.cisternas.splice($scope.cisternas.indexOf(cisterna), 1); 190 $scope.cisternas.splice($scope.cisternas.indexOf(cisterna), 1);
191 } 191 }
192 } 192 }
193 ); 193 );
194 }; 194 };
195 195
196 function validaCodigoUnidad() { 196 function validaCodigoUnidad() {
197 return new Promise(function(resolve, reject) { 197 return new Promise(function(resolve, reject) {
198 focaAbmVehiculoService 198 focaAbmVehiculoService
199 .getVehiculosPorTransportista(parseInt($routeParams.idTransportista)) 199 .getVehiculosPorTransportista(parseInt($routeParams.idTransportista))
200 .then(function(res) { 200 .then(function(res) {
201 //Valida si existe numero de unidad 201 //Valida si existe numero de unidad
202 var existe = res.data.filter(function(vehiculo) { 202 var existe = res.data.filter(function(vehiculo) {
203 return vehiculo.codigo === $scope.vehiculo.codigo && 203 return vehiculo.codigo === $scope.vehiculo.codigo &&
204 vehiculo.id !== $scope.vehiculo.id; 204 vehiculo.id !== $scope.vehiculo.id;
205 }); 205 });
206 206
207 if(existe.length) { 207 if(existe.length) {
208 reject(existe); 208 reject(existe);
209 }else { 209 }else {
210 resolve(); 210 resolve();
211 } 211 }
212 }); 212 });
213 }); 213 });
214 } 214 }
215 215
216 function validaTotalCargas() { 216 function validaTotalCargas() {
217 var total = 0; 217 var total = 0;
218 $scope.cisternas.forEach(function(cisterna) { 218 $scope.cisternas.forEach(function(cisterna) {
219 total += parseInt(cisterna.capacidad); 219 total += parseInt(cisterna.capacidad);
220 }); 220 });
221 return $scope.vehiculo.capacidad == total; 221 return $scope.vehiculo.capacidad == total;
222 } 222 }
223 223
224 function guardarCisternas() { 224 function guardarCisternas() {
225 var promesas = []; 225 var promesas = [];
226 226
227 focaAbmVehiculoService.getCisternasLocal().forEach(function(cisterna) { 227 focaAbmVehiculoService.getCisternasLocal().forEach(function(cisterna) {
228 delete cisterna.unidadMedida; 228 delete cisterna.unidadMedida;
229 delete cisterna.idTemp; 229 delete cisterna.idTemp;
230 promesas.push(focaAbmVehiculoService.guardarCisterna(cisterna)); 230 promesas.push(focaAbmVehiculoService.guardarCisterna(cisterna));
231 }); 231 });
232 return Promise.all(promesas); 232 return Promise.all(promesas);
233 } 233 }
234 } 234 }
235 ]); 235 ]);
236 236