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 | 54x 54x 784x 391x 391x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 361x 391x 339x 339x 339x 339x 339x 339x 339x 339x 339x 389x 795x 322923x 322923x 322923x 322923x 322923x 4179x 4179x 4179x 4179x 4179x 322923x 795x 339x 391x 784x 54x | 'use strict';
module.exports = ({ core: { createLedgerValuationRateResolver, }, }) => {
function materializeValuationPrices(database, valuationCommodity) {
if (!valuationCommodity)
return 0;
const introductions = database.prepare(`
WITH commodity_introductions AS (
SELECT r.amount_commodity AS commodity, MIN(p.posting_date) AS date
FROM resolved_posting_amounts AS r
JOIN postings AS p ON p.id = r.posting_id
GROUP BY r.amount_commodity
UNION ALL
SELECT r.amount_commodity, MIN(t.date) AS date
FROM resolved_posting_amounts AS r
JOIN postings AS p ON p.id = r.posting_id
JOIN transactions AS t ON t.entry_id = p.transaction_id
GROUP BY r.amount_commodity
UNION ALL
SELECT base_commodity, MIN(date) FROM prices GROUP BY base_commodity
UNION ALL
SELECT quote_commodity, MIN(date)
FROM prices
GROUP BY quote_commodity
)
SELECT commodity, MIN(date) AS start_date
FROM commodity_introductions
GROUP BY commodity
ORDER BY commodity
`).all();
const latestDate = database.prepare(`
SELECT MAX(date) FROM (
SELECT MAX(posting_date) AS date FROM postings
UNION ALL
SELECT MAX(date) AS date FROM transactions
UNION ALL
SELECT MAX(date) AS date FROM prices
)
`).pluck().get();
if (!latestDate)
return 0;
const priceRows = database.prepare(`
SELECT p.date, p.base_commodity, p.quote_quantity, p.quote_commodity
FROM prices AS p
ORDER BY p.base_commodity, p.date, p.entry_id
`).all();
const resolve = createLedgerValuationRateResolver(priceRows, valuationCommodity);
const insert = database.prepare('INSERT INTO valuation_prices (commodity, date, rate) VALUES (?, ?, ?)');
const nextDate = database.prepare("SELECT date(?, '+1 day')").pluck();
let changes = 0;
for (const { commodity, start_date: startDate } of introductions) {
for (let date = startDate; date && date <= latestDate; date = nextDate.get(date)) {
try {
insert.run(commodity, date, resolve(commodity, date));
changes += 1;
}
catch (error) {
if (!(error instanceof Error) ||
(!error.message.startsWith('No price for ') &&
!error.message.startsWith('Circular price chain ')))
throw error;
}
}
}
return changes;
}
return { materializeValuationPrices };
};
|