Commit 10b0f94bf65c6b0a37f615d3735f8cc100804ce3

Authored by Jose Pinto
1 parent 4bcdeb329c
Exists in master

refactor guardar

src/js/controller.js
... ... @@ -136,33 +136,22 @@ angular.module('focaAbmVehiculo')
136 136 $location.path('/vehiculo/' + $routeParams.idVehiculo + '/cisterna/' + id);
137 137 };
138 138 $scope.guardar = function() {
139   - if(!$scope.vehiculo.transportista) {
140   - focaModalService.alert('Elija Transportista');
141   - return;
142   - }
143   - focaAbmVehiculoService.getVehiculosPorTransportista($routeParams.idTransportista)
144   - .then(function(res) {
145   - var existe = res.data.filter(function(vehiculo) {
146   - return vehiculo.codigo === $scope.vehiculo.codigo &&
147   - vehiculo.id !== $scope.vehiculo.id;
  139 + //Valida si existe numero de unidad
  140 + existeCodigoUnidad().then(function() {
  141 + delete $scope.vehiculo.transportista;
  142 + focaAbmVehiculoService.guardarVehiculo($scope.vehiculo)
  143 + .then(function(res){
  144 + if($scope.nuevo) {
  145 + $location.path('/vehiculo/' + res.data.id +
  146 + '/' + res.data.idTransportista);
  147 + } else {
  148 + $location.path('/vehiculo');
  149 + }
148 150 });
  151 + }, function(){
  152 + focaModalService.alert('Código de unidad existente');
  153 + });
149 154  
150   - if(existe.length > 0) {
151   - return Promise.reject('Existe codigo de unidad');
152   - }else {
153   - delete $scope.vehiculo.transportista;
154   - return focaAbmVehiculoService.guerdarVehiculo($scope.vehiculo);
155   - }
156   - }).then(function(res) {
157   - if($scope.nuevo) {
158   - $location.path('/vehiculo/' + res.data.id +
159   - '/' + res.data.idTransportista);
160   - }else {
161   - $location.path('/vehiculo');
162   - }
163   - }, function(rej) {
164   - focaModalService.alert(rej);
165   - });
166 155 };
167 156 $scope.solicitarConfirmacionCisterna = function(cisterna) {
168 157 focaModalService.confirm('¿Está seguro que desea borrar la cisterna ' +
... ... @@ -198,5 +187,25 @@ angular.module('focaAbmVehiculo')
198 187 }
199 188 );
200 189 };
  190 +
  191 + function existeCodigoUnidad(){
  192 + return new Promise(function(resolve, reject) {
  193 + focaAbmVehiculoService
  194 + .getVehiculosPorTransportista($routeParams.idTransportista)
  195 + .then(function(res){
  196 + //Valida si existe numero de unidad
  197 + var existe = res.data.filter(function(vehiculo) {
  198 + return vehiculo.codigo === $scope.vehiculo.codigo &&
  199 + vehiculo.id !== $scope.vehiculo.id;
  200 + });
  201 +
  202 + if(existe.length){
  203 + reject(existe);
  204 + } else {
  205 + resolve();
  206 + }
  207 + });
  208 + });
  209 + }
201 210 }
202 211 ]);
src/js/controllerCisterna.js
... ... @@ -56,24 +56,17 @@ angular.module('focaAbmVehiculo')
56 56 focaModalService.alert('Ingrese producto');
57 57 return;
58 58 }
59   - focaAbmVehiculoService.getCisternadoPorVehiculo($routeParams.idVehiculo)
60   - .then(function(res) {
61   - var existe = res.data.filter(function(cisterna) {
62   - return cisterna.codigo === $scope.cisterna.codigo;
63   - });
64   - if(existe.length > 0) {
65   - return Promise.reject('Existe codigo de cisterna');
66   - }else {
67   - $scope.cisterna.idVehiculo = $routeParams.idVehiculo;
68   - delete $scope.cisterna.unidadMedida;
69   - delete $scope.cisterna.cisternaCarga.articulo;
70   - return focaAbmVehiculoService.guardarCisterna($scope.cisterna);
71   - }
72   - }).then(function() {
  59 + existeCodigoCisterna().then(function() {
  60 + $scope.cisterna.idVehiculo = $routeParams.idVehiculo;
  61 + delete $scope.cisterna.unidadMedida;
  62 + delete $scope.cisterna.cisternaCarga.articulo;
  63 + focaAbmVehiculoService.guardarCisterna($scope.cisterna).then(function(){
73 64 $location.path('/vehiculo/' + $routeParams.idVehiculo);
74   - }, function(rej) {
75   - focaModalService.alert(rej);
76 65 });
  66 + }, function() {
  67 + focaModalService.alert('Código de cisterna existente');
  68 + });
  69 +
77 70 };
78 71  
79 72 // $scope.seleccionarArticulo = function() {
... ... @@ -116,5 +109,24 @@ angular.module('focaAbmVehiculo')
116 109 $scope.cisterna.unidadMedida = unidaMedida;
117 110 });
118 111 };
  112 +
  113 + function existeCodigoCisterna(){
  114 + return new Promise(function(resolve, reject) {
  115 + focaAbmVehiculoService
  116 + .getCisternadoPorVehiculo($routeParams.idVehiculo)
  117 + .then(function(res){
  118 + //Valida si existe numero de unidad
  119 + var existe = res.data.filter(function(cisterna) {
  120 + return cisterna.codigo === $scope.cisterna.codigo;
  121 + });
  122 +
  123 + if(existe.length){
  124 + reject(existe);
  125 + } else {
  126 + resolve();
  127 + }
  128 + });
  129 + });
  130 + }
119 131 }
120 132 ]);
... ... @@ -10,7 +10,7 @@ angular.module('focaAbmVehiculo')
10 10 getTransportistas: function() {
11 11 return $http.get(API_ENDPOINT.URL + '/transportista');
12 12 },
13   - guerdarVehiculo: function(vehiculo) {
  13 + guardarVehiculo: function(vehiculo) {
14 14 return $http.post(API_ENDPOINT.URL + '/vehiculo', {vehiculo: vehiculo});
15 15 },
16 16 deleteVehiculo: function(id) {