Пример #1
0
        public void AddItem(IOrderable itemToOrder, int quantity, long forOrganizationRoleUserId,
                            long dataRecorderCreatorId, SourceCode sourceCode, EventCustomer eventCustomer,
                            ShippingDetail shippingDetail, OrderStatusState orderStatusState, long?sourceId = null)
        {
            if (sourceId == null && (itemToOrder is EventPackage || itemToOrder is EventTest))
            {
                var customerId = forOrganizationRoleUserId;
                if (itemToOrder is EventTest)
                {
                    var preApprovedTestList = _preApprovedTestRepository.GetByCustomerId(customerId).Select(x => x.TestId);
                    if (preApprovedTestList.Contains(((EventTest)itemToOrder).TestId))
                    {
                        sourceId = (long)OrderSource.PreApproved;
                    }
                }
                else if (itemToOrder is EventPackage)
                {
                    var preApprovedPackageList = _preApprovedPackageRepository.GetByCustomerId(customerId).Select(x => x.PackageId);
                    if (preApprovedPackageList.Contains(((EventPackage)itemToOrder).PackageId))
                    {
                        sourceId = (long)OrderSource.PreApproved;
                    }
                }
                if (sourceId == null)
                {
                    var oru = _organizationRoleUserRepository.GetOrganizationRoleUser(dataRecorderCreatorId);
                    if (oru.RoleId == (long)Roles.FranchiseeAdmin)
                    {
                        sourceId = (long)OrderSource.Admin;
                    }
                    else if (oru.RoleId == (long)Roles.CallCenterRep)
                    {
                        sourceId = (long)OrderSource.CallCenter;
                    }
                    else if (oru.RoleId == (long)Roles.Technician)
                    {
                        sourceId = (long)OrderSource.Technician;
                    }
                    else if (oru.RoleId == (long)Roles.NursePractitioner)
                    {
                        sourceId = (long)OrderSource.NursePractioner;
                    }
                }
            }


            OrderDetail orderDetail = _orderDetailFactory.CreateNewOrderDetail(itemToOrder, quantity,
                                                                               forOrganizationRoleUserId,
                                                                               dataRecorderCreatorId,
                                                                               sourceCode, eventCustomer, shippingDetail, sourceId);

            orderDetail.OrderItemStatus = _orderItemStatusFactory.CreateOrderItemStatus(itemToOrder.OrderItemType,
                                                                                        (int)orderStatusState);

            var pair = new OrderedPair <OrderDetail, IOrderable>(orderDetail, itemToOrder);

            _lineItems.Add(pair);
        }
Пример #2
0
        public void PlaceOrderSavesTwoEntitiesWhenOneItemOrdered()
        {
            const int       quantity = 1;
            const int       forOrganizationRoleUserId = 3;
            const int       dataRecorderCreatorId     = 4;
            const OrderType typeOfOrderToPlace        = OrderType.Retail;
            var             fakeOrderable             = new FakeOrderable();

            Expect.Call(_orderFactory.CreateNewOrder(typeOfOrderToPlace, dataRecorderCreatorId)).
            Return(new Order {
                OrderDetails = new List <OrderDetail>()
            });
            Expect.Call(_orderDetailFactory.CreateNewOrderDetail(fakeOrderable, quantity,
                                                                 forOrganizationRoleUserId, dataRecorderCreatorId, null, null, null)).
            Return(new OrderDetail());
            Expect.Call(_orderItemStatusFactory.CreateOrderItemStatus(0, 0)).IgnoreArguments().
            Return(FakeOrderItemStatus.IncompleteFakeStatus);

            // Expecting persistence of two entities.
            Expect.Call(_orderRepository.SaveOrder(null)).IgnoreArguments().
            Return(new Order());
            Expect.Call(_orderItemRepository.SaveOrderItem(fakeOrderable.Id, fakeOrderable.OrderItemType))
            .Return(new FakeOrderItem());

            _mocks.ReplayAll();
            _orderController.AddItem(fakeOrderable, quantity,
                                     forOrganizationRoleUserId, dataRecorderCreatorId);
            _orderController.PlaceOrder(typeOfOrderToPlace, dataRecorderCreatorId);
            _mocks.VerifyAll();
        }
