Commit 0f4ce9054f72dd9060a7565780dd3acf94a6b72a

Authored by Jose Pinto
1 parent 4296241f88
Exists in master and in 1 other branch develop

refactor funcionamiento cisternas locales

src/js/controller.js
... ... @@ -21,7 +21,6 @@ angular.module('focaAbmVehiculo')
21 21 if(focaAbmVehiculoService.transportistaSeleccionado.COD) {
22 22 elegirTransportista(focaAbmVehiculoService.transportistaSeleccionado);
23 23 }
24   - focaAbmVehiculoService.cleanCisternasLocal();
25 24 $scope.editar = function(id) {
26 25 $location.path('/vehiculo/' + id + '/' + $scope.idTransportista);
27 26 };
... ... @@ -122,7 +121,7 @@ angular.module('focaAbmVehiculo')
122 121 focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) {
123 122 if(res.data) {
124 123 $scope.transportistaStamp = ('00000' + res.data.transportista.COD).slice(-5);
125   - $scope.transportistaStamp += ' - ' + res.data.transportista.NOM
  124 + $scope.transportistaStamp += ' - ' + res.data.transportista.NOM;
126 125  
127 126 $scope.vehiculo = res.data;
128 127 $scope.$broadcast('addCabecera', {
... ... @@ -133,35 +132,32 @@ angular.module('focaAbmVehiculo')
133 132 label: 'Unidad:',
134 133 valor: res.data.codigo
135 134 });
  135 + focaAbmVehiculoService
  136 + .getCisternas($routeParams.idVehiculo)
  137 + .then(function(res) {
  138 + $scope.cisternas = res;
  139 + $scope.$apply();
  140 + });
136 141 }
137 142 });
138   - focaAbmVehiculoService.getCisternadoPorVehiculo($routeParams.idVehiculo)
139   - .then(function(res) {
140   - //CONCATENA LAS CISTERNAS DEL SERVICIO Y LAS DE LA BASE DE DATOS
141   - $scope.cisternas = focaAbmVehiculoService.getCisternasLocal().concat(res.data);
142   - });
  143 +
