public async Task <GroupOutletModel> PrepareGroupOutletListModel() { var groupOutlets = await _outletManagementService.GetAllGroupOutletsAsync(); var model = new GroupOutletModel { StoreGroupings = groupOutlets }; return(model); }
public async Task <IActionResult> AddGroupOutlet(GroupOutletModel model) { if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageOutletManagement)) { return(AccessDeniedView()); } if (model.SelectedStoreIds.Count == 0) { ModelState.AddModelError(string.Empty, "Store is required"); _notificationService.ErrorNotification("Store is required"); } try { StoreGrouping storeGrouping = new StoreGrouping { GroupName = model.GroupName, Store = new List <Store>() }; _storeGroupingRepository.Update(storeGrouping); //Update store grouping var storeList = await _storeService.GetStores(); foreach (var store in model.SelectedStoreIds) { var stores = storeList.FirstOrDefault(s => s.P_BranchNo == store); if (stores != null) { stores.StoreGroupingId = storeGrouping.Id; } await _storeService.UpdateStore(stores); } return(new NullJsonResult()); } catch (Exception e) { ModelState.AddModelError(string.Empty, e.Message); _notificationService.ErrorNotification(e.Message); return(Json(e.Message)); } }
public async Task <GroupOutletModel> PrepareGroupOutletModel(GroupOutletModel model, StoreGrouping storeGrouping) { if (storeGrouping != null) { model = model ?? new GroupOutletModel(); model.Id = storeGrouping.Id; model.GroupName = storeGrouping.GroupName; model.SelectedStoreIds = storeGrouping.Store.Select(sgs => sgs.P_BranchNo).ToList(); model.CreatedOn = _dateTimeHelper.ConvertToUserTime(storeGrouping.CreatedOnUtc, DateTimeKind.Utc); model.LastActivityDate = _dateTimeHelper.ConvertToUserTime(storeGrouping.ModifiedOnUtc.GetValueOrDefault(DateTime.UtcNow), DateTimeKind.Utc); } var stores = await _storeService.GetStores(); model.AvailableStores = stores.Select(store => new SelectListItem { Text = store.P_BranchNo.ToString() + " - " + store.P_Name, Value = store.P_BranchNo.ToString() }).ToList(); return(await Task.FromResult(model)); }
public async Task <IActionResult> EditGroupOutlet(GroupOutletModel model, bool continueEditing) { if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageOutletManagement)) { return(AccessDeniedView()); } var groupOutlet = await _outletManagementService.GetGroupOutletByIdAsync(model.Id); if (groupOutlet == null) { return(RedirectToAction("Index")); } //validate stores var allStores = await _storeService.GetStores(); var newStores = new List <Store>(); foreach (var store in allStores) { if (model.SelectedStoreIds.Contains(store.P_BranchNo)) { newStores.Add(store); } } if (model.SelectedStoreIds.Count == 0) { _notificationService.ErrorNotification("Store is required"); model = await _managementModelFactory.PrepareGroupOutletModel(model, groupOutlet); model.SelectedStoreIds = new List <int>(); return(View(model)); } if (ModelState.IsValid) { try { groupOutlet.GroupName = model.GroupName; //stores var storeList = await _storeService.GetStores(); foreach (var store in allStores) { if (model.SelectedStoreIds.Contains(store.P_BranchNo)) { //new store if (groupOutlet.Store.Count(map => map.P_BranchNo == store.P_BranchNo) == 0) { var stores = storeList.FirstOrDefault(s => s.P_BranchNo == store.P_BranchNo); if (stores != null) { stores.StoreGroupingId = groupOutlet.Id; } await _storeService.UpdateStore(stores); } } else { //remove store grouping id if (groupOutlet.Store.Count(mapping => mapping.P_BranchNo == store.P_BranchNo) > 0) { _outletManagementService.DeleteStoreGroupingId(model.Id, store); } } } _outletManagementService.UpdateGroupOutlet(groupOutlet); _notificationService.SuccessNotification("Store grouping has been updated successfully."); //set to active tab SaveSelectedTabName("groupOutletTab", true); if (!continueEditing) { return(RedirectToAction("Index")); } return(RedirectToAction("EditGroupOutlet", new { id = groupOutlet.Id })); } catch (Exception e) { _notificationService.ErrorNotification(e.Message); } } model = await _managementModelFactory.PrepareGroupOutletModel(model, groupOutlet); return(View(model)); }