public JsonResult Delete(int id) { GastoCreateModel gasto = new GastoCreateModel(); using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { var fromDbGasto = DB.t_Gastos.Where(g => g.id_gasto == id).FirstOrDefault(); if (fromDbGasto == null) { Response.StatusCode = 404; return(Json(new ResponseResult() { Success = false, Data = "Not found" })); } else { DB.t_Gastos.Remove(fromDbGasto); DB.SaveChanges(); } } return(Json(gasto)); }
public JsonResult Create(GastoCreateModel model) { if (ModelState.IsValid) { using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { var presInfo = (from p in DB.t_presupuesto join c in DB.t_cuenta_pres on p.id_pres equals c.id_pres where (p.Desde <= model.fecha && p.Hasta >= model.fecha) && c.id_cat == model.categoria select new { Desde = p.Desde, Hasta = p.Hasta, id_pres = p.id_pres, id_cuent = c.id_cuent, id_cat = c.id_cat, Limite = c.Limite }).FirstOrDefault(); if (presInfo is null) { return(Json(new ResponseResult() { Success = false, Data = new List <ValidationResult>() { new ValidationResult() { Key = "id_gasto", Errors = new ModelErrorCollection() { "No existe una cuenta presupuestaria con esta categoría de gasto que abarque esta fecha" } } } } )); } var gastosCat = DB.t_Gastos.Where(g => (g.fecha >= presInfo.Desde && g.fecha <= presInfo.Hasta) && g.id_cat == model.categoria).ToList(); decimal gastado = (gastosCat.Count == 0 ? 0 : gastosCat.Sum(g => g.valor)); decimal disponible = presInfo.Limite - gastado; if (model.valor > disponible) { return(Json(new ResponseResult() { Success = false, Data = new List <ValidationResult>() { new ValidationResult() { Key = "id_gasto", Errors = new ModelErrorCollection() { "El valor de este gasto excede el disponible de la cuenta presupuestaria" } } } } )); } DB.t_Gastos.Add(new Data.t_Gastos() { id_cat = model.categoria, fecha = model.fecha, justificacion = model.justificacion, valor = model.valor }); DB.SaveChanges(); } return(Json(new ResponseResult() { Success = true, Data = Validations.GetErrors(ModelState) })); } else { //Reject return(Json(new ResponseResult() { Success = false, Data = Validations.GetErrors(ModelState) })); } }