ubl2json
Version:
Türkiye e-Belge sistemi için kullanılan UBLTR XML formatındaki belgelerin json objesine dönüştürülmesi için kullanılan paket.
115 lines (107 loc) • 4.77 kB
JavaScript
const { XMLParser } = require('fast-xml-parser');
const dateTime = require('date-and-time');
const _ = require('lodash');
const options = require('../config/parser.config');
const normalizers = require('../utils/normalizers');
const CustomError = require('../utils/customError');
const convertProducerReceipt = async (receipt, optionsArgs) => {
const json = receipt.CreditNote || receipt.producerReceipt || receipt.ProducerReceipt || receipt.CreditNote || receipt;
const taxTotalObj = json.TaxTotal ? json.TaxTotal[0] : [];
const taxSubtotals = normalizers.taxSubtotalNormalizer(taxTotalObj);
const withholdingTaxSubtotals = normalizers.taxSubtotalNormalizer(
json.WithholdingTaxTotal ? json.WithholdingTaxTotal[0] : [],
);
const lines = normalizers.linesNormalizer(
json.CreditNoteLine || json.ProducerReceiptLine || json.producerReceiptLine || json.InvoiceLine || json.lines,
);
const normalizedJson = {
uuid: json.UUID?.val || json.uuid,
envelope_uuid: json.envelope_uuid || null,
number: json.ID?.val || json.number || json.id,
profile_id: json.ProfileID?.val || json.profile_id || 'EARSIVBELGE',
type_code: json.CreditNoteTypeCode?.val || json.type_code || 'MUSTAHSILMAKBUZ',
issue_datetime: json.IssueDate
? dateTime.parse(
`${json.IssueDate.val.substring(0, 10)}T${json.IssueTime ? json.IssueTime.val.substring(0, 8) : '00:00:00'}`,
'YYYY-MM-DDTHH:mm:ss',
true,
)
: json.issue_datetime
? new Date(json.issue_datetime)
: new Date(),
envelope_datetime: json.envelope_datetime || null,
notes: json.Note
? _.map(json.Note, (note) =>
note && typeof note === 'object' ? (note.val !== undefined ? String(note.val) : '') : String(note || ''),
).filter(Boolean)
: json.notes || [],
currency_code: json.DocumentCurrencyCode?.val || json.currency_code || 'TRY',
exchange_rate: json.PricingExchangeRate ? json.PricingExchangeRate.CalculationRate?.val : json.exchange_rate || 1,
sender_object: json.AccountingSupplierParty
? normalizers.partyNormalizer(json.AccountingSupplierParty)
: json.sender_object || {},
sender_name: json.AccountingSupplierParty
? normalizers.partyNormalizer(json.AccountingSupplierParty).name
: json.sender_name || '',
sender_tax: json.AccountingSupplierParty
? normalizers.partyNormalizer(json.AccountingSupplierParty).vkn_tckn
: json.sender_tax || '',
receiver_object: json.AccountingCustomerParty
? normalizers.partyNormalizer(json.AccountingCustomerParty)
: json.receiver_object || {},
receiver_name: json.AccountingCustomerParty
? normalizers.partyNormalizer(json.AccountingCustomerParty).name
: json.receiver_name || '',
receiver_tax: json.AccountingCustomerParty
? normalizers.partyNormalizer(json.AccountingCustomerParty).vkn_tckn
: json.receiver_tax || '',
line_extension: json.LegalMonetaryTotal?.LineExtensionAmount?.val || json.line_extension || 0,
tax_exclusive: json.LegalMonetaryTotal?.TaxExclusiveAmount?.val || json.tax_exclusive || 0,
tax_inclusive: json.LegalMonetaryTotal?.TaxInclusiveAmount?.val || json.tax_inclusive || 0,
tax_total: json.TaxTotal ? json.TaxTotal[0]?.TaxAmount?.val || json.tax_total || 0 : json.tax_total || 0,
tax_subtotals: taxSubtotals,
withholding_tax_total: json.WithholdingTaxTotal
? json.WithholdingTaxTotal[0]?.TaxAmount?.val || 0
: json.withholding_tax_total || 0,
withholding_tax_subtotals: withholdingTaxSubtotals,
allowance_total: json.LegalMonetaryTotal?.AllowanceTotalAmount?.val || json.allowance_total || 0,
charge_total: json.LegalMonetaryTotal?.ChargeTotalAmount?.val || json.charge_total || 0,
payable_amount: json.LegalMonetaryTotal?.PayableAmount?.val || json.payable_amount || 0,
lines,
};
return normalizedJson;
};
const convertToJson = async (xml) => {
const parser = new XMLParser(options);
const jsonObj = parser.parse(xml);
return jsonObj;
};
const rawJson = async (xml) => {
try {
const jsonObj = await convertToJson(xml);
return jsonObj;
} catch (error) {
throw new CustomError({
message: 'Raw JSON oluşturulurken hata oluştu.',
stack: error,
});
}
};
const convertedJson = async (xml, args) => {
// eslint-disable-next-line no-param-reassign
args = normalizers.setDefaults(args);
try {
const jsonObj = await convertToJson(xml);
const receipt = await convertProducerReceipt(jsonObj, args);
return receipt;
} catch (error) {
throw new CustomError({
message: 'Converted JSON oluşturulurken hata oluştu.',
stack: error,
});
}
};
module.exports = {
rawJson,
convertedJson,
};