All files / src/modules/implementations/accounting effective-posting-costs.js

97.03% Statements 131/135
95.57% Branches 108/113
100% Functions 8/8
97.03% Lines 131/135

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 13663x 63x 798x 798x 798x 798x 2000x 2000x 2000x 2000x 1963x 1963x 37x 2000x 2000x 798x 4008x 4008x 4008x 798x 391x 391x 361x 361x 3234x 3234x 3234x 3234x 361x 361x 361x 390x 14906x 34303x 34303x 34303x 13x 13x 13x 34303x 34303x 1982x 1x 1x 2x 2x 2x 2x 1x 1x 2x 34303x 14906x 14906x 14906x 14906x 44x 44x 44x 44x 3x 44x 3x 3x 44x 11x 2x 2x 9x 1x 1x 11x     11x     11x 44x 14906x 2005x 2005x 2005x 2005x 1995x 1995x 2005x 14906x 14906x 4x 4x 4x 2x 2x 2x 4x 14906x 14906x 34x 34x 34x 34x 17x 17x 17x 34x 17x 34x 14906x 14906x 14906x 34303x 34303x 34303x 3153x 34303x 34303x 34303x 37x 37x 37x 3116x 34283x 3082x 3082x 34270x 1044x 1044x 1044x 1044x 34303x 14906x 391x 798x 63x  
'use strict';
module.exports = ({ numeric: { rational: { zero, parse, neg, div, mul, cmp, format }, }, commodityMovements: { annotatedTotal, carriedMovements, unpricedReplacement }, allocationHistory: { AllocationHistory }, journal: { ingestionWarning: { createWarning, warningCodes }, }, }) => {
    const absolute = (value) => value.n < 0n ? neg(value) : value;
    const quantityOf = (posting) => parse(posting.amount.quantity);
    const totalOf = (posting, annotation) => annotatedTotal(annotation, quantityOf(posting));
    function annotation(total, quantity, commodity, preferTotal) {
        if (!quantity.n)
            return null;
        const unit = format(div(total, quantity));
        if (!preferTotal && !unit.includes('/')) {
            return { total: false, amount: { quantity: unit, commodity } };
        }
        const text = format(absolute(total));
        return text.includes('/') ? null : { total: true, amount: { quantity: text, commodity } };
    }
    function assign(posting, field, total, commodity, preferTotal) {
        if (!posting[field])
            posting[field] = annotation(total, quantityOf(posting), commodity, preferTotal);
    }
    function inferEffectivePostingCosts(entries, valuationCommodity, warnings) {
        if (!valuationCommodity)
            return;
        const histories = new Map();
        const historyFor = (account, commodity) => {
            const key = JSON.stringify([account, commodity]);
            if (!histories.has(key))
                histories.set(key, new AllocationHistory(zero));
            return histories.get(key);
        };
        const transactions = entries.filter((entry) => entry.type === 'transaction')
            .sort((left, right) => left.date.localeCompare(right.date));
        for (const transaction of transactions) {
            for (const posting of transaction.postings) {
                if (!posting.amount || posting.amount.commodity === valuationCommodity ||
                    quantityOf(posting).n >= 0n || posting.lotCost)
                    continue;
                const history = historyFor(posting.account, posting.amount.commodity);
                const quantity = neg(quantityOf(posting));
                const available = history.total(posting.account);
                if (history.invalid || cmp(quantity, available.quantity) > 0)
                    continue;
                if (cmp(quantity, available.quantity) === 0) {
                    assign(posting, 'lotCost', neg(available.cost), valuationCommodity);
                }
                else if (history.groups.has(posting.account)) {
                    const groups = history.holdings(posting.account)
                        .filter((group) => group.remaining.constant.n || group.remaining.coefficients.size);
                    if (groups.length && groups.every((group) => cmp(group.price, groups[0].price) === 0)) {
                        assign(posting, 'lotCost', mul(quantityOf(posting), groups[0].price), valuationCommodity);
                    }
                }
            }
            const foreign = transaction.postings.filter((posting) => posting.amount &&
                posting.amount.commodity !== valuationCommodity && quantityOf(posting).n);
            const monetary = transaction.postings.filter((posting) => posting.amount?.commodity === valuationCommodity);
            if (foreign.length === 2 && monetary.length === 0 && transaction.postings.length === 2) {
                const outgoing = foreign.find((posting) => quantityOf(posting).n < 0n);
                const incoming = foreign.find((posting) => quantityOf(posting).n > 0n);
                if (outgoing && incoming && outgoing.amount.commodity === incoming.amount.commodity &&
                    !outgoing.cost && !incoming.cost && outgoing.lotCost && !incoming.lotCost &&
                    (outgoing.account === incoming.account ||
                        cmp(quantityOf(incoming), neg(quantityOf(outgoing))) === 0)) {
                    assign(incoming, 'lotCost', neg(totalOf(outgoing, outgoing.lotCost)), valuationCommodity);
                }
                if (outgoing && incoming && outgoing.amount.commodity !== incoming.amount.commodity) {
                    if (outgoing.cost?.amount.commodity === valuationCommodity && !incoming.cost) {
                        assign(incoming, 'cost', neg(totalOf(outgoing, outgoing.cost)), valuationCommodity, outgoing.cost.total);
                    }
                    else if (incoming.cost?.amount.commodity === valuationCommodity && !outgoing.cost) {
                        assign(outgoing, 'cost', neg(totalOf(incoming, incoming.cost)), valuationCommodity, incoming.cost.total);
                    }
                    if (!outgoing.cost && !incoming.cost && outgoing.lotCost && !incoming.lotCost) {
                        assign(incoming, 'lotCost', neg(totalOf(outgoing, outgoing.lotCost)), valuationCommodity);
                    }
                    if (incoming.cost?.amount.commodity === valuationCommodity && !incoming.lotCost) {
                        assign(incoming, 'lotCost', totalOf(incoming, incoming.cost), valuationCommodity);
                    }
                }
            }
            if (foreign.length === 1 && monetary.length === 1 && transaction.postings.length === 2) {
                const posting = foreign[0];
                const settlement = neg(parse(monetary[0].amount.quantity));
                assign(posting, 'cost', settlement, valuationCommodity);
                if (quantityOf(posting).n > 0n) {
                    assign(posting, 'lotCost', settlement, valuationCommodity);
                }
            }
            const replacement = unpricedReplacement(transaction, valuationCommodity);
            if (replacement?.outgoing.lotCost && replacement.incoming.lotCost) {
                const oldBasis = neg(totalOf(replacement.outgoing, replacement.outgoing.lotCost));
                const newBasis = totalOf(replacement.incoming, replacement.incoming.lotCost);
                if (cmp(oldBasis, newBasis) !== 0) {
                    warnings.push(createWarning(warningCodes.INVALID_COMMODITY_TRADE, `An unpriced commodity replacement changes acquisition basis from ` +
                        `${format(oldBasis)} ${valuationCommodity} to ${format(newBasis)} ${valuationCommodity}`, transaction.location));
                }
            }
            const pairs = carriedMovements(transaction, valuationCommodity);
            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]));
            for (const posting of transaction.postings) {
                if (!posting.amount || posting.amount.commodity === valuationCommodity ||
                    !quantityOf(posting).n)
                    continue;
                const history = historyFor(posting.account, posting.amount.commodity);
                const basis = posting.lotCost?.amount.commodity === valuationCommodity
                    ? totalOf(posting, posting.lotCost) : null;
                if (basis === null) {
                    history.invalid = true;
                    continue;
                }
                if (incoming.has(posting))
                    continue;
                const quantity = quantityOf(posting);
                if (quantity.n > 0n)
                    history.acquire(posting.account, quantity, basis);
                else {
                    const destination = outgoing.get(posting);
                    history.dispose(posting.account, neg(quantity), neg(basis), destination?.account, destination ? quantityOf(destination) : undefined);
                }
            }
        }
    }
    return { inferEffectivePostingCosts };
};