xinvoice/ts/formats/base.decoder.ts

143 lines
3.4 KiB
TypeScript

import * as plugins from '../plugins.js';
/**
* Base decoder class for all invoice XML formats.
* Provides common functionality and interfaces for different format decoders.
*/
export abstract class BaseDecoder {
protected xmlString: string;
constructor(xmlString: string) {
if (!xmlString) {
throw new Error('No XML string provided to decoder');
}
this.xmlString = xmlString;
}
/**
* Abstract method that each format-specific decoder must implement.
* Converts XML into a structured letter object based on the XML format.
*/
public abstract getLetterData(): Promise<plugins.tsclass.business.ILetter>;
/**
* Creates a default letter object with minimal data.
* Used as a fallback when parsing fails.
*/
protected createDefaultLetter(): plugins.tsclass.business.ILetter {
// Create a default seller
const seller: plugins.tsclass.business.TContact = {
name: 'Unknown Seller',
type: 'company',
description: 'Unknown Seller',
address: {
streetName: 'Unknown',
houseNumber: '0',
city: 'Unknown',
country: 'Unknown',
postalCode: 'Unknown',
},
registrationDetails: {
vatId: 'Unknown',
registrationId: 'Unknown',
registrationName: 'Unknown'
},
foundedDate: {
year: 2000,
month: 1,
day: 1
},
closedDate: {
year: 9999,
month: 12,
day: 31
},
status: 'active'
};
// Create a default buyer
const buyer: plugins.tsclass.business.TContact = {
name: 'Unknown Buyer',
type: 'company',
description: 'Unknown Buyer',
address: {
streetName: 'Unknown',
houseNumber: '0',
city: 'Unknown',
country: 'Unknown',
postalCode: 'Unknown',
},
registrationDetails: {
vatId: 'Unknown',
registrationId: 'Unknown',
registrationName: 'Unknown'
},
foundedDate: {
year: 2000,
month: 1,
day: 1
},
closedDate: {
year: 9999,
month: 12,
day: 31
},
status: 'active'
};
// Create default invoice data
const invoiceData: plugins.tsclass.finance.IInvoice = {
id: 'Unknown',
status: null,
type: 'debitnote',
billedBy: seller,
billedTo: buyer,
deliveryDate: Date.now(),
dueInDays: 30,
periodOfPerformance: null,
printResult: null,
currency: 'EUR' as plugins.tsclass.finance.TCurrency,
notes: [],
items: [
{
name: 'Unknown Item',
unitQuantity: 1,
unitNetPrice: 0,
vatPercentage: 0,
position: 0,
unitType: 'units',
}
],
reverseCharge: false,
};
// Return a default letter
return {
versionInfo: {
type: 'draft',
version: '1.0.0',
},
type: 'invoice',
date: Date.now(),
subject: 'Unknown Invoice',
from: seller,
to: buyer,
content: {
invoiceData: invoiceData,
textData: null,
timesheetData: null,
contractData: null,
},
needsCoverSheet: false,
objectActions: [],
pdf: null,
incidenceId: null,
language: null,
legalContact: null,
logoUrl: null,
pdfAttachments: null,
accentColor: null,
};
}
}