@finopsbricks/tally-sync 0.0.0-stage → 0.1.0

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 (125) hide show
  1. package/.next/BUILD_ID +1 -0
  2. package/.next/app-build-manifest.json +66 -0
  3. package/.next/app-path-routes-manifest.json +7 -0
  4. package/.next/build-manifest.json +33 -0
  5. package/.next/export-marker.json +6 -0
  6. package/.next/images-manifest.json +58 -0
  7. package/.next/next-minimal-server.js.nft.json +1 -0
  8. package/.next/next-server.js.nft.json +1 -0
  9. package/.next/package.json +1 -0
  10. package/.next/prerender-manifest.json +11 -0
  11. package/.next/react-loadable-manifest.json +1 -0
  12. package/.next/required-server-files.json +322 -0
  13. package/.next/routes-manifest.json +80 -0
  14. package/.next/server/app/_not-found/page.js +2 -0
  15. package/.next/server/app/_not-found/page.js.nft.json +1 -0
  16. package/.next/server/app/_not-found/page_client-reference-manifest.js +1 -0
  17. package/.next/server/app/companies/[guid]/page.js +2 -0
  18. package/.next/server/app/companies/[guid]/page.js.nft.json +1 -0
  19. package/.next/server/app/companies/[guid]/page_client-reference-manifest.js +1 -0
  20. package/.next/server/app/companies/[guid]/tables/[table]/page.js +2 -0
  21. package/.next/server/app/companies/[guid]/tables/[table]/page.js.nft.json +1 -0
  22. package/.next/server/app/companies/[guid]/tables/[table]/page_client-reference-manifest.js +1 -0
  23. package/.next/server/app/companies/[guid]/vouchers/[voucher_guid]/page.js +2 -0
  24. package/.next/server/app/companies/[guid]/vouchers/[voucher_guid]/page.js.nft.json +1 -0
  25. package/.next/server/app/companies/[guid]/vouchers/[voucher_guid]/page_client-reference-manifest.js +1 -0
  26. package/.next/server/app/page.js +2 -0
  27. package/.next/server/app/page.js.nft.json +1 -0
  28. package/.next/server/app/page_client-reference-manifest.js +1 -0
  29. package/.next/server/app-paths-manifest.json +7 -0
  30. package/.next/server/chunks/230.js +32 -0
  31. package/.next/server/chunks/611.js +6 -0
  32. package/.next/server/chunks/706.js +1 -0
  33. package/.next/server/chunks/748.js +1 -0
  34. package/.next/server/functions-config-manifest.json +4 -0
  35. package/.next/server/interception-route-rewrite-manifest.js +1 -0
  36. package/.next/server/middleware-build-manifest.js +1 -0
  37. package/.next/server/middleware-manifest.json +6 -0
  38. package/.next/server/middleware-react-loadable-manifest.js +1 -0
  39. package/.next/server/next-font-manifest.js +1 -0
  40. package/.next/server/next-font-manifest.json +1 -0
  41. package/.next/server/pages/500.html +1 -0
  42. package/.next/server/pages/_app.js +1 -0
  43. package/.next/server/pages/_app.js.nft.json +1 -0
  44. package/.next/server/pages/_document.js +1 -0
  45. package/.next/server/pages/_document.js.nft.json +1 -0
  46. package/.next/server/pages/_error.js +19 -0
  47. package/.next/server/pages/_error.js.nft.json +1 -0
  48. package/.next/server/pages-manifest.json +5 -0
  49. package/.next/server/server-reference-manifest.js +1 -0
  50. package/.next/server/server-reference-manifest.json +1 -0
  51. package/.next/server/webpack-runtime.js +1 -0
  52. package/.next/static/V0rxN1KuyEM0zJoOJVDoX/_buildManifest.js +1 -0
  53. package/.next/static/V0rxN1KuyEM0zJoOJVDoX/_ssgManifest.js +1 -0
  54. package/.next/static/chunks/122-df645619bf29fc6e.js +1 -0
  55. package/.next/static/chunks/255-73938ce8a8eac1ad.js +1 -0
  56. package/.next/static/chunks/4bd1b696-c023c6e3521b1417.js +1 -0
  57. package/.next/static/chunks/510-5993a923674db2de.js +1 -0
  58. package/.next/static/chunks/658-dfe2290e75772c42.js +1 -0
  59. package/.next/static/chunks/788-e10fcb5f53c6428d.js +1 -0
  60. package/.next/static/chunks/9ad0e60a-ad37bddef782d7c2.js +1 -0
  61. package/.next/static/chunks/app/_not-found/page-f187ad254cc1b0eb.js +1 -0
  62. package/.next/static/chunks/app/companies/[guid]/page-042071b4790e1a86.js +1 -0
  63. package/.next/static/chunks/app/companies/[guid]/tables/[table]/page-a46008185adae0f8.js +1 -0
  64. package/.next/static/chunks/app/companies/[guid]/vouchers/[voucher_guid]/page-1b17cd59db180e1f.js +1 -0
  65. package/.next/static/chunks/app/layout-d46ad692723a97e0.js +1 -0
  66. package/.next/static/chunks/app/not-found-61f58c9ef91de921.js +1 -0
  67. package/.next/static/chunks/app/page-042071b4790e1a86.js +1 -0
  68. package/.next/static/chunks/framework-acd67e14855de5a2.js +1 -0
  69. package/.next/static/chunks/main-6f56cad8fb2c15f2.js +1 -0
  70. package/.next/static/chunks/main-app-f136a4b0e7f8f69b.js +1 -0
  71. package/.next/static/chunks/pages/_app-7d307437aca18ad4.js +1 -0
  72. package/.next/static/chunks/pages/_error-cb2a52f75f2162e2.js +1 -0
  73. package/.next/static/chunks/polyfills-42372ed130431b0a.js +1 -0
  74. package/.next/static/chunks/webpack-4354c1f6ffe64d9d.js +1 -0
  75. package/.next/static/css/a8e56a10001bee9f.css +1 -0
  76. package/CHANGELOG.md +16 -0
  77. package/LICENSE +201 -0
  78. package/NOTICE +40 -0
  79. package/README.md +109 -2
  80. package/bin/tally-sync.js +12 -0
  81. package/ecosystem.config.cjs +12 -0
  82. package/next.config.mjs +7 -0
  83. package/package.json +58 -4
  84. package/src/app/companies/[guid]/page.jsx +261 -0
  85. package/src/app/companies/[guid]/tables/[table]/page.jsx +116 -0
  86. package/src/app/companies/[guid]/vouchers/[voucher_guid]/page.jsx +156 -0
  87. package/src/app/globals.css +3 -0
  88. package/src/app/layout.jsx +29 -0
  89. package/src/app/not-found.jsx +22 -0
  90. package/src/app/page.jsx +172 -0
  91. package/src/cli/_helpers.js +71 -0
  92. package/src/cli/index.js +56 -0
  93. package/src/cli/paths.js +26 -0
  94. package/src/cli/probe.js +61 -0
  95. package/src/cli/sync.js +84 -0
  96. package/src/cli/web.js +32 -0
  97. package/src/components/AppShell.jsx +17 -0
  98. package/src/components/AppSidebar.jsx +81 -0
  99. package/src/components/MainContainer.jsx +12 -0
  100. package/src/components/RowsTable.jsx +111 -0
  101. package/src/components/Section.jsx +16 -0
  102. package/src/mirror/agentDb.js +131 -0
  103. package/src/mirror/companyDb.js +54 -0
  104. package/src/mirror/schema.js +49 -0
  105. package/src/mirror/sqlite.js +41 -0
  106. package/src/probe/probeFile.js +54 -0
  107. package/src/probe/runProbe.js +118 -0
  108. package/src/sync/checkPull.js +61 -0
  109. package/src/sync/pullCompany.js +54 -0
  110. package/src/sync/runSyncPass.js +50 -0
  111. package/src/sync/syncCompany.js +75 -0
  112. package/src/sync/tieOut.js +93 -0
  113. package/src/tally/dates.js +23 -0
  114. package/src/tally/definitions.js +22 -0
  115. package/src/tally/definitions.yaml +370 -0
  116. package/src/tally/gateway.js +77 -0
  117. package/src/tally/http.js +92 -0
  118. package/src/tally/requests.js +133 -0
  119. package/src/tally/tallyErrors.js +25 -0
  120. package/src/tally/xml.js +329 -0
  121. package/src/utils/config.js +46 -0
  122. package/src/utils/debugLog.js +23 -0
  123. package/src/web/format.js +160 -0
  124. package/src/web/mirror.js +132 -0
  125. package/src/web/status.js +80 -0
