public static object sendTable(List <Dictionary <string, string> > dataTabla, string Nperiod, Decimal total, string pestania, float inflacion) { //Here I want to iterate the objects int a = dataTabla.Count(); string json = JsonConvert.SerializeObject(dataTabla); // OBTENEMOS LOS DATOS DE LA TABLA COSTOS DEL PERIODO SELECCIONADO string id_gastos = System.Guid.NewGuid().ToString("D");/**** crear los id en random formato string***/ string id_proyect = (string)System.Web.HttpContext.Current.Session["ID_Proyecto"]; string id_periodo = Nperiod; string tabl_json = json; Decimal tabl_total = total; try { // GUARDAMOS A LA BASE DE DATOS var db = new Entidades(); switch (pestania) { case "NGastos1": //Para guardar los datos por primera vez var NuevoGasto = new Gastos_Pro(); NuevoGasto.ID_Gastos_pro = id_gastos; NuevoGasto.ID_Proyecto = id_proyect; NuevoGasto.ID_Periodo = id_periodo; NuevoGasto.Produccion = tabl_json; NuevoGasto.Ventas = ""; NuevoGasto.Financiamiento = ""; NuevoGasto.Admon = ""; NuevoGasto.Total = tabl_total; db.Gastos_Pro.Add(NuevoGasto); //Guardo el id que se creo System.Web.HttpContext.Current.Session["id_gasto"] = id_gastos; break; default: //Para el update if (System.Web.HttpContext.Current.Session["id_gasto"] != null) { string idGasto = (string)System.Web.HttpContext.Current.Session["id_gasto"]; // Realizamos la consulta var gastos = db.Gastos_Pro.Where(costo => costo.ID_Gastos_pro == idGasto); // Modificamos los objetos que consideremos oportunos foreach (var gasto in gastos) { System.Diagnostics.Debug.WriteLine("case 2---->pestania-->" + pestania); if (pestania == "NGastos2") { gasto.Ventas = tabl_json; gasto.Total = gasto.Total + tabl_total; } else if (pestania == "NGastos3") { gasto.Admon = tabl_json; gasto.Total = gasto.Total + tabl_total; } else if (pestania == "NGastos4") { gasto.Financiamiento = tabl_json; gasto.Total = gasto.Total + tabl_total; float val_inflacion = inflacion; Proyeccion(db, id_proyect, id_periodo, gasto.Total, val_inflacion); agregar_avance(id_proyect); } } } break; } db.SaveChanges(); return("succes"); } // Most specific: catch (ArgumentNullException e) { Console.WriteLine("{0} First exception caught.", e); return(e); } }
private static object Proyeccion(Entidades db, string id_proyect, string id_periodo, decimal tabl_total, float infla) { var inflacion = (infla / 100) + 1; //BUSCAMOS CUANTOS PERIODOS TIENE var httpContext = HttpContext.Current; string id_user = httpContext.User.Identity.GetUserId(); var consulta = db.Proyecto.Where(Proyect => Proyect.ID_Proyecto == id_proyect); int nperiodos = 0; var claveperiodo = ""; foreach (Proyecto Proyect in consulta) { // para obtener solo el A(anual) M(mensual) claveperiodo = (Proyect.ID_Periodo).Substring((Proyect.ID_Periodo).Length - 1, 1); // para obtener solo el numero de periodo nperiodos = Int32.Parse((Proyect.ID_Periodo).Substring(0, ((Proyect.ID_Periodo).Length) - 1)); } var query = db.Gastos_Pro.Where(Gastos => Gastos.ID_Proyecto == id_proyect && Gastos.ID_Periodo == id_periodo); List <string> result_query = new List <string>(); foreach (var Result in query) { result_query.Add(Result.Produccion); result_query.Add(Result.Ventas); result_query.Add(Result.Admon); result_query.Add(Result.Financiamiento); } Decimal total = tabl_total; var arreglo = result_query; for (int i = 2; i <= nperiodos; i++) { string clave = (i - 1).ToString() + claveperiodo; List <string> campos = new List <string>(); foreach (var prime in arreglo) { List <Dictionary <string, string> > obj = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Dictionary <string, string> > >(prime); foreach (Dictionary <string, string> lst in obj) { decimal gastoU = decimal.Parse(lst["$ Gasto Unitario"]) * (decimal)(inflacion); decimal gastoT = decimal.Parse(lst["$ Gasto Total"]) * (decimal)(inflacion); lst["$ Gasto Unitario"] = gastoU.ToString(); lst["$ Gasto Total"] = gastoT.ToString(); } campos.Add(JsonConvert.SerializeObject(obj)); } total = total * (decimal)inflacion; //GUARDAMOS string id_costos = System.Guid.NewGuid().ToString("D"); var NuevoGasto = new Gastos_Pro(); NuevoGasto.ID_Gastos_pro = id_costos; NuevoGasto.ID_Proyecto = id_proyect; NuevoGasto.ID_Periodo = i + claveperiodo; NuevoGasto.Produccion = campos[0]; NuevoGasto.Ventas = campos[1]; NuevoGasto.Admon = campos[2]; NuevoGasto.Financiamiento = campos[3]; NuevoGasto.Total = total; db.Gastos_Pro.Add(NuevoGasto); arreglo = campos; } return("succes"); }