angular.module('focaCrearNotaPedido') .controller('notaPedidoCtrl', [ '$scope', '$uibModal', '$location', '$filter', 'crearNotaPedidoService', 'focaModalService', function( $scope, $uibModal, $location, $filter, crearNotaPedidoService, focaModalService ) { $scope.botonera = [ {texto: 'Vendedor', accion: function() {$scope.seleccionarVendedor();}}, {texto: 'Cliente', accion: function() {$scope.seleccionarCliente();}}, {texto: 'Proveedor', accion: function() {$scope.seleccionarProveedor();}}, {texto: 'Moneda', accion: function() {$scope.abrirModalMoneda();}}, { texto: 'Precios y condiciones', accion: function() {$scope.abrirModalListaPrecio();}}, {texto: 'Flete', accion: function() {$scope.abrirModalFlete();}}, {texto: '', accion: function() {}}, {texto: '', accion: function() {}} ]; $scope.datepickerAbierto = false; $scope.show = false; $scope.cargando = true; $scope.dateOptions = { maxDate: new Date(), minDate: new Date(2010, 0, 1) }; $scope.crearObjetoNotaPedido = function() { $scope.notaPedido = { vendedor: {}, cliente: {}, proveedor: {}, domicilio: {dom: ''}, moneda: {}, cotizacion: {} }; }; //Trabajo con la cotización más reciente, por eso uso siempre la primera '[0]' $scope.obtenerMonedaPorDefecto = function() { crearNotaPedidoService.getCotizacionByIdMoneda(1).then(function(res) { monedaPorDefecto = { id: res.data[0].ID, detalle: res.data[0].DETALLE, simbolo: res.data[0].SIMBOLO, cotizaciones: res.data[0].cotizaciones }; addCabecera('Moneda:', monedaPorDefecto.detalle); addCabecera('Fecha cotizacion:', new Date(monedaPorDefecto.cotizaciones[0].FECHA).toLocaleDateString()); addCabecera('Cotizacion:', monedaPorDefecto.cotizaciones[0].COTIZACION); $scope.notaPedido.moneda = monedaPorDefecto; $scope.notaPedido.cotizacion.id = monedaPorDefecto.cotizaciones[0].ID; }); }; var monedaPorDefecto; $scope.crearObjetoNotaPedido(); $scope.obtenerMonedaPorDefecto(); $scope.cabecera = []; $scope.showCabecera = true; $scope.now = new Date(); $scope.puntoVenta = Math.round(Math.random() * 10000); $scope.comprobante = Math.round(Math.random() * 1000000); $scope.articulosTabla = []; $scope.idLista = undefined; //La pantalla solo se usa para cargar pedidos //var notaPedidoTemp = crearNotaPedidoService.getNotaPedido(); crearNotaPedidoService.getPrecioCondicion().then( function(res) { $scope.precioCondiciones = res.data; } ); //La pantalla solo se usa para cargar pedidos // if (notaPedidoTemp !== undefined) { // notaPedidoTemp.fechaCarga = new Date(notaPedidoTemp.fechaCarga); // $scope.notaPedido = notaPedidoTemp; // $scope.notaPedido.flete = ($scope.notaPedido.flete).toString(); // $scope.notaPedido.bomba = ($scope.notaPedido.bomba).toString(); // $scope.idLista = $scope.notaPedido.precioCondicion; // crearNotaPedidoService // .getArticulosByIdNotaPedido($scope.notaPedido.id).then( // function(res) { // $scope.articulosTabla = res.data; // } // ); //TODO DOMICILIOS QUE SE CARGAN AL EDITAR NOTA DE PEDIDO //(NO REQUERIDO EN ESTA VERSION) // crearNotaPedidoService.getDomiciliosByIdNotaPedido($scope.notaPedido.id).then( // function(res) { // $scope.notaPedido.domicilio = res.data; // } // ); // } else { // $scope.notaPedido.fechaCarga = new Date(); // $scope.notaPedido.bomba = '0'; // $scope.notaPedido.flete = '0'; // $scope.idLista = undefined; // } //TO DO - FUNCIONES PARA MULTIPLES DOMICILIOS NO IMPLEMENTADAS EN ESTA DEMO // $scope.addNewDom = function() { // $scope.notaPedido.domicilio.push({ 'id': 0 }); // }; // $scope.removeNewChoice = function(choice) { // if ($scope.notaPedido.domicilio.length > 1) { // $scope.notaPedido.domicilio.splice($scope.notaPedido.domicilio.findIndex( // function(c) { // return c.$$hashKey === choice.$$hashKey; // } // ), 1); // } // }; $scope.crearNotaPedido = function() { if(!$scope.notaPedido.vendedor.codigo) { focaModalService.alert('Ingrese Vendedor'); return; } else if(!$scope.notaPedido.cliente.id) { focaModalService.alert('Ingrese Cliente'); return; } else if(!$scope.notaPedido.proveedor.codigo) { focaModalService.alert('Ingrese Proveedor'); return; } else if(!$scope.notaPedido.moneda.id) { focaModalService.alert('Ingrese Moneda'); return; } else if(!$scope.notaPedido.cotizacion.id) { focaModalService.alert('Ingrese Cotización'); return; } else if(!$scope.plazosPagos) { focaModalService.alert('Ingrese Precios y Condiciones'); return; } else if(!$scope.notaPedido.flete) { focaModalService.alert('Ingrese Flete'); return; } else if(!$scope.notaPedido.domicilio.id) { focaModalService.aler('Ingrese Domicilio'); return; } else if($scope.articulosTabla.length === 0) { focaModalService.alert('Debe cargar almenos un articulo'); return; } var date = new Date(); var notaPedido = { id: 0, fechaCarga: new Date(date.getTime() - (date.getTimezoneOffset() * 60000)) .toISOString().slice(0, 19).replace('T', ' '), idVendedor: $scope.notaPedido.vendedor.codigo, idCliente: $scope.notaPedido.cliente.id, idProveedor: $scope.notaPedido.proveedor.codigo, idDomicilio: $scope.notaPedido.domicilio.id, idCotizacion: $scope.notaPedido.cotizacion.id, flete: parseInt($scope.notaPedido.flete), fob: parseInt($scope.notaPedido.fob), bomba: parseInt($scope.notaPedido.bomba), kilometros: $scope.notaPedido.kilometros, total: $scope.getTotal() }; crearNotaPedidoService.crearNotaPedido(notaPedido).then( function(data) { var articulosNotaPedido = $scope.articulosTabla; for(var i = 0; i < articulosNotaPedido.length; i++) { delete articulosNotaPedido[i].editCantidad; delete articulosNotaPedido[i].editPrecio; articulosNotaPedido[i].idNotaPedido = data.data.id; crearNotaPedidoService .crearArticulosParaNotaPedido(articulosNotaPedido[i]); } var plazos = $scope.plazosPagos; for(var j = 0; j < plazos.length; j++) { var json = { idPedido: data.data.id, dias: plazos[j].dias }; crearNotaPedidoService.crearPlazosParaNotaPedido(json); } focaModalService.alert('Nota pedido creada'); $scope.obtenerMonedaPorDefecto(); $scope.crearObjetoNotaPedido(); $scope.cabecera = []; $scope.articulosTabla = []; } ); }; $scope.seleccionarArticulo = 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: { idLista: function() { return $scope.idLista; } }, 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.articulosTabla.length + 1, nombre: producto.descripcion, precio: parseFloat(producto.precio.toFixed(2)), costoUnitario: producto.costo, editCantidad: false, editPrecio: false }; $scope.articuloACargar = newArt; $scope.cargando = false; }, function() { // funcion ejecutada cuando se cancela el modal } ); }; $scope.seleccionarVendedor = function() { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Vendedores', templateUrl: 'modal-vendedores.html', controller: 'modalVendedoresCtrl', size: 'lg' } ); modalInstance.result.then( function(vendedor) { addCabecera('Vendedor:', vendedor.NomVen); $scope.notaPedido.vendedor.codigo = vendedor.CodVen; }, function() { } ); }; $scope.seleccionarProveedor = function() { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Proveedor', templateUrl: 'modal-proveedor.html', controller: 'focaModalProveedorCtrl', size: 'lg' } ); modalInstance.result.then( function(proveedor) { $scope.notaPedido.proveedor.codigo = proveedor.COD; addCabecera('Proveedor:', proveedor.NOM); }, function() { } ); }; $scope.seleccionarCliente = function() { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Cliente', templateUrl: 'foca-busqueda-cliente-modal.html', controller: 'focaBusquedaClienteModalController', size: 'lg' } ); modalInstance.result.then( function(cliente) { crearNotaPedidoService.getDomiciliosByIdCliente(cliente.cod).then( function(data) { if(data.data.length === 0){ focaModalService .alert('El cliente no tienen domicilios de entrega') .then( function() { $scope.seleccionarCliente(); } ); return; } $scope.abrirModalDomicilios(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; }}, size: 'lg', } ); modalInstanceDomicilio.result.then( function(domicilio) { $scope.notaPedido.domicilio.id = domicilio.id; $scope.notaPedido.cliente.id = cliente.cod; addCabecera('Cliente:', cliente.nom); addCabecera('Domicilio:', domicilio.dom); }, function() { $scope.seleccionarCliente(); return; } ); }; $scope.mostrarFichaCliente = function() { $uibModal.open( { ariaLabelledBy: 'Datos del Cliente', templateUrl: 'foca-crear-nota-pedido-ficha-cliente.html', controller: 'focaCrearNotaPedidoFichaClienteController', size: 'lg' } ); }; $scope.getTotal = function() { var total = 0; var arrayTempArticulos = $scope.articulosTabla; 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.abrirModalListaPrecio = function() { 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.plazosPagos = precioCondicion.plazoPago; $scope.idLista = precioCondicion.idListaPrecio; for(var i = 0; i < precioCondicion.plazoPago.length; i++) { plazosConcat += precioCondicion.plazoPago[i].dias + ' '; } cabecera = precioCondicion.nombre + ' ' + plazosConcat.trim(); } else { //Cuando se ingresan los plazos manualmente $scope.idLista = -1; //-1, el modal productos busca todos los productos $scope.plazosPagos = precioCondicion; for(var j = 0; j < precioCondicion.length; j++) { plazosConcat += precioCondicion[j].dias + ' '; } cabecera = 'Ingreso manual ' + plazosConcat.trim(); } $scope.articulosTabla = []; addCabecera('Precios y condiciones:', cabecera); }, function() { } ); }; $scope.abrirModalFlete = function() { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Flete', templateUrl: 'modal-flete.html', controller: 'focaModalFleteController', size: 'lg', resolve: { parametrosFlete: function() { return { flete: $scope.notaPedido.flete, bomba: $scope.notaPedido.bomba, 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; addCabecera('Flete:', datos.flete); if(datos.flete === '1') { addCabecera('Bomba:', datos.bomba); addCabecera('Kilometros:', datos.kilometros); } else { removeCabecera('Bomba:'); removeCabecera('Kilometros:'); } }, function() { } ); }; $scope.abrirModalMoneda = function() { var modalInstance = $uibModal.open( { ariaLabelledBy: 'Busqueda de Moneda', templateUrl: 'modal-moneda.html', controller: 'focaModalMonedaController', size: 'lg' } ); modalInstance.result.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.notaPedido.moneda = { id: moneda.ID, detalle: moneda.DETALLE, simbolo: moneda.SIMBOLO }; $scope.notaPedido.cotizacion = { id: cotizacion.ID, cotizacion: cotizacion.COTIZACION }; addCabecera('Moneda:', moneda.DETALLE); addCabecera( 'Fecha cotizacion:', $filter('date')(cotizacion.FECHA, 'dd/MM/yyyy') ); addCabecera('Cotizacion:', cotizacion.COTIZACION); }, 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.articulosTabla.push($scope.articuloACargar); $scope.cargando = true; } }; $scope.quitarArticulo = function(key) { $scope.articulosTabla.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.limpiarFlete = function() { $scope.notaPedido.fleteNombre = ''; $scope.notaPedido.chofer = ''; $scope.notaPedido.vehiculo = ''; $scope.notaPedido.kilometros = ''; $scope.notaPedido.costoUnitarioKmFlete = ''; $scope.choferes = ''; $scope.vehiculos = ''; }; $scope.limpiarPantalla = function() { $scope.limpiarFlete(); $scope.notaPedido.flete = '0'; $scope.notaPedido.bomba = '0'; $scope.notaPedido.precioCondicion = ''; $scope.articulosTabla = []; $scope.notaPedido.vendedor.nombre = ''; $scope.notaPedido.cliente = {nombre: ''}; $scope.notaPedido.domicilio = {dom: ''}; $scope.domiciliosCliente = []; }; $scope.resetFilter = function() { $scope.articuloACargar = {}; $scope.cargando = true; }; $scope.selectFocus = function($event) { $event.target.select(); }; $scope.salir = function() { $location.path('/'); }; function addCabecera(label, valor) { var propiedad = $filter('filter')($scope.cabecera, {label: label}, true); if(propiedad.length === 1) { propiedad[0].valor = valor; } else { $scope.cabecera.push({label: label, valor: valor}); } } function removeCabecera(label) { var propiedad = $filter('filter')($scope.cabecera, {label: label}, true); if(propiedad.length === 1){ $scope.cabecera.splice($scope.cabecera.indexOf(propiedad[0]), 1); } } } ] ) .controller('notaPedidoListaCtrl', [ '$scope', 'crearNotaPedidoService', '$location', function($scope, crearNotaPedidoService, $location) { crearNotaPedidoService.obtenerNotaPedido().then(function(datos) { $scope.notaPedidos = datos.data; }); $scope.editar = function(notaPedido) { crearNotaPedidoService.setNotaPedido(notaPedido); $location.path('/venta-nota-pedido/abm/'); }; $scope.crearPedido = function() { crearNotaPedidoService.clearNotaPedido(); $location.path('/venta-nota-pedido/abm/'); }; } ]) .controller('focaCrearNotaPedidoFichaClienteController', [ '$scope', 'crearNotaPedidoService', '$location', function($scope, crearNotaPedidoService, $location) { crearNotaPedidoService.obtenerNotaPedido().then(function(datos) { $scope.notaPedidos = datos.data; }); $scope.editar = function(notaPedido) { crearNotaPedidoService.setNotaPedido(notaPedido); $location.path('/venta-nota-pedido/abm/'); }; $scope.crearPedido = function() { crearNotaPedidoService.clearNotaPedido(); $location.path('/venta-nota-pedido/abm/'); }; } ]);