private async Task <OrderSessionPaySummaryModel> GetPaymentSummary(OrderSession orderSession, OrderSessionPaySummaryRequestModel model)
        {
            //Calculate base price
            var price     = CalculatePrice(orderSession);
            var fullPrice = price;

            //Note: calculation order matters here, we redeem loyalty points 1st, because if the vouchers are percentage based, overall this results in potentially higher net gain for the restaurant

            int redeemedPoints = 0;

            //Loyalty points
            if (model.LoyaltyCardNumber != null)
            {
                var loyaltyCard = await DbContext.LoyaltyCards.Where(lc => lc.CardNumber == model.LoyaltyCardNumber).SingleOrDefaultAsync();

                if (loyaltyCard == null)
                {
                    loyaltyCard = await LoyaltyCardService.AddLoyaltyCard(model.LoyaltyCardNumber ?? 1);

                    await DbContext.SaveChangesAsync();
                }

                //Redeem points
                if (model.ShouldRedeemPoints)
                {
                    redeemedPoints      = Math.Min(loyaltyCard.Points, price);
                    loyaltyCard.Points -= redeemedPoints;
                    price -= redeemedPoints;
                }

                //Add new points for the purchase
                loyaltyCard.Points += (int)Math.Round(fullPrice * OrderConfig.LoyaltyPointsMultiplier);
            }

            int discountedPrice = price;

            //Vouchers
            if (!string.IsNullOrEmpty(model.VoucherCode) && price > OrderConfig.MinPrice)
            {
                discountedPrice = await CalculateVoucherDiscountedPrice(model.VoucherCode, price);
            }

            int discountAmount = price - discountedPrice;

            price = discountedPrice;

            price = Math.Max(price, OrderConfig.MinPrice);

            return(new OrderSessionPaySummaryModel()
            {
                FullPrice = fullPrice,
                FinalPrice = price,
                LoyaltyCardNumber = model.LoyaltyCardNumber,
                UsedLoyaltyPoints = redeemedPoints,
                VoucherCode = model.VoucherCode,
                VoucherTotalDiscountAmount = discountAmount
            });
        }
        public async Task <OrderSessionPaySummaryModel> GetPaymentSummary(int orderSessionId, OrderSessionPaySummaryRequestModel model)
        {
            var orderSession = await DbContext.OrderSessions.Include(os => os.Orders)
                               .ThenInclude(o => o.Items)
                               .ThenInclude(oi => oi.MenuItem)
                               .Where(os => os.Id == orderSessionId && (os.Status == OrderSessionStatus.Active || os.Status == OrderSessionStatus.Delivering))
                               .SingleOrDefaultAsync();

            if (orderSession == null)
            {
                throw new RestaurantNotFoundException("A rendelési folyamat nem létezik vagy a fizetése nem lehetséges!");
            }

            await StatusService.CheckRightsForStatus(orderSession.Status);

            return(await GetPaymentSummary(orderSession, model));
        }
示例#3
0
 public async Task <OrderSessionPaySummaryModel> GetOrderSessionPaymentSummary([FromQuery] OrderSessionPaySummaryRequestModel requestModel, int id)
 => await OrderSessionService.GetPaymentSummary(id, requestModel);