public void ProcesarDatos(string idProduct, string idFiltro, string filtro, DataTable tableContent, DataTable tableData, bool moneda) { /*Si la elección del cliente fue en soles la variable "moneda" debería estar en true, en caso contrarío * se le asigna el valor de falso lo cual significa que está en dólares. */ string npu; string ncosto; if (moneda) //soles { npu = "e"; ncosto = "g"; } else //dolares { npu = "h"; ncosto = "i"; } #region CalculosMatematicos try { descripcion = tableData.AsEnumerable().Where(x => x.Field <string>("a").Trim() == idProduct). Where(x => x.Field <string>(filtro).Trim() == idFiltro).Select(x => x.Field <string>("b")).FirstOrDefault(); } catch (Exception) { descripcion = ""; } try { medida = tableData.AsEnumerable().Where(x => x.Field <string>("a").Trim() == idProduct). Where(x => x.Field <string>(filtro).Trim() == idFiltro).Select(x => x.Field <string>("d")).FirstOrDefault(); } catch (Exception) { medida = ""; } try { totalUnidades = tableData.AsEnumerable().Where(x => x.Field <string>("a").Trim() == idProduct). Where(x => x.Field <string>(filtro).Trim() == idFiltro).Select(x => x.Field <double>("c")).Sum(); } catch (Exception) { totalUnidades = 0; } try { totalPuVentas = tableData.AsEnumerable().Where(x => x.Field <string>("a").Trim() == idProduct). Where(x => x.Field <string>(filtro).Trim() == idFiltro).Select( (x => x.Field <double>("c") * (x.Field <double>(npu) / (1 + (x.Field <double>("f") / 100))))).Sum(); } catch (Exception) { totalPuVentas = 0; } try { totalPuCosto = tableData.AsEnumerable().Where(x => x.Field <string>("a").Trim() == idProduct). Where(x => x.Field <string>(filtro).Trim() == idFiltro).Select( (x => x.Field <double>(ncosto) * x.Field <double>("c"))).Sum(); } catch (Exception) { totalPuCosto = 0; } Convert.ToDecimal(totalUnidades); Convert.ToDecimal(totalPuVentas); Convert.ToDecimal(totalPuCosto); if (totalUnidades != 0) { PuVentas = Convert.ToDecimal(totalPuVentas) / Convert.ToDecimal(totalUnidades); PuCosto = Convert.ToDecimal(totalPuCosto) / Convert.ToDecimal(totalUnidades); } else { PuVentas = 0; PuCosto = 0; } muUnitario = PuVentas - PuCosto; if (PuCosto != 0) { mu = (muUnitario / PuCosto) * 100; } else { mu = 0; } montoVentas = PuVentas * Convert.ToDecimal(totalUnidades); montoCosto = PuCosto * Convert.ToDecimal(totalUnidades); margenUtil = montoVentas - montoCosto; #endregion DataRow row; row = tableContent.NewRow(); row["C"] = idProduct.Trim(); row["D"] = Convert.ToString(descripcion); row["M"] = Convert.ToString(medida); row["U"] = Math.Round(Convert.ToDecimal(totalUnidades), 2); row["PV"] = cal.CompleteZeros(Math.Round(Convert.ToDecimal(PuVentas), 4)); row["PC"] = cal.CompleteZeros(Math.Round(Convert.ToDecimal(PuCosto), 4)); row["MUU"] = cal.CompleteZeros(Math.Round(Convert.ToDecimal(muUnitario), 4)); row["MV"] = cal.CompleteZeros(Math.Round(Convert.ToDecimal(montoVentas), 4)); row["MC"] = cal.CompleteZeros(Math.Round(Convert.ToDecimal(montoCosto), 4)); row["MU"] = cal.CompleteZeros(Math.Round(Convert.ToDecimal(margenUtil), 4)); tableContent.Rows.Add(row); }