discount beim checkout überarbeitet

This commit is contained in:
Tizian.Breuch
2025-10-10 13:45:28 +02:00
parent e915d5a105
commit 2e3f04f0be

View File

@@ -1,4 +1,5 @@
// src/Webshop.Application/Services/Customers/CheckoutService.cs // /src/Webshop.Application/Services/Customers/CheckoutService.cs
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -42,7 +43,7 @@ namespace Webshop.Application.Services.Customers
await using var transaction = await _context.Database.BeginTransactionAsync(); await using var transaction = await _context.Database.BeginTransactionAsync();
try try
{ {
// 1. Validierung von Kunde, Adressen und Methoden // --- 1. Validierung von Kunde, Adressen und Methoden ---
var customer = await _customerRepository.GetByUserIdAsync(userId!); var customer = await _customerRepository.GetByUserIdAsync(userId!);
if (customer == null) if (customer == null)
return ServiceResult.Fail<OrderDetailDto>(ServiceResultType.Unauthorized, "Kundenprofil nicht gefunden."); return ServiceResult.Fail<OrderDetailDto>(ServiceResultType.Unauthorized, "Kundenprofil nicht gefunden.");
@@ -61,7 +62,7 @@ namespace Webshop.Application.Services.Customers
if (paymentMethod == null || !paymentMethod.IsActive) if (paymentMethod == null || !paymentMethod.IsActive)
return ServiceResult.Fail<OrderDetailDto>(ServiceResultType.InvalidInput, "Ungültige oder inaktive Zahlungsmethode."); return ServiceResult.Fail<OrderDetailDto>(ServiceResultType.InvalidInput, "Ungültige oder inaktive Zahlungsmethode.");
// 2. Artikel verarbeiten und Lagerbestand prüfen // --- 2. Artikel verarbeiten und Lagerbestand prüfen ---
var orderItems = new List<OrderItem>(); var orderItems = new List<OrderItem>();
var productIds = orderDto.Items.Select(i => i.ProductId).ToList(); var productIds = orderDto.Items.Select(i => i.ProductId).ToList();
var products = await _context.Products.Where(p => productIds.Contains(p.Id)).ToListAsync(); var products = await _context.Products.Where(p => productIds.Contains(p.Id)).ToListAsync();
@@ -97,18 +98,43 @@ namespace Webshop.Application.Services.Customers
itemsTotal += orderItem.TotalPrice; itemsTotal += orderItem.TotalPrice;
} }
// 3. Preise, Rabatte, Steuern und Gesamtbetrag berechnen // --- 3. Preise, Rabatte, Steuern und Gesamtbetrag berechnen ---
var discountResult = await _discountService.CalculateDiscountAsync(orderItems, orderDto.CouponCode); decimal discountAmount = 0;
decimal discountAmount = discountResult.TotalDiscountAmount; var discountResult = new DiscountCalculationResult();
if (!string.IsNullOrWhiteSpace(orderDto.CouponCode))
{
discountResult = await _discountService.CalculateDiscountAsync(orderItems, orderDto.CouponCode);
discountAmount = discountResult.TotalDiscountAmount;
if (discountAmount <= 0)
{
await transaction.RollbackAsync();
return ServiceResult.Fail<OrderDetailDto>(ServiceResultType.InvalidInput, "Der angegebene Gutscheincode ist ungültig, abgelaufen oder nicht auf die Produkte im Warenkorb anwendbar.");
}
if (discountResult.AppliedDiscountIds.Count > 1)
{
await transaction.RollbackAsync();
return ServiceResult.Fail<OrderDetailDto>(ServiceResultType.Failure, "Es können nicht mehrere Rabatte gleichzeitig angewendet werden.");
}
}
decimal shippingCost = shippingMethod.BaseCost; decimal shippingCost = shippingMethod.BaseCost;
decimal subTotal = itemsTotal + shippingCost - discountAmount; decimal subTotalBeforeDiscount = itemsTotal + shippingCost;
if (subTotal < 0) subTotal = 0;
if (discountAmount > subTotalBeforeDiscount)
{
discountAmount = subTotalBeforeDiscount;
}
decimal subTotalAfterDiscount = subTotalBeforeDiscount - discountAmount;
decimal taxRate = await _settingService.GetSettingValueAsync<decimal>("GlobalTaxRate", 0.19m); decimal taxRate = await _settingService.GetSettingValueAsync<decimal>("GlobalTaxRate", 0.19m);
decimal taxAmount = subTotal * taxRate; decimal taxAmount = subTotalAfterDiscount * taxRate;
decimal orderTotal = subTotal + taxAmount; decimal orderTotal = subTotalAfterDiscount + taxAmount;
// 4. Bestellung erstellen // --- 4. Bestellung erstellen ---
var newOrder = new Order var newOrder = new Order
{ {
CustomerId = customer.Id, CustomerId = customer.Id,
@@ -129,7 +155,7 @@ namespace Webshop.Application.Services.Customers
}; };
await _orderRepository.AddAsync(newOrder); await _orderRepository.AddAsync(newOrder);
// 5. Rabattnutzung erhöhen // --- 5. Rabattnutzung erhöhen ---
if (discountResult.AppliedDiscountIds.Any()) if (discountResult.AppliedDiscountIds.Any())
{ {
var appliedDiscounts = await _context.Discounts.Where(d => discountResult.AppliedDiscountIds.Contains(d.Id)).ToListAsync(); var appliedDiscounts = await _context.Discounts.Where(d => discountResult.AppliedDiscountIds.Contains(d.Id)).ToListAsync();
@@ -139,14 +165,16 @@ namespace Webshop.Application.Services.Customers
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
await transaction.CommitAsync(); await transaction.CommitAsync();
// 6. Erfolgreiche Antwort erstellen // --- 6. Erfolgreiche Antwort erstellen ---
var createdOrder = await _orderRepository.GetByIdAsync(newOrder.Id); var createdOrder = await _orderRepository.GetByIdAsync(newOrder.Id);
return ServiceResult.Ok(MapToOrderDetailDto(createdOrder!)); return ServiceResult.Ok(MapToOrderDetailDto(createdOrder!));
} }
catch (Exception ex) catch (Exception ex)
{ {
await transaction.RollbackAsync(); await transaction.RollbackAsync();
return ServiceResult.Fail<OrderDetailDto>(ServiceResultType.Failure, $"Ein unerwarteter Fehler ist aufgetreten: {ex.Message}"); // Log the full exception for debugging purposes
// _logger.LogError(ex, "An unexpected error occurred in CreateOrderAsync.");
return ServiceResult.Fail<OrderDetailDto>(ServiceResultType.Failure, "Ein unerwarteter Fehler ist aufgetreten. Bitte versuchen Sie es erneut.");
} }
} }
@@ -158,7 +186,7 @@ namespace Webshop.Application.Services.Customers
OrderNumber = order.OrderNumber, OrderNumber = order.OrderNumber,
OrderDate = order.OrderDate, OrderDate = order.OrderDate,
CustomerId = order.CustomerId, CustomerId = order.CustomerId,
Status = Enum.Parse<OrderStatus>(order.OrderStatus), Status = Enum.TryParse<OrderStatus>(order.OrderStatus, true, out var os) ? os : OrderStatus.Pending,
TotalAmount = order.OrderTotal, TotalAmount = order.OrderTotal,
ShippingAddress = new AddressDto ShippingAddress = new AddressDto
{ {
@@ -184,8 +212,8 @@ namespace Webshop.Application.Services.Customers
Country = order.BillingAddress.Country, Country = order.BillingAddress.Country,
Type = order.BillingAddress.Type Type = order.BillingAddress.Type
}, },
PaymentMethod = order.PaymentMethod, PaymentMethod = order.PaymentMethodInfo?.Name,
PaymentStatus = Enum.Parse<PaymentStatus>(order.PaymentStatus), PaymentStatus = Enum.TryParse<PaymentStatus>(order.PaymentStatus, true, out var ps) ? ps : PaymentStatus.Pending,
ShippingTrackingNumber = order.ShippingTrackingNumber, ShippingTrackingNumber = order.ShippingTrackingNumber,
ShippedDate = order.ShippedDate, ShippedDate = order.ShippedDate,
DeliveredDate = order.DeliveredDate, DeliveredDate = order.DeliveredDate,