UNPKG

@logismix/mydata-client

Version:
122 lines (121 loc) 6.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.XmlHelper = void 0; const xml2js_1 = require("xml2js"); const utils_1 = require("./utils"); class XmlHelper { constructor() { this.builderOptions = { xmldec: { version: '1.0', encoding: 'UTF-8' }, renderOpts: { pretty: false }, headless: false }; } async parseXml(xml) { try { const result = await (0, xml2js_1.parseStringPromise)(xml, { explicitArray: false, mergeAttrs: true, explicitRoot: true }); return result; } catch (error) { // console.error('XML Parsing Error:', error); throw new Error(`Failed to parse XML: ${error.message}`); } } buildInvoicesDocXml(invoices) { const invoicesDoc = { invoice: invoices }; const formattedDoc = (0, utils_1.formatDatesInObject)(invoicesDoc); const rootObject = { InvoicesDoc: { $: { xmlns: 'http://www.aade.gr/myDATA/invoice/v1.0', 'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:schemaLocation': 'http://www.aade.gr/myDATA/invoice/v1.0/InvoicesDoc-v1.0.10.xsd', 'xmlns:icls': 'https://www.aade.gr/myDATA/incomeClassificaton/v1.0', 'xmlns:ecls': 'https://www.aade.gr/myDATA/expensesClassificaton/v1.0' }, invoice: formattedDoc.invoice } }; const builder = new xml2js_1.Builder(this.builderOptions); let xml = builder.buildObject(rootObject); xml = this.applyInvoiceNamespacePrefixes(xml); return xml; } buildIncomeClassificationXml(classificationsDoc) { const formattedDoc = (0, utils_1.formatDatesInObject)(classificationsDoc); const rootObject = { 'icls:IncomeClassificationsDoc': { $: { 'xmlns:icls': 'https://www.aade.gr/myDATA/incomeClassificaton/v1.0', 'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance' }, 'icls:incomeInvoiceClassification': formattedDoc.incomeInvoiceClassification } }; const builder = new xml2js_1.Builder(this.builderOptions); let xml = builder.buildObject(rootObject); xml = this.applyIncomeClassificationPrefixes(xml); return xml; } buildExpensesClassificationXml(classificationsDoc) { const formattedDoc = (0, utils_1.formatDatesInObject)(classificationsDoc); const rootObject = { 'ecls:ExpensesClassificationsDoc': { $: { 'xmlns:ecls': 'https://www.aade.gr/myDATA/expensesClassificaton/v1.0', 'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance' }, 'ecls:expensesInvoiceClassification': formattedDoc.expensesInvoiceClassification } }; const builder = new xml2js_1.Builder(this.builderOptions); let xml = builder.buildObject(rootObject); xml = this.applyExpenseClassificationPrefixes(xml); return xml; } buildPaymentMethodsXml(paymentsDoc) { const formattedDoc = (0, utils_1.formatDatesInObject)(paymentsDoc); const rootObject = { 'pmt:PaymentMethodsDoc': { $: { 'xmlns:pmt': 'https://www.aade.gr/myDATA/paymentMethod/v1.0', 'xmlns:inv': 'http://www.aade.gr/myDATA/invoice/v1.0', 'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance' }, 'pmt:payment': formattedDoc.payment } }; const builder = new xml2js_1.Builder(this.builderOptions); let xml = builder.buildObject(rootObject); xml = this.applyPaymentMethodPrefixes(xml); return xml; } applyInvoiceNamespacePrefixes(xml) { // let result = xml.replace( // new RegExp( // '<(/?)(issuer|counterpart|invoiceHeader|paymentMethods|paymentMethodDetails|invoiceDetails|taxesTotals|taxes|invoiceSummary|otherTransportDetails)', // 'g' // ), // '<$1inv:$2' // ); let result = xml.replace(new RegExp('<incomeClassification>([\\s\\S]*?)<\\/incomeClassification>', 'g'), (match) => match.replace(new RegExp('<(/?)(classificationType|classificationCategory|amount|id)', 'g'), '<$1icls:$2')); result = result.replace(new RegExp('<expensesClassification>([\\s\\S]*?)<\\/expensesClassification>', 'g'), (match) => match.replace(new RegExp('<(/?)(classificationType|classificationCategory|amount|vatAmount|vatCategory|vatExemptionCategory|id)', 'g'), '<$1ecls:$2')); return result; } applyIncomeClassificationPrefixes(xml) { return xml.replace(new RegExp('<(/?)(invoiceMark|classificationMark|entityVatNumber|transactionMode|invoicesIncomeClassificationDetails|lineNumber|incomeClassificationDetailData|classificationType|classificationCategory|amount|id)', 'g'), '<$1icls:$2'); } applyExpenseClassificationPrefixes(xml) { return xml.replace(new RegExp('<(/?)(invoiceMark|classificationMark|entityVatNumber|transactionMode|invoicesExpensesClassificationDetails|lineNumber|expensesClassificationDetailData|classificationType|classificationCategory|amount|vatAmount|vatCategory|vatExemptionCategory|id|classificationPostMode)', 'g'), '<$1ecls:$2'); } applyPaymentMethodPrefixes(xml) { let result = xml.replace(new RegExp('<(/?)(invoiceMark|paymentMethodMark|entityVatNumber|paymentMethodDetails)', 'g'), '<$1pmt:$2'); result = result.replace(new RegExp('<pmt:paymentMethodDetails>([\\s\\S]*?)<\\/pmt:paymentMethodDetails>', 'g'), (match) => match.replace(new RegExp('<(/?)(type|amount|paymentMethodInfo|tipAmount|transactionId|tid|ProvidersSignature|ECRToken|SigningAuthor|Signature|SessionNumber)', 'g'), '<$1inv:$2')); return result; } } exports.XmlHelper = XmlHelper;