All files / src/modules/implementations/accounting sale-proceeds-validator.js

100% Statements 71/71
96.42% Branches 54/56
100% Functions 4/4
100% Lines 71/71

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 7263x 63x 798x 798x 798x 798x 1005x 1005x 1005x 1005x 1005x 1005x 1993x 1993x 1993x 1005x 1005x 1005x 2048x 2048x 2048x 2048x 2048x 2048x 1022x 1022x 2044x 1022x 2044x 1022x 1022x 1022x 1022x 9x 1005x 798x 14904x 14904x 14904x 14904x 34260x 34260x 34260x 34190x 34190x 34190x 34190x 31108x 31108x 31103x 31103x 31103x 34190x 34190x 34190x 3043x 3043x 3043x 34154x 14866x 14900x 1005x 1005x 14898x 9x 9x 9x 9x 14904x 798x 63x  
'use strict';
module.exports = ({ numeric: { rational: { zero, parse, add, sub, neg, cmp, div, format }, }, commodityMovements: { annotatedTotal }, journal: { ingestionWarning: { createWarning, warningCodes }, }, }) => {
    // Account names do not identify settlement, fees, or realized results. Check
    // whether any partition of the monetary postings can explain the trade value.
    // A bounded search may be inconclusive; it must never manufacture a warning.
    function canExplainSettlement(amounts, target, tolerance) {
        const absolute = (value) => value.n < 0n ? neg(value) : value;
        const ordered = amounts.filter((value) => value.n)
            .sort((left, right) => cmp(absolute(right), absolute(left)));
        const lower = [zero];
        const upper = [zero];
        for (let index = ordered.length - 1; index >= 0; index -= 1) {
            lower.unshift(add(lower[0], ordered[index].n < 0n ? ordered[index] : zero));
            upper.unshift(add(upper[0], ordered[index].n > 0n ? ordered[index] : zero));
        }
        const pending = [{ index: 0, remaining: target }];
        const visited = new Set();
        while (pending.length) {
            const { index, remaining } = pending.pop();
            if (cmp(absolute(remaining), tolerance) <= 0)
                return true;
            if (index === ordered.length || cmp(remaining, sub(lower[index], tolerance)) < 0 ||
                cmp(remaining, add(upper[index], tolerance)) > 0)
                continue;
            const key = `${index}:${format(remaining)}`;
            if (visited.has(key))
                continue;
            if (visited.size >= 100000)
                return null;
            visited.add(key);
            pending.push({ index: index + 1, remaining });
            pending.push({ index: index + 1, remaining: sub(remaining, ordered[index]) });
        }
        return false;
    }
    function validateSaleProceeds(transaction, resolved, valuationCommodity, carried, roundingUnit, warnings) {
        let tradeValue = zero;
        let hasSale = false;
        const monetaryAmounts = [];
        for (const [index, posting] of transaction.postings.entries()) {
            if (carried.has(posting))
                continue;
            for (const amount of resolved[index]) {
                const quantity = parse(amount.quantity);
                if (!quantity.n)
                    continue;
                if (amount.commodity === valuationCommodity) {
                    if (posting.cost || posting.lotCost)
                        return;
                    monetaryAmounts.push(quantity);
                    continue;
                }
                const annotation = quantity.n < 0n ? posting.cost : posting.lotCost;
                if (annotation?.amount.commodity !== valuationCommodity)
                    return;
                tradeValue = add(tradeValue, annotatedTotal(annotation, quantity));
                hasSale ||= quantity.n < 0n;
            }
        }
        if (!hasSale)
            return;
        const settlement = neg(tradeValue);
        const tolerance = div(roundingUnit, parse('2'));
        if (canExplainSettlement(monetaryAmounts, settlement, tolerance) === false) {
            warnings.push(createWarning(warningCodes.SALE_PROCEEDS_MISMATCH, `${transaction.description}: recorded trade prices require settlement of ` +
                `${format(settlement)} ${valuationCommodity}, which cannot be explained by ` +
                'the monetary postings, including separately posted fees', transaction.location));
        }
    }
    return { validateSaleProceeds };
};