第4项:对页面、表单及两版共享规则启用严格类型检查
This commit is contained in:
1 parent
389952f31c
commit
f570929cfe
77 files changed
+7629
-2981
No files matched your search
+220
-99
@@ -1,14 +1,40 @@
|
||||
'use strict';
|
||||
// 两版对账共用的校验和归集规则。成员、当前查询以及日期校验由调用方提供。
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').Business} Business */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').DailyFunds} DailyFunds */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').OwnershipReceipts} OwnershipReceipts */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').PayerCosts} PayerCosts */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').Range} Range */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').RecoveryCosts} RecoveryCosts */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').Reimbursement} Reimbursement */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').ReimbursementList} ReimbursementList */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').Report} Report */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').Row} Row */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').RuleContext} RuleContext */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').Team} Team */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').CostItem} CostItem */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').ReceiptItem} ReceiptItem */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').Profit} Profit */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').MaterialValue} MaterialValue */
|
||||
/** @typedef {import('../web/src/pages/Report/contracts').AdvanceItem} AdvanceItem */
|
||||
(function (root) {
|
||||
function reportAfterSaleAmount(cost) {
|
||||
return cost.items.filter(item => item.kind === 'afterSale').reduce((sum, item) => sum + item.amount, 0);
|
||||
/** @param {unknown} value
|
||||
* @returns {value is number} */
|
||||
function integer(value) {
|
||||
return Number.isSafeInteger(value);
|
||||
}
|
||||
|
||||
/** @param {PayerCosts} cost */
|
||||
function reportAfterSaleAmount(cost) {
|
||||
return cost.items.filter(item => item.kind === 'afterSale').reduce((sum, item) => sum + (item.amount ?? 0), 0);
|
||||
}
|
||||
|
||||
/** @param {PayerCosts} cost */
|
||||
function reportCostItems(cost) {
|
||||
return cost.items.filter(item => item.kind !== 'goods' || item.amount !== 0);
|
||||
}
|
||||
|
||||
/** @param {Row} row */
|
||||
function reportRowCost(row) {
|
||||
const goods = (row.costItems || []).filter(item => item.kind === 'goods');
|
||||
return {
|
||||
@@ -16,10 +42,11 @@
|
||||
missing: goods.filter(item => item.amount == null).length,
|
||||
paidFees: (row.costItems || [])
|
||||
.filter(item => ['paid', 'afterSale'].includes(item.kind))
|
||||
.reduce((sum, item) => sum + item.amount, 0),
|
||||
.reduce((sum, item) => sum + (item.amount ?? 0), 0),
|
||||
};
|
||||
}
|
||||
|
||||
/** @param {Business} business */
|
||||
function reportCurrentAccountDays(business) {
|
||||
const account = business.currentAccount;
|
||||
return account.days.length
|
||||
@@ -42,6 +69,10 @@
|
||||
: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{amount:string}[]} items
|
||||
* @param {(value: string) => number} cents
|
||||
*/
|
||||
function reimbursementAmounts(items, cents) {
|
||||
let total = 0,
|
||||
complete = true;
|
||||
@@ -63,6 +94,7 @@
|
||||
};
|
||||
}
|
||||
|
||||
/** @param {RuleContext} context */
|
||||
function forContext({
|
||||
teamDataKeys,
|
||||
validOrderDate,
|
||||
@@ -73,7 +105,13 @@
|
||||
state,
|
||||
reportMember,
|
||||
}) {
|
||||
function validateReport(r, range) {
|
||||
/**
|
||||
* @param {unknown} input
|
||||
* @param {Range} range
|
||||
* @returns {asserts input is Report}
|
||||
*/
|
||||
function validateReport(input, range) {
|
||||
const r = /** @type {Report} */ (input);
|
||||
if (r.basis !== 'orderDate' || r.date !== range.date || r.from !== range.from)
|
||||
throw new Error('返回口径或日期与当前选择不一致,请重新读取。');
|
||||
if (
|
||||
@@ -87,7 +125,9 @@
|
||||
return (
|
||||
c &&
|
||||
Array.isArray(c.items) &&
|
||||
['goods', 'paidFees', 'total', 'missingCount', 'pendingCount'].every(name => Number.isSafeInteger(c[name]))
|
||||
/** @type {const} */ (['goods', 'paidFees', 'total', 'missingCount', 'pendingCount']).every(name =>
|
||||
Number.isSafeInteger(c[name]),
|
||||
)
|
||||
);
|
||||
})
|
||||
)
|
||||
@@ -103,7 +143,7 @@
|
||||
typeof person.name === 'string' &&
|
||||
person.name &&
|
||||
Array.isArray(person.items) &&
|
||||
['receipt', 'refund', 'net'].every(key => Number.isSafeInteger(person[key])) &&
|
||||
/** @type {const} */ (['receipt', 'refund', 'net']).every(key => Number.isSafeInteger(person[key])) &&
|
||||
person.items.every(
|
||||
item =>
|
||||
item &&
|
||||
@@ -113,14 +153,16 @@
|
||||
validOrderDate(item.date),
|
||||
) &&
|
||||
person.net === person.receipt - person.refund &&
|
||||
['receipt', 'refund'].every(
|
||||
/** @type {const} */ (['receipt', 'refund']).every(
|
||||
kind =>
|
||||
person.items.filter(item => item.kind === kind).reduce((sum, item) => sum + item.amount, 0) ===
|
||||
person.items.filter(item => item.kind === kind).reduce((sum, item) => sum + (item.amount ?? 0), 0) ===
|
||||
person[kind],
|
||||
),
|
||||
) ||
|
||||
!['receipt', 'refund', 'net'].every(
|
||||
key => recipients.reduce((sum, person) => sum + person[key], 0) === r.receiptsByOrderDate[key],
|
||||
!(
|
||||
/** @type {const} */ (['receipt', 'refund', 'net']).every(
|
||||
key => recipients.reduce((sum, person) => sum + person[key], 0) === r.receiptsByOrderDate[key],
|
||||
)
|
||||
)
|
||||
)
|
||||
throw new Error('收款人汇总与明细未完整返回或金额不一致,请重新读取。');
|
||||
@@ -129,7 +171,12 @@
|
||||
validateDailyProfits(r);
|
||||
}
|
||||
|
||||
function validateDailyFunds(data, range) {
|
||||
/**
|
||||
* @param {unknown} input
|
||||
* @param {Range} range
|
||||
*/
|
||||
function validateDailyFunds(input, range) {
|
||||
const data = /** @type {DailyFunds} */ (input);
|
||||
if (data?.team) {
|
||||
if (data.date !== range.date || data.from !== range.from || !Array.isArray(data.reimbursements))
|
||||
throw new Error('团队日期或报销资料未完整返回,请重新读取。');
|
||||
@@ -137,15 +184,15 @@
|
||||
data.reimbursements.forEach(validateReimbursement);
|
||||
return data;
|
||||
}
|
||||
const money = value => Number.isSafeInteger(value),
|
||||
nullable = value => value === null || money(value);
|
||||
const money = integer,
|
||||
nullable = (/** @type {unknown} */ value) => value === null || money(value);
|
||||
if (
|
||||
!data ||
|
||||
data.date !== range.date ||
|
||||
data.from !== range.from ||
|
||||
typeof data.readOnly !== 'boolean' ||
|
||||
typeof data.ready !== 'boolean' ||
|
||||
!['receipt', 'refund', 'net'].every(key => money(data.ownerReceipts?.[key])) ||
|
||||
!(/** @type {const} */ (['receipt', 'refund', 'net']).every(key => money(data.ownerReceipts?.[key]))) ||
|
||||
data.ownerReceipts.net !== data.ownerReceipts.receipt - data.ownerReceipts.refund ||
|
||||
!money(data.approvedReimbursements) ||
|
||||
!nullable(data.actualOutlay) ||
|
||||
@@ -169,7 +216,7 @@
|
||||
item.payer === 'owner' &&
|
||||
typeof item.name === 'string',
|
||||
) ||
|
||||
data.sources.items.reduce((sum, item) => sum + item.amount, 0) !== data.sources.total
|
||||
data.sources.items.reduce((sum, item) => sum + (item.amount ?? 0), 0) !== data.sources.total
|
||||
)
|
||||
throw new Error('转账核对资料未完整返回,请重新读取。');
|
||||
data.reimbursements.forEach(validateReimbursement);
|
||||
@@ -194,7 +241,9 @@
|
||||
return data;
|
||||
}
|
||||
|
||||
function validateDailyReimbursements(data) {
|
||||
/** @param {unknown} input */
|
||||
function validateDailyReimbursements(input) {
|
||||
const data = /** @type {ReimbursementList} */ (input);
|
||||
if (
|
||||
!data ||
|
||||
!Array.isArray(data.requests) ||
|
||||
@@ -202,13 +251,18 @@
|
||||
!Number.isSafeInteger(data.offset) ||
|
||||
!Number.isSafeInteger(data.limit) ||
|
||||
typeof data.hasMore !== 'boolean' ||
|
||||
!['pending', 'approved', 'rejected'].every(key => Number.isSafeInteger(data.counts?.[key]))
|
||||
!(
|
||||
/** @type {const} */ (['pending', 'approved', 'rejected']).every(key =>
|
||||
Number.isSafeInteger(data.counts?.[key]),
|
||||
)
|
||||
)
|
||||
)
|
||||
throw new Error('全日期报销列表未完整返回,请重新读取。');
|
||||
data.requests.forEach(validateReimbursement);
|
||||
return data;
|
||||
}
|
||||
|
||||
/** @param {Report} r */
|
||||
function validateOwnershipReceipts(r) {
|
||||
const fail = () => {
|
||||
throw new Error('所属订单净收入与账户明细未完整返回或金额不一致,请重新读取。');
|
||||
@@ -225,9 +279,10 @@
|
||||
!expected.every(key => keys.includes(key))
|
||||
)
|
||||
fail();
|
||||
const receipts = [],
|
||||
const receipts = /** @type {OwnershipReceipts[]} */ ([]),
|
||||
owned = [],
|
||||
sum = (items, kind) => items.filter(item => item.kind === kind).reduce((total, item) => total + item.amount, 0);
|
||||
sum = (/** @type {ReceiptItem[]} */ items, /** @type {string} */ kind) =>
|
||||
items.filter(item => item.kind === kind).reduce((total, item) => total + (item.amount ?? 0), 0);
|
||||
for (const key of keys) {
|
||||
const person = r.ownershipReceipts[key];
|
||||
if (
|
||||
@@ -237,7 +292,7 @@
|
||||
!person.name ||
|
||||
!Array.isArray(person.accounts) ||
|
||||
!Array.isArray(person.items) ||
|
||||
!['receipt', 'refund', 'net'].every(name => Number.isSafeInteger(person[name])) ||
|
||||
!(/** @type {const} */ (['receipt', 'refund', 'net']).every(name => Number.isSafeInteger(person[name]))) ||
|
||||
person.net !== person.receipt - person.refund
|
||||
)
|
||||
fail();
|
||||
@@ -250,10 +305,10 @@
|
||||
typeof item.account === 'string' &&
|
||||
validOrderDate(item.date),
|
||||
) ||
|
||||
!['receipt', 'refund'].every(kind => sum(person.items, kind) === person[kind])
|
||||
!(/** @type {const} */ (['receipt', 'refund']).every(kind => sum(person.items, kind) === person[kind]))
|
||||
)
|
||||
fail();
|
||||
const accounts = new Set();
|
||||
const accounts = /** @type {Set<string>} */ (new Set());
|
||||
for (const account of person.accounts) {
|
||||
if (
|
||||
!account ||
|
||||
@@ -262,7 +317,11 @@
|
||||
accounts.has(account.account) ||
|
||||
!teamMemberKeys().includes(account.recipient) ||
|
||||
!['wechat', 'alipay', 'cash', 'credit_card'].includes(account.channel) ||
|
||||
!['receipt', 'refund', 'net', 'entryCount'].every(name => Number.isSafeInteger(account[name])) ||
|
||||
!(
|
||||
/** @type {const} */ (['receipt', 'refund', 'net', 'entryCount']).every(name =>
|
||||
Number.isSafeInteger(account[name]),
|
||||
)
|
||||
) ||
|
||||
account.entryCount < 1 ||
|
||||
account.net !== account.receipt - account.refund
|
||||
)
|
||||
@@ -270,15 +329,17 @@
|
||||
const items = person.items.filter(item => item.account === account.account);
|
||||
if (
|
||||
items.length !== account.entryCount ||
|
||||
!['receipt', 'refund'].every(kind => sum(items, kind) === account[kind])
|
||||
!(/** @type {const} */ (['receipt', 'refund']).every(kind => sum(items, kind) === account[kind]))
|
||||
)
|
||||
fail();
|
||||
accounts.add(account.account);
|
||||
}
|
||||
if (
|
||||
person.items.some(item => !accounts.has(item.account)) ||
|
||||
!['receipt', 'refund', 'net'].every(
|
||||
name => person.accounts.reduce((total, account) => total + account[name], 0) === person[name],
|
||||
!(
|
||||
/** @type {const} */ (['receipt', 'refund', 'net']).every(
|
||||
name => person.accounts.reduce((total, account) => total + account[name], 0) === person[name],
|
||||
)
|
||||
)
|
||||
)
|
||||
fail();
|
||||
@@ -286,12 +347,14 @@
|
||||
owned.push(...person.items);
|
||||
}
|
||||
if (
|
||||
!['receipt', 'refund', 'net'].every(
|
||||
name => receipts.reduce((total, person) => total + person[name], 0) === r.receiptsByOrderDate[name],
|
||||
!(
|
||||
/** @type {const} */ (['receipt', 'refund', 'net']).every(
|
||||
name => receipts.reduce((total, person) => total + person[name], 0) === r.receiptsByOrderDate[name],
|
||||
)
|
||||
)
|
||||
)
|
||||
fail();
|
||||
const identity = item =>
|
||||
const identity = (/** @type {ReceiptItem} */ item) =>
|
||||
JSON.stringify([item.id, item.kind, item.amount, item.account, item.date, item.order_id, item.price_lock_id]),
|
||||
actual = teamDataKeys(r.recipientReceipts)
|
||||
.flatMap(key => r.recipientReceipts[key].items)
|
||||
@@ -301,13 +364,14 @@
|
||||
if (actual.length !== copies.length || actual.some((value, index) => value !== copies[index])) fail();
|
||||
}
|
||||
|
||||
/** @param {Report} r */
|
||||
function validateOwnershipBusiness(r) {
|
||||
const fail = () => {
|
||||
throw new Error('所属订单成本、利润或待转资料未完整返回,请重新读取。');
|
||||
},
|
||||
money = value => Number.isSafeInteger(value),
|
||||
nullable = value => value === null || money(value),
|
||||
count = value => money(value) && value >= 0,
|
||||
money = integer,
|
||||
nullable = (/** @type {unknown} */ value) => value === null || money(value),
|
||||
count = (/** @type {unknown} */ value) => money(value) && value >= 0,
|
||||
keys = teamDataKeys(r.ownershipReceipts);
|
||||
if (
|
||||
!r.ownershipBusiness ||
|
||||
@@ -328,19 +392,28 @@
|
||||
b.from !== r.from ||
|
||||
b.date !== r.date ||
|
||||
b.netReceipts !== r.ownershipReceipts[member].net ||
|
||||
!['sales', 'netReceipts', 'knownTotalCost', 'knownSelfCost', 'knownOtherCost', 'knownProfit'].every(key =>
|
||||
money(b[key]),
|
||||
!(
|
||||
/** @type {const} */ ([
|
||||
'sales',
|
||||
'netReceipts',
|
||||
'knownTotalCost',
|
||||
'knownSelfCost',
|
||||
'knownOtherCost',
|
||||
'knownProfit',
|
||||
]).every(key => money(b[key]))
|
||||
) ||
|
||||
!(/** @type {const} */ (['totalCost', 'selfCost', 'otherCost', 'profit']).every(key => nullable(b[key]))) ||
|
||||
!(
|
||||
/** @type {const} */ ([
|
||||
'missingCostCount',
|
||||
'pendingCostCount',
|
||||
'unknownProfitCount',
|
||||
'draftCount',
|
||||
'recordCount',
|
||||
'orderCount',
|
||||
'afterSaleCount',
|
||||
]).every(key => count(b[key]))
|
||||
) ||
|
||||
!['totalCost', 'selfCost', 'otherCost', 'profit'].every(key => nullable(b[key])) ||
|
||||
![
|
||||
'missingCostCount',
|
||||
'pendingCostCount',
|
||||
'unknownProfitCount',
|
||||
'draftCount',
|
||||
'recordCount',
|
||||
'orderCount',
|
||||
'afterSaleCount',
|
||||
].every(key => count(b[key])) ||
|
||||
typeof b.incomplete !== 'boolean' ||
|
||||
typeof b.hasActivity !== 'boolean'
|
||||
)
|
||||
@@ -424,7 +497,7 @@
|
||||
: settlement.dueOut !== null || settlement.dueIn !== null
|
||||
)
|
||||
fail();
|
||||
const seen = new Set();
|
||||
const seen = /** @type {Set<string>} */ (new Set());
|
||||
for (const row of settlement.counterparts) {
|
||||
if (
|
||||
!row ||
|
||||
@@ -433,8 +506,12 @@
|
||||
seen.has(row.member) ||
|
||||
typeof row.name !== 'string' ||
|
||||
!row.name ||
|
||||
!['dueOut', 'dueIn', 'offset'].every(key => count(row[key])) ||
|
||||
!['openingDue', 'periodNew', 'periodTransferred', 'closingDue'].every(key => money(row[key])) ||
|
||||
!(/** @type {const} */ (['dueOut', 'dueIn', 'offset']).every(key => count(row[key]))) ||
|
||||
!(
|
||||
/** @type {const} */ (['openingDue', 'periodNew', 'periodTransferred', 'closingDue']).every(key =>
|
||||
money(row[key]),
|
||||
)
|
||||
) ||
|
||||
row.openingDue + row.periodNew - row.periodTransferred !== row.closingDue ||
|
||||
row.dueOut !== Math.max(0, row.closingDue) ||
|
||||
row.dueIn !== Math.max(0, -row.closingDue) ||
|
||||
@@ -450,7 +527,7 @@
|
||||
}
|
||||
if (
|
||||
settlement.available &&
|
||||
['dueOut', 'dueIn'].some(
|
||||
/** @type {const} */ (['dueOut', 'dueIn']).some(
|
||||
key => settlement.counterparts.reduce((total, row) => total + row[key], 0) !== settlement[key],
|
||||
)
|
||||
)
|
||||
@@ -458,25 +535,26 @@
|
||||
}
|
||||
}
|
||||
|
||||
/** @param {Report} r */
|
||||
function validateDailyProfits(r) {
|
||||
const fail = () => {
|
||||
throw new Error('每日利润表未完整返回或与当前对账金额不一致,请重新读取。');
|
||||
},
|
||||
table = r.dailyProfits,
|
||||
money = Number.isSafeInteger,
|
||||
count = value => money(value) && value >= 0,
|
||||
nullable = value => value === null || money(value),
|
||||
money = integer,
|
||||
count = (/** @type {unknown} */ value) => money(value) && value >= 0,
|
||||
nullable = (/** @type {unknown} */ value) => value === null || money(value),
|
||||
members = teamDataKeys(r.ownershipReceipts);
|
||||
const amounts = ['sales', 'knownTotalCost', 'knownProfit'],
|
||||
counts = [
|
||||
const amounts = /** @type {const} */ (['sales', 'knownTotalCost', 'knownProfit']),
|
||||
counts = /** @type {const} */ ([
|
||||
'recordCount',
|
||||
'orderCount',
|
||||
'afterSaleCount',
|
||||
'draftCount',
|
||||
'unknownCostCount',
|
||||
'unknownProfitCount',
|
||||
];
|
||||
const valid = row =>
|
||||
]);
|
||||
const valid = (/** @type {Profit} */ row) =>
|
||||
row &&
|
||||
amounts.every(key => money(row[key])) &&
|
||||
counts.every(key => count(row[key])) &&
|
||||
@@ -534,8 +612,9 @@
|
||||
fail();
|
||||
}
|
||||
|
||||
/** @param {Team} report */
|
||||
function validateTeamFunds(report) {
|
||||
const money = value => Number.isSafeInteger(value);
|
||||
const money = integer;
|
||||
if (
|
||||
!report ||
|
||||
!Array.isArray(report.pairs) ||
|
||||
@@ -553,7 +632,7 @@
|
||||
pair.members.length !== 2 ||
|
||||
pair.members[0] === pair.members[1] ||
|
||||
!pair.members.every(member => teamMemberKeys().includes(member)) ||
|
||||
!['net', 'obligationNet', 'transferred', 'due'].every(key => money(pair[key])) ||
|
||||
!(/** @type {const} */ (['net', 'obligationNet', 'transferred', 'due']).every(key => money(pair[key]))) ||
|
||||
pair.due < 0 ||
|
||||
Math.abs(pair.net) !== pair.due ||
|
||||
!Array.isArray(pair.entries)
|
||||
@@ -565,6 +644,7 @@
|
||||
return report;
|
||||
}
|
||||
|
||||
/** @param {Reimbursement} row */
|
||||
function validateReimbursement(row) {
|
||||
if (
|
||||
!row ||
|
||||
@@ -583,18 +663,24 @@
|
||||
item => typeof item.name === 'string' && item.name && Number.isSafeInteger(item.amount) && item.amount > 0,
|
||||
) ||
|
||||
!Number.isSafeInteger(row.total) ||
|
||||
row.items.reduce((sum, item) => sum + item.amount, 0) !== row.total
|
||||
row.items.reduce((sum, item) => sum + (item.amount ?? 0), 0) !== row.total
|
||||
)
|
||||
throw new Error('报销单未完整返回,请重新读取。');
|
||||
return row;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Report} r
|
||||
* @param {Business} b
|
||||
* @param {() => never} fail
|
||||
* @returns {RecoveryCosts}
|
||||
*/
|
||||
function reportAfterSaleRecoveryCosts(r, b, fail) {
|
||||
const money = Number.isSafeInteger,
|
||||
count = value => money(value) && value >= 0,
|
||||
const money = integer,
|
||||
count = (/** @type {unknown} */ value) => money(value) && value >= 0,
|
||||
items = [],
|
||||
byPayer = Object.fromEntries(teamMemberKeys().map(key => [key, 0]));
|
||||
const identity = item =>
|
||||
const identity = (/** @type {Pick<CostItem,'component'|'payer'|'amount'|'source'|'materialPoolId'>} */ item) =>
|
||||
JSON.stringify([item.component, item.payer, item.amount, item.source, item.materialPoolId ?? null]);
|
||||
for (const row of r.orderRows.filter(
|
||||
row => row.kind === 'orderAfterSale' && (row.member ?? (teamMode() ? null : 'assistant')) === b.member,
|
||||
@@ -620,10 +706,10 @@
|
||||
fail();
|
||||
for (const line of summary.lines) {
|
||||
if (!line || !count(line.lineIndex) || typeof line.name !== 'string') fail();
|
||||
for (const [component, key, totalKey] of [
|
||||
for (const [component, key, totalKey] of /** @type {const} */ ([
|
||||
['material', 'materialAllocations', 'materialReturnedCost'],
|
||||
['labor', 'restockedLaborAllocations', 'laborRestockedCost'],
|
||||
]) {
|
||||
])) {
|
||||
const parts = line[key];
|
||||
if (
|
||||
!Array.isArray(parts) ||
|
||||
@@ -644,7 +730,7 @@
|
||||
}
|
||||
}
|
||||
if (
|
||||
['materialReturnedCost', 'laborRestockedCost'].some(
|
||||
/** @type {const} */ (['materialReturnedCost', 'laborRestockedCost']).some(
|
||||
key => summary.lines.reduce((sum, line) => sum + line[key], 0) !== summary[key],
|
||||
)
|
||||
)
|
||||
@@ -667,7 +753,7 @@
|
||||
item.afterSaleExpense !== true ||
|
||||
item.amount < 0),
|
||||
) ||
|
||||
row.costItems.reduce((sum, item) => sum + item.amount, 0) !== row.known?.totalCost ||
|
||||
row.costItems.reduce((sum, item) => sum + (item.amount ?? 0), 0) !== row.known?.totalCost ||
|
||||
row.totalCost !== row.known.totalCost ||
|
||||
!money(original?.total) ||
|
||||
!money(summary.retainedAmount) ||
|
||||
@@ -678,7 +764,7 @@
|
||||
)
|
||||
fail();
|
||||
for (const item of recovered) {
|
||||
byPayer[item.payer] += item.amount;
|
||||
byPayer[item.payer] += item.amount ?? 0;
|
||||
items.push({
|
||||
...item,
|
||||
id: row.id,
|
||||
@@ -692,14 +778,21 @@
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Business} b
|
||||
* @param {RecoveryCosts} recoveries
|
||||
* @param {() => never} fail
|
||||
*/
|
||||
function validatePoolMaterialCosts(b, recoveries, fail) {
|
||||
const money = Number.isSafeInteger,
|
||||
count = value => money(value) && value >= 0,
|
||||
nullable = value => value === null || count(value),
|
||||
const money = integer,
|
||||
count = (/** @type {unknown} */ value) => money(value) && value >= 0,
|
||||
nullable = (/** @type {unknown} */ value) => value === null || count(value),
|
||||
pool = b.poolMaterial;
|
||||
const valid = value =>
|
||||
const valid = (
|
||||
/** @type {Pick<import('../web/src/pages/Report/contracts').PoolMaterial,'mg'|'amount'|'knownAmount'|'missingCount'|'byPayer'>} */ value,
|
||||
) =>
|
||||
value &&
|
||||
['mg', 'knownAmount', 'missingCount'].every(key => count(value[key])) &&
|
||||
/** @type {const} */ (['mg', 'knownAmount', 'missingCount']).every(key => count(value[key])) &&
|
||||
nullable(value.amount) &&
|
||||
value.amount === (value.missingCount ? null : value.knownAmount) &&
|
||||
value.byPayer &&
|
||||
@@ -725,7 +818,7 @@
|
||||
(pool.otherCosts.amount !== null && pool.otherCosts.amount !== pool.otherCosts.knownAmount)
|
||||
)
|
||||
fail();
|
||||
const seen = new Set();
|
||||
const seen = /** @type {Set<string>} */ (new Set());
|
||||
for (const day of pool.days) {
|
||||
if (
|
||||
!valid(day) ||
|
||||
@@ -769,7 +862,7 @@
|
||||
seen.add(key);
|
||||
}
|
||||
if (
|
||||
['mg', 'knownAmount', 'missingCount'].some(
|
||||
/** @type {const} */ (['mg', 'knownAmount', 'missingCount']).some(
|
||||
key => pool.days.reduce((sum, day) => sum + day[key], 0) !== pool[key],
|
||||
) ||
|
||||
teamMemberKeys().some(
|
||||
@@ -787,12 +880,19 @@
|
||||
fail();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Business} b
|
||||
* @param {RecoveryCosts} recoveries
|
||||
* @param {() => never} fail
|
||||
*/
|
||||
function validateOwnerAdvances(b, recoveries, fail) {
|
||||
const money = Number.isSafeInteger,
|
||||
count = value => money(value) && value >= 0,
|
||||
nullable = value => value === null || money(value),
|
||||
const money = integer,
|
||||
count = (/** @type {unknown} */ value) => money(value) && value >= 0,
|
||||
nullable = (/** @type {unknown} */ value) => value === null || money(value),
|
||||
advances = b.ownerAdvances;
|
||||
const identity = item =>
|
||||
const identity = (
|
||||
/** @type {{id:string; date:string; source:string; component:string; amount:number|null;poolId?:string|null;materialPoolId?:string|null}} */ item,
|
||||
) =>
|
||||
JSON.stringify([
|
||||
item.id,
|
||||
item.date,
|
||||
@@ -822,9 +922,11 @@
|
||||
!/^\d{4}-\d{2}-\d{2}$/.test(day.date) ||
|
||||
!Array.isArray(day.items) ||
|
||||
!day.items.length ||
|
||||
!['knownAmount', 'knownPoolAmount', 'knownOtherAmount'].every(key => money(day[key])) ||
|
||||
!['missingCount', 'pendingCount'].every(key => count(day[key])) ||
|
||||
!['amount', 'poolAmount', 'otherAmount'].every(key => nullable(day[key]))
|
||||
!(
|
||||
/** @type {const} */ (['knownAmount', 'knownPoolAmount', 'knownOtherAmount']).every(key => money(day[key]))
|
||||
) ||
|
||||
!(/** @type {const} */ (['missingCount', 'pendingCount']).every(key => count(day[key]))) ||
|
||||
!(/** @type {const} */ (['amount', 'poolAmount', 'otherAmount']).every(key => nullable(day[key])))
|
||||
)
|
||||
fail();
|
||||
for (const item of day.items) {
|
||||
@@ -832,8 +934,10 @@
|
||||
!item ||
|
||||
item.date !== day.date ||
|
||||
!['order', 'afterSale'].includes(item.kind) ||
|
||||
!['id', 'number', 'paperNumber', 'customer', 'name', 'source', 'component'].every(
|
||||
key => typeof item[key] === 'string',
|
||||
!(
|
||||
/** @type {const} */ (['id', 'number', 'paperNumber', 'customer', 'name', 'source', 'component']).every(
|
||||
key => typeof item[key] === 'string',
|
||||
)
|
||||
) ||
|
||||
!item.id ||
|
||||
!item.name ||
|
||||
@@ -856,13 +960,13 @@
|
||||
(item.allocationPending && (!item.poolMaterial || item.status !== 'missing')))
|
||||
)
|
||||
fail();
|
||||
if (item.amount < 0 || item.nonCash === true) {
|
||||
if ((item.amount ?? 0) < 0 || item.nonCash === true) {
|
||||
const index = remaining.indexOf(identity(item));
|
||||
if (
|
||||
item.kind !== 'order' ||
|
||||
item.status !== 'recorded' ||
|
||||
item.nonCash !== true ||
|
||||
item.amount >= 0 ||
|
||||
(item.amount ?? 0) >= 0 ||
|
||||
index < 0
|
||||
)
|
||||
fail();
|
||||
@@ -871,7 +975,7 @@
|
||||
}
|
||||
const recorded = day.items.filter(item => item.status === 'recorded'),
|
||||
missing = day.items.filter(item => item.status === 'missing'),
|
||||
total = rows => rows.reduce((sum, item) => sum + item.amount, 0),
|
||||
total = (/** @type {AdvanceItem[]} */ rows) => rows.reduce((sum, item) => sum + (item.amount ?? 0), 0),
|
||||
poolMissing = missing.some(item => item.poolMaterial),
|
||||
otherMissing = missing.some(item => !item.poolMaterial);
|
||||
if (
|
||||
@@ -891,11 +995,16 @@
|
||||
if (remaining.length || knownTotal > b.costByPayer.owner.knownTotalCost) fail();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Business} b
|
||||
* @param {OwnershipReceipts} income
|
||||
* @param {() => never} fail
|
||||
*/
|
||||
function validateCurrentAccount(b, income, fail) {
|
||||
const account = b.currentAccount,
|
||||
money = Number.isSafeInteger,
|
||||
count = value => money(value) && value >= 0,
|
||||
nullable = value => value === null || money(value);
|
||||
money = integer,
|
||||
count = (/** @type {unknown} */ value) => money(value) && value >= 0,
|
||||
nullable = (/** @type {unknown} */ value) => value === null || money(value);
|
||||
if (
|
||||
!account ||
|
||||
account.basis !== 'orderDate' ||
|
||||
@@ -905,14 +1014,14 @@
|
||||
)
|
||||
fail();
|
||||
const ownerAccounts = new Set(income.accounts.filter(row => row.recipient === 'owner').map(row => row.account)),
|
||||
received = new Map();
|
||||
received = /** @type {Map<string,number>} */ (new Map());
|
||||
for (const item of income.items) {
|
||||
const date = item.price_lock_id || item.order_after_sale ? item.date : item.order_date;
|
||||
if (!date || date < b.from || date > b.date) continue;
|
||||
if (ownerAccounts.has(item.account))
|
||||
received.set(date, (received.get(date) || 0) + (item.kind === 'receipt' ? item.amount : -item.amount));
|
||||
}
|
||||
const seen = new Set();
|
||||
const seen = /** @type {Set<string>} */ (new Set());
|
||||
let previous = '';
|
||||
for (const day of account.days) {
|
||||
if (
|
||||
@@ -921,13 +1030,23 @@
|
||||
day.date < b.from ||
|
||||
day.date > b.date ||
|
||||
day.date <= previous ||
|
||||
!['recordCount', 'missingCostCount', 'pendingCostCount', 'unknownProfitCount'].every(key =>
|
||||
count(day[key]),
|
||||
!(
|
||||
/** @type {const} */ (['recordCount', 'missingCostCount', 'pendingCostCount', 'unknownProfitCount']).every(
|
||||
key => count(day[key]),
|
||||
)
|
||||
) ||
|
||||
!['knownMaterialCost', 'knownOtherCost', 'knownProfit', 'ownerNetReceipts', 'knownAmount'].every(key =>
|
||||
money(day[key]),
|
||||
!(
|
||||
/** @type {const} */ ([
|
||||
'knownMaterialCost',
|
||||
'knownOtherCost',
|
||||
'knownProfit',
|
||||
'ownerNetReceipts',
|
||||
'knownAmount',
|
||||
]).every(key => money(day[key]))
|
||||
) ||
|
||||
!(
|
||||
/** @type {const} */ (['materialCost', 'otherCost', 'profit', 'amount']).every(key => nullable(day[key]))
|
||||
) ||
|
||||
!['materialCost', 'otherCost', 'profit', 'amount'].every(key => nullable(day[key])) ||
|
||||
typeof day.incomplete !== 'boolean'
|
||||
)
|
||||
fail();
|
||||
@@ -944,9 +1063,9 @@
|
||||
const advance = b.ownerAdvances.days.find(row => row.date === day.date),
|
||||
items = advance?.items || [],
|
||||
recorded = items.filter(item => item.status === 'recorded');
|
||||
const material = rows => rows.filter(item => item.component === 'material'),
|
||||
other = rows => rows.filter(item => item.component !== 'material'),
|
||||
sum = rows => rows.reduce((total, item) => total + item.amount, 0),
|
||||
const material = (/** @type {AdvanceItem[]} */ rows) => rows.filter(item => item.component === 'material'),
|
||||
other = (/** @type {AdvanceItem[]} */ rows) => rows.filter(item => item.component !== 'material'),
|
||||
sum = (/** @type {AdvanceItem[]} */ rows) => rows.reduce((total, item) => total + (item.amount ?? 0), 0),
|
||||
missing = items.filter(item => item.status === 'missing');
|
||||
if (
|
||||
day.knownMaterialCost !== sum(material(recorded)) ||
|
||||
@@ -969,6 +1088,7 @@
|
||||
)
|
||||
fail();
|
||||
}
|
||||
/** @param {Report} r */
|
||||
function reportActiveAccounts(r) {
|
||||
const recorded = new Set(
|
||||
teamDataKeys(r.recipientReceipts).flatMap(key => r.recipientReceipts[key].items.map(item => item.account)),
|
||||
@@ -983,6 +1103,7 @@
|
||||
);
|
||||
}
|
||||
|
||||
/** @param {Report} r */
|
||||
function reportOwnershipKeys(r) {
|
||||
const keys = teamDataKeys(r.ownershipReceipts);
|
||||
return state.user.role === 'owner' && !reportMember
|
||||
@@ -1018,5 +1139,5 @@
|
||||
reimbursementAmounts,
|
||||
};
|
||||
if (typeof module === 'object' && module.exports) module.exports = api;
|
||||
else root.ReportRules = api;
|
||||
else Object.assign(root, { ReportRules: api });
|
||||
})(typeof globalThis === 'object' ? globalThis : this);
|
||||
Reference in new issue
Block a user