143 144 $scope.next = function(key) {
144 145 if (key === 13) $scope.focused++;
145 146 };
146 147 $scope.cancelar = function() {
147 148 $location.path('/vehiculo');
148 149 };
149   - $scope.editar = function(cisterna) {
150   - if(cisterna) {
151   - if(cisterna.idTemp != undefined) {
152   - $location.path('/vehiculo/' + $routeParams.idVehiculo +
153   - '/cisterna/0/' + cisterna.idTemp);
154   - }else {
155   - $location.path('/vehiculo/' + $routeParams.idVehiculo +
156   - '/cisterna/' + cisterna.id);
157   - }
  150 + $scope.editar = function(key) {
  151 + if(key) {
  152 + $location.path('/vehiculo/' + $routeParams.idVehiculo +
  153 + '/cisterna/' + key);
158 154 }else {
159 155 $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/0/');
160 156 }
161 157 };
162 158 $scope.guardar = function(key) {
163 159 key = (typeof key === 'undefined') ? 13 : key;
164   - if(key === 13){
  160 + if(key === 13) {
165 161 //Valida si existe numero de unidad
166 162 if(!validaTotalCargas() && !$scope.nuevo) {
167 163 focaModalService.alert('La suma de las capacidades de las cisternas' +
... ... @@ -182,21 +178,23 @@ angular.module('focaAbmVehiculo')
182 178 });
183 179 }
184 180 });
185   - }, function(){
  181 + }, function() {
186 182 focaModalService.alert('Cรณdigo de unidad existente');
187 183 });
188 184 }
189 185  
190 186 };
191   - $scope.solicitarConfirmacionCisterna = function(cisterna) {
  187 + $scope.solicitarConfirmacionCisterna = function(cisterna, idx) {
192 188 focaModalService.confirm('ยฟEstรก seguro que desea borrar la cisterna ' +
193 189 cisterna.id + ' ' + cisterna.codigo + ' ?').then(
194 190 function(data) {
195 191 if(data) {
196   - if(cisterna.id) {
197   - focaAbmVehiculoService.deleteCisterna(cisterna.id);
198   - }
199   - $scope.cisternas.splice($scope.cisternas.indexOf(cisterna), 1);
  192 + focaAbmVehiculoService.deleteCisterna(idx);
  193 + focaAbmVehiculoService
  194 + .getCisternas($routeParams.idVehiculo)
  195 + .then(function(res) {
  196 + $scope.cisternas = res;
  197 + });
200 198 }
201 199 }
202 200 );
... ... @@ -225,21 +223,26 @@ angular.module('focaAbmVehiculo')
225 223 function validaTotalCargas() {
226 224 var total = 0;
227 225 $scope.cisternas.forEach(function(cisterna) {
228   - total += parseInt(cisterna.capacidad);
  226 + if(!cisterna.desactivado) {
  227 + total += parseInt(cisterna.capacidad);
  228 + }
229 229 });
230 230 return $scope.vehiculo.capacidad == total;
231 231 }
232 232  
233 233 function guardarCisternas() {
234   - var promesas = [];
235   -
236   - focaAbmVehiculoService.getCisternasLocal().forEach(function(cisterna) {
237   - delete cisterna.unidadMedida;
238   - delete cisterna.idTemp;
239   - delete cisterna.cisternasCarga;
240   - promesas.push(focaAbmVehiculoService.guardarCisterna(cisterna));
  234 + var cisternas = $scope.cisternas.map(function(cisterna) {
  235 + return {
  236 + id: cisterna.id,
  237 + capacidad: parseInt(cisterna.capacidad),
  238 + codigo: cisterna.codigo,
  239 + idUnidadMedida: cisterna.idUnidadMedida,
  240 + idVehiculo: $routeParams.idVehiculo,
  241 + desactivado: cisterna.desactivado
  242 + };
241 243 });
242   - return Promise.all(promesas);
  244 +
  245 + return focaAbmVehiculoService.guardarCisternas(cisternas);
243 246 }
244 247 }
245 248 ]);
src/js/controllerCisterna.js
... ... @@ -24,14 +24,15 @@ angular.module('focaAbmVehiculo')
24 24 focaBotoneraLateralService.showGuardar(true, $scope.guardar);
25 25 });
26 26  
27   -
28   - if($routeParams.idTemp != undefined) {
29   - $scope.cisterna = focaAbmVehiculoService.getCisternasLocal()[$routeParams.idTemp];
30   - }else {
31   - focaAbmVehiculoService.getCisterna($routeParams.id).then(function(res) {
32   - if(res.data) $scope.cisterna = res.data;
33   - });
  27 + if($routeParams.idx !== -1) {
  28 + $scope.cisterna = [$routeParams.idx]
  29 + focaAbmVehiculoService
  30 + .getCisternas($routeParams.idVehiculo)
  31 + .then(function(res) {
  32 + $scope.cisterna = res[$routeParams.idx];
  33 + });
34 34 }
  35 +
35 36 focaAbmVehiculoService.getVehiculo($routeParams.idVehiculo).then(function(res) {
36 37 var codigo = ('00000' + res.data.transportista.COD).slice(-5);
37 38 $scope.transportista = res.data.transportista.COD;
... ... @@ -57,27 +58,19 @@ angular.module('focaAbmVehiculo')
57 58 focaModalService.alert('Ingrese unidad de medida');
58 59 return;
59 60 }
60   - validaCodigoCapacidad().then(function() {
61   - $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo);
62   - delete $scope.cisterna.vehiculo;
63   -
64   - if($routeParams.idTemp != undefined) {
65   - //SI SE EDITA UNA CISTERNA LOCALMENTE
  61 + validaCodigoCapacidad()
  62 + .then(function() {
  63 + $scope.cisterna.idVehiculo = parseInt($routeParams.idVehiculo);
  64 + delete $scope.cisterna.vehiculo;
  65 +
66 66 focaAbmVehiculoService
67   - .guardarCisternaLocal($scope.cisterna, $routeParams.idTemp);
68   - }else if($scope.cisterna.id) {
69   - //SI SE EDITA UNA CISTERNA PREVIAMENTE GUARDADA
70   - focaAbmVehiculoService.deleteCisterna($scope.cisterna.id);
71   - focaAbmVehiculoService.guardarCisternaLocal($scope.cisterna);
72   - }else {
73   - //SI SE EDITA CREA UNA NUEVA CISTERNA
74   - focaAbmVehiculoService.guardarCisternaLocal($scope.cisterna);
75   - }
76   - $window.location.assign('/#!/vehiculo/' + $routeParams.idVehiculo +
77   - '/' + $scope.transportista);
78   - }, function(err) {
79   - focaModalService.alert(err);
80   - });
  67 + .guardarCisterna($scope.cisterna, $routeParams.idx);
  68 +
  69 + $window.location.assign('/#!/vehiculo/' + $routeParams.idVehiculo +
  70 + '/' + $scope.transportista);
  71 + }, function(err) {
  72 + focaModalService.alert(err);
  73 + });
81 74  
82 75 };
83 76  
... ... @@ -99,19 +92,20 @@ angular.module('focaAbmVehiculo')
99 92 function validaCodigoCapacidad() {
100 93 return new Promise(function(resolve, reject) {
101 94 focaAbmVehiculoService
102   - .getCisternadoPorVehiculo($routeParams.idVehiculo)
  95 + .getCisternas($routeParams.idVehiculo)
103 96 .then(function(res) {
104   - var cisternas = focaAbmVehiculoService.getCisternasLocal().concat(res.data);
  97 + var cisternas = res;
105 98 var totalCargado = 0;
106   -
107   - cisternas.forEach(function(cisterna) {
  99 + console.log(cisternas);
  100 + cisternas.forEach(function(cisterna, idx) {
108 101 //SI EL CODIGO YA EXISTE
109 102 if(cisterna.codigo === $scope.cisterna.codigo &&
110   - (cisterna.id !== $scope.cisterna.id ||
111   - cisterna.idTemp !== $scope.cisterna.idTemp)) {
  103 + idx != $routeParams.idx &&
  104 + !cisterna.desactivado) {
112 105 reject('Cรณdigo de cisterna existente');
113 106 }
114   - if(cisterna.id !== $scope.cisterna.id) {
  107 + if(idx != $routeParams.idx &&
  108 + !cisterna.desactivado) {
115 109 totalCargado += cisterna.capacidad;
116 110 }
117 111 });
... ... @@ -29,16 +29,7 @@ angular.module('focaAbmVehiculo')
29 29 .config([
30 30 '$routeProvider',
31 31 function($routeProvider) {
32   - $routeProvider.when('/vehiculo/:idVehiculo/cisterna/:id', {
33   - controller: 'focaAbmVehiculoCisternaController',
34   - templateUrl: 'src/views/foca-abm-cisterna-item.html'
35   - });
36   - }
37   - ])
38   - .config([
39   - '$routeProvider',
40   - function($routeProvider) {
41   - $routeProvider.when('/vehiculo/:idVehiculo/cisterna/:id/:idTemp', {
  32 + $routeProvider.when('/vehiculo/:idVehiculo/cisterna/:idx', {
42 33 controller: 'focaAbmVehiculoCisternaController',
43 34 templateUrl: 'src/views/foca-abm-cisterna-item.html'
44 35 });
... ... @@ -17,32 +17,33 @@ angular.module('focaAbmVehiculo')
17 17 deleteVehiculo: function(id) {
18 18 return $http.delete(API_ENDPOINT.URL + '/vehiculo/' + id);
19 19 },
20   - getCisternadoPorVehiculo: function(idVehiculo) {
21   - return $http.get(API_ENDPOINT.URL + '/cisterna/listar/' + idVehiculo);
22   - },
23   - getCisterna: function(id) {
24   - return $http.get(API_ENDPOINT.URL + '/cisterna/obtener/' + id);
25   - },
26   - getCisternasLocal: function() {
27   - return cisternas;
  20 + getCisternas: function(idVehiculo) {
  21 + if(cisternas.length) {
  22 + return Promise.resolve(angular.copy(cisternas));
  23 + }else {
  24 + return new Promise(function(resolve, reject) {
  25 + $http.get(API_ENDPOINT.URL + '/cisterna/listar/' + idVehiculo)
  26 + .then(function(res) {
  27 + cisternas = res.data;
  28 + resolve(res.data);
  29 + });
  30 + });
  31 + }
28 32 },
29   - guardarCisternaLocal: function(cisterna, idTemp) {
30   - if(idTemp) {
31   - cisterna.idTemp = idTemp;
32   - cisternas[idTemp] = cisterna;
  33 + guardarCisterna: function(cisterna, idx) {
  34 + if(idx != -1) {
  35 + //update
  36 + cisternas[idx] = cisterna;
33 37 }else {
34   - cisterna.idTemp = cisternas.length;
  38 + //insert
35 39 cisternas.push(cisterna);
36 40 }
37 41 },
38   - cleanCisternasLocal: function(){
39   - cisternas = [];
40   - },
41   - guardarCisterna: function(cisterna) {
42   - return $http.post(API_ENDPOINT.URL + '/cisterna/guardar', {cisterna: cisterna});
  42 + guardarCisternas: function(cisternas) {
  43 + return $http.post(API_ENDPOINT.URL + '/cisterna', {cisternas: cisternas});
43 44 },
44   - deleteCisterna: function(id) {
45   - return $http.delete(API_ENDPOINT.URL + '/cisterna/borrar/' + id);
  45 + deleteCisterna: function(idx) {
  46 + cisternas[idx].desactivado = true;
46 47 },
47 48 getVehiculosPorTransportista: function(id) {
48 49 return $http.get(API_ENDPOINT.URL + '/vehiculo/transportista/' + id);
src/views/foca-abm-vehiculos-item.html
... ... @@ -83,28 +83,29 @@
83 83 <button
84 84 class="btn btn-outline-debo boton-accion"
85 85 title="Agregar"
86   - ng-click="editar()">
  86 + ng-click="editar(-1)">
87 87 <i class="fa fa-plus"></i>
88 88 </button>
89 89 </th>
90 90 </tr>
91 91 </thead>
92 92 <tbody>
93   - <tr ng-repeat="cisterna in cisternas | filter:filtros">
  93 + <tr ng-repeat="(key, cisterna) in cisternas | filter:filtros"
  94 + ng-show="!cisterna.desactivado">
94 95 <td ng-bind="cisterna.codigo" class="px-5"></td>
95 96 <td ng-bind="cisterna.capacidad + ' ' + cisterna.unidadMedida.NOM" class="text-right px-5"></td>
96 97 <td class="text-center px-4">
97 98 <button
98 99 class="btn btn-outline-dark boton-accion"
99 100 title="Editar"
100   - ng-click="editar(cisterna)"
  101 + ng-click="editar(key)"
101 102 >
102 103 <i class="fa fa-pencil"></i>
103 104 </button>
104 105 <button
105 106 class="btn btn-outline-dark boton-accion"
106 107 title="Eliminar"
107   - ng-click="solicitarConfirmacionCisterna(cisterna)"
  108 + ng-click="solicitarConfirmacionCisterna(cisterna, key)"
108 109 >
109 110 <i class="fa fa-trash"></i>
110 111 </button>