Fix: 修复定金计算逻辑
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "tsc -b && vite build",
|
||||
"prod": "tsc -b && vite build --base calc",
|
||||
"lint": "eslint .",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
allowBuilds:
|
||||
esbuild: true
|
||||
+14
-14
@@ -26,21 +26,21 @@ const packages: Package[] = [
|
||||
const COMBO_DISCOUNT = 5;
|
||||
|
||||
const DEPOSIT_RATE = 0.5;
|
||||
/** 当「订单金额 ×50%」低于该值,且订单金额超过该值时,定金取该值 */
|
||||
const DEPOSIT_FLOOR = 50;
|
||||
/** 定金封顶:50% 超过该值时只收该金额,避免大额订单定金过高 */
|
||||
const DEPOSIT_CAP = 50;
|
||||
|
||||
function formatMoney(n: number): string {
|
||||
return n % 1 === 0 ? String(n) : n.toFixed(1);
|
||||
}
|
||||
|
||||
/** 定金:默认 50%;若 50% 不足 50 元且订单超过 50 元,则定金为 50 元(且不超过订单总额) */
|
||||
function computeDeposit(finalPrice: number): { amount: number; isFloor50: boolean; half: number } {
|
||||
if (finalPrice <= 0) return { amount: 0, isFloor50: false, half: 0 };
|
||||
/** 定金:默认应付的 50%;若 50% 超过 50 元则封顶 50 元(且不超过订单总额) */
|
||||
function computeDeposit(finalPrice: number): { amount: number; isCapped: boolean; half: number } {
|
||||
if (finalPrice <= 0) return { amount: 0, isCapped: false, half: 0 };
|
||||
const half = finalPrice * DEPOSIT_RATE;
|
||||
if (finalPrice > DEPOSIT_FLOOR && half < DEPOSIT_FLOOR) {
|
||||
return { amount: Math.min(DEPOSIT_FLOOR, finalPrice), isFloor50: true, half };
|
||||
if (half > DEPOSIT_CAP) {
|
||||
return { amount: Math.min(DEPOSIT_CAP, finalPrice), isCapped: true, half };
|
||||
}
|
||||
return { amount: half, isFloor50: false, half };
|
||||
return { amount: half, isCapped: false, half };
|
||||
}
|
||||
|
||||
function App() {
|
||||
@@ -70,7 +70,7 @@ function App() {
|
||||
|
||||
const hasCombo = selectedSingle.length > 0 && selectedDual.length > 0;
|
||||
const finalPrice = hasCombo ? originalTotal - COMBO_DISCOUNT : originalTotal;
|
||||
const { amount: deposit, isFloor50, half: depositHalf } = computeDeposit(finalPrice);
|
||||
const { amount: deposit, isCapped, half: depositHalf } = computeDeposit(finalPrice);
|
||||
const balance = finalPrice - deposit;
|
||||
|
||||
const totalSinglePhotos = selectedSingle.reduce((sum, p) => sum + p.photos * getQty(p.id), 0);
|
||||
@@ -218,9 +218,9 @@ function App() {
|
||||
<div className="price-row deposit-row">
|
||||
<span>
|
||||
定金
|
||||
{isFloor50 ? (
|
||||
{isCapped ? (
|
||||
<span className="deposit-hint">
|
||||
({DEPOSIT_RATE * 100}% 为 {formatMoney(depositHalf)} 元,不足 ¥{DEPOSIT_FLOOR};订单 > ¥{DEPOSIT_FLOOR} 时收 ¥{DEPOSIT_FLOOR} 定金)
|
||||
({DEPOSIT_RATE * 100}% 为 {formatMoney(depositHalf)} 元,超过 ¥{DEPOSIT_CAP} 时封顶 ¥{DEPOSIT_CAP})
|
||||
</span>
|
||||
) : (
|
||||
<span className="deposit-hint">(应付×{DEPOSIT_RATE * 100}%)</span>
|
||||
@@ -253,9 +253,9 @@ function App() {
|
||||
</p>
|
||||
<p className="formula-line">
|
||||
<span className="formula-label">定金:</span>
|
||||
{isFloor50 ? (
|
||||
{isCapped ? (
|
||||
<span>
|
||||
应付 {formatMoney(finalPrice)} × {DEPOSIT_RATE} = {formatMoney(depositHalf)} 元 < ¥{DEPOSIT_FLOOR},且应付 > ¥{DEPOSIT_FLOOR},收{" "}
|
||||
应付 {formatMoney(finalPrice)} × {DEPOSIT_RATE} = {formatMoney(depositHalf)} 元 > ¥{DEPOSIT_CAP},封顶收{" "}
|
||||
<strong>¥{formatMoney(deposit)}</strong>;尾款 <strong>¥{formatMoney(balance)}</strong>
|
||||
</span>
|
||||
) : finalPrice > 0 ? (
|
||||
@@ -272,7 +272,7 @@ function App() {
|
||||
<footer className="footer">
|
||||
<p>💡 单人 1 张 / 双人 1 张 为附加项,不单独接单</p>
|
||||
<p>💡 同时选择单人 + 双人套餐可享组合优惠 ¥{formatMoney(COMBO_DISCOUNT)}</p>
|
||||
<p>💡 定金默认应付的 {DEPOSIT_RATE * 100}%;当 {DEPOSIT_RATE * 100}% 不足 ¥{DEPOSIT_FLOOR} 且订单 > ¥{DEPOSIT_FLOOR} 时,定金为 ¥{DEPOSIT_FLOOR}</p>
|
||||
<p>💡 定金默认应付的 {DEPOSIT_RATE * 100}%;当 {DEPOSIT_RATE * 100}% 超过 ¥{DEPOSIT_CAP} 时,定金封顶为 ¥{DEPOSIT_CAP}</p>
|
||||
</footer>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user