@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.
- package/.next/BUILD_ID +1 -0
- package/.next/app-build-manifest.json +66 -0
- package/.next/app-path-routes-manifest.json +7 -0
- package/.next/build-manifest.json +33 -0
- package/.next/export-marker.json +6 -0
- package/.next/images-manifest.json +58 -0
- package/.next/next-minimal-server.js.nft.json +1 -0
- package/.next/next-server.js.nft.json +1 -0
- package/.next/package.json +1 -0
- package/.next/prerender-manifest.json +11 -0
- package/.next/react-loadable-manifest.json +1 -0
- package/.next/required-server-files.json +322 -0
- package/.next/routes-manifest.json +80 -0
- package/.next/server/app/_not-found/page.js +2 -0
- package/.next/server/app/_not-found/page.js.nft.json +1 -0
- package/.next/server/app/_not-found/page_client-reference-manifest.js +1 -0
- package/.next/server/app/companies/[guid]/page.js +2 -0
- package/.next/server/app/companies/[guid]/page.js.nft.json +1 -0
- package/.next/server/app/companies/[guid]/page_client-reference-manifest.js +1 -0
- package/.next/server/app/companies/[guid]/tables/[table]/page.js +2 -0
- package/.next/server/app/companies/[guid]/tables/[table]/page.js.nft.json +1 -0
- package/.next/server/app/companies/[guid]/tables/[table]/page_client-reference-manifest.js +1 -0
- package/.next/server/app/companies/[guid]/vouchers/[voucher_guid]/page.js +2 -0
- package/.next/server/app/companies/[guid]/vouchers/[voucher_guid]/page.js.nft.json +1 -0
- package/.next/server/app/companies/[guid]/vouchers/[voucher_guid]/page_client-reference-manifest.js +1 -0
- package/.next/server/app/page.js +2 -0
- package/.next/server/app/page.js.nft.json +1 -0
- package/.next/server/app/page_client-reference-manifest.js +1 -0
- package/.next/server/app-paths-manifest.json +7 -0
- package/.next/server/chunks/230.js +32 -0
- package/.next/server/chunks/611.js +6 -0
- package/.next/server/chunks/706.js +1 -0
- package/.next/server/chunks/748.js +1 -0
- package/.next/server/functions-config-manifest.json +4 -0
- package/.next/server/interception-route-rewrite-manifest.js +1 -0
- package/.next/server/middleware-build-manifest.js +1 -0
- package/.next/server/middleware-manifest.json +6 -0
- package/.next/server/middleware-react-loadable-manifest.js +1 -0
- package/.next/server/next-font-manifest.js +1 -0
- package/.next/server/next-font-manifest.json +1 -0
- package/.next/server/pages/500.html +1 -0
- package/.next/server/pages/_app.js +1 -0
- package/.next/server/pages/_app.js.nft.json +1 -0
- package/.next/server/pages/_document.js +1 -0
- package/.next/server/pages/_document.js.nft.json +1 -0
- package/.next/server/pages/_error.js +19 -0
- package/.next/server/pages/_error.js.nft.json +1 -0
- package/.next/server/pages-manifest.json +5 -0
- package/.next/server/server-reference-manifest.js +1 -0
- package/.next/server/server-reference-manifest.json +1 -0
- package/.next/server/webpack-runtime.js +1 -0
- package/.next/static/V0rxN1KuyEM0zJoOJVDoX/_buildManifest.js +1 -0
- package/.next/static/V0rxN1KuyEM0zJoOJVDoX/_ssgManifest.js +1 -0
- package/.next/static/chunks/122-df645619bf29fc6e.js +1 -0
- package/.next/static/chunks/255-73938ce8a8eac1ad.js +1 -0
- package/.next/static/chunks/4bd1b696-c023c6e3521b1417.js +1 -0
- package/.next/static/chunks/510-5993a923674db2de.js +1 -0
- package/.next/static/chunks/658-dfe2290e75772c42.js +1 -0
- package/.next/static/chunks/788-e10fcb5f53c6428d.js +1 -0
- package/.next/static/chunks/9ad0e60a-ad37bddef782d7c2.js +1 -0
- package/.next/static/chunks/app/_not-found/page-f187ad254cc1b0eb.js +1 -0
- package/.next/static/chunks/app/companies/[guid]/page-042071b4790e1a86.js +1 -0
- package/.next/static/chunks/app/companies/[guid]/tables/[table]/page-a46008185adae0f8.js +1 -0
- package/.next/static/chunks/app/companies/[guid]/vouchers/[voucher_guid]/page-1b17cd59db180e1f.js +1 -0
- package/.next/static/chunks/app/layout-d46ad692723a97e0.js +1 -0
- package/.next/static/chunks/app/not-found-61f58c9ef91de921.js +1 -0
- package/.next/static/chunks/app/page-042071b4790e1a86.js +1 -0
- package/.next/static/chunks/framework-acd67e14855de5a2.js +1 -0
- package/.next/static/chunks/main-6f56cad8fb2c15f2.js +1 -0
- package/.next/static/chunks/main-app-f136a4b0e7f8f69b.js +1 -0
- package/.next/static/chunks/pages/_app-7d307437aca18ad4.js +1 -0
- package/.next/static/chunks/pages/_error-cb2a52f75f2162e2.js +1 -0
- package/.next/static/chunks/polyfills-42372ed130431b0a.js +1 -0
- package/.next/static/chunks/webpack-4354c1f6ffe64d9d.js +1 -0
- package/.next/static/css/a8e56a10001bee9f.css +1 -0
- package/CHANGELOG.md +16 -0
- package/LICENSE +201 -0
- package/NOTICE +40 -0
- package/README.md +109 -2
- package/bin/tally-sync.js +12 -0
- package/ecosystem.config.cjs +12 -0
- package/next.config.mjs +7 -0
- package/package.json +58 -4
- package/src/app/companies/[guid]/page.jsx +261 -0
- package/src/app/companies/[guid]/tables/[table]/page.jsx +116 -0
- package/src/app/companies/[guid]/vouchers/[voucher_guid]/page.jsx +156 -0
- package/src/app/globals.css +3 -0
- package/src/app/layout.jsx +29 -0
- package/src/app/not-found.jsx +22 -0
- package/src/app/page.jsx +172 -0
- package/src/cli/_helpers.js +71 -0
- package/src/cli/index.js +56 -0
- package/src/cli/paths.js +26 -0
- package/src/cli/probe.js +61 -0
- package/src/cli/sync.js +84 -0
- package/src/cli/web.js +32 -0
- package/src/components/AppShell.jsx +17 -0
- package/src/components/AppSidebar.jsx +81 -0
- package/src/components/MainContainer.jsx +12 -0
- package/src/components/RowsTable.jsx +111 -0
- package/src/components/Section.jsx +16 -0
- package/src/mirror/agentDb.js +131 -0
- package/src/mirror/companyDb.js +54 -0
- package/src/mirror/schema.js +49 -0
- package/src/mirror/sqlite.js +41 -0
- package/src/probe/probeFile.js +54 -0
- package/src/probe/runProbe.js +118 -0
- package/src/sync/checkPull.js +61 -0
- package/src/sync/pullCompany.js +54 -0
- package/src/sync/runSyncPass.js +50 -0
- package/src/sync/syncCompany.js +75 -0
- package/src/sync/tieOut.js +93 -0
- package/src/tally/dates.js +23 -0
- package/src/tally/definitions.js +22 -0
- package/src/tally/definitions.yaml +370 -0
- package/src/tally/gateway.js +77 -0
- package/src/tally/http.js +92 -0
- package/src/tally/requests.js +133 -0
- package/src/tally/tallyErrors.js +25 -0
- package/src/tally/xml.js +329 -0
- package/src/utils/config.js +46 -0
- package/src/utils/debugLog.js +23 -0
- package/src/web/format.js +160 -0
- package/src/web/mirror.js +132 -0
- package/src/web/status.js +80 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Pulling one company's whole books from Tally: every master table for the period, then
|
|
3
|
+
// every transaction table window by window.
|
|
4
|
+
|
|
5
|
+
import { countVouchersByDate, fetchTable, fetchTransactionWindow, planDateWindows, DEFAULT_BATCH_SIZE } from '../tally/requests.js';
|
|
6
|
+
import { laterDate } from '../tally/dates.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {Object} Pull
|
|
10
|
+
* @property {{from_date: string, to_date: string}} period
|
|
11
|
+
* @property {Record<string, Record<string, unknown>[]>} rows - rows per table name
|
|
12
|
+
* @property {import('../tally/requests.js').VoucherCounts} counts - Tally's own voucher counts
|
|
13
|
+
* @property {(import('../tally/requests.js').DateWindow & {vouchers_pulled: number})[]} windows
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The period a full sync covers: the books' start to the last voucher.
|
|
18
|
+
* @param {import('../tally/xml.js').CompanyInfo} info
|
|
19
|
+
*/
|
|
20
|
+
export function syncPeriod(info) {
|
|
21
|
+
return { from_date: info.books_from, to_date: laterDate(info.last_voucher_date, info.books_from) };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {import('../tally/gateway.js').Gateway} gateway
|
|
26
|
+
* @param {import('../tally/definitions.js').Definitions} definitions
|
|
27
|
+
* @param {import('../tally/xml.js').CompanyInfo} info
|
|
28
|
+
* @param {{batch_size?: number}} [options]
|
|
29
|
+
* @returns {Promise<Pull>}
|
|
30
|
+
*/
|
|
31
|
+
export async function pullCompany(gateway, definitions, info, { batch_size = DEFAULT_BATCH_SIZE } = {}) {
|
|
32
|
+
const company = info.name;
|
|
33
|
+
const period = syncPeriod(info);
|
|
34
|
+
/** @type {Record<string, Record<string, unknown>[]>} */
|
|
35
|
+
const rows = {};
|
|
36
|
+
|
|
37
|
+
for (const table of definitions.master) rows[table.name] = await fetchTable(gateway, table, { company, ...period });
|
|
38
|
+
|
|
39
|
+
const counts = await countVouchersByDate(gateway, { company, ...period });
|
|
40
|
+
const planned = planDateWindows(counts.by_date, { ...period, batch_size });
|
|
41
|
+
for (const table of definitions.transaction) rows[table.name] = [];
|
|
42
|
+
|
|
43
|
+
const windows = [];
|
|
44
|
+
for (const window of planned) {
|
|
45
|
+
let vouchers_pulled = 0;
|
|
46
|
+
for (const table of definitions.transaction) {
|
|
47
|
+
const window_rows = await fetchTransactionWindow(gateway, table, { company, window });
|
|
48
|
+
rows[table.name].push(...window_rows);
|
|
49
|
+
if (table.name === 'trn_voucher') vouchers_pulled = window_rows.length;
|
|
50
|
+
}
|
|
51
|
+
windows.push({ ...window, vouchers_pulled });
|
|
52
|
+
}
|
|
53
|
+
return { period, rows, counts, windows };
|
|
54
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// One sync pass: find the loaded companies, then sync each in turn.
|
|
3
|
+
|
|
4
|
+
import { getCompanyInfo, listCompanies } from '../tally/requests.js';
|
|
5
|
+
import { recordCompanySeen, recordPass } from '../mirror/agentDb.js';
|
|
6
|
+
import { syncCompany } from './syncCompany.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {Object} PassResult
|
|
10
|
+
* @property {boolean} tally_reachable
|
|
11
|
+
* @property {string[]} loaded_companies
|
|
12
|
+
* @property {(import('./syncCompany.js').CompanySyncResult | {company: string, status: 'not_loaded'})[]} results
|
|
13
|
+
* @property {string|null} error
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {Object} options
|
|
18
|
+
* @param {import('../tally/gateway.js').Gateway} options.gateway
|
|
19
|
+
* @param {import('../tally/definitions.js').Definitions} options.definitions
|
|
20
|
+
* @param {import('node:sqlite').DatabaseSync} options.agent_db
|
|
21
|
+
* @param {string} options.data_dir
|
|
22
|
+
* @param {() => string} options.now
|
|
23
|
+
* @param {string} [options.company] - sync only this company
|
|
24
|
+
* @param {boolean} [options.force]
|
|
25
|
+
* @param {number} [options.batch_size]
|
|
26
|
+
* @returns {Promise<PassResult>}
|
|
27
|
+
*/
|
|
28
|
+
export async function runSyncPass({ gateway, definitions, agent_db, data_dir, now, company, force, batch_size }) {
|
|
29
|
+
let loaded_companies;
|
|
30
|
+
try {
|
|
31
|
+
loaded_companies = await listCompanies(gateway);
|
|
32
|
+
} catch (err) {
|
|
33
|
+
recordPass(agent_db, { at: now(), tally_reachable: false, loaded_companies: [], error: err.message });
|
|
34
|
+
return { tally_reachable: false, loaded_companies: [], results: [], error: err.message };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const targets = company ? [company] : loaded_companies;
|
|
38
|
+
const results = [];
|
|
39
|
+
for (const name of targets) {
|
|
40
|
+
const info = await getCompanyInfo(gateway, name);
|
|
41
|
+
if (!info) {
|
|
42
|
+
results.push({ company: name, status: /** @type {const} */ ('not_loaded') });
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
recordCompanySeen(agent_db, { guid: info.guid, name: info.name }, now());
|
|
46
|
+
results.push(await syncCompany({ gateway, definitions, agent_db, data_dir, info, now, force, batch_size }));
|
|
47
|
+
}
|
|
48
|
+
recordPass(agent_db, { at: now(), tally_reachable: true, loaded_companies, error: null });
|
|
49
|
+
return { tally_reachable: true, loaded_companies, results, error: null };
|
|
50
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// One company's full sync: skip when Tally's change counters haven't moved, otherwise pull,
|
|
3
|
+
// check, tie out, and replace the mirror in one transaction. Every attempt is a sync run.
|
|
4
|
+
|
|
5
|
+
import { pullCompany } from './pullCompany.js';
|
|
6
|
+
import { checkPull } from './checkPull.js';
|
|
7
|
+
import { tieOut } from './tieOut.js';
|
|
8
|
+
import { openCompanyDb, replaceMirror } from '../mirror/companyDb.js';
|
|
9
|
+
import { finishRun, lastSucceededRun, RUN_STATUS, startRun } from '../mirror/agentDb.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {Object} CompanySyncResult
|
|
13
|
+
* @property {string} company - the company's name
|
|
14
|
+
* @property {'unchanged'|'succeeded'|'failed'} status
|
|
15
|
+
* @property {number} [run_id]
|
|
16
|
+
* @property {Record<string, number>} [counts] - rows per table
|
|
17
|
+
* @property {import('./checkPull.js').Problem[]} [problems]
|
|
18
|
+
* @property {import('./tieOut.js').TieOut} [tie_out]
|
|
19
|
+
* @property {string} [error]
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {Object} options
|
|
24
|
+
* @param {import('../tally/gateway.js').Gateway} options.gateway
|
|
25
|
+
* @param {import('../tally/definitions.js').Definitions} options.definitions
|
|
26
|
+
* @param {import('node:sqlite').DatabaseSync} options.agent_db
|
|
27
|
+
* @param {string} options.data_dir
|
|
28
|
+
* @param {import('../tally/xml.js').CompanyInfo} options.info - read from Tally just before
|
|
29
|
+
* @param {() => string} options.now - ISO timestamp
|
|
30
|
+
* @param {boolean} [options.force] - sync even when nothing changed
|
|
31
|
+
* @param {number} [options.batch_size]
|
|
32
|
+
* @returns {Promise<CompanySyncResult>}
|
|
33
|
+
*/
|
|
34
|
+
export async function syncCompany({ gateway, definitions, agent_db, data_dir, info, now, force = false, batch_size }) {
|
|
35
|
+
const last = lastSucceededRun(agent_db, info.guid);
|
|
36
|
+
const unchanged = last && last.alter_id_master === info.alter_id_master && last.alter_id_transaction === info.alter_id_transaction;
|
|
37
|
+
if (unchanged && !force) return { company: info.name, status: 'unchanged' };
|
|
38
|
+
|
|
39
|
+
const run_id = startRun(agent_db, {
|
|
40
|
+
company_guid: info.guid,
|
|
41
|
+
company_name: info.name,
|
|
42
|
+
started_at: now(),
|
|
43
|
+
alter_id_master: info.alter_id_master,
|
|
44
|
+
alter_id_transaction: info.alter_id_transaction,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
const pull = await pullCompany(gateway, definitions, info, { batch_size });
|
|
49
|
+
const counts = Object.fromEntries(Object.entries(pull.rows).map(([table, rows]) => [table, rows.length]));
|
|
50
|
+
const previous_voucher_count = last?.counts_json ? JSON.parse(last.counts_json).trn_voucher : null;
|
|
51
|
+
const problems = checkPull(pull, { previous_voucher_count });
|
|
52
|
+
if (problems.length > 0) {
|
|
53
|
+
finishRun(agent_db, run_id, { status: RUN_STATUS.FAILED, finished_at: now(), counts, problems, error: 'checks failed; the mirror was not replaced' });
|
|
54
|
+
return { company: info.name, status: 'failed', run_id, counts, problems };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const tie_out = tieOut(pull.rows);
|
|
58
|
+
const company_db = openCompanyDb(data_dir, info.guid);
|
|
59
|
+
try {
|
|
60
|
+
replaceMirror(company_db, {
|
|
61
|
+
tables: [...definitions.master, ...definitions.transaction],
|
|
62
|
+
rows: pull.rows,
|
|
63
|
+
info,
|
|
64
|
+
sync: { run_id, synced_at: now(), tie_out },
|
|
65
|
+
});
|
|
66
|
+
} finally {
|
|
67
|
+
company_db.close();
|
|
68
|
+
}
|
|
69
|
+
finishRun(agent_db, run_id, { status: RUN_STATUS.SUCCEEDED, finished_at: now(), counts, problems: [], tie_out });
|
|
70
|
+
return { company: info.name, status: 'succeeded', run_id, counts, problems: [], tie_out };
|
|
71
|
+
} catch (err) {
|
|
72
|
+
finishRun(agent_db, run_id, { status: RUN_STATUS.FAILED, finished_at: now(), error: err.message });
|
|
73
|
+
return { company: info.name, status: 'failed', run_id, error: err.message };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// The tie-out: does every ledger's balance in the pull equal Tally's own closing balance?
|
|
3
|
+
// opening + every accounting voucher's lines = closing, to the paisa (tally-to-duckdb, tieout.py).
|
|
4
|
+
|
|
5
|
+
/** Tally's reserved name for the ledger that carries the period's profit. */
|
|
6
|
+
export const PROFIT_AND_LOSS = 'Profit & Loss A/c';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Base voucher types that never reach the books: Memorandum vouchers sit in their own
|
|
10
|
+
* register, and Reversing Journals affect scenario reports only. $$IsAccountingVch counts
|
|
11
|
+
* both as accounting vouchers, so they're left out by type.
|
|
12
|
+
*/
|
|
13
|
+
export const NON_POSTING_BASE_TYPES = ['Memorandum', 'Reversing Journal'];
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Names of the voucher types whose base type doesn't post. A built-in type is its own parent;
|
|
17
|
+
* a company's own type points at the type it's built on, possibly through other custom types.
|
|
18
|
+
* @param {Record<string, unknown>[]} voucher_types - mst_vouchertype rows
|
|
19
|
+
* @returns {Set<string>}
|
|
20
|
+
*/
|
|
21
|
+
export function nonPostingVoucherTypes(voucher_types) {
|
|
22
|
+
const parent_of = new Map(voucher_types.map((t) => [String(t.name), String(t.parent ?? '')]));
|
|
23
|
+
/** @param {string} name */
|
|
24
|
+
const baseOf = (name) => {
|
|
25
|
+
const seen = new Set();
|
|
26
|
+
let current = name;
|
|
27
|
+
while (parent_of.has(current) && parent_of.get(current) !== current && parent_of.get(current) !== '' && !seen.has(current)) {
|
|
28
|
+
seen.add(current);
|
|
29
|
+
current = /** @type {string} */ (parent_of.get(current));
|
|
30
|
+
}
|
|
31
|
+
return current;
|
|
32
|
+
};
|
|
33
|
+
return new Set([...parent_of.keys()].filter((name) => NON_POSTING_BASE_TYPES.includes(baseOf(name))));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @typedef {Object} LedgerDifference
|
|
38
|
+
* @property {string} ledger
|
|
39
|
+
* @property {number} ours_paise
|
|
40
|
+
* @property {number} tally_paise
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @typedef {Object} TieOut
|
|
45
|
+
* @property {boolean} matched - every ledger except Profit & Loss A/c matched
|
|
46
|
+
* @property {number} ledgers - ledgers checked
|
|
47
|
+
* @property {number} vouchers_left_out - accounting vouchers of a type that doesn't post (Memorandum, Reversing Journal)
|
|
48
|
+
* @property {LedgerDifference[]} differences
|
|
49
|
+
* @property {{ours_paise: number, tally_paise: number, matched: boolean}|null} profit_and_loss
|
|
50
|
+
* Tally's Profit & Loss A/c closing also carries the period's profit and the change in stock.
|
|
51
|
+
* Stock isn't synced, so this ledger is reported apart and doesn't decide `matched`.
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
/** @param {unknown} value */
|
|
55
|
+
const paise = (value) => Math.round(Number(value ?? 0) * 100);
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param {Record<string, Record<string, unknown>[]>} rows - a pull's rows (mst_ledger, mst_vouchertype, trn_voucher, trn_accounting)
|
|
59
|
+
* @returns {TieOut}
|
|
60
|
+
*/
|
|
61
|
+
export function tieOut(rows) {
|
|
62
|
+
const non_posting = nonPostingVoucherTypes(rows.mst_vouchertype ?? []);
|
|
63
|
+
const accounting = (rows.trn_voucher ?? []).filter((v) => v.is_accounting_voucher);
|
|
64
|
+
const posting = accounting.filter((v) => !non_posting.has(String(v.voucher_type)));
|
|
65
|
+
const accounting_vouchers = new Set(posting.map((v) => String(v.guid)));
|
|
66
|
+
/** @type {Map<string, number>} */
|
|
67
|
+
const movement = new Map();
|
|
68
|
+
for (const line of rows.trn_accounting ?? []) {
|
|
69
|
+
if (!accounting_vouchers.has(String(line.guid))) continue;
|
|
70
|
+
const ledger = String(line.ledger);
|
|
71
|
+
movement.set(ledger, (movement.get(ledger) ?? 0) + paise(line.amount));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const ledgers = rows.mst_ledger ?? [];
|
|
75
|
+
const revenue_movement = ledgers.filter((l) => l.is_revenue).reduce((sum, l) => sum + (movement.get(String(l.name)) ?? 0), 0);
|
|
76
|
+
|
|
77
|
+
/** @type {LedgerDifference[]} */
|
|
78
|
+
const differences = [];
|
|
79
|
+
let profit_and_loss = null;
|
|
80
|
+
for (const ledger of ledgers) {
|
|
81
|
+
const name = String(ledger.name);
|
|
82
|
+
const ours = paise(ledger.opening_balance) + (movement.get(name) ?? 0);
|
|
83
|
+
const tally = paise(ledger.closing_balance);
|
|
84
|
+
if (name === PROFIT_AND_LOSS) {
|
|
85
|
+
const ours_with_profit = ours + revenue_movement;
|
|
86
|
+
profit_and_loss = { ours_paise: ours_with_profit, tally_paise: tally, matched: ours_with_profit === tally };
|
|
87
|
+
} else if (ours !== tally) {
|
|
88
|
+
differences.push({ ledger: name, ours_paise: ours, tally_paise: tally });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
differences.sort((a, b) => Math.abs(b.ours_paise - b.tally_paise) - Math.abs(a.ours_paise - a.tally_paise));
|
|
92
|
+
return { matched: differences.length === 0, ledgers: ledgers.length, vouchers_left_out: accounting.length - posting.length, differences, profit_and_loss };
|
|
93
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// ISO date helpers (yyyy-mm-dd strings), computed in UTC so no time zone can shift a day.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} iso_date
|
|
6
|
+
* @param {number} days - may be negative
|
|
7
|
+
* @returns {string}
|
|
8
|
+
*/
|
|
9
|
+
export function addDays(iso_date, days) {
|
|
10
|
+
const date = new Date(`${iso_date}T00:00:00Z`);
|
|
11
|
+
date.setUTCDate(date.getUTCDate() + days);
|
|
12
|
+
return date.toISOString().slice(0, 10);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The later of two ISO dates.
|
|
17
|
+
* @param {string} a
|
|
18
|
+
* @param {string} b
|
|
19
|
+
* @returns {string}
|
|
20
|
+
*/
|
|
21
|
+
export function laterDate(a, b) {
|
|
22
|
+
return a > b ? a : b;
|
|
23
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// The table definitions: which Tally collections to export and how each column is read.
|
|
3
|
+
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import yaml from 'js-yaml';
|
|
6
|
+
|
|
7
|
+
const DEFINITIONS_FILE = new URL('./definitions.yaml', import.meta.url);
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {Object} Definitions
|
|
11
|
+
* @property {import('./xml.js').TableDefinition[]} master
|
|
12
|
+
* @property {import('./xml.js').TableDefinition[]} transaction
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Loads the table definitions shipped with the package.
|
|
17
|
+
* @returns {Definitions}
|
|
18
|
+
*/
|
|
19
|
+
export function loadDefinitions() {
|
|
20
|
+
const parsed = /** @type {Definitions} */ (yaml.load(fs.readFileSync(DEFINITIONS_FILE, 'utf8')));
|
|
21
|
+
return { master: parsed.master, transaction: parsed.transaction };
|
|
22
|
+
}
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
# Tally export definitions for tally-sync.
|
|
2
|
+
#
|
|
3
|
+
# Copied unchanged from tally-database-loader by Dhananjay Gokhale (MIT licence),
|
|
4
|
+
# https://github.com/dhananjay1405/tally-database-loader, file tally-export-config.yaml
|
|
5
|
+
# at commit a23f193. TRIMMED: only the accounting tables below are kept; the other
|
|
6
|
+
# upstream entries (inventory, payroll, GST and others) are left out. See NOTICE.
|
|
7
|
+
#
|
|
8
|
+
# Each table: a Tally collection (dotted path for sub-collections), the fetch list,
|
|
9
|
+
# filters, and one TDL field expression per column. Field types: text, logical, date,
|
|
10
|
+
# number, amount, quantity, rate.
|
|
11
|
+
|
|
12
|
+
master:
|
|
13
|
+
- name: mst_group
|
|
14
|
+
collection: Group
|
|
15
|
+
nature: Primary
|
|
16
|
+
fields:
|
|
17
|
+
- name: guid
|
|
18
|
+
field: Guid
|
|
19
|
+
type: text
|
|
20
|
+
- name: name
|
|
21
|
+
field: Name
|
|
22
|
+
type: text
|
|
23
|
+
- name: parent
|
|
24
|
+
field: if $$IsEqual:$Parent:$$SysName:Primary then "" else $Parent
|
|
25
|
+
type: text
|
|
26
|
+
- name: primary_group
|
|
27
|
+
field: _PrimaryGroup
|
|
28
|
+
type: text
|
|
29
|
+
- name: is_revenue
|
|
30
|
+
field: IsRevenue
|
|
31
|
+
type: logical
|
|
32
|
+
- name: is_deemedpositive
|
|
33
|
+
field: IsDeemedPositive
|
|
34
|
+
type: logical
|
|
35
|
+
- name: is_reserved
|
|
36
|
+
field: IsReserved
|
|
37
|
+
type: logical
|
|
38
|
+
- name: affects_gross_profit
|
|
39
|
+
field: AffectsGrossProfit
|
|
40
|
+
type: logical
|
|
41
|
+
- name: sort_position
|
|
42
|
+
field: SortPosition
|
|
43
|
+
type: number
|
|
44
|
+
- name: mst_ledger
|
|
45
|
+
collection: Ledger
|
|
46
|
+
nature: Primary
|
|
47
|
+
fields:
|
|
48
|
+
- name: guid
|
|
49
|
+
field: Guid
|
|
50
|
+
type: text
|
|
51
|
+
- name: name
|
|
52
|
+
field: Name
|
|
53
|
+
type: text
|
|
54
|
+
- name: parent
|
|
55
|
+
field: if $$IsEqual:$Parent:$$SysName:Primary then "" else $Parent
|
|
56
|
+
type: text
|
|
57
|
+
- name: alias
|
|
58
|
+
field: OnlyAlias
|
|
59
|
+
type: text
|
|
60
|
+
- name: description
|
|
61
|
+
field: Description
|
|
62
|
+
type: text
|
|
63
|
+
- name: notes
|
|
64
|
+
field: Narration
|
|
65
|
+
type: text
|
|
66
|
+
- name: is_revenue
|
|
67
|
+
field: IsRevenue
|
|
68
|
+
type: logical
|
|
69
|
+
- name: is_deemedpositive
|
|
70
|
+
field: IsDeemedPositive
|
|
71
|
+
type: logical
|
|
72
|
+
- name: opening_balance
|
|
73
|
+
field: OpeningBalance
|
|
74
|
+
type: amount
|
|
75
|
+
- name: closing_balance
|
|
76
|
+
field: ClosingBalance
|
|
77
|
+
type: amount
|
|
78
|
+
- name: mailing_name
|
|
79
|
+
field: MailingName
|
|
80
|
+
type: text
|
|
81
|
+
- name: mailing_address
|
|
82
|
+
field: if $$IsEmpty:$Address then "" else $$FullList:Address:$Address
|
|
83
|
+
type: text
|
|
84
|
+
- name: mailing_state
|
|
85
|
+
field: LedStateName
|
|
86
|
+
type: text
|
|
87
|
+
- name: mailing_country
|
|
88
|
+
field: CountryName
|
|
89
|
+
type: text
|
|
90
|
+
- name: mailing_pincode
|
|
91
|
+
field: PinCode
|
|
92
|
+
type: text
|
|
93
|
+
- name: email
|
|
94
|
+
field: Email
|
|
95
|
+
type: text
|
|
96
|
+
- name: mobile
|
|
97
|
+
field: if NOT $$IsEmpty:$LedgerMobile then $$Sprintf:"%s %s":$LedgerCountryISDCode:$LedgerMobile else ""
|
|
98
|
+
type: text
|
|
99
|
+
- name: it_pan
|
|
100
|
+
field: IncomeTaxNumber
|
|
101
|
+
type: text
|
|
102
|
+
- name: gstn
|
|
103
|
+
field: if $$IsEmpty:$PartyGSTIN then $LedGSTRegDetails[Last].GSTIN else $PartyGSTIN
|
|
104
|
+
type: text
|
|
105
|
+
- name: gst_registration_type
|
|
106
|
+
field: if $$IsEmpty:$Gstregistrationtype then $LedGSTRegDetails[Last].Gstregistrationtype else $Gstregistrationtype
|
|
107
|
+
type: text
|
|
108
|
+
- name: gst_supply_type
|
|
109
|
+
field: Gsttypeofsupply
|
|
110
|
+
type: text
|
|
111
|
+
- name: gst_duty_head
|
|
112
|
+
field: Gstdutyhead
|
|
113
|
+
type: text
|
|
114
|
+
- name: bank_account_holder
|
|
115
|
+
field: Bankaccholdername
|
|
116
|
+
type: text
|
|
117
|
+
- name: bank_account_number
|
|
118
|
+
field: BankDetails
|
|
119
|
+
type: text
|
|
120
|
+
- name: bank_ifsc
|
|
121
|
+
field: Ifscode
|
|
122
|
+
type: text
|
|
123
|
+
- name: bank_swift
|
|
124
|
+
field: Swiftcode
|
|
125
|
+
type: text
|
|
126
|
+
- name: bank_name
|
|
127
|
+
field: Bankingconfigbank
|
|
128
|
+
type: text
|
|
129
|
+
- name: bank_branch
|
|
130
|
+
field: BankBranchname
|
|
131
|
+
type: text
|
|
132
|
+
- name: bill_credit_period
|
|
133
|
+
field: $$Number:$BillCreditPeriod
|
|
134
|
+
type: number
|
|
135
|
+
- name: mst_vouchertype
|
|
136
|
+
collection: VoucherType
|
|
137
|
+
nature: Primary
|
|
138
|
+
fields:
|
|
139
|
+
- name: guid
|
|
140
|
+
field: Guid
|
|
141
|
+
type: text
|
|
142
|
+
- name: name
|
|
143
|
+
field: Name
|
|
144
|
+
type: text
|
|
145
|
+
- name: parent
|
|
146
|
+
field: Parent
|
|
147
|
+
type: text
|
|
148
|
+
- name: numbering_method
|
|
149
|
+
field: NumberingMethod
|
|
150
|
+
type: text
|
|
151
|
+
- name: is_deemedpositive
|
|
152
|
+
field: IsDeemedPositive
|
|
153
|
+
type: logical
|
|
154
|
+
- name: affects_stock
|
|
155
|
+
field: AffectsStock
|
|
156
|
+
type: logical
|
|
157
|
+
- name: mst_cost_category
|
|
158
|
+
collection: CostCategory
|
|
159
|
+
nature: Primary
|
|
160
|
+
fields:
|
|
161
|
+
- name: guid
|
|
162
|
+
field: Guid
|
|
163
|
+
type: text
|
|
164
|
+
- name: name
|
|
165
|
+
field: Name
|
|
166
|
+
type: text
|
|
167
|
+
- name: allocate_revenue
|
|
168
|
+
field: AllocateRevenue
|
|
169
|
+
type: logical
|
|
170
|
+
- name: allocate_non_revenue
|
|
171
|
+
field: AllocateNonRevenue
|
|
172
|
+
type: logical
|
|
173
|
+
- name: mst_cost_centre
|
|
174
|
+
collection: CostCentre
|
|
175
|
+
nature: Primary
|
|
176
|
+
fields:
|
|
177
|
+
- name: guid
|
|
178
|
+
field: Guid
|
|
179
|
+
type: text
|
|
180
|
+
- name: name
|
|
181
|
+
field: Name
|
|
182
|
+
type: text
|
|
183
|
+
- name: parent
|
|
184
|
+
field: if $$IsEqual:$Parent:$$SysName:Primary then "" else $Parent
|
|
185
|
+
type: text
|
|
186
|
+
- name: category
|
|
187
|
+
field: Category
|
|
188
|
+
type: text
|
|
189
|
+
- name: mst_opening_bill_allocation
|
|
190
|
+
collection: Ledger.BillAllocations
|
|
191
|
+
nature: Derived
|
|
192
|
+
filters:
|
|
193
|
+
- $$NumItems:BillAllocations > 0
|
|
194
|
+
fields:
|
|
195
|
+
- name: ledger
|
|
196
|
+
field: ..Name
|
|
197
|
+
type: text
|
|
198
|
+
- name: opening_balance
|
|
199
|
+
field: OpeningBalance
|
|
200
|
+
type: amount
|
|
201
|
+
- name: bill_date
|
|
202
|
+
field: BillDate
|
|
203
|
+
type: date
|
|
204
|
+
- name: name
|
|
205
|
+
field: Name
|
|
206
|
+
type: text
|
|
207
|
+
- name: bill_credit_period
|
|
208
|
+
field: $$Number:$BillCreditPeriod
|
|
209
|
+
type: number
|
|
210
|
+
- name: is_advance
|
|
211
|
+
field: IsAdvance
|
|
212
|
+
type: logical
|
|
213
|
+
transaction:
|
|
214
|
+
- name: trn_voucher
|
|
215
|
+
collection: Voucher
|
|
216
|
+
nature: Primary
|
|
217
|
+
fetch:
|
|
218
|
+
- Narration,PartyLedgerName,PlaceOfSupply,Reference,ReferenceDate
|
|
219
|
+
filters:
|
|
220
|
+
- NOT $IsCancelled
|
|
221
|
+
- NOT $IsOptional
|
|
222
|
+
fields:
|
|
223
|
+
- name: guid
|
|
224
|
+
field: Guid
|
|
225
|
+
type: text
|
|
226
|
+
- name: date
|
|
227
|
+
field: Date
|
|
228
|
+
type: date
|
|
229
|
+
- name: voucher_type
|
|
230
|
+
field: VoucherTypeName
|
|
231
|
+
type: text
|
|
232
|
+
- name: voucher_number
|
|
233
|
+
field: VoucherNumber
|
|
234
|
+
type: text
|
|
235
|
+
- name: reference_number
|
|
236
|
+
field: Reference
|
|
237
|
+
type: text
|
|
238
|
+
- name: reference_date
|
|
239
|
+
field: ReferenceDate
|
|
240
|
+
type: date
|
|
241
|
+
- name: narration
|
|
242
|
+
field: Narration
|
|
243
|
+
type: text
|
|
244
|
+
- name: party_name
|
|
245
|
+
field: PartyLedgerName
|
|
246
|
+
type: text
|
|
247
|
+
- name: place_of_supply
|
|
248
|
+
field: PlaceOfSupply
|
|
249
|
+
type: text
|
|
250
|
+
- name: is_invoice
|
|
251
|
+
field: IsInvoice
|
|
252
|
+
type: logical
|
|
253
|
+
- name: is_accounting_voucher
|
|
254
|
+
field: if $$IsAccountingVch:$VoucherTypeName then 1 else 0
|
|
255
|
+
type: logical
|
|
256
|
+
- name: is_inventory_voucher
|
|
257
|
+
field: if $$IsInventoryVch:$VoucherTypeName then 1 else 0
|
|
258
|
+
type: logical
|
|
259
|
+
- name: is_order_voucher
|
|
260
|
+
field: if $$IsOrderVch:$VoucherTypeName then 1 else 0
|
|
261
|
+
type: logical
|
|
262
|
+
- name: trn_accounting
|
|
263
|
+
collection: Voucher.AllLedgerEntries
|
|
264
|
+
nature: Derived
|
|
265
|
+
fetch:
|
|
266
|
+
- AllLedgerEntries
|
|
267
|
+
filters:
|
|
268
|
+
- NOT $IsCancelled
|
|
269
|
+
- NOT $IsOptional
|
|
270
|
+
- $$NumItems:AllLedgerEntries > 0
|
|
271
|
+
fields:
|
|
272
|
+
- name: guid
|
|
273
|
+
field: Guid
|
|
274
|
+
type: text
|
|
275
|
+
- name: ledger
|
|
276
|
+
field: LedgerName
|
|
277
|
+
type: text
|
|
278
|
+
- name: amount
|
|
279
|
+
field: Amount
|
|
280
|
+
type: amount
|
|
281
|
+
- name: amount_forex
|
|
282
|
+
field: if $$IsEmpty:$$ForexValue:$Amount then 0 else $$StringFindAndReplace:(if $$IsDebit:$Amount then -$$ForexValue:$Amount else $$ForexValue:$Amount):"(-)":"-"
|
|
283
|
+
type: amount
|
|
284
|
+
- name: currency
|
|
285
|
+
field: $$Currency:$Amount
|
|
286
|
+
type: text
|
|
287
|
+
- name: trn_bill
|
|
288
|
+
collection: Voucher.AllLedgerEntries.BillAllocations
|
|
289
|
+
nature: Derived
|
|
290
|
+
fetch:
|
|
291
|
+
- AllLedgerEntries
|
|
292
|
+
filters:
|
|
293
|
+
- NOT $IsCancelled
|
|
294
|
+
- NOT $IsOptional
|
|
295
|
+
- $$NumItems:AllLedgerEntries > 0
|
|
296
|
+
fields:
|
|
297
|
+
- name: guid
|
|
298
|
+
field: Guid
|
|
299
|
+
type: text
|
|
300
|
+
- name: ledger
|
|
301
|
+
field: LedgerName
|
|
302
|
+
type: text
|
|
303
|
+
- name: name
|
|
304
|
+
field: Name
|
|
305
|
+
type: text
|
|
306
|
+
- name: amount
|
|
307
|
+
field: Amount
|
|
308
|
+
type: amount
|
|
309
|
+
- name: billtype
|
|
310
|
+
field: BillType
|
|
311
|
+
type: text
|
|
312
|
+
- name: bill_credit_period
|
|
313
|
+
field: $$Number:$BillCreditPeriod
|
|
314
|
+
type: number
|
|
315
|
+
- name: trn_bank
|
|
316
|
+
collection: Voucher.AllLedgerEntries.BankAllocations
|
|
317
|
+
nature: Derived
|
|
318
|
+
fetch:
|
|
319
|
+
- AllLedgerEntries
|
|
320
|
+
filters:
|
|
321
|
+
- NOT $IsCancelled
|
|
322
|
+
- NOT $IsOptional
|
|
323
|
+
- $$NumItems:AllLedgerEntries > 0
|
|
324
|
+
fields:
|
|
325
|
+
- name: guid
|
|
326
|
+
field: Guid
|
|
327
|
+
type: text
|
|
328
|
+
- name: ledger
|
|
329
|
+
field: ..LedgerName
|
|
330
|
+
type: text
|
|
331
|
+
- name: transaction_type
|
|
332
|
+
field: TransactionType
|
|
333
|
+
type: text
|
|
334
|
+
- name: instrument_date
|
|
335
|
+
field: InstrumentDate
|
|
336
|
+
type: date
|
|
337
|
+
- name: instrument_number
|
|
338
|
+
field: InstrumentNumber
|
|
339
|
+
type: text
|
|
340
|
+
- name: bank_name
|
|
341
|
+
field: BankName
|
|
342
|
+
type: text
|
|
343
|
+
- name: amount
|
|
344
|
+
field: Amount
|
|
345
|
+
type: amount
|
|
346
|
+
- name: bankers_date
|
|
347
|
+
field: BankersDate
|
|
348
|
+
type: date
|
|
349
|
+
- name: trn_cost_centre
|
|
350
|
+
collection: Voucher.AllLedgerEntries.CostCentreAllocations
|
|
351
|
+
nature: Derived
|
|
352
|
+
fetch:
|
|
353
|
+
- AllLedgerEntries
|
|
354
|
+
filters:
|
|
355
|
+
- NOT $IsCancelled
|
|
356
|
+
- NOT $IsOptional
|
|
357
|
+
- $$NumItems:AllLedgerEntries > 0
|
|
358
|
+
fields:
|
|
359
|
+
- name: guid
|
|
360
|
+
field: Guid
|
|
361
|
+
type: text
|
|
362
|
+
- name: ledger
|
|
363
|
+
field: LedgerName
|
|
364
|
+
type: text
|
|
365
|
+
- name: costcentre
|
|
366
|
+
field: Name
|
|
367
|
+
type: text
|
|
368
|
+
- name: amount
|
|
369
|
+
field: Amount
|
|
370
|
+
type: amount
|