public void PollForAdjustOrder()
        {
            _logger.Info("Enter in Service to adjust Order");

            var eventCustomersToAdjustOrder = _eventCustomerAdjustOrderLogRepository.GetEventCustomerToAdjustOrder();

            if (eventCustomersToAdjustOrder.IsNullOrEmpty())
            {
                _logger.Info("No Event Customer Found For Adjusting Order");
                return;
            }

            var eventCustomers = _eventCustomerRepository.GetByIds(eventCustomersToAdjustOrder.Select(x => x.EventCustomerId));

            foreach (var eventCustomer in eventCustomers)
            {
                if (eventCustomer.AppointmentId == null)
                {
                    _logger.Info("Appointment has been cancel");
                    continue;
                }

                var customrePreApprovedTest = _preApprovedTestRepository.GetByCustomerId(eventCustomer.Id);

                if (customrePreApprovedTest.IsNullOrEmpty())
                {
                    _logger.Info("Customer  has no pre approved test" + eventCustomer.CustomerId);
                }

                var preApprovedPackage = _preApprovedPackageRepository.GetByCustomerId(eventCustomer.CustomerId);

                if (customrePreApprovedTest.IsNullOrEmpty())
                {
                    _logger.Info("Customer  has no pre-approved test" + eventCustomer.CustomerId);
                }

                if (preApprovedPackage.IsNullOrEmpty())
                {
                    _logger.Info("Customer  has no pre-approved package" + eventCustomer.CustomerId);
                }

                var customerOrderDtails = new CustomerOrderDetail
                {
                    CustomerId         = eventCustomer.CustomerId,
                    EventId            = eventCustomer.EventId,
                    PreApprovedTestIds = customrePreApprovedTest != null?customrePreApprovedTest.Select(x => x.TestId) : null,
                                             PrePackageId = preApprovedPackage.IsNullOrEmpty() ? 0 : preApprovedPackage.First().PackageId
                };

                ChangePackage(customerOrderDtails);
            }
        }
        public void ChangePackage(CustomerOrderDetail customerOrderDetail)
        {
            using (var scope = new TransactionScope())
            {
                try
                {
                    var eventid    = customerOrderDetail.EventId;
                    var customerid = customerOrderDetail.CustomerId;

                    var order = GetOrder(customerid, eventid);


                    IOrderController orderController = new OrderController();
                    var orderDetail = orderController.GetActiveOrderDetail(order);

                    if (order == null || order.OrderDetails.IsEmpty())
                    {
                        return;
                    }

                    var orderables      = new List <IOrderable>();
                    var selectedTestIds = new List <long>();

                    if (customerOrderDetail.PreApprovedTestIds != null && customerOrderDetail.PreApprovedTestIds.Any())
                    {
                        selectedTestIds = customerOrderDetail.PreApprovedTestIds.ToList();
                    }

                    long packageId = 0;

                    if (customerOrderDetail.PrePackageId > 0)
                    {
                        var eventPackage = _eventPackageRepository.GetByEventAndPackageIds(eventid, customerOrderDetail.PrePackageId);
                        if (eventPackage != null)
                        {
                            packageId = eventPackage.PackageId;
                        }
                    }

                    if (packageId == 0)
                    {
                        if (orderDetail.OrderItem.OrderItemType == OrderItemType.EventPackageItem)
                        {
                            var eventPackage = _eventPackageRepository.GetById(orderDetail.OrderItem.ItemId);
                            packageId = eventPackage.PackageId;
                        }
                    }

                    if (packageId > 0)
                    {
                        orderables.Add(_eventPackageRepository.GetByEventAndPackageIds(eventid, packageId));
                        selectedTestIds = RemoveTestsAlreadyInPackage(selectedTestIds, eventid, packageId);
                    }

                    if (!selectedTestIds.IsNullOrEmpty())
                    {
                        var eventTests = _eventTestRepository.GetByEventAndTestIds(eventid, selectedTestIds);
                        if (packageId > 0)
                        {
                            foreach (var eventTest in eventTests)
                            {
                                eventTest.Price = eventTest.WithPackagePrice;
                            }
                        }

                        orderables.AddRange(eventTests);
                    }

                    var indentedLineItemsAdded = false;

                    // TODO: applying hook to the system all the indented line items will be attached to the first order item.
                    foreach (var orderable in orderables)
                    {
                        if (!indentedLineItemsAdded && (orderable.OrderItemType == OrderItemType.EventPackageItem || orderable.OrderItemType == OrderItemType.EventTestItem))
                        {
                            _orderController.AddItem(orderable, 1, customerid, 1, null, null, null, OrderStatusState.FinalSuccess);
                            indentedLineItemsAdded = true;
                        }
                        else
                        {
                            _orderController.AddItem(orderable, 1, customerid, 1, OrderStatusState.FinalSuccess);
                        }
                    }

                    _orderController.PlaceOrder(order);


                    scope.Complete();
                }
                catch (Exception exception)
                {
                    _logger.Error("Some Error occured" + exception.Message);
                }
            }
        }