public AuditFileMasterFiles masterFiles() { var groupListProduct = _productSalesController.ListALL(). Where(c => c.DataCadastro.Date >= dataInicio.Date && c.DataCadastro.Date <= dataFinal.Date && c.Fatura.TipoDocumento.SalesInvoices).ToList(). GroupBy(c => c.ProductId); var groupListProductCustomer = _productSalesController.ListALL(). Where(c => c.DataCadastro.Date >= dataInicio.Date && c.DataCadastro.Date <= dataFinal.Date && c.Fatura.TipoDocumento.SalesInvoices).ToList(). GroupBy(c => c.Fatura.ClienteId); AuditFileMasterFilesProduct[] auditFileProduct = new AuditFileMasterFilesProduct[groupListProduct.Count()]; AuditFileMasterFilesCustomer[] auditFileCustomer = new AuditFileMasterFilesCustomer[groupListProductCustomer.Count()]; AuditFileMasterFilesTaxTableEntry[] auditTaxTable; /* foreach (var item in groupList) * { * * Console.WriteLine(item.Key); * foreach (var prodSale in item) * { * Console.WriteLine(" * - " + prodSale.Product.Descricao); * } * }*/ List <TabelaImposto> tabela_taxas = new List <TabelaImposto>(); int i = 0; string imposto; string impostoType; string impostoDescricao; foreach (var item in groupListProduct) { ProductSales p = item.ToList()[0]; string cod = p.Product.Codigo_Barra != "" && p.Product.Codigo_Barra != null ? p.Product.Codigo_Barra : p.Product.Id.ToString(); auditFileProduct[i] = new AuditFileMasterFilesProduct() { ProductCode = p.Product.Id.ToString(), ProductDescription = p.Descricao_Produto, ProductType = p.Product.TipoArtigo.Tipo, ProductNumberCode = cod, ProductGroup = p.Product.Categoria.Categoria }; imposto = p.Product.CodigoTaxa.Code; impostoType = p.Product.CodigoTaxaId == 3 ? "NS" : "IVA"; impostoDescricao = p.Product.CodigoTaxa.Descricao; tabela_taxas.Add(new TabelaImposto() { TaxType = impostoType, TaxCode = imposto, TaxCodeDescription = impostoDescricao, Taxa = decimal.Parse(p.Taxa.ToString()), }); i++; } var groupTabela_taxas = tabela_taxas.GroupBy(c => c.TaxCode).ToList(); auditTaxTable = new AuditFileMasterFilesTaxTableEntry[groupTabela_taxas.Count()]; i = 0; foreach (var item in groupTabela_taxas) { auditTaxTable[i] = new AuditFileMasterFilesTaxTableEntry() { TaxType = item.ToList()[0].TaxType, TaxCountryRegion = "AO", TaxCode = item.ToList()[0].TaxCode, TaxPercentage = item.ToList()[0].Taxa.ToString("F3").Replace(",", "."), Description = item.ToList()[0].TaxCodeDescription, }; i++; } List <Customer> _customers = new List <Customer>(); i = 0; int contFinal = 0; foreach (var item in groupListProductCustomer) { Customer c = item.ToList()[0].Fatura.Cliente; if ((c.Nif.Contains("9999999") || c.Nif == string.Empty)) { contFinal++; } bool isOnlist = false; foreach (var itemCustomer in _customers) { if (itemCustomer.Id == c.Id) { isOnlist = true; } } if (!isOnlist) { if ((c.Nif.Contains("9999999") || c.Nif == string.Empty) && contFinal == 1) { _customers.Add(finalCustomer); } else if (!c.Nif.Contains("9999999") && c.Nif.Length > 6) { _customers.Add(c); } else { isOnlist = true; } } if (!isOnlist) { bool finalCl = c.Nif == string.Empty || c.Nif.Contains("9999999"); string endereco = c.Endereco != null && c.Endereco != "" ? c.Endereco : "Nao definido"; string contaCorrente = c.Conta_Corrente != null && c.Conta_Corrente != string.Empty ? c.Conta_Corrente : "Desconhecido"; string siglaPais = new CountryController().getOne(c.Cidade.PaisId).Sigla; auditFileCustomer[i] = new AuditFileMasterFilesCustomer() { CustomerID = finalCl ? finalCustomer.Id.ToString() : c.Id.ToString(), CustomerTaxID = finalCl ? finalCustomer.Nif : c.Nif, CompanyName = finalCl ? finalCustomer.Cliente : c.Cliente, SelfBillingIndicator = "0", AccountID = finalCl ? finalCustomer.Conta_Corrente : contaCorrente, BillingAddress = new AuditFileMasterFilesCustomerBillingAddress() { City = finalCl ? finalCustomer.Cidade.Cidade : c.Cidade.Cidade, Country = finalCl ? "AO" : siglaPais, PostalCode = finalCl ? finalCustomer.Postal : c.Postal, AddressDetail = finalCl ? "Nao definido" : endereco, }, }; i++; } } return(new AuditFileMasterFiles() { Product = auditFileProduct, Customer = auditFileCustomer, TaxTable = auditTaxTable, }); }