Commit b11fcc2bf10dde9fc3a230ebd0d26b2dd7b0aea8

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

Merge branch 'develop' into 'master'

Develop

See merge request !17
src/js/controller.js
1 1 angular.module('focaAbmChofer')
2 2 .controller('focaAbmChoferesController', [
3 3 '$scope', 'focaAbmChoferService', '$location', '$uibModal',
4   - 'focaModalService', 'focaBotoneraLateralService', '$timeout',
  4 + 'focaModalService', 'focaBotoneraLateralService', '$timeout', '$localStorage',
5 5 function($scope, focaAbmChoferService, $location, $uibModal, focaModalService,
6   - focaBotoneraLateralService, $timeout) {
  6 + focaBotoneraLateralService, $timeout, $localStorage) {
7 7  
8 8 $scope.now = new Date();
9 9 $scope.filters = '';
... ... @@ -20,20 +20,20 @@ angular.module('focaAbmChofer')
20 20 focaBotoneraLateralService.showCancelar(false);
21 21 focaBotoneraLateralService.showGuardar(false);
22 22 focaBotoneraLateralService.addCustomButton('Salir', salir);
23   - });
  23 + });
24 24  
25   - if(focaAbmChoferService.transportistaSeleccionado.COD) {
  25 + if (focaAbmChoferService.transportistaSeleccionado.COD) {
26 26 elegirTransportista(focaAbmChoferService.transportistaSeleccionado);
27 27 }
28 28  
29   - $scope.editar = function(id) {
  29 + $scope.editar = function(id) {
30 30 $location.path('/chofer/' + id + '/' + $scope.idTransportista);
31 31 };
32 32  
33 33 $scope.solicitarConfirmacion = function(chofer) {
34 34 focaModalService.confirm('¿Está seguro que desea borrar el chofer ' +
35 35 chofer.nombre + ' ?').then(function(confirmed) {
36   - if(confirmed) {
  36 + if (confirmed) {
37 37 focaAbmChoferService.deleteChofer(chofer.id);
38 38 $scope.choferes.splice($scope.choferes.indexOf(chofer), 1);
39 39 }
... ... @@ -89,13 +89,19 @@ angular.module('focaAbmChofer')
89 89 focaAbmChoferService.transportistaSeleccionado = {};
90 90 $location.path('/');
91 91 }
  92 +
  93 + if ($localStorage.chofer) {
  94 + var chofer = JSON.parse($localStorage.chofer);
  95 + if (!chofer.id) { chofer.id = 0; }
  96 + $location.path('/chofer/' + chofer.id + '/' + chofer.idTransportista);
  97 + }
92 98 }
93 99 ])
94 100 .controller('focaAbmChoferController', [
95   - '$scope', 'focaAbmChoferService', '$routeParams',
96   - '$location', 'focaBotoneraLateralService', '$timeout', 'focaModalService',
97   - function($scope, focaAbmChoferService, $routeParams,
98   - $location, focaBotoneraLateralService, $timeout, focaModalService) {
  101 + '$scope', 'focaAbmChoferService', '$routeParams', '$localStorage', '$filter',
  102 + '$location', 'focaBotoneraLateralService', '$timeout', 'focaModalService', '$window',
  103 + function($scope, focaAbmChoferService, $routeParams, $localStorage, $filter,
  104 + $location, focaBotoneraLateralService, $timeout, focaModalService){
99 105  
100 106 $scope.focused = 1;
101 107 $scope.nuevo = $routeParams.id === '0';
... ... @@ -114,11 +120,15 @@ angular.module('focaAbmChofer')
114 120 $timeout(function() {
115 121 focaBotoneraLateralService.showSalir(false);
116 122 focaBotoneraLateralService.showPausar(true);
117   - focaBotoneraLateralService.showCancelar(true);
  123 + focaBotoneraLateralService.showCancelar(false);
118 124 focaBotoneraLateralService.showGuardar(true, $scope.guardar);
  125 + focaBotoneraLateralService.addCustomButton('Salir', salir);
  126 +
119 127 });
120 128  
121   - if($scope.nuevo) {
  129 + $timeout(function() {getLSChofer();});
  130 +
  131 + if ($scope.nuevo) {
122 132 focaAbmChoferService
123 133 .getTransportistaPorId($routeParams.idTransportista)
124 134 .then(function(res) {
... ... @@ -147,11 +157,27 @@ angular.module('focaAbmChofer')
147 157 $scope.transportistas = res.data;
148 158 });
149 159  
  160 + function setearChofer(chofer) {
  161 + $scope.chofer = chofer;
  162 + $scope.$broadcast('addCabecera', {
  163 + label: 'Transportista:',
  164 + valor: $filter('rellenarDigitos')(chofer.idTransportista.chofer, 2) + ' - '
  165 + });
  166 + }
  167 + function getLSChofer() {
  168 + var chofer = JSON.parse($localStorage.chofer || null);
  169 +
  170 + if (chofer) {
  171 + setearChofer(chofer);
  172 + delete $localStorage.chofer;
  173 + }
  174 + }
  175 +
150 176 $scope.cancelar = function() {
151 177 $location.path('/chofer');
152 178 };
153 179  
154   - $scope.guardar = function(key) {
  180 + $scope.guardar = function(key) {
155 181  
156 182 if (!$scope.chofer.nombre) {
157 183 focaModalService.alert('Ingrese nombre');
... ... @@ -159,13 +185,13 @@ angular.module('focaAbmChofer')
159 185 } else if (!$scope.chofer.idTipoDocumento) {
160 186 focaModalService.alert('Seleccione tipo de documento');
161 187 return;
162   - } else if (!$scope.chofer.dni){
  188 + } else if (!$scope.chofer.dni) {
163 189 focaModalService.alert('Ingrese DNI');
164 190 return;
165 191 }
166 192  
167 193 key = (typeof key === 'undefined') ? 13 : key;
168   - if(key === 13) {
  194 + if (key === 13) {
169 195 validaDni().then(function() {
170 196 $scope.chofer.idTransportista = $routeParams.idTransportista;
171 197 delete $scope.chofer.transportista;
... ... @@ -177,16 +203,44 @@ angular.module('focaAbmChofer')
177 203 });
178 204 }
179 205 };
  206 +
  207 + $scope.$watch('chofer', function(newValue) {
  208 + focaBotoneraLateralService.setPausarData({
  209 + label: 'chofer',
  210 + val: {
  211 + dni: newValue.dni,
  212 + idTransportista: newValue.idTransportista,
  213 + nombre: newValue.nombre,
  214 + telefono: newValue.telefono,
  215 + idTipoDocumento: newValue.idTipoDocumento,
  216 + id: newValue.id
  217 + }
  218 + });
  219 + }, true);
  220 +
  221 + function salir() {
  222 + if ($scope.formChofer.$pristine == false) {
  223 + focaModalService.confirm(
  224 + '¿Está seguro de que desea salir? Se perderán todos los datos cargados.'
  225 + ).then(function(data) {
  226 + if (data) {
  227 + $location.path('/chofer');
  228 + }
  229 + });
  230 + } else {
  231 + $location.path('/chofer');
  232 + }
  233 + }
180 234  
181 235 function validaDni() {
182 236 return new Promise(function(resolve, reject) {
183 237 focaAbmChoferService
184 238 .getChoferPorDni($scope.chofer.dni)
185 239 .then(function(res) {
186   - if(res.data.id &&
  240 + if (res.data.id &&
187 241 $scope.chofer.id !== res.data.id) {
188 242 reject(res.data);
189   - }else {
  243 + } else {
190 244 resolve();
191 245 }
192 246 });