Exemplo n.º 1
0
        public CartVM GetCart(decimal?orderId = null, bool addTrackDiscounts = false)
        {
            Entities.Context.Order order = null;
            Entities.Context.Order planOrder;
            if (orderId.HasValue)
            {
                order     = OrderService.GetByPK(orderId.Value);
                planOrder = OrderService.GetCurrentOrder(true) ?? new Entities.Context.Order();
            }
            else
            {
                order     = OrderService.GetCurrentOrder() ?? new Entities.Context.Order();
                planOrder = OrderService.GetCurrentOrder(true) ?? new Entities.Context.Order();
            }

            var trainer = EmployeeService.AllEmployees().GetValueOrDefault(order.FavoriteTeacher1);

            var cart = new CartVM(order)
            {
                InPlan     = new OrderSeparator(planOrder),
                User       = AuthService.CurrentUser,
                FavTrainer = trainer.GetOrDefault(x => x.FullName)
            };

            if (cart.OnlyIsCompaty &&
                order.CustomerType == OrderCustomerType.PrivatePerson)
            {
                UpdateOrder(OrderCustomerType.Organization);
            }

            cart.CourseTCHasExtrases = order.OrderDetails.Where(od => od.Track_TC == null &&
                                                                ExtrasService.GetFor(od).Any()).Select(od => od.Course_TC).ToList();

            cart.ExtrasTexts = ExtrasService.Texts();
            cart.IsNearestGroupOrderDetails = order.OrderDetails.Where(x =>
                                                                       x.Group_ID.HasValue && GroupService.GetGroupsForCourse(x.Course_TC)
                                                                       .FirstOrDefault().GetOrDefault(g => g.Group_ID == x.Group_ID))
                                              .Select(x => x.OrderDetailID).ToList();

            if (addTrackDiscounts && order.CustomerType != OrderCustomerType.Organization &&
                !cart.OnlyIsCompaty)
            {
                var trackInCart = order.OrderDetails.Where(x => x.Track_TC != null).Select(x => x.Track_TC).ToList();
                var courseTCs   = order.OrderDetails.Where(x => x.Track_TC.IsEmpty()).Select(x => x.Course_TC).ToList();
                if (courseTCs.Any())
                {
                    var trackCourses = CourseService.GetActiveTrackCourses().Select(x =>
                                                                                    new { x.Key, CourseCount = x.Value.Intersect(courseTCs).Count() })
                                       .Where(x => x.CourseCount > 0).ToDictionary(x => x.Key, x => x.CourseCount);
                    var trackTCs       = trackCourses.Select(x => x.Key).Where(tc => !trackInCart.Contains(tc)).ToList();
                    var tracks         = CourseService.GetAll(x => trackTCs.Contains(x.Course_TC)).ToList();
                    var trackDiscounts = TrackService.GetTrackDiscounts(tracks)
                                         .OrderByDescending(x => trackCourses[x.Track.Course_TC]).ThenBy(x => x.Price);
                    cart.TrackDiscounts = trackDiscounts.ToList();
                }

                var allOrderCourseTCs = order.OrderDetails.Select(x => x.Course_TC).ToList();
                cart.WithWebinar =
                    allOrderCourseTCs.Where(x => PriceService.GetAllPricesForCourse(x, null).Any(y => y.IsWebinar)).ToList();
            }

            return(cart);
        }