import { useEvaluation, EngineContext } from 'Components/utils/EngineContext' import Value from 'Components/EngineValue' import { formatValue } from 'Engine/format' import React, { Fragment, useContext } from 'react' import { Trans } from 'react-i18next' import { DottedName } from 'Rules' import './PaySlip.css' import { Line, SalaireBrutSection, SalaireNetSection } from './PaySlipSections' import RuleLink from './RuleLink' import { ParsedRules, ParsedRule } from 'Rules' export const SECTION_ORDER = [ 'protection sociale . santé', 'protection sociale . accidents du travail et maladies professionnelles', 'protection sociale . retraite', 'protection sociale . famille', 'protection sociale . assurance chômage', 'protection sociale . formation', 'protection sociale . transport', 'protection sociale . autres' ] as const type Section = typeof SECTION_ORDER[number] function getSection(rule: ParsedRule): Section { const section = ('protection sociale . ' + rule.cotisation?.branche) as Section if (SECTION_ORDER.includes(section)) { return section } return 'protection sociale . autres' } export function getCotisationsBySection( parsedRules: ParsedRules ): Array<[Section, DottedName[]]> { const cotisations = [ ...parsedRules['contrat salarié . cotisations . patronales'].formule .explanation.explanation, ...parsedRules['contrat salarié . cotisations . salariales'].formule .explanation.explanation ] .map(cotisation => cotisation.dottedName) .filter(Boolean) .reduce((acc, cotisation: DottedName) => { const sectionName = getSection(parsedRules[cotisation]) return { ...acc, [sectionName]: (acc[sectionName] ?? new Set()).add(cotisation) } }, {}) as Record> return Object.entries(cotisations) .map(([section, dottedNames]) => [section, [...dottedNames.values()]]) .sort( ([a], [b]) => SECTION_ORDER.indexOf(a as Section) - SECTION_ORDER.indexOf(b as Section) ) as Array<[Section, DottedName[]]> } export default function PaySlip() { const parsedRules = useContext(EngineContext).getParsedRules() const cotisationsBySection = getCotisationsBySection(parsedRules) return (
{/* Section cotisations */}

Cotisations sociales

Part employeur

Part salarié

{cotisationsBySection.map(([sectionDottedName, cotisations]) => { let section = parsedRules[sectionDottedName] return (
{cotisations.map(cotisation => ( ))}
) })} {/* Réductions */}
Réductions
{/* Total cotisation */}

Total des retenues

{/* Salaire chargé */}
{/* Section salaire net */}
) } function Cotisation({ dottedName }: { dottedName: DottedName }) { const parsedRules = useContext(EngineContext).getParsedRules() const partSalariale = useEvaluation( 'contrat salarié . cotisations . salariales' )?.formule.explanation.explanation.find( (cotisation: ParsedRule) => cotisation.dottedName === dottedName ) const partPatronale = useEvaluation( 'contrat salarié . cotisations . patronales' )?.formule.explanation.explanation.find( (cotisation: ParsedRule) => cotisation.dottedName === dottedName ) if (!partPatronale?.nodeValue && !partSalariale?.nodeValue) { return null } return ( <> {partPatronale?.nodeValue ? formatValue({ ...partPatronale, unit: '€' }) : '–'} {partSalariale?.nodeValue ? formatValue({ ...partSalariale, unit: '€' }) : '–'} ) }