angular.module('focaParametros') .controller('focaParametrosCtrl', [ '$scope', 'focaParametrosService', 'focaBotoneraLateralService', '$filter', 'focaModalService', '$timeout', '$uibModal', '$location', function($scope, focaParametrosService, focaBotoneraLateralService, $filter, focaModalService, $timeout, $uibModal, $location) { $scope.botonera = []; $scope.now = new Date(); $scope.botoneraPrincipal = focaParametrosService.getBotonesPrincipal; $timeout(function() { focaBotoneraLateralService.showSalir(true); focaBotoneraLateralService.showGuardar(true, $scope.guardar); }); focaParametrosService.getParametros().then(function(res) { $scope.$broadcast('cleanCabecera'); res.data.forEach(function(entidad) { setearEntidad(entidad); }); }); $scope.guardar = function() { focaParametrosService.saveParametros(getObjGuardar()).then(function() { focaModalService.alert('Parámetros guardados con éxito'); $location.url('/'); }); }; $scope.seleccionarPuntosDeDescarga = function() { if (!$scope[getModulo()].cliente.COD || !$scope[getModulo()].domicilio.id) { focaModalService.alert('Primero seleccione un cliente y un domicilio'); return; } else { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Búsqueda de Puntos de descarga', templateUrl: 'modal-punto-descarga.html', controller: 'focaModalPuntoDescargaController', size: 'lg', resolve: { filters: { idDomicilio: $scope[getModulo()].domicilio.id, idCliente: $scope[getModulo()].cliente.COD, articulos: $scope[getModulo()].articulosNotaPedido || [], puntoDescarga: $scope[getModulo()].notaPedidoPuntoDescarga, domicilio: $scope[getModulo()].domicilio } } } ); modalInstance.result.then( function(puntoDescarga) { $scope[getModulo()].notaPedidoPuntoDescarga = puntoDescarga; $scope.$broadcast('addCabecera', { label: 'Puntos de descarga:', seccion: getModulo('label'), valor: getCabeceraPuntoDescarga(puntoDescarga) }); }, function() { $scope.abrirModalDomicilios($scope.cliente); } ); } }; $scope.seleccionarFlete = function() { if ($scope[getModulo()].flete === undefined) { $scope[getModulo()].fob = undefined; $scope[getModulo()].bomba = undefined; $scope[getModulo()].kilometros = undefined; } var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Flete', templateUrl: 'modal-flete.html', controller: 'focaModalFleteController', size: 'lg', resolve: { parametrosFlete: function() { return { flete: $scope[getModulo()].fob ? 'FOB' : ( $scope[getModulo()].flete ? '1' : ($scope[getModulo()].flete === undefined ? null : '0')), bomba: $scope[getModulo()].bomba ? '1' : ($scope[getModulo()].bomba === undefined ? null : '0'), kilometros: $scope[getModulo()].kilometros }; } } } ); modalInstance.result.then( function(datos) { $scope[getModulo()].flete = datos.flete; $scope[getModulo()].fob = datos.FOB; $scope[getModulo()].bomba = datos.bomba; $scope[getModulo()].kilometros = datos.kilometros; $scope.$broadcast('addCabecera', { label: 'Flete:', seccion: getModulo('label'), valor: datos.FOB ? 'FOB' : (datos.flete ? 'Si' : 'No') }); if (datos.flete) { $scope.$broadcast('addCabecera', { label: 'Bomba:', seccion: getModulo('label'), valor: datos.bomba ? 'Si' : 'No' }); $scope.$broadcast('addCabecera', { label: 'Kilometros:', seccion: getModulo('label'), valor: datos.kilometros }); } else { $scope.$broadcast('removeCabecera', 'Bomba:'); $scope.$broadcast('removeCabecera', 'Kilometros:'); $scope[getModulo()].bomba = false; $scope[getModulo()].kilometros = null; } }, function() { } ); }; $scope.seleccionarVendedor = function() { var parametrosModal = { titulo: 'Búsqueda vendedores', query: '/vendedor', columnas: [ { propiedad: 'NUM', nombre: 'Código', filtro: { nombre: 'rellenarDigitos', parametro: 3 } }, { propiedad: 'NOM', nombre: 'Nombre' } ], size: 'md' }; focaModalService.modal(parametrosModal).then( function(vendedor) { $scope.$broadcast('addCabecera', { label: 'Vendedor:', seccion: getModulo('label'), valor: $filter('rellenarDigitos')(vendedor.NUM, 3) + ' - ' + vendedor.NOM }); $scope[getModulo()].vendedor = vendedor; }, function() {} ); }; $scope.seleccionarCliente = function() { if (!$scope[getModulo()].vendedor) { focaModalService.alert('Seleccione Vendedor'); return; } var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Cliente', templateUrl: 'foca-busqueda-cliente-modal.html', controller: 'focaBusquedaClienteModalController', resolve: { vendedor: function() { return $scope.notaPedido.vendedor; }, cobrador: function() { return null; } }, size: 'lg' } ); modalInstance.result.then( function(cliente) { $scope.abrirModalDomicilios(cliente); }, function() { } ); }; $scope.seleccionarProveedor = function() { var parametrosModal = { titulo: 'Búsqueda de Proveedor', query: '/proveedor', columnas: [ { nombre: 'Código', propiedad: 'COD', filtro: { nombre: 'rellenarDigitos', parametro: 5 } }, { nombre: 'Nombre', propiedad: 'NOM' }, { nombre: 'CUIT', propiedad: 'CUIT' } ], tipo: 'POST', json: {razonCuitCod: ''} }; focaModalService.modal(parametrosModal).then( function(proveedor) { $scope[getModulo()].proveedor = proveedor; $scope.$broadcast('addCabecera', { label: 'Proveedor:', seccion: getModulo('label'), valor: $filter('rellenarDigitos')(proveedor.COD, 5) + ' - ' + proveedor.NOM }); }, function() { } ); }; $scope.abrirModalDomicilios = function(cliente) { var modalInstanceDomicilio = $uibModal.open( { ariaLabelledBy: 'Busqueda de Domicilios', templateUrl: 'modal-domicilio.html', controller: 'focaModalDomicilioController', resolve: { idCliente: function() { return cliente.cod; }, esNuevo: function() { return cliente.esNuevo; } }, size: 'lg', } ); modalInstanceDomicilio.result.then( function(domicilio) { $scope[getModulo()].domicilio = domicilio; $scope[getModulo()].cliente = { COD: cliente.cod, CUIT: cliente.cuit, NOM: cliente.nom, MOD: cliente.mod }; var domicilioStamp = domicilio.Calle + ' ' + domicilio.Numero + ', ' + domicilio.Localidad + ', ' + domicilio.Provincia; $scope[getModulo()].domicilioStamp = domicilioStamp; $scope[getModulo()].notaPedidoPuntoDescarga = domicilio.puntoDescarga; $scope.$broadcast('addCabecera', { label: 'Cliente:', seccion: getModulo('label'), valor: $filter('rellenarDigitos')(cliente.cod, 5) + ' - ' + cliente.nom }); $scope.$broadcast('addCabecera', { label: 'Domicilio:', seccion: getModulo('label'), valor: domicilioStamp }); if (domicilio.verPuntos) { delete $scope[getModulo()].domicilio.verPuntos; $scope.seleccionarPuntosDeDescarga(); } else { focaParametrosService .getPuntosDescargaByClienDom(domicilio.id, cliente.cod) .then(function(res) { if (res.data.length) $scope.seleccionarPuntosDeDescarga(); }); } }, function() { $scope.seleccionarCliente(true); return; } ); }; $scope.seleccionarMoneda = function() { var parametrosModal = { titulo: 'Búsqueda de monedas', query: '/moneda', columnas: [ { propiedad: 'DETALLE', nombre: 'Nombre' }, { propiedad: 'SIMBOLO', nombre: 'Símbolo' } ], size: 'md' }; focaModalService.modal(parametrosModal).then( function(moneda) { $scope.abrirModalCotizacion(moneda); }, function() { } ); }; $scope.abrirModalCotizacion = function(moneda) { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Cotización', templateUrl: 'modal-cotizacion.html', controller: 'focaModalCotizacionController', size: 'lg', resolve: { idMoneda: function() { return moneda.ID; } } } ); modalInstance.result.then( function(cotizacion) { $scope[getModulo()].cotizacion = cotizacion; $scope[getModulo()].cotizacion.moneda = moneda; $scope.$broadcast('addCabecera', { label: 'Moneda:', seccion: getModulo('label'), valor: moneda.DETALLE }); $scope.$broadcast('addCabecera', { label: 'Fecha cotizacion:', seccion: getModulo('label'), valor: $filter('date')(cotizacion.FECHA, 'dd/MM/yyyy') }); $scope.$broadcast('addCabecera', { label: 'Cotizacion:', seccion: getModulo('label'), valor: $filter('number')(cotizacion.VENDEDOR, '2') }); }, function() { } ); }; $scope.seleccionarObservaciones = function() { var observacion = { titulo: 'Ingrese Observaciones', value: $scope[getModulo()].observaciones, maxlength: 155, textarea: true }; focaModalService .prompt(observacion) .then(function(observaciones) { $scope[getModulo()].observaciones = observaciones; $scope.$broadcast('addCabecera', { label: 'Observaciones:', valor: observaciones }); }); }; $scope.seleccionarPreciosYCondiciones = function() { if (!$scope[getModulo()].cliente || !$scope[getModulo()].cliente.COD) { focaModalService.alert('Primero seleccione un cliente'); return; } var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Precio Condición', templateUrl: 'modal-precio-condicion.html', controller: 'focaModalPrecioCondicionController', size: 'lg', resolve: { idListaPrecio: function() { return $scope[getModulo()].cliente.MOD || null; } } } ); modalInstance.result.then( function(precioCondicion) { var cabecera = ''; var plazosConcat = ''; if (!Array.isArray(precioCondicion)) { $scope[getModulo()][getModulo() + 'Plazo' ]= precioCondicion.plazoPago; $scope[getModulo()].precioCondicion = precioCondicion; $scope[getModulo()].idPrecioCondicion = precioCondicion.id; $scope.idLista = precioCondicion.idListaPrecio; for (var i = 0; i < precioCondicion.plazoPago.length; i++) { plazosConcat += precioCondicion.plazoPago[i].dias + ' '; } cabecera = $filter('rellenarDigitos')(precioCondicion.id, 4) + ' - ' + precioCondicion.nombre + ' ' + plazosConcat.trim(); } else { //Cuando se ingresan los plazos manualmente $scope[getModulo()].idPrecioCondicion = 0; //-1, el modal productos busca todos los productos $scope.idLista = -1; $scope[getModulo()][ getModulo() + 'Plazo'] = precioCondicion; for (var j = 0; j < precioCondicion.length; j++) { plazosConcat += precioCondicion[j].dias + ' '; } cabecera = 'Ingreso manual ' + plazosConcat.trim(); } $scope.$broadcast('addCabecera', { label: 'Precios y condiciones:', seccion: getModulo('label'), valor: cabecera }); }, function() { } ); }; $scope.seleccionarTarifario = function() { focaModalService .prompt({ titulo: 'Tarifa flete', value: $scope[getModulo()].tarifario }) .then(function(costo) { if (isNaN(costo)) { focaModalService .alert('Ingrese un valor válido') .then(function() { $scope.seleccionarTarifario(); }); return; } $scope[getModulo()].tarifario = costo; $scope.$broadcast('addCabecera', { label: 'Tarifario:', valor: costo, seccion: getModulo('label') }); }); }; $scope.seleccionarTransportista = function() { var parametrosModal = { titulo: 'Búsqueda de transportista', query: '/transportista/no-relacion', columnas: [ { nombre: 'Código', propiedad: 'COD' }, { nombre: 'Nombre', propiedad: 'NOM' }, { nombre: 'CUIT', propiedad: 'CUIT' } ] }; focaModalService.modal(parametrosModal).then( function(proveedor) { $scope[getModulo()].transportista = proveedor; $scope.$broadcast('addCabecera', { label: 'Transportista:', seccion: getModulo('label'), valor: $filter('rellenarDigitos')(proveedor.COD, 5) + ' - ' + proveedor.NOM }); }, function() { } ); }; $scope.$watch('botonera', function() { // Creo el string en donde guardo el objeto parseado $scope[getModulo()] = $scope[getModulo()] || {}; getCheckeds(); }, true); $scope.botoneraPrincipal.forEach(function(botonPincipal) { // watch en objetos principales para ir guardando los valores $scope.$watch(botonPincipal.variable, function() { $scope[botonPincipal.variable + 'String'] = { jsonText: JSON.stringify($scope[botonPincipal.variable]), modulo: botonPincipal.variable, id: $scope[botonPincipal.variable + 'String'] ? $scope[botonPincipal.variable + 'String'].id : undefined }; getCheckeds(); }, true); // creo las funciones para seleccionar boton principal $scope[nombreFuncion(botonPincipal.label)] = function() { $scope.botoneraPrincipal.filter(function(boton) { boton.checked = false; }); botonPincipal.checked = true; $scope.botonera = focaParametrosService.getBotones(botonPincipal.modulo); }; }); function nombreFuncion(string) { var texto = 'seleccionar'; var arr = string.split(' '); arr.forEach(function(palabra) { palabra = palabra.charAt(0).toUpperCase() + palabra.slice(1); texto += palabra; }); return texto; } function getModulo(propiedad) { var nombre = ''; $scope.botoneraPrincipal.filter(function(boton) { if (boton.checked) { if(!propiedad) { nombre = boton.variable; } else { nombre = boton[propiedad]; } } }); return nombre; } function getCheckeds() { $scope.botonera.forEach(function(boton) { if ($scope[getModulo()][boton.variable] !== undefined) { boton.checked = true; //Creo la función para limpiar obj $scope['clean' + boton.variable] = function() { focaModalService.alert('Esta seguro que desea eliminar el parámetro?') .then(function() { delete $scope[getModulo()][boton.variable]; $scope.$broadcast('cleanCabecera', { seccion: getModulo('label') }); setearEntidad({ jsonText: JSON.stringify($scope[getModulo()]), modulo: getModulo(), id: $scope[getModulo() + 'String'].id }); }); }; } else { boton.checked = false; } }); } function getCabeceraPuntoDescarga(puntoDescarga) { var puntosStamp = ''; puntoDescarga.forEach(function(punto, idx, arr) { puntosStamp += punto.descripcion; if ((idx + 1) !== arr.length) puntosStamp += ', '; }); return puntosStamp; } function setearEntidad(entidad) { $scope[entidad.modulo] = JSON.parse(entidad.jsonText) || {}; $scope[entidad.modulo + 'String'].id = entidad.id; if (!$scope[entidad.modulo].domicilio) { $scope[entidad.modulo].domicilio = { id: $scope[entidad.modulo].idDomicilio }; } var cabeceras = []; if ($scope[entidad.modulo].cotizacion) { cabeceras.push({ label: 'Moneda:', valor: $scope[entidad.modulo].cotizacion.moneda.DETALLE }); cabeceras.push({ label: 'Fecha cotizacion:', valor: $filter('date')($scope[entidad.modulo].cotizacion.FECHA, 'dd/MM/yyyy') }); cabeceras.push({ label: 'Cotizacion:', valor: $filter('number')($scope[entidad.modulo].cotizacion.VENDEDOR, '2') }); } if ($scope[entidad.modulo].vendedor && $scope[entidad.modulo].vendedor.NUM) { cabeceras.push({ label: 'Vendedor:', valor: $filter('rellenarDigitos')($scope[entidad.modulo].vendedor.NUM, 3) + ' - ' + $scope[entidad.modulo].vendedor.NOM }); } if ($scope[entidad.modulo].cliente && $scope[entidad.modulo].cliente.COD) { cabeceras.push({ label: 'Cliente:', valor: $scope[entidad.modulo].cliente.NOM }); cabeceras.push({ label: 'Domicilio:', valor: $scope[entidad.modulo].domicilioStamp }); } if ($scope[entidad.modulo].proveedor && $scope[entidad.modulo].proveedor.COD) { cabeceras.push({ label: 'Proveedor:', valor: $filter('rellenarDigitos')($scope[entidad.modulo].proveedor.COD, 5) + ' - ' + $scope[entidad.modulo].proveedor.NOM }); } if ($scope[entidad.modulo][entidad + 'Plazo'] && $scope[entidad.modulo][entidad + 'Plazo'].length) { cabeceras.push({ label: 'Precios y condiciones:', valor: valorPrecioCondicion() + ' ' + focaParametrosService.plazoToString( $scope[entidad.modulo][entidad.modulo + 'Plazo']) }); } if ($scope[entidad.modulo].flete !== undefined) { cabeceras.push({ label: 'Flete:', valor: $scope[entidad.modulo].fob === 1 ? 'FOB' : ( $scope[entidad.modulo].flete === 1 ? 'Si' : 'No') }); } function valorPrecioCondicion() { if ($scope[entidad.modulo].idPrecioCondicion > 0) { return $scope[entidad.modulo].precioCondicion.nombre; } else { return 'Ingreso Manual'; } } if ($scope[entidad.modulo].flete === 1) { var cabeceraBomba = { label: 'Bomba:', valor: $scope[entidad.modulo].bomba === 1 ? 'Si' : 'No' }; if ($scope[entidad.modulo].kilometros) { var cabeceraKilometros = { label: 'Kilometros:', valor: $scope[entidad.modulo].kilometros }; cabeceras.push(cabeceraKilometros); } cabeceras.push(cabeceraBomba); } if ($scope[entidad.modulo].idPrecioCondicion > 0) { $scope.idLista = $scope[entidad.modulo].precioCondicion.idListaPrecio; } else { $scope.idLista = -1; } if ($scope[entidad.modulo][entidad.modulo + 'PuntoDescarga']) { var puntos = []; $scope[entidad.modulo][entidad.modulo + 'PuntoDescarga'] .forEach(function(entidadPuntoDescarga) { puntos.push(entidadPuntoDescarga); }); cabeceras.push({ label: 'Puntos de descarga: ', valor: $filter('rellenarDigitos')(getCabeceraPuntoDescarga(puntos)) }); } if ($scope[entidad.modulo].tarifario) { cabeceras.push({ label: 'Tarifario', valor: $scope[entidad.modulo].tarifario }); } if ($scope[entidad.modulo].transportista) { cabeceras.push({ label: 'Transportista', valor: $filter('rellenarDigitos')($scope[entidad.modulo].transportista.COD, 5) + ' - ' + $scope[entidad.modulo].transportista.NOM }); } addArrayCabecera(cabeceras, entidad.modulo); } function addArrayCabecera(array, entidad) { for (var i = 0; i < array.length; i++) { $scope.$broadcast('addCabecera', { label: array[i].label, valor: array[i].valor, seccion: $filter('filter') ($scope.botoneraPrincipal, {variable: entidad}, true)[0].label }); } } function getObjGuardar() { var guardar = []; $scope.botoneraPrincipal.forEach(function(botonPincipal) { if (!botonPincipal || !$scope[botonPincipal.variable + 'String']) return; guardar.push($scope[botonPincipal.variable + 'String']); }); return guardar; } }]);