All files / src/modules/implementations/accounting journal-validator.js

100% Statements 188/188
98.33% Branches 118/120
100% Functions 11/11
100% Lines 188/188

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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 18963x 63x 798x 798x 404790x 404790x 206x 206x 404790x 798x 2797x 2797x 12x 12x 12x 2785x 2785x 2797x 798x 34511x 34511x 34511x 34511x 34511x 34511x 34511x 34511x 172555x 172555x 34511x 12x 12x 34511x 798x 14999x 10976x 10976x 14999x 34511x 34511x 14999x 798x 14999x 34511x 4x 4x 34511x 14999x 798x 26198x 26198x 26195x 22813x 22813x 22813x 45626x 6x 6x 6x 45626x 22813x 22813x 3110x 26198x 23x 23x 23x 26198x 20x 20x 20x 26198x 798x 34573x 34573x 34396x 34396x 34396x 34573x 68792x 68792x 68792x 68792x 68792x 68792x 68792x 19x 19x 19x 68792x 34573x 798x 34511x 3191x 3191x 3191x 16x 16x 16x 16x 3191x 34511x 34511x 34511x 34511x 798x 14944x 14944x 14944x 14944x 14944x 4x 4x 4x 14944x 34403x 34403x 34403x 34403x 34403x 111x 198x 198x 34403x 34403x 62x 62x 62x 34403x 14944x 798x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 444x 112915x 1738x 4x 4x 1738x 111177x 1000x 4x 4x 996x 78x 78x 1000x 110177x 59x 4x 4x 59x 110118x 14999x 14999x 14999x 14999x 14999x 14999x 4x 4x 4x 14999x 14999x 14999x 8301x 8301x 8301x 8301x 14999x 14999x 95119x 93368x 93368x 93368x 112915x 444x 444x 798x 63x  
'use strict';
module.exports = ({ numeric: { decimal: { compareDecimals, parseDecimal }, rational: { zero, parse: parseRational, add, cmp }, }, commodityMovements: { annotatedTotal, carriedMovements, unpricedReplacement }, journal: { ingestionWarning: { createWarning, warningCodes }, }, journalValuationCommodity: { valuationCommodityFromJournal }, }) => {
    const ZERO = parseDecimal('0');
    function warnUnlessDeclared(kind, name, declarations, location, warnings) {
        if (name === null || name === undefined || declarations.has(name))
            return;
        const label = kind[0].toUpperCase() + kind.slice(1);
        warnings.push(createWarning(warningCodes[`UNDECLARED_${kind.toUpperCase()}`], `${label} ${name} must be declared before use`, location));
    }
    function declare(kind, name, declarations, location, warnings) {
        const label = kind[0].toUpperCase() + kind.slice(1);
        if (declarations.has(name)) {
            warnings.push(createWarning(warningCodes[`DUPLICATE_${kind.toUpperCase()}_DECLARATION`], `${label} ${name} has already been declared`, location));
            return false;
        }
        declarations.add(name);
        return true;
    }
    function validatePostingDeclarations(posting, declarations, warnings) {
        warnUnlessDeclared('account', posting.account, declarations.accounts, posting.location, warnings);
        for (const amount of [
            posting.amount,
            posting.lotCost?.amount,
            posting.cost?.amount,
            posting.balanceAssignment,
            posting.balanceAssertion,
        ]) {
            warnUnlessDeclared('commodity', amount?.commodity, declarations.commodities, posting.location, warnings);
        }
        for (const tag of (posting.tags || [])) {
            warnUnlessDeclared('tag', tag.name, declarations.tags, tag.location || posting.location, warnings);
        }
    }
    function validateTransactionDeclarations(transaction, declarations, warnings) {
        for (const tag of (transaction.tags || [])) {
            warnUnlessDeclared('tag', tag.name, declarations.tags, tag.location || transaction.location, warnings);
        }
        for (const posting of transaction.postings) {
            validatePostingDeclarations(posting, declarations, warnings);
        }
    }
    function validatePostingDates(transaction, warnings) {
        for (const posting of transaction.postings) {
            if (posting.postingDate && posting.postingDate < transaction.date) {
                warnings.push(createWarning(warningCodes.POSTING_DATE_BEFORE_TRANSACTION, `Posting date ${posting.postingDate} must not precede transaction date ${transaction.date}`, posting.location));
            }
        }
    }
    function validateCommodityTrade(posting, defaultCommodity, warnings) {
        if (!posting.amount || !defaultCommodity)
            return;
        if (posting.amount.commodity === defaultCommodity) {
            for (const [label, annotation] of [
                ['lot cost', posting.lotCost], ['transaction price', posting.cost],
            ]) {
                if (annotation && annotation.amount.commodity !== defaultCommodity) {
                    warnings.push(createWarning(warningCodes.INVALID_COMMODITY_TRADE, `A ${defaultCommodity} posting cannot have a ${label} in ` +
                        `${annotation.amount.commodity}. Amounts in the default commodity are valued at face value.`, posting.location));
                }
            }
            return;
        }
        const sign = compareDecimals(parseDecimal(posting.amount.quantity), ZERO);
        if (sign > 0 && !posting.lotCost) {
            warnings.push(createWarning(warningCodes.INVALID_COMMODITY_TRADE, `Positive ${posting.amount.commodity} posting must use a lot cost ({...} or {{...}}); ` +
                `the default commodity is ${defaultCommodity}`, posting.location));
        }
        if (sign < 0 && (!posting.lotCost || !posting.cost)) {
            warnings.push(createWarning(warningCodes.INVALID_COMMODITY_TRADE, `Negative ${posting.amount.commodity} posting must use both a lot cost ({...} or {{...}}) ` +
                `and a transaction price (@ or @@); the default commodity is ${defaultCommodity}`, posting.location));
        }
    }
    function validateSelfCommodityCosts(posting, warnings) {
        if (!posting.amount?.commodity)
            return;
        const { commodity, quantity } = posting.amount;
        for (const [label, annotation] of [
            ['Lot cost', posting.lotCost], ['Transaction price', posting.cost],
        ]) {
            if (!annotation || annotation.amount.commodity !== commodity)
                continue;
            const expected = parseRational(annotation.total ? quantity : '1');
            const actual = annotation.total
                ? annotatedTotal(annotation, expected)
                : parseRational(annotation.amount.quantity);
            if (cmp(actual, expected) !== 0) {
                warnings.push(createWarning(warningCodes.INVALID_COMMODITY_TRADE, `${label} expressed in the posting commodity ${commodity} ` +
                    `must value one ${commodity} at exactly one ${commodity}`, posting.location));
            }
        }
    }
    function validatePosting(posting, defaultCommodity, warnings, carried) {
        if (posting.lotCost) {
            const costCommodity = posting.lotCost.amount.commodity;
            if (defaultCommodity && costCommodity && costCommodity !== defaultCommodity &&
                posting.amount?.commodity !== defaultCommodity) {
                warnings.push(createWarning(warningCodes.FOREIGN_LOT_COST_CURRENCY, `${posting.account}: lot cost in ${costCommodity} must be expressed in ` +
                    `the default commodity ${defaultCommodity}. Unrealized gains omit affected positions; ` +
                    'their totals may be incomplete', posting.location));
            }
        }
        validateSelfCommodityCosts(posting, warnings);
        if (!carried.has(posting))
            validateCommodityTrade(posting, defaultCommodity, warnings);
    }
    function validateResolvedCommodityTrades(transaction, resolved, defaultCommodity, warnings) {
        const postings = transaction.postings.flatMap((posting, index) => resolved[index].map((amount) => ({ ...posting, amount, original: posting })));
        const carried = new Set(carriedMovements({ ...transaction, postings }, defaultCommodity)
            .flatMap(({ outgoing, incoming }) => [outgoing, incoming]));
        const replacement = unpricedReplacement({ ...transaction, postings }, defaultCommodity);
        if (replacement) {
            carried.add(replacement.outgoing);
            carried.add(replacement.incoming);
        }
        for (const posting of postings) {
            // Explicit amounts were checked before resolution. Assignments must use
            // the actual change in holdings, not the target balance's sign.
            if (posting.original.amount && !posting.original.balanceAssignment)
                continue;
            if (!posting.original.amount && !posting.original.balanceAssignment &&
                postings.some((candidate) => candidate !== posting && candidate.original.amount &&
                    candidate.amount.commodity === posting.amount.commodity &&
                    cmp(add(parseRational(candidate.amount.quantity), parseRational(posting.amount.quantity)), zero) === 0 &&
                    !candidate.cost && !posting.cost))
                continue;
            validateSelfCommodityCosts(posting, warnings);
            if (!carried.has(posting))
                validateCommodityTrade(posting, defaultCommodity, warnings);
        }
    }
    function validateJournal(journal, defaultCommodity, initialWarnings) {
        const warnings = [...(initialWarnings || [])];
        const invalidEntries = new Set();
        const effectiveDefaultCommodity = defaultCommodity === undefined
            ? valuationCommodityFromJournal(journal, warnings)
            : defaultCommodity;
        const declarations = {
            accounts: new Set(),
            commodities: new Set(),
            tags: new Set(),
        };
        for (const entry of journal.entries) {
            if (entry.type === 'account') {
                if (!declare('account', entry.name, declarations.accounts, entry.location, warnings)) {
                    invalidEntries.add(entry);
                }
            }
            else if (entry.type === 'commodity') {
                if (!declare('commodity', entry.symbol, declarations.commodities, entry.location, warnings)) {
                    invalidEntries.add(entry);
                }
                else if (!entry.properties.some(({ name }) => name === 'format')) {
                    warnings.push(createWarning(warningCodes.MISSING_COMMODITY_FORMAT, `Commodity ${entry.symbol} must declare a format property`, entry.location));
                }
            }
            else if (entry.type === 'tag') {
                if (!declare('tag', entry.name, declarations.tags, entry.location, warnings)) {
                    invalidEntries.add(entry);
                }
            }
            else if (entry.type === 'transaction') {
                validateTransactionDeclarations(entry, declarations, warnings);
                validatePostingDates(entry, warnings);
                const carried = new Set(carriedMovements(entry, effectiveDefaultCommodity)
                    .flatMap(({ outgoing, incoming }) => [outgoing, incoming]));
                const replacement = unpricedReplacement(entry, effectiveDefaultCommodity);
                if (replacement) {
                    carried.add(replacement.outgoing);
                    carried.add(replacement.incoming);
                }
                if (entry.postings.length === 2 && entry.postings.every((posting) => posting.amount && !posting.lotCost && !posting.cost) &&
                    entry.postings[0].amount.commodity === entry.postings[1].amount.commodity &&
                    cmp(add(parseRational(entry.postings[0].amount.quantity), parseRational(entry.postings[1].amount.quantity)), zero) === 0) {
                    const outgoing = entry.postings.find((posting) => parseRational(posting.amount.quantity).n < 0n);
                    if (outgoing)
                        carried.add(outgoing);
                }
                entry.postings.forEach((posting) => validatePosting(posting, effectiveDefaultCommodity, warnings, carried));
            }
            else if (entry.type === 'price') {
                warnUnlessDeclared('commodity', entry.commodity, declarations.commodities, entry.location, warnings);
                warnUnlessDeclared('commodity', entry.price?.commodity, declarations.commodities, entry.location, warnings);
            }
        }
        return { invalidEntries, journal, warnings };
    }
    return { validateJournal, validateResolvedCommodityTrades };
};