import _ from 'lodash' import calculateBatchItem from './calculateBatchItem' export default function calculateOrder (order: IAppOrder, promoDiscount: number = 0, riceCoinDiscount: number = 0): IAppOrder { // * 餐點原價 let originalTotal = 0 // * 餐點折扣 let calculatedDiscountTotal = 0 // * 餐點服務費 let surchargeTotal = 0 // * 餐點總價 let total = 0 order.batches.forEach(batch => { const calculatedItems = batch.items.map(item => calculateBatchItem(item, order)) originalTotal += _.sumBy(calculatedItems, 'originalTotal') calculatedDiscountTotal += _.sumBy(calculatedItems, 'totalDiscount') surchargeTotal += _.sumBy(calculatedItems, 'surcharge') total += _.sumBy(calculatedItems, 'total') }) // * 運費原價 let originalShippingFee = 0 if (order.shipping != null && order.deliveryType === 'storeDelivery') { if (order.shipping.totalFee != null) { originalShippingFee = order.shipping.totalFee } if (order.originalShippingFee != null) { originalShippingFee = order.originalShippingFee } } // * 運費折扣 let calculatedShippingFeeDiscount = 0 // * 運費總價 let shippingFeeTotal = originalShippingFee // 運費折扣 if (order.deliveryType === 'storeDelivery' && order.shipping != null && total > order.shipping.minAmount && order.shipping.discounts[0] != null) { const shippingDiscount = order.shipping.discounts[0] calculatedShippingFeeDiscount += shippingDiscount.amount shippingFeeTotal -= shippingDiscount.amount } // 折扣碼 if (promoDiscount > 0) { const newTotal = total -= promoDiscount // 實際折扣金額 const diffTotal = total - newTotal // 累計折扣金額 calculatedDiscountTotal += diffTotal // 更新 total total = newTotal } // calculate order level modifiers order.modifiers.forEach(modifier => { let newTotal = total let newShippingFeeTotal = shippingFeeTotal // 全單折扣或 RiceCoin if (modifier.applyTo === 'ALL' || modifier.type === 'RICECOIN') { // 將 total 及 shippingFeeTotal 計算 percent 折扣 newTotal = total * (100 + modifier.percent) / 100 newShippingFeeTotal = shippingFeeTotal * (100 + modifier.percent) / 100 // modifier amount 優先折在運費 if (modifier.amount > shippingFeeTotal) { // 折扣金額大於運費,折完運費繼續折餐點 shippingFeeTotal -= modifier.amount newTotal -= (modifier.amount - newShippingFeeTotal) } else { // 只折在運費 newShippingFeeTotal -= modifier.amount } } // 餐點折扣或優惠碼和店家給的折扣 if (modifier.applyTo === 'PRODUCT' || modifier.type === 'PROMOCODE' || modifier.type === 'MERCHANT') { // 計算折價後總計運費 newTotal = total * (100 + modifier.percent) / 100 + modifier.amount } // 運費折扣 if (modifier.applyTo === 'SHIPPING' || modifier.type === 'SHIPPING_DISCOUNT') { // 計算折價後總計運費 newShippingFeeTotal = shippingFeeTotal * (100 + modifier.percent) / 100 + modifier.amount } // 計算折了多少 const diffTotal = total - newTotal const diffShippingFee = shippingFeeTotal - newShippingFeeTotal // 累計折了多少 if (diffTotal > 0) { // 折扣 calculatedDiscountTotal += diffTotal } else { // 費用 surchargeTotal += Math.abs(diffTotal) } calculatedShippingFeeDiscount += diffShippingFee // 更新總計金額 total = newTotal shippingFeeTotal = newShippingFeeTotal }) // RiceCoin if (riceCoinDiscount > 0) { let newTotal = total let newShippingFeeTotal = shippingFeeTotal // 優先折在運費 if (riceCoinDiscount > shippingFeeTotal) { // 折扣金額大於運費,折完運費繼續折餐點 shippingFeeTotal -= riceCoinDiscount newTotal -= (riceCoinDiscount - newShippingFeeTotal) } else { // 只折在運費 newShippingFeeTotal -= riceCoinDiscount } // 計算折了多少 const diffTotal = total - newTotal const diffShippingFee = shippingFeeTotal - newShippingFeeTotal // 累計折了多少 calculatedDiscountTotal += diffTotal calculatedShippingFeeDiscount += diffShippingFee // 更新總計金額 total = newTotal shippingFeeTotal = newShippingFeeTotal } order.originalTotal = originalTotal order.calculatedDiscountTotal = calculatedDiscountTotal order.surchargeTotal = surchargeTotal order.total = total order.originalShippingFee = originalShippingFee order.discountShippingFee = calculatedShippingFeeDiscount order.shippingFee = shippingFeeTotal return order }