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