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 AvailableListingDetailsViewModel CreateAvailableListingDetailsViewModel(ApplicationDbContext db, Guid listingId, string breadcrumb, bool displayOnly, HttpRequestBase request, string controllerValue, string actionValue, string callingActionDisplayName, Dictionary <int, string> breadcrumbDictionary, bool?recalled, IPrincipal user, int?maxDistance, double?maxAge) { AvailableListing listing = AvailableListingHelpers.GetAvailableListing(db, listingId); if (listing != null) { //Set up the calling fields if (!recalled.HasValue) { GeneralHelpers.GetCallingDetailsFromRequest(request, controllerValue, actionValue, out controllerValue, out actionValue); } AppUser recordChangedBy = AppUserHelpers.GetAppUser(db, listing.RecordChangeBy); AppUser listingOriginatorAppUser = AppUserHelpers.GetAppUser(db, listing.ListingOriginatorAppUserId); AvailableListingDetailsViewModel model = new AvailableListingDetailsViewModel() { MaxDistance = maxDistance, MaxAge = maxAge, Breadcrumb = breadcrumb, DisplayOnly = displayOnly, ListingId = listing.ListingId, ItemDescription = listing.ItemDescription, ItemCategory = listing.ItemCategory, ItemType = listing.ItemType, QuantityAvailable = listing.QuantityAvailable, QuantityFulfilled = listing.QuantityFulfilled, QuantityOutstanding = listing.QuantityOutstanding, UoM = listing.UoM, AvailableFrom = listing.AvailableFrom, AvailableTo = listing.AvailableTo, ItemCondition = listing.ItemCondition, DisplayUntilDate = listing.DisplayUntilDate, SellByDate = listing.SellByDate, UseByDate = listing.UseByDate, DeliveryAvailable = listing.DeliveryAvailable, ListingStatus = listing.ListingStatus, ListingOrganisationPostcode = listing.ListingOrganisationPostcode, RecordChange = listing.RecordChange, RecordChangeOn = listing.RecordChangeOn, RecordChangeByName = recordChangedBy.FirstName + " " + recordChangedBy.LastName, RecordChangeByEmail = recordChangedBy.LoginEmail, ListingOriginatorAppUserName = listingOriginatorAppUser.FirstName + " " + listingOriginatorAppUser.LastName, ListingOriginatorAppUserEmail = listingOriginatorAppUser.LoginEmail, ListingOriginatorDateTime = listing.ListingOriginatorDateTime, CallingController = controllerValue, CallingAction = actionValue, CallingActionDisplayName = callingActionDisplayName, BreadcrumbTrail = breadcrumbDictionary }; if (controllerValue == "GeneralInfo" && actionValue == "Available") { //Get user info for the offer side of the display AppUser currentUser = AppUserHelpers.GetAppUser(db, user); Organisation currentOrg = OrganisationHelpers.GetOrganisation(db, currentUser.OrganisationId); Offer offer = OfferHelpers.GetOfferForListingByUser(db, listing.ListingId, currentUser.AppUserId, currentOrg.OrganisationId, currentOrg.ListingPrivacyLevel); if (offer == null) { model.OfferDescription = "Make an offer"; } else { model.OfferDescription = "Current offer"; model.OfferId = offer.OfferId; model.OfferQty = offer.CurrentOfferQuantity; model.OfferCounterQty = offer.CounterOfferQuantity; model.OfferStatus = offer.OfferStatus; } } return(model); } else { return(null); } }
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); }