public async Task <IActionResult> Edit(int id, [Bind("ID,TimeStamp,Sum,Income,MonthlyIncome,PaySourceID")] Inpayment inpayment) { if (id != inpayment.ID) { return(NotFound()); } if (ModelState.IsValid) { try { Helpers.Money.CheckInpaymentSumAndAjustPaySource(_context, inpayment); _context.Update(inpayment); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!InpaymentExists(inpayment.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["PaySourceID"] = new SelectList(_context.PaySources, "ID", "ID", inpayment.PaySourceID); PopulateCategoryDropDownList(); PopulatePaySourceDropDownList(); return(View(inpayment)); }
// GET: Inpayments/Create public IActionResult Create() { ViewData["PaySourceID"] = new SelectList(_context.PaySources, "ID", "ID"); var inpayment = new Inpayment { TimeStamp = DateTime.Now }; PopulateCategoryDropDownList(); PopulatePaySourceDropDownList(); return(View(inpayment)); }
public async Task <IActionResult> Create([Bind("ID,TimeStamp,Sum,Income,MonthlyIncome,PaySourceID")] Inpayment inpayment) { if (ModelState.IsValid) { _context.Add(inpayment); Helpers.Money.Earn(_context, inpayment); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["PaySourceID"] = new SelectList(_context.PaySources, "ID", "ID", inpayment.PaySourceID); PopulateCategoryDropDownList(); PopulatePaySourceDropDownList(); return(View(inpayment)); }
public static void CheckInpaymentSumAndAjustPaySource(FamilyFinancesContext context, Inpayment inpayment) { var originalSum = (decimal)context.Entry(inpayment).GetDatabaseValues().GetValue <object>("Sum"); var currentSum = context.Entry(inpayment).Property(x => x.Sum).CurrentValue; PaySource paySource = context.PaySources.Find(inpayment.PaySourceID); SetNewBalance(context, paySource, paySource.Balance - originalSum + currentSum); }
public static void RemoveEarn(FamilyFinancesContext context, Inpayment inpayment) { var paySource = context.PaySources.Find(inpayment.PaySourceID); SetNewBalance(context, paySource, paySource.Balance - inpayment.Sum); }