public static decimal GetListingQuantityForListingIdListingType(ApplicationDbContext db, Guid listingId, ListingTypeEnum listingType)
        {
            decimal quantityOutstanding = 0.00M;

            switch (listingType)
            {
            case ListingTypeEnum.Available:
                quantityOutstanding = AvailableListingHelpers.GetAvailableListing(db, listingId).QuantityOutstanding;
                break;

            case ListingTypeEnum.Requirement:
                quantityOutstanding = RequiredListingHelpers.GetRequiredListing(db, listingId).QuantityOutstanding;
                break;
            }

            return(quantityOutstanding);
        }
        public static Offer CreateOffer(ApplicationDbContext db, Guid listingId, decimal?offerQty, ListingTypeEnum listingType, AppUser currentUser, IPrincipal user)
        {
            if (currentUser == null)
            {
                currentUser = AppUserHelpers.GetAppUser(db, user);
            }

            Guid     listingOrigAppUserId = Guid.Empty;
            Guid     listingOrigOrgId     = Guid.Empty;
            DateTime listingOrigDateTime  = DateTime.MinValue;
            string   itemDescription      = "";

            //Get originator information for the correct listing
            if (listingType == ListingTypeEnum.Available)
            {
                AvailableListing availableListing = AvailableListingHelpers.GetAvailableListing(db, listingId);
                listingOrigAppUserId = availableListing.ListingOriginatorAppUserId;
                listingOrigOrgId     = availableListing.ListingOriginatorOrganisationId;
                listingOrigDateTime  = availableListing.ListingOriginatorDateTime;
                itemDescription      = availableListing.ItemDescription;
            }
            else
            {
                RequiredListing requiredListing = RequiredListingHelpers.GetRequiredListing(db, listingId);
                listingOrigAppUserId = requiredListing.ListingOriginatorAppUserId;
                listingOrigOrgId     = requiredListing.ListingOriginatorOrganisationId;
                listingOrigDateTime  = requiredListing.ListingOriginatorDateTime;
                itemDescription      = requiredListing.ItemDescription;
            }

            //create offer
            Offer offer = new Offer()
            {
                OfferId                         = Guid.NewGuid(),
                ListingId                       = listingId,
                ListingType                     = listingType,
                OfferStatus                     = OfferStatusEnum.New,
                ItemDescription                 = itemDescription,
                CurrentOfferQuantity            = offerQty.Value,
                OfferOriginatorAppUserId        = currentUser.AppUserId,
                OfferOriginatorOrganisationId   = currentUser.OrganisationId,
                OfferOriginatorDateTime         = DateTime.Now,
                ListingOriginatorAppUserId      = listingOrigAppUserId,
                ListingOriginatorOrganisationId = listingOrigOrgId,
                ListingOriginatorDateTime       = listingOrigDateTime
            };

            db.Offers.Add(offer);
            db.SaveChanges();

            //Create Action
            Organisation org = OrganisationHelpers.GetOrganisation(db, currentUser.OrganisationId);

            NotificationHelpers.CreateNotification(db, NotificationTypeEnum.NewOfferReceived, "New offer received from " + org.OrganisationName, offer.OfferId, listingOrigAppUserId, listingOrigOrgId, user);

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