Add variant collections system and enhance ProductVariant with weight/stock tracking
This commit introduces a comprehensive variant management system and enhances the existing ProductVariant model with per-variant weight overrides and stock tracking, integrated across Admin Panel and TeleBot. Features Added: - Variant Collections: Reusable variant templates (e.g., "Standard Sizes") - Admin UI for managing variant collections (CRUD operations) - Dynamic variant editor with JavaScript-based UI - Per-variant weight and weight unit overrides - Per-variant stock level tracking - SalesLedger model for financial tracking ProductVariant Enhancements: - Added Weight (decimal, nullable) field for variant-specific weights - Added WeightUnit (enum, nullable) field for variant-specific units - Maintains backward compatibility with product-level weights TeleBot Integration: - Enhanced variant selection UI to display stock levels - Shows weight information with proper unit conversion (µg, g, oz, lb, ml, L) - Compact button format: "Medium (15 in stock, 350g)" - Real-time stock availability display Database Migrations: - 20250928014850_AddVariantCollectionsAndSalesLedger - 20250928155814_AddWeightToProductVariants Technical Changes: - Updated Product model to support VariantCollectionId and VariantsJson - Extended ProductService with variant collection operations - Enhanced OrderService to handle variant-specific pricing and weights - Updated LittleShop.Client DTOs to match server models - Added JavaScript dynamic variant form builder Files Modified: 15 Files Added: 17 Lines Changed: ~2000 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -210,6 +210,9 @@ public class OrderService : IOrderService
|
||||
{
|
||||
var order = await _context.Orders
|
||||
.Include(o => o.Customer)
|
||||
.Include(o => o.Items)
|
||||
.ThenInclude(oi => oi.Product)
|
||||
.Include(o => o.Payments)
|
||||
.FirstOrDefaultAsync(o => o.Id == id);
|
||||
if (order == null) return false;
|
||||
|
||||
@@ -231,6 +234,12 @@ public class OrderService : IOrderService
|
||||
order.ShippedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
if (updateOrderStatusDto.Status == OrderStatus.PaymentReceived && previousStatus != OrderStatus.PaymentReceived)
|
||||
{
|
||||
await RecordSalesLedgerAsync(order);
|
||||
await DeductStockAsync(order);
|
||||
}
|
||||
|
||||
order.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
@@ -611,4 +620,52 @@ public class OrderService : IOrderService
|
||||
_ => $"Order #{orderId} status updated from {previousStatus} to {newStatus}."
|
||||
};
|
||||
}
|
||||
|
||||
private async Task RecordSalesLedgerAsync(Order order)
|
||||
{
|
||||
var payment = order.Payments.FirstOrDefault(p => p.Status == PaymentStatus.Completed);
|
||||
|
||||
foreach (var item in order.Items)
|
||||
{
|
||||
var ledgerEntry = new SalesLedger
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
OrderId = order.Id,
|
||||
ProductId = item.ProductId,
|
||||
ProductName = item.Product.Name,
|
||||
Quantity = item.Quantity,
|
||||
SalePriceFiat = item.TotalPrice,
|
||||
FiatCurrency = "GBP",
|
||||
SalePriceBTC = payment?.PaidAmount,
|
||||
Cryptocurrency = payment?.Currency.ToString(),
|
||||
SoldAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
_context.SalesLedgers.Add(ledgerEntry);
|
||||
|
||||
_logger.LogInformation("Recorded sales ledger entry for Order {OrderId}, Product {ProductId}, Quantity {Quantity}",
|
||||
order.Id, item.ProductId, item.Quantity);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DeductStockAsync(Order order)
|
||||
{
|
||||
foreach (var item in order.Items)
|
||||
{
|
||||
var product = await _context.Products.FindAsync(item.ProductId);
|
||||
if (product != null && product.StockQuantity >= item.Quantity)
|
||||
{
|
||||
product.StockQuantity -= item.Quantity;
|
||||
product.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
_logger.LogInformation("Deducted {Quantity} units from product {ProductId} stock (Order {OrderId})",
|
||||
item.Quantity, item.ProductId, order.Id);
|
||||
}
|
||||
else if (product != null)
|
||||
{
|
||||
_logger.LogWarning("Insufficient stock for product {ProductId}. Order {OrderId} requires {Required} but only {Available} available",
|
||||
item.ProductId, order.Id, item.Quantity, product.StockQuantity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user