UNPKG

rdpcrystal-edi-library

Version:

ASCII X12 EDI Library, Create, Parse, Load, Validate, View, Edit, Split, Join And Scrub EDI Data

569 lines (444 loc) 16.9 kB
# RDPCrystal EDI Library RDPCrystal EDI Library is a NodeJS library that is used to Create, Parse, Load, Validate, View, Edit, Split, Join and Scrub EDI data. RDPCrystal EDI Library is located at [rdpcrystal.com](http://www.rdpcrystal.com/edi-sdk/). Full documentation can be found [here](http://www.rdpcrystal.com/kb/rdpcrystaledilibrary-nodejs-javascript/). RDPCrystal EDI Library is used by Fortune 100/500 and companies all over the world to handle X12 EDI documents. Examples projects are located in the /examples folder Send any support questions to [support@rdpcrystal.com](support@rdpcrystal.com) ## Installation ```bash npm i rdpcrystal-edi-library ``` # Usage ## Parse an 5010 837I X12 EDI File ```javascript /// <reference path="../../lib/RDPCrystalEDILibrary.d.ts" /> const edi = require('../../lib/RDPCrystalEDILibrary').RDPCrystalEDILibrary; const fs = require('fs'); const enumMap = require('./enummap'); let map = new enumMap(); //Create a new validator let validator = new edi.EDIValidator(); //Load a 5010 270 validation rules let validationRules = fs.readFileSync('../../validationrules/5010/Rules_5010_837I_005010X223A2.Rules').toString(); validator.EDIRulesFileData = validationRules; //Set the EDI data to validate and load. This can also be read in from a file //Set EDI data to validate and load validator.EDIDataString = "ISA*00* *00* *ZZ*87726 *ZZ*593697504 *120815*0801*^*00501*000000001*0*T*:~" + "GS*HC*87726*BOAFHC*20120815*080159*1*X*005010X223A2~"+ "ST*837*000000001*005010X223A2~"+ "BHT*0019*00*000000001*20120815*080159*CH~"+ "NM1*41*2*UNITED HEALTH*****46*12345~"+ "PER*IC*TOM JONES*TE*1234567891~"+ "NM1*40*2*UNITED HEALTH*****46*87726~"+ "HL*1**20*1~"+ "NM1*85*2*HOSPITAL INC*****XX*12345~"+ "N3*PO BOX 633571~"+ "N4*CINCINNATI*OH*452633571~"+ "REF*EI*12345~"+ "HL*2*1*22*0~"+ "SBR*P*18*12345*GE*****13~"+ "NM1*IL*1*VAZQUEW*SMITH****MI*123456~"+ "N3*1234 LAKE~"+ "N4*FAIRFIELD*OH*12345~"+ "DMG*D8*19520206*M~"+ "NM1*PR*2*BANK OF AMERICA*****PI*123456~"+ "CLM*10000014893900*2017.85***22:A:1**A*Y*Y~"+ "DTP*434*RD8*20120723-20120723~"+ "CL1*1*7*30~"+ "REF*D9*123456~"+ "HI*BK:7244~"+ "HI*BF:4019*BF:2720*BF:25000*BF:2749*BF:V4582*BF:7244~"+ "NM1*71*1*ORLANDO*SING~"+ "LX*1~"+ "SV2*250*HC:OPS*2017.85*UN*1**0~"+ "DTP*47xx2*RD8*20120723-20120723~"+ "SVD*87726*1378.4*HC:OPS**0*0~"+ "CAS*PR*1*0*0*2*344.6*0*3*0*0~"+ "DTP*573*D8*20120814~"+ "HL*3**20*1~"+ "NM1*85*2*HOSPITAL INC*****XX*123456~"+ "N3*PO BOX 633571~"+ "N4*CINCINNATI*OH*452633571~"+ "REF*EI*123456~"+ "HL*4*3*22*1~"+ "SBR*P*18*304000*GE*****13~"+ "NM1*IL*1*Gates*Bill****MI*12345~"+ "N3*999 EASTWICK~"+ "N4*CINCINNATI*NY*12345~"+ "DMG*D8*19440928*M~"+ "NM1*PR*2*BANK*****PI*12345~"+ "HL*5*4*23*0~"+ "PAT*01~"+ "NM1*QC*1*Gates*Bill~"+ "N3*999 EASTWICK~"+ "N4*CINCINNATI*NY*12345~"+ "DMG*D8*19540531*F~"+ "CLM*10000015567100*1820.32***22:A:1**A*Y*Y~"+ "DTP*434*RD8*20120727-20120727~"+ "CL1*1*7*30~"+ "REF*D9*12345~"+ "HI*BK:V7651~"+ "HI*BF:4550*BF:4019*BF:V1272*BF:V7651~"+ "NM1*71*1*MCDONALD*RONALD~"+ "LX*1~"+ "SV2*250*HC:OPS*1820.32*UN*4**0~"+ "DTP*472*RD8*20120727-20120727~"+ "SVD*87726*1348.62*HC:OPS**0*0~"+ "CAS*PR*1*134.55*0*2*337.15*0*3*0*0~"+ "DTP*573*D8*20120814~"+ "SE*60*000000001~"+ "GE*1*1~"+ "IEA*1*000000001~"; console.log("Validating 5010 837I EDI File"); validator.validate(); //Get the EDI document loaded into memory let loadedEDIFile = validator.EDILightWeightDocument; //Get EDI Data let stTransaction = loadedEDIFile.Loops.getItem(0).getLoop("INTERCHANGE HEADER/FUNCTIONAL GROUP/ST HEADER") let stSegment = stTransaction.getSegment("ST"); let ediData = ""; ediData += "Transaction Name = " + stSegment.Name + "\n"; ediData += "Transaction Number = " + stSegment.Elements.getItem(1).DataValue + "\n"; ediData += "Transaction Impl = " + stSegment.Elements.getItem(2).DataValue + "\n"; ediData += "\n"; //Get all billing providers let billingProviderLoops = stTransaction.getLoops("2000A"); for(let i=0;i < billingProviderLoops.Count;i++) { //Get billing provider name let billingProviderLoop = billingProviderLoops.getItem(i).getLoop("2010AA"); let billingProviderNameSegment = billingProviderLoop.getSegment("NM1"); ediData += "Billing Provider = " + billingProviderNameSegment.Elements.getItem(2).DataValue+ "\n"; //Get subscriber name let subscriberLoop = billingProviderLoops.getItem(i).getLoop("2000B/2010BA"); let subscriberNameSegment = subscriberLoop.getSegment("NM1"); ediData += "Subscriber Name = " + subscriberNameSegment.Elements.getItem(2).DataValue+ "\n"; //Get the claim loop let claimLoop = billingProviderLoops.getItem(i).getLoop("2000B/2300"); if (claimLoop == null){ claimLoop = billingProviderLoops.getItem(i).getLoop("2000B/2000C/2300"); } let claimSegment = claimLoop.getSegment("CLM"); ediData += "Claim ID = " + claimSegment.Elements.getItem(0).DataValue + "\n"; ediData += "Claim Price = " + claimSegment.Elements.getItem(1).DataValue + "\n"; let serviceLoops = claimLoop.getLoops("2400"); for(let j=0;j < serviceLoops.Count;j++) { let serviceSegment = serviceLoops.getItem(j).getSegment("SV2"); ediData += "Service Price= " + serviceSegment.Elements.getItem(2).DataValue+ "\n"; } ediData += "\n\n"; } console.log(ediData); ``` ## Validate an X12 EDI File ```javascript const fs = require('fs'); const edi = require('rdpcrystal-edi-library'); //Create a new validator let validator = new edi.EDIValidator(); //Load a 5010 270 validation rules let validationRules = fs.readFileSync('Rules_5010_270_005010X279A1.Rules').toString(); validator.EDIRulesFileData = validationRules; //Set the EDI data to validate and load. This can also be read in from a file validator.EDIDataString = "ISA*00*..........*01*SECRET....*ZZ*SUBMITTERS.ID..*ZZ*RECEIVERS.ID...*030101*1253*^*00501*000000905*1*T*:~" + "GS*Hk*SENDER CODE*RECEIVERCODE*19991231*0802*1*X*005010X279A1~" + "ST*271*1234*005010X279A1~" + "BHT*0022*13*10001234*20060501*1319~" + "HL*1**20*1~" + "NM1*PR*2*ABC COMPANY*****PI*842610001~" + "HL*2*1*21*1~" + "NM1*1P*2*BONE AND JOINT CLINIC*****SV*2000035~" + "HL*3*2*22*0~" + "TRN*1*93175-012547*9877281234~" + "NM1*IL*1*SMITH*ROBERT****MI*11122333301~" + "DMG*D8*19430519~" + "DTP*291*D8*20060501~" + "EQ*30~" + "SE*13*1234~" + "GE*1*1~" + "IEA*1*000000905~"; console.log("Validating 5010 271 EDI Files"); validator.validate(); console.log("Errors Found"); //Get all errors from the EDI data for (let i = 0; i < validator.Errors.Count; i++) { let error = validator.Errors.getItem(i); console.log( { Type: "Error", Line: error.LineNumber, Transaction: "", SnipLevel: error.SnipLevel, //SnipTestLevel Enum Message: error.Message, //EDIValidationMessage Enum Loop: error.Loop, Segment: error.Segment, Element: error.ElementOrdinal, Composite: error.CompositeElementOrdinal, Description: error.Description, Ordinal: error.SegmentOrdinal }); } //Write out a visual representation of the loaded EDI document let doc = validator.EDILightWeightDocument; writeDocumentTree(doc.Loops.getItem(0), 0); function writeDocumentTree(loop, indent) { if (loop != null) { for (let i = 0; i < indent; i++) { process.stdout.write(" "); } console.log(loop.Name); indent++; writeSegment(loop.Segments, indent); if (loop.Loops != null) { for (let i = 0; i < loop.Loops.Count; i++) { writeDocumentTree(loop.Loops.getItem(i), indent); } indent++; } } } function writeSegment(segments, indent) { for (let i = 0; i < segments.Count; i++) { let seg = segments.getItem(i); for (let i = 0; i < indent; i++) { process.stdout.write(" "); } console.log(seg.Name); writeElement(seg.Elements, indent); } } function writeElement(elements, indent) { for (let i = 0; i < elements.Count; i++) { let elem = elements.getItem(i); for (let i = 0; i < indent; i++) { process.stdout.write(" "); } if (elem.Composite) { console.log("Composite"); //Check for composite elements if (elem.Elements != null && elem.Elements.Count > 0) { writeElement(elem.Elements, ++indent); indent--; } } else { console.log("[" + elem.DataValue + "]"); } } } ``` ## Create An X12 EDI File ```javascript const edi = require('rdpcrystal-edi-library'); const STAR = 42; //'*' const COLON = 58 //':' const TILDA = 126 //'~' let doc = new edi.EDILightWeightDocument(); //Set the delimiters doc.Delimiters.ElementTerminatorCharacter = STAR; doc.Delimiters.CompositeTerminatorCharacter = COLON; doc.Delimiters.SegmentTerminatorCharacter = TILDA; //Automatically set current segment count in SE01 doc.AutoPlaceCorrectNumOfSegments = true; //Write each segment in a new line doc.EachSegmentInNewLine = true; let interchangeLoop = doc.createLoop("Interchange header"); let isa = interchangeLoop.createSegment("ISA"); isa.addElement("00"); isa.addElement(" "); isa.addElement("00"); isa.addElement(" "); isa.addElement("ZZ"); isa.addElement("InterchangeSenderID"); isa.addElement("ZZ"); isa.addElement("InterchangeReceiverID"); isa.addElement("070303"); isa.addElement("1804"); isa.addElement("U"); isa.addElement("00401"); isa.addElement("1"); isa.addElement("1"); isa.addElement("T"); isa.addElement(":"); let functionalGroup = interchangeLoop.createLoop("FunctionalGroup"); let gs = functionalGroup.createSegment("GS"); gs.addElement("T"); gs.addElement("SH"); gs.addElement("ApplicationSenderCode"); gs.addElement("ApplicationReceiverCode"); gs.addElement("2005"); gs.addElement("132334"); gs.addElement("1"); gs.addElement("X"); gs.addElement("004010"); //Create a new sample composite element let compositeElement = new edi.LightWeightElement(); compositeElement.addCompositeElement("aa"); compositeElement.addCompositeElement("bb"); gs.elements.add(compositeElement); let transaction = functionalGroup.createLoop("Transaction Header"); let st = transaction.createSegment("ST"); st.addElement("837"); st.addElement("123"); st.addElement("005010X222A1"); let se = transaction.createSegment("SE"); se.addElement("0"); se.addElement("123"); let endfunctionalGroup = functionalGroup.createLoop("EndFunctionalGroup"); let ge = endfunctionalGroup.createSegment("GE"); ge.addElement("0"); ge.addElement("1"); let endInterchange = interchangeLoop.createLoop("EndInterchange"); let iea = endInterchange.createSegment("IEA"); iea.addElement("0"); iea.addElement("1"); //Display the new EDI document console.log(doc.generateEDIData()); ``` ## Generate X12 EDI 999 Acknowlegements ```javascript const fs = require('fs'); const edi = require('rdpcrystal-edi-library'); //Create a new validator let validator = new edi.EDIValidator(); //Load a 5010 270 validation rules let validationRules = fs.readFileSync('Rules_5010_270_005010X279A1.Rules').toString(); validator.EDIRulesFileData = validationRules; //Set the EDI data to validate and load. This can also be read in from a file validator.EDIDataString = "ISA*00*..........*01*SECRET....*ZZ*SUBMITTERS.ID..*ZZ*RECEIVERS.ID...*030101*1253*^*00501*000000905*1*T*:~" + "GS*Hk*SENDER CODE*RECEIVERCODE*19991231*0802*1*X*005010X279A1~" + "ST*271*1234*005010X279A1~" + "BHT*0022*13*10001234*20060501*1319~" + "HL*1**20*1~" + "NM1*PR*2*ABC COMPANY*****PI*842610001~" + "HL*2*1*21*1~" + "NM1*1P*2*BONE AND JOINT CLINIC*****SV*2000035~" + "HL*3*2*22*0~" + "TRN*1*93175-012547*9877281234~" + "NM1*IL*1*SMITH*ROBERT****MI*11122333301~" + "DMG*D8*19430519~" + "DTP*291*D8*20060501~" + "EQ*30~" + "SE*13*1234~" + "GE*1*1~" + "IEA*1*000000905~"; console.log("Validating 5010 271 EDI Files"); validator.validate(); console.log("Errors Found->"); //Get all errors from the EDI data for (let i = 0; i < validator.Errors.Count; i++) { let error = validator.Errors.getItem(i); console.log( { Type: "Error", Line: error.LineNumber, Transaction: "", SnipLevel: error.SnipLevel, Message: error.Message, Loop: error.Loop, Segment: error.Segment, Element: error.ElementOrdinal, Composite: error.CompositeElementOrdinal, Description: error.Description, Ordinal: error.SegmentOrdinal }); } console.log("Generated 5010 999 Acknowledgment File"); //Generate 5010 999 Acknowledgment File let ack999Generator = new edi.Ack999Generator(); let ackDocument = ack999Generator.generate(validator); console.log(ackDocument.generateEDIData()); ``` ## Spit an X12 EDI File ```javascript const edi = require('rdpcrystal-edi-library'); //original document with 2 Transactions (ST) - //PLEASE ADD YOUR OWN EDI DATA HERE let original ="ISA*00* *00* *ZZ*133052274 *ZZ*311279999 ..."; let splitter = new edi.EDIFileSplitter(); //Split the document at the ST header splitter.FileSplitLevel = edi.FileSplitLevel.HEADER; //Put 1 ST-SE loop in each file splitter.NumberOfItemsPerFile = 1; //split the orginal document let splitDocs = splitter.split(original); console.log("First Split Document"); console.log(splitDocs[0]); console.log("Second Split Document"); console.log(splitDocs[1]); ``` ## Join an EDI File ```javascript const edi = require('rdpcrystal-edi-library'); //First document //PLEASE ADD YOUR OWN EDI DATA HERE let ediDoc1 = "ISA*00* *00* *ZZ*133052274 *ZZ*311279999..."; //Second Document //PLEASE ADD YOUR OWN EDI DATA HERE let ediDoc2 = "ISA*00* *00* *ZZ*133052274 *ZZ*311279999..."; let documents = [ediDoc1, ediDoc2]; let joiner = new edi.EDIFileJoiner(); //This these two documents at the functional group (GS) level joiner.FileJoinLevel = edi.FileJoinLevel.FUNCTIONALGROUP; //Join the two documents let joinedDocuments = joiner.join(documents); console.log("Joined document by Functional Group"); console.log(joinedDocuments); ``` ## Load an X12 EDI File ```javascript const edi = require('rdpcrystal-edi-library'); //PLEASE ADD YOUR OWN EDI DATA HERE let dataToLoad = "ISA*00* *00* *ZZ*133052274 *ZZ*311279999..."; let fileLoader = new edi.EDIFileLoader(); //Load EDI data fileLoader.EDIDataString = dataToLoad; let flatDoc = fileLoader.load(); //Build a visual representation of the EDI document writeDocumentTree(flatDoc.Loops.getItem(0), 0); function writeDocumentTree(loop, indent) { if (loop != null) { for (let i = 0; i < indent; i++) { process.stdout.write(" "); } console.log(loop.Name); indent++; writeSegment(loop.Segments, indent); if (loop.Loops != null) { for (let i = 0; i < loop.Loops.Count; i++) { writeDocumentTree(loop.Loops.getItem(i), indent); } indent++; } } } function writeSegment(segments, indent) { for (let i = 0; i < segments.Count; i++) { let seg = segments.getItem(i); for (let i = 0; i < indent; i++) { process.stdout.write(" "); } console.log(seg.Name); writeElement(seg.Elements, indent); } } function writeElement(elements, indent) { for (let i = 0; i < elements.Count; i++) { let elem = elements.getItem(i); for (let i = 0; i < indent; i++) { process.stdout.write(" "); } if (elem.Composite) { console.log("Composite"); //Check for composite elements if (elem.Elements != null && elem.Elements.Count > 0) { writeElement(elem.Elements, ++indent); indent--; } } else { console.log("[" + elem.DataValue + "]"); } } } ``` ## License http://www.rdpcrystal.com/free-trial/