orhantoy / edifunct

Fun with EDIFACT :tada:
MIT License
3 stars 3 forks source link

EDIFACT formats #7

Closed ZAJDAN closed 2 years ago

ZAJDAN commented 2 years ago

Please could You write into description for which type of EDIFACT formats it is usable?

Thank You Zdenek

orhantoy commented 2 years ago

Hello Zdenek,

I'm not an EDIFACT expert but mainly built this gem to solve the generic need to parse EDIFACT files at my previous job. Which EDIFACT formats are you interested in checking if this gem support?

ZAJDAN commented 2 years ago

EDIFACT D96A

UNA:+,? ' UNB+UNOC:3+O01770000000000X0AT000RMP:ZZ+MyCompany:ZZ+211117:0956+00002' UNH+1+DELFOR:D:96A:UN:A09041' BGM+241+20214601' DTM+137:20211117:102' NAD+BY+MyCompany::92' NAD+SE+000324431::92' UNS+D' NAD+CN+VIE::92' LIN++3+82.62900-5093:IN' PIA+1+EMPTY+ENG.CHANGE =:EC' LOC+11+ML' DTM+51:20210101:102' RFF+AAN:20214601' DTM+171:20211117:102' RFF+AIF:20213401' DTM+171:20210824:102' RFF+ON:4600002847' QTY+113:1:PCE' SCC+1' DTM+2:20220427:102' QTY+113:1:PCE' SCC+1' DTM+2:20220504:102' QTY+113:1:PCE' SCC+1' DTM+2:20220511:102' QTY+113:3:PCE' SCC+1' DTM+2:20220518:102' QTY+12:0:PCE' RFF+AAK:00000000' QTY+70:0:PCE' QTY+1:1' UNS+S' UNT+34+1' UNZ+1+00002'

orhantoy commented 2 years ago

Looks like it should be supported as this gem isn't built for any specific EDIFACT format.

ZAJDAN commented 2 years ago

OK...I am trying it, so far I get error:

Traceback (most recent call last):
        4: from convert.rb:8:in `<main>'
        3: from /var/lib/gems/2.7.0/gems/edifunct-2.0.0/lib/edifunct.rb:11:in `parse'
        2: from /var/lib/gems/2.7.0/gems/edifunct-2.0.0/lib/edifunct.rb:11:in `new'
        1: from /var/lib/gems/2.7.0/gems/edifunct-2.0.0/lib/edifunct/parser.rb:13:in `initialize'
/var/lib/gems/2.7.0/gems/edifunct-2.0.0/lib/edifunct/tokenizer.rb:10:in `for_message': undefined method `start_with?' for #<File:test.edi> (NoMethodError)

Ruby 2.7.4


require 'edifunct'

raw_edi = File.open("test.edi")
iftsta_schema =  File.open("schema.json")

iftsta_example = Edifunct.parse(raw_edi, schema: iftsta_schema)
iftsta_example.lookup_groups('SG13').each_with_object({}) do |group, consignments|
  sg14 = group.lookup_group('SG14')
  next unless sg14

  rff_cu_record = sg14.lookup_segment('RFF') { |s| s.data_elements[0] && s.data_elements[0][0] == 'CU' }
  next unless rff_cu_record

  dtm_record = sg14.lookup_segment('DTM')
  next unless dtm_record

  sts_record = sg14.lookup_segment('STS')
  next unless sts_record

  consignment_ref = rff_cu_record.data_elements[0][1]
  consignments[consignment_ref] = [
    {
      status: sts_record.data_elements[1][0],
      event_time: dtm_record.data_elements[0][1],
    }
  ]
end
orhantoy commented 2 years ago

Try changing the top part like so:

require "json"
require "edifunct"

raw_edi = File.read("test.edi")
iftsta_schema = JSON.load(File.new("schema.json"))

# [...]
ZAJDAN commented 2 years ago

Try changing the top part like so:

require "json"
require "edifunct"

raw_edi = File.read("test.edi")
iftsta_schema = JSON.load(File.new("schema.json"))

# [...]

super, now without errors Thank You

ZAJDAN commented 2 years ago

Where I can catch the result of the conversion and put it into variable?

orhantoy commented 2 years ago

Doesn't the assignment

iftsta_example = Edifunct.parse(raw_edi, schema: iftsta_schema)

do that?

ZAJDAN commented 2 years ago

puts iftsta_example returns #<Edifunct::SegmentGroup:0x000055fed31c3a18>

orhantoy commented 2 years ago

You can try puts iftsta_example.print_with_structure if you wanna see more details.

Otherwise, you have a few lookup methods as can be seen here.

ZAJDAN commented 2 years ago

You can try puts iftsta_example.print_with_structure if you wanna see more details.

Otherwise, you have a few lookup methods as can be seen here.

returns:

UNB+UNOC:3+O01770000000000X0AT000RMP:ZZ+MyCompany:ZZ+220222:1617+00006'
UNH+1+DELFOR:D:96A:UN:A09041'
BGM+241+20220801'
DTM+137:20220222:102'
#<Edifunct::Segment:0x000055d3bdae44a8>
#<Edifunct::Segment:0x000055d3bdae2c48>
#<Edifunct::Segment:0x000055d3bdae2360>
#<Edifunct::Segment:0x000055d3bdae17f8>
orhantoy commented 2 years ago

I'm not sure exactly what you're looking for or trying to do but I can't provide much more guidance than I have already done. I hope you understand ✌️

As mentioned earlier, this library is quite generic and should support most EDIFACT formats. But that also means it requires you to define some schema and then extract the data from the parsed content. You might want to look around for other libraries if you have different needs and/or expectations.