UNPKG

@accounter/shaam6111-generator

Version:

Fully typed application that generates, parses, and validates SHAAM 6111 tax reports.

50 lines (49 loc) 2.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseReport = parseReport; const index_js_1 = require("../types/index.js"); const index_js_2 = require("../validation/index.js"); const parse_header_js_1 = require("./parse-header.js"); const parse_report_entries_js_1 = require("./parse-report-entries.js"); /** * Parses a SHAAM6111 report content string into a structured ReportData object * @param content - The raw content of the SHAAM6111 report * @returns A structured ReportData object */ function parseReport(content, validate = true) { // Remove any carriage returns and split by line breaks to work with individual lines const headerLine = content .replace(/\r/g, '') .split('\n') .find(line => line.trim().length > 0) ?? ''; if (!headerLine || headerLine.length !== 8612) { throw new index_js_1.ValidationError('Validation failed: Report parts are missing or have incorrect length.'); } // Determine if this is an individual or company report based on required fields // For simplicity, we'll check if balance sheet is included with value 1 as an indicator for a company const isIndividual = parseInt(headerLine.substring(112, 115).trim()) === 0 && parseInt(headerLine.charAt(105)) === 2; // TODO: enhance this check // Parse header record const header = (0, parse_header_js_1.parseHeaderRecord)(headerLine); // Get report entries for each section const profitAndLoss = (0, parse_report_entries_js_1.parseReportEntries)(content, 'profitAndLoss'); const taxAdjustment = (0, parse_report_entries_js_1.parseReportEntries)(content, 'taxAdjustment'); const balanceSheet = (0, parse_report_entries_js_1.parseReportEntries)(content, 'balanceSheet'); // Construct the full report data const reportData = { header, profitAndLoss, taxAdjustment, balanceSheet, individualOrCompany: isIndividual ? index_js_1.IndividualOrCompanyEnum.INDIVIDUAL : index_js_1.IndividualOrCompanyEnum.COMPANY, }; // Validate the report data using the schema if (validate) { const validationResult = (0, index_js_2.validateData)(reportData); if (!validationResult.isValid) { throw new index_js_1.ValidationError('Validation failed', validationResult.errors); } } return reportData; }