UNPKG

@accounter/shaam-uniform-format-generator

Version:

Fully typed application that generates, parses, and validates SHAAM uniform format tax reports (INI.TXT and BKMVDATA.TXT).

79 lines (78 loc) 3.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.A100Schema = void 0; exports.encodeA100 = encodeA100; exports.parseA100 = parseA100; const zod_1 = require("zod"); const index_js_1 = require("../format/index.js"); const constants_js_1 = require("../constants.js"); /** * Helper function to format a field with specified width and alignment */ function formatField(value, width, align) { if (align === 'left') { return (0, index_js_1.padRight)(value, width); } return (0, index_js_1.padLeft)(value, width); } /** * A100 Record Schema - Business opening record * Fields 1100-1105 based on SHAAM 1.31 specification */ exports.A100Schema = zod_1.z.object({ code: zod_1.z.literal('A100').describe('Record type code - always "A100"'), recordNumber: zod_1.z.string().min(1).max(9).describe('Sequential record number'), vatId: zod_1.z.string().min(1).max(9).describe('VAT identification number'), uniqueId: zod_1.z.string().min(1).max(15).describe('Unique business identifier'), reserved: zod_1.z.string().max(50).default('').describe('Reserved field for future use'), }); /** * Encodes an A100 record to fixed-width string format * Total line width: 95 characters + CRLF */ function encodeA100(input) { const fields = [ formatField(input.code, 4, 'left'), // Field 1100: Record code (4) formatField(input.recordNumber, 9, 'right'), // Field 1101: Record number (9) formatField(input.vatId, 9, 'left'), // Field 1102: VAT ID (9) formatField(input.uniqueId, 15, 'left'), // Field 1103: Unique ID (15) formatField(constants_js_1.SHAAM_VERSION, 8, 'left'), // Field 1104: SHAAM version (8) formatField(input.reserved, 50, 'left'), // Field 1105: Reserved (50) ]; return fields.join('') + index_js_1.CRLF; } /** * Parses a fixed-width A100 record line back to object * Expected line length: 95 characters (excluding CRLF) */ function parseA100(line) { // Remove CRLF if present const cleanLine = line.replace(/\r?\n$/, ''); if (cleanLine.length !== 95) { throw new Error(`Invalid A100 record length: expected 95 characters, got ${cleanLine.length}`); } // Extract fields at their fixed positions const code = cleanLine.slice(0, 4).trim(); const recordNumber = cleanLine.slice(4, 13).trim(); const vatId = cleanLine.slice(13, 22).trim(); const uniqueId = cleanLine.slice(22, 37).trim(); const systemCode = cleanLine.slice(37, 45).trim(); const reserved = cleanLine.slice(45, 95).trim(); // Validate the code field if (code !== 'A100') { throw new Error(`Invalid A100 record code: expected "A100", got "${code}"`); } // Validate the SHAAM version field if (systemCode !== constants_js_1.SHAAM_VERSION) { throw new Error(`Invalid SHAAM version: expected "${constants_js_1.SHAAM_VERSION}", got "${systemCode}"`); } const parsed = { code, recordNumber, vatId, uniqueId, reserved, }; // Validate against schema return exports.A100Schema.parse(parsed); }