Files
ShopSolution-backend/Webshop.Application/Services/Customers/CheckoutService.cs
2025-08-29 13:57:39 +02:00

227 lines
11 KiB
C#

// src/Webshop.Application/Services/Customers/CheckoutService.cs
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Customers;
using Webshop.Application.DTOs.Orders;
using Webshop.Application.Services.Customers.Interfaces;
using Webshop.Application.Services.Public.Interfaces;
using Webshop.Domain.Entities;
using Webshop.Domain.Enums;
using Webshop.Domain.Interfaces;
using Webshop.Infrastructure.Data;
namespace Webshop.Application.Services.Customers
{
public class CheckoutService : ICheckoutService
{
private readonly ApplicationDbContext _context;
private readonly IOrderRepository _orderRepository;
private readonly ICustomerRepository _customerRepository;
private readonly IShippingMethodRepository _shippingMethodRepository;
private readonly ISettingService _settingService;
private readonly IDiscountService _discountService;
public CheckoutService(
ApplicationDbContext context,
IOrderRepository orderRepository,
ICustomerRepository customerRepository,
IShippingMethodRepository shippingMethodRepository,
ISettingService settingService,
IDiscountService discountService)
{
_context = context;
_orderRepository = orderRepository;
_customerRepository = customerRepository;
_shippingMethodRepository = shippingMethodRepository;
_settingService = settingService;
_discountService = discountService;
}
public async Task<(bool Success, OrderDetailDto? CreatedOrder, string? ErrorMessage)> CreateOrderAsync(CreateOrderDto orderDto, string? userId)
{
await using var transaction = await _context.Database.BeginTransactionAsync();
try
{
// --- 1. Validierung von Kunde, Adressen und Methoden ---
Customer? customer = null;
if (!string.IsNullOrEmpty(userId))
{
customer = await _customerRepository.GetByUserIdAsync(userId);
if (customer == null) return (false, null, "Kundenprofil nicht gefunden.");
// Validiere, dass die Adressen zum eingeloggten Kunden gehören
if (!await _context.Addresses.AnyAsync(a => a.Id == orderDto.ShippingAddressId && a.CustomerId == customer.Id) ||
!await _context.Addresses.AnyAsync(a => a.Id == orderDto.BillingAddressId && a.CustomerId == customer.Id))
{
return (false, null, "Ungültige oder nicht zugehörige Liefer- oder Rechnungsadresse.");
}
}
else
{
// Gast-Checkout: Validierung der Gast-Daten
if (string.IsNullOrWhiteSpace(orderDto.GuestEmail))
return (false, null, "Für Gastbestellungen ist eine E-Mail-Adresse erforderlich.");
// Hinweis: Bei Gastbestellungen müssten die Adress-DTOs mitgesendet und hier neue Adressen erstellt werden.
// Der Einfachheit halber nehmen wir an, die Adress-IDs sind gültig, aber nicht mit einem Kunden verknüpft.
}
var shippingMethod = await _shippingMethodRepository.GetByIdAsync(orderDto.ShippingMethodId);
if (shippingMethod == null || !shippingMethod.IsActive) return (false, null, "Ungültige oder inaktive Versandmethode.");
var paymentMethod = await _context.PaymentMethods.FindAsync(orderDto.PaymentMethodId);
if (paymentMethod == null || !paymentMethod.IsActive) return (false, null, "Ungültige oder inaktive Zahlungsmethode.");
// --- 2. Artikel verarbeiten und Lagerbestand prüfen ---
var orderItems = new List<OrderItem>();
var productIds = orderDto.Items.Select(i => i.ProductId).ToList();
var products = await _context.Products.Where(p => productIds.Contains(p.Id)).ToListAsync();
decimal itemsTotal = 0;
foreach (var itemDto in orderDto.Items)
{
var product = products.FirstOrDefault(p => p.Id == itemDto.ProductId);
if (product == null || !product.IsActive)
{
await transaction.RollbackAsync();
return (false, null, $"Ein Produkt im Warenkorb ist nicht mehr verfügbar.");
}
if (product.StockQuantity < itemDto.Quantity)
{
await transaction.RollbackAsync();
return (false, null, $"Nicht genügend Lagerbestand für '{product.Name}'. Verfügbar: {product.StockQuantity}, benötigt: {itemDto.Quantity}.");
}
product.StockQuantity -= itemDto.Quantity; // Lagerbestand reduzieren
var orderItem = new OrderItem
{
ProductId = product.Id,
ProductVariantId = itemDto.ProductVariantId,
ProductName = product.Name,
ProductSKU = product.SKU,
Quantity = itemDto.Quantity,
UnitPrice = product.Price,
TotalPrice = product.Price * itemDto.Quantity
};
orderItems.Add(orderItem);
itemsTotal += orderItem.TotalPrice;
}
// --- 3. Preise, Rabatte, Steuern und Gesamtbetrag berechnen ---
var discountResult = await _discountService.CalculateDiscountAsync(orderItems, orderDto.CouponCode);
decimal discountAmount = discountResult.TotalDiscountAmount;
decimal shippingCost = shippingMethod.BaseCost;
decimal subTotal = itemsTotal + shippingCost - discountAmount;
if (subTotal < 0) subTotal = 0;
decimal taxRate = await _settingService.GetSettingValueAsync<decimal>("GlobalTaxRate", 0.19m);
decimal taxAmount = subTotal * taxRate;
decimal orderTotal = subTotal + taxAmount;
// --- 4. Bestellung erstellen ---
var newOrder = new Order
{
CustomerId = customer?.Id,
GuestEmail = customer == null ? orderDto.GuestEmail : null,
GuestPhoneNumber = customer == null ? orderDto.GuestPhoneNumber : null,
OrderNumber = $"WS-{DateTime.UtcNow:yyyyMMdd}-{new Random().Next(1000, 9999)}",
OrderDate = DateTimeOffset.UtcNow,
OrderStatus = OrderStatus.Pending.ToString(),
PaymentStatus = PaymentStatus.Pending.ToString(),
OrderTotal = orderTotal,
ShippingCost = shippingCost,
TaxAmount = taxAmount,
DiscountAmount = discountAmount,
PaymentMethod = paymentMethod.Name,
PaymentMethodId = paymentMethod.Id,
ShippingMethodId = shippingMethod.Id,
BillingAddressId = orderDto.BillingAddressId,
ShippingAddressId = orderDto.ShippingAddressId,
OrderItems = orderItems
};
await _orderRepository.AddAsync(newOrder);
// --- 5. Rabattnutzung erhöhen ---
if (discountResult.AppliedDiscountIds.Any())
{
var appliedDiscounts = await _context.Discounts
.Where(d => discountResult.AppliedDiscountIds.Contains(d.Id))
.ToListAsync();
foreach (var discount in appliedDiscounts)
{
discount.CurrentUsageCount++;
}
}
await _context.SaveChangesAsync(); // Speichert Lagerbestandsänderungen & Rabattnutzung
await transaction.CommitAsync();
// --- 6. Erfolgreiche Antwort erstellen ---
var createdOrder = await _orderRepository.GetByIdAsync(newOrder.Id);
var orderDetailDto = MapToOrderDetailDto(createdOrder!);
return (true, orderDetailDto, null);
}
catch (Exception ex)
{
await transaction.RollbackAsync();
return (false, null, $"Ein unerwarteter Fehler ist aufgetreten: {ex.Message}");
}
}
private OrderDetailDto MapToOrderDetailDto(Order order)
{
return new OrderDetailDto
{
Id = order.Id,
OrderNumber = order.OrderNumber,
OrderDate = order.OrderDate,
CustomerId = order.CustomerId,
Status = Enum.Parse<OrderStatus>(order.OrderStatus),
TotalAmount = order.OrderTotal,
ShippingAddress = new AddressDto
{
Id = order.ShippingAddress.Id,
FirstName = order.ShippingAddress.FirstName,
LastName = order.ShippingAddress.LastName,
Street = order.ShippingAddress.Street,
HouseNumber = order.ShippingAddress.HouseNumber,
City = order.ShippingAddress.City,
PostalCode = order.ShippingAddress.PostalCode,
Country = order.ShippingAddress.Country,
Type = order.ShippingAddress.Type
},
BillingAddress = new AddressDto
{
Id = order.BillingAddress.Id,
FirstName = order.BillingAddress.FirstName,
LastName = order.BillingAddress.LastName,
Street = order.BillingAddress.Street,
HouseNumber = order.BillingAddress.HouseNumber,
City = order.BillingAddress.City,
PostalCode = order.BillingAddress.PostalCode,
Country = order.BillingAddress.Country,
Type = order.BillingAddress.Type
},
PaymentMethod = order.PaymentMethod,
PaymentStatus = Enum.Parse<PaymentStatus>(order.PaymentStatus),
ShippingTrackingNumber = order.ShippingTrackingNumber,
ShippedDate = order.ShippedDate,
DeliveredDate = order.DeliveredDate,
OrderItems = order.OrderItems.Select(oi => new OrderItemDto
{
Id = oi.Id,
ProductId = oi.ProductId,
ProductVariantId = oi.ProductVariantId,
ProductName = oi.ProductName,
ProductSKU = oi.ProductSKU,
Quantity = oi.Quantity,
UnitPrice = oi.UnitPrice,
TotalPrice = oi.TotalPrice
}).ToList()
};
}
}
}