206 lines
10 KiB
C#
206 lines
10 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;
|
|
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<ServiceResult<OrderDetailDto>> CreateOrderAsync(CreateOrderDto orderDto, string? userId)
|
|
{
|
|
await using var transaction = await _context.Database.BeginTransactionAsync();
|
|
try
|
|
{
|
|
// 1. Validierung von Kunde, Adressen und Methoden
|
|
var customer = await _customerRepository.GetByUserIdAsync(userId!);
|
|
if (customer == null)
|
|
return ServiceResult.Fail<OrderDetailDto>(ServiceResultType.Unauthorized, "Kundenprofil nicht gefunden.");
|
|
|
|
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 ServiceResult.Fail<OrderDetailDto>(ServiceResultType.Forbidden, "Ungültige oder nicht zugehörige Liefer- oder Rechnungsadresse.");
|
|
}
|
|
|
|
var shippingMethod = await _shippingMethodRepository.GetByIdAsync(orderDto.ShippingMethodId);
|
|
if (shippingMethod == null || !shippingMethod.IsActive)
|
|
return ServiceResult.Fail<OrderDetailDto>(ServiceResultType.InvalidInput, "Ungültige oder inaktive Versandmethode.");
|
|
|
|
var paymentMethod = await _context.PaymentMethods.FindAsync(orderDto.PaymentMethodId);
|
|
if (paymentMethod == null || !paymentMethod.IsActive)
|
|
return ServiceResult.Fail<OrderDetailDto>(ServiceResultType.InvalidInput, "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 ServiceResult.Fail<OrderDetailDto>(ServiceResultType.Conflict, "Ein Produkt im Warenkorb ist nicht mehr verfügbar.");
|
|
}
|
|
if (product.StockQuantity < itemDto.Quantity)
|
|
{
|
|
await transaction.RollbackAsync();
|
|
return ServiceResult.Fail<OrderDetailDto>(ServiceResultType.Conflict, $"Nicht genügend Lagerbestand für '{product.Name}'. Verfügbar: {product.StockQuantity}, benötigt: {itemDto.Quantity}.");
|
|
}
|
|
|
|
product.StockQuantity -= itemDto.Quantity;
|
|
|
|
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,
|
|
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();
|
|
await transaction.CommitAsync();
|
|
|
|
// 6. Erfolgreiche Antwort erstellen
|
|
var createdOrder = await _orderRepository.GetByIdAsync(newOrder.Id);
|
|
return ServiceResult.Ok(MapToOrderDetailDto(createdOrder!));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await transaction.RollbackAsync();
|
|
return ServiceResult.Fail<OrderDetailDto>(ServiceResultType.Failure, $"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()
|
|
};
|
|
}
|
|
}
|
|
} |