@makakwastaken/ts-edifact
Version:
Edifact parser library
112 lines • 4.43 kB
JavaScript
;
/**
* @author Roman Vottner
* @copyright 2020 Roman Vottner
* @license Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const messageStructureParser_1 = require("../src/edi/messageStructureParser");
const interchangeBuilder_1 = require("../src/interchangeBuilder");
const reader_1 = require("../src/reader");
const util_1 = require("../src/util");
let document = '';
document += "UNB+UNOA:1+005435656:1+006415160:1+060515:1434+00000000000778'";
document += "UNH+00000000000117+INVOIC:D:01B:UN'";
document += "BGM+380+342459+9'";
document += "DTM+3:20060515:102'";
document += "RFF+ON:521052'";
document += "NAD+BY+792820524::16++CUMMINS MID-RANGE ENGINE PLANT'";
document += "NAD+SE+005435656::16++GENERAL WIDGET COMPANY'";
document += "CUX+1:USD'";
document += "LIN+1++157870:IN'"; // start of detail section and first item, items (LIN and following segments) will be grouped and added to the respective item group
document += "IMD+F++:::WIDGET'";
document += "QTY+47:1020:EA'";
document += "ALI+US'";
document += "MOA+203:1202.58'";
document += "PRI+INV:1.179'";
document += "LIN+2++157871:IN'"; // start 2nd item
document += "IMD+F++:::DIFFERENT WIDGET'";
document += "QTY+47:20:EA'";
document += "ALI+JP'";
document += "MOA+203:410'";
document += "PRI+INV:20.5'";
document += "UNS+S'"; // start of summary section
document += "MOA+39:2137.58'";
document += "ALC+C+ABG'";
document += "MOA+8:525'";
document += "UNT+23+00000000000117'";
document += "UNZ+1+00000000000778'";
async function parseDocument(doc) {
const specDir = './';
const specParser = new messageStructureParser_1.UNECEMessageStructureParser('D01B', 'INVOIC');
const edifact = await specParser
.loadTypeSpec()
.then((data) => {
(0, util_1.persist)(data, specDir, true);
})
.then(() => {
const reader = new reader_1.Reader(specDir);
const result = reader.parse(doc);
const separators = reader.separators;
const builder = new interchangeBuilder_1.InterchangeBuilder(result, separators, specDir);
return builder.interchange;
})
.catch((error) => {
throw error;
});
return edifact;
}
parseDocument(document)
.then((doc) => {
for (const entry of doc.messages[0].detail[0].data) {
if (entry instanceof interchangeBuilder_1.Group) {
let articleNumber = '';
let name = '';
let qty = 0;
let price = 0;
let total = 0;
for (const itemData of entry.data) {
if (!(itemData instanceof interchangeBuilder_1.Group)) {
let item;
if ((item = itemData)) {
articleNumber = item.itemNumberIdentification?.itemIdentifier;
}
else if ((item = itemData)) {
name = item.itemDescription?.itemDescription;
}
else if ((item = itemData)) {
qty = item.quantityDetails.quantity;
}
}
else {
for (const subGroupItem of itemData.data) {
let item;
if ((item = subGroupItem)) {
price = item.priceInformation?.priceAmount;
}
else if ((item = subGroupItem)) {
total = item.monetaryAmount?.monetaryAmount;
}
}
}
}
console.debug(articleNumber, name, qty, price, total);
}
}
})
.catch((error) => {
console.trace(`Caught exception while attempting to parse Edifact document. Reason: ${error.message}`);
});
//# sourceMappingURL=interchangeBuilderSample.js.map