@hed-hog/finance 0.0.261 → 0.0.266

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. package/dist/dto/update-finance-scenario-settings.dto.d.ts +7 -0
  2. package/dist/dto/update-finance-scenario-settings.dto.d.ts.map +1 -0
  3. package/dist/dto/update-finance-scenario-settings.dto.js +39 -0
  4. package/dist/dto/update-finance-scenario-settings.dto.js.map +1 -0
  5. package/dist/finance-data.controller.d.ts +61 -7
  6. package/dist/finance-data.controller.d.ts.map +1 -1
  7. package/dist/finance-data.controller.js +23 -3
  8. package/dist/finance-data.controller.js.map +1 -1
  9. package/dist/finance.service.d.ts +79 -9
  10. package/dist/finance.service.d.ts.map +1 -1
  11. package/dist/finance.service.js +471 -70
  12. package/dist/finance.service.js.map +1 -1
  13. package/hedhog/data/route.yaml +9 -0
  14. package/hedhog/data/setting_group.yaml +152 -0
  15. package/hedhog/frontend/app/_lib/use-finance-data.ts.ejs +31 -3
  16. package/hedhog/frontend/app/planning/cash-flow-forecast/page.tsx.ejs +38 -7
  17. package/hedhog/frontend/app/planning/receivables-calendar/page.tsx.ejs +3 -1
  18. package/hedhog/frontend/app/planning/scenarios/page.tsx.ejs +74 -4
  19. package/hedhog/frontend/app/reports/actual-vs-forecast/page.tsx.ejs +361 -0
  20. package/hedhog/frontend/app/reports/aging-default/page.tsx.ejs +368 -0
  21. package/hedhog/frontend/app/reports/cash-position/page.tsx.ejs +432 -0
  22. package/hedhog/frontend/messages/en.json +182 -0
  23. package/hedhog/frontend/messages/pt.json +182 -0
  24. package/hedhog/query/triggers-period-close.sql +361 -0
  25. package/package.json +8 -8
  26. package/src/dto/update-finance-scenario-settings.dto.ts +21 -0
  27. package/src/finance-data.controller.ts +18 -3
  28. package/src/finance.service.ts +781 -79