Пример #3
0
        // TODO: Once order system is refactored this method will have to be rewritten.
        public Order SynchronizeOrder(Order oldOrder, Order newOrder, SourceCodeOrderDetail newSourceCodeOrderDetail)
        {
            if (oldOrder.OrderDetails.IsNullOrEmpty())
            {
                return(newOrder);
            }

            var newOrderDetails = new List <OrderDetail>();

            newOrder.OrderDetails.ForEach(newOrderDetails.Add);
            var newOrderItemIds       = newOrderDetails.Select(od => od.OrderItemId);
            var newOrderDetailCreator = newOrderDetails.First().DataRecorderMetaData.DataRecorderCreator.Id;
            var oldOrderDetails       = oldOrder.OrderDetails;
            var oldOrderItemIds       = oldOrderDetails.Select(od => od.OrderItemId);

            newOrder.OrderDetails.Clear();

            var alreadyCancelledOrderDetails =
                oldOrderDetails.Where(
                    ood =>
                    !ood.IsCompleted ||
                    (ood.DetailType != OrderItemType.EventPackageItem && ood.DetailType != OrderItemType.EventTestItem && ood.DetailType != OrderItemType.ProductItem))
                .ToList();

            var alreadyCancelledOrderItemIds = alreadyCancelledOrderDetails.Select(od => od.OrderItemId);

            newOrder.OrderDetails.AddRange(alreadyCancelledOrderDetails);

            // It will give the line items which still exists for the order.
            var existingOrderDetails = newOrderDetails.Join(oldOrderDetails, nod => nod.OrderItemId,
                                                            ood => ood.OrderItemId, (nod, ood) => ood);

            newOrder.OrderDetails.AddRange(existingOrderDetails);

            // It will give the line items which does not exist for the order.
            var nonExistingOrderDetails =
                oldOrderDetails.Where(
                    eod =>
                    !newOrderItemIds.Contains(eod.OrderItemId) && eod.IsCompleted &&
                    (eod.DetailType == OrderItemType.EventPackageItem || eod.DetailType == OrderItemType.EventTestItem));

            EventCustomerOrderDetail         eventCustomerOrderDetail   = null;
            List <ShippingDetailOrderDetail> shippingDetailOrderDetails = null;

            foreach (var nonExistingOrderDetail in nonExistingOrderDetails)
            {
                if (nonExistingOrderDetail.EventCustomerOrderDetail != null)
                {
                    eventCustomerOrderDetail = new EventCustomerOrderDetail
                    {
                        EventCustomerId =
                            nonExistingOrderDetail.EventCustomerOrderDetail.
                            EventCustomerId,
                        IsActive      = true,
                        OrderDetailId = 0
                    };
                    if (!nonExistingOrderDetail.ShippingDetailOrderDetails.IsNullOrEmpty())
                    {
                        if (shippingDetailOrderDetails.IsNullOrEmpty())
                        {
                            shippingDetailOrderDetails = new List <ShippingDetailOrderDetail>();
                        }

                        foreach (var shippingDetailOrderDetail in nonExistingOrderDetail.ShippingDetailOrderDetails)
                        {
                            if (shippingDetailOrderDetails != null)
                            {
                                shippingDetailOrderDetails.Add(new ShippingDetailOrderDetail
                                {
                                    ShippingDetailId =
                                        shippingDetailOrderDetail.ShippingDetailId,
                                    IsActive = true
                                });
                            }
                        }
                    }

                    shippingDetailOrderDetails = nonExistingOrderDetail.ShippingDetailOrderDetails;
                    shippingDetailOrderDetails.ForEach(sdod => sdod.OrderDetailId = 0);
                }

                var cancelledOrderDetail = _orderDetailFactory.CreateNewOrderDetail(nonExistingOrderDetail,
                                                                                    nonExistingOrderDetail.
                                                                                    ForOrganizationRoleUserId,
                                                                                    newOrderDetailCreator);

                newOrder.OrderDetails.Add(nonExistingOrderDetail);
                newOrder.OrderDetails.Add(cancelledOrderDetail);
            }

            // It will give the line items which are new for this order.
            var freshOrderDetails = newOrderDetails.Where(nod => !oldOrderItemIds.Contains(nod.OrderItemId)).ToList();

            var alreadyAddedOrderItems = newOrder.OrderDetails.Where(od => od.IsCompleted).Select(nod => nod.OrderItemId);

            // Items which were cancelled earlier and are made active again.
            var cancelledOrderDetailsToBeRevived =
                newOrderDetails.Where(
                    nod =>
                    alreadyCancelledOrderItemIds.Contains(nod.OrderItemId) && nod.IsCompleted &&
                    !alreadyAddedOrderItems.Contains(nod.OrderItemId));

            newOrder.OrderDetails.AddRange(cancelledOrderDetailsToBeRevived);

            newOrder.OrderDetails.AddRange(freshOrderDetails);

            if (eventCustomerOrderDetail != null)
            {
                var currentActiveOrderDetail =
                    newOrder.OrderDetails.SingleOrDefault(
                        od =>
                        od.SourceCodeOrderDetail != null &&
                        od.OrderItemStatus.OrderStatusState == OrderStatusState.FinalSuccess) ??
                    newOrder.OrderDetails.First(
                        od =>
                        od.OrderItemStatus.OrderStatusState == OrderStatusState.FinalSuccess &&
                        (od.DetailType == OrderItemType.EventPackageItem || od.DetailType == OrderItemType.EventTestItem));

                newOrder.OrderDetails.Remove(currentActiveOrderDetail);

                currentActiveOrderDetail.EventCustomerOrderDetail   = eventCustomerOrderDetail;
                currentActiveOrderDetail.ShippingDetailOrderDetails = shippingDetailOrderDetails;
                currentActiveOrderDetail.SourceCodeOrderDetail      = newSourceCodeOrderDetail;

                newOrder.OrderDetails.Add(currentActiveOrderDetail);
            }
            else
            {
                var currentActiveOrderDetail =
                    newOrder.OrderDetails.SingleOrDefault(
                        od =>
                        od.EventCustomerOrderDetail != null &&
                        od.OrderItemStatus.OrderStatusState == OrderStatusState.FinalSuccess);

                newOrder.OrderDetails.Remove(currentActiveOrderDetail);
                if (newSourceCodeOrderDetail != null)
                {
                    currentActiveOrderDetail.SourceCodeOrderDetail = newSourceCodeOrderDetail;
                    newSourceCodeOrderDetail.OrderDetailId         = currentActiveOrderDetail.Id;
                }
                newOrder.OrderDetails.Add(currentActiveOrderDetail);
            }

            foreach (var orderDetail in newOrder.OrderDetails)
            {
                if (orderDetail.IsCompleted)
                {
                    var newOrderDetail = newOrderDetails.SingleOrDefault(od => od.OrderItemId == orderDetail.OrderItemId);
                    if (newOrderDetail != null)
                    {
                        orderDetail.Price = newOrderDetail.Price;
                    }
                }
            }

            return(newOrder);
        }
Пример #4
0
        public void CreateNewOrderDetailCreatesOrderItemStatusInInitialStateWithOrderableOrderItemType()
        {
            var orderable = new FakeOrderable();

            Expect.Call(_orderItemStatusFactory.CreateOrderItemStatus(orderable.OrderItemType,
                                                                      (int)OrderStatusState.Initial)).Return(null);
            Expect.Call(_dataRecorderMetaDataFactory.CreateDataRecorderMetaData(0)).IgnoreArguments().
            Return(null);

            _mocks.ReplayAll();
            _orderDetailFactory.CreateNewOrderDetail(orderable, 1, 0, 0, null, null, null);
            _mocks.VerifyAll();
        }