littleshop/TeleBot/TeleBot/Models/ShoppingCart.cs
SysAdmin 034b8facee Implement product multi-buys and variants system
Major restructuring of product variations:
- Renamed ProductVariation to ProductMultiBuy for quantity-based pricing (e.g., "3 for £25")
- Added new ProductVariant model for string-based options (colors, flavors)
- Complete separation of multi-buy pricing from variant selection

Features implemented:
- Multi-buy deals with automatic price-per-unit calculation
- Product variants for colors/flavors/sizes with stock tracking
- TeleBot checkout supports both multi-buys and variant selection
- Shopping cart correctly calculates multi-buy bundle prices
- Order system tracks selected variants and multi-buy choices
- Real-time bot activity monitoring with SignalR
- Public bot directory page with QR codes for Telegram launch
- Admin dashboard shows multi-buy and variant metrics

Technical changes:
- Updated all DTOs, services, and controllers
- Fixed cart total calculation for multi-buy bundles
- Comprehensive test coverage for new functionality
- All existing tests passing with new features

Database changes:
- Migrated ProductVariations to ProductMultiBuys
- Added ProductVariants table
- Updated OrderItems to track variants

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-21 00:30:12 +01:00

124 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace TeleBot.Models
{
public class ShoppingCart
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public List<CartItem> Items { get; set; } = new();
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
public void AddItem(Guid productId, string productName, decimal price, int quantity = 1, Guid? multiBuyId = null, string? selectedVariant = null)
{
var existingItem = Items.FirstOrDefault(i =>
i.ProductId == productId && i.MultiBuyId == multiBuyId && i.SelectedVariant == selectedVariant);
if (existingItem != null)
{
// For multi-buys, we don't add quantities - each multi-buy is a separate bundle
// For regular items, we add the quantities together
if (!multiBuyId.HasValue)
{
existingItem.Quantity += quantity;
existingItem.UpdateTotalPrice();
}
// If it's a multi-buy and already exists, we don't add it again
// (user should explicitly add another multi-buy bundle if they want more)
}
else
{
var newItem = new CartItem
{
ProductId = productId,
MultiBuyId = multiBuyId,
SelectedVariant = selectedVariant,
ProductName = productName,
UnitPrice = price,
Quantity = quantity
};
newItem.UpdateTotalPrice(); // Ensure total is calculated after all properties are set
Items.Add(newItem);
}
UpdatedAt = DateTime.UtcNow;
}
public void RemoveItem(Guid productId)
{
Items.RemoveAll(i => i.ProductId == productId);
UpdatedAt = DateTime.UtcNow;
}
public void UpdateQuantity(Guid productId, int quantity)
{
var item = Items.FirstOrDefault(i => i.ProductId == productId);
if (item != null)
{
if (quantity <= 0)
{
RemoveItem(productId);
}
else
{
item.Quantity = quantity;
item.UpdateTotalPrice();
UpdatedAt = DateTime.UtcNow;
}
}
}
public void Clear()
{
Items.Clear();
UpdatedAt = DateTime.UtcNow;
}
public decimal GetTotalAmount()
{
return Items.Sum(i => i.TotalPrice);
}
public int GetTotalItems()
{
return Items.Sum(i => i.Quantity);
}
public bool IsEmpty()
{
return !Items.Any();
}
}
public class CartItem
{
public Guid ProductId { get; set; }
public Guid? MultiBuyId { get; set; } // For quantity pricing (e.g., 3 for £25)
public string? SelectedVariant { get; set; } // For color/flavor selection
public string ProductName { get; set; } = string.Empty;
public int Quantity { get; set; }
public decimal UnitPrice { get; set; } // For multi-buys, this is the bundle price; for regular items, it's per-unit
public decimal TotalPrice { get; set; }
public CartItem()
{
// Don't calculate total in constructor - wait for properties to be set
}
public void UpdateTotalPrice()
{
// For multi-buys, UnitPrice is already the total bundle price
// For regular items, we need to multiply by quantity
if (MultiBuyId.HasValue)
{
TotalPrice = UnitPrice; // Bundle price, not multiplied
}
else
{
TotalPrice = UnitPrice * Quantity; // Regular per-unit pricing
}
}
}
}