Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | 63x 63x 798x 361x 361x 14904x 34299x 34298x 34298x 3177x 3177x 34289x 3177x 34298x 34298x 34298x 3177x 3177x 3177x 3177x 34282x 34298x 14904x 14904x 361x 478x 478x 478x 478x 3145x 3145x 21x 21x 21x 3145x 478x 361x 798x 391x 391x 361x 361x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 3245x 3245x 3245x 3245x 391x 391x 14904x 14904x 14904x 14904x 34x 34x 34x 34x 17x 17x 17x 34x 17x 34x 14904x 14904x 14904x 34299x 34298x 34298x 34298x 3177x 3177x 34289x 478x 478x 478x 478x 478x 3177x 3177x 3177x 34298x 34298x 34298x 61x 61x 61x 61x 3116x 34298x 34298x 3082x 34265x 1044x 1044x 1044x 1044x 40x 40x 40x 40x 40x 1044x 34298x 14904x 14904x 361x 390x 478x 478x 478x 391x 40x 40x 40x 40x 40x 27x 27x 38x 40x 40x 40x 40x 391x 13x 13x 13x 13x 391x 798x 63x | 'use strict';
module.exports = ({ numeric: { rational: { zero, parse, add, neg, format }, }, commodityMovements: { annotatedTotal, carriedMovements }, allocationHistory: { AllocationHistory }, saleProceedsValidator: { validateSaleProceeds }, journal: { ingestionWarning: { createWarning, warningCodes }, }, }) => {
function validatePostingDateHoldings(resolvedTransactions, valuationCommodity, warnings) {
const positions = new Map();
for (const [transaction, resolved] of resolvedTransactions) {
transaction.postings.forEach((posting, index) => {
for (const amount of resolved[index]) {
if (amount.commodity === valuationCommodity)
continue;
const key = JSON.stringify([posting.account, amount.commodity]);
if (!positions.has(key))
positions.set(key, new Map());
const days = positions.get(key);
const date = posting.postingDate || transaction.date;
if (!days.has(date))
days.set(date, { quantity: zero });
const day = days.get(date);
const quantity = parse(amount.quantity);
day.quantity = add(day.quantity, quantity);
if (quantity.n < 0n)
day.location = posting.location;
}
});
}
for (const [key, days] of positions) {
const [account, commodity] = JSON.parse(key);
let balance = zero;
// Reports expose daily positions, so same-day movements are netted first.
for (const [date, day] of [...days].sort(([left], [right]) => left.localeCompare(right))) {
balance = add(balance, day.quantity);
if (balance.n < 0n && day.quantity.n < 0n) {
warnings.push(createWarning(warningCodes.NEGATIVE_POSTING_DATE_HOLDING, `${account}: holding is ${format(balance)} ${commodity} on ${date} ` +
'using posting dates; disposals must not precede available acquisitions', day.location));
}
}
}
}
function validateGlobalAccounting(entries, resolvedTransactions, valuationCommodity, warnings) {
if (!valuationCommodity)
return;
validatePostingDateHoldings(resolvedTransactions, valuationCommodity, warnings);
const declaration = entries.find((entry) => entry.type === 'commodity' && entry.symbol === valuationCommodity);
const declaredFormat = declaration?.properties.find(({ name }) => name === 'format')?.value;
const formatMatch = /^(?:[\d,]+)(?:\.(\d+))?[ \t]+/u.exec(declaredFormat || '');
const scale = formatMatch?.[1]?.length ?? 0;
const roundingUnit = formatMatch ? parse(scale ? `0.${'0'.repeat(scale - 1)}1` : '1') : zero;
const histories = new Map();
const positions = new Map();
const impossible = [];
const transactions = entries.filter((entry) => entry.type === 'transaction' && resolvedTransactions.has(entry))
.sort((left, right) => left.date.localeCompare(right.date));
const historyFor = (account, commodity) => {
const key = JSON.stringify([account, commodity]);
if (!histories.has(key))
histories.set(key, new AllocationHistory(roundingUnit));
return histories.get(key);
};
for (const transaction of transactions) {
const resolved = resolvedTransactions.get(transaction);
const pairs = carriedMovements(transaction, valuationCommodity);
validateSaleProceeds(transaction, resolved, valuationCommodity, new Set(pairs.flatMap(({ outgoing, incoming }) => [outgoing, incoming])), roundingUnit, warnings);
for (const pair of pairs) {
const commodity = pair.outgoing.amount.commodity;
const source = historyFor(pair.outgoing.account, commodity);
const target = historyFor(pair.incoming.account, commodity);
if (source !== target) {
source.merge(target);
for (const [key, history] of histories)
if (history === target)
histories.set(key, source);
}
}
const incoming = new Set(pairs.map((pair) => pair.incoming));
const outgoing = new Map(pairs.map((pair) => [pair.outgoing, pair.incoming]));
transaction.postings.forEach((posting, index) => {
for (const amount of resolved[index]) {
const quantity = parse(amount.quantity);
if (amount.commodity === valuationCommodity)
continue;
const history = historyFor(posting.account, amount.commodity);
const key = JSON.stringify([posting.account, amount.commodity]);
if (!positions.has(key)) {
positions.set(key, {
account: posting.account, commodity: amount.commodity,
quantity: zero, cost: zero, known: true, location: posting.location,
});
}
const position = positions.get(key);
position.quantity = add(position.quantity, quantity);
position.location = posting.location;
const cost = posting.lotCost?.amount.commodity === valuationCommodity
? annotatedTotal(posting.lotCost, quantity) : null;
if (cost === null) {
position.known = false;
history.invalid = true;
continue;
}
position.cost = add(position.cost, cost);
if (incoming.has(posting) || !quantity.n)
continue;
if (quantity.n > 0n)
history.acquire(posting.account, quantity, cost);
else {
const destination = outgoing.get(posting);
const failure = history.dispose(posting.account, neg(quantity), neg(cost), destination?.account, destination ? parse(destination.amount.quantity) : undefined);
if (failure) {
impossible.push({
transaction, posting, key, commodity: amount.commodity,
quantity: neg(quantity), cost: neg(cost), closed: !position.quantity.n, ...failure,
});
}
}
}
});
}
const residuals = new Set();
for (const [key, position] of positions) {
if (position.known && !position.quantity.n && position.cost.n)
residuals.add(key);
}
for (const failure of impossible) {
// A closed position's residual is the more direct diagnosis of the same
// failed final disposal. Keep historical violations on other positions.
if (failure.closed && residuals.has(failure.key) &&
positions.get(failure.key).location === failure.posting.location)
continue;
const { transaction, posting, commodity, quantity, cost, minimum, maximum } = failure;
const range = failure.insufficient
? 'insufficient acquired units are available'
: `allowed range is ${format(minimum)} to ${format(maximum)} ${valuationCommodity}`;
warnings.push(createWarning(warningCodes.IMPOSSIBLE_COST_BASIS, `${transaction.description}: ${posting.account} sold ${format(quantity)} ${commodity} ` +
`with cost ${format(cost)} ${valuationCommodity}; ${range}`, posting.location));
}
for (const key of residuals) {
const position = positions.get(key);
warnings.push(createWarning(warningCodes.RESIDUAL_COST_BASIS, `${position.account}: zero ${position.commodity} units retain cost basis ` +
`${format(position.cost)} ${valuationCommodity}`, position.location));
}
}
return { validateGlobalAccounting };
};
|