public IActionResult Edit(PricingFactor pricingFactor) { if (ModelState.IsValid) { _context.PricingFactors.Update(pricingFactor); _context.SaveChanges(); return(RedirectToAction("Index")); } return(View(pricingFactor)); }
public IActionResult CreateOrEdit(QuoteViewModel quoteViewModel) { if (quoteViewModel.thisQuote.Id == null) { // New Quote quoteViewModel.thisQuote.Id = Guid.NewGuid().ToString(); quoteViewModel.thisQuote.CreatedByUserId = HttpContext.Session.GetString("UserId"); quoteViewModel.thisQuote.CreatedDateTime = DateTime.Now; quoteViewModel.thisQuote.LastModifiedByUserId = HttpContext.Session.GetString("UserId"); quoteViewModel.thisQuote.LastModifiedDateTime = DateTime.Now; quoteViewModel.thisCustomer = _context.Customers.Where(c => c.Id == quoteViewModel.thisQuote.CustomerId).Single(); quoteViewModel.thisQuote.IsTaxExempt = quoteViewModel.thisCustomer.IsTaxExempt; // Extract despatch site from delivery/collection address. if (quoteViewModel.thisQuote.DeliveryAddress != null) { quoteViewModel.thisQuote.DespatchSite = quoteViewModel.thisQuote.DeliveryAddress.Substring(quoteViewModel.thisQuote.DeliveryAddress.IndexOf("[")).Replace("[", "").Replace("]", "").TrimEnd(); } // Establish next quote ref string lastRef = ""; lastRef = _context.Quotes .Where(q => (q.QuoteReferenceNumber).Substring(0, q.QuoteReferenceNumber.IndexOf("-")) == quoteViewModel.thisQuote.CustomerId.Substring(0, 3)) .OrderByDescending(x => x.CreatedDateTime) .Take(1) .Select(q => q.QuoteReferenceNumber).FirstOrDefault(); if (lastRef != null) { int increment = Convert.ToInt32(lastRef.Substring(9)); quoteViewModel.thisQuote.QuoteReferenceNumber = $"{quoteViewModel.thisQuote.CustomerId.Substring(0, 3)}-{DateTime.Now.Year}-{increment += 1}"; } else { quoteViewModel.thisQuote.QuoteReferenceNumber = $"{quoteViewModel.thisQuote.CustomerId.Substring(0, 3)}-{DateTime.Now.Year}-1"; } if (ModelState.IsValid) { if (quoteViewModel.thisCustomerDeliveryAddress.DeliverySaveAddress == true) { // Save new delivery address to customer record. CustomerAddress newDeliveryAddress = new CustomerAddress(); newDeliveryAddress.AddressLine1 = quoteViewModel.thisCustomerDeliveryAddress.DeliveryAddressLine1; newDeliveryAddress.AddressLine2 = quoteViewModel.thisCustomerDeliveryAddress.DeliveryAddressLine2; newDeliveryAddress.TownCity = quoteViewModel.thisCustomerDeliveryAddress.DeliveryTownCity; newDeliveryAddress.County = quoteViewModel.thisCustomerDeliveryAddress.DeliveryCounty; newDeliveryAddress.PostCode = quoteViewModel.thisCustomerDeliveryAddress.DeliveryPostCode; newDeliveryAddress.CreatedByUserId = HttpContext.Session.GetString("UserId"); newDeliveryAddress.CreatedDateTime = DateTime.Now; newDeliveryAddress.CustomerId = quoteViewModel.thisQuote.CustomerId; newDeliveryAddress.DespatchSite = quoteViewModel.thisCustomerDeliveryAddress.DeliveryDespatchSite; newDeliveryAddress.Id = Guid.NewGuid().ToString(); newDeliveryAddress.IsInvoiceAddress = false; newDeliveryAddress.IsPrimaryDeliveryAddress = false; newDeliveryAddress.ValidatedByGoogleAPI = quoteViewModel.thisCustomerDeliveryAddress.ValidatedByGoogleAPI; _context.CustomerAddresses.Add(newDeliveryAddress); _context.SaveChanges(); } _context.Quotes.Add(quoteViewModel.thisQuote); _context.SaveChanges(); return(RedirectToAction("CreateOrEdit", new { id = quoteViewModel.thisQuote.Id })); } else { // Populate collections etc so validation form is populated with options. quoteViewModel.ActiveCustomers = _context.Customers.ToList(); ViewBag.ActiveCustomers = quoteViewModel.ActiveCustomers; List <CustomerAddress> customerAddresses = new List <CustomerAddress>(); customerAddresses = _context.CustomerAddresses.Where(c => c.CustomerId == quoteViewModel.thisQuote.CustomerId).ToList(); List <SelectListItem> stringAddresses = new List <SelectListItem>(); if (customerAddresses.Count > 0) { //'<option selected="selected" selected disabled value="-1">-- Select Delivery Address --</option>' if (quoteViewModel.thisQuote.DeliveryAddress == null) { stringAddresses.Add(new SelectListItem { Selected = true, Disabled = true, Value = "-1", Text = "-- Select Delivery Address --" }); } else { stringAddresses.Add(new SelectListItem { Disabled = true, Value = "-1", Text = "-- Select Delivery Address --" }); } foreach (CustomerAddress address in customerAddresses) { stringAddresses.Add(new SelectListItem { Value = $"{address.AddressLine1}, {address.AddressLine2}, {address.TownCity}, {address.County}, {address.PostCode}, [{address.DespatchSite}]", Text = $"{address.AddressLine1}, {address.AddressLine2}, {address.TownCity}, {address.County}, {address.PostCode}, [{address.DespatchSite}]" }); } } quoteViewModel.DeliveryAddresses = stringAddresses; quoteViewModel.StockProfileColours = _context.ProfileColours.Where(p => p.IsAffordableStockColour == true && p.IsEnabled == true).OrderBy(p => p.ColourCode).ToList(); quoteViewModel.NonStockProfileColours = _context.ProfileColours.Where(p => p.IsAffordableStockColour == false && p.IsEnabled == true).OrderBy(p => p.ColourCode).ToList(); foreach (ProfileColour col in quoteViewModel.StockProfileColours) { col.ColourDisplayName = $"{col.ColourCode} {col.ColourName} {col.ColourFinish}"; } foreach (ProfileColour col in quoteViewModel.NonStockProfileColours) { col.ColourDisplayName = $"{col.ColourCode} {col.ColourName} {col.ColourFinish}"; } quoteViewModel.thisBifoldItems = new List <BifoldItem>(); quoteViewModel.thisExtraItems = new List <ExtraItem>(); quoteViewModel.thisGlassItems = new List <GlassItem>(); quoteViewModel.thisPeripheralItems = new List <PeripheralItem>(); quoteViewModel.IsNewQuote = true; return(View("CreateOrEdit", quoteViewModel)); } } else { // Edit Existing Quote if (ModelState.IsValid) { //Save edit details quoteViewModel.thisQuote.LastModifiedByUserId = HttpContext.Session.GetString("UserId"); quoteViewModel.thisQuote.LastModifiedDateTime = DateTime.Now; bool updateBifoldItem = false; quoteViewModel.thisBifoldItems = _context.BifoldItems.Where(b => b.QuoteId == quoteViewModel.thisQuote.Id).OrderBy(b => b.ItemNumber).ToList(); quoteViewModel.thisBifoldItemViewModels = new List <BifoldItemViewModel>(); BifoldItemViewModel bifoldItemViewModel; if (quoteViewModel.thisBifoldItems.Count > 0) { foreach (BifoldItem bifoldItem in quoteViewModel.thisBifoldItems) { bifoldItemViewModel = new BifoldItemViewModel(); bifoldItemViewModel.thisBifoldItem = bifoldItem; // bifoldItemViewModel.InternalColourName = _context.ProfileColours.Where(c => c.Id == bifoldItem.InternalColourId).Select(c => c.ColourName).Single(); //bifoldItemViewModel.ExternalColourName = _context.ProfileColours.Where(c => c.Id == bifoldItem.ExternalColourId).Select(c => c.ColourName).Single(); ProfileColour internalColour = _context.ProfileColours.Where(p => p.Id == bifoldItem.InternalColourId).Single(); if (internalColour.IsAffordableStockColour == true) { quoteViewModel.IsStockColourChosen = true; } else { quoteViewModel.IsStockColourChosen = false; } quoteViewModel.thisBifoldItemViewModels.Add(bifoldItemViewModel); } } foreach (BifoldItemViewModel bifold in quoteViewModel.thisBifoldItemViewModels) { if (bifold.thisBifoldItem.InternalColourId != quoteViewModel.thisQuote.MasterInternalColourId) { bifold.thisBifoldItem.InternalColourId = quoteViewModel.thisQuote.MasterInternalColourId; updateBifoldItem = true; } if (bifold.thisBifoldItem.ExternalColourId != quoteViewModel.thisQuote.MasterExternalColourId) { bifold.thisBifoldItem.ExternalColourId = quoteViewModel.thisQuote.MasterExternalColourId; updateBifoldItem = true; } if (bifold.thisBifoldItem.IsMarineOrHazardousCoating != quoteViewModel.thisQuote.IsMarineOrHazardousCoating) { bifold.thisBifoldItem.IsMarineOrHazardousCoating = quoteViewModel.thisQuote.IsMarineOrHazardousCoating; updateBifoldItem = true; } if (updateBifoldItem == true) { //Update Bifold Item _context.Entry(bifold.thisBifoldItem).State = EntityState.Modified; _context.SaveChanges(); } updateBifoldItem = false; } //Update the rest of the quote if (quoteViewModel.thisQuote.DeliveryAddress != null) { quoteViewModel.thisQuote.DespatchSite = quoteViewModel.thisQuote.DeliveryAddress.Substring(quoteViewModel.thisQuote.DeliveryAddress.IndexOf("[")).Replace("[", "").Replace("]", "").TrimEnd(); } if (quoteViewModel.thisCustomerDeliveryAddress.DeliverySaveAddress == true) { // Save new delivery address to customer record. CustomerAddress newDeliveryAddress = new CustomerAddress(); newDeliveryAddress.AddressLine1 = quoteViewModel.thisCustomerDeliveryAddress.DeliveryAddressLine1; newDeliveryAddress.AddressLine2 = quoteViewModel.thisCustomerDeliveryAddress.DeliveryAddressLine2; newDeliveryAddress.TownCity = quoteViewModel.thisCustomerDeliveryAddress.DeliveryTownCity; newDeliveryAddress.County = quoteViewModel.thisCustomerDeliveryAddress.DeliveryCounty; newDeliveryAddress.PostCode = quoteViewModel.thisCustomerDeliveryAddress.DeliveryPostCode; newDeliveryAddress.CreatedByUserId = HttpContext.Session.GetString("UserId"); newDeliveryAddress.CreatedDateTime = DateTime.Now; newDeliveryAddress.CustomerId = quoteViewModel.thisQuote.CustomerId; newDeliveryAddress.DespatchSite = quoteViewModel.thisCustomerDeliveryAddress.DeliveryDespatchSite; newDeliveryAddress.Id = Guid.NewGuid().ToString(); newDeliveryAddress.IsInvoiceAddress = false; newDeliveryAddress.IsPrimaryDeliveryAddress = false; newDeliveryAddress.ValidatedByGoogleAPI = quoteViewModel.thisCustomerDeliveryAddress.ValidatedByGoogleAPI; _context.CustomerAddresses.Add(newDeliveryAddress); _context.SaveChanges(); } _context.Entry(quoteViewModel.thisQuote).State = EntityState.Modified; _context.SaveChanges(); return(RedirectToAction("CreateOrEdit", new { id = quoteViewModel.thisQuote.Id })); } else { // Populate collections etc so validation form is populated with options. quoteViewModel.ActiveCustomers = _context.Customers.ToList(); ViewBag.ActiveCustomers = quoteViewModel.ActiveCustomers; List <CustomerAddress> customerAddresses = new List <CustomerAddress>(); customerAddresses = _context.CustomerAddresses.Where(c => c.CustomerId == quoteViewModel.thisQuote.CustomerId).ToList(); List <SelectListItem> stringAddresses = new List <SelectListItem>(); if (customerAddresses.Count > 0) { //'<option selected="selected" selected disabled value="-1">-- Select Delivery Address --</option>' if (quoteViewModel.thisQuote.DeliveryAddress == null) { stringAddresses.Add(new SelectListItem { Selected = true, Disabled = true, Value = "-1", Text = "-- Select Delivery Address --" }); } else { stringAddresses.Add(new SelectListItem { Disabled = true, Value = "-1", Text = "-- Select Delivery Address --" }); } foreach (CustomerAddress address in customerAddresses) { stringAddresses.Add(new SelectListItem { Value = $"{address.AddressLine1}, {address.AddressLine2}, {address.TownCity}, {address.County}, {address.PostCode}, [{address.DespatchSite}]", Text = $"{address.AddressLine1}, {address.AddressLine2}, {address.TownCity}, {address.County}, {address.PostCode}, [{address.DespatchSite}]" }); } } quoteViewModel.DeliveryAddresses = stringAddresses; quoteViewModel.StockProfileColours = _context.ProfileColours.Where(p => p.IsAffordableStockColour == true && p.IsEnabled == true).OrderBy(p => p.ColourCode).ToList(); quoteViewModel.NonStockProfileColours = _context.ProfileColours.Where(p => p.IsAffordableStockColour == false && p.IsEnabled == true).OrderBy(p => p.ColourCode).ToList(); foreach (ProfileColour col in quoteViewModel.StockProfileColours) { col.ColourDisplayName = $"{col.ColourCode} {col.ColourName} {col.ColourFinish}"; } foreach (ProfileColour col in quoteViewModel.NonStockProfileColours) { col.ColourDisplayName = $"{col.ColourCode} {col.ColourName} {col.ColourFinish}"; } quoteViewModel.thisBifoldItemViewModels = new List <BifoldItemViewModel>(); BifoldItemViewModel bifoldItemViewModel; quoteViewModel.thisBifoldItems = _context.BifoldItems.Where(b => b.QuoteId == quoteViewModel.thisQuote.Id).OrderBy(b => b.ItemNumber).ToList(); if (quoteViewModel.thisBifoldItems.Count > 0) { foreach (BifoldItem bifoldItem in quoteViewModel.thisBifoldItems) { bifoldItemViewModel = new BifoldItemViewModel(); bifoldItemViewModel.thisBifoldItem = bifoldItem; // bifoldItemViewModel.InternalColourName = _context.ProfileColours.Where(c => c.Id == bifoldItem.InternalColourId).Select(c => c.ColourName).Single(); //bifoldItemViewModel.ExternalColourName = _context.ProfileColours.Where(c => c.Id == bifoldItem.ExternalColourId).Select(c => c.ColourName).Single(); ProfileColour internalColour = _context.ProfileColours.Where(p => p.Id == bifoldItem.InternalColourId).Single(); if (internalColour.IsAffordableStockColour == true) { quoteViewModel.IsStockColourChosen = true; } else { quoteViewModel.IsStockColourChosen = false; } quoteViewModel.thisBifoldItemViewModels.Add(bifoldItemViewModel); } } quoteViewModel.thisExtraItems = new List <ExtraItem>(); quoteViewModel.thisGlassItems = new List <GlassItem>(); quoteViewModel.thisPeripheralItems = new List <PeripheralItem>(); quoteViewModel.IsNewQuote = false; return(View("CreateOrEdit", quoteViewModel)); } } }
public IActionResult Create(CustomerViewModel newCustomer) { // Set properties that are not set from form input. newCustomer.thisNewCustomer.Id = Guid.NewGuid().ToString(); newCustomer.thisNewCustomer.IsActiveCustomer = true; newCustomer.thisNewCustomer.CustomerIsLocked = false; newCustomer.thisNewCustomer.CreatedDateTime = DateTime.Now; newCustomer.thisNewCustomer.AutoInvoicingEnabled = true; newCustomer.thisNewCustomer.CustomerLastOrderedDateTime = DateTime.Parse("01/01/1753"); // Globally Acquired newCustomer.thisNewCustomer.CreatedByUserId = HttpContext.Session.GetString("UserId"); newCustomer.thisNewCustomer.CreatedByBranchCompanyId = HttpContext.Session.GetString("UserBranchId"); newCustomer.thisNewCustomerContact.Id = Guid.NewGuid().ToString(); newCustomer.thisNewCustomerContact.CreatedDateTime = DateTime.Now; newCustomer.thisNewCustomerContact.CustomerId = newCustomer.thisNewCustomer.Id; newCustomer.thisNewCustomerContact.IsAccountContact = true; newCustomer.thisNewCustomerContact.IsPrimaryContact = true; //Globally Acquired newCustomer.thisNewCustomerContact.CreatedByUserId = HttpContext.Session.GetString("UserId"); newCustomer.thisNewCustomerInvoiceAddress.Id = Guid.NewGuid().ToString(); //Globally Acquired newCustomer.thisNewCustomerInvoiceAddress.CreatedByUserId = HttpContext.Session.GetString("UserId"); newCustomer.thisNewCustomerInvoiceAddress.CreatedDateTime = DateTime.Now; newCustomer.thisNewCustomerInvoiceAddress.CustomerId = newCustomer.thisNewCustomer.Id; newCustomer.thisNewCustomerInvoiceAddress.IsInvoiceAddress = true; newCustomer.thisNewCustomerInvoiceAddress.IsPrimaryDeliveryAddress = true; int lastLegacyId; lastLegacyId = _context.Customers.OrderByDescending(c => c.LegacyId).Take(1).FirstOrDefault().LegacyId; newCustomer.thisNewCustomer.LegacyId = lastLegacyId += 1; if (ModelState.IsValid) { // Bespoke Validation? // Update database etc. _context.Customers.Add(newCustomer.thisNewCustomer); _context.SaveChanges(); _context.CustomerAddresses.Add(newCustomer.thisNewCustomerInvoiceAddress); _context.CustomerContacts.Add(newCustomer.thisNewCustomerContact); _context.SaveChanges(); // Set flag to refresh parent options. i.e. Add the new customer to the customer selection. newCustomer.thisNewCustomer.IsNewCustomer = true; return(PartialView("_CreateCustomerModal", newCustomer)); // Return a view or redirect after success. // return RedirectToAction("Create", "Quote"); } else { newCustomer.FailedValidation = true; } // Return the original view model if input does not pass validation. return(PartialView("_CreateCustomerModal", newCustomer)); }