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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 67x 68x 68x 68x 68x 68x 68x 68x 68x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 9x 9x 9x 76x 76x 1x 1x 74x 74x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 1x | import { type AccessorNode, type ConstantNode } from 'mathjs';
import { AmaliaFunctionCategory, AmaliaFunctionKeys, type AmaliaFormula } from '@amalia/amalia-lang/formula/types';
import { type RecordContent } from '@amalia/data-capture/connectors/types';
import { type ComputeEngine2DTable } from '@amalia/payout-calculation/types';
import { AmaliaFunctionDefault } from '../../AmaliaFunction';
import { getValueOrFormula } from '../../utils';
import { getRowFieldAmount, getTableSlice } from '../common';
export const indicesRowTiers = new AmaliaFunctionDefault<
[RecordContent, ComputeEngine2DTable<number>, RecordContent[], string, string, number?],
number
>({
name: AmaliaFunctionKeys.rowTiersIndex,
category: AmaliaFunctionCategory.INDICES,
nbParamsRequired: 5,
exec: (rowToCompute, table, rows, fieldToSum, uniqueId, startFromRaw) => {
const startFrom = startFromRaw || 0;
// 1- sum all filter rows to get correct slice index to apply.
// Retrieve rows until row to compare
const indexRowToCompute = rows.findIndex((row) => row[uniqueId] === rowToCompute[uniqueId]);
const sumPreviousRows =
rows.slice(0, indexRowToCompute).reduce((acc, row) => acc + getRowFieldAmount(row, fieldToSum), 0) + startFrom;
const currentRowValue = getRowFieldAmount(rowToCompute, fieldToSum);
const sumWithCurrentRow = sumPreviousRows + currentRowValue;
const currentSlice = table
.slice()
.reverse()
.map((slice) => getTableSlice(slice))
.find((tableSlice) => sumWithCurrentRow >= tableSlice.min);
Eif (!currentSlice) {
return 0;
}
return currentSlice.percent;
},
generateComputedFunctionResult: (args) => ({
array: getValueOrFormula(args[2]),
formula: `${(args[0] as AccessorNode).name}.${(args[3] as ConstantNode).value}` as AmaliaFormula,
}),
description:
'This function will get the index at which the `dataObject` is present ' +
'in the `filteredRows`, and will compute a cumulative sum of the `fieldToSum` until this ' +
'row, included. It will then apply the `table` to this cumulative sum.',
params: [
{ name: 'dataObject', description: 'Data Object' },
{ name: 'table', description: 'Table for indices' },
{ name: 'rows', description: 'Filter of records: can be sorted' },
{ name: 'fieldToSum', description: 'Field usd for cumulative sum' },
{ name: 'uniqueId', description: 'A unique id used by Amalia calculation system to retrieve the current line' },
{
name: 'startFrom',
description: "Row tiers index computation will start cumulative sum from this value. By default it's 0",
defaultValue: 0,
},
],
examples: [
{
desc: 'Performs a tier based calculation of Amount Adjusted from opportunity based on the filter closedInPeriod starting from the oldest closing date.',
formula:
'rowTiersIndex(opportunity, statement.commissionTable, SORT(filter.closedInPeriod, "closingDate"), "amountAdjusted", "id")' as AmaliaFormula,
},
{
desc: 'Performs a tier based calculation of Amount Adjusted from opportunity based on the filter closedInPeriod starting from the oldest closing date and from record 2000.',
formula:
'rowTiersIndex(opportunity, statement.commissionTable, SORT(filter.closedInPeriod, "closingDate"), "amountAdjusted", "id", 2000)' as AmaliaFormula,
},
],
});
|