@@ -0,0 +1,261 @@
1
+ import Link from 'next/link';
2
+ import { notFound } from 'next/navigation';
3
+ import AlertBox from '@fob/lib-ui/components/AlertBox';
4
+ import AppBreadcrumbs from '@fob/lib-ui/components/AppBreadcrumbs';
5
+ import PageHeader from '@fob/lib-ui/components/PageHeader';
6
+ import { Badge } from '@fob/lib-ui/primitives/badge';
7
+ import MainContainer from '../../../components/MainContainer.jsx';
8
+ import RowsTable from '../../../components/RowsTable.jsx';
9
+ import Section from '../../../components/Section.jsx';
10
+ import { readAgentStatus, readRecentRuns } from '../../../web/status.js';
11
+ import { readCompanyMirror } from '../../../web/mirror.js';
12
+ import { formatAgo, formatDate, formatDateTime, formatDuration, formatMoney, runDetail, runStatus, tableLabel, tieOutVerdict } from '../../../web/format.js';
13
+
14
+ export const dynamic = 'force-dynamic';
15
+
16
+ const COUNT = new Intl.NumberFormat('en-IN');
17
+
18
+ /**
19
+ * One company: what the mirror holds, whether it ties out to Tally, its recent syncs and
20
+ * its tables.
21
+ * @param {{params: Promise<{guid: string}>}} props
22
+ */
23
+ export default async function CompanyPage({ params }) {
24
+ const { guid: raw_guid } = await params;
25
+ const guid = decodeURIComponent(raw_guid);
26
+ const data_dir = process.env.TALLY_SYNC_DATA_DIR ?? '';
27
+ const status = readAgentStatus(data_dir);
28
+ const company = status.companies.find((c) => c.guid === guid);
29
+ const mirror = readCompanyMirror(data_dir, guid);
30
+ if (!company && !mirror) notFound();
31
+
32
+ const name = mirror?.company.name ?? company?.name ?? guid;
33
+ const runs = readRecentRuns(data_dir, guid);
34
+ const latest = runs[0] ?? null;
35
+ const latest_status = runStatus(latest?.status);
36
+ const href = `/companies/${encodeURIComponent(guid)}`;
37
+
38
+ const meta = [
39
+ company && (company.loaded ? 'Loaded in Tally' : 'Not loaded in Tally now'),
40
+ mirror?.company.books_from && `Books from ${formatDate(mirror.company.books_from)}`,
41
+ mirror?.company.last_voucher_date && `Last voucher ${formatDate(mirror.company.last_voucher_date)}`,
42
+ mirror && `Synced ${formatDateTime(mirror.company.synced_at)}`,
43
+ company?.last_checked_at && `Checked with Tally ${formatAgo(company.last_checked_at)}`,
44
+ ].filter(Boolean);
45
+
46
+ return (
47
+ <>
48
+ <AppBreadcrumbs breadcrumbs={[{ text: 'Companies', href: '/' }, { text: name }]} />
49
+ <MainContainer>
50
+ <div className="flex flex-col gap-1">
51
+ <PageHeader header={name} RightButtons={<Badge variant={latest_status.variant}>{latest_status.label}</Badge>} />
52
+ {meta.length > 0 && <p className="text-sm text-muted-foreground">{meta.join(' · ')}</p>}
53
+ </div>
54
+
55
+ {latest?.status === 'failed' && <FailedRun run={latest} synced_at={mirror?.company.synced_at ?? null} />}
56
+ {!mirror && latest?.status !== 'failed' && (
57
+ <AlertBox color="info" title="Not synced yet" message="This company has no mirror yet. It is synced on the next pass while it's loaded in Tally." />
58
+ )}
59
+
60
+ {mirror && <TieOut tie_out={mirror.tie_out} />}
61
+
62
+ <Section title="Recent syncs" id="recent-syncs">
63
+ <RecentRuns runs={runs} />
64
+ </Section>
65
+
66
+ {mirror && (
67
+ <Section title="Tables" id="tables">
68
+ <div className="grid gap-4 sm:grid-cols-2">
69
+ {['master', 'transaction'].map((kind) => (
70
+ <RowsTable
71
+ key={kind}
72
+ label={kind === 'master' ? 'Masters' : 'Transactions'}
73
+ columns={[
74
+ { key: 'name', label: kind === 'master' ? 'Masters' : 'Transactions' },
75
+ { key: 'rows', label: 'Rows', align: 'right' },
76
+ ]}
77
+ rows={mirror.tables
78
+ .filter((t) => t.kind === kind)
79
+ .map((t) => ({
80
+ key: t.name,
81
+ cells: {
82
+ name: (
83
+ <Link href={`${href}/tables/${t.name}`} className="text-primary hover:underline">
84
+ {tableLabel(t.name)}
85
+ </Link>
86
+ ),
87
+ rows: COUNT.format(t.rows),
88
+ },
89
+ }))}
90
+ />
91
+ ))}
92
+ </div>
93
+ </Section>
94
+ )}
95
+ </MainContainer>
96
+ </>
97
+ );
98
+ }
99
+
100
+ /**
101
+ * Why the latest sync failed, and what the page shows instead.
102
+ * @param {{run: Record<string, any>, synced_at: string|null}} props
103
+ */
104
+ function FailedRun({ run, synced_at }) {
105
+ const { problems } = runDetail(run);
106
+ const reasons = problems.length > 0 ? problems : [String(run.error ?? 'The sync stopped with no reason given.')];
107
+ return (
108
+ <AlertBox
109
+ color="danger"
110
+ title={`The last sync failed, ${formatAgo(run.finished_at ?? run.started_at)}`}
111
+ message={
112
+ <div className="flex flex-col gap-1">
113
+ {reasons.length === 1 ? (
114
+ <p>{reasons[0]}</p>
115
+ ) : (
116
+ <ul className="list-inside list-disc">
117
+ {reasons.map((r) => (
118
+ <li key={r}>{r}</li>
119
+ ))}
120
+ </ul>
121
+ )}
122
+ <p>{synced_at ? `This page shows the last sync that worked, from ${formatDateTime(synced_at)}.` : 'Nothing has been mirrored yet.'}</p>
123
+ </div>
124
+ }
125
+ />
126
+ );
127
+ }
128
+
129
+ /**
130
+ * Does every ledger's balance in the mirror equal Tally's closing balance?
131
+ * @param {{tie_out: import('../../../sync/tieOut.js').TieOut|null}} props
132
+ */
133
+ function TieOut({ tie_out }) {
134
+ const verdict = tieOutVerdict(tie_out);
135
+ return (
136
+ <Section title="Tie-out" id="tie-out">
137
+ {!tie_out || !verdict ? (
138
+ <p className="text-sm text-muted-foreground">This sync has no tie-out.</p>
139
+ ) : (
140
+ <>
141
+ <div className="flex flex-wrap items-center gap-2 text-sm">
142
+ <Badge variant={verdict.variant}>{verdict.label}</Badge>
143
+ <span>
144
+ {tie_out.matched
145
+ ? `In sync: every ledger matches Tally (${COUNT.format(tie_out.ledgers)} checked).`
146
+ : `${tie_out.differences.length} of ${COUNT.format(tie_out.ledgers)} ledgers don't match Tally's closing balance.`}
147
+ </span>
148
+ </div>
149
+ {tie_out.differences.length > 0 && (
150
+ <RowsTable
151
+ label="Ledgers that differ from Tally"
152
+ cards
153
+ columns={[
154
+ { key: 'ledger', label: 'Ledger' },
155
+ { key: 'ours', label: 'Mirror', align: 'right' },
156
+ { key: 'tally', label: 'Tally', align: 'right' },
157
+ { key: 'difference', label: 'Difference', align: 'right' },
158
+ ]}
159
+ rows={tie_out.differences.map((d) => ({
160
+ key: d.ledger,
161
+ cells: {
162
+ ledger: d.ledger,
163
+ ours: formatMoney(d.ours_paise),
164
+ tally: formatMoney(d.tally_paise),
165
+ difference: formatMoney(d.ours_paise - d.tally_paise),
166
+ },
167
+ card: {
168
+ title: d.ledger,
169
+ value: formatMoney(d.ours_paise - d.tally_paise),
170
+ meta: [`Mirror ${formatMoney(d.ours_paise)}`, `Tally ${formatMoney(d.tally_paise)}`],
171
+ },
172
+ }))}
173
+ />
174
+ )}
175
+ <TieOutNotes tie_out={tie_out} />
176
+ </>
177
+ )}
178
+ </Section>
179
+ );
180
+ }
181
+
182
+ /**
183
+ * The parts of the tie-out that don't decide it: Profit & Loss A/c, and vouchers left out.
184
+ * @param {{tie_out: import('../../../sync/tieOut.js').TieOut}} props
185
+ */
186
+ function TieOutNotes({ tie_out }) {
187
+ const notes = [];
188
+ const pl = tie_out.profit_and_loss;
189
+ if (pl && !pl.matched) {
190
+ notes.push(
191
+ `Profit & Loss A/c is ${formatMoney(pl.ours_paise)} here and ${formatMoney(pl.tally_paise)} in Tally. Tally's figure includes the change in stock, which isn't synced, so this ledger is checked apart.`,
192
+ );
193
+ }
194
+ if (tie_out.vouchers_left_out > 0) {
195
+ const n = tie_out.vouchers_left_out;
196
+ notes.push(`${COUNT.format(n)} memorandum or reversing journal ${n === 1 ? 'voucher was' : 'vouchers were'} left out: they don't post to the books.`);
197
+ }
198
+ if (notes.length === 0) return null;
199
+ return (
200
+ <ul className="flex flex-col gap-1 text-sm text-muted-foreground">
201
+ {notes.map((note) => (
202
+ <li key={note}>{note}</li>
203
+ ))}
204
+ </ul>
205
+ );
206
+ }
207
+
208
+ /**
209
+ * The company's latest sync runs, newest first, with why any of them failed.
210
+ * @param {{runs: Record<string, any>[]}} props
211
+ */
212
+ function RecentRuns({ runs }) {
213
+ if (runs.length === 0) return <p className="text-sm text-muted-foreground">No syncs yet.</p>;
214
+ return (
215
+ <RowsTable
216
+ label="Recent syncs"
217
+ cards
218
+ columns={[
219
+ { key: 'started', label: 'Started', nowrap: true },
220
+ { key: 'status', label: 'Status' },
221
+ { key: 'took', label: 'Took', nowrap: true },
222
+ { key: 'vouchers', label: 'Vouchers', align: 'right' },
223
+ { key: 'ledgers', label: 'Ledgers', align: 'right' },
224
+ ...(runs.some((run) => run.status === 'failed') ? [{ key: 'problem', label: 'What went wrong' }] : []),
225
+ ]}
226
+ rows={runs.map((run) => {
227
+ const s = runStatus(run.status);
228
+ const { counts, problems } = runDetail(run);
229
+ const reasons = run.status === 'failed' ? (problems.length > 0 ? problems : [String(run.error ?? '')]) : [];
230
+ const vouchers = counts?.trn_voucher === undefined ? '' : COUNT.format(counts.trn_voucher);
231
+ const ledgers = counts?.mst_ledger === undefined ? '' : COUNT.format(counts.mst_ledger);
232
+ const took = formatDuration(run.started_at, run.finished_at);
233
+ const problem_list =
234
+ reasons.length > 0 ? (
235
+ <ul className="flex flex-col gap-1 text-destructive">
236
+ {reasons.map((r) => (
237
+ <li key={r}>{r}</li>
238
+ ))}
239
+ </ul>
240
+ ) : null;
241
+ return {
242
+ key: String(run.id),
243
+ cells: {
244
+ started: formatDateTime(run.started_at),
245
+ status: <Badge variant={s.variant}>{s.label}</Badge>,
246
+ took,
247
+ vouchers,
248
+ ledgers,
249
+ problem: problem_list,
250
+ },
251
+ card: {
252
+ title: formatDateTime(run.started_at),
253
+ value: <Badge variant={s.variant}>{s.label}</Badge>,
254
+ meta: [took && `Took ${took}`, vouchers && `${vouchers} vouchers`, ledgers && `${ledgers} ledgers`],
255
+ extra: problem_list,
256
+ },
257
+ };
258
+ })}
259
+ />
260
+ );
261
+ }
@@ -0,0 +1,116 @@
1
+ import Link from 'next/link';
2
+ import { notFound } from 'next/navigation';
3
+ import { Search } from 'lucide-react';
4
+ import AlertBox from '@fob/lib-ui/components/AlertBox';
5
+ import AppBreadcrumbs from '@fob/lib-ui/components/AppBreadcrumbs';
6
+ import PageHeader from '@fob/lib-ui/components/PageHeader';
7
+ import Pagination from '@fob/lib-ui/components/Pagination';
8
+ import { Button } from '@fob/lib-ui/primitives/button';
9
+ import { Input } from '@fob/lib-ui/primitives/input';
10
+ import MainContainer from '../../../../../components/MainContainer.jsx';
11
+ import RowsTable from '../../../../../components/RowsTable.jsx';
12
+ import { readCompanyMirror, readTablePage } from '../../../../../web/mirror.js';
13
+ import { formatDate, formatMoney, tableLabel } from '../../../../../web/format.js';
14
+
15
+ export const dynamic = 'force-dynamic';
16
+
17
+ const COUNT = new Intl.NumberFormat('en-IN');
18
+
19
+ /**
20
+ * Any mirror table, a page of rows at a time, with a search over its text columns. The
21
+ * page and the search are GET params, so the page renders on the server and a link keeps them.
22
+ * @param {{params: Promise<{guid: string, table: string}>, searchParams: Promise<{page?: string, q?: string}>}} props
23
+ */
24
+ export default async function TablePage({ params, searchParams }) {
25
+ const { guid: raw_guid, table } = await params;
26
+ const { page: page_param, q = '' } = await searchParams;
27
+ const guid = decodeURIComponent(raw_guid);
28
+ const data_dir = process.env.TALLY_SYNC_DATA_DIR ?? '';
29
+ const search = String(q).trim();
30
+ const result = readTablePage(data_dir, guid, table, { page: Number(page_param) || 1, search });
31
+ const mirror = result && readCompanyMirror(data_dir, guid);
32
+ if (!result || !mirror) notFound();
33
+
34
+ const company_href = `/companies/${encodeURIComponent(guid)}`;
35
+ const label = tableLabel(table);
36
+ const table_rows = mirror.tables.find((t) => t.name === table)?.rows ?? 0;
37
+
38
+ return (
39
+ <>
40
+ <AppBreadcrumbs breadcrumbs={[{ text: 'Companies', href: '/' }, { text: String(mirror.company.name), href: company_href }, { text: label }]} />
41
+ <MainContainer>
42
+ <div className="flex flex-col gap-1">
43
+ <PageHeader header={label} />
44
+ <p className="text-sm text-muted-foreground">
45
+ <code>{table}</code> · {COUNT.format(table_rows)} {table_rows === 1 ? 'row' : 'rows'}
46
+ </p>
47
+ </div>
48
+
49
+ {table_rows > 0 && (
50
+ <form method="get" role="search" className="flex max-w-xl items-center gap-2">
51
+ <Input type="search" name="q" defaultValue={search} placeholder="Search names, numbers, narration…" aria-label={`Search ${label.toLowerCase()}`} />
52
+ <Button type="submit" variant="outline">
53
+ <Search aria-hidden="true" />
54
+ Search
55
+ </Button>
56
+ </form>
57
+ )}
58
+
59
+ {result.total === 0 ? (
60
+ search ? (
61
+ <div className="flex flex-col items-start gap-2">
62
+ <AlertBox color="info" title={`No ${label.toLowerCase()} match “${search}”`} message="The search looks in every text column." />
63
+ <Button asChild variant="outline" size="sm">
64
+ <Link href={`${company_href}/tables/${table}`}>Clear search</Link>
65
+ </Button>
66
+ </div>
67
+ ) : (
68
+ <AlertBox color="info" title={`No ${label.toLowerCase()} in this company`} message="Tally has none of these for this company, as of the last sync." />
69
+ )
70
+ ) : (
71
+ <>
72
+ <RowsTable
73
+ label={label}
74
+ columns={result.columns.map((c) => ({
75
+ key: c,
76
+ label: c.endsWith('_paise') ? c.slice(0, -'_paise'.length) : c,
77
+ align: c.endsWith('_paise') ? 'right' : undefined,
78
+ nowrap: true,
79
+ }))}
80
+ rows={result.rows.map((row, i) => ({
81
+ key: `${result.page}-${i}`,
82
+ cells: Object.fromEntries(result.columns.map((c) => [c, cell(table, c, row[c], company_href)])),
83
+ }))}
84
+ />
85
+ <Pagination total={result.total} pageSize={result.page_size} page={result.page} />
86
+ </>
87
+ )}
88
+ </MainContainer>
89
+ </>
90
+ );
91
+ }
92
+
93
+ /**
94
+ * One value as the browser shows it: money with Dr/Cr, dates in words, flags as Yes/No,
95
+ * and a transaction's voucher GUID as a link to the voucher.
96
+ * @param {string} table
97
+ * @param {string} column
98
+ * @param {unknown} value
99
+ * @param {string} company_href
100
+ * @returns {React.ReactNode}
101
+ */
102
+ function cell(table, column, value, company_href) {
103
+ if (value === null || value === undefined || value === '') return '';
104
+ if (column.endsWith('_paise')) return formatMoney(Number(value));
105
+ if (column === 'date' || column.endsWith('_date')) return formatDate(String(value));
106
+ if (column.startsWith('is_') || column.startsWith('affects_')) return Number(value) ? 'Yes' : 'No';
107
+ if (column === 'guid' && table.startsWith('trn_')) {
108
+ return (
109
+ <Link href={`${company_href}/vouchers/${encodeURIComponent(String(value))}`} className="text-primary hover:underline">
110
+ {String(value)}
111
+ </Link>
112
+ );
113
+ }
114
+ const text = String(value);
115
+ return text.length > 40 ? <span className="block w-max max-w-sm whitespace-normal">{text}</span> : text;
116
+ }
@@ -0,0 +1,156 @@
1
+ import { notFound } from 'next/navigation';
2
+ import AppBreadcrumbs from '@fob/lib-ui/components/AppBreadcrumbs';
3
+ import PageHeader from '@fob/lib-ui/components/PageHeader';
4
+ import MainContainer from '../../../../../components/MainContainer.jsx';
5
+ import RowsTable from '../../../../../components/RowsTable.jsx';
6
+ import Section from '../../../../../components/Section.jsx';
7
+ import { readCompanyMirror, readVoucher } from '../../../../../web/mirror.js';
8
+ import { formatDate, formatMoney } from '../../../../../web/format.js';
9
+
10
+ export const dynamic = 'force-dynamic';
11
+
12
+ /**
13
+ * One voucher as Tally has it: the header, its ledger lines with Dr/Cr, and its bill, bank
14
+ * and cost-centre allocations.
15
+ * @param {{params: Promise<{guid: string, voucher_guid: string}>}} props
16
+ */
17
+ export default async function VoucherPage({ params }) {
18
+ const { guid: raw_guid, voucher_guid: raw_voucher_guid } = await params;
19
+ const guid = decodeURIComponent(raw_guid);
20
+ const voucher_guid = decodeURIComponent(raw_voucher_guid);
21
+ const data_dir = process.env.TALLY_SYNC_DATA_DIR ?? '';
22
+ const detail = readVoucher(data_dir, guid, voucher_guid);
23
+ const mirror = detail && readCompanyMirror(data_dir, guid);
24
+ if (!detail || !mirror) notFound();
25
+
26
+ const { voucher } = detail;
27
+ const company_href = `/companies/${encodeURIComponent(guid)}`;
28
+ const number = voucher.voucher_number ? String(voucher.voucher_number) : 'No number';
29
+ const title = `${voucher.voucher_type} ${number}`;
30
+ const meta = [
31
+ formatDate(String(voucher.date ?? '')),
32
+ voucher.party_name && `Party: ${voucher.party_name}`,
33
+ voucher.reference_number && `Ref ${voucher.reference_number}${voucher.reference_date ? ` of ${formatDate(String(voucher.reference_date))}` : ''}`,
34
+ voucher.place_of_supply && `Place of supply: ${voucher.place_of_supply}`,
35
+ ].filter(Boolean);
36
+ const money = (/** @type {unknown} */ paise) => formatMoney(Number(paise));
37
+
38
+ return (
39
+ <>
40
+ <AppBreadcrumbs
41
+ breadcrumbs={[
42
+ { text: 'Companies', href: '/' },
43
+ { text: String(mirror.company.name), href: company_href },
44
+ { text: 'Vouchers', href: `${company_href}/tables/trn_voucher` },
45
+ { text: title },
46
+ ]}
47
+ />
48
+ <MainContainer>
49
+ <div className="flex flex-col gap-1">
50
+ <PageHeader header={{ code: String(voucher.voucher_type), name: number }} />
51
+ <p className="text-sm text-muted-foreground">{meta.join(' · ')}</p>
52
+ </div>
53
+ {voucher.narration && <p className="max-w-prose text-sm">{String(voucher.narration)}</p>}
54
+
55
+ <Section title="Ledger lines" id="ledger-lines">
56
+ {detail.lines.length === 0 ? (
57
+ <p className="text-sm text-muted-foreground">This voucher has no ledger lines.</p>
58
+ ) : (
59
+ <RowsTable
60
+ label="Ledger lines"
61
+ columns={[
62
+ { key: 'ledger', label: 'Ledger' },
63
+ { key: 'amount', label: 'Amount', align: 'right' },
64
+ ]}
65
+ rows={detail.lines.map((line, i) => ({
66
+ key: String(i),
67
+ cells: { ledger: String(line.ledger), amount: money(line.amount_paise) },
68
+ }))}
69
+ />
70
+ )}
71
+ </Section>
72
+
73
+ {detail.bills.length > 0 && (
74
+ <Section title="Bill allocations" id="bills">
75
+ <RowsTable
76
+ label="Bill allocations"
77
+ cards
78
+ columns={[
79
+ { key: 'ledger', label: 'Ledger' },
80
+ { key: 'name', label: 'Bill' },
81
+ { key: 'type', label: 'Type', nowrap: true },
82
+ { key: 'amount', label: 'Amount', align: 'right' },
83
+ ]}
84
+ rows={detail.bills.map((bill, i) => ({
85
+ key: String(i),
86
+ cells: { ledger: String(bill.ledger), name: String(bill.name ?? ''), type: String(bill.billtype ?? ''), amount: money(bill.amount_paise) },
87
+ card: { title: String(bill.ledger), value: money(bill.amount_paise), meta: [String(bill.name ?? ''), String(bill.billtype ?? '')] },
88
+ }))}
89
+ />
90
+ </Section>
91
+ )}
92
+
93
+ {detail.bank.length > 0 && (
94
+ <Section title="Bank allocations" id="bank">
95
+ <RowsTable
96
+ label="Bank allocations"
97
+ cards
98
+ columns={[
99
+ { key: 'ledger', label: 'Ledger' },
100
+ { key: 'type', label: 'Type', nowrap: true },
101
+ { key: 'instrument', label: 'Instrument', nowrap: true },
102
+ { key: 'instrument_date', label: 'Instrument date', nowrap: true },
103
+ { key: 'bankers_date', label: "Banker's date", nowrap: true },
104
+ { key: 'amount', label: 'Amount', align: 'right' },
105
+ ]}
106
+ rows={detail.bank.map((b, i) => ({
107
+ key: String(i),
108
+ cells: {
109
+ ledger: String(b.ledger),
110
+ type: String(b.transaction_type ?? ''),
111
+ instrument: String(b.instrument_number ?? ''),
112
+ instrument_date: formatDate(/** @type {string} */ (b.instrument_date)),
113
+ bankers_date: formatDate(/** @type {string} */ (b.bankers_date)),
114
+ amount: money(b.amount_paise),
115
+ },
116
+ card: {
117
+ title: String(b.ledger),
118
+ value: money(b.amount_paise),
119
+ meta: [
120
+ String(b.transaction_type ?? ''),
121
+ b.instrument_number && `No. ${b.instrument_number}`,
122
+ b.instrument_date && formatDate(/** @type {string} */ (b.instrument_date)),
123
+ b.bankers_date && `Banker's date ${formatDate(/** @type {string} */ (b.bankers_date))}`,
124
+ ],
125
+ },
126
+ }))}
127
+ />
128
+ </Section>
129
+ )}
130
+
131
+ {detail.cost_centres.length > 0 && (
132
+ <Section title="Cost-centre allocations" id="cost-centres">
133
+ <RowsTable
134
+ label="Cost-centre allocations"
135
+ cards
136
+ columns={[
137
+ { key: 'ledger', label: 'Ledger' },
138
+ { key: 'costcentre', label: 'Cost centre' },
139
+ { key: 'amount', label: 'Amount', align: 'right' },
140
+ ]}
141
+ rows={detail.cost_centres.map((c, i) => ({
142
+ key: String(i),
143
+ cells: { ledger: String(c.ledger), costcentre: String(c.costcentre ?? ''), amount: money(c.amount_paise) },
144
+ card: { title: String(c.ledger), value: money(c.amount_paise), meta: [String(c.costcentre ?? '')] },
145
+ }))}
146
+ />
147
+ </Section>
148
+ )}
149
+
150
+ {detail.bills.length + detail.bank.length + detail.cost_centres.length === 0 && (
151
+ <p className="text-sm text-muted-foreground">No bill, bank or cost-centre allocations.</p>
152
+ )}
153
+ </MainContainer>
154
+ </>
155
+ );
156
+ }
@@ -0,0 +1,3 @@
1
+ /* Theme tokens (light + .dark), Tailwind and base styles live in lib-ui. */
2
+ @import "@fob/lib-ui/styles/globals.css";
3
+ @source "../../node_modules/@fob/lib-ui/src/**/*.{js,jsx}";
@@ -0,0 +1,29 @@
1
+ import './globals.css';
2
+ import AppShell from '../components/AppShell.jsx';
3
+ import { readAgentStatus } from '../web/status.js';
4
+ import pkg from '../../package.json';
5
+
6
+ export const metadata = {
7
+ title: 'Tally Sync',
8
+ description: 'What tally-sync has mirrored from Tally on this PC',
9
+ };
10
+
11
+ export const dynamic = 'force-dynamic';
12
+
13
+ /**
14
+ * The app shell around every page. Reads only the local agent.db (for the sidebar's
15
+ * companies) and loads nothing from the network: no web fonts, no analytics, no CDN.
16
+ * @param {{children: React.ReactNode}} props
17
+ */
18
+ export default function RootLayout({ children }) {
19
+ const { companies } = readAgentStatus(process.env.TALLY_SYNC_DATA_DIR ?? '');
20
+ return (
21
+ <html lang="en">
22
+ <body>
23
+ <AppShell companies={companies.map(({ guid, name }) => ({ guid, name }))} version={pkg.version}>
24
+ {children}
25
+ </AppShell>
26
+ </body>
27
+ </html>
28
+ );
29
+ }
@@ -0,0 +1,22 @@
1
+ import AlertBox from '@fob/lib-ui/components/AlertBox';
2
+ import AppBreadcrumbs from '@fob/lib-ui/components/AppBreadcrumbs';
3
+ import Link from 'next/link';
4
+ import { Button } from '@fob/lib-ui/primitives/button';
5
+ import MainContainer from '../components/MainContainer.jsx';
6
+
7
+ /** A company, table or voucher the mirror doesn't have. */
8
+ export default function NotFound() {
9
+ return (
10
+ <>
11
+ <AppBreadcrumbs breadcrumbs={[{ text: 'Companies', href: '/' }, { text: 'Not found' }]} />
12
+ <MainContainer>
13
+ <div className="flex flex-col items-start gap-2">
14
+ <AlertBox color="info" title="Not in the mirror" message="This company, table or voucher isn't in what tally-sync has mirrored on this PC." />
15
+ <Button asChild variant="outline" size="sm">
16
+ <Link href="/">All companies</Link>
17
+ </Button>
18
+ </div>
19
+ </MainContainer>
20
+ </>
21
+ );
22
+ }