forked from mjpete3/x12
-
Notifications
You must be signed in to change notification settings - Fork 1
Generating
mjpete3 edited this page Dec 2, 2012
·
1 revision
Here is how to perform a reverse operation and generate a well-formed 997 message (the source is in example/factory.rb):
require 'x12'
# Read message definition and create an actual parser
# by compiling .d12 file
parser = X12::Parser.new('misc/997.xml')
# Make a new 997 message
r = parser.factory('997')
# # Set various fields as desired #
# Set fields directly r.ST.TransactionSetIdentifierCode = 997 r.ST.TransactionSetControlNumber = '2878'
# Set fields inside a segment (AK1 in this case)
r.AK1 { |ak1|
ak1.FunctionalIdentifierCode = 'HS'
ak1.GroupControlNumber = 293328532
}
# Set fields deeply inside a segment inside # nested loops (L1000/L1010/AK4 in this case) r.L1000.L1010.AK4.DataElementSyntaxErrorCode = 55 r.L1000.AK2.TransactionSetIdentifierCode = 270
# Set nested loops
r.L1000.L1010 {|l|
l.AK3 {|s|
s.SegmentIdCode = 'NM1'
s.LoopIdentifierCode = 'L1000D'
}
l.AK4 {|s|
s.CopyOfBadDataElement = 'Bad element'
}
}
# Add loop repeats
r.L1000.repeat {|l1000|
(0..1).each {|loop_repeat| # Two repeats of the loop L1010
l1000.L1010.repeat {|l1010|
l1010.AK3 {|s|
s.SegmentIdCode = 'DMG'
s.SegmentPositionInTransactionSet = 0
s.LoopIdentifierCode = 'L1010'
s.SegmentSyntaxErrorCode = 22
} if loop_repeat == 0 # AK3 only in the first repeat of L1010
(0..1).each {|ak4_repeat| # Two repeats of the segment AK4
l1010.AK4.repeat {|s|
s.PositionInSegment = loop_repeat
s.DataElementSyntaxErrorCode = ak4_repeat
} # s
} # ak4_repeat
} # l1010
} # loop_repeat
l1000.AK5{|a|
a.TransactionSetAcknowledgmentCode = 666
a.TransactionSetSyntaxErrorCode4 = 999
} # a
} # l1000
# Print the message as a string -> ST*997*2878~AK1*HS*293328532~ # AK2*270*~AK3*NM1**L1000D~AK4***55*Bad element~AK5*~AK3*DMG*0* # L1010*22~AK4*0**0~AK4*0**1~AK4*1**0~AK4*1**1~AK5*666****999~ # AK9****~SE**~ puts r.render