class EDI::E::MsgGroup

Class EDI::E::MsgGroup

This class implements a group of business documents of the same type Its header unites features from UNB as well as from UNH.

Attributes

messages_created[R]

Public Class Methods

new( p, user_par={} ) click to toggle source

Creates an empty UN/EDIFACT message group Don’t use directly - use new_msggroup of class Interchange instead!

First parameter

This is always the parent object (an interchange object). Use method new_msggroup in the corresponding object instead of creating message groups unattended - the parent reference will be accounted for automatically.

Second parameter

List of supported hash keys:

UNG presets for your convenience, may be changed later

:msg_type

Sets DE 0038, default = ‘INVOIC’

:resp_agency

Sets DE 0051, default = ‘UN’

:version

Sets S008.0052, default = ‘D’

:release

Sets S008.0054, default = ‘96A’

Optional parameters, required depending upon use case

:assigned_code

Sets S008.0057 (subset), default = nil

:sender

Presets DE S006/0040, default = nil

:recipient

Presets DE S007/0044, default = nil

:group_reference

Presets DE 0048, auto-incremented

Notes

  • The functional group reference number in UNG and UNE (0048) is set automatically to a number that is unique for this message group and the running process (auto-increment).

  • The counter in UNG (0060) is set automatically to the number of included messages.

  • The trailer segment (UNE) is generated automatically.

  • Whenever possible, avoid writing to the counters of the message header or trailer segments!

Calls superclass method EDI::MsgGroup::new
# File lib/edi4r/edifact.rb, line 737
def initialize( p, user_par={} )
  super( p, user_par )
  @messages_created = 0
 
  if user_par.is_a? Hash
    preset_group( user_par )
    @header = new_segment('UNG')
    @trailer = new_segment('UNE')
    @trailer.d0060 = 0

    @header.d0038 = @name
    @header.d0051 = @resp_agency
    cde = @header.cS008
    cde.d0052 = @version
    cde.d0054 = @release
    cde.d0057 = @subset

    @header.cS006.d0040 = user_par[:sender]    || root.header.cS002.d0004
    @header.cS007.d0044 = user_par[:recipient] || root.header.cS003.d0010
    @header.d0048 = user_par[:group_reference] || p.groups_created
    #      @trailer.d0048 = @header.d0048

    t = Time.now
    @header.cS004.d0017 = t.strftime(p.version==4 ? '%Y%m%d':'%y%m%d')
    @header.cS004.d0019 = t.strftime("%H%M")
    
  elsif user_par.is_a? Segment

    @header = user_par
    raise "UNG expected, #{@header.name} found!" if @header.name != 'UNG'
    @header.parent = self
    @header.root = self.root

    # Assign a temporary UNE segment
    de_sep = root.una.de_sep
    @trailer = Segment.parse(root, 'UNE' << de_sep << '0' << de_sep << '0')

    s008 = @header.cS008
    @name = @header.d0038
    @version = s008.d0052
    @release = s008.d0054
    @resp_agency = @header.d0051
    @subset = s008.d0057
  else
    raise "First parameter: Illegal type!"
  end

end
parse(p, segment_list) click to toggle source
# File lib/edi4r/edifact.rb, line 808
def MsgGroup.parse (p, segment_list) # List of segments
  grp = p.new_msggroup(:msg_type => 'DUMMY')

  # We now expect a sequence of segments that comprises one group,
  # starting with UNG and ending with UNE, and with messages in between.
  # We process the UNG/UNE envelope separately, then work on the content.

  header  = grp.parse_segment(segment_list.shift, 'UNG')
  trailer = grp.parse_segment(segment_list.pop,   'UNE')

  init_seg = Regexp.new('^UNH')
  exit_seg = Regexp.new('^UNT')
  
  while segbuf = segment_list.shift
    case segbuf

    when init_seg
      sub_list = Array.new
      sub_list.push segbuf

    when exit_seg
      sub_list.push segbuf  
      grp.add grp.parse_message(sub_list)

    else
      sub_list.push segbuf  
    end
  end

  grp.header  = header
  grp.trailer = trailer
  grp
end

Public Instance Methods

add( msg, auto_validate=true ) click to toggle source
Calls superclass method EDI::MsgGroup#add
# File lib/edi4r/edifact.rb, line 862
def add( msg, auto_validate=true )
  super( msg )
  @trailer.d0060 = @trailer.d0060.to_i if @trailer.d0060.is_a? String
  @trailer.d0060 += 1
end
new_message(params={}) click to toggle source
# File lib/edi4r/edifact.rb, line 843
def new_message(params={})
  @messages_created += 1
  Message.new(self, params)
end
to_s() click to toggle source
Calls superclass method EDI::Collection_HT#to_s
# File lib/edi4r/edifact.rb, line 869
def to_s
  postfix = '' << root.una.seg_term << root.e_linebreak
  super( postfix )
end
validate( err_count=0 ) click to toggle source
Calls superclass method EDI::Collection_HT#validate
# File lib/edi4r/edifact.rb, line 875
def validate( err_count=0 )

  # Consistency checks

  if (a=@trailer.d0060) != (b=self.size)
    warn "UNE: DE 0060 (#{a}) does not match number of messages (#{b})"
    err_count += 1
  end
  a, b = @trailer.d0048, @header.d0048
  if a != b
    warn "UNE: DE 0048 (#{a}) does not match reference in UNG (#{b})"
    err_count += 1
  end
  
  # FIXME: Check if messages are uniquely numbered

  super
end