From f08ef0b39caaac64ee887b27fe61f077d6e0c52e Mon Sep 17 00:00:00 2001 From: Maxime Quandalle Date: Wed, 26 Feb 2020 14:51:40 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20Ajout=20d'un=20argument=20?= =?UTF-8?q?d=C3=A9cimales=20sur=20le=20m=C3=A9canisme=20arrondi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/engine/mecanisms/arrondi.tsx | 86 ++++++++++++++++++++++++----- source/locales/en.yaml | 1 + source/types/rule.ts | 5 +- test/mécanismes/arrondi.yaml | 19 +++++++ 4 files changed, 95 insertions(+), 16 deletions(-) diff --git a/source/engine/mecanisms/arrondi.tsx b/source/engine/mecanisms/arrondi.tsx index 15cc0f639..edf30ef61 100644 --- a/source/engine/mecanisms/arrondi.tsx +++ b/source/engine/mecanisms/arrondi.tsx @@ -1,39 +1,97 @@ -import { evaluateNode, makeJsx } from 'Engine/evaluation' +import { + defaultNode, + evaluateNode, + makeJsx, + mergeAllMissing +} from 'Engine/evaluation' import { Node } from 'Engine/mecanismViews/common' +import { has } from 'ramda' import React from 'react' +import { Trans } from 'react-i18next' +import { EvaluatedRule } from 'Types/rule' -function MecanismRound({ nodeValue, explanation }) { +type MecanismRoundProps = { + nodeValue: EvaluatedRule['nodeValue'] + explanation: ArrondiExplanation +} + +type ArrondiExplanation = { + value: EvaluatedRule + decimals: EvaluatedRule +} + +function MecanismRound({ nodeValue, explanation }: MecanismRoundProps) { return ( + {makeJsx(explanation.value)} + {explanation.decimals.isDefault !== false && ( +

+ + Arrondi à {explanation.decimals.nodeValue} décimales + +

+ )} + + } /> ) } -export default (recurse, k, v) => { - const node = recurse(v) +function roundWithPrecision(n: number, fractionDigits: number) { + return +n.toFixed(fractionDigits) +} - let evaluate = (cache, situation, parsedRules, node) => { - const child = evaluateNode(cache, situation, parsedRules, node.explanation) - const nodeValue = - child.nodeValue === null ? null : Math.round(child.nodeValue) - return { ...node, unit: child.unit, nodeValue, explanation: child } - } +let evaluate = ( + cache, + situation, + parsedRules, + node: EvaluatedRule +) => { + const evaluateAttribute = evaluateNode.bind( + null, + cache, + situation, + parsedRules + ) + const value = evaluateAttribute(node.explanation.value) + const decimals = evaluateAttribute(node.explanation.decimals) + + const nodeValue = + typeof value.nodeValue === 'number' + ? roundWithPrecision(value.nodeValue, decimals.nodeValue) + : value.nodeValue return { + ...node, + unit: value.unit, + nodeValue, + missingVariables: mergeAllMissing([value, decimals]), + explanation: { value, decimals } + } +} + +export default (recurse, k, v) => { + const explanation = { + value: has('valeur', v) ? recurse(v['valeur']) : recurse(v), + decimals: has('décimales', v) ? recurse(v['décimales']) : defaultNode(0) + } as ArrondiExplanation + + return { + explanation, evaluate, // eslint-disable-next-line jsx: (nodeValue, explanation) => ( ), - explanation: recurse(v), category: 'mecanism', name: 'arrondi', type: 'numeric', - unit: node.unit + unit: explanation.value.unit } } diff --git a/source/locales/en.yaml b/source/locales/en.yaml index 5e787e5d7..384bdde85 100644 --- a/source/locales/en.yaml +++ b/source/locales/en.yaml @@ -173,6 +173,7 @@ après: is composed by the SIREN followed by the establishment number (NIC). titre: The SIRET number titre: After registration +arrondi-to-decimals: 'Rounding to {explanation.decimals.nodeValue} decimals' assiette: base associés: choix1: Alone diff --git a/source/types/rule.ts b/source/types/rule.ts index 7d8aa2fbd..493b47468 100644 --- a/source/types/rule.ts +++ b/source/types/rule.ts @@ -16,9 +16,10 @@ export type Rule = { // This type should be defined inline by the function evaluating the rule (and // probably infered as its return type). This is only a partial definition but // it type-checks. -export type EvaluatedRule = Rule & { +export type EvaluatedRule = Rule & { nodeValue?: number + isDefault?: boolean isApplicable: boolean missingVariables: Array - explanation: EvaluatedRule + explanation: Explanation } diff --git a/test/mécanismes/arrondi.yaml b/test/mécanismes/arrondi.yaml index 26cf8ecb2..472287055 100644 --- a/test/mécanismes/arrondi.yaml +++ b/test/mécanismes/arrondi.yaml @@ -19,3 +19,22 @@ Arrondi: situation: cotisation retraite: 1200.50 valeur attendue: 1201 + +nombre de décimales: + +Arrondi avec precision: + formule: + arrondi: + valeur: cotisation retraite + décimales: nombre de décimales + exemples: + - nom: pas de décimales + situation: + cotisation retraite: 1200.21 + nombre de décimales: 0 + valeur attendue: 1200 + - nom: deux décimales + situation: + cotisation retraite: 1200.21 + nombre de décimales: 2 + valeur attendue: 1200.21