forked from dtc-innovation/dataviz-finances-montreuil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fonctions.html
277 lines (218 loc) · 10.2 KB
/
fonctions.html
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!doctype html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="referrer" content="same-origin">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Formules d'agrégations de lignes budgétaires</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script crossorigin="anonymous" src="https://cdn.polyfill.io/v2/polyfill.min.js"></script>
<script crossorigin="anonymous" src="https://cdn.jsdelivr.net/npm/[email protected]/lib/nearley.min.js"></script>
<script crossorigin="anonymous" src="https://davidbruant.github.io/formule-doc-budg/grammar.js"></script>
<script type="module">
import Bouture from 'https://cdn.jsdelivr.net/gh/DavidBruant/bouture@13cb6c683fa87e5feea574311dcda6353489bb3b/bouture.js'
import memoize from 'https://cdn.jsdelivr.net/npm/[email protected]/memoize.js'
import sum from 'https://cdn.jsdelivr.net/npm/[email protected]/src/sum.js'
function makeFilterFromParserOutput(parserOutput){
function matchesSimple(r, subset){
switch(subset){
case 'R':
case 'D':
return r['CodRD'] === subset;
case 'F':
case 'I':
return r['FI'] === subset;
case 'RF':
case 'RI':
case 'DF':
case 'DI':
return r['CodRD'] === subset[0] && r['FI'] === subset[1];
}
if(subset.startsWith('N'))
return subset.slice(1) === r['Nature']
if(subset.startsWith('F'))
return r['Fonction'].startsWith(subset.slice(1))
if(subset.startsWith('C'))
return subset.slice(1) === r['Chapitre']
console.warn('matchesSubset - Unhandled case', subset);
}
function matchesComplex(r, combo){
if(typeof combo === 'string')
return matchesSimple(r, combo);
// Array.isArray(combo)
const [left, middle, right] = combo;
if(left === '(' && right === ')')
return matchesComplex(r, middle)
else{
const operator = middle;
switch(operator){
case '+':
return matchesComplex(r, left) || matchesComplex(r, right)
case '*':
return matchesComplex(r, left) && matchesComplex(r, right)
case '-':
return matchesComplex(r, left) && !matchesComplex(r, right)
default:
console.warn('matchesSubset - Unhandled case', operator, combo);
}
}
console.warn('matchesSubset - Unhandled case', combo);
}
return function(r){
return matchesComplex(r, parserOutput[0])
}
}
function makeTable(rows, year){
return Bouture.section([
Bouture.h1('CA Montreuil ', year),
Bouture.h2(rows.length, ' elements | ', sum(rows.map(r => r['MtReal'])).toFixed(2)+'€'),
Bouture.table([
Bouture.thead.tr(['RD', 'FI', 'Fonction', 'Nature', 'Montant'].map(t => Bouture.th(t))),
Bouture.tbody(
rows.map(r => {
return Bouture.tr([
Bouture.td(r['CodRD']),
Bouture.td(r['FI']),
Bouture.td(r['Fonction']),
Bouture.td(r['Nature']),
Bouture.td(r['MtReal'].toFixed(2)+'€')
])
})
)
])
])
}
const docBudgP = fetch('./build/finances/doc-budgs.json').then(r => r.json())
document.addEventListener('DOMContentLoaded', e => {
const input = document.body.querySelector('input');
const output = document.body.querySelector('output');
docBudgP.then(docBudgs => {
const docBudg = docBudgs.find(db => db["Exer"] === 2017)
function makeOutputFromFormula(formula){
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
parser.feed(formula);
const filter = makeFilterFromParserOutput(parser.results);
return Bouture.output([docBudg].map(
db => makeTable(db['rows'].filter(filter), db['Exer'])
)).getElement()
}
const memzMakeOutputFromFormula = memoize(makeOutputFromFormula)
let changeHashTimeout;
input.addEventListener('input', e => {
const formula = e.target.value.trim();
document.body.querySelector('output').replaceWith(
memzMakeOutputFromFormula(formula)
)
// save in hash if formula stayed unchanged for 3secs
clearTimeout(changeHashTimeout)
changeHashTimeout = setTimeout(() => {
location.hash = encodeURIComponent(formula)
}, 3000)
})
})
function hashUpdate(){
const hashValue = decodeURIComponent(location.hash.slice(1));
if(hashValue && hashValue !== input.value){
input.value = hashValue;
input.dispatchEvent(new Event('input'))
}
}
// empty input (sometimes filled with pre-refresh value)
input.value = "";
input.focus()
// initialize input vith hash value
docBudgP.then( hashUpdate )
window.addEventListener('hashchange', hashUpdate)
// Create examples list
const examples = [
{
formula: 'D',
description: 'toutes les dépenses'
},
{
formula: 'R',
description: 'toutes les recettes'
},
{
formula: 'RI',
description: `toutes les recettes d'investissement`
},
{
formula: 'DF',
description: 'toutes les dépenses de fonctionnement'
},
{
formula: 'DF*F4',
description: 'toutes les dépenses de fonctionnement de la fonction 4'
},
{
formula: 'DF*(F4+F5)',
description: 'toutes les dépenses de fonctionnement des fonctions 4 et 5'
},
{
formula: 'DF*(N64111+N6451)',
description: 'toutes les dépenses de fonctionnement des natures 64111 et 6451 (salaire + URSSAF)'
},
{
formula: 'DF*(N64111+N6451)*F52',
description: 'toutes les dépenses de fonctionnement salaires+URSSAF lié à la fonction 52 (Personnes handicapées)'
},
{
formula: 'RI*C16',
description: `toutes les recettes d'investissement du chapitre 16 (emprunts)`
}
];
const ul = Bouture.ul(examples.map(({formula, description}) => {
return Bouture.li([
Bouture.a({href: `#${encodeURIComponent(formula)}`}).code(formula),
Bouture.span(` : ${description}`)
])
})).getElement();
document.body.querySelector('ul.examples').replaceWith(ul)
}, {once: true})
</script>
<link rel="stylesheet" href="https://rawgit.com/twbs/bootstrap/v4-dev/dist/css/bootstrap-reboot.css">
<style>
body{
display: flex;
flex-direction: column;
align-items: center;
}
main{
display: flex;
flex-direction: column;
max-width: 60em;
}
input{
width: 100%;
font-size: 2em;
margin-bottom: 0.5em;
}
output{
background-color: #EEE;
white-space: pre;
display: flex;
flex-direction: row;
justify-content: space-around;
}
output table tbody td{
padding: 0 0.5em;
}
output table tbody td:nth-child(4),
output table tbody td:nth-child(5){
text-align: right;
}
</style>
</head>
<body>
<main>
<h1>Outil de test de formules</h1>
<h2>Un outil pour filtrer les lignes d'un document budgétaires</h2>
<p>Pour le moment, cet outil ne filtre que les données des comptes administratifs de la ville de Montreuil pour les années 2017.</p>
<h2>Exemples de formules</h2>
<ul class="examples"></ul>
<input type="text">
<output></output>
</main>
</body>
</html>