@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,77 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// The one way the agent talks to Tally. Tally is single-threaded, so requests go one at
|
|
3
|
+
// a time, in the order they were made. Every attempt is reported to onExchange, which is
|
|
4
|
+
// how the probe file and the debug log see the raw XML.
|
|
5
|
+
|
|
6
|
+
import { postXml } from './http.js';
|
|
7
|
+
import { TallyIncompleteError } from './tallyErrors.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {Object} Exchange
|
|
11
|
+
* @property {string} label - what the request was for, e.g. "table mst_ledger"
|
|
12
|
+
* @property {number} attempt - 1 for the first try
|
|
13
|
+
* @property {string} started_at - ISO timestamp
|
|
14
|
+
* @property {number} ms - how long Tally took
|
|
15
|
+
* @property {string} request_xml
|
|
16
|
+
* @property {string|null} response_text - null when no answer came
|
|
17
|
+
* @property {number|null} bytes
|
|
18
|
+
* @property {{code: string, message: string}|null} error
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @typedef {Object} Gateway
|
|
23
|
+
* @property {(label: string, xml: string, options?: {check?: (text: string) => string|null}) => Promise<string>} request
|
|
24
|
+
* Sends one request and returns Tally's answer. `check` returns a message when the answer
|
|
25
|
+
* is cut short; the request is then retried.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {Object} options
|
|
30
|
+
* @param {import('./http.js').TallyConnection} options.connection
|
|
31
|
+
* @param {(exchange: Exchange) => void} [options.onExchange] - called after every attempt
|
|
32
|
+
* @param {typeof postXml} [options.post] - the transport, replaceable in tests
|
|
33
|
+
* @param {number} [options.max_attempts] - tries for an answer that comes back cut short
|
|
34
|
+
* @param {number} [options.retry_base_ms] - first back-off; doubles on each retry
|
|
35
|
+
* @returns {Gateway}
|
|
36
|
+
*/
|
|
37
|
+
export function createGateway({ connection, onExchange = () => {}, post = postXml, max_attempts = 3, retry_base_ms = 2000 }) {
|
|
38
|
+
let queue = Promise.resolve();
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {string} label
|
|
42
|
+
* @param {string} xml
|
|
43
|
+
* @param {(text: string) => string|null} check
|
|
44
|
+
*/
|
|
45
|
+
async function send(label, xml, check) {
|
|
46
|
+
for (let attempt = 1; ; attempt++) {
|
|
47
|
+
const started = Date.now();
|
|
48
|
+
/** @type {Exchange} */
|
|
49
|
+
const exchange = { label, attempt, started_at: new Date(started).toISOString(), ms: 0, request_xml: xml, response_text: null, bytes: null, error: null };
|
|
50
|
+
try {
|
|
51
|
+
const answer = await post(connection, xml);
|
|
52
|
+
exchange.ms = Date.now() - started;
|
|
53
|
+
exchange.response_text = answer.text;
|
|
54
|
+
exchange.bytes = answer.bytes;
|
|
55
|
+
const problem = check(answer.text);
|
|
56
|
+
if (problem) throw new TallyIncompleteError(`${label}: ${problem}`);
|
|
57
|
+
onExchange(exchange);
|
|
58
|
+
return answer.text;
|
|
59
|
+
} catch (err) {
|
|
60
|
+
exchange.ms = Date.now() - started;
|
|
61
|
+
exchange.error = { code: err.code ?? 'ERROR', message: err.message };
|
|
62
|
+
onExchange(exchange);
|
|
63
|
+
const can_retry = err instanceof TallyIncompleteError && attempt < max_attempts;
|
|
64
|
+
if (!can_retry) throw err;
|
|
65
|
+
await new Promise((resolve) => setTimeout(resolve, retry_base_ms * 2 ** (attempt - 1)));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
request(label, xml, { check = () => null } = {}) {
|
|
72
|
+
const result = queue.then(() => send(label, xml, check));
|
|
73
|
+
queue = result.catch(() => {});
|
|
74
|
+
return result;
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Posting one XML request to Tally's HTTP server. UTF-16LE in both directions, exactly
|
|
3
|
+
// as tally-database-loader sends it (postTallyXML). See NOTICE.
|
|
4
|
+
|
|
5
|
+
import http from 'node:http';
|
|
6
|
+
import https from 'node:https';
|
|
7
|
+
import { TallyError } from './tallyErrors.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {Object} TallyConnection
|
|
11
|
+
* @property {'http'|'https'} [protocol] - http (Tally itself) unless Tally is reached through an HTTPS tunnel
|
|
12
|
+
* @property {string} host
|
|
13
|
+
* @property {number} port
|
|
14
|
+
* @property {number} timeout_ms - give up on one request after this long
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {Object} TallyAnswer
|
|
19
|
+
* @property {number} status - HTTP status
|
|
20
|
+
* @property {string} text - the decoded answer
|
|
21
|
+
* @property {number} bytes - size of the raw answer
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Decodes Tally's answer. Tally answers in the request's encoding; a UTF-16 body has a
|
|
26
|
+
* BOM or NUL bytes every other byte (tally-to-duckdb, client.py).
|
|
27
|
+
* @param {Buffer} raw
|
|
28
|
+
* @returns {string}
|
|
29
|
+
*/
|
|
30
|
+
export function decodeAnswer(raw) {
|
|
31
|
+
if (raw.length >= 2 && raw[0] === 0xff && raw[1] === 0xfe) return raw.subarray(2).toString('utf16le');
|
|
32
|
+
if (raw.length >= 2 && raw[0] === 0xfe && raw[1] === 0xff) return Buffer.from(raw.subarray(2)).swap16().toString('utf16le');
|
|
33
|
+
if (raw.length >= 2 && raw[1] === 0x00) return raw.toString('utf16le');
|
|
34
|
+
return raw.toString('utf8');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A readable reason for a network error. Node reports a refused connection to "localhost"
|
|
39
|
+
* as an AggregateError with an empty message, one error per address tried.
|
|
40
|
+
* @param {any} err
|
|
41
|
+
* @returns {string}
|
|
42
|
+
*/
|
|
43
|
+
function describeNetworkError(err) {
|
|
44
|
+
if (err.message) return err.message;
|
|
45
|
+
if (Array.isArray(err.errors) && err.errors.length) return err.errors.map((e) => e.message || e.code).join('; ');
|
|
46
|
+
return err.code ?? 'unknown network error';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Posts an XML request to Tally and returns its decoded answer.
|
|
51
|
+
* @param {TallyConnection} connection
|
|
52
|
+
* @param {string} xml
|
|
53
|
+
* @returns {Promise<TallyAnswer>}
|
|
54
|
+
* @throws {TallyError} when Tally can't be reached, times out, or answers non-200
|
|
55
|
+
*/
|
|
56
|
+
export function postXml({ protocol = 'http', host, port, timeout_ms }, xml) {
|
|
57
|
+
const body = Buffer.from(xml, 'utf16le');
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
const request = (protocol === 'https' ? https : http).request(
|
|
60
|
+
{
|
|
61
|
+
host,
|
|
62
|
+
port,
|
|
63
|
+
path: '/',
|
|
64
|
+
method: 'POST',
|
|
65
|
+
headers: { 'Content-Type': 'text/xml;charset=utf-16', 'Content-Length': body.length },
|
|
66
|
+
timeout: timeout_ms,
|
|
67
|
+
},
|
|
68
|
+
(response) => {
|
|
69
|
+
const chunks = [];
|
|
70
|
+
response.on('data', (chunk) => chunks.push(chunk));
|
|
71
|
+
response.on('error', (err) => reject(new TallyError(`Tally's answer broke off: ${err.message}`, { code: 'TALLY_STREAM', cause: err })));
|
|
72
|
+
response.on('end', () => {
|
|
73
|
+
const raw = Buffer.concat(chunks);
|
|
74
|
+
const status = response.statusCode ?? 0;
|
|
75
|
+
if (status !== 200) {
|
|
76
|
+
reject(new TallyError(`Tally answered HTTP ${status} ${response.statusMessage ?? ''}`.trim(), { code: 'TALLY_HTTP' }));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
resolve({ status, text: decodeAnswer(raw), bytes: raw.length });
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
);
|
|
83
|
+
request.on('timeout', () => {
|
|
84
|
+
request.destroy(new TallyError(`Tally did not answer within ${Math.round(timeout_ms / 1000)} s`, { code: 'TALLY_TIMEOUT' }));
|
|
85
|
+
});
|
|
86
|
+
request.on('error', (err) => {
|
|
87
|
+
if (err instanceof TallyError) return reject(err);
|
|
88
|
+
reject(new TallyError(`Cannot reach Tally at ${host}:${port} (${describeNetworkError(err)}). Is Tally running, with its server on port ${port}?`, { code: 'TALLY_UNREACHABLE', cause: err }));
|
|
89
|
+
});
|
|
90
|
+
request.end(body);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// What the agent asks Tally for. Each function sends its requests through the gateway and
|
|
3
|
+
// returns plain data.
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
companyInfoXml,
|
|
7
|
+
isCompleteReport,
|
|
8
|
+
listCompaniesXml,
|
|
9
|
+
parseCompanyInfo,
|
|
10
|
+
parseCompanyList,
|
|
11
|
+
parseReport,
|
|
12
|
+
reportXml,
|
|
13
|
+
windowFilter,
|
|
14
|
+
} from './xml.js';
|
|
15
|
+
import { addDays } from './dates.js';
|
|
16
|
+
|
|
17
|
+
/** The most vouchers asked for in one request. A whole-year pull once hung Tally (tally-to-duckdb). */
|
|
18
|
+
export const DEFAULT_BATCH_SIZE = 5000;
|
|
19
|
+
|
|
20
|
+
/** @param {string} text */
|
|
21
|
+
const checkComplete = (text) => (isCompleteReport(text) ? null : `Tally's answer was cut off (${text.length} characters)`);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Names of the companies loaded in Tally right now.
|
|
25
|
+
* @param {import('./gateway.js').Gateway} gateway
|
|
26
|
+
* @returns {Promise<string[]>}
|
|
27
|
+
*/
|
|
28
|
+
export async function listCompanies(gateway) {
|
|
29
|
+
const text = await gateway.request('list companies', listCompaniesXml());
|
|
30
|
+
return parseCompanyList(text);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* One loaded company's GUID, dates and AlterID counters; null when it isn't loaded.
|
|
35
|
+
* @param {import('./gateway.js').Gateway} gateway
|
|
36
|
+
* @param {string} company
|
|
37
|
+
* @returns {Promise<import('./xml.js').CompanyInfo|null>}
|
|
38
|
+
*/
|
|
39
|
+
export async function getCompanyInfo(gateway, company) {
|
|
40
|
+
const text = await gateway.request(`company info "${company}"`, companyInfoXml(company));
|
|
41
|
+
return parseCompanyInfo(text);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Exports one table for a period.
|
|
46
|
+
* @param {import('./gateway.js').Gateway} gateway
|
|
47
|
+
* @param {import('./xml.js').TableDefinition} table
|
|
48
|
+
* @param {{company: string, from_date: string, to_date: string, extra_filters?: string[]}} options
|
|
49
|
+
* @returns {Promise<Record<string, string|number|boolean|null>[]>}
|
|
50
|
+
*/
|
|
51
|
+
export async function fetchTable(gateway, table, options) {
|
|
52
|
+
const text = await gateway.request(`table ${table.name}`, reportXml(table, options), { check: checkComplete });
|
|
53
|
+
return parseReport(table, text);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The date-only export used to count vouchers per day before pulling them. */
|
|
57
|
+
const VOUCHER_DATES_TABLE = {
|
|
58
|
+
name: 'voucher_dates',
|
|
59
|
+
collection: 'Voucher',
|
|
60
|
+
filters: ['NOT $IsCancelled', 'NOT $IsOptional'],
|
|
61
|
+
fields: [{ name: 'date', field: 'Date', type: /** @type {const} */ ('date') }],
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @typedef {Object} VoucherCounts
|
|
66
|
+
* @property {number} total - every voucher in the period, including any without a date
|
|
67
|
+
* @property {Record<string, number>} by_date - ISO date → vouchers on that day
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Counts the company's vouchers per day: what date windows are sized from, and the total a
|
|
72
|
+
* full pull must reproduce.
|
|
73
|
+
* @param {import('./gateway.js').Gateway} gateway
|
|
74
|
+
* @param {{company: string, from_date: string, to_date: string}} options
|
|
75
|
+
* @returns {Promise<VoucherCounts>}
|
|
76
|
+
*/
|
|
77
|
+
export async function countVouchersByDate(gateway, options) {
|
|
78
|
+
const rows = await fetchTable(gateway, VOUCHER_DATES_TABLE, options);
|
|
79
|
+
/** @type {Record<string, number>} */
|
|
80
|
+
const by_date = {};
|
|
81
|
+
for (const { date } of rows) {
|
|
82
|
+
if (typeof date === 'string') by_date[date] = (by_date[date] ?? 0) + 1;
|
|
83
|
+
}
|
|
84
|
+
return { total: rows.length, by_date };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @typedef {Object} DateWindow
|
|
89
|
+
* @property {string} from_date
|
|
90
|
+
* @property {string} to_date
|
|
91
|
+
* @property {number} count - vouchers Tally counted in the window
|
|
92
|
+
*/
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Splits a period into contiguous windows of at most batch_size vouchers. The windows cover
|
|
96
|
+
* from_date..to_date exactly, each ending the day before the next begins, so no voucher can
|
|
97
|
+
* fall between two (tally-to-duckdb, date_windows). A single day with more vouchers than
|
|
98
|
+
* batch_size gets a window of its own.
|
|
99
|
+
* @param {Record<string, number>} by_date
|
|
100
|
+
* @param {{from_date: string, to_date: string, batch_size?: number}} options
|
|
101
|
+
* @returns {DateWindow[]}
|
|
102
|
+
*/
|
|
103
|
+
export function planDateWindows(by_date, { from_date, to_date, batch_size = DEFAULT_BATCH_SIZE }) {
|
|
104
|
+
const days = Object.keys(by_date).filter((day) => day >= from_date && day <= to_date).sort();
|
|
105
|
+
/** @type {{start: string, count: number}[]} */
|
|
106
|
+
const batches = [];
|
|
107
|
+
for (const day of days) {
|
|
108
|
+
const last = batches.at(-1);
|
|
109
|
+
if (last && last.count + by_date[day] <= batch_size) last.count += by_date[day];
|
|
110
|
+
else batches.push({ start: day, count: by_date[day] });
|
|
111
|
+
}
|
|
112
|
+
if (batches.length <= 1) return [{ from_date, to_date, count: batches[0]?.count ?? 0 }];
|
|
113
|
+
return batches.map((batch, i) => ({
|
|
114
|
+
from_date: i === 0 ? from_date : batch.start,
|
|
115
|
+
to_date: i === batches.length - 1 ? to_date : addDays(batches[i + 1].start, -1),
|
|
116
|
+
count: batch.count,
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Exports one transaction table for one date window.
|
|
122
|
+
* @param {import('./gateway.js').Gateway} gateway
|
|
123
|
+
* @param {import('./xml.js').TableDefinition} table
|
|
124
|
+
* @param {{company: string, window: DateWindow}} options
|
|
125
|
+
*/
|
|
126
|
+
export function fetchTransactionWindow(gateway, table, { company, window }) {
|
|
127
|
+
return fetchTable(gateway, table, {
|
|
128
|
+
company,
|
|
129
|
+
from_date: window.from_date,
|
|
130
|
+
to_date: window.to_date,
|
|
131
|
+
extra_filters: [windowFilter(window.from_date, window.to_date)],
|
|
132
|
+
});
|
|
133
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Typed errors for talking to Tally, so callers can tell "Tally isn't there" from
|
|
3
|
+
// "Tally answered badly".
|
|
4
|
+
|
|
5
|
+
/** Tally could not be reached, answered with a non-200 status, or timed out. */
|
|
6
|
+
export class TallyError extends Error {
|
|
7
|
+
/**
|
|
8
|
+
* @param {string} message
|
|
9
|
+
* @param {{code: string, cause?: unknown}} details
|
|
10
|
+
*/
|
|
11
|
+
constructor(message, { code, cause }) {
|
|
12
|
+
super(message, { cause });
|
|
13
|
+
this.name = 'TallyError';
|
|
14
|
+
this.code = code;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Tally answered, but the answer was cut short. Asking again usually works. */
|
|
19
|
+
export class TallyIncompleteError extends TallyError {
|
|
20
|
+
/** @param {string} message */
|
|
21
|
+
constructor(message) {
|
|
22
|
+
super(message, { code: 'TALLY_INCOMPLETE' });
|
|
23
|
+
this.name = 'TallyIncompleteError';
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/tally/xml.js
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Building Tally XML requests and reading their answers. Pure functions only.
|
|
3
|
+
//
|
|
4
|
+
// Every TDL definition we send (report, form, part, line, field, collection, filter) is
|
|
5
|
+
// named TallySync…: a TDL add-on loaded in the client's Tally can define the loader's generic
|
|
6
|
+
// names (MyForm, MyPart, MyCollection), and its definitions then win over ours.
|
|
7
|
+
//
|
|
8
|
+
// The request shapes follow tally-database-loader (tally.mts: generateXMLfromYAML,
|
|
9
|
+
// processTdlOutputManipulation, saveCompanyInfo) and tally-to-duckdb's port of it
|
|
10
|
+
// (client.py), both MIT. See NOTICE.
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {Object} FieldDefinition
|
|
14
|
+
* @property {string} name - column name
|
|
15
|
+
* @property {string} field - Tally field name, or a full TDL expression
|
|
16
|
+
* @property {'text'|'logical'|'date'|'number'|'amount'|'quantity'|'rate'} type
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {Object} TableDefinition
|
|
21
|
+
* @property {string} name - table name, e.g. mst_ledger
|
|
22
|
+
* @property {string} collection - Tally collection, dotted for sub-collections (Voucher.AllLedgerEntries)
|
|
23
|
+
* @property {string} [nature] - Primary or Derived
|
|
24
|
+
* @property {string[]} [fetch]
|
|
25
|
+
* @property {string[]} [filters] - TDL filter expressions
|
|
26
|
+
* @property {FieldDefinition[]} fields
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/** Tally's "empty date" mark: the date field expression returns $$StrByCharCode:241. */
|
|
30
|
+
export const NULL_DATE_MARK = 'ñ';
|
|
31
|
+
|
|
32
|
+
const XML_ESCAPES = [
|
|
33
|
+
['&', '&'],
|
|
34
|
+
['<', '<'],
|
|
35
|
+
['>', '>'],
|
|
36
|
+
['"', '"'],
|
|
37
|
+
["'", '''],
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Escapes a value for use inside Tally XML.
|
|
42
|
+
* @param {string} value
|
|
43
|
+
* @returns {string}
|
|
44
|
+
*/
|
|
45
|
+
export function escapeXml(value) {
|
|
46
|
+
return XML_ESCAPES.reduce((text, [from, to]) => text.replaceAll(from, to), value);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Reverses escapeXml.
|
|
51
|
+
* @param {string} value
|
|
52
|
+
* @returns {string}
|
|
53
|
+
*/
|
|
54
|
+
export function unescapeXml(value) {
|
|
55
|
+
return [...XML_ESCAPES].reverse().reduce((text, [to, from]) => text.replaceAll(from, to), value);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Formats an ISO date (yyyy-mm-dd) the way Tally's SVFROMDATE/SVTODATE expect: 1-Apr-2025.
|
|
62
|
+
* @param {string} iso_date
|
|
63
|
+
* @returns {string}
|
|
64
|
+
*/
|
|
65
|
+
export function formatTallyDate(iso_date) {
|
|
66
|
+
const [year, month, day] = iso_date.split('-').map(Number);
|
|
67
|
+
return `${day}-${MONTHS[month - 1]}-${year}`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Converts a yyyymmdd string or number to an ISO date.
|
|
72
|
+
* @param {string|number} yyyymmdd
|
|
73
|
+
* @returns {string}
|
|
74
|
+
*/
|
|
75
|
+
export function yyyymmddToIso(yyyymmdd) {
|
|
76
|
+
const text = String(yyyymmdd).trim();
|
|
77
|
+
return `${text.slice(0, 4)}-${text.slice(4, 6)}-${text.slice(6, 8)}`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The XML envelope for an export request.
|
|
82
|
+
* @param {{type: 'Data'|'Collection'|'Function', id: string, static_variables?: string, tdl?: string}} parts
|
|
83
|
+
* @returns {string}
|
|
84
|
+
*/
|
|
85
|
+
function exportEnvelope({ type, id, static_variables = '', tdl = '' }) {
|
|
86
|
+
const desc_static = static_variables ? `<STATICVARIABLES>${static_variables}</STATICVARIABLES>` : '';
|
|
87
|
+
const desc_tdl = tdl ? `<TDL><TDLMESSAGE>${tdl}</TDLMESSAGE></TDL>` : '';
|
|
88
|
+
return (
|
|
89
|
+
'<?xml version="1.0" encoding="utf-8"?><ENVELOPE><HEADER><VERSION>1</VERSION>' +
|
|
90
|
+
`<TALLYREQUEST>Export</TALLYREQUEST><TYPE>${type}</TYPE><ID>${escapeXml(id)}</ID></HEADER>` +
|
|
91
|
+
`<BODY><DESC>${desc_static}${desc_tdl}</DESC></BODY></ENVELOPE>`
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Request for the companies loaded in Tally right now.
|
|
97
|
+
* @returns {string}
|
|
98
|
+
*/
|
|
99
|
+
export function listCompaniesXml() {
|
|
100
|
+
return exportEnvelope({
|
|
101
|
+
type: 'Collection',
|
|
102
|
+
id: 'List of Companies',
|
|
103
|
+
static_variables: '<SVEXPORTFORMAT>$$SysName:XML</SVEXPORTFORMAT>',
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Reads company names from a List of Companies answer, in Tally's exact spelling.
|
|
109
|
+
* @param {string} response_text
|
|
110
|
+
* @returns {string[]}
|
|
111
|
+
*/
|
|
112
|
+
export function parseCompanyList(response_text) {
|
|
113
|
+
return [...response_text.matchAll(/<COMPANY NAME="([^"]+)"/g)].map((match) => unescapeXml(match[1]));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const COMPANY_INFO_FIELDS =
|
|
117
|
+
'<FIELD NAME="TallySyncFldGuid"><SET>$Guid</SET></FIELD>' +
|
|
118
|
+
'<FIELD NAME="TallySyncFldName"><SET>$$StringFindAndReplace:$Name:\'"\':\'""\'</SET></FIELD>' +
|
|
119
|
+
'<FIELD NAME="TallySyncFldBooksFrom"><SET>(($$YearOfDate:$BooksFrom)*10000)+(($$MonthOfDate:$BooksFrom)*100)+(($$DayOfDate:$BooksFrom)*1)</SET></FIELD>' +
|
|
120
|
+
'<FIELD NAME="TallySyncFldLastVoucherDate"><SET>(($$YearOfDate:$LastVoucherDate)*10000)+(($$MonthOfDate:$LastVoucherDate)*100)+(($$DayOfDate:$LastVoucherDate)*1)</SET></FIELD>' +
|
|
121
|
+
'<FIELD NAME="TallySyncFldLastAlterIdMaster"><SET>$AltMstId</SET></FIELD>' +
|
|
122
|
+
'<FIELD NAME="TallySyncFldLastAlterIdTransaction"><SET>$AltVchId</SET></FIELD>' +
|
|
123
|
+
'<FIELD NAME="TallySyncFldEOL"><SET>†</SET></FIELD>';
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Request for one company's GUID, books-from date, last voucher date and AlterID counters.
|
|
127
|
+
* The loader's saveCompanyInfo request, filtered to the named company.
|
|
128
|
+
* @param {string} company
|
|
129
|
+
* @returns {string}
|
|
130
|
+
*/
|
|
131
|
+
export function companyInfoXml(company) {
|
|
132
|
+
return exportEnvelope({
|
|
133
|
+
type: 'Data',
|
|
134
|
+
id: 'TallySyncCompanyInfo',
|
|
135
|
+
static_variables: '<SVEXPORTFORMAT>ASCII (Comma Delimited)</SVEXPORTFORMAT>',
|
|
136
|
+
tdl:
|
|
137
|
+
'<REPORT NAME="TallySyncCompanyInfo"><FORMS>TallySyncForm</FORMS></REPORT>' +
|
|
138
|
+
'<FORM NAME="TallySyncForm"><PARTS>TallySyncPart</PARTS></FORM>' +
|
|
139
|
+
'<PART NAME="TallySyncPart"><LINES>TallySyncLine</LINES><REPEAT>TallySyncLine : TallySyncCollection</REPEAT><SCROLLED>Vertical</SCROLLED></PART>' +
|
|
140
|
+
'<LINE NAME="TallySyncLine"><FIELDS>TallySyncFldGuid,TallySyncFldName,TallySyncFldBooksFrom,TallySyncFldLastVoucherDate,TallySyncFldLastAlterIdMaster,TallySyncFldLastAlterIdTransaction,TallySyncFldEOL</FIELDS></LINE>' +
|
|
141
|
+
COMPANY_INFO_FIELDS +
|
|
142
|
+
'<COLLECTION NAME="TallySyncCollection"><TYPE>Company</TYPE><FILTER>TallySyncFilterCompany</FILTER></COLLECTION>' +
|
|
143
|
+
`<SYSTEM TYPE="Formulae" NAME="TallySyncFilterCompany">$$IsEqual:"${escapeXml(company)}":$Name</SYSTEM>`,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @typedef {Object} CompanyInfo
|
|
149
|
+
* @property {string} guid
|
|
150
|
+
* @property {string} name
|
|
151
|
+
* @property {string} books_from - ISO date
|
|
152
|
+
* @property {string} last_voucher_date - ISO date
|
|
153
|
+
* @property {number} alter_id_master - the company's AltMstId
|
|
154
|
+
* @property {number} alter_id_transaction - the company's AltVchId
|
|
155
|
+
*/
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Reads a company info answer. Returns null when the company isn't loaded (Tally sends no row).
|
|
159
|
+
* @param {string} response_text
|
|
160
|
+
* @returns {CompanyInfo|null}
|
|
161
|
+
* @throws {Error} when the answer is something else entirely (another TDL answered instead of ours)
|
|
162
|
+
*/
|
|
163
|
+
export function parseCompanyInfo(response_text) {
|
|
164
|
+
const text = response_text.trim();
|
|
165
|
+
if (text === '') return null;
|
|
166
|
+
if (!text.includes('"†"')) throw new Error(`Tally answered the company info request with something else: ${JSON.stringify(text.slice(0, 120))}`);
|
|
167
|
+
const parts = text.split('","').map((part) => part.trim().replace(/^"|"$/g, ''));
|
|
168
|
+
return {
|
|
169
|
+
guid: parts[0],
|
|
170
|
+
name: parts[1].replaceAll('""', '"'),
|
|
171
|
+
books_from: yyyymmddToIso(parts[2]),
|
|
172
|
+
last_voucher_date: yyyymmddToIso(parts[3]),
|
|
173
|
+
alter_id_master: Number.parseInt(parts[4], 10) || 0,
|
|
174
|
+
alter_id_transaction: Number.parseInt(parts[5], 10) || 0,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const PLAIN_FIELD = /^(\.\.)?[a-zA-Z0-9_]+$/;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The TDL expression for one field, by type. A field that is already an expression is used as is.
|
|
182
|
+
* @param {FieldDefinition} field
|
|
183
|
+
* @returns {string}
|
|
184
|
+
*/
|
|
185
|
+
export function fieldExpression({ field, type }) {
|
|
186
|
+
if (!PLAIN_FIELD.test(field)) return field;
|
|
187
|
+
const expressions = {
|
|
188
|
+
text: `$${field}`,
|
|
189
|
+
logical: `if $${field} then 1 else 0`,
|
|
190
|
+
date: `if $$IsEmpty:$${field} then $$StrByCharCode:241 else $$PyrlYYYYMMDDFormat:$${field}:"-"`,
|
|
191
|
+
number: `if $$IsEmpty:$${field} then "0" else $$StringFindAndReplace:($$String:$${field}):"(-)":"-"`,
|
|
192
|
+
amount: `$$StringFindAndReplace:(if $$IsDebit:$${field} then -$$NumValue:$${field} else $$NumValue:$${field}):"(-)":"-"`,
|
|
193
|
+
quantity: `$$StringFindAndReplace:(if $$IsInwards:$${field} then $$Number:$$String:$${field}:"TailUnits" else -$$Number:$$String:$${field}:"TailUnits"):"(-)":"-"`,
|
|
194
|
+
rate: `if $$IsEmpty:$${field} then 0 else $$Number:$${field}`,
|
|
195
|
+
};
|
|
196
|
+
return expressions[type] ?? field;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** @param {number} n */
|
|
200
|
+
const pad2 = (n) => String(n).padStart(2, '0');
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* A report request that exports one table: one line per row, one <Fnn> tag per field.
|
|
204
|
+
* Line for line the loader's generateXMLfromYAML, with the company always named.
|
|
205
|
+
* @param {TableDefinition} table
|
|
206
|
+
* @param {{company: string, from_date: string, to_date: string, extra_filters?: string[]}} options
|
|
207
|
+
* from_date / to_date are ISO dates; extra_filters are added to the table's own
|
|
208
|
+
* @returns {string}
|
|
209
|
+
*/
|
|
210
|
+
export function reportXml(table, { company, from_date, to_date, extra_filters = [] }) {
|
|
211
|
+
const routes = table.collection.split('.');
|
|
212
|
+
const target_collection = routes.shift();
|
|
213
|
+
routes.unshift('TallySyncCollection');
|
|
214
|
+
|
|
215
|
+
const parts = routes
|
|
216
|
+
.map((route, i) => `<PART NAME="TallySyncPart${pad2(i + 1)}"><LINES>TallySyncLine${pad2(i + 1)}</LINES><REPEAT>TallySyncLine${pad2(i + 1)} : ${route}</REPEAT><SCROLLED>Vertical</SCROLLED></PART>`)
|
|
217
|
+
.join('');
|
|
218
|
+
const explode_lines = routes
|
|
219
|
+
.slice(0, -1)
|
|
220
|
+
.map((_, i) => `<LINE NAME="TallySyncLine${pad2(i + 1)}"><FIELDS>TallySyncFldBlank</FIELDS><EXPLODE>TallySyncPart${pad2(i + 2)}</EXPLODE></LINE>`)
|
|
221
|
+
.join('');
|
|
222
|
+
const field_names = table.fields.map((_, i) => `TallySyncFld${pad2(i + 1)}`).join(',');
|
|
223
|
+
const data_line = `<LINE NAME="TallySyncLine${pad2(routes.length)}"><FIELDS>${field_names}</FIELDS></LINE>`;
|
|
224
|
+
const fields = table.fields
|
|
225
|
+
.map((field, i) => `<FIELD NAME="TallySyncFld${pad2(i + 1)}"><SET>${fieldExpression(field)}</SET><XMLTAG>F${pad2(i + 1)}</XMLTAG></FIELD>`)
|
|
226
|
+
.join('');
|
|
227
|
+
|
|
228
|
+
const filters = [...(table.filters ?? []), ...extra_filters];
|
|
229
|
+
const fetch = table.fetch?.length ? `<FETCH>${table.fetch.join(',')}</FETCH>` : '';
|
|
230
|
+
const filter_refs = filters.length ? `<FILTER>${filters.map((_, j) => `TallySyncFltr${pad2(j + 1)}`).join(',')}</FILTER>` : '';
|
|
231
|
+
const filter_defs = filters.map((filter, j) => `<SYSTEM TYPE="Formulae" NAME="TallySyncFltr${pad2(j + 1)}">${filter}</SYSTEM>`).join('');
|
|
232
|
+
|
|
233
|
+
return exportEnvelope({
|
|
234
|
+
type: 'Data',
|
|
235
|
+
id: 'TallySyncReport',
|
|
236
|
+
static_variables:
|
|
237
|
+
'<SVEXPORTFORMAT>XML (Data Interchange)</SVEXPORTFORMAT>' +
|
|
238
|
+
`<SVFROMDATE>${formatTallyDate(from_date)}</SVFROMDATE><SVTODATE>${formatTallyDate(to_date)}</SVTODATE>` +
|
|
239
|
+
`<SVCURRENTCOMPANY>${escapeXml(company)}</SVCURRENTCOMPANY>`,
|
|
240
|
+
tdl:
|
|
241
|
+
'<REPORT NAME="TallySyncReport"><FORMS>TallySyncForm</FORMS></REPORT><FORM NAME="TallySyncForm"><PARTS>TallySyncPart01</PARTS></FORM>' +
|
|
242
|
+
parts +
|
|
243
|
+
explode_lines +
|
|
244
|
+
data_line +
|
|
245
|
+
fields +
|
|
246
|
+
'<FIELD NAME="TallySyncFldBlank"><SET>""</SET></FIELD>' +
|
|
247
|
+
`<COLLECTION NAME="TallySyncCollection"><TYPE>${target_collection}</TYPE>${fetch}${filter_refs}</COLLECTION>` +
|
|
248
|
+
filter_defs,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Whether a report answer arrived whole. Tally under load returns answers cut short.
|
|
254
|
+
* @param {string} response_text
|
|
255
|
+
* @returns {boolean}
|
|
256
|
+
*/
|
|
257
|
+
export function isCompleteReport(response_text) {
|
|
258
|
+
return response_text.trimEnd().endsWith('</ENVELOPE>');
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Turns a report answer into tab-separated lines, one per row: the loader's
|
|
263
|
+
* processTdlOutputManipulation. Tally writes no tabs or line breaks inside values.
|
|
264
|
+
* @param {string} response_text
|
|
265
|
+
* @returns {string[][]} rows of raw field values
|
|
266
|
+
*/
|
|
267
|
+
export function parseReportRows(response_text) {
|
|
268
|
+
const text = response_text
|
|
269
|
+
.replace('<ENVELOPE>', '')
|
|
270
|
+
.replace('</ENVELOPE>', '')
|
|
271
|
+
.replaceAll('<FLDBLANK></FLDBLANK>', '')
|
|
272
|
+
.replace(/\s+\r\n/g, '')
|
|
273
|
+
.replaceAll('\r\n', '')
|
|
274
|
+
.replaceAll('\t', ' ')
|
|
275
|
+
.replace(/\s+<F/g, '<F')
|
|
276
|
+
.replace(/<\/F\d+>/g, '')
|
|
277
|
+
.replaceAll('<F01>', '\r\n')
|
|
278
|
+
.replace(/<F\d+>/g, '\t')
|
|
279
|
+
.replaceAll('&tab;', '');
|
|
280
|
+
return text
|
|
281
|
+
.split('\r\n')
|
|
282
|
+
.slice(1)
|
|
283
|
+
.map((line) => line.split('\t').map((value) => unescapeXml(value).replace(/&#\d+;/g, '')));
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Converts one raw value by its field type. Amounts are returned as numbers with Tally's
|
|
288
|
+
* sign kept (negative = debit).
|
|
289
|
+
* @param {string} raw
|
|
290
|
+
* @param {FieldDefinition['type']} type
|
|
291
|
+
* @returns {string|number|boolean|null}
|
|
292
|
+
*/
|
|
293
|
+
export function convertValue(raw, type) {
|
|
294
|
+
const value = raw.trim();
|
|
295
|
+
if (type === 'logical') return value === '1';
|
|
296
|
+
if (type === 'date') return value === '' || value === NULL_DATE_MARK ? null : value;
|
|
297
|
+
if (type === 'number' || type === 'amount' || type === 'quantity' || type === 'rate') {
|
|
298
|
+
const number = Number.parseFloat(value);
|
|
299
|
+
return Number.isNaN(number) ? 0 : number;
|
|
300
|
+
}
|
|
301
|
+
return raw;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Reads a report answer into objects keyed by the table's column names.
|
|
306
|
+
* @param {TableDefinition} table
|
|
307
|
+
* @param {string} response_text
|
|
308
|
+
* @returns {Record<string, string|number|boolean|null>[]}
|
|
309
|
+
*/
|
|
310
|
+
export function parseReport(table, response_text) {
|
|
311
|
+
return parseReportRows(response_text).map((values) =>
|
|
312
|
+
Object.fromEntries(table.fields.map((field, i) => [field.name, convertValue(values[i] ?? '', field.type)])),
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* A TDL filter keeping vouchers dated from_date..to_date inclusive. Tally ignores
|
|
318
|
+
* SVFROMDATE/SVTODATE for voucher collections, and a date-literal comparison returned
|
|
319
|
+
* nothing, so the date is compared as a yyyymmdd number (tally-to-duckdb, client.py).
|
|
320
|
+
* @param {string} from_date - ISO date
|
|
321
|
+
* @param {string} to_date - ISO date
|
|
322
|
+
* @returns {string}
|
|
323
|
+
*/
|
|
324
|
+
export function windowFilter(from_date, to_date) {
|
|
325
|
+
const as_number = '(($$YearOfDate:$Date)*10000+($$MonthOfDate:$Date)*100+$$DayOfDate:$Date)';
|
|
326
|
+
const lo = from_date.replaceAll('-', '');
|
|
327
|
+
const hi = to_date.replaceAll('-', '');
|
|
328
|
+
return `${as_number} >= ${lo} AND ${as_number} <= ${hi}`;
|
|
329
|
+
}
|