Example #1
0
        public static List <AvailableListing> GetAllGeneralInfoFilteredAvailableListings(ApplicationDbContext db, Guid appUserId)
        {
            AppUser         appUser  = AppUserHelpers.GetAppUser(db, appUserId);
            Branch          branch   = BranchHelpers.GetBranch(db, appUser.CurrentBranchId);
            AppUserSettings settings = AppUserSettingsHelpers.GetAppUserSettingsForUser(db, appUserId);

            var dateCheck           = DateTime.MinValue;
            int settingsMaxDistance = settings.AvailableListingGeneralInfoMaxDistance ?? 0;

            //Create list within the time frame if setting set
            List <AvailableListing> list = (from rl in db.AvailableListings
                                            where ((rl.ListingStatus == ItemRequiredListingStatusEnum.Open || rl.ListingStatus == ItemRequiredListingStatusEnum.Partial) &&
                                                   rl.ListingOriginatorDateTime >= dateCheck)
                                            orderby rl.AvailableTo ?? DateTime.MaxValue
                                            select rl).ToList();

            //Now bring in the Selection Level sort
            switch (settings.AvailableListingGeneralInfoExternalSelectionLevel)
            {
            case ExternalSearchLevelEnum.All:     //do nothing
                break;

            case ExternalSearchLevelEnum.Branch:     //user's current branch to filter
                list = list.Where(l => l.ListingOriginatorBranchId == branch.BranchId).ToList();
                break;

            case ExternalSearchLevelEnum.Company:     //user's current company to filter
                list = list.Where(l => l.ListingOriginatorCompanyId == branch.CompanyId).ToList();
                break;

            case ExternalSearchLevelEnum.Group:     //LSLSLS user's built group sets to filter ***TO BE DONE***
                break;
            }

            //Now sort by distance
            if (settingsMaxDistance > 0)
            {
                List <AvailableListing> removeList = new List <AvailableListing>();

                foreach (AvailableListing listing in list)
                {
                    DistanceHelpers distance      = new DistanceHelpers();
                    int             distanceValue = distance.GetDistance(branch.AddressPostcode, listing.ListingBranchPostcode);

                    if (distanceValue > settingsMaxDistance)
                    {
                        removeList.Add(listing);
                    }
                }

                if (removeList.Count > 0)
                {
                    list.RemoveAll(l => removeList.Contains(l));
                }
            }

            return(list);
        }
Example #2
0
        public static List <Campaign> GetAllGeneralInfoFilteredCampaigns(ApplicationDbContext db, Guid appUserId)
        {
            AppUser         appUser  = AppUserHelpers.GetAppUser(db, appUserId);
            Branch          branch   = BranchHelpers.GetBranch(db, appUser.CurrentBranchId);
            AppUserSettings settings = AppUserSettingsHelpers.GetAppUserSettingsForUser(db, appUserId);

            var dateCheck           = DateTime.MinValue;
            int settingsMaxDistance = settings.CampaignGeneralInfoMaxDistance ?? 0;

            //Create list within the time frame if setting set
            List <Campaign> list = (from c in db.Campaigns
                                    where (c.EntityStatus == EntityStatusEnum.Active && c.CampaignOriginatorDateTime >= dateCheck)
                                    orderby c.CampaignEndDateTime ?? DateTime.MaxValue
                                    select c).ToList();

            //Now bring in the Selection Level sort
            switch (settings.CampaignGeneralInfoExternalSelectionLevel)
            {
            case ExternalSearchLevelEnum.All:     //do nothing
                break;

            case ExternalSearchLevelEnum.Branch:     //user's current branch to filter
                list = list.Where(l => l.CampaignOriginatorBranchId == branch.BranchId).ToList();
                break;

            case ExternalSearchLevelEnum.Company:     //user's current company to filter
                list = list.Where(l => l.CampaignOriginatorCompanyId == branch.CompanyId).ToList();
                break;

            case ExternalSearchLevelEnum.Group:     //user's built group sets to filter ***TO BE DONE***
                break;
            }

            //Now sort by distance
            if (settingsMaxDistance > 0)
            {
                List <Campaign> removeList = new List <Campaign>();

                foreach (Campaign campaign in list)
                {
                    DistanceHelpers distance      = new DistanceHelpers();
                    int             distanceValue = distance.GetDistance(branch.AddressPostcode, campaign.LocationAddressPostcode);

                    if (distanceValue > settingsMaxDistance)
                    {
                        removeList.Add(campaign);
                    }
                }

                if (removeList.Count > 0)
                {
                    list.RemoveAll(l => removeList.Contains(l));
                }
            }

            return(list);
        }
Example #3
0
        public static List <AvailableListing> FilterAvailableListingsByDistance(ApplicationDbContext db, List <AvailableListing> currentList, Guid organisationId, int maxDistanceFilter)
        {
            //get this organisation post code to match against those of the listed organisations
            Organisation thisOrganisation = OrganisationHelpers.GetOrganisation(db, organisationId);

            //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 <AvailableListing> newList = new List <AvailableListing>();

            foreach (AvailableListing item in currentList)
            {
                //if we have checked this org before then just add or not depending on flag
                if (listingOrgIds.ContainsKey(item.ListingOriginatorOrganisationId))
                {
                    if (listingOrgIds[item.ListingOriginatorOrganisationId])
                    {
                        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(thisOrganisation.AddressPostcode, item.ListingOrganisationPostcode);
                    if (distanceValue <= maxDistanceFilter)
                    {
                        listingOrgIds.Add(item.ListingOriginatorOrganisationId, true);
                        newList.Add(item);
                    }
                    else
                    {
                        listingOrgIds.Add(item.ListingOriginatorOrganisationId, false);
                    }
                }
            }

            return(newList);
        }
        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);
        }