Files
weiguang-jewelry-ledger/新账系统/web/src/lib/materialQuote.js
T

91 lines
3.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { grams, yuan } from './format';
export function validateMaterialQuote(result, input, memberKeys) {
const integer = value => Number.isSafeInteger(value) && value >= 0,
amount = value => value === null || integer(value);
if (
!result ||
result.date !== input.date ||
result.member !== input.member ||
!Array.isArray(result.entries) ||
result.entries.length !== input.entries.length ||
!Array.isArray(result.pools)
)
throw new Error('分来源料费未完整返回,请刷新料价。');
for (const original of input.entries) {
const row = result.entries.find(row => row.lineIndex === original.lineIndex);
if (
!row ||
row.poolId !== original.poolId ||
row.mg !== original.mg ||
!amount(row.amount) ||
!Array.isArray(row.allocations) ||
!Array.isArray(row.materialSources) ||
row.materialSources.reduce((sum, source) => sum + source.quantity, 0) !== row.mg
)
throw new Error('分来源料费与当前商品不一致,请重新核对。');
for (const source of row.materialSources)
if (
!['personal_recovery', 'public'].includes(source.kind) ||
(source.kind === 'personal_recovery' && source.member !== input.member) ||
!integer(source.quantity) ||
!amount(source.amount) ||
!Array.isArray(source.allocations) ||
source.allocations.some(part => !memberKeys.includes(part.payer) || !integer(part.amount)) ||
(source.amount !== null && source.allocations.reduce((sum, part) => sum + part.amount, 0) !== source.amount)
)
throw new Error('个人回收料或付款来源不完整,请重新读取。');
if (
row.amount !== null &&
(row.materialSources.some(source => source.amount === null) ||
row.materialSources.reduce((sum, source) => sum + source.amount, 0) !== row.amount ||
row.allocations.reduce((sum, part) => sum + part.amount, 0) !== row.amount)
)
throw new Error('分来源料费合计不一致,请重新读取。');
}
if (
result.pools.length !== new Set(input.entries.map(row => row.poolId)).size ||
result.pools.some(
pool =>
!input.entries.some(row => row.poolId === pool.poolId) ||
!['personalRemainingMg', 'publicRemainingMg', 'eligibleRemainingMg'].every(key => integer(pool[key])) ||
!amount(pool.personalRemainingAmount) ||
!amount(pool.publicRemainingAmount),
)
)
throw new Error('个人与公共可用克重未完整返回,请重新读取。');
return result;
}
export function describeMaterialSources(sources = [], memberName) {
return sources
.map(source => {
const label =
source.kind === 'personal_recovery'
? (source.member ? memberName(source.member) : '待补') + '个人回收料'
: '公共料';
const amount = source.amount == null ? '料费待补' : '¥' + yuan(source.amount);
const rate = source.averageRate == null ? '' : '(加权约 ¥' + yuan(source.averageRate) + '/克)';
return label + ' ' + grams(source.quantity) + 'g · ' + amount + rate;
})
.join(';');
}
export function describeMaterialBalance(pool) {
const before = pool.before
? '本次前可用:个人 ' +
grams(pool.before.personalRemainingMg) +
'g,公共 ' +
grams(pool.before.publicRemainingMg) +
'g;'
: '';
return (
before +
'本次后余:个人回收料 ' +
grams(pool.personalRemainingMg) +
'g,公共料 ' +
grams(pool.publicRemainingMg) +
'g。'
);
}