Added coupon processing. Fixed price calculation
This commit is contained in:
parent
3d4f22f6f6
commit
2dd1d1f69c
2 changed files with 48 additions and 21 deletions
|
@ -61,13 +61,13 @@ public class StoreOrderService
|
|||
if (coupon != null && user.CouponUses.Any(x => x.Coupon.Id == coupon.Id))
|
||||
throw new DisplayException("Coupon already used");
|
||||
|
||||
if (coupon != null && coupon.Amount == 0)
|
||||
if (coupon != null && coupon.Amount < 1)
|
||||
throw new DisplayException("No coupon uses left");
|
||||
|
||||
var price = product.Price * durationMultiplier;
|
||||
|
||||
if (coupon != null)
|
||||
price = Math.Round(price * coupon.Percent / 100, 2);
|
||||
price = Math.Round(price - (price * coupon.Percent / 100), 2);
|
||||
|
||||
if (user.Balance < price)
|
||||
throw new DisplayException("Order is too expensive");
|
||||
|
@ -100,7 +100,7 @@ public class StoreOrderService
|
|||
var price = p.Price * durationMultiplier;
|
||||
|
||||
if (c != null)
|
||||
price = Math.Round(price * c.Percent / 100, 2);
|
||||
price = Math.Round(price - (price * c.Percent / 100), 2);
|
||||
|
||||
// Calculate duration
|
||||
var duration = durationMultiplier * p.Duration;
|
||||
|
@ -108,10 +108,37 @@ public class StoreOrderService
|
|||
// Add transaction
|
||||
await transactionService.Add(u, -1 * price, $"Bought product '{p.Name}' for {duration} days");
|
||||
|
||||
// Create service
|
||||
return await serviceService.Admin.Create(u, p, service =>
|
||||
// Process coupon if used
|
||||
if (c != null)
|
||||
{
|
||||
service.RenewAt = DateTime.UtcNow.AddDays(duration);
|
||||
// Remove one use from the coupon
|
||||
var couponRepo = scope.ServiceProvider.GetRequiredService<Repository<Coupon>>();
|
||||
|
||||
var coupon = couponRepo
|
||||
.Get()
|
||||
.First(x => x.Id == c.Id);
|
||||
|
||||
coupon.Amount--;
|
||||
couponRepo.Update(coupon);
|
||||
|
||||
// Add coupon use to user
|
||||
var userRepo = scope.ServiceProvider.GetRequiredService<Repository<User>>();
|
||||
|
||||
var user = userRepo
|
||||
.Get()
|
||||
.Include(x => x.CouponUses)
|
||||
.First(x => x.Id == u.Id);
|
||||
|
||||
user.CouponUses.Add(new ()
|
||||
{
|
||||
Coupon = coupon
|
||||
});
|
||||
|
||||
userRepo.Update(user);
|
||||
}
|
||||
|
||||
// Create service
|
||||
return await serviceService.Admin.Create(u, p,
|
||||
service => { service.RenewAt = DateTime.UtcNow.AddDays(duration); });
|
||||
}
|
||||
}
|
|
@ -75,7 +75,7 @@ TODO: Add 404 here
|
|||
if (SelectedCoupon == null)
|
||||
actualPrice = defaultPrice;
|
||||
else
|
||||
actualPrice = Math.Round(defaultPrice * SelectedCoupon.Percent / 100, 2);
|
||||
actualPrice = Math.Round(defaultPrice - (defaultPrice * SelectedCoupon.Percent / 100), 2);
|
||||
|
||||
var currency = ConfigService.Get().Store.Currency;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue