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);
        }
Exemple #4
0
        public static List <NotificationViewModel> GetNotificationsViewModelForOrganisationFromUser(ApplicationDbContext db, IPrincipal user, bool getHistory)
        {
            List <NotificationViewModel> list = new List <NotificationViewModel>();

            foreach (Notification notification in NotificationHelpers.GetNotificationsForOrganisationFromUser(db, user, getHistory))
            {
                //build view
                NotificationViewModel view = CreateNotificationsViewModel(db, notification);

                list.Add(view);
            }

            return(list);
        }
Exemple #5
0
        public static Notification UpdateNotificationEntityStatus(ApplicationDbContext db, Guid?notificationId, Notification action, EntityStatusEnum newStatus, IPrincipal user)
        {
            if (action == null)
            {
                action = NotificationHelpers.GetNotification(db, notificationId.Value);
            }

            action.EntityStatus   = newStatus;
            action.RecordChange   = RecordChangeEnum.StatusChange;
            action.RecordChangeBy = AppUserHelpers.GetAppUserIdFromUser(user);
            action.RecordChangeOn = DateTime.Now;

            db.Entry(action).State = EntityState.Modified;
            db.SaveChanges();

            return(action);
        }
        public static Offer RejectOffer(ApplicationDbContext db, Guid offerId, IPrincipal user)
        {
            Offer   offer   = db.Offers.Find(offerId);
            AppUser appUser = AppUserHelpers.GetAppUser(db, user);

            offer.OfferStatus = OfferStatusEnum.Rejected;
            offer.RejectedBy  = appUser.AppUserId;
            offer.RejectedOn  = DateTime.Now;

            db.Entry(offer).State = EntityState.Modified;
            db.SaveChanges();

            //set any related actions to Closed
            NotificationHelpers.RemoveNotificationsForOffer(db, offerId, user);

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