public virtual PaymentEntity Calculate(int orderID, float subtotal, DeliveryChargeEntity dChargeE)
        {
            var paymentE = new PaymentEntity(orderID, dChargeE.Id, subtotal + dChargeE.Charge, subtotal);

            return(paymentE);
        }
        public virtual async Task <OrderDto> CreateAsync(OrderCreateDto dto)
        {
            var userId   = CurrentUser.Id;
            var addressE = (await AsyncExecuter
                            .SingleOrDefaultAsync(from addrR in AddressRepository
                                                  join cusR in CustomerRepository on addrR.CustomerID equals cusR.Id
                                                  where cusR.UserID == userId && addrR.Id == dto.AddressID
                                                  select addrR)) ?? throw new BusinessException();
            var itemIds = dto.Items.Select(e => e.ItemID).ToArray();
            var dsEArr  = await DeliveryScheduleAppService.GetAllCompatiblesAsync(addressE.Id, itemIds);

            var dsE        = dsEArr.SingleOrDefault(e => e.Id == dto.DeliveryScheduleID) ?? throw new BusinessException();
            var promotionE = (dto.PromotionID != null) ? await GetPromotionEntity((int)dto.PromotionID) : null;

            var subtotal       = 0.0;
            var orderItemEList = new List <OrderItemEntity>();

            if (!(await ItemRepository.ValidateItemsWithinRadiusAsync(addressE.CityID, itemIds)))
            {
                throw new BusinessException();
            }

            foreach (var cartItem in dto.Items)
            {
                var item = await ItemRepository.SingleOrDefaultAsync(e => e.Id == cartItem.ItemID) ?? throw new BusinessException();

                subtotal += item.ItemPrice * cartItem.Qty;
                orderItemEList.Add(new OrderItemEntity(0, item.Id, (uint)cartItem.Qty, item.ItemPrice));
            }

            var dcDto = await DeliveryChargeAppService.CalculateAsync(addressE.Id, dsE.Id, dto.Items.ToArray());

            var orderE = new OrderEntity(addressE.Id, dsE.Id, promotionE?.Id);

            orderE = await OrderRepository.InsertAsync(orderE, autoSave : true);

            foreach (var oi in orderItemEList)
            {
                var item = new OrderItemEntity(orderE.Id, oi.ItemID, oi.Quantity, oi.ItemPrice);
                await OrderItemRepository.InsertAsync(item, autoSave : true);
            }

            var dCharge = dcDto.DistanceCharge + dcDto.SubtotalPercentage + dcDto.IncreasedCost;

            if (promotionE != null)
            {
                dCharge = dcDto.SubtotalPercentage;
                if (promotionE.IsOneTime || promotionE.NoOfTimes - 1 < 1)
                {
                    await PromotionRepository.DeleteAsync(promotionE);
                }
            }

            var dcE = new DeliveryChargeEntity(dCharge, dcDto.DistanceChargeID, dcDto.SubtotalPercentageID, dcDto.DeliveryScheduleID);

            await DeliveryChargeRepository.InsertAsync(dcE, autoSave : true);

            var paymentE = new PaymentEntity(orderE.Id, dcE.Id, (float)(subtotal + dcE.Charge), (float)subtotal);

            await PaymentRepository.InsertAsync(paymentE, autoSave : true);

            return(ObjectMapper.Map <OrderEntity, OrderDto>(orderE));
        }