All files / src/modules/implementations/queries/support investment-flows.js

100% Statements 85/85
98.33% Branches 59/60
100% Functions 7/7
100% Lines 85/85

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 8651x 51x 51x 783x 50x 50x 189x 189x 189x 189x 50x 50x 50x 50x 93x 93x 93x 1x 1x 92x 50x 50x 50x 79x 79x 79x 4x 4x 4x 2x 2x 2x 4x 79x 79x 189x 189x 121x 121x 189x 121x 121x 79x 108x 108x 108x 91x 91x 91x 91x 108x 3x 3x 3x 3x 3x 4x 4x 4x 1x 1x 4x 2x 2x 2x 2x 105x 89x 89x 89x 28x 28x 61x 61x 61x 61x 89x 89x 88x 78x 49x 49x 50x 783x 51x  
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
module.exports = ({ core: { publicErrors: { createError, errorCodes }, }, numeric: { rational: { zero, parse, add, mul, neg, format }, }, accounting: { commodityMovements: { annotatedTotal, carriedMovements }, }, }) => {
    function calculateFlows(postings, options, valuationCommodity) {
        const transactions = new Map();
        for (const posting of postings) {
            if (!transactions.has(posting.transactionId))
                transactions.set(posting.transactionId, []);
            transactions.get(posting.transactionId).push(posting);
        }
        const daily = new Map();
        const addFlow = (date, value) => daily.set(date, add(daily.get(date) || zero, value));
        const inPeriod = (date) => (!options.from || date >= options.from) && (!options.to || date <= options.to);
        const convert = (quantity, rate, commodity, date) => {
            if (!quantity.n)
                return zero;
            if (rate === null) {
                throw createError(errorCodes.MISSING_VALUATION_DATA, `No price for ${commodity} on or before ${date} can convert a cash flow to ${valuationCommodity}`);
            }
            return mul(quantity, parse(rate));
        };
        const marketValue = (posting) => convert(parse(posting.amount.quantity), posting.marketRate, posting.amount.commodity, posting.date);
        for (const transaction of transactions.values()) {
            const paired = new Set();
            const internal = new Set();
            for (const { outgoing, incoming } of carriedMovements({ postings: transaction }, valuationCommodity)) {
                paired.add(outgoing);
                paired.add(incoming);
                if (outgoing.selected && incoming.selected && outgoing.date === incoming.date) {
                    internal.add(outgoing);
                    internal.add(incoming);
                }
            }
            const groups = new Map();
            for (const posting of transaction) {
                if (!posting.selectedAccount || !inPeriod(posting.date))
                    continue;
                const key = JSON.stringify([posting.date, posting.account]);
                if (!groups.has(key))
                    groups.set(key, []);
                groups.get(key).push(posting);
            }
            for (const group of groups.values()) {
                const selected = group.filter((posting) => posting.selected && !internal.has(posting));
                if (!selected.length)
                    continue;
                const annotationFor = (posting) => parse(posting.amount.quantity).n < 0n
                    ? posting.cost : posting.lotCost;
                const incomplete = selected.some((posting) => posting.amount.commodity !== valuationCommodity &&
                    parse(posting.amount.quantity).n && !paired.has(posting) && !annotationFor(posting));
                if (incomplete) {
                    // Preserve the market/counterposting fallback for incomplete journals.
                    // Valid annotated trades never depend on which account holds cash.
                    let selectedValue = zero;
                    let unselectedValue = zero;
                    for (const posting of group) {
                        const value = marketValue(posting);
                        if (posting.selected)
                            selectedValue = add(selectedValue, value);
                        else
                            unselectedValue = add(unselectedValue, value);
                    }
                    if (selectedValue.n)
                        addFlow(group[0].date, unselectedValue.n ? neg(unselectedValue) : selectedValue);
                    continue;
                }
                for (const posting of selected) {
                    const annotation = annotationFor(posting);
                    let value;
                    if (posting.amount.commodity === valuationCommodity || paired.has(posting) || !annotation) {
                        value = marketValue(posting);
                    }
                    else {
                        const rate = annotation === posting.cost ? posting.costRate : posting.lotRate;
                        value = convert(annotatedTotal(annotation, parse(posting.amount.quantity)), rate, annotation.amount.commodity, posting.date);
                    }
                    addFlow(posting.date, value);
                }
            }
        }
        return [...daily].sort(([left], [right]) => left.localeCompare(right))
            .map(([date, flow]) => ({ date, flow: Number(format(flow)) }));
    }
    return { calculateFlows };
};