public static AvailableListing UpdateAvailableListingQuantities(ApplicationDbContext db, Guid listingId, ListingQuantityChange changeOfValue, decimal changeQty, IPrincipal user) { AvailableListing listing = AvailableListingHelpers.GetAvailableListing(db, listingId); listing.RecordChange = RecordChangeEnum.RecordUpdated; listing.RecordChangeBy = AppUserHelpers.GetAppUserIdFromUser(user); listing.RecordChangeOn = DateTime.Now; if (changeOfValue == ListingQuantityChange.Subtract) { listing.QuantityFulfilled += changeQty; listing.QuantityOutstanding -= changeQty; if (listing.QuantityOutstanding == 0) { listing.ListingStatus = ItemRequiredListingStatusEnum.Complete; listing.RecordChange = RecordChangeEnum.ListingStatusChange; } } else { listing.QuantityFulfilled -= changeQty; listing.QuantityOutstanding += changeQty; } db.Entry(listing).State = EntityState.Modified; db.SaveChanges(); return(listing); }
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 OfferManageView GetOfferManageViewForOffer(ApplicationDbContext db, Offer offer, IPrincipal user) { AppUser offerAppUser = AppUserHelpers.GetAppUser(db, offer.OfferOriginatorAppUserId); AvailableListing availableListing = null; RequirementListing requirementListing = null; AppUser listingAppUser = null; switch (offer.ListingType) { case ListingTypeEnum.Available: availableListing = AvailableListingHelpers.GetAvailableListing(db, offer.ListingId); listingAppUser = AppUserHelpers.GetAppUser(db, availableListing.ListingOriginatorAppUserId); break; case ListingTypeEnum.Requirement: requirementListing = RequirementListingHelpers.GetRequirementListing(db, offer.ListingId); listingAppUser = AppUserHelpers.GetAppUser(db, requirementListing.ListingOriginatorAppUserId); break; } OfferManageView offerManageView = new OfferManageView() { OfferDetails = offer, AvailableListingDetails = availableListing, RequirementListingDetails = requirementListing, OfferAppUserDetails = offerAppUser, ListingAppUserDetails = listingAppUser, OfferBranchDetails = BranchHelpers.GetBranch(db, offerAppUser.CurrentBranchId), ListingBranchDetails = BranchHelpers.GetBranch(db, listingAppUser.CurrentBranchId), OfferAppUserSettings = AppUserSettingsHelpers.GetAppUserSettingsForUser(db, offerAppUser.AppUserId), ListingAppUserSettings = AppUserSettingsHelpers.GetAppUserSettingsForUser(db, listingAppUser.AppUserId) }; AppUser thisAppUser = AppUserHelpers.GetAppUser(db, user); //If we allow branch trading then differentiate between branches for in/out trading, otherwise it is at company level Company thisCompany = CompanyHelpers.GetCompanyForUser(db, user); //set Inhouse flag offerManageView.InhouseOffer = OfferProcessHelpers.SetInhouseFlag(offer, thisAppUser, thisCompany); //set buttons bool?displayAcceptButton = null; bool?displayRejectButton = null; bool?displayCounterButton = null; bool?displayOfferButton = null; OfferProcessHelpers.SetOrderButtons(db, user, offerManageView, out displayAcceptButton, out displayRejectButton, out displayCounterButton, out displayOfferButton); offerManageView.DisplayAcceptButton = displayAcceptButton; offerManageView.DisplayRejectButton = displayRejectButton; offerManageView.DisplayCounterButton = displayCounterButton; offerManageView.DisplayOfferButton = displayOfferButton; return(offerManageView); }
//Build a NotificationsViewModel record from a Notification public static NotificationViewModel CreateNotificationsViewModel(ApplicationDbContext db, Notification notification) { string referenceInfo = ""; switch (notification.NotificationType) { case NotificationTypeEnum.NewOfferReceived: Offer offer1 = OfferHelpers.GetOffer(db, notification.ReferenceKey); referenceInfo = offer1.ItemDescription + " x " + offer1.CurrentOfferQuantity.ToString(); break; case NotificationTypeEnum.CounterOfferReceived: Offer offer2 = OfferHelpers.GetOffer(db, notification.ReferenceKey); referenceInfo = offer2.ItemDescription + " x " + offer2.CounterOfferQuantity.ToString(); break; case NotificationTypeEnum.NewOrderReceived: Order order = OrderHelpers.GetOrder(db, notification.ReferenceKey); switch (order.ListingType) { case ListingTypeEnum.Available: AvailableListing listingA = AvailableListingHelpers.GetAvailableListing(db, order.ListingId.Value); referenceInfo = listingA.ItemDescription = " x " + order.OrderQuanity; break; case ListingTypeEnum.Requirement: RequiredListing listingB = RequiredListingHelpers.GetRequiredListing(db, order.ListingId.Value); referenceInfo = listingB.ItemDescription = " x " + order.OrderQuanity; break; } break; } //build view NotificationViewModel view = new NotificationViewModel() { NotificationId = notification.NotificationId, NotificationType = notification.NotificationType, NotificationDescription = notification.NotificationDescription, ReferenceInformation = referenceInfo, AppUser = AppUserHelpers.GetAppUser(db, notification.AppUserId.Value), ChangedOn = notification.RecordChangeOn, ChangedBy = AppUserHelpers.GetAppUserName(db, notification.RecordChangeBy) }; return(view); }
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 AvailableListingEditView GetAvailableListingEditView(ApplicationDbContext db, Guid listingId, IPrincipal user) { AvailableListing availableListing = AvailableListingHelpers.GetAvailableListing(db, listingId); AppUser listingAppUser = AppUserHelpers.GetAppUser(db, availableListing.ListingOriginatorAppUserId); Branch listingBranch = BranchHelpers.GetBranch(db, availableListing.ListingOriginatorBranchId); Company listingCompany = CompanyHelpers.GetCompany(db, availableListing.ListingOriginatorCompanyId); ViewButtons buttons = ViewButtonsHelpers.GetAvailableButtonsForSingleView(db, listingAppUser, listingBranch, listingCompany, user); AvailableListingEditView view = new AvailableListingEditView() { ListingId = availableListing.ListingId, ItemDescription = availableListing.ItemDescription, ItemCategory = availableListing.ItemCategory, ItemType = availableListing.ItemType, QuantityRequired = availableListing.QuantityRequired, QuantityFulfilled = availableListing.QuantityFulfilled, QuantityOutstanding = availableListing.QuantityOutstanding, UoM = availableListing.UoM, AvailableFrom = availableListing.AvailableFrom, AvailableTo = availableListing.AvailableTo, ItemCondition = availableListing.ItemCondition, DisplayUntilDate = availableListing.DisplayUntilDate, SellByDate = availableListing.SellByDate, UseByDate = availableListing.UseByDate, DeliveryAvailable = availableListing.DeliveryAvailable, ListingStatus = availableListing.ListingStatus, ListingOriginatorDateTime = availableListing.ListingOriginatorDateTime, ListingAppUser = listingAppUser, ListingBranchDetails = listingBranch, ListingCompanyDetails = listingCompany, Buttons = buttons }; return(view); }
public static OrderEditView GetOrderEditView(ApplicationDbContext db, Guid orderId, IPrincipal user) { Order orderDetails = OrderHelpers.GetOrder(db, orderId); AppUser orderAppUser = null; Branch orderBranch = null; AppUser offerAppUser = null; Branch offerBranch = null; AppUser listingAppUser = null; Branch listingBranch = null; Offer offerDetails = null; AvailableListing availableListing = null; RequirementListing requirementListing = null; if (orderDetails.OrderOriginatorAppUserId != null) { if (orderDetails.OrderOriginatorAppUserId.Value != Guid.Empty) { orderAppUser = AppUserHelpers.GetAppUser(db, orderDetails.OrderOriginatorAppUserId.Value); } } if (orderDetails.OrderOriginatorBranchId != null) { if (orderDetails.OrderOriginatorBranchId.Value != Guid.Empty) { orderBranch = BranchHelpers.GetBranch(db, orderDetails.ListingOriginatorBranchId.Value); } } if (orderDetails.OfferOriginatorAppUserId != null) { if (orderDetails.OfferOriginatorAppUserId.Value != Guid.Empty) { offerAppUser = AppUserHelpers.GetAppUser(db, orderDetails.OfferOriginatorAppUserId.Value); } } if (orderDetails.OfferOriginatorBranchId != null) { if (orderDetails.OfferOriginatorBranchId.Value != Guid.Empty) { offerBranch = BranchHelpers.GetBranch(db, orderDetails.OfferOriginatorBranchId.Value); } } if (orderDetails.ListingOriginatorAppUserId != null) { if (orderDetails.ListingOriginatorAppUserId.Value != Guid.Empty) { listingAppUser = AppUserHelpers.GetAppUser(db, orderDetails.ListingOriginatorAppUserId.Value); } } if (orderDetails.ListingOriginatorBranchId != null) { if (orderDetails.ListingOriginatorBranchId.Value != Guid.Empty) { listingBranch = BranchHelpers.GetBranch(db, orderDetails.ListingOriginatorBranchId.Value); } } if (orderDetails.OfferId != null) { if (orderDetails.OfferId.Value != Guid.Empty) { offerDetails = OfferHelpers.GetOffer(db, orderDetails.OfferId.Value); if (orderDetails.ListingId != null) { if (orderDetails.ListingId.Value != Guid.Empty) { switch (offerDetails.ListingType) { case ListingTypeEnum.Available: availableListing = AvailableListingHelpers.GetAvailableListing(db, orderDetails.ListingId.Value); break; case ListingTypeEnum.Requirement: requirementListing = RequirementListingHelpers.GetRequirementListing(db, orderDetails.ListingId.Value); break; } } } } } OrderEditView view = new OrderEditView() { OrderId = orderDetails.OrderId, ListingType = orderDetails.ListingType, OrderQuanity = orderDetails.OrderQuanity, OrderStatus = orderDetails.OrderStatus, OrderCreationDateTime = orderDetails.OrderCreationDateTime, OrderDistributionDateTime = orderDetails.OrderDistributionDateTime, OrderDeliveredDateTime = orderDetails.OrderDeliveredDateTime, OrderCollectedDateTime = orderDetails.OrderCollectedDateTime, OrderClosedDateTime = orderDetails.OrderClosedDateTime, OrderAppUser = orderAppUser, OrderBranchDetails = orderBranch, OfferId = orderDetails.OfferId.GetValueOrDefault(), OfferAppUser = offerAppUser, OfferBranchDetails = offerBranch, OfferDetails = offerDetails, ListingId = orderDetails.ListingId.GetValueOrDefault(), ListingAppUser = listingAppUser, ListingBranchDetails = listingBranch, AvailableListingDetails = availableListing, RequirementListingDetails = requirementListing }; AppUser thisAppUser = AppUserHelpers.GetAppUser(db, user); //If we allow branch trading then differentiate between branches for in/out trading, otherwise it is at company level Company thisCompany = CompanyHelpers.GetCompanyForUser(db, user); //set Inhouse flag view.InhouseOrder = OrderProcessHelpers.SetInhouseFlag(orderDetails, thisAppUser, thisCompany); //Set OrderOut flag view.OrderOut = OrderProcessHelpers.SetOrderOutFlag(orderDetails, view.InhouseOrder); //set buttons bool?displayDespatchButton = null; bool?displayDeliveredButton = null; bool?displayReceivedButton = null; bool?displayCollectedButton = null; bool?displayClosedButton = null; OrderProcessHelpers.SetOrderButtons(db, user, orderDetails, view.OrderOut, out displayDespatchButton, out displayDeliveredButton, out displayReceivedButton, out displayCollectedButton, out displayClosedButton); view.DisplayDespatchButton = displayDespatchButton; view.DisplayDeliveredButton = displayDeliveredButton; view.DisplayReceivedButton = displayReceivedButton; view.DisplayCollectedButton = displayCollectedButton; view.DisplayClosedButton = displayClosedButton; return(view); }
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 OrderViewModel GetOfferViewModel(ApplicationDbContext db, HttpRequestBase request, Guid orderId, string breadcrumb, string callingActionDisplayName, bool displayOnly, string type, bool?recalled, string controllerValue, string actionValue, IPrincipal user) { AppUser currentUser = AppUserHelpers.GetAppUser(db, user); Dictionary <int, string> breadcrumbDictionary = new Dictionary <int, string>(); breadcrumbDictionary.Add(0, breadcrumb); if (!recalled.HasValue || recalled.Value != true) { GeneralHelpers.GetCallingDetailsFromRequest(request, controllerValue, actionValue, out controllerValue, out actionValue); } Order order = OrderHelpers.GetOrder(db, orderId); string itemDescription = ""; ItemTypeEnum itemType = ItemTypeEnum.Canned; string uoM = ""; if (order.ListingType == ListingTypeEnum.Available) { AvailableListing listing = AvailableListingHelpers.GetAvailableListing(db, order.ListingId.Value); itemDescription = listing.ItemDescription; itemType = listing.ItemType; uoM = listing.UoM; } else { RequiredListing listing = RequiredListingHelpers.GetRequiredListing(db, order.ListingId.Value); itemDescription = listing.ItemDescription; itemType = listing.ItemType; uoM = listing.UoM; } OrderViewModel model = new OrderViewModel() { DisplayOnly = displayOnly, Breadcrumb = breadcrumb, BreadcrumbDictionary = breadcrumbDictionary, Type = type, OrderId = order.OrderId, ListingType = order.ListingType, ItemDescription = itemDescription, ItemType = itemType, UoM = uoM, OrderQuanity = order.OrderQuanity, OrderInStatus = order.OrderInStatus, OrderOutStatus = order.OrderOutStatus, OrderCreationDateTime = order.OrderCreationDateTime, OrderDistributionDateTime = order.OrderDistributionDateTime, OrderDistributedBy = AppUserHelpers.GetAppUserName(db, order.OrderDistributedBy), OrderDeliveredDateTime = order.OrderDeliveredDateTime, OrderDeliveredBy = AppUserHelpers.GetAppUserName(db, order.OrderDeliveredBy), OrderCollectedDateTime = order.OrderCollectedDateTime, OrderCollectedBy = AppUserHelpers.GetAppUserName(db, order.OrderCollectedBy), OrderReceivedDateTime = order.OrderReceivedDateTime, OrderReceivedBy = AppUserHelpers.GetAppUserName(db, order.OrderReceivedBy), OrderInClosedDateTime = order.OrderInClosedDateTime, OrderInClosedBy = AppUserHelpers.GetAppUserName(db, order.OrderInClosedBy), OrderOutClosedDateTime = order.OrderOutClosedDateTime, OrderOutClosedBy = AppUserHelpers.GetAppUserName(db, order.OrderOutClosedBy), OrderOriginatorAppUser = AppUserHelpers.GetAppUserName(db, order.OrderOriginatorAppUserId), OrderOriginatorOrganisation = OrganisationHelpers.GetOrganisationName(db, order.OrderOriginatorOrganisationId), OrderOriginatorDateTime = order.OrderOriginatorDateTime, OfferId = order.OfferId, OfferOriginatorAppUser = AppUserHelpers.GetAppUserName(db, order.OfferOriginatorAppUserId), OfferOriginatorOrganisation = OrganisationHelpers.GetOrganisationName(db, order.OfferOriginatorOrganisationId), ListingId = order.ListingId, ListingOriginatorAppUser = AppUserHelpers.GetAppUserName(db, order.ListingOriginatorAppUserId), ListingOriginatorOrganisation = OrganisationHelpers.GetOrganisationName(db, order.ListingOriginatorOrganisationId), CallingAction = actionValue, CallingActionDisplayName = callingActionDisplayName, CallingController = controllerValue, BreadcrumbTrail = breadcrumbDictionary }; return(model); }