update cart
This commit is contained in:
@@ -22,12 +22,13 @@ export interface Cart {
|
||||
coupon?: CartCoupon;
|
||||
addressId?: string;
|
||||
paymentMethodId?: string;
|
||||
paymentMethodFee: number;
|
||||
totalPrice: number;
|
||||
totalDiscount: number;
|
||||
couponDiscount: number;
|
||||
vatAmount: number;
|
||||
finalPrice: number;
|
||||
itemsDiscount: number;
|
||||
totalDiscount: number;
|
||||
subTotal: number;
|
||||
tax: number;
|
||||
deliveryFee: number;
|
||||
total: number;
|
||||
totalItems: number;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
|
||||
@@ -46,12 +46,13 @@ export class CartService {
|
||||
restaurantId,
|
||||
restaurantName: restaurant.name,
|
||||
items: [],
|
||||
paymentMethodFee: 0,
|
||||
totalPrice: 0,
|
||||
totalDiscount: 0,
|
||||
deliveryFee: 0,
|
||||
subTotal: 0,
|
||||
itemsDiscount: 0,
|
||||
couponDiscount: 0,
|
||||
vatAmount: 0,
|
||||
finalPrice: 0,
|
||||
totalDiscount: 0,
|
||||
tax: 0,
|
||||
total: 0,
|
||||
totalItems: 0,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
@@ -396,9 +397,9 @@ export class CartService {
|
||||
}
|
||||
|
||||
cart.paymentMethodId = setPaymentMethodDto.paymentMethodId;
|
||||
// cart.paymentMethodFee = Number(restaurantPaymentMethod.price) || 0;
|
||||
// cart.deliveryFee = Number(restaurantPaymentMethod.price) || 0;
|
||||
|
||||
// Recalculate cart totals to include payment method fee
|
||||
// Recalculate cart totals to include delivery fee
|
||||
await this.recalculateCartTotals(cart);
|
||||
await this.saveCart(cart);
|
||||
|
||||
@@ -406,66 +407,68 @@ export class CartService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Recalculate cart totals (including coupon discount and VAT)
|
||||
* Recalculate cart totals (including coupon discount and tax)
|
||||
*/
|
||||
private async recalculateCartTotals(cart: Cart): Promise<void> {
|
||||
let totalPrice = 0;
|
||||
let totalDiscount = 0;
|
||||
let subTotal = 0;
|
||||
let itemsDiscount = 0;
|
||||
let totalItems = 0;
|
||||
|
||||
for (const item of cart.items) {
|
||||
const itemPrice = item.price * item.quantity;
|
||||
const itemDiscount = item.discount * item.quantity;
|
||||
totalPrice += itemPrice;
|
||||
totalDiscount += itemDiscount;
|
||||
subTotal += itemPrice;
|
||||
itemsDiscount += itemDiscount;
|
||||
totalItems += item.quantity;
|
||||
}
|
||||
|
||||
cart.totalPrice = totalPrice;
|
||||
cart.totalDiscount = totalDiscount;
|
||||
cart.subTotal = subTotal;
|
||||
cart.itemsDiscount = itemsDiscount;
|
||||
|
||||
// Calculate coupon discount
|
||||
let couponDiscount = 0;
|
||||
if (cart.coupon) {
|
||||
if (cart.coupon.discountType === 'percentage') {
|
||||
const priceAfterItemDiscount = totalPrice - totalDiscount;
|
||||
const priceAfterItemDiscount = subTotal - itemsDiscount;
|
||||
couponDiscount = (priceAfterItemDiscount * cart.coupon.discount) / 100;
|
||||
} else {
|
||||
// amount discount
|
||||
const priceAfterItemDiscount = totalPrice - totalDiscount;
|
||||
const priceAfterItemDiscount = subTotal - itemsDiscount;
|
||||
couponDiscount = Math.min(cart.coupon.discount, priceAfterItemDiscount);
|
||||
}
|
||||
}
|
||||
|
||||
cart.couponDiscount = couponDiscount;
|
||||
const priceAfterDiscounts = totalPrice - totalDiscount - couponDiscount;
|
||||
// Calculate total discount: totalDiscount = couponDiscount + itemsDiscount
|
||||
cart.totalDiscount = couponDiscount + itemsDiscount;
|
||||
|
||||
// Calculate VAT if restaurant has VAT rate > 0
|
||||
let vatAmount = 0;
|
||||
// Calculate tax if restaurant has VAT rate > 0
|
||||
let tax = 0;
|
||||
const restaurant = await this.em.findOne(Restaurant, { id: cart.restaurantId });
|
||||
if (restaurant && restaurant.vat && restaurant.vat > 0) {
|
||||
// VAT is calculated on the price after discounts and coupons
|
||||
vatAmount = (priceAfterDiscounts * Number(restaurant.vat)) / 100;
|
||||
// Tax is calculated on the price after discounts
|
||||
const priceAfterDiscounts = subTotal - cart.totalDiscount;
|
||||
tax = (priceAfterDiscounts * Number(restaurant.vat)) / 100;
|
||||
}
|
||||
|
||||
cart.vatAmount = vatAmount;
|
||||
cart.tax = tax;
|
||||
|
||||
// Get payment method fee if payment method is set
|
||||
let paymentMethodFee = 0;
|
||||
// Get delivery fee if payment method is set
|
||||
let deliveryFee = 0;
|
||||
// if (cart.paymentMethodId) {
|
||||
// const restaurantPaymentMethod = await this.em.findOne(RestaurantPaymentMethod, {
|
||||
// restaurant: { id: cart.restaurantId },
|
||||
// paymentMethod: { id: cart.paymentMethodId },
|
||||
// });
|
||||
// if (restaurantPaymentMethod) {
|
||||
// paymentMethodFee = Number(restaurantPaymentMethod.price) || 0;
|
||||
// paymentMethodFee = 1;
|
||||
// deliveryFee = Number(restaurantPaymentMethod.price) || 0;
|
||||
// deliveryFee = 1;
|
||||
// }
|
||||
// }
|
||||
cart.paymentMethodFee = paymentMethodFee;
|
||||
cart.deliveryFee = deliveryFee;
|
||||
|
||||
// Final price includes items, discounts, VAT, and payment method fee
|
||||
cart.finalPrice = priceAfterDiscounts + vatAmount + paymentMethodFee;
|
||||
// Calculate total: total = subtotal – totalDiscount + tax + deliveryFee
|
||||
cart.total = subTotal - cart.totalDiscount + tax + deliveryFee;
|
||||
cart.totalItems = totalItems;
|
||||
cart.updatedAt = new Date().toISOString();
|
||||
}
|
||||
@@ -482,11 +485,13 @@ export class CartService {
|
||||
typeof cart.userId === 'string' &&
|
||||
typeof cart.restaurantId === 'string' &&
|
||||
Array.isArray(cart.items) &&
|
||||
typeof cart.totalPrice === 'number' &&
|
||||
typeof cart.totalDiscount === 'number' &&
|
||||
typeof cart.subTotal === 'number' &&
|
||||
typeof cart.itemsDiscount === 'number' &&
|
||||
typeof cart.couponDiscount === 'number' &&
|
||||
typeof cart.vatAmount === 'number' &&
|
||||
typeof cart.finalPrice === 'number' &&
|
||||
typeof cart.totalDiscount === 'number' &&
|
||||
typeof cart.tax === 'number' &&
|
||||
typeof cart.deliveryFee === 'number' &&
|
||||
typeof cart.total === 'number' &&
|
||||
typeof cart.totalItems === 'number' &&
|
||||
typeof cart.createdAt === 'string' &&
|
||||
typeof cart.updatedAt === 'string'
|
||||
|
||||
Reference in New Issue
Block a user