public void UpdateCustomerPreApprovedForFutureEvents(IEnumerable <EventCustomer> eventCustomers, CorporateCustomerEditModel customerEditModel)
        {
            if (!eventCustomers.IsNullOrEmpty())
            {
                var pairs = TestType.A1C.GetNameValuePairs();

                var preApprovedTestIds = customerEditModel.PreApprovedTest.IsNullOrEmpty() ? new long[0] : pairs.Where(x => customerEditModel.PreApprovedTest.Contains(x.SecondValue.ToLower())).Select(x => (long)x.FirstValue).ToArray();

                foreach (var eventCustomer in eventCustomers)
                {
                    if (!preApprovedTestIds.IsNullOrEmpty())
                    {
                        preApprovedTestIds = _corporateUploadHelper.RemoveFocFromPreApprovedTest(preApprovedTestIds.ToList()).ToArray();

                        _eventCustomerPreApprovedTestRepository.Save(eventCustomer.Id, preApprovedTestIds);
                    }

                    if (customerEditModel.PreApprovedPackageId > 0)
                    {
                        var preApprovedPacakgeTests = _packageTestRepository.GetbyPackageId(customerEditModel.PreApprovedPackageId);

                        if (preApprovedPacakgeTests != null && preApprovedPacakgeTests.Any())
                        {
                            _eventCustomerPreApprovedPackageTestRepository.Save(eventCustomer.Id, customerEditModel.PreApprovedPackageId, preApprovedPacakgeTests.Select(x => x.TestId));
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public IEnumerable <EventCusomerAdjustOrderViewModel> MarkcustomerForAdjustOrder(CorporateCustomerEditModel model, IEnumerable <EventCustomer> eventCustomers, long createdByOrgRoleUserId, long customerId, long?corporateUploadId)
        {
            var pairs = TestType.A1C.GetNameValuePairs();

            var preApprovedTestIds = model.PreApprovedTest.IsNullOrEmpty() ? new long[0] : pairs.Where(x => model.PreApprovedTest.Contains(x.SecondValue.ToLower())).Select(x => (long)x.FirstValue).ToArray();

            preApprovedTestIds = _corporateUploadHelper.RemoveFocFromPreApprovedTest(preApprovedTestIds.ToList()).ToArray();

            var eventIds = eventCustomers.Select(x => x.EventId);

            var events = ((IUniqueItemRepository <Event>)_eventRepository).GetByIds(eventIds);

            var orders = _orderRepository.GetAllOrdersByEventCustomerIds(eventCustomers.Select(x => x.Id));

            var orderIds = orders.Select(o => o.Id).ToArray();

            var orderEventPackageIdPairs = _eventPackageRepository.GetEventPackageIdsForOrder(orderIds);                // orderId, EventPackaegId
            var eventPacakageTestIds     = _eventPackageRepository.GetOrderIdEventPackageTestIdPairsForOrder(orderIds); //orderId, TestId(package test)
            var orderIdTestIdPairs       = _eventTestRepository.GetOrderIdTestIdPairsForOrders(orderIds);               // orderId, TestId

            var eventPackageIds = orderEventPackageIdPairs.Select(pair => pair.SecondValue).ToArray();

            var eventPackages = _eventPackageRepository.GetByIds(eventPackageIds).ToArray();

            var list = new List <EventCusomerAdjustOrderViewModel>();

            foreach (var eventCustomer in eventCustomers)
            {
                var order = orders.FirstOrDefault(x => x.CustomerId == eventCustomer.CustomerId && x.EventId == eventCustomer.EventId);

                if (order == null)
                {
                    continue;
                }

                var theEvent = events.First(s => s.Id == eventCustomer.EventId);

                var eventPackagePair          = orderEventPackageIdPairs.FirstOrDefault(x => x.FirstValue == order.Id);
                var preApprovedPackageUpdated = IsPreApprovedPackageUpdated(eventPackagePair, eventPackages, order, model.PreApprovedPackageId);
                var testIdInPackage           = new List <long>();
                if (eventPackagePair != null)
                {
                    testIdInPackage = eventPacakageTestIds.Where(x => x.FirstValue == eventPackagePair.FirstValue).Select(s => s.SecondValue).ToList();
                }

                var preApprovedTestNotInOrder = PreApprovedTestUpdated(orderIdTestIdPairs, order, preApprovedTestIds, testIdInPackage);
                var preApprovedTestToUpdate   = string.Empty;

                if (preApprovedTestNotInOrder != null)
                {
                    preApprovedTestToUpdate = string.Join(",", preApprovedTestNotInOrder.Select(x => ((TestType)x).ToString()));
                }

                if (preApprovedPackageUpdated || !preApprovedTestNotInOrder.IsNullOrEmpty())
                {
                    list.Add(new EventCusomerAdjustOrderViewModel
                    {
                        CustomerId = eventCustomer.CustomerId,
                        EventId    = eventCustomer.EventId,
                        FirstName  = model.FirstName,
                        MiddleName = model.MiddleName,
                        LastName   = model.LastName,
                        MemberId   = model.MemberId,
                        EventDate  = theEvent.EventDate,
                        PreApprovedPackageToAdjust = preApprovedPackageUpdated ? model.PreApprovedPackage : string.Empty,
                        PreApprovedTestsToAdjust   = preApprovedTestToUpdate
                    });


                    long newPackageId = 0;
                    long oldPackageId = 0;

                    if (preApprovedPackageUpdated)
                    {
                        newPackageId = model.PreApprovedPackageId;
                    }

                    if (eventPackagePair != null)
                    {
                        var package = eventPackages.First(x => x.Id == eventPackagePair.SecondValue).Package;
                        oldPackageId = package.Id;
                    }

                    var orderedTestIds = orderIdTestIdPairs.Where(x => x.FirstValue == order.Id).Select(x => x.SecondValue);

                    AdjustCustomerOrder(eventCustomer, order, newPackageId, oldPackageId, createdByOrgRoleUserId, customerId, orderedTestIds,
                                        preApprovedTestIds, testIdInPackage, corporateUploadId.HasValue ? corporateUploadId.Value : 0);
                }
            }

            return(list.IsNullOrEmpty() ? null : list);
        }