public IActionResult ProcessInvoice(InvoiceVewModel model) { if (ModelState.IsValid) { model = TallyInvoice(model); } ViewData["Action"] = "Edit"; return(View("Invoice", model)); }
public async Task <IActionResult> SaveInvoiceAsync(InvoiceVewModel model) { if (ModelState.IsValid) { ApplicationUser Usr = await _userManager.GetUserAsync(User); var _unitOfWork = new UnitofWork(_applicationDBcontext, Usr, loggerfactory); var Exists = _unitOfWork.Invoices.GetInvoicebyId(model.Id); if (Exists != null) { // need to dig in to automapper on this lot more complex than it looks. Exists.Code = model.Code; Exists.Date = model.Date; Exists.From = model.From; Exists.Note = model.Note; Exists.Price = model.Price; Exists.Tax = model.Tax; Exists.To = model.To; Exists.AttentionOf = model.AttentionOf; Exists.ToEmail = model.ToEmail; Exists.Total = model.Total; // as the logic to update and add Items to the Invoice is complex decided to clear the list and replaces it with the // one in the model. EF seems happy with this approach which it was not when modifying/adding Items to existing list. Exists.Items.Clear(); for (int ct = 0; ct < model.Items.Count; ct++) { Exists.Items.Add(new tblInvoiceItems() { Item = model.Items[ct].Item, Price = model.Items[ct].Price, Tax = model.Items[ct].Tax, Total = model.Items[ct].Total, }); } } else { var ToSave = _mapper.Map <InvoiceVewModel, tblInvoice>(model); _unitOfWork.Invoices.AddRecord(ToSave); } var result = _unitOfWork.Complete(); if (result.StartsWith("Failure ")) { ModelState.AddModelError("", result); //ModelState.AddModelError("", "Invoice not saved, Error recieved on posting Invoice to Database. "); } else { ModelState.AddModelError("", "Invoice saved"); } } ViewData["Action"] = "Edit"; return(View("Invoice", model)); }
public IActionResult RemoveItem(InvoiceVewModel model, string ItemId) { if (ModelState.IsValid) { model.Items = model.Items.Where(x => x.Id != ItemId).ToList(); ModelState.AddModelError("", "Removed Item, Save to update database else changes will be lost. "); model = TallyInvoice(model); } ViewData["Action"] = "Edit"; return(View("Invoice", model)); }
public IActionResult RaiseInvoice() { InvoiceVewModel model = new InvoiceVewModel(); model.ApplicationUserId = _userManager.GetUserAsync(User).Result.Id.ToString(); model.Date = DateTime.Now.ToString("dd/mmm/yyyy HH:mm"); model.From = ""; model.Id = Guid.NewGuid().ToString(); model.Status = status.Open; ViewData["Action"] = "Edit"; return(View("Invoice", model)); }
public IActionResult AddItem(InvoiceVewModel model) { if (ModelState.IsValid) { model.Items.Add(new InvoiceItemViewModel() { InvoiceId = model.Id, Id = Guid.NewGuid().ToString(), RowVersion = "", Item = "", Price = "0.0", Tax = "0.0", Total = "0.0" }); model = TallyInvoice(model); } ViewData["Action"] = "Edit"; return(View("Invoice", model)); }
public InvoiceVewModel TallyInvoice(InvoiceVewModel model) { decimal Price = 0.0m; decimal Tax = 0.0m; for (var ct = 0; ct < model.Items.Count; ct++) { Price += decimal.Parse(model.Items[ct].Price); Tax += decimal.Parse(model.Items[ct].Tax); model.Items[ct].Total = (decimal.Parse(model.Items[ct].Price) + decimal.Parse(model.Items[ct].Tax)).ToString(); } model.Price = Price.ToString(); model.Tax = Tax.ToString(); model.Total = (Price + Tax).ToString(); return(model); }