public static Offer AcceptOffer(ApplicationDbContext db, Guid offerId, IPrincipal user) { Offer offer = db.Offers.Find(offerId); AppUser appUser = AppUserHelpers.GetAppUser(db, user); Order order = OrderHelpers.CreateOrder(db, offer, user); offer.OfferStatus = OfferStatusEnum.Accepted; offer.AcceptedBy = appUser.AppUserId; offer.AcceptedOn = DateTime.Now; offer.OrderId = order.OrderId; offer.OrderOriginatorAppUserId = appUser.AppUserId; offer.OrderOriginatorOrganisationId = appUser.OrganisationId; offer.OrderOriginatorDateTime = DateTime.Now; db.Entry(offer).State = EntityState.Modified; db.SaveChanges(); //set any related actions to Closed NotificationHelpers.RemoveNotificationsForOffer(db, offerId, user); //set any related current offers to closed if there is no stock left (currentOfferQuantity = 0) if (offer.CurrentOfferQuantity == 0) { OfferHelpers.CloseOffersRelatedToListing(db, offer.ListingId, user); } //Create Action to show order ready Organisation org = OrganisationHelpers.GetOrganisation(db, offer.OfferOriginatorOrganisationId); NotificationHelpers.CreateNotification(db, NotificationTypeEnum.NewOrderReceived, "New order received from " + org.OrganisationName, order.OrderId, appUser.AppUserId, org.OrganisationId, user); return(offer); }
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 Offer UpdateOffer(ApplicationDbContext db, Guid offerid, OfferStatusEnum offerStatus, decimal?currentOfferQuantity, decimal?counterOfferQuantity, IPrincipal user) { Offer offer = OfferHelpers.GetOffer(db, offerid); switch (offerStatus) { case OfferStatusEnum.Reoffer: //update offer value and move counter value to previous if (currentOfferQuantity.HasValue) { offer.CurrentOfferQuantity = currentOfferQuantity.Value; offer.PreviousCounterOfferQuantity = offer.CounterOfferQuantity; offer.CounterOfferQuantity = null; offer.LastOfferOriginatorAppUserId = AppUserHelpers.GetAppUserIdFromUser(user); offer.LastOfferOriginatorDateTime = DateTime.Now; offer.OfferStatus = offerStatus; db.Entry(offer).State = EntityState.Modified; db.SaveChanges(); } break; case OfferStatusEnum.Countered: //update counter value and move current offer to previous offer if (counterOfferQuantity.HasValue) { offer.CounterOfferQuantity = counterOfferQuantity; offer.PreviousOfferQuantity = offer.CurrentOfferQuantity; offer.CurrentOfferQuantity = 0.00M; if (!offer.CounterOfferOriginatorOrganisationId.HasValue) { AppUser appUser = AppUserHelpers.GetAppUser(db, AppUserHelpers.GetAppUserIdFromUser(user)); offer.CounterOfferOriginatorAppUserId = appUser.AppUserId; offer.CounterOfferOriginatorDateTime = DateTime.Now; offer.CounterOfferOriginatorOrganisationId = appUser.OrganisationId; } else { offer.LastCounterOfferOriginatorAppUserId = AppUserHelpers.GetAppUserIdFromUser(user); offer.LastCounterOfferOriginatorDateTime = DateTime.Now; } offer.OfferStatus = offerStatus; db.Entry(offer).State = EntityState.Modified; db.SaveChanges(); } break; } //Create Action Organisation org = OrganisationHelpers.GetOrganisation(db, AppUserHelpers.GetAppUser(db, AppUserHelpers.GetAppUserIdFromUser(user)).OrganisationId); NotificationHelpers.CreateNotification(db, NotificationTypeEnum.NewOfferReceived, "New offer received from " + org.OrganisationName, offer.OfferId, offer.ListingOriginatorAppUserId.Value, offer.ListingOriginatorOrganisationId.Value, user); return(offer); }