⚙️ Ajout d'un argument décimales sur le mécanisme arrondi

pull/908/head
Maxime Quandalle 2020-02-26 14:51:40 +01:00
parent 48e14b8e20
commit f08ef0b39c
4 changed files with 95 additions and 16 deletions

View File

@ -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 (
<Node
classes="mecanism arrondi"
name="arrondi"
value={nodeValue}
unit={explanation.unit}
child={makeJsx(explanation)}
unit={explanation.value.unit}
child={
<>
{makeJsx(explanation.value)}
{explanation.decimals.isDefault !== false && (
<p>
<Trans i18nKey="arrondi-to-decimals">
Arrondi à {explanation.decimals.nodeValue} décimales
</Trans>
</p>
)}
</>
}
/>
)
}
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<ArrondiExplanation>
) => {
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) => (
<MecanismRound nodeValue={nodeValue} explanation={explanation} />
),
explanation: recurse(v),
category: 'mecanism',
name: 'arrondi',
type: 'numeric',
unit: node.unit
unit: explanation.value.unit
}
}

View File

@ -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

View File

@ -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<Explanation = Rule> = Rule & {
nodeValue?: number
isDefault?: boolean
isApplicable: boolean
missingVariables: Array<DottedName>
explanation: EvaluatedRule
explanation: Explanation
}

View File

@ -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