public ActionResult Available(AvailableListingGeneralViewListModel model) { if (ModelState.IsValid) { if (Request.Form["savebutton"] != null) { //Create offers OfferHelpers.CreateOffers(db, model, null, ListingTypeEnum.Available, User); } return(RedirectToAction("Available", "GeneralInfo", new { maxDistance = model.MaxDistance, maxAge = model.MaxAge })); } return(View(model)); }
public static List <Offer> CreateOffers(ApplicationDbContext db, AvailableListingGeneralViewListModel availableModel, RequiredListingGeneralViewListModel requiredModel, ListingTypeEnum listingType, IPrincipal user) { AppUser currentUser = AppUserHelpers.GetAppUser(db, user); List <Offer> createdOffers = new List <Offer>(); if (listingType == ListingTypeEnum.Available) { //go through the records, if there are offer qty's > 0 then create an offer from these foreach (AvailableListingGeneralViewModel item in availableModel.Listing) { if (item.OfferQty.HasValue) { if (item.OfferQty > 0) { //only create offers if they don't already exist if (OfferHelpers.GetOfferForListingByUser(db, item.ListingId, currentUser.AppUserId, currentUser.OrganisationId, LevelEnum.Organisation) == null) { createdOffers.Add(CreateOffer(db, item.ListingId, item.OfferQty, listingType, currentUser, user)); } } } } } if (listingType == ListingTypeEnum.Requirement) { //go through the records, if there are offer qty's > 0 then create an offer from these foreach (RequiredListingGeneralViewModel item in requiredModel.Listing) { if (item.RequiredQty.HasValue) { if (item.RequiredQty > 0) { //only create offers if they don't already exist if (OfferHelpers.GetOfferForListingByUser(db, item.ListingId, currentUser.AppUserId, currentUser.OrganisationId, LevelEnum.Organisation) == null) { createdOffers.Add(CreateOffer(db, item.ListingId, item.RequiredQty, listingType, currentUser, user)); } } } } } return(createdOffers); }
public static AvailableListingGeneralViewListModel GetAvailableListingGeneralViewListModel(ApplicationDbContext db, IPrincipal user, int?maxDistance, double?maxAge) { List <AvailableListingGeneralViewModel> list = new List <AvailableListingGeneralViewModel>(); //Get user so we can get the settings to initialise the search on the screen AppUser currentUser = AppUserHelpers.GetAppUser(db, user); Organisation currentOrg = OrganisationHelpers.GetOrganisation(db, currentUser.OrganisationId); //set the search criteria. If nothing passed in then take the values from the settings. If values past in then this is the dynamic changes made on the list screen and resubmitted int? maxDistanceFilter = maxDistance ?? currentUser.MaxDistanceFilter ?? 1500; double?maxAgeFilter = maxAge ?? currentUser.MaxAgeFilter ?? 9999; //Get the group Member IDs from groups that this user/organisation are part of, so we can remove them from the list List <Guid> groupMemberOrgIds = null; if (currentUser.SelectionLevelFilter == ExternalSearchLevelEnum.Group) { groupMemberOrgIds = GroupMembersHelpers.GetGroupsMembersOrgGuidsForGroupsFromOrg(db, currentOrg.OrganisationId); } //build the age filter to apply when building list double negativeDays = 0 - maxAgeFilter.Value; var dateCheck = DateTime.Now.AddDays(negativeDays); //build list depending on whether to filter on groups or not (settings, search level = groups) if (groupMemberOrgIds == null) { list = (from al in db.AvailableListings join org in db.Organisations on al.ListingOriginatorOrganisationId equals org.OrganisationId where (al.ListingOriginatorOrganisationId != currentUser.OrganisationId && (al.ListingStatus == ItemEnums.ItemRequiredListingStatusEnum.Open || al.ListingStatus == ItemEnums.ItemRequiredListingStatusEnum.Partial) && al.ListingOriginatorDateTime >= dateCheck) select new AvailableListingGeneralViewModel() { ListingId = al.ListingId, ItemDescription = al.ItemDescription, ItemType = al.ItemType, QuantityOutstanding = al.QuantityOutstanding, UoM = al.UoM, AvailableTo = al.AvailableTo, ItemCondition = al.ItemCondition, ExpiryDate = al.SellByDate ?? al.UseByDate, DeliveryAvailable = al.DeliveryAvailable, SupplierDetails = org.OrganisationName, ListingOriginatorOrganisationId = al.ListingOriginatorOrganisationId, ListingOrganisationPostcode = al.ListingOrganisationPostcode }).Distinct().ToList(); } else { list = (from al in db.AvailableListings join org in db.Organisations on al.ListingOriginatorOrganisationId equals org.OrganisationId join grpmem in groupMemberOrgIds on al.ListingOriginatorOrganisationId equals grpmem where (al.ListingOriginatorOrganisationId != currentUser.OrganisationId && (al.ListingStatus == ItemEnums.ItemRequiredListingStatusEnum.Open || al.ListingStatus == ItemEnums.ItemRequiredListingStatusEnum.Partial) && al.ListingOriginatorDateTime >= dateCheck) select new AvailableListingGeneralViewModel() { ListingId = al.ListingId, ItemDescription = al.ItemDescription, ItemType = al.ItemType, QuantityOutstanding = al.QuantityOutstanding, UoM = al.UoM, AvailableTo = al.AvailableTo, ItemCondition = al.ItemCondition, ExpiryDate = al.SellByDate ?? al.UseByDate, DeliveryAvailable = al.DeliveryAvailable, SupplierDetails = org.OrganisationName, ListingOriginatorOrganisationId = al.ListingOriginatorOrganisationId, ListingOrganisationPostcode = al.ListingOrganisationPostcode }).Distinct().ToList(); } //Filter by DISTANCE and add OFFER info also. //hold list of organisationIds already checked - set to true if within range Dictionary <Guid, bool> listingOrgIds = new Dictionary <Guid, bool>(); //hold new list from old List <AvailableListingGeneralViewModel> newList = new List <AvailableListingGeneralViewModel>(); foreach (AvailableListingGeneralViewModel item in list) { //if we have checked this org before then just add or not depending on flag if (listingOrgIds.ContainsKey(item.ListingOriginatorOrganisationId)) { if (listingOrgIds[item.ListingOriginatorOrganisationId]) { //quick check for offer Offer offer = OfferHelpers.GetOfferForListingByUser(db, item.ListingId, currentUser.AppUserId, currentOrg.OrganisationId, currentOrg.ListingPrivacyLevel); if (offer == null) { item.OfferQty = 0.00M; } else { item.OfferId = offer.OfferId; item.OfferQty = offer.CurrentOfferQuantity; } newList.Add(item); } } else //add the org to the dictionary with the flag set and add to new list if within range { int distanceValue = DistanceHelpers.GetDistance(currentOrg.AddressPostcode, item.ListingOrganisationPostcode); if (distanceValue <= maxDistanceFilter) { listingOrgIds.Add(item.ListingOriginatorOrganisationId, true); //quick check for offer Offer offer = OfferHelpers.GetOfferForListingByUser(db, item.ListingId, currentUser.AppUserId, currentOrg.OrganisationId, currentOrg.ListingPrivacyLevel); if (offer == null) { item.OfferQty = 0.00M; } else { item.OfferId = offer.OfferId; item.OfferQty = offer.CurrentOfferQuantity; } newList.Add(item); } else { listingOrgIds.Add(item.ListingOriginatorOrganisationId, false); } } } AvailableListingGeneralViewListModel model = new AvailableListingGeneralViewListModel() { MaxDistance = maxDistanceFilter, MaxAge = maxAgeFilter, EditableFields = newList.Any(x => x.OfferId == null), //only set if there are no offers in the list Listing = newList }; return(model); }
public ActionResult Available(int?maxDistance, double?maxAge) { AvailableListingGeneralViewListModel model = AvailableListingViewHelpers.GetAvailableListingGeneralViewListModel(db, User, maxDistance, maxAge); return(View(model)); }