public ActionResult Delete(CountableEntryViewModel vm) { try { var countableEntry = Mapper.Map(vm, new CountableEntry()); db.CountableEntries.Attach(countableEntry); db.CountableEntries.Remove(countableEntry); db.SaveChanges(); return Json(new { Result = "OK", Message = "Elemento Eliminado." }); } catch (Exception ex) { return Json(new { Result = "Error", Message = ex.StackTrace }); } }
public ActionResult Save(CountableEntryViewModel vm) { if (ModelState.ContainsKey("FechaRegistro")) { ModelState["FechaRegistro"].Errors.Clear(); } var countableEntry = Mapper.Map(vm, new CountableEntry()); if (ModelState.IsValid) { var account = db.Accounts.Find(countableEntry.AccountId); if (!string.Equals(countableEntry.MovementType, account.AccountType.Origin)) { countableEntry.AsientoAmount *= -1; } string mensaje = ""; if (countableEntry.Id > 0) { //using (var dbTemp = new DbCountable()) //{ // var temp = dbTemp.CountableEntries.Find(countableEntry.Id); // if (temp.AsientoAmount != countableEntry.AsientoAmount) // { // var newAmount = countableEntry.AsientoAmount - temp.AsientoAmount; // account.Balance += newAmount; // } //} db.CountableEntries.Attach(countableEntry); db.Entry(countableEntry).State = EntityState.Modified; //db.SaveChanges(); mensaje = "Datos Actualizados."; } else { db.CountableEntries.Add(countableEntry); //account.Balance += countableEntry.AsientoAmount; mensaje = "Entrada registrada"; } //db.Accounts.Attach(account); //db.Entry(account).State = EntityState.Modified; db.SaveChanges(); countableEntry.Account = account; vm = Mapper.Map(countableEntry, new CountableEntryViewModel()); return Json(new { Result = "OK", Data = vm, Message = mensaje }); } return Json(new { Result = "ERROR", Message = "Hay Datos erroneos" }); }