diff --git a/Netina.Core/EntityServices/DiscountHandlers/CalculateOrderDiscountCommandHandler.cs b/Netina.Core/EntityServices/DiscountHandlers/CalculateOrderDiscountCommandHandler.cs index 848ef80..b3404bf 100644 --- a/Netina.Core/EntityServices/DiscountHandlers/CalculateOrderDiscountCommandHandler.cs +++ b/Netina.Core/EntityServices/DiscountHandlers/CalculateOrderDiscountCommandHandler.cs @@ -73,6 +73,18 @@ public class CalculateOrderDiscountCommandHandler( } } + else if (discount.Type == DiscountType.Brand) + { + + var brandDiscount = await repositoryWrapper.SetRepository() + .TableNoTracking + .FirstOrDefaultAsync(d => d.Code == request.DiscountCode, cancellationToken); + + if (brandDiscount != null && !brandDiscount.IsExpired()) + { + totalPrice = request.Order.OrderProducts.Where(op => op.BrandId == brandDiscount.BrandId).Sum(op => op.ProductCost); + } + } else if (discount.Type == DiscountType.Subscriber) { throw new NotImplementedException("Subscribe discount not implemented"); diff --git a/Netina.Domain/CommandQueries/Commands/DiscountCommands.cs b/Netina.Domain/CommandQueries/Commands/DiscountCommands.cs index acf51b6..da9f253 100644 --- a/Netina.Domain/CommandQueries/Commands/DiscountCommands.cs +++ b/Netina.Domain/CommandQueries/Commands/DiscountCommands.cs @@ -21,7 +21,8 @@ bool IsForInvitation, bool IsSpecialOffer, bool IsForFirstPurchase, Guid ProductId, -Guid CategoryId) : IRequest; +Guid CategoryId, +Guid BrandId) : IRequest; public sealed record UpdateDiscountCommand( Guid Id, diff --git a/Netina.Domain/Entities/Discounts/BrandDiscount.cs b/Netina.Domain/Entities/Discounts/BrandDiscount.cs new file mode 100644 index 0000000..450b270 --- /dev/null +++ b/Netina.Domain/Entities/Discounts/BrandDiscount.cs @@ -0,0 +1,21 @@ +namespace Netina.Domain.Entities.Discounts; + +public partial class BrandDiscount : Discount +{ + public BrandDiscount() + { + + } + + public BrandDiscount(string code, string description, int discountPercent, long discountAmount, bool hasCode, DiscountAmountType amountType, DiscountType type, + int count, bool immortal, DateTime startDate, DateTime expireDate, long priceFloor, bool hasPriceFloor, long priceCeiling, + bool hasPriceCeiling, bool isInfinity, long useCount, bool isForInvitation, + bool isForFirstPurchase, bool isSpecialOffer , Guid brandId) + : base(code, description, discountPercent, discountAmount, hasCode, amountType, type, count, startDate, expireDate, priceFloor, hasPriceFloor, priceCeiling, hasPriceCeiling, isInfinity, useCount, + isForInvitation, isSpecialOffer,isForFirstPurchase, immortal) + { + BrandId = brandId; + } + public Guid BrandId { get; internal set; } + public Brand? Brand { get; internal set; } +} \ No newline at end of file diff --git a/Netina.Domain/Entities/Discounts/Discount.Aggregate.cs b/Netina.Domain/Entities/Discounts/Discount.Aggregate.cs index 4c7c884..124df49 100644 --- a/Netina.Domain/Entities/Discounts/Discount.Aggregate.cs +++ b/Netina.Domain/Entities/Discounts/Discount.Aggregate.cs @@ -39,6 +39,19 @@ public partial class Discount } } +public partial class BrandDiscount +{ + public static BrandDiscount Create(string code, string description, int discountPercent, long discountAmount, bool hasCode, DiscountAmountType amountType, DiscountType type, + int count, bool immortal, DateTime startDate, DateTime expireDate, long priceFloor, bool hasPriceFloor, long priceCeiling, + bool hasPriceCeiling, bool isInfinity, long useCount, bool isForInvitation, + bool isForFirstPurchase, bool isSpecialOffer, Guid brandId) + { + return new BrandDiscount(code, description, discountPercent, discountAmount, hasCode, amountType, type, count, immortal, startDate, + expireDate, priceFloor, hasPriceFloor, priceCeiling, hasPriceCeiling, isInfinity, useCount, + isForInvitation, isForFirstPurchase, isSpecialOffer, brandId); + } +} + public partial class ProductDiscount { public static ProductDiscount Create(string code, string description, int discountPercent, long discountAmount, bool hasCode, DiscountAmountType amountType, DiscountType type, diff --git a/Netina.Domain/Entities/Orders/Order.Aggregate.cs b/Netina.Domain/Entities/Orders/Order.Aggregate.cs index 8a52de9..2f35dfe 100644 --- a/Netina.Domain/Entities/Orders/Order.Aggregate.cs +++ b/Netina.Domain/Entities/Orders/Order.Aggregate.cs @@ -12,7 +12,10 @@ public partial class Order } - public void AddToOrderBag(Guid productId, double cost, double costWithDiscount, bool hasDiscount, double packingCost, Guid categoryId, int count) + public void AddToOrderBag(Guid productId, double cost, double costWithDiscount, bool hasDiscount, double packingCost, + Guid categoryId, + Guid brandId, + int count) { var orderProduct = OrderProducts.FirstOrDefault(op => op.ProductId == productId); if (orderProduct == null) @@ -25,6 +28,7 @@ public partial class Order OrderStatus.OrderBag, productId, categoryId, + brandId, this.Id); OrderProducts.Add(orderProduct); } @@ -44,7 +48,10 @@ public partial class Order } } - public void ChangeOrderBag(Guid productId, double cost, double costWithDiscount, bool hasDiscount, double packingCost, Guid categoryId, int count) + public void ChangeOrderBag(Guid productId, double cost, double costWithDiscount, bool hasDiscount, double packingCost, + Guid categoryId, + Guid brandId, + int count) { var orderProduct = OrderProducts.FirstOrDefault(op => op.ProductId == productId); if (orderProduct != null) @@ -52,10 +59,10 @@ public partial class Order if (orderProduct.Count > count) RemoveFromOrderBag(productId, count); else - AddToOrderBag(productId, cost, costWithDiscount, hasDiscount, packingCost, categoryId, count); + AddToOrderBag(productId, cost, costWithDiscount, hasDiscount, packingCost, categoryId,brandId, count); } else - AddToOrderBag(productId, cost, costWithDiscount, hasDiscount, packingCost, categoryId, count); + AddToOrderBag(productId, cost, costWithDiscount, hasDiscount, packingCost, categoryId, brandId, count); } @@ -165,11 +172,16 @@ public partial class OrderProduct OrderStatus orderProductStatus, Guid productId, Guid productCategoryId, + Guid brandId, Guid orderId) { var productCost = count * productFeeWithDiscount; var packingCost = count * packingFee; - return new OrderProduct(count, productFee, productFeeWithDiscount, hasDiscount, productCost, packingFee, packingCost, orderProductStatus, productId, productCategoryId, orderId); + return new OrderProduct(count, productFee, + productFeeWithDiscount, hasDiscount, + productCost, packingFee, packingCost, + orderProductStatus, productId, + productCategoryId, orderId,brandId); } public void SetCount(int count) diff --git a/Netina.Domain/Entities/Orders/OrderProduct.cs b/Netina.Domain/Entities/Orders/OrderProduct.cs index c774541..4316c0e 100644 --- a/Netina.Domain/Entities/Orders/OrderProduct.cs +++ b/Netina.Domain/Entities/Orders/OrderProduct.cs @@ -19,7 +19,8 @@ public partial class OrderProduct : ApiEntity OrderStatus orderProductStatus, Guid productId, Guid productCategoryId, - Guid orderId) + Guid orderId, + Guid brandId) { Count = count; ProductFee = productFee; @@ -32,6 +33,7 @@ public partial class OrderProduct : ApiEntity ProductCategoryId = productCategoryId; PackingFee = packingFee; PackingCost = packingCost; + BrandId = brandId; } public int Count { get; internal set; } public double ProductFee { get; internal set; } @@ -43,6 +45,7 @@ public partial class OrderProduct : ApiEntity public OrderStatus OrderProductStatus { get; internal set; } public Guid ProductId { get; internal set; } + public Guid BrandId { get; internal set; } public Guid ProductCategoryId { get; internal set; } public Product? Product { get; internal set; } diff --git a/Netina.Domain/Enums/DiscountType.cs b/Netina.Domain/Enums/DiscountType.cs index cbc49e1..3c77813 100644 --- a/Netina.Domain/Enums/DiscountType.cs +++ b/Netina.Domain/Enums/DiscountType.cs @@ -9,5 +9,7 @@ public enum DiscountType [Display(Name = "دسته ای")] Category = 2, [Display(Name = "مشترکی")] - Subscriber = 3 + Subscriber = 3, + [Display(Name = "برند")] + Brand = 4, } \ No newline at end of file diff --git a/Netina.Repository/Handlers/Discounts/CreateDiscountCommandHandler.cs b/Netina.Repository/Handlers/Discounts/CreateDiscountCommandHandler.cs index 83e75eb..a26054d 100644 --- a/Netina.Repository/Handlers/Discounts/CreateDiscountCommandHandler.cs +++ b/Netina.Repository/Handlers/Discounts/CreateDiscountCommandHandler.cs @@ -48,6 +48,13 @@ public class CreateDiscountCommandHandler(IRepositoryWrapper repositoryWrapper) request.IsForInvitation, request.IsForFirstPurchase, request.IsSpecialOffer, request.ProductId); repositoryWrapper.SetRepository().Add(productDis); break; + case DiscountType.Brand: + var brandDis = BrandDiscount.Create(request.Code, request.Description, request.DiscountPercent, request.DiscountAmount, request.HasCode, + request.AmountType, request.Type, request.Count, request.IsImmortal, request.StartDate, request.ExpireDate, request.PriceFloor, + request.HasPriceFloor, request.PriceCeiling, request.HasPriceCeiling, request.IsInfinity, request.UseCount, + request.IsForInvitation, request.IsForFirstPurchase, request.IsSpecialOffer, request.BrandId); + repositoryWrapper.SetRepository().Add(brandDis); + break; default: var def = Discount.Create(request.Code, request.Description, request.DiscountPercent, request.DiscountAmount, request.HasCode, request.AmountType, request.Type, request.Count, request.IsImmortal, request.StartDate, request.ExpireDate, request.PriceFloor, diff --git a/Netina.Repository/Handlers/Discounts/UpdateDiscountCommandHandler.cs b/Netina.Repository/Handlers/Discounts/UpdateDiscountCommandHandler.cs index 1cc746d..0741e89 100644 --- a/Netina.Repository/Handlers/Discounts/UpdateDiscountCommandHandler.cs +++ b/Netina.Repository/Handlers/Discounts/UpdateDiscountCommandHandler.cs @@ -55,6 +55,23 @@ public class UpdateDiscountCommandHandler(IRepositoryWrapper repositoryWrapper) productDis.CreatedBy = productEnt.CreatedBy; repositoryWrapper.SetRepository().Update(productDis); break; + + case DiscountType.Brand: + var brandEnt = await repositoryWrapper.SetRepository().TableNoTracking.FirstOrDefaultAsync(d => d.Id == request.Id, cancellationToken); + if (brandEnt == null) + throw new AppException("Discount not found", ApiResultStatusCode.NotFound); + + var brandDis = BrandDiscount.Create(request.Code, request.Description, + request.DiscountPercent, request.DiscountAmount, request.HasCode, + request.AmountType, request.Type, request.Count, request.IsImmortal, + request.StartDate, request.ExpireDate, request.PriceFloor, request.HasPriceFloor, + request.PriceCeiling, request.HasPriceCeiling, request.IsInfinity, + request.UseCount, request.IsForInvitation, request.IsForFirstPurchase, request.IsSpecialOffer, request.ProductId); + brandDis.Id = brandEnt.Id; + brandDis.CreatedAt = brandEnt.CreatedAt; + brandDis.CreatedBy = brandEnt.CreatedBy; + repositoryWrapper.SetRepository().Update(brandDis); + break; } await repositoryWrapper.SaveChangesAsync(cancellationToken); return true;