You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
319 lines
14 KiB
TypeScript
319 lines
14 KiB
TypeScript
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import { fromRupees, fyOf, uuidv7, validateGstin } from '@sims/domain'
|
|
import { writeAudit } from './audit'
|
|
import { openDb, type DB } from './db'
|
|
import { seedIfEmpty } from './seed'
|
|
import { seedSeries } from './series'
|
|
|
|
/**
|
|
* APEX cutover importer (spec §4): stage CSVs → verification report →
|
|
* commit + INVOICE series seed. Staged rows carry their own problem list;
|
|
* commit refuses to run until every staged row is clean.
|
|
*/
|
|
|
|
// ---------- tiny CSV parser (header row, comma, double-quote escaping) ----------
|
|
|
|
function parseCsv(text: string): Record<string, string>[] {
|
|
const rows: string[][] = []
|
|
let field = ''
|
|
let row: string[] = []
|
|
let inQuotes = false
|
|
const endField = (): void => { row.push(field); field = '' }
|
|
const endRow = (): void => { endField(); rows.push(row); row = [] }
|
|
for (let i = 0; i < text.length; i++) {
|
|
const ch = text[i]!
|
|
if (inQuotes) {
|
|
if (ch === '"') {
|
|
if (text[i + 1] === '"') { field += '"'; i++ } else inQuotes = false
|
|
} else field += ch
|
|
} else if (ch === '"') inQuotes = true
|
|
else if (ch === ',') endField()
|
|
else if (ch === '\n') endRow()
|
|
else if (ch !== '\r') field += ch
|
|
}
|
|
if (field !== '' || row.length > 0) endRow()
|
|
const header = (rows.shift() ?? []).map((h) => h.trim())
|
|
return rows
|
|
.filter((r) => r.some((c) => c.trim() !== ''))
|
|
.map((r) => Object.fromEntries(header.map((h, i) => [h, (r[i] ?? '').trim()])))
|
|
}
|
|
|
|
// ---------- staging ----------
|
|
|
|
const CLIENT_STATUSES = new Set(['lead', 'active', 'dormant', 'lost'])
|
|
const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/
|
|
|
|
async function stageClients(db: DB, records: Record<string, string>[]): Promise<number> {
|
|
await db.run(`DELETE FROM stg_client`)
|
|
const existing = new Set(
|
|
(await db.all<{ code: string }>(`SELECT code FROM client`)).map((r) => r.code),
|
|
)
|
|
const seen = new Set<string>()
|
|
const INSERT_SQL =
|
|
`INSERT INTO stg_client (row_no, code, name, gstin, state_code, address, phone, email, status,
|
|
anydesk, os, district, sector, problems)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
for (let i = 0; i < records.length; i++) {
|
|
const r = records[i]!
|
|
const problems: string[] = []
|
|
const code = r['code'] ?? ''
|
|
if (code === '') problems.push('missing code')
|
|
else if (seen.has(code) || existing.has(code)) problems.push(`duplicate code: ${code}`)
|
|
if (code !== '') seen.add(code)
|
|
if ((r['name'] ?? '') === '') problems.push('missing name')
|
|
const gstin = r['gstin'] ?? ''
|
|
if (gstin !== '' && !validateGstin(gstin).ok) problems.push(`bad GSTIN: ${gstin}`)
|
|
const status = r['status'] ?? ''
|
|
if (status !== '' && !CLIENT_STATUSES.has(status)) problems.push(`bad status: ${status}`)
|
|
await db.run(
|
|
INSERT_SQL,
|
|
i + 1, code, r['name'] ?? '', gstin, r['state_code'] ?? '',
|
|
r['address'] ?? '', r['phone'] ?? '', r['email'] ?? '', status,
|
|
// WS-F support-access fields — optional headers; absent columns stage as ''.
|
|
r['anydesk'] ?? '', r['os'] ?? '', r['district'] ?? '', r['sector'] ?? '',
|
|
JSON.stringify(problems),
|
|
)
|
|
}
|
|
return records.length
|
|
}
|
|
|
|
function paiseOrNull(raw: string): number | null {
|
|
if (raw === '') return null
|
|
const n = Number(raw)
|
|
return Number.isFinite(n) ? fromRupees(n) : null
|
|
}
|
|
|
|
async function stageInvoices(db: DB, records: Record<string, string>[]): Promise<number> {
|
|
await db.run(`DELETE FROM stg_invoice`)
|
|
const existing = new Set(
|
|
(await db.all<{ doc_no: string }>(`SELECT doc_no FROM document WHERE doc_no IS NOT NULL`))
|
|
.map((r) => r.doc_no),
|
|
)
|
|
const seen = new Set<string>()
|
|
const INSERT_SQL =
|
|
`INSERT INTO stg_invoice (row_no, client_code, doc_no, doc_date, taxable_paise, tax_paise, total_paise, paid, problems)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
for (let i = 0; i < records.length; i++) {
|
|
const r = records[i]!
|
|
const problems: string[] = []
|
|
if ((r['client_code'] ?? '') === '') problems.push('missing client_code')
|
|
const docNo = r['doc_no'] ?? ''
|
|
if (docNo === '') problems.push('missing doc_no')
|
|
else if (seen.has(docNo) || existing.has(docNo)) problems.push(`duplicate doc_no: ${docNo}`)
|
|
if (docNo !== '') seen.add(docNo)
|
|
const docDate = r['doc_date'] ?? ''
|
|
if (!ISO_DATE.test(docDate) || Number.isNaN(Date.parse(docDate))) problems.push(`bad date: ${docDate}`)
|
|
const taxable = paiseOrNull(r['taxable'] ?? '')
|
|
const tax = paiseOrNull(r['tax'] ?? '')
|
|
const total = paiseOrNull(r['total'] ?? '')
|
|
if (taxable === null) problems.push(`bad taxable: ${r['taxable'] ?? ''}`)
|
|
if (tax === null) problems.push(`bad tax: ${r['tax'] ?? ''}`)
|
|
if (total === null) problems.push(`bad total: ${r['total'] ?? ''}`)
|
|
await db.run(
|
|
INSERT_SQL,
|
|
i + 1, r['client_code'] ?? '', docNo, docDate,
|
|
taxable, tax, total, r['paid'] === '1' ? 1 : 0,
|
|
JSON.stringify(problems),
|
|
)
|
|
}
|
|
return records.length
|
|
}
|
|
|
|
export async function stageCsv(db: DB, kind: 'clients' | 'invoices', csvText: string): Promise<{ staged: number }> {
|
|
const records = parseCsv(csvText)
|
|
// One transaction: the DELETE + re-insert of the staging area and its audit row
|
|
// land (or roll back) together — same-txn audit rule.
|
|
return db.transaction(async () => {
|
|
const staged = kind === 'clients' ? await stageClients(db, records) : await stageInvoices(db, records)
|
|
await writeAudit(db, 'system', 'stage', `stg_${kind}`, kind, undefined, { staged })
|
|
return { staged }
|
|
})
|
|
}
|
|
|
|
// ---------- verification report ----------
|
|
|
|
export interface VerificationReport {
|
|
clients: { staged: number; problems: number }
|
|
invoices: { staged: number; problems: number; totalPaise: number }
|
|
samples: { firstClients: string[]; lastInvoices: string[] }
|
|
}
|
|
|
|
export async function verificationReport(db: DB): Promise<VerificationReport> {
|
|
const c = (await db.get<{ staged: number; problems: number }>(
|
|
`SELECT COUNT(*) AS staged,
|
|
COALESCE(SUM(CASE WHEN problems <> '[]' THEN 1 ELSE 0 END), 0) AS problems
|
|
FROM stg_client`,
|
|
))!
|
|
const i = (await db.get<{ staged: number; problems: number; totalPaise: number }>(
|
|
`SELECT COUNT(*) AS staged,
|
|
COALESCE(SUM(CASE WHEN problems <> '[]' THEN 1 ELSE 0 END), 0) AS problems,
|
|
COALESCE(SUM(total_paise), 0) AS totalPaise
|
|
FROM stg_invoice`,
|
|
))!
|
|
const firstClients = (await db.all<{ code: string; name: string }>(
|
|
`SELECT code, name FROM stg_client ORDER BY row_no LIMIT 5`,
|
|
)).map((r) => `${r.code} ${r.name}`.trim())
|
|
const lastInvoices = (await db.all<{ doc_no: string }>(
|
|
`SELECT doc_no FROM stg_invoice ORDER BY row_no DESC LIMIT 5`,
|
|
)).map((r) => r.doc_no)
|
|
return {
|
|
clients: { staged: c.staged, problems: c.problems },
|
|
invoices: { staged: i.staged, problems: i.problems, totalPaise: i.totalPaise },
|
|
samples: { firstClients, lastInvoices },
|
|
}
|
|
}
|
|
|
|
// ---------- commit ----------
|
|
|
|
interface StgClientRow {
|
|
row_no: number; code: string; name: string; gstin: string; state_code: string
|
|
address: string; phone: string; email: string; status: string
|
|
anydesk: string; os: string; district: string; sector: string; problems: string
|
|
}
|
|
|
|
interface StgInvoiceRow {
|
|
row_no: number; client_code: string; doc_no: string; doc_date: string
|
|
taxable_paise: number; tax_paise: number; total_paise: number; paid: number; problems: string
|
|
}
|
|
|
|
export interface CommitResult {
|
|
clients: number
|
|
invoices: number
|
|
seeded: { fy: string; lastSeq: number } | null
|
|
}
|
|
|
|
async function companyStateCode(db: DB): Promise<string> {
|
|
const row = await db.get<{ value: string }>(`SELECT value FROM setting WHERE key='company.state_code'`)
|
|
// Silent defaults on place-of-supply are how wrong GST reaches the portal — fail loudly.
|
|
if (row === undefined) throw new Error(`Setting 'company.state_code' is not configured`)
|
|
return row.value
|
|
}
|
|
|
|
export async function commitImport(db: DB, userId: string): Promise<CommitResult> {
|
|
return db.transaction(async (): Promise<CommitResult> => {
|
|
const bad = (await db.get<{ n: number }>(
|
|
`SELECT (SELECT COUNT(*) FROM stg_client WHERE problems <> '[]')
|
|
+ (SELECT COUNT(*) FROM stg_invoice WHERE problems <> '[]') AS n`,
|
|
))!.n
|
|
if (bad > 0) throw new Error(`${bad} staged row(s) have problems — fix the CSV and re-stage before commit`)
|
|
|
|
const ourState = await companyStateCode(db)
|
|
const now = new Date().toISOString()
|
|
|
|
// Clients — source 'apex' marks the cutover rows.
|
|
const stgClients = await db.all<StgClientRow>(`SELECT * FROM stg_client ORDER BY row_no`)
|
|
const INSERT_CLIENT_SQL =
|
|
`INSERT INTO client (id, code, name, gstin, state_code, address, contacts, status,
|
|
anydesk, os, district, sector, source, created_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'apex', ?)`
|
|
const orNull = (v: string): string | null => (v !== '' ? v : null)
|
|
for (const c of stgClients) {
|
|
const id = uuidv7()
|
|
const contacts = c.phone !== '' || c.email !== ''
|
|
? [{
|
|
name: c.name,
|
|
...(c.phone !== '' ? { phone: c.phone } : {}),
|
|
...(c.email !== '' ? { email: c.email } : {}),
|
|
}]
|
|
: []
|
|
await db.run(
|
|
INSERT_CLIENT_SQL,
|
|
id, c.code, c.name, c.gstin !== '' ? c.gstin : null,
|
|
c.state_code !== '' ? c.state_code : ourState, c.address,
|
|
JSON.stringify(contacts), c.status !== '' ? c.status : 'active',
|
|
// WS-F: the old book's support-access data rides the cutover, not re-keying.
|
|
orNull(c.anydesk), orNull(c.os), orNull(c.district), orNull(c.sector), now,
|
|
)
|
|
await writeAudit(db, userId, 'import', 'client', id, undefined, { code: c.code, name: c.name, source: 'apex' })
|
|
}
|
|
|
|
// Invoices — issued, history-only documents (empty payload lines) that keep
|
|
// their legacy APEX numbers; the series is seeded past them below.
|
|
const stgInvoices = await db.all<StgInvoiceRow>(`SELECT * FROM stg_invoice ORDER BY row_no`)
|
|
const INSERT_DOC_SQL =
|
|
`INSERT INTO document (id, doc_type, doc_no, fy, client_id, doc_date, status, ref_doc_id,
|
|
taxable_paise, cgst_paise, sgst_paise, igst_paise, round_off_paise, payable_paise,
|
|
payload, source, created_by, created_at)
|
|
VALUES (?, 'INVOICE', ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, 'apex', ?, ?)`
|
|
for (const inv of stgInvoices) {
|
|
const client = await db.get<{ id: string; state_code: string }>(
|
|
`SELECT id, state_code FROM client WHERE code=?`, inv.client_code,
|
|
)
|
|
if (client === undefined) {
|
|
throw new Error(`Invoice ${inv.doc_no}: client code ${inv.client_code} not found — stage clients.csv first`)
|
|
}
|
|
const intra = client.state_code === ourState
|
|
const cgst = intra ? Math.floor(inv.tax_paise / 2) : 0
|
|
const sgst = intra ? inv.tax_paise - cgst : 0
|
|
const igst = intra ? 0 : inv.tax_paise
|
|
const roundOff = inv.total_paise - inv.taxable_paise - inv.tax_paise
|
|
const totals = {
|
|
grossPaise: inv.taxable_paise, discountPaise: 0, taxablePaise: inv.taxable_paise,
|
|
cgstPaise: cgst, sgstPaise: sgst, igstPaise: igst, cessPaise: 0,
|
|
roundOffPaise: roundOff, payablePaise: inv.total_paise, savingsVsMrpPaise: 0,
|
|
}
|
|
const id = uuidv7()
|
|
const status = inv.paid === 1 ? 'paid' : 'sent'
|
|
await db.run(
|
|
INSERT_DOC_SQL,
|
|
id, inv.doc_no, fyOf(inv.doc_date), client.id, inv.doc_date, status,
|
|
inv.taxable_paise, cgst, sgst, igst, roundOff, inv.total_paise,
|
|
JSON.stringify({ lines: [], totals }), userId, now,
|
|
)
|
|
await writeAudit(db, userId, 'import', 'document', id, undefined,
|
|
{ docNo: inv.doc_no, status, payablePaise: inv.total_paise, source: 'apex' })
|
|
}
|
|
|
|
// Series seed (spec §4): continue INVOICE numbering after the last APEX
|
|
// number of the current FY. Legacy prefixes differ, so only the numeric
|
|
// tail matters.
|
|
const currentFy = fyOf(new Date().toISOString().slice(0, 10))
|
|
let seeded: CommitResult['seeded'] = null
|
|
const tails = stgInvoices
|
|
.filter((inv) => fyOf(inv.doc_date) === currentFy)
|
|
.map((inv) => /(\d+)$/.exec(inv.doc_no)?.[1])
|
|
.filter((t): t is string => t !== undefined)
|
|
.map((t) => Number(t))
|
|
if (tails.length > 0) {
|
|
const lastSeq = Math.max(...tails)
|
|
await seedSeries(db, 'INVOICE', currentFy, lastSeq)
|
|
seeded = { fy: currentFy, lastSeq }
|
|
await writeAudit(db, userId, 'import', 'doc_series', `INVOICE/${currentFy}`, undefined, { lastSeq })
|
|
}
|
|
|
|
// Commit consumes the staging area.
|
|
await db.run(`DELETE FROM stg_client`)
|
|
await db.run(`DELETE FROM stg_invoice`)
|
|
|
|
return { clients: stgClients.length, invoices: stgInvoices.length, seeded }
|
|
})
|
|
}
|
|
|
|
// ---------- CLI: npm run import -- --dir <folder> [--commit] ----------
|
|
|
|
if (process.argv[1] !== undefined && /import-apex\.(ts|cjs|js)$/.test(process.argv[1])) {
|
|
void (async () => {
|
|
const args = process.argv.slice(2)
|
|
const dirIdx = args.indexOf('--dir')
|
|
const dir = dirIdx >= 0 ? args[dirIdx + 1] : undefined
|
|
if (dir === undefined) {
|
|
console.error('Usage: npm run import -- --dir <folder> [--commit]')
|
|
process.exit(1)
|
|
}
|
|
const db = openDb(process.env['HQ_DATA_DIR'])
|
|
await seedIfEmpty(db)
|
|
const clients = await stageCsv(db, 'clients', fs.readFileSync(path.join(dir, 'clients.csv'), 'utf8'))
|
|
const invoices = await stageCsv(db, 'invoices', fs.readFileSync(path.join(dir, 'invoices.csv'), 'utf8'))
|
|
console.log(`Staged ${clients.staged} clients, ${invoices.staged} invoices from ${dir}`)
|
|
console.log(JSON.stringify(await verificationReport(db), null, 2))
|
|
if (args.includes('--commit')) {
|
|
const out = await commitImport(db, 'system')
|
|
console.log(`Committed: ${out.clients} clients, ${out.invoices} invoices; series seeded:`,
|
|
out.seeded ?? 'no current-FY invoices')
|
|
} else {
|
|
console.log('Dry run — verify the report above, then re-run with --commit to apply.')
|
|
}
|
|
})()
|
|
}
|