All files / src/modules/implementations/commands/list-definitions/raw transactions.js

100% Statements 177/177
93.93% Branches 93/99
100% Functions 12/12
100% Lines 177/177

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 17840x 40x 40x 768x 17x 768x 768x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 768x 768x 17x 17x 258x 258x 258x 258x 258x 17x 31x 31x 31x 17x 273x 273x 273x 256x 256x 17x 2x 2x 2x 2x 2x 273x 273x 273x 19x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 7x 7x 7x 19x 273x 9x 9x 273x 2x 2x 273x 1x 1x 273x 273x 273x 273x 273x 17x 17x 17x 17x 17x 17x 17x 17x 17x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 130x 134x 118x 134x 134x 134x 134x 134x 134x 134x 273x 273x 273x 273x 273x 273x 2x 273x 273x 273x 273x 273x 273x 273x 273x 273x 120x 273x 153x 153x 273x 273x 273x 273x 273x 120x 120x 273x 273x 273x 273x 273x 273x 273x 273x 273x 273x 273x 134x 134x 134x 17x 17x 17x 17x 17x 17x 768x 40x  
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
module.exports = ({ numeric: { decimal: { addDecimals, compareDecimals, divideDecimals, formatDecimal, parseDecimal } } }) => ({
    loadFormatData(journal) {
        return journal.commodities({ usage: 'all' });
    },
    formatCsv(report, _cliOptions, { csvField }) {
        function transactionRows(report) {
            return report.transactions.flatMap((transaction) => transaction.postings.flatMap((posting) => posting.amounts.map((amount) => ({
                transactionId: transaction.transactionId,
                transactionDate: transaction.transactionDate,
                description: transaction.description,
                transactionComments: JSON.stringify(transaction.comments),
                transactionTags: JSON.stringify(transaction.tags || []),
                postingDate: posting.postingDate,
                account: posting.account,
                postingComments: JSON.stringify(posting.comments),
                postingTags: JSON.stringify(posting.tags || []),
                quantity: amount.quantity,
                commodity: amount.commodity,
            }))));
        }
        function formatTransactionsCsv(report) {
            const fields = [
                'transactionId', 'transactionDate', 'description', 'transactionComments',
                'transactionTags', 'postingDate', 'account', 'postingComments', 'postingTags',
                'quantity', 'commodity',
            ];
            const lines = [fields.join(',')];
            for (const row of transactionRows(report)) {
                lines.push(fields.map((field) => csvField(row[field])).join(','));
            }
            return `${lines.join('\n')}\n`;
        }
        return formatTransactionsCsv(report);
    },
    formatText(report, _cliOptions, format, descriptions) {
        const { commodityFormats, decimalScale, displayQuantity, formatPriceQuantity } = format;
        function formatPostingAmount(amount, formats) {
            const quantity = formats.has(amount.commodity)
                ? displayQuantity(amount.quantity, amount.commodity, formats, null)
                : amount.quantity;
            return `${quantity} ${amount.commodity}`;
        }
        function formatAnnotationAmount(amount, formats) {
            return `${formatPriceQuantity(amount.quantity, amount.commodity, formats)} ` +
                amount.commodity;
        }
        function formatPostingExpression(posting, formats) {
            const expressions = [];
            let amountText = null;
            if (posting.amount !== null) {
                amountText = formatPostingAmount(posting.amount, formats);
            }
            else if (posting.balanceAssignment !== null) {
                const inferredAmount = posting.amounts.find((amount) => amount.commodity === posting.balanceAssignment.commodity);
                if (inferredAmount) {
                    amountText = formatPostingAmount(inferredAmount, formats);
                }
            }
            if (amountText !== null)
                expressions.push(amountText);
            if (posting.lotCost !== null) {
                if (posting.lotCost.isTotal) {
                    const amount = parseDecimal(posting.amount.quantity);
                    const absoluteAmount = amount.coefficient < 0n
                        ? { ...amount, coefficient: -amount.coefficient }
                        : amount;
                    const unitCostScale = decimalScale(posting.lotCost.quantity) +
                        decimalScale(posting.amount.quantity) + 6;
                    const unitCost = divideDecimals(parseDecimal(posting.lotCost.quantity), absoluteAmount, unitCostScale);
                    const lotCost = {
                        quantity: formatDecimal(unitCost),
                        commodity: posting.lotCost.commodity,
                    };
                    expressions.push(`{${formatAnnotationAmount(lotCost, formats)}}`);
                }
                else {
                    expressions.push(`{${formatAnnotationAmount(posting.lotCost, formats)}}`);
                }
            }
            if (posting.cost !== null) {
                expressions.push(`${posting.cost.isTotal ? '@@' : '@'} ${formatAnnotationAmount(posting.cost, formats)}`);
            }
            if (posting.balanceAssignment !== null) {
                expressions.push(`= ${formatAnnotationAmount(posting.balanceAssignment, formats)}`);
            }
            if (posting.balanceAssertion !== null) {
                expressions.push(`= ${formatAnnotationAmount(posting.balanceAssertion, formats)}`);
            }
            return {
                text: expressions.join(' '),
                amountWidth: amountText?.length ?? 0,
            };
        }
        function formatTransactionsText(report, descriptions) {
            const formatTag = ({ name, value }) => value === null
                ? `:${name}:` : `${name}: ${value}`;
            const accountColumnWidth = 34;
            const amountColumnWidth = 12;
            const maximumPostingLineWidth = 61;
            const formats = commodityFormats(descriptions);
            const lines = [];
            for (const transaction of report.transactions) {
                const date = transaction.transactionDate;
                const header = `${date} ${transaction.description}`;
                const inlineTransactionComment = transaction.comments[0];
                const inlineHeader = inlineTransactionComment === undefined ? '' : ` ; ${inlineTransactionComment}`;
                const wrapHeaderComment = inlineTransactionComment !== undefined &&
                    `${header}${inlineHeader}`.length > 80;
                lines.push(`${header}${wrapHeaderComment ? '' : inlineHeader}`);
                if (wrapHeaderComment)
                    lines.push(`    ; ${inlineTransactionComment}`);
                for (const comment of transaction.comments.slice(1))
                    lines.push(`    ; ${comment}`);
                for (const tag of transaction.tags || [])
                    lines.push(`    ; ${formatTag(tag)}`);
                const canElideAmount = transaction.postings.length === 2 &&
                    transaction.postings.every((posting) => posting.amounts.length === 1 && posting.lotCost === null && posting.cost === null &&
                        posting.balanceAssignment === null && posting.balanceAssertion === null) &&
                    transaction.postings[0].amounts[0].commodity ===
                        transaction.postings[1].amounts[0].commodity &&
                    compareDecimals(addDecimals(parseDecimal(transaction.postings[0].amounts[0].quantity), parseDecimal(transaction.postings[1].amounts[0].quantity)), parseDecimal('0')) === 0;
                const implicitPostingIndexes = transaction.postings.flatMap((posting, index) => posting.amount === null ? [index] : []);
                const elidedAmountIndex = canElideAmount
                    ? implicitPostingIndexes.length === 1 ? implicitPostingIndexes[0] : 1
                    : -1;
                transaction.postings.forEach((posting, index) => {
                    const inlinePostingComment = posting.comments[0];
                    const postingDateMarker = posting.postingDate && posting.postingDate !== date
                        ? `[${posting.postingDate}]` : null;
                    const inlinePostingMetadata = postingDateMarker === null
                        ? inlinePostingComment
                        : inlinePostingComment === undefined || inlinePostingComment === ''
                            ? postingDateMarker
                            : `${postingDateMarker} ${inlinePostingComment}`;
                    const formattedExpression = formatPostingExpression(posting, formats);
                    const expression = index === elidedAmountIndex ? '' : formattedExpression.text;
                    const alignmentWidth = posting.lotCost !== null || posting.cost !== null
                        ? expression.length
                        : formattedExpression.amountWidth;
                    const alignedExpression = (width) => expression.padStart(expression.length + Math.max(0, width - alignmentWidth));
                    const body = expression === ''
                        ? posting.account +
                            (posting.amount !== null && posting.account.length > accountColumnWidth ? '  ' : '')
                        : posting.account.length > accountColumnWidth
                            ? `${posting.account}  ${alignedExpression(10)}`
                            : `${posting.account.padEnd(accountColumnWidth)}  ` +
                                alignedExpression(amountColumnWidth);
                    const inlineComment = inlinePostingMetadata === undefined ? '' : `  ; ${inlinePostingMetadata}`;
                    const maximumWidth = expression === '' ? maximumPostingLineWidth : 80;
                    const projectedLineLength = expression === ''
                        ? 4 + Math.min(posting.account.length, accountColumnWidth) +
                            (posting.amount !== null && posting.account.length > accountColumnWidth ? 2 : 0) +
                            inlineComment.length
                        : `    ${body}${inlineComment}`.length;
                    const wrapPostingComment = inlinePostingMetadata !== undefined &&
                        projectedLineLength > maximumWidth;
                    const postingComment = wrapPostingComment ? '' : inlineComment;
                    lines.push(`    ${body}${postingComment}`);
                    if (wrapPostingComment)
                        lines.push(`    ; ${inlinePostingMetadata}`);
                    for (const comment of posting.comments.slice(1))
                        lines.push(`    ; ${comment}`);
                    for (const tag of posting.tags || [])
                        lines.push(`    ; ${formatTag(tag)}`);
                });
                lines.push('');
            }
            if (lines.length === 0)
                return '';
            lines.pop();
            return `${lines.join('\n')}\n`;
        }
        return formatTransactionsText(report, descriptions);
    },
});