mon-entreprise/source/components/ResultsGrid.js

175 lines
4.8 KiB
JavaScript
Raw Normal View History

2017-11-28 15:00:43 +00:00
import R from 'ramda'
import React, { Component } from 'react'
2017-12-01 11:51:28 +00:00
import { connect } from 'react-redux'
2017-11-28 15:00:43 +00:00
import { withRouter } from 'react-router'
import './Results.css'
import '../engine/mecanismViews/Somme.css'
2017-12-01 11:51:28 +00:00
import { humanFigure } from './rule/RuleValueVignette'
2017-11-28 15:00:43 +00:00
import { capitalise0 } from '../utils'
// Filtered variables and rules can't be filtered in a uniform way, for now
2017-12-01 11:51:28 +00:00
let paidBy = which => R.pathEq(['explanation', 'cotisation', 'dû par'], which)
let filteredBy = which => R.pathEq(['cotisation', 'dû par'], which)
export let byName = branch => R.groupBy(R.prop('dottedName'), branch)
export let cell = (branch, payer, analysis) => {
let row = byBranch(analysis)[branch],
2017-12-01 11:51:28 +00:00
items = R.filter(
item => paidBy(payer)(item) || filteredBy(payer)(item),
row
),
values = R.map(R.prop('nodeValue'), items)
return R.sum(values)
}
2017-11-29 19:37:24 +00:00
export let subCell = (row, name, payer) => {
let cells = row[name],
2017-12-01 11:51:28 +00:00
items = R.filter(
item => paidBy(payer)(item) || filteredBy(payer)(item),
cells
),
values = R.map(R.prop('nodeValue'), items)
2017-11-29 19:37:24 +00:00
return R.sum(values)
}
2017-12-01 11:51:28 +00:00
export let byBranch = analysis => {
2017-11-28 15:00:43 +00:00
let sal = analysis.dict['contrat salarié . cotisations salariales']
let pat = analysis.dict['contrat salarié . cotisations patronales']
let l1 = sal ? sal.explanation.formule.explanation.explanation : [],
l2 = pat ? pat.explanation.formule.explanation.explanation : [],
explanations = R.concat(l1, l2),
2017-12-01 11:51:28 +00:00
byBranch = R.groupBy(
R.pathOr('autre', ['explanation', 'cotisation', 'branche']),
explanations
)
2017-11-28 15:00:43 +00:00
return byBranch
2017-11-28 15:00:43 +00:00
}
@withRouter
2017-12-01 11:51:28 +00:00
@connect(state => ({
analysis: state.analysis,
targetNames: state.targetNames,
situationGate: state.situationGate
}))
2017-11-28 15:00:43 +00:00
export default class ResultsGrid extends Component {
render() {
2017-12-01 11:51:28 +00:00
let { analysis, situationGate } = this.props
2017-11-28 15:00:43 +00:00
if (!analysis) return null
let extract = x => (x && x.nodeValue) || 0,
fromSituation = name => situationGate(name),
2017-12-01 11:51:28 +00:00
fromEval = name => R.find(R.propEq('dottedName', name), analysis.targets),
2017-11-29 19:37:24 +00:00
fromDict = name => analysis.dict[name],
2017-12-01 11:51:28 +00:00
get = name =>
extract(fromSituation(name) || fromEval(name) || fromDict(name))
let results = byBranch(analysis),
brut = get('contrat salarié . salaire brut'),
net = get('contrat salarié . salaire net'),
total = get('contrat salarié . salaire total')
2017-11-28 15:00:43 +00:00
return (
<div className="somme">
2017-12-01 11:51:28 +00:00
<table>
<thead>
<tr>
<td className="element"><span className="annotation">Salaire brut</span></td>
<td colSpan="4" className="element" id="sommeBase">
{humanFigure(2)(brut)}
2017-12-01 11:51:28 +00:00
</td>
</tr>
</thead>
<tbody>
{R.keys(results).map(branch => {
let props = { branch, values: results[branch], analysis }
return <Row key={branch} {...props} />
})}
<tr>
<td className="element" />
<td className="operator">=</td>
<td className="element">{humanFigure(2)(net)} <div className="annotation">Salaire net</div></td>
2017-12-01 11:51:28 +00:00
<td className="operator">=</td>
<td className="element">
{humanFigure(2)(total)} <div className="annotation">Salaire total</div>
2017-12-01 11:51:28 +00:00
</td>
</tr>
</tbody>
</table>
</div>
)
}
}
class Row extends Component {
2017-11-29 19:37:24 +00:00
state = {
folded: true
}
render() {
2017-11-29 19:37:24 +00:00
let { branch, values, analysis } = this.props,
detail = byName(values)
2017-11-29 19:37:24 +00:00
let title = name => {
let node = R.head(detail[name])
return node.title || capitalise0(node.name)
}
let aggregateRow = () => {
2017-12-01 11:51:28 +00:00
return this.state.folded ? (
2017-11-29 19:37:24 +00:00
<tr onClick={() => this.setState({ folded: !this.state.folded })}>
2017-12-01 11:51:28 +00:00
<td className="element">
{capitalise0(branch)}&nbsp;<span className="unfoldIndication">
</span>
</td>
<td className="operator">-</td>
<td className="element">
{humanFigure(2)(cell(branch, 'salarié', analysis))}
</td>
<td className="operator">+</td>
<td className="element">
{humanFigure(2)(cell(branch, 'employeur', analysis))}
</td>
2017-11-29 19:37:24 +00:00
</tr>
2017-12-01 11:51:28 +00:00
) : (
// unfolded
2017-11-29 19:37:24 +00:00
<tr onClick={() => this.setState({ folded: !this.state.folded })}>
2017-12-01 11:51:28 +00:00
<td className="element">
{capitalise0(branch)}&nbsp;<span className="unfoldIndication">
'
</span>
</td>
<td colSpan="4" />
2017-11-29 19:37:24 +00:00
</tr>
2017-12-01 11:51:28 +00:00
)
2017-11-29 19:37:24 +00:00
}
let detailRows = () => {
2017-12-01 11:51:28 +00:00
return this.state.folded
? []
: R.keys(detail).map(subCellName => (
2017-11-29 19:37:24 +00:00
<tr>
<td className="element name">&nbsp;{title(subCellName)}</td>
2017-11-29 19:37:24 +00:00
<td className="operator">-</td>
2017-12-01 11:51:28 +00:00
<td className="element">
{humanFigure(2)(subCell(detail, subCellName, 'salarié'))}
</td>
2017-11-29 19:37:24 +00:00
<td className="operator">+</td>
2017-12-01 11:51:28 +00:00
<td className="element">
{humanFigure(2)(subCell(detail, subCellName, 'employeur'))}
</td>
2017-11-29 19:37:24 +00:00
</tr>
2017-12-01 11:51:28 +00:00
))
2017-11-29 19:37:24 +00:00
}
// returns an array of <tr>
2017-12-01 11:51:28 +00:00
return R.concat([aggregateRow()], detailRows())
2017-11-28 15:00:43 +00:00
}
}