All files / src/modules/implementations/queries/support cost-valuation.js

100% Statements 32/32
80% Branches 4/5
100% Functions 2/2
100% Lines 32/32

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 3351x 51x 51x 783x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 6x 6x 6x 32x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 51x  
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
module.exports = ({ core: { publicErrors: { createError, errorCodes }, }, databaseValuationCommodity: { valuationCommodityFromDatabase }, }) => {
    function validateCostValuation(database, filter) {
        const valuationCommodity = valuationCommodityFromDatabase(database);
        const missing = database.prepare(`
      SELECT p.account, r.amount_commodity AS commodity
      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
      ${filter.sql}
        ${filter.sql ? 'AND' : 'WHERE'} r.amount_commodity != ?
        AND (p.lot_cost_quantity IS NULL OR p.lot_cost_commodity != ?)
      LIMIT 1
    `).get(...filter.parameters, valuationCommodity, valuationCommodity);
        if (missing) {
            throw createError(errorCodes.MISSING_VALUATION_DATA, `Cannot value ${missing.commodity} at cost for ${missing.account}: ` +
                `a lot cost in ${valuationCommodity} is required`);
        }
    }
    const costValueSql = `CASE
    WHEN r.amount_commodity = (SELECT value FROM database_metadata WHERE key = 'valuation_commodity')
      THEN r.amount_quantity
    WHEN p.lot_cost_is_total = 1 THEN CASE
      WHEN decimal_cmp(r.amount_quantity, '0') < 0 AND decimal_cmp(p.lot_cost_quantity, '0') > 0
        THEN decimal_mul(p.lot_cost_quantity, '-1')
      ELSE p.lot_cost_quantity
    END
    ELSE decimal_mul(r.amount_quantity, p.lot_cost_quantity)
  END`;
    return { validateCostValuation, costValueSql };
};