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 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); }
public static DashboardView GetDashboardViewLogin(ApplicationDbContext db, IPrincipal user) { AppUser appUser = AppUserHelpers.GetAppUser(db, user); //initialise the view DashboardView dashboardView = new DashboardView(); //get the campaigns and listings for this user List <Campaign> campaignsForUser = CampaignHelpers.GetAllCampaignsForUser(db, appUser.AppUserId, false); dashboardView.CampaignList = campaignsForUser; List <Campaign> campaignsDashboardList = CampaignHelpers.GetAllDashboardFilteredCampaigns(db, appUser.AppUserId); dashboardView.CampaignDashboardList = campaignsDashboardList; List <RequirementListing> requirementListingsForUser = RequirementListingHelpers.GetAllRequirementListingsForUser(db, appUser.AppUserId, false); dashboardView.RequirementListingList = requirementListingsForUser; List <RequirementListing> requirementListingDashboardList = RequirementListingHelpers.GetAllDashboardFilteredRequirementListings(db, appUser.AppUserId); dashboardView.RequirementListingDashboardList = requirementListingDashboardList; List <AvailableListing> availableListingsForUser = AvailableListingHelpers.GetAllAvailableListingsForUser(db, appUser.AppUserId, false); dashboardView.AvailableListingList = availableListingsForUser; List <AvailableListing> availableListingDashboardList = AvailableListingHelpers.GetAllDashboardFilteredAvailableListings(db, appUser.AppUserId); dashboardView.AvailableListingDashboardList = availableListingDashboardList; List <Offer> offersForUser = OfferHelpers.GetAllOffersForUser(db, appUser.AppUserId, false); dashboardView.OfferList = offersForUser; List <Order> ordersForUser = OrderHelpers.GetAllOrdersForUser(db, appUser.AppUserId, false); dashboardView.OrderList = ordersForUser; //get listings for admin areas if this user is not a 'User' - i.e. is Manager, Admin etc. if (user.Identity.GetCurrentUserRole() != "User") { List <UserTask> tasksForUser = UserTaskHelpers.GetUserTasksForUser(db, appUser.AppUserId); //LSLSLS get list of 'actions' once written dashboardView.UserTaskList = tasksForUser; } return(dashboardView); }
public static List <OfferManageView> GetAllOffersManageView(ApplicationDbContext db, IPrincipal user, bool getHistory) { List <OfferManageView> allOffersManageView = new List <OfferManageView>(); AppUser appUser = AppUserHelpers.GetAppUser(db, user); List <Offer> allOffersForUser = OfferHelpers.GetAllManageListingFilteredOffers(db, appUser.AppUserId, getHistory); foreach (Offer offerForUser in allOffersForUser) { allOffersManageView.Add(GetOfferManageViewForOffer(db, offerForUser, user)); } return(allOffersManageView); }
//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 Offer UpdateStatus(ApplicationDbContext db, Guid offerId, OfferStatusEnum newStatus, IPrincipal user) { Offer offer = OfferHelpers.GetOffer(db, offerId); offer.OfferStatus = newStatus; if (newStatus == OfferStatusEnum.ClosedNoStock) { offer.ClosedNoStockBy = AppUserHelpers.GetAppUserIdFromUser(user); offer.ClosedNoStockOn = DateTime.Now; } db.Entry(offer).State = EntityState.Modified; db.SaveChanges(); 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); }
public static OfferManageView GetOfferManageViewForOffer(ApplicationDbContext db, Guid offerId, IPrincipal user) { Offer offer = OfferHelpers.GetOffer(db, offerId); return(GetOfferManageViewForOffer(db, offer, user)); }
public static HomeDashboardView CreateHomeDashboardView(ApplicationDbContext db, IPrincipal user) { HomeDashboardView view = new HomeDashboardView(); Organisation currentOrg = OrganisationHelpers.GetOrganisation(db, AppUserHelpers.GetOrganisationIdFromUser(db, user)); DateTime weekAgo = DateTime.Now.AddDays(-7); DateTime dayAgo = DateTime.Now.AddDays(-1); List <Notification> openNotifications = NotificationHelpers.GetNotificationsForOrganisationFromUser(db, user, false); view.OpenNotifications = openNotifications.Count; view.OpenNotificationsThisWeek = (from oN in openNotifications where oN.RecordChangeOn >= weekAgo select oN).Count(); if (view.OpenNotificationsThisWeek == 0) { view.OpenNotificationsThisWeekPercent = 0; } else { view.OpenNotificationsThisWeekPercent = (Convert.ToDecimal(view.OpenNotificationsThisWeek) / Convert.ToDecimal(view.OpenNotifications)) * 100; } view.OpenNotificationsPastDay = (from oN in openNotifications where oN.RecordChangeOn >= dayAgo select oN).Count(); if (view.OpenNotificationsPastDay == 0) { view.OpenNotificationsPastDayPercent = 0; } else { view.OpenNotificationsPastDayPercent = (Convert.ToDecimal(view.OpenNotificationsPastDay) / Convert.ToDecimal(view.OpenNotifications)) * 100; } List <UserTask> openUserTasks = UserTasksHelpers.GetUserTasksForOrganisationFromUser(db, user, false); view.OpenTasks = openUserTasks.Count; view.OpenTasksThisWeek = (from oT in openUserTasks where oT.RecordChangeOn >= weekAgo select oT).Count(); if (view.OpenTasksThisWeek == 0) { view.OpenTasksThisWeekPercent = 0; } else { view.OpenTasksThisWeekPercent = (Convert.ToDecimal(view.OpenTasksThisWeek) / Convert.ToDecimal(view.OpenTasks)) * 100; } view.OpenTasksPastDay = (from oT in openUserTasks where oT.RecordChangeOn >= dayAgo select oT).Count(); if (view.OpenTasksPastDay == 0) { view.OpenTasksPastDayPercent = 0; } else { view.OpenTasksPastDayPercent = (Convert.ToDecimal(view.OpenTasksPastDay) / Convert.ToDecimal(view.OpenTasks)) * 100; } List <Offer> offersCreated = OfferHelpers.GetOpenOffersCreatedForOrganisation(db, currentOrg.OrganisationId); view.OffersCreatedOpen = offersCreated.Count; view.OffersCreatedCountered = (from oC in offersCreated where oC.OfferStatus == OfferStatusEnum.Countered select oC).Count(); if (view.OffersCreatedCountered == 0) { view.OffersCreatedCounteredPercent = 0; } else { view.OffersCreatedCounteredPercent = (Convert.ToDecimal(view.OffersCreatedCountered) / Convert.ToDecimal(view.OffersCreatedOpen)) * 100; } view.OffersCreatedReOffered = (from oC in offersCreated where oC.OfferStatus == OfferStatusEnum.Reoffer select oC).Count(); if (view.OffersCreatedReOffered == 0) { view.OffersCreatedReOfferedPercent = 0; } else { view.OffersCreatedReOfferedPercent = (Convert.ToDecimal(view.OffersCreatedReOffered) / Convert.ToDecimal(view.OffersCreatedOpen)) * 100; } List <Offer> offersCreatedClosed = OfferHelpers.GetAllOffersCreatedClosedForWeekForOrganisation(db, currentOrg.OrganisationId); view.OffersCreatedAcceptedThisWeek = (from oCC in offersCreatedClosed where oCC.OfferStatus == OfferStatusEnum.Accepted select oCC).Count(); view.OffersCreatedRejectedThisWeek = (from oCC in offersCreatedClosed where oCC.OfferStatus == OfferStatusEnum.Rejected select oCC).Count(); view.OffersCreatedClosedThisWeek = (from oCC in offersCreatedClosed where oCC.OfferStatus == OfferStatusEnum.ClosedNoStock select oCC).Count(); view.OffersCreatedClosedTotalThisWeek = view.OffersCreatedAcceptedThisWeek + view.OffersCreatedRejectedThisWeek + view.OffersCreatedClosedThisWeek; if (view.OffersCreatedAcceptedThisWeek == 0) { view.OffersCreatedAcceptedThisWeekPercent = 0; } else { view.OffersCreatedAcceptedThisWeekPercent = (Convert.ToDecimal(view.OffersCreatedAcceptedThisWeek) / Convert.ToDecimal(view.OffersCreatedClosedTotalThisWeek)) * 100; } if (view.OffersCreatedRejectedThisWeek == 0) { view.OffersCreatedRejectedThisWeekPercent = 0; } else { view.OffersCreatedRejectedThisWeekPercent = (Convert.ToDecimal(view.OffersCreatedRejectedThisWeek) / Convert.ToDecimal(view.OffersCreatedClosedTotalThisWeek)) * 100; } if (view.OffersCreatedClosedThisWeek == 0) { view.OffersCreatedClosedThisWeekPercent = 0; } else { view.OffersCreatedClosedThisWeekPercent = (Convert.ToDecimal(view.OffersCreatedClosedThisWeek) / Convert.ToDecimal(view.OffersCreatedClosedTotalThisWeek)) * 100; } List <Offer> offersReceived = OfferHelpers.GetOpenOffersReceivedForOrganisation(db, currentOrg.OrganisationId); view.OffersReceivedOpen = offersReceived.Count; view.OffersReceivedCountered = (from oR in offersReceived where oR.OfferStatus == OfferStatusEnum.Countered select oR).Count(); if (view.OffersReceivedCountered == 0) { view.OffersReceivedCounteredPercent = 0; } else { view.OffersReceivedCounteredPercent = (Convert.ToDecimal(view.OffersReceivedCountered) / Convert.ToDecimal(view.OffersReceivedOpen)) * 100; } view.OffersReceivedReOffered = (from oR in offersReceived where oR.OfferStatus == OfferStatusEnum.Reoffer select oR).Count(); if (view.OffersReceivedReOffered == 0) { view.OffersReceivedReOfferedPercent = 0; } else { view.OffersReceivedReOfferedPercent = (Convert.ToDecimal(view.OffersReceivedReOffered) / Convert.ToDecimal(view.OffersReceivedOpen)) * 100; } List <Offer> offersReceivedClosed = OfferHelpers.GetAllOffersReceivedClosedForWeekForOrganisation(db, currentOrg.OrganisationId); view.OffersReceivedAcceptedThisWeek = (from oRC in offersReceived where oRC.OfferStatus == OfferStatusEnum.Accepted select oRC).Count(); view.OffersReceivedRejectedThisWeek = (from oRC in offersReceived where oRC.OfferStatus == OfferStatusEnum.Rejected select oRC).Count(); view.OffersReceivedClosedThisWeek = (from oRC in offersReceived where oRC.OfferStatus == OfferStatusEnum.ClosedNoStock select oRC).Count(); view.OffersReceivedClosedTotalThisWeek = view.OffersReceivedAcceptedThisWeek + view.OffersReceivedRejectedThisWeek + view.OffersReceivedClosedThisWeek; if (view.OffersReceivedAcceptedThisWeek == 0) { view.OffersReceivedAcceptedThisWeekPercent = 0; } else { view.OffersReceivedAcceptedThisWeekPercent = (Convert.ToDecimal(view.OffersReceivedAcceptedThisWeek) / Convert.ToDecimal(view.OffersReceivedClosedTotalThisWeek)) * 100; } if (view.OffersReceivedRejectedThisWeek == 0) { view.OffersReceivedRejectedThisWeekPercent = 0; } else { view.OffersReceivedRejectedThisWeekPercent = (Convert.ToDecimal(view.OffersReceivedRejectedThisWeek) / Convert.ToDecimal(view.OffersReceivedClosedTotalThisWeek)) * 100; } if (view.OffersReceivedClosedThisWeek == 0) { view.OffersReceivedClosedThisWeekPercent = 0; } else { view.OffersReceivedClosedThisWeekPercent = (Convert.ToDecimal(view.OffersReceivedClosedThisWeek) / Convert.ToDecimal(view.OffersReceivedClosedTotalThisWeek)) * 100; } view.OrdersInOpen = OrderHelpers.GetOrdersInForOganisationCount(db, currentOrg.OrganisationId); List <Order> ordersIn = OrderHelpers.GetOrdersInForWeekForOrganisation(db, currentOrg.OrganisationId); view.OrdersInCollectedThisWeek = (from oI in ordersIn where oI.OrderInStatus == OrderInStatusEnum.Collected select oI).Count(); view.OrdersInReceivedThisWeek = (from oI in ordersIn where oI.OrderInStatus == OrderInStatusEnum.Received select oI).Count(); view.OrdersInClosedThisWeek = (from oI in ordersIn where oI.OrderInStatus == OrderInStatusEnum.Closed select oI).Count(); int ordersInClosedCount = view.OrdersInCollectedThisWeek + view.OrdersInReceivedThisWeek + view.OrdersInClosedThisWeek; if (view.OrdersInCollectedThisWeek == 0) { view.OrdersInCollectedThisWeekPercent = 0; } else { view.OrdersInCollectedThisWeekPercent = (Convert.ToDecimal(view.OrdersInCollectedThisWeek) / Convert.ToDecimal(ordersInClosedCount)) * 100; } if (view.OrdersInReceivedThisWeek == 0) { view.OrdersInReceivedThisWeekPercent = 0; } else { view.OrdersInReceivedThisWeekPercent = (Convert.ToDecimal(view.OrdersInReceivedThisWeek) / Convert.ToDecimal(ordersInClosedCount)) * 100; } if (view.OrdersInClosedThisWeek == 0) { view.OrdersInClosedThisWeekPercent = 0; } else { view.OrdersInClosedThisWeekPercent = (Convert.ToDecimal(view.OrdersInClosedThisWeek) / Convert.ToDecimal(ordersInClosedCount)) * 100; } view.OrdersOutOpen = OrderHelpers.GetOrdersOutForOganisationCount(db, currentOrg.OrganisationId); List <Order> ordersOut = OrderHelpers.GetOrdersOutForWeekForOganisation(db, currentOrg.OrganisationId); view.OrdersOutDespatchedThisWeek = (from oO in ordersOut where oO.OrderOutStatus == OrderOutStatusEnum.Dispatched select oO).Count(); view.OrdersOutDeliveredThisWeek = (from oO in ordersOut where oO.OrderOutStatus == OrderOutStatusEnum.Delivered select oO).Count(); view.OrdersOutClosedThisWeek = (from oO in ordersOut where oO.OrderOutStatus == OrderOutStatusEnum.Closed select oO).Count(); int ordersOutClosedCount = view.OrdersOutDespatchedThisWeek + view.OrdersOutDeliveredThisWeek + view.OrdersOutClosedThisWeek; if (view.OrdersOutDespatchedThisWeek == 0) { view.OrdersOutDespatchedThisWeekPercent = 0; } else { view.OrdersOutDespatchedThisWeekPercent = (Convert.ToDecimal(view.OrdersOutDespatchedThisWeek) / Convert.ToDecimal(ordersOutClosedCount)) * 100; } if (view.OrdersOutDeliveredThisWeek == 0) { view.OrdersOutDeliveredThisWeekPercent = 0; } else { view.OrdersOutDeliveredThisWeekPercent = (Convert.ToDecimal(view.OrdersOutDeliveredThisWeek) / Convert.ToDecimal(ordersOutClosedCount)) * 100; } if (view.OrdersOutClosedThisWeek == 0) { view.OrdersOutClosedThisWeekPercent = 0; } else { view.OrdersOutClosedThisWeekPercent = (Convert.ToDecimal(view.OrdersOutClosedThisWeek) / Convert.ToDecimal(ordersOutClosedCount)) * 100; } 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 List <AvailableListingGeneralInfoView> GetAllAvailableListingsGeneralInfoView(ApplicationDbContext db, IPrincipal user) { List <AvailableListingGeneralInfoView> allAvailableListingsGeneralInfoView = new List <AvailableListingGeneralInfoView>(); AppUser appUser = AppUserHelpers.GetAppUser(db, user); AppUserSettings settings = AppUserSettingsHelpers.GetAppUserSettingsForUser(db, appUser.AppUserId); Branch currentBranch = BranchHelpers.GetBranch(appUser.CurrentBranchId); List <AvailableListing> allAvailableListings = AvailableListingHelpers.GetAllGeneralInfoFilteredAvailableListings(db, appUser.AppUserId); foreach (AvailableListing availableListing in allAvailableListings) { //Find any related offers Offer offer = OfferHelpers.GetOfferForListingAndUser(db, availableListing.ListingId, appUser.AppUserId); decimal offerQty = 0M; if (offer != null) { offerQty = offer.CurrentOfferQuantity; } bool userBlocked = false; bool branchBlocked = false; bool companyBlocked = false; BlockHelpers.GetBlocksForAllTypesForSpecificOfBy(db, availableListing.ListingOriginatorAppUserId, appUser.AppUserId, availableListing.ListingOriginatorBranchId, currentBranch.BranchId, availableListing.ListingOriginatorCompanyId, currentBranch.CompanyId, out userBlocked, out branchBlocked, out companyBlocked); bool userMatchedOwner = false; bool branchMatchedOwner = false; bool companyMatchedOwner = false; if (currentBranch.CompanyId == availableListing.ListingOriginatorCompanyId) { companyMatchedOwner = true; } if (currentBranch.BranchId == availableListing.ListingOriginatorBranchId) { branchMatchedOwner = true; } if (appUser.AppUserId == availableListing.ListingOriginatorAppUserId) { userMatchedOwner = true; } Company company = CompanyHelpers.GetCompany(db, availableListing.ListingOriginatorCompanyId); AvailableListingGeneralInfoView AvailableListingGeneralInfoView = new AvailableListingGeneralInfoView() { AvailableListing = availableListing, OfferQuantity = offerQty, AllowBranchTrading = company.AllowBranchTrading, UserLevelBlock = userBlocked, BranchLevelBlock = branchBlocked, CompanyLevelBlock = companyBlocked, DisplayBlocks = settings.AvailableListingGeneralInfoDisplayBlockedListings, CompanyLevelOwner = companyMatchedOwner, DisplayMyCompanyRecords = settings.AvailableListingGeneralInfoDisplayMyUserListings, BranchLevelOwner = branchMatchedOwner, DisplayMyBranchRecords = settings.AvailableListingGeneralInfoDisplayMyBranchListings, UserLevelOwner = userMatchedOwner, DisplayMyRecords = settings.AvailableListingGeneralInfoDisplayMyUserListings }; allAvailableListingsGeneralInfoView.Add(AvailableListingGeneralInfoView); } return(allAvailableListingsGeneralInfoView); }
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 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); }
public static OfferViewModel GetOfferViewModel(ApplicationDbContext db, HttpRequestBase request, Guid offerId, 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); } Offer offer = OfferHelpers.GetOffer(db, offerId); OfferViewModel model = new OfferViewModel() { DisplayOnly = displayOnly, Breadcrumb = breadcrumb, BreadcrumbDictionary = breadcrumbDictionary, Type = type, OfferId = offer.OfferId, ListingId = offer.ListingId, ListingType = offer.ListingType, OfferStatus = offer.OfferStatus, ItemDescription = offer.ItemDescription, QuantityOutstanding = OfferViewHelpers.GetListingQuantityForListingIdListingType(db, offer.ListingId, offer.ListingType), CurrentOfferQuantity = offer.CurrentOfferQuantity, PreviousOfferQuantity = offer.PreviousOfferQuantity, CounterOfferQuantity = offer.CounterOfferQuantity, PreviousCounterOfferQuantity = offer.PreviousCounterOfferQuantity, OfferOriginatorAppUser = AppUserHelpers.GetAppUserName(db, offer.OfferOriginatorAppUserId), OfferOriginatorOrganisation = OrganisationHelpers.GetOrganisationName(db, offer.OfferOriginatorOrganisationId), OfferOriginatorDateTime = offer.OfferOriginatorDateTime, YourOrganisationId = currentUser.OrganisationId, OfferOriginatorOrganisationId = offer.OfferOriginatorOrganisationId, RejectedBy = AppUserHelpers.GetAppUserName(db, offer.RejectedBy), RejectedOn = offer.RejectedOn, LastOfferOriginatorAppUser = AppUserHelpers.GetAppUserName(db, offer.LastOfferOriginatorAppUserId), LastOfferOriginatorDateTime = offer.LastOfferOriginatorDateTime, ListingOriginatorAppUser = AppUserHelpers.GetAppUserName(db, offer.ListingOriginatorAppUserId), ListingOriginatorOrganisation = OrganisationHelpers.GetOrganisationName(db, offer.ListingOriginatorOrganisationId), ListingOriginatorDateTime = offer.ListingOriginatorDateTime, CounterOfferOriginatorAppUser = AppUserHelpers.GetAppUserName(db, offer.CounterOfferOriginatorAppUserId), CounterOfferOriginatorOrganisation = OrganisationHelpers.GetOrganisationName(db, offer.CounterOfferOriginatorOrganisationId), CounterOfferOriginatorDateTime = offer.CounterOfferOriginatorDateTime, CounterOfferOriginatorOrganisationId = offer.CounterOfferOriginatorOrganisationId, LastCounterOfferOriginatorAppUser = AppUserHelpers.GetAppUserName(db, offer.LastCounterOfferOriginatorAppUserId), LastCounterOfferOriginatorDateTime = offer.LastCounterOfferOriginatorDateTime, OrderOriginatorAppUser = AppUserHelpers.GetAppUserName(db, offer.OrderOriginatorAppUserId), OrderOriginatorOrganisation = OrganisationHelpers.GetOrganisationName(db, offer.OrderOriginatorOrganisationId), OrderOriginatorDateTime = offer.OrderOriginatorDateTime, CallingAction = actionValue, CallingActionDisplayName = callingActionDisplayName, CallingController = controllerValue, BreadcrumbTrail = breadcrumbDictionary }; if (displayOnly) { model.EditableQuantity = false; } else { if (type == "created") { model.EditableQuantity = offer.CurrentOfferQuantity == 0; } else if (type == "received") { model.EditableQuantity = offer.CurrentOfferQuantity > 0; } } return(model); }