@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,361 @@
1
+ 'use client';
2
+
3
+ import { Page, PageHeader } from '@/components/entity-list';
4
+ import { Badge } from '@/components/ui/badge';
5
+ import { Button } from '@/components/ui/button';
6
+ import {
7
+ Card,
8
+ CardContent,
9
+ CardDescription,
10
+ CardHeader,
11
+ CardTitle,
12
+ } from '@/components/ui/card';
13
+ import { Money } from '@/components/ui/money';
14
+ import {
15
+ Select,
16
+ SelectContent,
17
+ SelectItem,
18
+ SelectTrigger,
19
+ SelectValue,
20
+ } from '@/components/ui/select';
21
+ import {
22
+ Table,
23
+ TableBody,
24
+ TableCell,
25
+ TableHead,
26
+ TableHeader,
27
+ TableRow,
28
+ } from '@/components/ui/table';
29
+ import {
30
+ Download,
31
+ Loader2,
32
+ Target,
33
+ TrendingDown,
34
+ TrendingUp,
35
+ } from 'lucide-react';
36
+ import { useTranslations } from 'next-intl';
37
+ import { useMemo, useState } from 'react';
38
+ import {
39
+ CartesianGrid,
40
+ Legend,
41
+ Line,
42
+ LineChart,
43
+ ResponsiveContainer,
44
+ Tooltip,
45
+ XAxis,
46
+ YAxis,
47
+ } from 'recharts';
48
+ import { formatarData } from '../../_lib/formatters';
49
+ import { useFinanceData } from '../../_lib/use-finance-data';
50
+
51
+ type Scenario = 'base' | 'pessimista' | 'otimista';
52
+
53
+ export default function ActualVsForecastPage() {
54
+ const t = useTranslations('finance.ActualVsForecastPage');
55
+
56
+ const [horizonte, setHorizonte] = useState<string | null>(null);
57
+ const [cenario, setCenario] = useState<Scenario | null>(null);
58
+
59
+ const { data, isFetching } = useFinanceData({
60
+ horizonteDias: horizonte ? Number(horizonte) : undefined,
61
+ cenario: cenario || undefined,
62
+ });
63
+
64
+ const selectedHorizon = horizonte || String(data.defaultHorizonDays || 90);
65
+ const selectedScenario = cenario || data.defaultScenario || 'base';
66
+
67
+ const comparisonData = useMemo(
68
+ () =>
69
+ data.fluxoCaixaPrevisto.map((item: any) => {
70
+ const previsto = Number(item.saldoPrevisto || 0);
71
+ const realizado = Number(item.saldoRealizado || 0);
72
+ const desvio = realizado - previsto;
73
+ const percentual =
74
+ previsto !== 0 ? (desvio / Math.abs(previsto)) * 100 : 0;
75
+
76
+ return {
77
+ data: item.data,
78
+ dataLabel: formatarData(item.data),
79
+ previsto,
80
+ realizado,
81
+ desvio,
82
+ percentual,
83
+ };
84
+ }),
85
+ [data.fluxoCaixaPrevisto]
86
+ );
87
+
88
+ const totals = useMemo(() => {
89
+ const previsto = comparisonData.reduce((acc, row) => acc + row.previsto, 0);
90
+ const realizado = comparisonData.reduce(
91
+ (acc, row) => acc + row.realizado,
92
+ 0
93
+ );
94
+ const desvio = realizado - previsto;
95
+ const acuracia =
96
+ previsto !== 0
97
+ ? Math.max(0, 100 - (Math.abs(desvio) / Math.abs(previsto)) * 100)
98
+ : 100;
99
+
100
+ return {
101
+ previsto,
102
+ realizado,
103
+ desvio,
104
+ acuracia,
105
+ };
106
+ }, [comparisonData]);
107
+
108
+ return (
109
+ <Page>
110
+ <PageHeader
111
+ title={t('header.title')}
112
+ description={t('header.description')}
113
+ breadcrumbs={[
114
+ { label: t('breadcrumbs.home'), href: '/' },
115
+ { label: t('breadcrumbs.finance'), href: '/finance' },
116
+ { label: t('breadcrumbs.current') },
117
+ ]}
118
+ actions={
119
+ <Button variant="outline" disabled>
120
+ <Download className="mr-2 h-4 w-4" />
121
+ {t('actions.export')}
122
+ </Button>
123
+ }
124
+ />
125
+
126
+ <div className="flex flex-col gap-4 sm:flex-row sm:items-center">
127
+ <Select
128
+ value={selectedHorizon}
129
+ onValueChange={setHorizonte}
130
+ disabled={isFetching}
131
+ >
132
+ <SelectTrigger className="w-[180px]">
133
+ <SelectValue placeholder={t('filters.horizon')} />
134
+ </SelectTrigger>
135
+ <SelectContent>
136
+ <SelectItem value="30">{t('filters.days30')}</SelectItem>
137
+ <SelectItem value="60">{t('filters.days60')}</SelectItem>
138
+ <SelectItem value="90">{t('filters.days90')}</SelectItem>
139
+ <SelectItem value="180">{t('filters.days180')}</SelectItem>
140
+ <SelectItem value="365">{t('filters.days365')}</SelectItem>
141
+ </SelectContent>
142
+ </Select>
143
+ <Select
144
+ value={selectedScenario}
145
+ onValueChange={(value) => setCenario(value as Scenario)}
146
+ disabled={isFetching}
147
+ >
148
+ <SelectTrigger className="w-[180px]">
149
+ <SelectValue placeholder={t('filters.scenario')} />
150
+ </SelectTrigger>
151
+ <SelectContent>
152
+ <SelectItem value="base">{t('scenarios.base')}</SelectItem>
153
+ <SelectItem value="pessimista">
154
+ {t('scenarios.pessimistic')}
155
+ </SelectItem>
156
+ <SelectItem value="otimista">
157
+ {t('scenarios.optimistic')}
158
+ </SelectItem>
159
+ </SelectContent>
160
+ </Select>
161
+ {isFetching ? (
162
+ <div className="text-muted-foreground flex items-center gap-2 text-sm">
163
+ <Loader2 className="h-4 w-4 animate-spin" />
164
+ {t('messages.updating')}
165
+ </div>
166
+ ) : null}
167
+ </div>
168
+
169
+ <div className="grid gap-4 md:grid-cols-4">
170
+ <Card>
171
+ <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
172
+ <CardTitle className="text-sm font-medium">
173
+ {t('cards.totalForecast')}
174
+ </CardTitle>
175
+ <Target className="h-4 w-4 text-muted-foreground" />
176
+ </CardHeader>
177
+ <CardContent>
178
+ <div className="text-2xl font-bold">
179
+ <Money value={totals.previsto} />
180
+ </div>
181
+ </CardContent>
182
+ </Card>
183
+ <Card>
184
+ <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
185
+ <CardTitle className="text-sm font-medium">
186
+ {t('cards.totalActual')}
187
+ </CardTitle>
188
+ <TrendingUp className="h-4 w-4 text-muted-foreground" />
189
+ </CardHeader>
190
+ <CardContent>
191
+ <div className="text-2xl font-bold">
192
+ <Money value={totals.realizado} />
193
+ </div>
194
+ </CardContent>
195
+ </Card>
196
+ <Card>
197
+ <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
198
+ <CardTitle className="text-sm font-medium">
199
+ {t('cards.totalDeviation')}
200
+ </CardTitle>
201
+ {totals.desvio >= 0 ? (
202
+ <TrendingUp className="h-4 w-4 text-green-600" />
203
+ ) : (
204
+ <TrendingDown className="h-4 w-4 text-red-600" />
205
+ )}
206
+ </CardHeader>
207
+ <CardContent>
208
+ <div
209
+ className={`text-2xl font-bold ${totals.desvio >= 0 ? 'text-green-600' : 'text-red-600'}`}
210
+ >
211
+ <Money value={totals.desvio} showSign />
212
+ </div>
213
+ </CardContent>
214
+ </Card>
215
+ <Card>
216
+ <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
217
+ <CardTitle className="text-sm font-medium">
218
+ {t('cards.accuracy')}
219
+ </CardTitle>
220
+ <Target className="h-4 w-4 text-muted-foreground" />
221
+ </CardHeader>
222
+ <CardContent>
223
+ <div className="text-2xl font-bold">
224
+ {totals.acuracia.toFixed(1)}%
225
+ </div>
226
+ </CardContent>
227
+ </Card>
228
+ </div>
229
+
230
+ <Card>
231
+ <CardHeader>
232
+ <CardTitle>{t('chart.title')}</CardTitle>
233
+ <CardDescription>{t('chart.description')}</CardDescription>
234
+ </CardHeader>
235
+ <CardContent>
236
+ <ResponsiveContainer width="100%" height={340}>
237
+ <LineChart data={comparisonData}>
238
+ <CartesianGrid strokeDasharray="3 3" className="stroke-muted" />
239
+ <XAxis dataKey="dataLabel" tick={{ fontSize: 12 }} />
240
+ <YAxis
241
+ tick={{ fontSize: 12 }}
242
+ tickFormatter={(value) => `${(value / 1000).toFixed(0)}k`}
243
+ />
244
+ <Tooltip
245
+ formatter={(value: number) =>
246
+ new Intl.NumberFormat('pt-BR', {
247
+ style: 'currency',
248
+ currency: 'BRL',
249
+ }).format(value)
250
+ }
251
+ contentStyle={{
252
+ backgroundColor: 'hsl(var(--background))',
253
+ border: '1px solid hsl(var(--border))',
254
+ borderRadius: '8px',
255
+ }}
256
+ />
257
+ <Legend />
258
+ <Line
259
+ type="monotone"
260
+ dataKey="previsto"
261
+ name={t('chart.forecastLabel')}
262
+ stroke="hsl(var(--primary))"
263
+ strokeWidth={2}
264
+ dot={false}
265
+ />
266
+ <Line
267
+ type="monotone"
268
+ dataKey="realizado"
269
+ name={t('chart.actualLabel')}
270
+ stroke="hsl(var(--chart-2))"
271
+ strokeWidth={2}
272
+ dot={false}
273
+ />
274
+ </LineChart>
275
+ </ResponsiveContainer>
276
+ </CardContent>
277
+ </Card>
278
+
279
+ <Card>
280
+ <CardHeader>
281
+ <CardTitle>{t('table.title')}</CardTitle>
282
+ <CardDescription>{t('table.description')}</CardDescription>
283
+ </CardHeader>
284
+ <CardContent>
285
+ <Table>
286
+ <TableHeader>
287
+ <TableRow>
288
+ <TableHead>{t('table.headers.date')}</TableHead>
289
+ <TableHead className="text-right">
290
+ {t('table.headers.forecast')}
291
+ </TableHead>
292
+ <TableHead className="text-right">
293
+ {t('table.headers.actual')}
294
+ </TableHead>
295
+ <TableHead className="text-right">
296
+ {t('table.headers.deviation')}
297
+ </TableHead>
298
+ <TableHead className="text-right">
299
+ {t('table.headers.percentDeviation')}
300
+ </TableHead>
301
+ <TableHead>{t('table.headers.status')}</TableHead>
302
+ </TableRow>
303
+ </TableHeader>
304
+ <TableBody>
305
+ {comparisonData.length === 0 ? (
306
+ <TableRow>
307
+ <TableCell
308
+ colSpan={6}
309
+ className="text-muted-foreground text-center"
310
+ >
311
+ {t('table.empty')}
312
+ </TableCell>
313
+ </TableRow>
314
+ ) : (
315
+ comparisonData.map((item, index) => {
316
+ const statusVariant =
317
+ item.desvio > 0
318
+ ? 'default'
319
+ : item.desvio < 0
320
+ ? 'destructive'
321
+ : 'secondary';
322
+
323
+ return (
324
+ <TableRow key={`${item.data}-${index}`}>
325
+ <TableCell>{item.dataLabel}</TableCell>
326
+ <TableCell className="text-right">
327
+ <Money value={item.previsto} />
328
+ </TableCell>
329
+ <TableCell className="text-right">
330
+ <Money value={item.realizado} />
331
+ </TableCell>
332
+ <TableCell
333
+ className={`text-right font-medium ${item.desvio >= 0 ? 'text-green-600' : 'text-red-600'}`}
334
+ >
335
+ <Money value={item.desvio} showSign />
336
+ </TableCell>
337
+ <TableCell
338
+ className={`text-right font-medium ${item.percentual >= 0 ? 'text-green-600' : 'text-red-600'}`}
339
+ >
340
+ {item.percentual.toFixed(1)}%
341
+ </TableCell>
342
+ <TableCell>
343
+ <Badge variant={statusVariant}>
344
+ {item.desvio > 0
345
+ ? t('status.above')
346
+ : item.desvio < 0
347
+ ? t('status.below')
348
+ : t('status.onTarget')}
349
+ </Badge>
350
+ </TableCell>
351
+ </TableRow>
352
+ );
353
+ })
354
+ )}
355
+ </TableBody>
356
+ </Table>
357
+ </CardContent>
358
+ </Card>
359
+ </Page>
360
+ );
361
+ }