angular.module('focaAbmPreciosCondiciones') .controller('focaAbmPreciosCondicionesController', [ '$scope', 'focaAbmPreciosCondicionesService', '$location', 'focaModalService', 'focaBotoneraLateralService', '$timeout', function($scope, focaAbmPreciosCondicionesService, $location, focaModalService, focaBotoneraLateralService, $timeout) { $timeout(function() { focaBotoneraLateralService.showSalir(true); focaBotoneraLateralService.showPausar(false); focaBotoneraLateralService.showCancelar(false); focaBotoneraLateralService.showGuardar(false); }); $scope.filters = ''; $scope.now = new Date(); $scope.editar = function(id) { $location.path('/precio-condicion/' + id); }; $scope.solicitarConfirmacion = function(precioCondicion) { focaModalService.confirm('¿Está seguro que desea borrar el precio condición' + precioCondicion.codigo + ' ' + precioCondicion.nombre + ' ?').then( function(data) { if (data) { focaAbmPreciosCondicionesService .borrarPrecioCondicion(precioCondicion.id); $scope.preciosCondiciones.splice( $scope.preciosCondiciones.indexOf(precioCondicion), 1 ); } } ); }; $scope.buscar = function(key) { if(key === 13){ focaAbmPreciosCondicionesService .obtenerPreciosCondiciones($scope.filters) .then(function(datos) { $scope.preciosCondiciones = datos.data; $scope.preciosCondiciones.forEach(function(precioCondicion) { precioCondicion.plazoPago.sort(function(a, b) { return a.dias- b.dias; }); }); }); } }; } ]) .controller('focaAbmPrecioCondicionController', [ '$scope', 'focaAbmPreciosCondicionesService', 'focaBotoneraLateralService', '$routeParams', '$location', 'focaModalService', '$timeout', '$uibModal', '$window', function( $scope, focaAbmPreciosCondicionesService, focaBotoneraLateralService, $routeParams, $location, focaModalService, $timeout, $uibModal, $window ) { $scope.focused = 1; $scope.plazosAEliminar = []; $scope.mostrarPlazos = $routeParams.id > 0; $scope.now = new Date(); $scope.listaPrecioLabel = ''; focaAbmPreciosCondicionesService.obtenerPrecioCondicion($routeParams.id) .then(function(datos) { $scope.precioCondicion = { id: 0, codigo: '', nombre: '', descripcion: '', idListaPrecio: 0, vigencia: new Date() }; if(datos.data.id) { $scope.precioCondicion = datos.data; $scope.listaPrecioLabel = datos.data.listaPrecio.ID.trim() + ' - ' + datos.data.listaPrecio.DES; $scope.$broadcast('addCabecera', { label: 'Precio y condición:', valor: datos.data.nombre }); focaAbmPreciosCondicionesService.obtenerPlazoPago(datos.data.id) .then(function(datos) { $scope.precioCondicion.plazos = datos.data; $scope.precioCondicion.plazos.sort(function(a, b) { return a.dias- b.dias; }); $scope.plazoACargar = { item: datos.data.length + 1 }; }); } }); $timeout(function() { focaBotoneraLateralService.showSalir(false); focaBotoneraLateralService.showPausar(true); focaBotoneraLateralService.showCancelar(true); focaBotoneraLateralService.showGuardar(true, $scope.guardar); }); $scope.cancelar = function() { $location.path('/precio-condicion'); }; $scope.guardar = function() { var promises = []; var precioCondicion = $scope.precioCondicion; precioCondicion.idListaPrecio = precioCondicion.listaPrecio.ID; delete precioCondicion.listaPrecio; if($scope.mostrarPlazos) { promises.push( focaAbmPreciosCondicionesService .guardarPlazosPago($scope.precioCondicion.plazos) ); $scope.plazosAEliminar.forEach(function(id) { promises.push( focaAbmPreciosCondicionesService .borrarPlazoPago(id) ); }); } promises.push( focaAbmPreciosCondicionesService .guardarPrecioCondicion(precioCondicion) ); Promise.all(promises).then(function() { $window.location.assign('/#!/precio-condicion'); }, function(err){ console.error(err); }); }; $scope.agregarPlazo = function(key) { if(key === 13) { if(!$scope.plazoACargar.dias) { focaModalService.alert('Ingrese cantidad de días'); return; } var tieneEseDia = $scope.precioCondicion.plazos.filter(function(a) { return a.dias === $scope.plazoACargar.dias; }); if(tieneEseDia.length > 0) { focaModalService.alert('Ya ha ingresado un plazo con esos días'); return; } $scope.plazoACargar.idPreciosCondiciones = $scope.precioCondicion.id; $scope.plazoACargar.activo = true; $scope.precioCondicion.plazos.push($scope.plazoACargar); $scope.plazoACargar = { item: $scope.precioCondicion.plazos.length + 1 }; } }; $scope.quitarPlazo = function(key) { if($scope.precioCondicion.plazos[key].id) $scope.plazosAEliminar.push($scope.precioCondicion.plazos[key].id); $scope.precioCondicion.plazos.splice(key, 1); $scope.plazoACargar = { item: $scope.precioCondicion.plazos.length + 1 }; }; $scope.solicitarConfirmacionPlazoPago = function(plazoPago) { focaModalService.confirm('¿Está seguro que desea borrar el plazo de pago ' + plazoPago.item + ' ' + plazoPago.dias + ' ?').then( function(confirm) { if (confirm) { focaAbmPreciosCondicionesService.borrarPlazoPago(plazoPago.id); $scope.precioCondicion.plazos.splice( $scope.precioCondicion.plazos.indexOf(plazoPago), 1 ); } } ); }; $scope.seleccionarListaPrecio = function(key) { if(key === 13){ var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Listas de precio', templateUrl: 'modal-lista-precio.html', controller: 'focaModalListaPrecioCtrl', size: 'lg' } ); modalInstance.result.then(function(listaPrecio) { $scope.precioCondicion.listaPrecio = listaPrecio; $scope.listaPrecioLabel = listaPrecio.ID + ' - ' + listaPrecio.DES; $scope.focused = 4; }); } }; $scope.next = function(key) { if(key === 13) $scope.focused++; }; } ]);