public ActionResult Required(RequiredListingGeneralViewListModel model)
        {
            if (ModelState.IsValid)
            {
                if (Request.Form["savebutton"] != null)
                {
                    //Create offers
                    OfferHelpers.CreateOffers(db, null, model, ListingTypeEnum.Requirement, User);
                }

                return(RedirectToAction("Required", "GeneralInfo", new { maxDistance = model.MaxDistance, maxAge = model.MaxAge }));
            }

            return(View(model));
        }
Example #2
0
        public static RequiredListingGeneralViewListModel GetRequiredListingGeneralViewListModel(ApplicationDbContext db, IPrincipal user, int?maxDistance, double?maxAge)
        {
            List <RequiredListingGeneralViewModel> list = new List <RequiredListingGeneralViewModel>();

            //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 rl in db.RequiredListings
                        join org in db.Organisations on rl.ListingOriginatorOrganisationId equals org.OrganisationId
                        where (rl.ListingOriginatorOrganisationId != currentUser.OrganisationId && (rl.ListingStatus == ItemEnums.ItemRequiredListingStatusEnum.Open || rl.ListingStatus == ItemEnums.ItemRequiredListingStatusEnum.Partial) &&
                               rl.ListingOriginatorDateTime >= dateCheck)
                        select new RequiredListingGeneralViewModel()
                {
                    ListingId = rl.ListingId,
                    ItemDescription = rl.ItemDescription,
                    ItemType = rl.ItemType,
                    QuantityOutstanding = rl.QuantityOutstanding,
                    UoM = rl.UoM,
                    RequiredTo = rl.RequiredTo,
                    AcceptDamagedItems = rl.AcceptDamagedItems,
                    AcceptOutOfDateItems = rl.AcceptOutOfDateItems,
                    CollectionAvailable = rl.CollectionAvailable,
                    RequesterDetails = org.OrganisationName,
                    ListingOriginatorOrganisationId = rl.ListingOriginatorOrganisationId,
                    ListingOrganisationPostcode = rl.ListingOrganisationPostcode
                }).Distinct().ToList();
            }
            else
            {
                list = (from rl in db.RequiredListings
                        join org in db.Organisations on rl.ListingOriginatorOrganisationId equals org.OrganisationId
                        join grpmem in groupMemberOrgIds on rl.ListingOriginatorOrganisationId equals grpmem
                        where (rl.ListingOriginatorOrganisationId != currentUser.OrganisationId && (rl.ListingStatus == ItemEnums.ItemRequiredListingStatusEnum.Open || rl.ListingStatus == ItemEnums.ItemRequiredListingStatusEnum.Partial) &&
                               rl.ListingOriginatorDateTime >= dateCheck)
                        select new RequiredListingGeneralViewModel()
                {
                    ListingId = rl.ListingId,
                    ItemDescription = rl.ItemDescription,
                    ItemType = rl.ItemType,
                    QuantityOutstanding = rl.QuantityOutstanding,
                    UoM = rl.UoM,
                    RequiredTo = rl.RequiredTo,
                    AcceptDamagedItems = rl.AcceptDamagedItems,
                    AcceptOutOfDateItems = rl.AcceptOutOfDateItems,
                    CollectionAvailable = rl.CollectionAvailable,
                    RequesterDetails = org.OrganisationName,
                    ListingOriginatorOrganisationId = rl.ListingOriginatorOrganisationId,
                    ListingOrganisationPostcode = rl.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 <RequiredListingGeneralViewModel> newList = new List <RequiredListingGeneralViewModel>();

            foreach (RequiredListingGeneralViewModel 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.RequiredQty = 0.00M;
                        }
                        else
                        {
                            item.OfferId     = offer.OfferId;
                            item.RequiredQty = 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.RequiredQty = 0.00M;
                        }
                        else
                        {
                            item.OfferId     = offer.OfferId;
                            item.RequiredQty = offer.CurrentOfferQuantity;
                        }

                        newList.Add(item);
                    }
                    else
                    {
                        listingOrgIds.Add(item.ListingOriginatorOrganisationId, false);
                    }
                }
            }

            RequiredListingGeneralViewListModel model = new RequiredListingGeneralViewListModel()
            {
                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 Required(int?maxDistance, double?maxAge)
        {
            RequiredListingGeneralViewListModel model = RequiredListingViewHelpers.GetRequiredListingGeneralViewListModel(db, User, maxDistance, maxAge);

            return(View(model));
        }
Example #4
0
        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);
        }