angular.module('focaCrearNotaPedido') .controller('notaPedidoCtrl', [ '$scope', '$uibModal', '$location', '$filter', '$timeout', 'crearNotaPedidoService', 'focaBotoneraLateralService', 'focaModalService', 'notaPedidoBusinessService', '$rootScope', 'focaSeguimientoService', 'APP', 'focaLoginService', '$localStorage', function( $scope, $uibModal, $location, $filter, $timeout, crearNotaPedidoService, focaBotoneraLateralService, focaModalService, notaPedidoBusinessService, $rootScope, focaSeguimientoService, APP, focaLoginService, $localStorage) { config(); function config() { // PARAMETROS INICIALES PARA FUNCIONAMIENTO DEL PROGRAMA $scope.isNumber = angular.isNumber; $scope.datepickerAbierto = false; $scope.show = false; $scope.cargando = true; $scope.puntoVenta = $filter('rellenarDigitos')(0, 4); $scope.comprobante = $filter('rellenarDigitos')(0, 8); $scope.dateOptions = { maxDate: new Date(), minDate: new Date(2010, 0, 1) }; //SETEO BOTONERA LATERAL $timeout(function() { focaBotoneraLateralService.showSalir(false); focaBotoneraLateralService.showPausar(true); focaBotoneraLateralService.showGuardar(true, $scope.crearNotaPedido); focaBotoneraLateralService.addCustomButton('Salir', salir); }); // SETEA BOTONERA DE FACTURADOR TENIENDO EN CUENTA SI ESTA SETEADO EL VENDEDOR if (APP === 'distribuidor') { $scope.idVendedor = focaLoginService.getLoginData().vendedorCobrador; $scope.botonera = crearNotaPedidoService.getBotonera($scope.idVendedor); } else { $scope.botonera = crearNotaPedidoService.getBotonera(); } //Trabajo con la cotización más reciente, por eso uso siempre la primera '[0]' crearNotaPedidoService.getCotizacionByIdMoneda(1).then(function(res) { var monedaPorDefecto = res.data[0]; $scope.cotizacionPorDefecto = Object.assign( {moneda: monedaPorDefecto}, monedaPorDefecto.cotizaciones[0] ); init(); getLSNotaPedido(); }); } function init() { $scope.$broadcast('cleanCabecera'); $scope.notaPedido = { id: 0, cliente: {}, proveedor: {}, domicilio: {dom: ''}, vendedor: {}, fechaCarga: new Date(), cotizacion: $scope.cotizacionPorDefecto }; $scope.notaPedido.articulosNotaPedido = []; $scope.idLista = undefined; crearNotaPedidoService.getNumeroNotaPedido().then( function(res) { $scope.puntoVenta = $filter('rellenarDigitos')( res.data.sucursal, 4 ); $scope.comprobante = $filter('rellenarDigitos')( res.data.numeroNotaPedido, 8 ); }, function(err) { focaModalService.alert('La terminal no esta configurada correctamente'); console.info(err); } ); if (APP === 'distribuidor') { crearNotaPedidoService.getVendedorById($scope.idVendedor).then( function(res) { var vendedor = res.data; $scope.$broadcast('addCabecera', { label: 'Vendedor:', valor: $filter('rellenarDigitos')(vendedor.NUM, 3) + ' - ' + vendedor.NOM }); $scope.notaPedido.vendedor = vendedor; $scope.inicial.notaPedido.vendedor =$scope.notaPedido.vendedor; } ); } $scope.inicial = angular.copy($scope.notaPedido); } $scope.$watch('notaPedido', function(newValue, oldValue) { focaBotoneraLateralService.setPausarData({ label: 'notaPedido', val: newValue }); }, true); $scope.crearNotaPedido = function() { if(!$scope.notaPedido.vendedor.id) { focaModalService.alert('Ingrese Vendedor'); return; } else if(!$scope.notaPedido.cliente.COD) { focaModalService.alert('Ingrese Cliente'); return; } else if(!$scope.notaPedido.proveedor.COD) { focaModalService.alert('Ingrese Proveedor'); return; } else if(!$scope.notaPedido.cotizacion.moneda.ID) { focaModalService.alert('Ingrese Moneda'); return; } else if(!$scope.notaPedido.cotizacion.ID) { focaModalService.alert('Ingrese Cotización'); return; } else if(!$scope.notaPedido.notaPedidoPlazo) { focaModalService.alert('Ingrese Precios y Condiciones'); return; } else if( $scope.notaPedido.flete === undefined || $scope.notaPedido.flete === null) { focaModalService.alert('Ingrese Flete'); return; } else if(!$scope.notaPedido.domicilioStamp) {//TODO validar domicilio correcto focaModalService.alert('Ingrese Domicilio'); return; } else if($scope.notaPedido.articulosNotaPedido.length === 0) { focaModalService.alert('Debe cargar al menos un articulo'); return; } focaBotoneraLateralService.startGuardar(); $scope.saveLoading = true; var notaPedido = { id: $scope.notaPedido.id, fechaCarga: new Date($scope.notaPedido.fechaCarga).toISOString().slice(0, 19).replace('T', ' '), idVendedor: $scope.notaPedido.vendedor.id, idCliente: $scope.notaPedido.cliente.COD, nombreCliente: $scope.notaPedido.cliente.NOM, cuitCliente: $scope.notaPedido.cliente.CUIT, idProveedor: $scope.notaPedido.proveedor.COD, idDomicilio: $scope.notaPedido.idDomicilio || $scope.notaPedido.domicilio.id, idCotizacion: $scope.notaPedido.cotizacion.ID, idPrecioCondicion: $scope.notaPedido.idPrecioCondicion, cotizacion: $scope.notaPedido.cotizacion.VENDEDOR, flete: $scope.notaPedido.flete, fob: $scope.notaPedido.fob, bomba: $scope.notaPedido.bomba, kilometros: $scope.notaPedido.kilometros, domicilioStamp: $scope.notaPedido.domicilioStamp, observaciones: $scope.notaPedido.observaciones, estado: 0, total: $scope.getTotal() }; crearNotaPedidoService.crearNotaPedido(notaPedido).then( function(data) { // Al guardar los datos de la nota de pedido logueamos la // actividad para su seguimiento. //TODO: GUARDAR POSISIONAMIENTO AL EDITAR? focaSeguimientoService.guardarPosicion( 'Nota de pedido', data.data.id, '' ); notaPedidoBusinessService.addArticulos($scope.notaPedido.articulosNotaPedido, data.data.id, $scope.notaPedido.cotizacion.VENDEDOR); if($scope.notaPedido.notaPedidoPuntoDescarga) { notaPedidoBusinessService.addPuntosDescarga(data.data.id, $scope.notaPedido.notaPedidoPuntoDescarga); } var plazos = $scope.notaPedido.notaPedidoPlazo; var plazosACrear = []; plazos.forEach(function(plazo) { plazosACrear.push({ idNotaPedido: data.data.id, dias: plazo.dias }); }); if (plazosACrear.length) { crearNotaPedidoService.crearPlazosParaNotaPedido(plazosACrear); } notaPedidoBusinessService.addEstado(data.data.id, $scope.notaPedido.vendedor.id); focaBotoneraLateralService.endGuardar(true); $scope.saveLoading = false; init(); }, function(error) { focaModalService.alert('Hubo un error al crear la nota de pedido'); focaBotoneraLateralService.endGuardar(); $scope.saveLoading = false; console.info(error); } ); }; $scope.seleccionarNotaPedido = function() { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Nota de Pedido', templateUrl: 'foca-modal-nota-pedido.html', controller: 'focaModalNotaPedidoController', size: 'lg', resolve: { usadoPor: function() {return 'notaPedido';}, idVendedor: function() { if(APP === 'distribuidor') return $scope.notaPedido.vendedor.id; else return null; } } } ); modalInstance.result.then(setearNotaPedido); }; $scope.seleccionarProductos = function() { if ($scope.idLista === undefined) { focaModalService.alert( 'Primero seleccione una lista de precio y condicion'); return; } var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Productos', templateUrl: 'modal-busqueda-productos.html', controller: 'modalBusquedaProductosCtrl', resolve: { parametroProducto: { idLista: $scope.idLista, cotizacion: $scope.notaPedido.cotizacion.VENDEDOR, simbolo: $scope.notaPedido.cotizacion.moneda.SIMBOLO } }, size: 'lg' } ); modalInstance.result.then( function(producto) { var newArt = { id: 0, codigo: producto.codigo, sector: producto.sector, sectorCodigo: producto.sector + '-' + producto.codigo, descripcion: producto.descripcion, item: $scope.notaPedido.articulosNotaPedido.length + 1, nombre: producto.descripcion, precio: parseFloat(producto.precio.toFixed(4)), costoUnitario: producto.costo, editCantidad: false, editPrecio: false, rubro: producto.CodRub, exentoUnitario: producto.precio, ivaUnitario: producto.IMPIVA, impuestoInternoUnitario: producto.ImpInt, impuestoInterno1Unitario: producto.ImpInt2, impuestoInterno2Unitario: producto.ImpInt3, precioLista: producto.precio, combustible: 1, facturado: 0, idArticulo: producto.id }; $scope.articuloACargar = newArt; $scope.cargando = false; }, function() { // funcion ejecutada cuando se cancela el modal } ); }; $scope.seleccionarPuntosDeDescarga = function() { if(!$scope.notaPedido.cliente.COD || !$scope.notaPedido.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.notaPedido.domicilio.id, idCliente: $scope.notaPedido.cliente.COD, articulos: $scope.notaPedido.articulosNotaPedido, puntosDescarga: $scope.notaPedido.notaPedidoPuntoDescarga } } } ); modalInstance.result.then( function(puntosDescarga) { $scope.notaPedido.notaPedidoPuntoDescarga = puntosDescarga; //AGREGO PUNTOS DE DESCARGA A CABECERA var puntosStamp = ''; puntosDescarga.forEach(function(punto, idx, arr) { puntosStamp += punto.descripcion; if((idx + 1) !== arr.length) puntosStamp += ', '; }); $scope.$broadcast('addCabecera', { label: 'Puntos de descarga:', valor: puntosStamp }); }, function() { $scope.abrirModalDomicilios($scope.cliente); } ); } }; $scope.seleccionarVendedor = function() { if(validarNotaRemitada()) { 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:', valor: $filter('rellenarDigitos')(vendedor.NUM, 3) + ' - ' + vendedor.NOM }); $scope.notaPedido.vendedor = vendedor; }, function() { } ); } }; $scope.seleccionarProveedor = function() { if(validarNotaRemitada()) { 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.notaPedido.proveedor = proveedor; $scope.$broadcast('addCabecera', { label: 'Proveedor:', valor: $filter('rellenarDigitos')(proveedor.COD, 5) + ' - ' + proveedor.NOM }); }, function() { } ); } }; $scope.seleccionarCliente = function() { if(!$scope.notaPedido.vendedor.NUM) { focaModalService.alert('Primero seleccione un vendedor'); return; } if(validarNotaRemitada()) { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Cliente', templateUrl: 'foca-busqueda-cliente-modal.html', controller: 'focaBusquedaClienteModalController', resolve: { vendedor: function() { return $scope.notaPedido.vendedor; } }, size: 'lg' } ); modalInstance.result.then( function(cliente) { $scope.abrirModalDomicilios(cliente); $scope.cliente = cliente; }, 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.notaPedido.domicilio = domicilio; $scope.notaPedido.cliente = { COD: cliente.cod, CUIT: cliente.cuit, NOM: cliente.nom }; var domicilioStamp = domicilio.Calle + ' ' + domicilio.Numero + ', ' + domicilio.Localidad + ', ' + domicilio.Provincia; $scope.notaPedido.domicilioStamp = domicilioStamp; $scope.notaPedido.notaPedidoPuntoDescarga = domicilio.puntosDescarga; $scope.$broadcast('addCabecera', { label: 'Cliente:', valor: $filter('rellenarDigitos')(cliente.cod, 5) + ' - ' + cliente.nom }); $scope.$broadcast('addCabecera', { label: 'Domicilio:', valor: domicilioStamp }); if(domicilio.verPuntos) { delete $scope.notaPedido.domicilio.verPuntos; $scope.seleccionarPuntosDeDescarga(); }else { crearNotaPedidoService .getPuntosDescargaByClienDom(domicilio.id, cliente.cod) .then(function(res) { if(res.data.length) $scope.seleccionarPuntosDeDescarga(); }); } }, function() { $scope.seleccionarCliente(); return; } ); }; $scope.getTotal = function() { var total = 0; if ($scope.notaPedido.articulosNotaPedido) { var arrayTempArticulos = $scope.notaPedido.articulosNotaPedido; for (var i = 0; i < arrayTempArticulos.length; i++) { total += arrayTempArticulos[i].precio * arrayTempArticulos[i].cantidad; } } return parseFloat(total.toFixed(2)); }; $scope.getSubTotal = function() { if($scope.articuloACargar) { return $scope.articuloACargar.precio * $scope.articuloACargar.cantidad; } }; $scope.seleccionarPreciosYCondiciones = function() { if(validarNotaRemitada()) { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Precio Condición', templateUrl: 'modal-precio-condicion.html', controller: 'focaModalPrecioCondicionController', size: 'lg' } ); modalInstance.result.then( function(precioCondicion) { var cabecera = ''; var plazosConcat = ''; if(!Array.isArray(precioCondicion)) { $scope.notaPedido.notaPedidoPlazo = precioCondicion.plazoPago; $scope.notaPedido.precioCondicion = precioCondicion; $scope.notaPedido.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.notaPedido.idPrecioCondicion = 0; //-1, el modal productos busca todos los productos $scope.idLista = -1; $scope.notaPedido.notaPedidoPlazo = precioCondicion; for(var j = 0; j < precioCondicion.length; j++) { plazosConcat += precioCondicion[j].dias + ' '; } cabecera = 'Ingreso manual ' + plazosConcat.trim(); } $scope.notaPedido.articulosNotaPedido = []; $scope.$broadcast('addCabecera', { label: 'Precios y condiciones:', valor: cabecera }); }, function() { } ); } }; $scope.seleccionarFlete = function() { if(validarNotaRemitada()) { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Flete', templateUrl: 'modal-flete.html', controller: 'focaModalFleteController', size: 'lg', resolve: { parametrosFlete: function() { return { flete: $scope.notaPedido.fob ? 'FOB' : ( $scope.notaPedido.flete ? '1' : ($scope.notaPedido.flete === undefined ? null : '0')), bomba: $scope.notaPedido.bomba ? '1' : ($scope.notaPedido.bomba === undefined ? null : '0'), kilometros: $scope.notaPedido.kilometros }; } } } ); modalInstance.result.then( function(datos) { $scope.notaPedido.flete = datos.flete; $scope.notaPedido.fob = datos.FOB; $scope.notaPedido.bomba = datos.bomba; $scope.notaPedido.kilometros = datos.kilometros; $scope.$broadcast('addCabecera', { label: 'Flete:', valor: datos.FOB ? 'FOB' : (datos.flete ? 'Si' : 'No') }); if(datos.flete) { $scope.$broadcast('addCabecera', { label: 'Bomba:', valor: datos.bomba ? 'Si' : 'No' }); $scope.$broadcast('addCabecera', { label: 'Kilometros:', valor: datos.kilometros }); } else { $scope.$broadcast('removeCabecera', 'Bomba:'); $scope.$broadcast('removeCabecera', 'Kilometros:'); $scope.notaPedido.bomba = false; $scope.notaPedido.kilometros = null; } }, function() { } ); } }; $scope.seleccionarMoneda = function() { if(validarNotaRemitada()) { 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.seleccionarObservaciones = function() { focaModalService .prompt('Ingrese observaciones', $scope.notaPedido.observaciones, true) .then(function(observaciones) { $scope.notaPedido.observaciones = observaciones; }); }; $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) { var articulosTablaTemp = $scope.notaPedido.articulosNotaPedido; for(var i = 0; i < articulosTablaTemp.length; i++) { articulosTablaTemp[i].precio = articulosTablaTemp[i].precio * $scope.notaPedido.cotizacion.VENDEDOR; articulosTablaTemp[i].precio = articulosTablaTemp[i].precio / cotizacion.VENDEDOR; } $scope.notaPedido.articulosNotaPedido = articulosTablaTemp; $scope.notaPedido.cotizacion = cotizacion; $scope.notaPedido.cotizacion.moneda = moneda; if(moneda.DETALLE === 'PESOS ARGENTINOS') { $scope.$broadcast('removeCabecera', 'Moneda:'); $scope.$broadcast('removeCabecera', 'Fecha cotizacion:'); $scope.$broadcast('removeCabecera', 'Cotizacion:'); } else { $scope.$broadcast('addCabecera', { label: 'Moneda:', valor: moneda.DETALLE }); $scope.$broadcast('addCabecera', { label: 'Fecha cotizacion:', valor: $filter('date')(cotizacion.FECHA, 'dd/MM/yyyy') }); $scope.$broadcast('addCabecera', { label: 'Cotizacion:', valor: $filter('number')(cotizacion.VENDEDOR, '2') }); } }, function() { } ); }; $scope.agregarATabla = function(key) { if(key === 13) { if($scope.articuloACargar.cantidad === undefined || $scope.articuloACargar.cantidad === 0 || $scope.articuloACargar.cantidad === null ) { focaModalService.alert('El valor debe ser al menos 1'); return; } delete $scope.articuloACargar.sectorCodigo; $scope.notaPedido.articulosNotaPedido.push($scope.articuloACargar); $scope.cargando = true; } }; $scope.quitarArticulo = function(key) { $scope.notaPedido.articulosNotaPedido.splice(key, 1); }; $scope.editarArticulo = function(key, articulo) { if(key === 13) { if(articulo.cantidad === null || articulo.cantidad === 0 || articulo.cantidad === undefined) { focaModalService.alert('El valor debe ser al menos 1'); return; } articulo.editCantidad = false; articulo.editPrecio = false; } }; $scope.cambioEdit = function(articulo, propiedad) { if(propiedad === 'cantidad') { articulo.editCantidad = true; } else if(propiedad === 'precio') { articulo.editPrecio = true; } }; $scope.resetFilter = function() { $scope.articuloACargar = {}; $scope.cargando = true; }; //Recibe aviso si el teclado está en uso $rootScope.$on('usarTeclado', function(event, data) { if(data) { $scope.mostrarTeclado = true; return; } $scope.mostrarTeclado = false; }); $scope.selectFocus = function($event) { // Si el teclado esta en uso no selecciona el valor if($scope.mostrarTeclado) { return; } $event.target.select(); }; $scope.salir = function() { $location.path('/'); }; $scope.parsearATexto = function(articulo) { articulo.cantidad = parseFloat(articulo.cantidad); articulo.precio = parseFloat(articulo.precio); }; function setearNotaPedido(notaPedido) { //añado cabeceras $scope.notaPedido = notaPedido; $scope.$broadcast('removeCabecera', 'Bomba:'); $scope.$broadcast('removeCabecera', 'Kilometros:'); var cabeceras = []; if (notaPedido.cotizacion) { cabeceras.push({ label: 'Moneda:', valor: notaPedido.cotizacion.moneda.DETALLE }); cabeceras.push({ label: 'Fecha cotizacion:', valor: $filter('date')(notaPedido.cotizacion.FECHA, 'dd/MM/yyyy') }); cabeceras.push({ label: 'Cotizacion:', valor: $filter('number')(notaPedido.cotizacion.VENDEDOR, '2') }); } if (notaPedido.cliente.COD) { cabeceras.push({ label: 'Cliente:', valor: notaPedido.cliente.NOM }); cabeceras.push({ label: 'Domicilio:', valor: notaPedido.domicilioStamp }); } if (notaPedido.vendedor.NUM) { cabeceras.push({ label: 'Vendedor:', valor: $filter('rellenarDigitos')(notaPedido.vendedor.NUM, 3) + ' - ' + notaPedido.vendedor.NOM }); } if (notaPedido.proveedor.COD) { cabeceras.push({ label: 'Proveedor:', valor: $filter('rellenarDigitos')(notaPedido.proveedor.COD, 5) + ' - ' + notaPedido.proveedor.NOM }); } if (notaPedido.notaPedidoPlazo) { cabeceras.push({ label: 'Precios y condiciones:', valor: valorPrecioCondicion() + ' ' + notaPedidoBusinessService .plazoToString(notaPedido.notaPedidoPlazo) }); } if (notaPedido.flete !== undefined) { cabeceras.push({ label: 'Flete:', valor: notaPedido.fob === 1 ? 'FOB' : ( notaPedido.flete === 1 ? 'Si' : 'No') }); } function valorPrecioCondicion() { if(notaPedido.idPrecioCondicion > 0) { return notaPedido.precioCondicion.nombre; } else { return 'Ingreso Manual'; } } if(notaPedido.flete === 1) { var cabeceraBomba = { label: 'Bomba:', valor: notaPedido.bomba === 1 ? 'Si' : 'No' }; if(notaPedido.kilometros) { var cabeceraKilometros = { label: 'Kilometros:', valor: notaPedido.kilometros }; cabeceras.push(cabeceraKilometros); } cabeceras.push(cabeceraBomba); } notaPedidoBusinessService.calcularArticulos($scope.notaPedido.articulosNotaPedido, notaPedido.cotizacion.VENDEDOR); if(notaPedido.idPrecioCondicion > 0) { $scope.idLista = notaPedido.precioCondicion.idListaPrecio; } else { $scope.idLista = -1; } $scope.puntoVenta = $filter('rellenarDigitos')( notaPedido.sucursal, 4 ); $scope.comprobante = $filter('rellenarDigitos')( notaPedido.numeroNotaPedido, 8 ); $scope.notaPedido.notaPedidoPuntoDescarga = formatearPuntosDescarga(notaPedido.notaPedidoPuntoDescarga || []); addArrayCabecera(cabeceras); } function addArrayCabecera(array) { for(var i = 0; i < array.length; i++) { $scope.$broadcast('addCabecera', { label: array[i].label, valor: array[i].valor }); } } function validarNotaRemitada() { if(!$scope.notaPedido.idRemito) { return true; }else{ focaModalService.alert('No se puede editar una nota de pedido remitada'); return false; } } function formatearPuntosDescarga(puntosDescarga) { var result = []; puntosDescarga.forEach(function(el) { var puntoDescarga = result.filter(function(resultPunto) { return resultPunto.id === el.idPuntoDescarga; }); if (puntoDescarga.length) { puntoDescarga[0].articulosAgregados.push({ cantidad: el.cantidad, descripcion: el.producto.descripcion, id: el.producto.id }); } else { result.push({ id: el.puntoDescarga.id, id_cliente: el.puntoDescarga.id_cliente, id_da_config_0: el.puntoDescarga.id_da_config_0, latitud: el.puntoDescarga.latitud, longitud: el.puntoDescarga.longitud, descripcion: el.puntoDescarga.descripcion, articulosAgregados: [ { cantidad: el.cantidad, descripcion: el.producto.descripcion, id: el.producto.id } ] }); } }); return result; } function salir() { var confirmacion = false; if (!angular.equals($scope.notaPedido, $scope.inicial)) { confirmacion = true; } if (confirmacion) { focaModalService.confirm( '¿Está seguro de que desea salir? Se perderán todos los datos cargados.' ).then(function(data) { if (data) { $location.path('/'); } }); } else { $location.path('/'); } } function getLSNotaPedido() { var notaPedido = JSON.parse($localStorage.notaPedido || null); if (notaPedido) { setearNotaPedido(notaPedido); delete $localStorage.notaPedido; } } } ]);