public static Offer CreateOfferForRequirement(ApplicationDbContext db, IPrincipal user, RequirementListing requirementListing, decimal offerQuantity) { BranchUser branchUser = BranchUserHelpers.GetBranchUserCurrentForUser(db, user); Offer offer = new Offer() { OfferId = Guid.NewGuid(), ListingId = requirementListing.ListingId, ListingType = ListingTypeEnum.Requirement, OfferStatus = OfferStatusEnum.New, CurrentOfferQuantity = offerQuantity, OfferOriginatorAppUserId = branchUser.UserId, OfferOriginatorBranchId = branchUser.BranchId, OfferOriginatorCompanyId = branchUser.CompanyId, OfferOriginatorDateTime = DateTime.Now, ListingOriginatorAppUserId = requirementListing.ListingOriginatorAppUserId, ListingOriginatorBranchId = requirementListing.ListingOriginatorBranchId, ListingOriginatorCompanyId = requirementListing.ListingOriginatorCompanyId, ListingOriginatorDateTime = requirementListing.ListingOriginatorDateTime }; db.Offers.Add(offer); db.SaveChanges(); return(offer); }
public static AvailableListing CreateAvailableListing(ApplicationDbContext db, IPrincipal user, string itemDescription, ItemCategoryEnum itemCategory, ItemTypeEnum itemType, decimal quantityRequired, string uom, DateTime?availableFrom, DateTime?availableTo, ItemConditionEnum itemCondition, DateTime?displayUntilDate, DateTime?sellByDate, DateTime?useByDate, bool?deliveryAvailable, ItemRequiredListingStatusEnum listingStatus) { BranchUser branchUser = BranchUserHelpers.GetBranchUserCurrentForUser(db, user); Branch branch = BranchHelpers.GetBranch(db, branchUser.BranchId); AvailableListing AvailableListing = new AvailableListing() { ListingId = Guid.NewGuid(), ItemDescription = itemDescription, ItemCategory = itemCategory, ItemType = itemType, QuantityRequired = quantityRequired, QuantityFulfilled = 0, QuantityOutstanding = quantityRequired, UoM = uom, AvailableFrom = availableFrom, AvailableTo = availableTo, ItemCondition = itemCondition, DisplayUntilDate = displayUntilDate, SellByDate = sellByDate, UseByDate = useByDate, DeliveryAvailable = deliveryAvailable ?? false, ListingBranchPostcode = branch.AddressPostcode, ListingOriginatorAppUserId = branchUser.UserId, ListingOriginatorBranchId = branchUser.BranchId, ListingOriginatorCompanyId = branchUser.CompanyId, ListingOriginatorDateTime = DateTime.Now, ListingStatus = ItemRequiredListingStatusEnum.Open }; db.AvailableListings.Add(AvailableListing); db.SaveChanges(); return(AvailableListing); }
public static RequirementListing CreateRequirementListing(ApplicationDbContext db, IPrincipal user, string itemDescription, ItemCategoryEnum itemCategory, ItemTypeEnum itemType, decimal quantityRequired, string uom, DateTime?requiredFrom, DateTime?requiredTo, bool acceptDamagedItems, bool acceptOutOfDateItems, bool collectionAvailable, ItemRequiredListingStatusEnum listingStatus, Guid?selectedCampaignId) { BranchUser branchUser = BranchUserHelpers.GetBranchUserCurrentForUser(db, user); Branch branch = BranchHelpers.GetBranch(db, branchUser.BranchId); RequirementListing requirementListing = new RequirementListing() { ListingId = Guid.NewGuid(), ItemDescription = itemDescription, ItemCategory = itemCategory, ItemType = itemType, QuantityRequired = quantityRequired, QuantityFulfilled = 0, QuantityOutstanding = quantityRequired, UoM = uom, RequiredFrom = requiredFrom, RequiredTo = requiredTo, AcceptDamagedItems = acceptDamagedItems, AcceptOutOfDateItems = acceptOutOfDateItems, CollectionAvailable = collectionAvailable, ListingBranchPostcode = branch.AddressPostcode, ListingOriginatorAppUserId = branchUser.UserId, ListingOriginatorBranchId = branchUser.BranchId, ListingOriginatorCompanyId = branchUser.CompanyId, ListingOriginatorDateTime = DateTime.Now, ListingStatus = ItemRequiredListingStatusEnum.Open, CampaignId = selectedCampaignId }; db.RequirementListings.Add(requirementListing); db.SaveChanges(); return(requirementListing); }
public static AppUser UpdateAppUserFromAppUserEditView(ApplicationDbContext db, AppUserEditView view) { //Update AppUser AppUser appUser = GetAppUser(db, view.AppUserId); appUser.FirstName = view.FirstName; appUser.LastName = view.LastName; appUser.EntityStatus = view.EntityStatus; appUser.PrivacyLevel = view.PrivacyLevel; if (view.SelectedBranchId != null) { appUser.CurrentBranchId = view.SelectedBranchId.Value; } db.Entry(appUser).State = EntityState.Modified; db.SaveChanges(); //Update BranchUser (Role) BranchUser branchUser = BranchUserHelpers.GetBranchUser(db, appUser.AppUserId, appUser.CurrentBranchId); BranchUserHelpers.UpdateBranchUserRole(db, branchUser.BranchUserId, view.UserRole); //Update UserSettings AppUserSettingsHelpers.UpdateUserSettingsFromAppUserEditView(db, view); return(appUser); }
public static BranchUser UpdateBranchUserRole(ApplicationDbContext db, Guid branchUserId, UserRoleEnum userRole) { BranchUser branchUser = GetBranchUser(db, branchUserId); //if user role changing FROM or TO Super/Admin user then set appUser flags accordingly if (userRole == UserRoleEnum.SuperUser) { AppUserHelpers.UpdateRoleFlags(db, branchUser.UserId, true, false); } if (userRole == UserRoleEnum.Admin) { AppUserHelpers.UpdateRoleFlags(db, branchUser.UserId, false, true); } //if role changes to Admin/Super user from anything else then add all company branches to user and activate if required if ((userRole == UserRoleEnum.SuperUser || userRole == UserRoleEnum.Admin) && (branchUser.UserRole != UserRoleEnum.SuperUser && branchUser.UserRole != UserRoleEnum.Admin)) { BranchUserHelpers.CreateBranchUserAdminRolesForUserForAllBranches(db, branchUser, userRole); } else { branchUser.UserRole = userRole; } branchUser.UserRole = userRole; db.Entry(branchUser).State = EntityState.Modified; db.SaveChanges(); return(branchUser); }
public static void CreateBranchUserAdminRolesForUserForAllBranches(ApplicationDbContext db, BranchUser branchUser, UserRoleEnum userRole) { List <Branch> companyBranches = BranchHelpers.GetBranchesForCompany(db, branchUser.CompanyId); foreach (Branch branch in companyBranches) { BranchUser thisBranchUser = BranchUserHelpers.GetBranchUser(db, branchUser.UserId, branch.BranchId, branch.CompanyId); //Update if required else create new if missing if (thisBranchUser != null) { //if this branchuser is having the status changed then just check any outstanding actions and remove if (userRole != thisBranchUser.UserRole) { thisBranchUser.UserRole = userRole; thisBranchUser.EntityStatus = EntityStatusEnum.Active; db.Entry(branchUser).State = EntityState.Modified; db.SaveChanges(); } } else { BranchUserHelpers.CreateBranchUser(db, branchUser.UserId, branch.BranchId, branch.CompanyId, userRole, EntityStatusEnum.Active); } } }
public static Company GetCompanyForUser(ApplicationDbContext db, AppUser appUser) { BranchUser branchUser = BranchUserHelpers.GetBranchUser(db, appUser.AppUserId, appUser.CurrentBranchId); Company company = GetCompany(db, branchUser.CompanyId); return(company); }
public static void UpdateBranchUserRoleForAllBranches(ApplicationDbContext db, Guid appUserId, UserRoleEnum userRole) { List <BranchUser> branchUserEntriesForUser = BranchUserHelpers.GetBranchUsersForUser(appUserId); foreach (BranchUser branchUser in branchUserEntriesForUser) { UpdateBranchUserRole(db, branchUser.BranchUserId, userRole); } }
public static Branch CreateBranch(ApplicationDbContext db, Branch branch) { branch.BranchId = Guid.NewGuid(); db.Branches.Add(branch); db.SaveChanges(); //if addAdminUsersToThisBranch is true then add all admin users for the company to this branch BranchUserHelpers.CreateBranchAdminUsersForNewBranch(db, branch, UserRoleEnum.Admin); return(branch); }
public static List <Branch> GetBranchesForUser(ApplicationDbContext db, Guid appUserId) { List <BranchUser> branchUserForUser = BranchUserHelpers.GetBranchUsersForUser(db, appUserId); List <Branch> branchesForUser = new List <Branch>(); foreach (BranchUser branchUser in branchUserForUser) { branchesForUser.Add(BranchHelpers.GetBranch(db, branchUser.BranchId)); } List <Branch> branchesForUserDistinct = branchesForUser.Distinct().ToList(); return(branchesForUserDistinct); }
/// <summary> /// Adds all Admin users for the company of the new branch to the new branch /// </summary> /// <param name="db"></param> /// <param name="branchId">new branch id</param> public static void CreateBranchAdminUsersForNewBranch(ApplicationDbContext db, Branch branch, UserRoleEnum role) { List <AppUser> adminAppUsersForCompany = AppUserHelpers.GetAdminAppUsersForCompany(db, branch.CompanyId); foreach (AppUser adminUser in adminAppUsersForCompany) { BranchUser branchUser = BranchUserHelpers.GetBranchUser(db, adminUser.AppUserId, branch.BranchId, branch.CompanyId); //Only add if not already there if (branchUser == null) { BranchUserHelpers.CreateBranchUser(db, adminUser.AppUserId, branch.BranchId, branch.CompanyId, role, EntityStatusEnum.Active); } } }
public static Offer UpdateCounterOffer(ApplicationDbContext db, IPrincipal user, Offer offer, decimal offerQuantity) { BranchUser branchUser = BranchUserHelpers.GetBranchUserCurrentForUser(db, user); offer.CounterOfferQuantity = offerQuantity; offer.PreviousOfferQuantity = offer.CurrentOfferQuantity; offer.CurrentOfferQuantity = 0; offer.LastCounterOfferOriginatorAppUserId = branchUser.UserId; offer.LastCounterOfferOriginatorBranchId = branchUser.BranchId; offer.LastCounterOfferOriginatorCompanyId = branchUser.CompanyId; offer.LastCounterOfferOriginatorDateTime = DateTime.Now; db.Entry(offer).State = EntityState.Modified; db.SaveChanges(); return(offer); }
public static List <CampaignManageView> GetAllCampaignsManageViewForUserBranch(ApplicationDbContext db, IPrincipal user) { List <CampaignManageView> allCampaignsManageView = new List <CampaignManageView>(); BranchUser branchUser = BranchUserHelpers.GetBranchUserCurrentForUser(db, user); List <Campaign> allCampaignsForBranchUser = CampaignHelpers.GetAllCampaignsForBranchUser(db, branchUser); foreach (Campaign campaignForBranchUser in allCampaignsForBranchUser) { CampaignManageView campaignManageView = new CampaignManageView() { Campaign = campaignForBranchUser }; allCampaignsManageView.Add(campaignManageView); } return(allCampaignsManageView); }
public static List <AvailableListingManageView> GetAllAvailableListingsManageViewForUserBranch(ApplicationDbContext db, IPrincipal user) { List <AvailableListingManageView> allAvailableListingsManageView = new List <AvailableListingManageView>(); BranchUser branchUser = BranchUserHelpers.GetBranchUserCurrentForUser(db, user); List <AvailableListing> allAvailableListingsForBranchUser = AvailableListingHelpers.GetAllAvailableListingsForBranchUser(db, branchUser); foreach (AvailableListing AvailableListingForBranchUser in allAvailableListingsForBranchUser) { AvailableListingManageView AvailableListingManageView = new AvailableListingManageView() { AvailableListing = AvailableListingForBranchUser }; allAvailableListingsManageView.Add(AvailableListingManageView); } return(allAvailableListingsManageView); }
public static List <Branch> GetBranchesForUserForAdminView(ApplicationDbContext db, Guid appUserId) { AppUser currentAppUser = AppUserHelpers.GetAppUser(db, appUserId); Branch currentBranch = BranchHelpers.GetBranch(db, currentAppUser.CurrentBranchId); BranchUser currentBranchUser = BranchUserHelpers.GetBranchUser(db, appUserId, currentAppUser.CurrentBranchId, currentBranch.CompanyId); List <BranchUser> branchUserForUser = BranchUserHelpers.GetBranchUsersForUserWithRole(db, appUserId, currentBranchUser.UserRole); List <Branch> branchesForUser = new List <Branch>(); foreach (BranchUser branchUser in branchUserForUser) { branchesForUser.Add(BranchHelpers.GetBranch(db, branchUser.BranchId)); } List <Branch> branchesForUserDistinct = branchesForUser.Distinct().ToList(); return(branchesForUserDistinct); }
public static Campaign CreateCampaign(ApplicationDbContext db, IPrincipal user, string name, string strapLine, string description, byte[] image, string imageLocation, string website, DateTime?campaignStartDateTime, DateTime?campaignEndDateTime, string locationName, string locationAddressLine1, string locationAddressLine2, string locationAddressLine3, string locationAddressTownCity, string locationAddressCounty, string locationAddressPostcode, string locationTelephoneNumber, string locationEmail, string locationContactName, EntityStatusEnum entityStatus) { BranchUser branchUser = BranchUserHelpers.GetBranchUserCurrentForUser(db, user); Campaign campaign = new Campaign() { CampaignId = Guid.NewGuid(), Name = name, StrapLine = strapLine, Description = description, Image = image, ImageLocation = imageLocation, Website = website, CampaignStartDateTime = campaignStartDateTime, CampaignEndDateTime = campaignEndDateTime, LocationName = locationName, LocationAddressLine1 = locationAddressLine1, LocationAddressLine2 = locationAddressLine2, LocationAddressLine3 = locationAddressLine3, LocationAddressTownCity = locationAddressTownCity, LocationAddressCounty = locationAddressCounty, LocationAddressPostcode = locationAddressPostcode, LocationTelephoneNumber = locationTelephoneNumber, LocationEmail = locationEmail, LocationContactName = locationContactName, EntityStatus = entityStatus, CampaignOriginatorAppUserId = branchUser.UserId, CampaignOriginatorBranchId = branchUser.BranchId, CampaignOriginatorCompanyId = branchUser.CompanyId, CampaignOriginatorDateTime = DateTime.Now }; db.Campaigns.Add(campaign); db.SaveChanges(); return(campaign); }
public static ViewButtons GetAvailableButtonsForSingleView(ApplicationDbContext db, AppUser appUser, Branch branch, Company company, IPrincipal user) { //Build initial list ViewButtons buttons = new ViewButtons() { CompanyBlockButton = false, CompanyAddFriendButton = false, CompanyAddToGroupButton = false, BranchBlockButton = false, BranchAddFriendButton = false, BranchAddToGroupButton = false, UserBlockButton = false, UserAddFriendButton = false, UserAddToGroupButton = false }; //First: check this is not our listing AppUser currentUser = AppUserHelpers.GetAppUser(db, user); if (currentUser.AppUserId == appUser.AppUserId) { return(buttons); } //Second: check the branch is not our branch, else return current button settings if (currentUser.CurrentBranchId == branch.BranchId) { return(buttons); } //Third: check if the Company allows interbranch dealing, if not and company level are the same return current button settings Branch currentUserBranch = BranchHelpers.GetBranch(db, currentUser.CurrentBranchId); if (!company.AllowBranchTrading && currentUserBranch.CompanyId == company.CompanyId) { return(buttons); } //Now validate for button status depending on type of user for this branch user combo BranchUser branchUser = BranchUserHelpers.GetBranchUser(db, appUser.AppUserId, branch.BranchId, company.CompanyId); switch (branchUser.UserRole) { //Note - must be in this order as there are override checks the lower down the ranking/role you go case UserRoleEnum.SuperUser: case UserRoleEnum.Admin: buttons = SetCompanyButtons(db, buttons, company.CompanyId, currentUserBranch.CompanyId); buttons = SetBranchButtons(db, buttons, branch.BranchId, currentUser.CurrentBranchId); buttons = SetUserButtons(db, buttons, appUser.AppUserId, currentUser.AppUserId); break; case UserRoleEnum.Manager: buttons = SetBranchButtons(db, buttons, branch.BranchId, currentUser.CurrentBranchId); buttons = SetUserButtons(db, buttons, appUser.AppUserId, currentUser.AppUserId); break; case UserRoleEnum.User: buttons = SetUserButtons(db, buttons, appUser.AppUserId, currentUser.AppUserId); break; } return(buttons); }
public static AppUserEditView GetAppUserEditViewForUser(ApplicationDbContext db, AppUser appUserDetails) { Branch branch = BranchHelpers.GetBranch(db, appUserDetails.CurrentBranchId); BranchUser branchUser = BranchUserHelpers.GetBranchUser(db, appUserDetails.AppUserId, appUserDetails.CurrentBranchId); AppUserSettings appUserSettings = AppUserSettingsHelpers.GetAppUserSettingsForUser(db, appUserDetails.AppUserId); AppUserEditView view = new AppUserEditView() { AppUserId = appUserDetails.AppUserId, FirstName = appUserDetails.FirstName, LastName = appUserDetails.LastName, EntityStatus = appUserDetails.EntityStatus, PrivacyLevel = appUserDetails.PrivacyLevel, SelectedBranchId = appUserDetails.CurrentBranchId, UserRole = branchUser.UserRole, AppUserSettingsId = appUserSettings.AppUserSettingsId, BranchName = branch.BranchName, BranchBusinessType = branch.BusinessType, BranchAddressLine1 = branch.AddressLine1, BranchAddressLine2 = branch.AddressLine2, BranchAddressLine3 = branch.AddressLine3, BranchAddressTownCity = branch.AddressTownCity, BranchAddressCounty = branch.AddressCounty, BranchAddressPostcode = branch.AddressPostcode, CampaignDashboardMaxDistance = appUserSettings.CampaignDashboardMaxDistance, CampaignDashboardMaxAge = appUserSettings.CampaignDashboardMaxAge, RequiredListingDashboardMaxDistance = appUserSettings.RequiredListingDashboardMaxDistance, RequiredListingDashboardMaxAge = appUserSettings.RequiredListingDashboardMaxAge, AvailableListingDashboardMaxDistance = appUserSettings.AvailableListingDashboardMaxDistance, AvailableListingDashboardMaxAge = appUserSettings.AvailableListingDashboardMaxAge, CampaignDashboardExternalSelectionLevel = appUserSettings.CampaignDashboardExternalSelectionLevel, RequiredListingDashboardExternalSelectionLevel = appUserSettings.RequiredListingDashboardExternalSelectionLevel, AvailableListingDashboardExternalSelectionLevel = appUserSettings.AvailableListingDashboardExternalSelectionLevel, CampaignGeneralInfoDisplayMyUserListings = appUserSettings.CampaignGeneralInfoDisplayMyUserListings, CampaignGeneralInfoDisplayMyBranchListings = appUserSettings.CampaignGeneralInfoDisplayMyBranchListings, CampaignGeneralInfoDisplayMyCompanyListings = appUserSettings.CampaignGeneralInfoDisplayMyCompanyListings, CampaignGeneralInfoDisplayBlockedListings = appUserSettings.CampaignGeneralInfoDisplayBlockedListings, CampaignGeneralInfoMaxDistance = appUserSettings.CampaignGeneralInfoMaxDistance, RequiredListingGeneralInfoDisplayMyUserListings = appUserSettings.RequiredListingGeneralInfoDisplayMyUserListings, RequiredListingGeneralInfoDisplayMyBranchListings = appUserSettings.RequiredListingGeneralInfoDisplayMyBranchListings, RequiredListingGeneralInfoDisplayMyCompanyListings = appUserSettings.RequiredListingGeneralInfoDisplayMyCompanyListings, RequiredListingGeneralInfoDisplayBlockedListings = appUserSettings.RequiredListingGeneralInfoDisplayBlockedListings, RequiredListingGeneralInfoMaxDistance = appUserSettings.RequiredListingGeneralInfoMaxDistance, AvailableListingGeneralInfoDisplayMyUserListings = appUserSettings.AvailableListingGeneralInfoDisplayMyUserListings, AvailableListingGeneralInfoDisplayMyBranchListings = appUserSettings.AvailableListingGeneralInfoDisplayMyBranchListings, AvailableListingGeneralInfoDisplayMyCompanyListings = appUserSettings.AvailableListingGeneralInfoDisplayMyCompanyListings, AvailableListingGeneralInfoDisplayBlockedListings = appUserSettings.AvailableListingGeneralInfoDisplayBlockedListings, AvailableListingGeneralInfoMaxDistance = appUserSettings.AvailableListingGeneralInfoMaxDistance, CampaignManageViewInternalSelectionLevel = appUserSettings.CampaignManageViewInternalSelectionLevel, RequiredListingManageViewInternalSelectionLevel = appUserSettings.RequiredListingManageViewInternalSelectionLevel, AvailableListingManageViewInternalSelectionLevel = appUserSettings.AvailableListingManageViewInternalSelectionLevel, OffersManageViewInternalSelectionLevel = appUserSettings.OffersManageViewInternalSelectionLevel, OffersAcceptedAuthorisationManageViewLevel = appUserSettings.OffersAcceptedAuthorisationManageViewLevel, OffersRejectedAuthorisationManageViewLevel = appUserSettings.OffersRejectedAuthorisationManageViewLevel, OffersReturnedAuthorisationManageViewLevel = appUserSettings.OffersReturnedAuthorisationManageViewLevel, OrdersManageViewInternalSelectionLevel = appUserSettings.OrdersManageViewInternalSelectionLevel, OrdersDespatchedAuthorisationManageViewLevel = appUserSettings.OrdersDespatchedAuthorisationManageViewLevel, OrdersDeliveredAuthorisationManageViewLevel = appUserSettings.OrdersDeliveredAuthorisationManageViewLevel, OrdersCollectedAuthorisationManageViewLevel = appUserSettings.OrdersCollectedAuthorisationManageViewLevel, OrdersClosedAuthorisationManageViewLevel = appUserSettings.OrdersClosedAuthorisationManageViewLevel, CampaignGeneralInfoExternalSelectionLevel = appUserSettings.CampaignGeneralInfoExternalSelectionLevel, RequiredListingGeneralInfoExternalSelectionLevel = appUserSettings.RequiredListingGeneralInfoExternalSelectionLevel, AvailableListingGeneralInfoExternalSelectionLevel = appUserSettings.AvailableListingGeneralInfoExternalSelectionLevel, GroupListViewsForUserOnly = GroupViewHelpers.GetGroupEditViewForForUserOnly(db, appUserDetails.AppUserId), UserFriendListView = FriendViewHelpers.GetFriendViewByType(db, appUserDetails.AppUserId, LevelEnum.User), UserBranchFriendListView = FriendViewHelpers.GetFriendViewByType(db, appUserDetails.AppUserId, LevelEnum.Branch), UserCompanyFriendListView = FriendViewHelpers.GetFriendViewByType(db, appUserDetails.AppUserId, LevelEnum.Company), UserBlockListView = BlockViewHelpers.GetBlockViewByType(db, appUserDetails.AppUserId, LevelEnum.User), UserBranchBlockListView = BlockViewHelpers.GetBlockViewByType(db, appUserDetails.AppUserId, LevelEnum.Branch), UserCompanyBlockListView = BlockViewHelpers.GetBlockViewByType(db, appUserDetails.AppUserId, LevelEnum.Company) }; return(view); }
public static List <BranchAdminView> GetBranchAdminViewList(ApplicationDbContext db, IPrincipal user) { List <BranchAdminView> branchAdminViewList = new List <BranchAdminView>(); //Get the AppUser of this User AppUser appUser = AppUserHelpers.GetAppUser(db, user); //Get the list of branches this User is linked to List <Branch> branchesForAppUser = BranchHelpers.GetBranchesForUserForAdminView(db, appUser.AppUserId); //Build a list of all users that are linked to this company List <AppUser> allUsersForCompany = AppUserHelpers.GetAppUsersForCompany(branchesForAppUser[0].CompanyId); foreach (Branch branch in branchesForAppUser) { //Build list of company users to add to this branchView List <BranchAdminViewCompanyUser> branchAdminCompanyUsers = new List <BranchAdminViewCompanyUser>(); //Build a list of all users for this branch List <AppUser> allUsersForThisBranch = (from bu in db.BranchUsers join au in db.AppUsers on bu.UserId equals au.AppUserId where (bu.BranchId == branch.BranchId && bu.EntityStatus == EntityStatusEnum.Active) select au).Distinct().ToList(); //Add all company users to the 'RelatedCompanyUsers' and set the flag to true if the user appears in the branch list List <BranchAdminViewCompanyUser> relatedCompanyUsers = new List <BranchAdminViewCompanyUser>(); foreach (AppUser userForCompany in allUsersForCompany) { UserRoleEnum?role = null; //If the user appears in branchlist then set the 'linked' flag bool found = false; AppUser foundUser = allUsersForThisBranch.FirstOrDefault(x => x.AppUserId == userForCompany.AppUserId); if (foundUser != null) { //get role from branchuser for this userForCompany/branch BranchUser branchUserForCompany = BranchUserHelpers.GetBranchUser(db, userForCompany.AppUserId, branch.BranchId, branch.CompanyId); role = branchUserForCompany.UserRole; found = true; } BranchAdminViewCompanyUser branchAdminCompanyUser = new BranchAdminViewCompanyUser() { AppUserId = userForCompany.AppUserId, CurrentBranchId = userForCompany.CurrentBranchId, FirstName = userForCompany.FirstName, LastName = userForCompany.LastName, UserRole = role, EntityStatus = userForCompany.EntityStatus, LinkedToThisBranch = found }; branchAdminCompanyUsers.Add(branchAdminCompanyUser); } //Add this list of users (as UserAdminRelatedBranchView(s)) to the model BranchAdminView branchAdminView = new BranchAdminView() { BranchId = branch.BranchId, CompanyId = branch.CompanyId, BranchName = branch.BranchName, BusinessType = branch.BusinessType, AddressLine1 = branch.AddressLine1, AddressLine2 = branch.AddressLine2, AddressLine3 = branch.AddressLine3, AddressTownCity = branch.AddressTownCity, AddressCounty = branch.AddressCounty, AddressPostcode = branch.AddressPostcode, TelephoneNumber = branch.TelephoneNumber, Email = branch.Email, ContactName = branch.ContactName, PrivacyLevel = branch.PrivacyLevel, EntityStatus = branch.EntityStatus, CompanyUserListId = Guid.NewGuid(), //used to identify the following list in the view (can't use BranchId as that is used for a different block in the table) RelatedCompanyUsers = branchAdminCompanyUsers }; branchAdminViewList.Add(branchAdminView); } return(branchAdminViewList); }
public static bool UpdateBranchesFromBranchAdminView(ApplicationDbContext db, List <BranchAdminView> branchesAdminView, IPrincipal user) { //Get logged in user details for Task creation (if required) AppUser loggedInUser = AppUserHelpers.GetAppUser(db, user); try { foreach (BranchAdminView branchAdminVeiw in branchesAdminView) { //Get original branch record so that we can compare previous and current entity status' Branch branch = BranchHelpers.GetBranch(db, branchAdminVeiw.BranchId); EntityStatusEnum previousEntityStatus = branch.EntityStatus; //Update branch branch = BranchHelpers.UpdateBranch(db, branchAdminVeiw.BranchId, branchAdminVeiw.CompanyId, branchAdminVeiw.BusinessType, branchAdminVeiw.BranchName, branchAdminVeiw.AddressLine1, branchAdminVeiw.AddressLine2, branchAdminVeiw.AddressLine3, branchAdminVeiw.AddressTownCity, branchAdminVeiw.AddressCounty, branchAdminVeiw.AddressPostcode, branchAdminVeiw.TelephoneNumber, branchAdminVeiw.Email, branchAdminVeiw.ContactName, branchAdminVeiw.PrivacyLevel, branchAdminVeiw.EntityStatus); //if change of status from on-hold - anything then look for outstanding task and set to closed if (branchAdminVeiw.EntityStatus != EntityStatusEnum.OnHold && previousEntityStatus == EntityStatusEnum.OnHold) { List <UserTask> activeTasksForThisBranch = UserTaskHelpers.GetUserTasksForBranch(db, branch.BranchId); foreach (UserTask activeTaskForThisBranch in activeTasksForThisBranch) { UserTaskHelpers.UpdateEntityStatus(activeTaskForThisBranch.UserTaskId, EntityStatusEnum.Closed); } } //If change of status to on-hold then create a Task if (branchAdminVeiw.EntityStatus == EntityStatusEnum.OnHold && previousEntityStatus != EntityStatusEnum.OnHold) { UserTaskHelpers.CreateUserTask(TaskTypeEnum.BranchOnHold, "New branch on hold, awaiting administrator activation", branch.BranchId, loggedInUser.AppUserId, EntityStatusEnum.Active); } //Update Users link with Branch foreach (BranchAdminViewCompanyUser companyUser in branchAdminVeiw.RelatedCompanyUsers) { //Try to find the user BranchUser branchUser = BranchUserHelpers.GetBranchUser(db, companyUser.AppUserId, branchAdminVeiw.BranchId, branchAdminVeiw.CompanyId); //Now check if user is checked in list then ensure it is on branchUser, else remove if it is on branchUser if (companyUser.LinkedToThisBranch) { //if company user linked but not on BranchUser, add to BranchUser if (branchUser == null) { BranchUserHelpers.CreateBranchUser(db, companyUser.AppUserId, branchAdminVeiw.BranchId, branchAdminVeiw.CompanyId, UserRoleEnum.User, EntityStatusEnum.Active); } //if company user linked but not ACTIVE on BranchUser else if (branchUser.EntityStatus != EntityStatusEnum.Active) { BranchUserHelpers.UpdateBranchUserStatus(db, branchUser, EntityStatusEnum.Active, companyUser.AppUserId); } } else { //if company user not linked but is on BranchUser, remove from BranchUser by setting status to Inactive if (branchUser != null) { BranchUserHelpers.UpdateBranchUserStatus(db, branchUser, EntityStatusEnum.Inactive, companyUser.AppUserId); } } } } return(true); } catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); return(false); } }
public static Order CreateOrder(ApplicationDbContext db, IPrincipal user, Offer offer) { BranchUser branchUser = BranchUserHelpers.GetBranchUserCurrentForUser(db, user); //depending on 'branch trading' check to see if last counter is related to this user, if not then use the last counter details (if counter details exist) if (offer.LastCounterOfferOriginatorAppUserId != null) { Company company = CompanyHelpers.GetCompany(db, branchUser.CompanyId); if (company.AllowBranchTrading) { if (branchUser.BranchId != offer.LastCounterOfferOriginatorBranchId) { branchUser = BranchUserHelpers.GetBranchUser(db, offer.LastCounterOfferOriginatorAppUserId.Value, offer.LastCounterOfferOriginatorBranchId.Value, offer.LastCounterOfferOriginatorCompanyId.Value); } } else { if (branchUser.CompanyId != offer.LastCounterOfferOriginatorCompanyId) { branchUser = BranchUserHelpers.GetBranchUser(db, offer.LastCounterOfferOriginatorAppUserId.Value, offer.LastCounterOfferOriginatorBranchId.Value, offer.LastCounterOfferOriginatorCompanyId.Value); } } } decimal orderQty = offer.CurrentOfferQuantity; if (offer.CurrentOfferQuantity == 0 && offer.CounterOfferQuantity != 0) { orderQty = offer.CounterOfferQuantity.Value; } Order order = new Order() { OrderId = Guid.NewGuid(), ListingType = offer.ListingType, OrderQuanity = orderQty, OrderStatus = OrderStatusEnum.New, OrderCreationDateTime = DateTime.Now, OrderOriginatorAppUserId = branchUser.UserId, OrderOriginatorBranchId = branchUser.BranchId, OrderOriginatorCompanyId = branchUser.CompanyId, OrderOriginatorDateTime = DateTime.Now, OfferId = offer.OfferId, OfferOriginatorAppUserId = offer.OfferOriginatorAppUserId, OfferOriginatorBranchId = offer.OfferOriginatorBranchId, OfferOriginatorCompanyId = offer.OfferOriginatorCompanyId, ListingId = offer.ListingId, ListingOriginatorAppUserId = offer.ListingOriginatorAppUserId, ListingOriginatorBranchId = offer.ListingOriginatorBranchId, ListingOriginatorCompanyId = offer.ListingOriginatorCompanyId }; db.Orders.Add(order); //Update the quantities on listing switch (offer.ListingType) { case ListingTypeEnum.Available: AvailableListingHelpers.UpdateQuantitiesFromOrder(db, offer); break; case ListingTypeEnum.Requirement: RequirementListingHelpers.UpdateQuantitiesFromOrder(db, offer); break; } db.SaveChanges(); return(order); }
public static bool UpdateUsersFromUserAdminView(ApplicationDbContext db, List <UserAdminView> userAdminViewForUser, IPrincipal user) { //Get logged in user details for Task creation (if required) AppUser loggedInUser = AppUserHelpers.GetAppUser(db, user); try { List <Guid> userChangedFromAdminList = new List <Guid>(); foreach (UserAdminView userAdminView in userAdminViewForUser) { //Get original appUser record so that we can compare previous and current entity status' AppUser appUser = AppUserHelpers.GetAppUser(db, userAdminView.AppUserId); EntityStatusEnum previousEntityStatus = appUser.EntityStatus; //Update the AppUser record (except Current Branch as that was done in real time) appUser = AppUserHelpers.UpdateAppUserExcludingCurrentBranchField(db, userAdminView.AppUserId, userAdminView.FirstName, userAdminView.LastName, userAdminView.AppUserEntityStatus, userAdminView.PrivacyLevel); //if change of status from on-hold - anything then look for outstanding task and set to closed if (userAdminView.AppUserEntityStatus != EntityStatusEnum.OnHold && previousEntityStatus == EntityStatusEnum.OnHold) { UserTaskHelpers.CloseAllTasksForUserChangingStatusFromOnHold(db, appUser.AppUserId); } //If change of status to on-hold then create a Task if (userAdminView.AppUserEntityStatus == EntityStatusEnum.OnHold && previousEntityStatus != EntityStatusEnum.OnHold) { UserTaskHelpers.CreateUserTask(TaskTypeEnum.UserOnHold, "User on hold, awaiting administrator/manager activation", appUser.AppUserId, loggedInUser.AppUserId, EntityStatusEnum.Active); } //If change of status from Active then check outstanding actions/tasks and reassign/create new tasks/actions??? //CURRENTLY - don't do anything for ACTIONS as these are specific and TASKS will be assigned at ADMIN level also so don't do anything here at present //If change of role from ADMIN to something else then tasks will need to be re-assigned - THIS IS DONE IN /Data/SetUserToNewRole List <Guid> userSetToAdminList = new List <Guid>(); //Update the User Role on each Branch foreach (UserAdminRelatedBranchesView relatedBranch in userAdminView.RelatedBranches) { //just keep a list of the user's we have set to Admin as within the UpdateBranchUserRole, if a role is set to Admin on 1 user, then that user on every branch is set to Admin, so we don't want to set it back Guid toAdminResult = userSetToAdminList.Find(x => x == relatedBranch.AppUserId); if (toAdminResult == Guid.Empty) { BranchUserHelpers.UpdateBranchUserRole(db, relatedBranch.BranchUserId, relatedBranch.UserRole); if (relatedBranch.UserRole == UserRoleEnum.Admin) { userSetToAdminList.Add(relatedBranch.AppUserId); } } } } return(true); } catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); return(false); } }
public static List <UserAdminView> GetUserAdminViewListForUser(ApplicationDbContext db, IPrincipal user) { List <UserAdminView> userAdminViewListForUser = new List <UserAdminView>(); List <UserAdminRelatedBranchesView> relatedBranches = new List <UserAdminRelatedBranchesView>(); AppUser appUser = AppUserHelpers.GetAppUser(db, user); BranchUser branchUser = BranchUserHelpers.GetBranchUser(db, appUser.AppUserId, appUser.CurrentBranchId); switch (user.Identity.GetCurrentUserRole()) { case "SuperUser": case "Admin": //Get all users for the company of this user var branchUsersForCompany = (from b in db.BranchUsers join a in db.AppUsers on b.UserId equals a.AppUserId where (b.CompanyId == branchUser.CompanyId && b.EntityStatus == EntityStatusEnum.Active) select new { AppUserId = b.UserId, BranchId = b.BranchId, BranchUserId = b.BranchUserId, CurrentBranchId = a.CurrentBranchId, UserRole = b.UserRole }).Distinct().ToList(); foreach (var branchUserForCompany in branchUsersForCompany) { UserAdminRelatedBranchesView relatedBranch = new UserAdminRelatedBranchesView(); relatedBranch.AppUserId = branchUserForCompany.AppUserId; relatedBranch.BranchId = branchUserForCompany.BranchId; relatedBranch.BranchUserId = branchUserForCompany.BranchUserId; relatedBranch.UserRole = branchUserForCompany.UserRole; //relatedBranch.BranchUserDetails = BranchUserHelpers.GetBranchUser(db, branchUserForCompany.BranchUserId); Branch branchDetails = BranchHelpers.GetBranch(db, branchUserForCompany.BranchId); relatedBranch.BranchName = branchDetails.BranchName; relatedBranch.AddressLine1 = branchDetails.AddressLine1; relatedBranch.AddressTownCity = branchDetails.AddressTownCity; relatedBranch.AddressPostcode = branchDetails.AddressPostcode; if (branchUserForCompany.BranchId == branchUserForCompany.CurrentBranchId) { relatedBranch.CurrentBranch = true; } else { relatedBranch.CurrentBranch = false; } relatedBranches.Add(relatedBranch); } List <AppUser> appUsersForCompany = (from b in branchUsersForCompany join a in db.AppUsers on b.AppUserId equals a.AppUserId select a).Distinct().ToList(); foreach (AppUser appUserForCompany in appUsersForCompany) { UserAdminView userAdminView = new UserAdminView(); userAdminView.AppUserId = appUserForCompany.AppUserId; userAdminView.FirstName = appUserForCompany.FirstName; userAdminView.LastName = appUserForCompany.LastName; userAdminView.AppUserEntityStatus = appUserForCompany.EntityStatus; userAdminView.CurrentBranchId = appUserForCompany.CurrentBranchId; userAdminView.LoginEmail = appUserForCompany.LoginEmail; userAdminView.RelatedBranches = (from rb in relatedBranches where (rb.AppUserId == appUserForCompany.AppUserId) select rb).ToList(); userAdminViewListForUser.Add(userAdminView); } break; case "Manager": //Get all users for the branches of this user as manager (manager) var branchList = (from bu in db.BranchUsers where (bu.UserId == appUser.AppUserId && bu.UserRole == UserRoleEnum.Manager) select new { BranchId = bu.BranchId }).Distinct().ToList(); //var branchUsersForBranch = (from b in db.BranchUsers // join a in db.AppUsers on b.UserId equals a.AppUserId // join c in branchList on b.BranchId equals c.BranchId // where (b.BranchId == appUser.CurrentBranchId && b.EntityStatus == EntityStatusEnum.Active) // select new { AppUserId = b.UserId, BranchId = b.BranchId, BranchUserId = b.BranchUserId, CurrentBranchId = a.CurrentBranchId, UserRole = b.UserRole }).Distinct().ToList(); var branchUsersForBranchList = (from bl in branchList join bu in db.BranchUsers on bl.BranchId equals bu.BranchId join au in db.AppUsers on bu.UserId equals au.AppUserId where (bu.CompanyId == branchUser.CompanyId && bu.EntityStatus == EntityStatusEnum.Active) select new { AppUserId = bu.UserId, BranchId = bu.BranchId, BranchUserId = bu.BranchUserId, CurrentBranchId = au.CurrentBranchId, UserRole = bu.UserRole }).Distinct().ToList(); foreach (var branchUserForBranch in branchUsersForBranchList) { UserAdminRelatedBranchesView relatedBranch = new UserAdminRelatedBranchesView(); relatedBranch.AppUserId = branchUserForBranch.AppUserId; relatedBranch.BranchId = branchUserForBranch.BranchId; relatedBranch.BranchUserId = branchUserForBranch.BranchUserId; relatedBranch.UserRole = branchUserForBranch.UserRole; //relatedBranch.BranchUserDetails = BranchUserHelpers.GetBranchUser(db, branchUserForBranch.BranchUserId); Branch branchDetails = BranchHelpers.GetBranch(db, branchUserForBranch.BranchId); relatedBranch.BranchName = branchDetails.BranchName; relatedBranch.AddressLine1 = branchDetails.AddressLine1; relatedBranch.AddressTownCity = branchDetails.AddressTownCity; relatedBranch.AddressPostcode = branchDetails.AddressPostcode; if (branchUserForBranch.BranchId == branchUserForBranch.CurrentBranchId) { relatedBranch.CurrentBranch = true; } else { relatedBranch.CurrentBranch = false; } relatedBranches.Add(relatedBranch); } List <AppUser> appUsersForBranchList = (from b in branchUsersForBranchList join a in db.AppUsers on b.AppUserId equals a.AppUserId select a).Distinct().ToList(); foreach (AppUser appUserForBranch in appUsersForBranchList) { UserAdminView userAdminView = new UserAdminView(); userAdminView.AppUserId = appUserForBranch.AppUserId; userAdminView.FirstName = appUserForBranch.FirstName; userAdminView.LastName = appUserForBranch.LastName; userAdminView.AppUserEntityStatus = appUserForBranch.EntityStatus; userAdminView.CurrentBranchId = appUserForBranch.CurrentBranchId; userAdminView.LoginEmail = appUserForBranch.LoginEmail; userAdminView.RelatedBranches = (from rb in relatedBranches where (rb.AppUserId == appUserForBranch.AppUserId) select rb).ToList(); userAdminViewListForUser.Add(userAdminView); } break; } return(userAdminViewListForUser); }