60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import {
|
||
rowAmount,
|
||
linesTotal,
|
||
lineTaxAmount,
|
||
taxTotal,
|
||
roundOffAmount,
|
||
netTotal,
|
||
openDebts,
|
||
debtTotal,
|
||
} from '../src/utils/salesman.js'
|
||
|
||
test('rowAmount / linesTotal', () => {
|
||
assert.equal(rowAmount({ qty: 2, price: 12.5 }), 25)
|
||
assert.equal(linesTotal([{ qty: 2, price: 12.5 }, { qty: 1, price: 6 }]), 31)
|
||
assert.equal(linesTotal([]), 0)
|
||
})
|
||
|
||
test('lineTaxAmount uses 价内拆分 (D4 同公式)', () => {
|
||
// 113 @13% → 税 13
|
||
assert.ok(Math.abs(lineTaxAmount(113, 0.13) - 13) < 1e-9)
|
||
assert.equal(lineTaxAmount(100, 0), 0)
|
||
assert.equal(lineTaxAmount(0, 0.13), 0)
|
||
})
|
||
|
||
test('taxTotal sums per-row 价内税', () => {
|
||
const rows = [
|
||
{ qty: 1, price: 113, taxRate: 0.13 },
|
||
{ qty: 1, price: 100, taxRate: 0 },
|
||
]
|
||
assert.ok(Math.abs(taxTotal(rows) - 13) < 1e-9)
|
||
})
|
||
|
||
test('roundOffAmount 抹零口径与 Salesman.vue 一致', () => {
|
||
assert.equal(roundOffAmount(31, ''), 0)
|
||
assert.equal(roundOffAmount(31, '1'), 0)
|
||
// 31.37 抹角 → 0.07
|
||
assert.ok(Math.abs(roundOffAmount(31.37, '0.1') - 0.07) < 1e-9)
|
||
// 31.37 抹分 → 0
|
||
assert.equal(roundOffAmount(31.37, '0.01'), 0)
|
||
})
|
||
|
||
test('netTotal = 行合计 - 抹零', () => {
|
||
const rows = [{ qty: 2, price: 12.5 }, { qty: 1, price: 6 }]
|
||
assert.ok(Math.abs(netTotal(rows, '0.1') - 31) < 1e-9)
|
||
assert.ok(Math.abs(netTotal([{ qty: 1, price: 31.37 }], '0.1') - 31.3) < 1e-9)
|
||
})
|
||
|
||
test('openDebts 只留 open/partial,debtTotal 求和 balance', () => {
|
||
const rows = [
|
||
{ id: 1, status: 'open', balance: '31.00' },
|
||
{ id: 2, status: 'partial', balance: '10.50' },
|
||
{ id: 3, status: 'paid', balance: '0' },
|
||
]
|
||
assert.equal(openDebts(rows).length, 2)
|
||
assert.ok(Math.abs(debtTotal(rows) - 41.5) < 1e-9)
|
||
assert.equal(debtTotal([]), 0)
|
||
})
|