@@ -0,0 +1,368 @@
1
+ 'use client';
2
+
3
+ import { Page, PageHeader } from '@/components/entity-list';
4
+ import { Badge } from '@/components/ui/badge';
5
+ import {
6
+ Card,
7
+ CardContent,
8
+ CardDescription,
9
+ CardHeader,
10
+ CardTitle,
11
+ } from '@/components/ui/card';
12
+ import { Money } from '@/components/ui/money';
13
+ import {
14
+ Select,
15
+ SelectContent,
16
+ SelectItem,
17
+ SelectTrigger,
18
+ SelectValue,
19
+ } from '@/components/ui/select';
20
+ import {
21
+ Table,
22
+ TableBody,
23
+ TableCell,
24
+ TableHead,
25
+ TableHeader,
26
+ TableRow,
27
+ } from '@/components/ui/table';
28
+ import { AlertTriangle, Loader2 } from 'lucide-react';
29
+ import { useTranslations } from 'next-intl';
30
+ import { useMemo, useState } from 'react';
31
+ import {
32
+ Bar,
33
+ BarChart,
34
+ CartesianGrid,
35
+ ResponsiveContainer,
36
+ Tooltip,
37
+ XAxis,
38
+ YAxis,
39
+ } from 'recharts';
40
+ import { useFinanceData } from '../../_lib/use-finance-data';
41
+
42
+ type Scenario = 'base' | 'pessimista' | 'otimista';
43
+
44
+ function toPercent(value: number) {
45
+ if (!Number.isFinite(value)) {
46
+ return 0;
47
+ }
48
+
49
+ return value <= 1 ? value * 100 : value;
50
+ }
51
+
52
+ export default function AgingDefaultReportPage() {
53
+ const t = useTranslations('finance.AgingDefaultReportPage');
54
+
55
+ const [horizonte, setHorizonte] = useState<string | null>(null);
56
+ const [cenario, setCenario] = useState<Scenario | null>(null);
57
+
58
+ const { data, isFetching } = useFinanceData({
59
+ horizonteDias: horizonte ? Number(horizonte) : undefined,
60
+ cenario: cenario || undefined,
61
+ });
62
+
63
+ const selectedHorizon = horizonte || String(data.defaultHorizonDays || 90);
64
+ const selectedScenario = cenario || data.defaultScenario || 'base';
65
+
66
+ const agingInadimplencia = data.agingInadimplencia || [];
67
+
68
+ const totals = useMemo(() => {
69
+ const bucket0to30 = agingInadimplencia.reduce(
70
+ (acc: number, item: any) => acc + Number(item.bucket0_30 || 0),
71
+ 0
72
+ );
73
+ const bucket31to60 = agingInadimplencia.reduce(
74
+ (acc: number, item: any) => acc + Number(item.bucket31_60 || 0),
75
+ 0
76
+ );
77
+ const bucket61to90 = agingInadimplencia.reduce(
78
+ (acc: number, item: any) => acc + Number(item.bucket61_90 || 0),
79
+ 0
80
+ );
81
+ const bucket90plus = agingInadimplencia.reduce(
82
+ (acc: number, item: any) => acc + Number(item.bucket90plus || 0),
83
+ 0
84
+ );
85
+ const total = agingInadimplencia.reduce(
86
+ (acc: number, item: any) => acc + Number(item.total || 0),
87
+ 0
88
+ );
89
+
90
+ return {
91
+ bucket0to30,
92
+ bucket31to60,
93
+ bucket61to90,
94
+ bucket90plus,
95
+ total,
96
+ clients: agingInadimplencia.length,
97
+ };
98
+ }, [agingInadimplencia]);
99
+
100
+ const chartData = [
101
+ {
102
+ name: t('chart.range0to30'),
103
+ value: totals.bucket0to30,
104
+ },
105
+ {
106
+ name: t('chart.range31to60'),
107
+ value: totals.bucket31to60,
108
+ },
109
+ {
110
+ name: t('chart.range61to90'),
111
+ value: totals.bucket61to90,
112
+ },
113
+ {
114
+ name: t('chart.range90plus'),
115
+ value: totals.bucket90plus,
116
+ },
117
+ ];
118
+
119
+ return (
120
+ <Page>
121
+ <PageHeader
122
+ title={t('header.title')}
123
+ description={t('header.description')}
124
+ breadcrumbs={[
125
+ { label: t('breadcrumbs.home'), href: '/' },
126
+ { label: t('breadcrumbs.finance'), href: '/finance' },
127
+ { label: t('breadcrumbs.current') },
128
+ ]}
129
+ />
130
+
131
+ <div className="flex flex-col gap-4 sm:flex-row sm:items-center">
132
+ <Select
133
+ value={selectedHorizon}
134
+ onValueChange={setHorizonte}
135
+ disabled={isFetching}
136
+ >
137
+ <SelectTrigger className="w-[180px]">
138
+ <SelectValue placeholder={t('filters.horizon')} />
139
+ </SelectTrigger>
140
+ <SelectContent>
141
+ <SelectItem value="30">{t('filters.days30')}</SelectItem>
142
+ <SelectItem value="60">{t('filters.days60')}</SelectItem>
143
+ <SelectItem value="90">{t('filters.days90')}</SelectItem>
144
+ <SelectItem value="180">{t('filters.days180')}</SelectItem>
145
+ <SelectItem value="365">{t('filters.days365')}</SelectItem>
146
+ </SelectContent>
147
+ </Select>
148
+ <Select
149
+ value={selectedScenario}
150
+ onValueChange={(value) => setCenario(value as Scenario)}
151
+ disabled={isFetching}
152
+ >
153
+ <SelectTrigger className="w-[180px]">
154
+ <SelectValue placeholder={t('filters.scenario')} />
155
+ </SelectTrigger>
156
+ <SelectContent>
157
+ <SelectItem value="base">{t('scenarios.base')}</SelectItem>
158
+ <SelectItem value="pessimista">
159
+ {t('scenarios.pessimistic')}
160
+ </SelectItem>
161
+ <SelectItem value="otimista">
162
+ {t('scenarios.optimistic')}
163
+ </SelectItem>
164
+ </SelectContent>
165
+ </Select>
166
+ {isFetching ? (
167
+ <div className="text-muted-foreground flex items-center gap-2 text-sm">
168
+ <Loader2 className="h-4 w-4 animate-spin" />
169
+ {t('messages.updating')}
170
+ </div>
171
+ ) : null}
172
+ </div>
173
+
174
+ <div className="grid gap-4 md:grid-cols-4">
175
+ <Card>
176
+ <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
177
+ <CardTitle className="text-sm font-medium">
178
+ {t('cards.totalDefault')}
179
+ </CardTitle>
180
+ <AlertTriangle className="h-4 w-4 text-destructive" />
181
+ </CardHeader>
182
+ <CardContent>
183
+ <div className="text-2xl font-bold">
184
+ <Money value={totals.total} />
185
+ </div>
186
+ </CardContent>
187
+ </Card>
188
+
189
+ <Card>
190
+ <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
191
+ <CardTitle className="text-sm font-medium">
192
+ {t('cards.lateClients')}
193
+ </CardTitle>
194
+ </CardHeader>
195
+ <CardContent>
196
+ <div className="text-2xl font-bold">{totals.clients}</div>
197
+ </CardContent>
198
+ </Card>
199
+
200
+ <Card>
201
+ <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
202
+ <CardTitle className="text-sm font-medium">
203
+ {t('cards.over90Days')}
204
+ </CardTitle>
205
+ </CardHeader>
206
+ <CardContent>
207
+ <div className="text-2xl font-bold text-destructive">
208
+ <Money value={totals.bucket90plus} />
209
+ </div>
210
+ </CardContent>
211
+ </Card>
212
+
213
+ <Card>
214
+ <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
215
+ <CardTitle className="text-sm font-medium">
216
+ {t('cards.defaultRate')}
217
+ </CardTitle>
218
+ </CardHeader>
219
+ <CardContent>
220
+ <div className="text-2xl font-bold">
221
+ {toPercent(Number(data.kpis?.inadimplencia || 0)).toFixed(1)}%
222
+ </div>
223
+ </CardContent>
224
+ </Card>
225
+ </div>
226
+
227
+ <Card>
228
+ <CardHeader>
229
+ <CardTitle>{t('chart.title')}</CardTitle>
230
+ <CardDescription>{t('chart.description')}</CardDescription>
231
+ </CardHeader>
232
+ <CardContent>
233
+ <ResponsiveContainer width="100%" height={280}>
234
+ <BarChart data={chartData}>
235
+ <CartesianGrid strokeDasharray="3 3" className="stroke-muted" />
236
+ <XAxis dataKey="name" tick={{ fontSize: 12 }} />
237
+ <YAxis
238
+ tick={{ fontSize: 12 }}
239
+ tickFormatter={(value) => `${(value / 1000).toFixed(0)}k`}
240
+ />
241
+ <Tooltip
242
+ formatter={(value: number) =>
243
+ new Intl.NumberFormat('pt-BR', {
244
+ style: 'currency',
245
+ currency: 'BRL',
246
+ }).format(value)
247
+ }
248
+ contentStyle={{
249
+ backgroundColor: 'hsl(var(--background))',
250
+ border: '1px solid hsl(var(--border))',
251
+ borderRadius: '8px',
252
+ }}
253
+ />
254
+ <Bar
255
+ dataKey="value"
256
+ radius={[4, 4, 0, 0]}
257
+ fill="hsl(var(--primary))"
258
+ />
259
+ </BarChart>
260
+ </ResponsiveContainer>
261
+ </CardContent>
262
+ </Card>
263
+
264
+ <Card>
265
+ <CardHeader>
266
+ <CardTitle>{t('table.title')}</CardTitle>
267
+ <CardDescription>{t('table.description')}</CardDescription>
268
+ </CardHeader>
269
+ <CardContent>
270
+ <Table>
271
+ <TableHeader>
272
+ <TableRow>
273
+ <TableHead>{t('table.headers.client')}</TableHead>
274
+ <TableHead className="text-right">
275
+ {t('table.headers.range0to30')}
276
+ </TableHead>
277
+ <TableHead className="text-right">
278
+ {t('table.headers.range31to60')}
279
+ </TableHead>
280
+ <TableHead className="text-right">
281
+ {t('table.headers.range61to90')}
282
+ </TableHead>
283
+ <TableHead className="text-right">
284
+ {t('table.headers.range90plus')}
285
+ </TableHead>
286
+ <TableHead className="text-right">
287
+ {t('table.headers.total')}
288
+ </TableHead>
289
+ <TableHead>{t('table.headers.risk')}</TableHead>
290
+ </TableRow>
291
+ </TableHeader>
292
+ <TableBody>
293
+ {agingInadimplencia.length === 0 ? (
294
+ <TableRow>
295
+ <TableCell
296
+ colSpan={7}
297
+ className="text-muted-foreground text-center"
298
+ >
299
+ {t('table.empty')}
300
+ </TableCell>
301
+ </TableRow>
302
+ ) : (
303
+ agingInadimplencia.map((item: any) => {
304
+ const riskVariant =
305
+ item.bucket90plus > 0
306
+ ? 'destructive'
307
+ : item.bucket61_90 > 0
308
+ ? 'secondary'
309
+ : 'outline';
310
+
311
+ return (
312
+ <TableRow key={item.clienteId}>
313
+ <TableCell className="font-medium">
314
+ {item.cliente}
315
+ </TableCell>
316
+ <TableCell className="text-right">
317
+ {item.bucket0_30 > 0 ? (
318
+ <Money value={item.bucket0_30} />
319
+ ) : (
320
+ '-'
321
+ )}
322
+ </TableCell>
323
+ <TableCell className="text-right">
324
+ {item.bucket31_60 > 0 ? (
325
+ <Money value={item.bucket31_60} />
326
+ ) : (
327
+ '-'
328
+ )}
329
+ </TableCell>
330
+ <TableCell className="text-right">
331
+ {item.bucket61_90 > 0 ? (
332
+ <Money value={item.bucket61_90} />
333
+ ) : (
334
+ '-'
335
+ )}
336
+ </TableCell>
337
+ <TableCell className="text-right">
338
+ {item.bucket90plus > 0 ? (
339
+ <span className="text-destructive font-medium">
340
+ <Money value={item.bucket90plus} />
341
+ </span>
342
+ ) : (
343
+ '-'
344
+ )}
345
+ </TableCell>
346
+ <TableCell className="text-right font-semibold">
347
+ <Money value={item.total} />
348
+ </TableCell>
349
+ <TableCell>
350
+ <Badge variant={riskVariant}>
351
+ {item.bucket90plus > 0
352
+ ? t('risk.high')
353
+ : item.bucket61_90 > 0
354
+ ? t('risk.medium')
355
+ : t('risk.low')}
356
+ </Badge>
357
+ </TableCell>
358
+ </TableRow>
359
+ );
360
+ })
361
+ )}
362
+ </TableBody>
363
+ </Table>
364
+ </CardContent>
365
+ </Card>
366
+ </Page>
367
+ );
368
+ }