public decimal?GetBalance(int?id_cat, DateTime?fecha) { if (id_cat == null || id_cat == 0 || fecha == null) { return(null); } 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 (c.id_cat == (int)id_cat) && (p.Desde <= fecha && p.Hasta >= fecha) select new { p.Desde, p.Hasta, Limite = c.Limite }).FirstOrDefault(); if (presInfo == null) { return(null); } var gastosCat = DB.t_Gastos.Where(g => (g.fecha >= presInfo.Desde && g.fecha <= presInfo.Hasta) && g.id_cat == id_cat).ToList(); decimal gastado = (gastosCat.Count == 0 ? 0 : gastosCat.Sum(g => g.valor)); decimal disponible = presInfo.Limite - gastado; return(disponible); } }
public JsonResult Delete(int id) { CuentaPres data = new CuentaPres(); using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { var fromDb = DB.t_cuenta_pres.Where(c => c.id_cuent == id).FirstOrDefault(); if (fromDb == null) { Response.StatusCode = 404; return(Json(new ResponseResult() { Success = false, Data = "Not found" })); } var pres = DB.t_presupuesto.Where(a => a.id_pres == fromDb.id_pres).FirstOrDefault(); if (DB.t_Gastos.Any(a => (a.fecha >= pres.Desde && a.fecha <= pres.Hasta) && a.id_cat == fromDb.id_cat)) { return(Json(new ResponseResult() { Success = false, Data = "No se puede eliminar la cuenta, debido a que tiene registros vinculados" })); } DB.t_cuenta_pres.Remove(fromDb); DB.SaveChanges(); } return(Json(new ResponseResult() { Success = true, Data = "Éxito" })); }
public JsonResult Create(CategoriaCreate model) { if (ModelState.IsValid) { //Save using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { DB.t_Categoria_gasto.Add(new Data.t_Categoria_gasto() { Nombre = model.Nombre }); DB.SaveChanges(); } return(Json(new ResponseResult() { Success = true, Data = Validations.GetErrors(ModelState) })); } else { //Reject return(Json(new ResponseResult() { Success = false, Data = Validations.GetErrors(ModelState) })); } }
public JsonResult Delete(int id) { using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { //Validando no hay registros relacionados al presupuesto if (DB.t_cuenta_pres.Any(c => c.id_pres == id)) { return(Json(new ResponseResult() { Success = false, Data = "No se puede eliminar el presupuesto, debido a que tiene registros vinculados" })); } var fromDbCat = DB.t_presupuesto.Where(c => c.id_pres == id).FirstOrDefault(); if (fromDbCat == null) { Response.StatusCode = 404; return(Json(new ResponseResult() { Success = false, Data = "Not found" })); } else { DB.t_presupuesto.Remove(fromDbCat); DB.SaveChanges(); } } return(Json(new ResponseResult() { Success = true, Data = "Éxito al eliminar" })); }
public JsonResult Get(int id) { CuentaPres data = new CuentaPres(); using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { var fromDbCat = DB.t_cuenta_pres.Where(c => c.id_cuent == id).FirstOrDefault(); if (fromDbCat == null) { Response.StatusCode = 404; return(Json(new ResponseResult() { Success = false, Data = "Not found" })); } else { data = new CuentaPres() { id_cuent = fromDbCat.id_cuent, id_pres = fromDbCat.id_pres, id_cat = fromDbCat.id_cat, Limite = fromDbCat.Limite, Categoria = DB.t_Categoria_gasto.Where(c => c.id_cat == fromDbCat.id_cat).FirstOrDefault().Nombre }; } } return(Json(data)); }
public JsonResult GetCurrent() { Presupuesto data = new Presupuesto(); using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { var date = DateTime.Now.Date; //var date = new DateTime(2020,12,30); var fromDb = DB.t_presupuesto.Where(p => p.Desde <= date && p.Hasta >= date).FirstOrDefault(); if (fromDb == null) { return(Json(new ResponseResult() { Success = false, Data = "Not found" })); } else { data = new Presupuesto() { id_pres = fromDb.id_pres, Nombre = fromDb.Nombre, Desde = fromDb.Desde, Hasta = fromDb.Hasta }; } } return(Json(new ResponseResult() { Success = true, Data = data })); }
public JsonResult Get(int id) { Presupuesto data = new Presupuesto(); using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { var fromDbCat = DB.t_presupuesto.Where(c => c.id_pres == id).FirstOrDefault(); if (fromDbCat == null) { Response.StatusCode = 404; return(Json(new ResponseResult() { Success = false, Data = "Not found" })); } else { data = new Presupuesto() { id_pres = fromDbCat.id_pres, Nombre = fromDbCat.Nombre, Desde = fromDbCat.Desde, Hasta = fromDbCat.Hasta }; } } return(Json(data)); }
public JsonResult Edit(Categoria model) { if (ModelState.IsValid) { //Save using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { Data.t_Categoria_gasto data = DB.t_Categoria_gasto.Where(c => c.id_cat == model.id_cat).FirstOrDefault(); data.id_cat = model.id_cat; data.Nombre = model.Nombre; DB.SaveChanges(); } return(Json(new ResponseResult() { Success = true, Data = Validations.GetErrors(ModelState) })); } else { //Reject return(Json(new ResponseResult() { Success = false, Data = Validations.GetErrors(ModelState) })); } }
public JsonResult Get(int id) { GastoModel gasto = new GastoModel(); 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 { gasto = new GastoModel() { id_gasto = fromDbGasto.id_gasto, categoria = fromDbGasto.id_cat, justificacion = fromDbGasto.justificacion, fecha = fromDbGasto.fecha, valor = fromDbGasto.valor }; } } return(Json(gasto)); }
public JsonResult GetCategorias() { List <Categoria> categorias = new List <Categoria>(); using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { categorias.AddRange(DB.t_Categoria_gasto.Select(c => new Categoria() { id_cat = c.id_cat, Nombre = c.Nombre })); } return(Json(categorias)); }
public string GetAll() { List <Categoria> data = new List <Categoria>(); using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { data.AddRange(DB.t_Categoria_gasto.Select(c => new Categoria() { id_cat = c.id_cat, Nombre = c.Nombre }).ToList()); } return(Newtonsoft.Json.JsonConvert.SerializeObject(data)); }
public JsonResult Edit(CuentaPres model) { if (ModelState.IsValid) { //Save using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { //Validando que solo exista una cuenta presupuestaria para esta categoria if (DB.t_cuenta_pres.Any(a => (a.id_cat == model.id_cat && a.id_pres == model.id_pres) && a.id_cuent != model.id_cuent)) { return(Json(new ResponseResult() { Success = false, Data = new List <ValidationResult>() { new ValidationResult() { Key = "id_cat", Errors = new ModelErrorCollection() { "Ya existe esta categoria de gasto en este presupuesto" } } } })); } Data.t_cuenta_pres data = DB.t_cuenta_pres.Where(c => c.id_cuent == model.id_cuent).FirstOrDefault(); data.id_pres = model.id_pres; data.id_cat = model.id_cat; data.Limite = model.Limite; DB.SaveChanges(); } return(Json(new ResponseResult() { Success = true, Data = Validations.GetErrors(ModelState) })); } else { //Reject return(Json(new ResponseResult() { Success = false, Data = Validations.GetErrors(ModelState) })); } }
public string GetAll() { List <Presupuesto> data = new List <Presupuesto>(); using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { data.AddRange(from p in DB.t_presupuesto select new Presupuesto() { id_pres = p.id_pres, Nombre = p.Nombre, Desde = p.Desde, Hasta = p.Hasta }); } return(Newtonsoft.Json.JsonConvert.SerializeObject(data.OrderBy(p => p.Desde))); }
public JsonResult GetResumenCuentas(int id_pres) { List <CuentaPresResumen> data = new List <CuentaPresResumen>(); using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { Data.t_presupuesto presupuesto = DB.t_presupuesto.Where(p => p.id_pres == id_pres).FirstOrDefault(); if (presupuesto == null) { Response.StatusCode = 404; return(Json(new ResponseResult { Success = false, Data = "Not Found!" })); } List <CuentaPres> cuentas = (from c in DB.t_cuenta_pres join ca in DB.t_Categoria_gasto on c.id_cat equals ca.id_cat where c.id_pres == id_pres select new CuentaPres() { id_cuent = c.id_cuent, id_pres = c.id_pres, id_cat = c.id_cat, Categoria = ca.Nombre, Limite = c.Limite }).ToList(); List <Data.t_Gastos> gastos = DB.t_Gastos.Where(g => g.fecha >= presupuesto.Desde && g.fecha <= presupuesto.Hasta).ToList(); foreach (var cuenta in cuentas) { decimal gastado = gastos.Where(g => g.id_cat == cuenta.id_cat)?.Sum(g => g.valor) ?? 0; data.Add(new CuentaPresResumen() { id_cuent = cuenta.id_cuent, id_pres = cuenta.id_pres, id_cat = cuenta.id_cat, Categoria = cuenta.Categoria, Limite = cuenta.Limite, Gastado = gastado }); } } return(Json(new ResponseResult() { Success = true, Data = new { List = data, TotalLimites = data.Sum(d => d.Limite), TotalGastado = data.Sum(d => d.Gastado) } })); }
public string GetAll(int?id) { List <CuentaPres> data = new List <CuentaPres>(); using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { data.AddRange((from c in DB.t_cuenta_pres join cat in DB.t_Categoria_gasto on c.id_cat equals cat.id_cat where c.id_pres == id || id == null select new CuentaPres() { id_cuent = c.id_cuent, id_pres = c.id_pres, id_cat = c.id_cat, Limite = c.Limite, Categoria = cat.Nombre }).ToList()); } return(Newtonsoft.Json.JsonConvert.SerializeObject(data)); }
public string GetGastos() { List <GastoModel> gastos = new List <GastoModel>(); using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { gastos.AddRange(from g in DB.t_Gastos join c in DB.t_Categoria_gasto on g.id_cat equals c.id_cat select new GastoModel() { id_gasto = g.id_gasto, categoriaNombre = c.Nombre, categoria = g.id_cat, fecha = g.fecha, valor = g.valor, justificacion = g.justificacion }); } return(Newtonsoft.Json.JsonConvert.SerializeObject(gastos)); }
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 string GetGastos(int categoria, DateTime desde, DateTime hasta) { List <GastoModel> gastos = new List <GastoModel>(); using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { gastos.AddRange(from g in DB.t_Gastos join c in DB.t_Categoria_gasto on g.id_cat equals c.id_cat where (g.fecha >= desde && g.fecha <= hasta) && (g.id_cat == categoria || categoria == 0) select new GastoModel() { id_gasto = g.id_gasto, categoriaNombre = c.Nombre, categoria = g.id_cat, fecha = g.fecha, valor = g.valor, justificacion = g.justificacion }); } return(Newtonsoft.Json.JsonConvert.SerializeObject(new { list = gastos.OrderByDescending(g => g.fecha).ToList(), total = gastos.Sum(g => g.valor), numero = gastos.Count })); }
public JsonResult Edit(Presupuesto model) { if (ModelState.IsValid) { //Save using (Data.FinanzasPersonales DB = new Data.FinanzasPersonales()) { //Validando que la fecha desde sea mayor o igual a la fecha hasta if (model.Desde > model.Hasta) { return(Json(new ResponseResult() { Success = false, Data = new List <ValidationResult>() { new ValidationResult() { Key = "id_pres", Errors = new ModelErrorCollection() { "La fecha 'Hasta' debe ser igual o mayor a la fecha 'Desde'" } } } })); } //Validando que otro presupuesto no entre en conflicto de fechas con este if (DB.t_presupuesto.Any(p => ( (p.Desde <= model.Desde && p.Hasta >= model.Desde) || (p.Desde <= model.Hasta && p.Hasta >= model.Hasta) ) && (p.id_pres != model.id_pres) ) ) { return(Json(new ResponseResult() { Success = false, Data = new List <ValidationResult>() { new ValidationResult() { Key = "id_pres", Errors = new ModelErrorCollection() { "Hay conflictos entre las fechas digitadas y las que existen" } } } })); } Data.t_presupuesto data = DB.t_presupuesto.Where(c => c.id_pres == model.id_pres).FirstOrDefault(); data.id_pres = model.id_pres; data.Nombre = model.Nombre; data.Desde = model.Desde; data.Hasta = model.Hasta; DB.SaveChanges(); } return(Json(new ResponseResult() { Success = true, Data = Validations.GetErrors(ModelState) })); } else { //Reject return(Json(new ResponseResult() { Success = false, Data = Validations.GetErrors(ModelState) })); } }
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) })); } }