eancom 1.5.7 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a6528d1683b7871c94ada34d8b29f46827c1fcc9c970855e81190d020cfe435e
4
- data.tar.gz: c3a8326ce9be33cd202b315fedc82235fff74e9ee1238b05e13008a40ece8f7d
3
+ metadata.gz: 10b8633addd1b19df008fcfbf0b33b75c2639cac939e2f2923d6518515c1e670
4
+ data.tar.gz: 26a096e4e9afa9c39a0e2184dc2bd60fdf7f2f95140e70dfc19a31d59262cd27
5
5
  SHA512:
6
- metadata.gz: 719ec7a34fe12a4fc3738d97bb732dd36bc793e965b3e21b85bce5007c0e1f2ee6a4694901faf2a55721980da3058c0374a4be4bd2cd81098968e66be6070902
7
- data.tar.gz: 2935634f2d295dfbe11b213eee756d7ed6268cd266afebd2717f35b5d29b145cfeacc250745763f185e0d346fe538fe28eddbfd11b36c7db28cb72692b05c868
6
+ metadata.gz: 34f0152b2721cf4effa65f159642077d52a1e79653e22461a9deb558894c628687775265c1f0b1ced37dfc1a41e87b47af0e6239b53325bee11190845221dd26
7
+ data.tar.gz: 52974b80bc2d5bfb9b429c58f2ac01e3c1029e655c8cce924e7115c5ed4af483b56727ec18292871509f2961134683c1f7408e994fbcb7b811360820b1d1461a
@@ -109,78 +109,78 @@ Eancom::Definition.create(name: 'D01B', type: :slsrpt) do |document, config|
109
109
  )
110
110
  end
111
111
 
112
- # Location
113
- if location = message.location
114
- code_list_responsible_agency_code = if location.code_list_responsible_agancy_code
115
- structure = Eancom.find_structure(tag: 'LOC')
116
- structure.dictionary_lookup(
117
- :code_list_responsible_agancy_code,
118
- location.code_list_responsible_agancy_code
119
- )
120
- else
121
- '9'
122
- end
123
-
124
- body.segment Eancom::Edifact::LOC.new(
125
- code_list_identification_code: location.code_list_identification_code,
126
- code_list_responsible_agancy_code: code_list_responsible_agency_code,
127
- location_function_qualifier: '162',
128
- location_name_code: location.location_name_code
129
- )
130
- end
112
+ item_counter = 0
131
113
 
132
- # Sales Date
133
- if date_time = message.sales_date_and_or_time_and_or_period.date_time
134
- body.segment Eancom::Edifact::DTM.new(
135
- type: '356',
136
- date_time: date_time,
137
- format: '102'
138
- )
139
- end
140
-
141
- # Items
142
- total_quantity = 0
114
+ message.locations.each do |location_group|
115
+ if location = location_group.location
116
+ code_list_responsible_agency_code = if location.code_list_responsible_agancy_code
117
+ structure = Eancom.find_structure(tag: 'LOC')
118
+ structure.dictionary_lookup(
119
+ :code_list_responsible_agancy_code,
120
+ location.code_list_responsible_agancy_code
121
+ )
122
+ else
123
+ '9'
124
+ end
143
125
 
144
- items = message.items
126
+ body.segment Eancom::Edifact::LOC.new(
127
+ code_list_identification_code: location.code_list_identification_code,
128
+ code_list_responsible_agancy_code: code_list_responsible_agency_code,
129
+ location_function_qualifier: '162',
130
+ location_name_code: location.location_name_code
131
+ )
132
+ end
145
133
 
146
- items.each_with_index do |item, index|
147
- total_quantity += item.quantity.to_i
134
+ # Sales Date
135
+ if date_time = location_group.sales_date_and_or_time_and_or_period&.date_time
136
+ body.segment Eancom::Edifact::DTM.new(
137
+ type: '356',
138
+ date_time: date_time,
139
+ format: '102'
140
+ )
141
+ end
148
142
 
149
- body.segment Eancom::Edifact::LIN.new(
150
- line_item_identifier_1: "#{index + 1}",
151
- item_identifier: item.ean,
152
- item_type_identification_code: 'SRV'
153
- )
143
+ # Items
144
+ location_group.items.each do |item|
145
+ item_counter += 1
154
146
 
155
- if transaction_reference_number = item.transaction_reference_number
156
- body.segment Eancom::Edifact::RFF.new(
157
- reference_code_qualifier: 'TN',
158
- reference_identifier: transaction_reference_number.reference_identifier
147
+ body.segment Eancom::Edifact::LIN.new(
148
+ line_item_identifier_1: item_counter.to_s,
149
+ item_identifier: item.ean,
150
+ item_type_identification_code: 'SRV'
159
151
  )
160
- end
161
152
 
162
- prices = item.prices
163
- prices.each do |price|
164
- if price = price.price
165
- structure = Eancom.find_structure(tag: 'PRI')
166
- body.segment Eancom::Edifact::PRI.new(
167
- price_amount: price.price_amount,
168
- price_code_qualifier: structure.dictionary_lookup(:price_code_qualifier, price.price_code_qualifier),
169
- price_specification_code: structure.dictionary_lookup(:price_specification_code, price.price_specification_code),
170
- price_type_code: price.price_type_code
153
+ if transaction_reference_number = item.transaction_reference_number
154
+ body.segment Eancom::Edifact::RFF.new(
155
+ reference_code_qualifier: 'TN',
156
+ reference_identifier: transaction_reference_number.reference_identifier
171
157
  )
172
158
  end
173
- end
174
159
 
175
- # Quantity
176
- if statistical_sales_quantitiy = item.statistical_sales_quantitiy
177
- body.segment Eancom::Edifact::QTY.new(
178
- quantity_type_code_qualifier: '153',
179
- quantity: statistical_sales_quantitiy.quantity
180
- )
160
+ prices = item.prices
161
+ prices.each do |price_group|
162
+ if price = price_group.price
163
+ structure = Eancom.find_structure(tag: 'PRI')
164
+ body.segment Eancom::Edifact::PRI.new(
165
+ price_amount: price.price_amount,
166
+ price_code_qualifier: structure.dictionary_lookup(:price_code_qualifier, price.price_code_qualifier),
167
+ price_specification_code: structure.dictionary_lookup(:price_specification_code, price.price_specification_code),
168
+ price_type_code: price.price_type_code
169
+ )
170
+ end
171
+ end
172
+
173
+ # Quantity
174
+ if statistical_sales_quantitiy = item.statistical_sales_quantitiy
175
+ body.segment Eancom::Edifact::QTY.new(
176
+ quantity_type_code_qualifier: '153',
177
+ quantity: statistical_sales_quantitiy.quantity
178
+ )
179
+ end
181
180
  end
182
181
  end
183
182
 
183
+
184
184
  body.segment Eancom::Edifact::UNS.new(
185
185
  section_identification: 'S'
186
186
  )
@@ -106,78 +106,80 @@ Eancom::Definition.create(name: 'D96A', type: :slsrpt) do |document, config|
106
106
  )
107
107
  end
108
108
 
109
- # Location
110
- if location = message.location
111
- code_list_responsible_agency_code = if location.code_list_responsible_agancy_code
112
- structure = Eancom.find_structure(tag: 'LOC')
113
- structure.dictionary_lookup(
114
- :code_list_responsible_agancy_code,
115
- location.code_list_responsible_agancy_code
116
- )
117
- else
118
- '9'
119
- end
120
- body.segment Eancom::Edifact::LOC.new(
121
- code_list_identification_code: location.code_list_identification_code,
122
- code_list_responsible_agancy_code: code_list_responsible_agency_code,
123
- location_function_qualifier: '150',
124
- location_name_code: location.location_name_code
125
- )
126
- end
127
-
128
- # Sales Date
129
- if date_time = message.sales_date_and_or_time_and_or_period.date_time
130
- body.segment Eancom::Edifact::DTM.new(
131
- type: '356',
132
- date_time: date_time,
133
- format: '203'
134
- )
135
- end
109
+ item_counter = 0
136
110
 
137
- # Items
138
- total_quantity = 0
111
+ message.locations.each do |location_group|
139
112
 
140
- items = message.items
113
+ # Location
114
+ if location = location_group.location
115
+ code_list_responsible_agency_code = if location.code_list_responsible_agancy_code
116
+ structure = Eancom.find_structure(tag: 'LOC')
117
+ structure.dictionary_lookup(
118
+ :code_list_responsible_agancy_code,
119
+ location.code_list_responsible_agancy_code
120
+ )
121
+ else
122
+ '9'
123
+ end
124
+ body.segment Eancom::Edifact::LOC.new(
125
+ code_list_identification_code: location.code_list_identification_code,
126
+ code_list_responsible_agancy_code: code_list_responsible_agency_code,
127
+ location_function_qualifier: '150',
128
+ location_name_code: location.location_name_code
129
+ )
130
+ end
141
131
 
142
- items.each_with_index do |item, index|
143
- total_quantity += item.quantity.to_i
132
+ # Sales Date
133
+ if date_time = location_group.sales_date_and_or_time_and_or_period&.date_time
134
+ body.segment Eancom::Edifact::DTM.new(
135
+ type: '356',
136
+ date_time: date_time,
137
+ format: '203'
138
+ )
139
+ end
144
140
 
145
- body.segment Eancom::Edifact::LIN.new(
146
- line_item_identifier_1: "#{index + 1}",
147
- item_identifier: item.ean,
148
- item_type_identification_code: 'EN'
149
- )
141
+ # Items
142
+ location_group.items.each do |item|
143
+ item_counter += 1
150
144
 
151
- if transaction_reference_number = item.transaction_reference_number
152
- body.segment Eancom::Edifact::RFF.new(
153
- reference_code_qualifier: 'TN',
154
- reference_identifier: transaction_reference_number.reference_identifier
145
+ body.segment Eancom::Edifact::LIN.new(
146
+ line_item_identifier_1: item_counter.to_s,
147
+ item_identifier: item.ean,
148
+ item_type_identification_code: 'EN'
155
149
  )
156
- end
157
150
 
158
- prices = item.prices
159
- prices.each do |price|
160
- if price = price.price
161
- structure = Eancom.find_structure(tag: 'PRI')
162
- body.segment Eancom::Edifact::PRI.new(
163
- price_amount: price.price_amount,
164
- price_code_qualifier: structure.dictionary_lookup(:price_code_qualifier, price.price_code_qualifier),
165
- price_specification_code: structure.dictionary_lookup(:price_specification_code, price.price_specification_code),
166
- price_type_code: price.price_type_code
151
+ if transaction_reference_number = item.transaction_reference_number
152
+ body.segment Eancom::Edifact::RFF.new(
153
+ reference_code_qualifier: 'TN',
154
+ reference_identifier: transaction_reference_number.reference_identifier
167
155
  )
168
156
  end
169
- end
170
157
 
171
- # Quantity
172
- if minimum_production_batch = item.minimum_production_batch
173
- body.segment Eancom::Edifact::QTY.new(
174
- quantity_type_code_qualifier: '160',
175
- quantity: minimum_production_batch.quantity,
176
- measurement_unit_code: 'PCE'
177
- )
158
+ prices = item.prices
159
+ prices.each do |price|
160
+ if price = price.price
161
+ structure = Eancom.find_structure(tag: 'PRI')
162
+ body.segment Eancom::Edifact::PRI.new(
163
+ price_amount: price.price_amount,
164
+ price_code_qualifier: structure.dictionary_lookup(:price_code_qualifier, price.price_code_qualifier),
165
+ price_specification_code: structure.dictionary_lookup(:price_specification_code, price.price_specification_code),
166
+ price_type_code: price.price_type_code
167
+ )
168
+ end
169
+ end
170
+
171
+ # Quantity
172
+ if minimum_production_batch = item.minimum_production_batch
173
+ body.segment Eancom::Edifact::QTY.new(
174
+ quantity_type_code_qualifier: '160',
175
+ quantity: minimum_production_batch.quantity,
176
+ measurement_unit_code: 'PCE'
177
+ )
178
+ end
178
179
  end
179
180
  end
180
181
 
182
+
181
183
  body.segment Eancom::Edifact::UNT.new(
182
184
  number_of_segments_in_message: "#{document.total_segments + 2}",
183
185
  message_reference_number: config.header.message_reference_number,
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Eancom
2
4
  module Edifact
3
5
  class Body < DocumentElement
4
-
5
- attr_accessor :hash, :items, :messages
6
+ attr_accessor :hash, :messages, :locations, :items
6
7
 
7
8
  def initialize
8
9
  super
@@ -11,52 +12,32 @@ module Eancom
11
12
  end
12
13
 
13
14
  def to_json_hash
14
- @segments.each do |segment|
15
- start_message(segment) if segment.starts_message?
16
- start_item(segment) if segment.starts_item?
17
- if @item
18
- @item << segment
19
- else
20
- @message << segment
21
- end
22
- end
23
- @message.add_item(@item.to_json_hash)
24
- @messages << @message.to_json_hash
25
- hash.merge!({ messages: messages })
26
- { body: hash }
15
+ @to_json_hash ||= build_json_hash
27
16
  end
28
17
 
29
- def start_message(segment)
18
+ def start_message
30
19
  if @message
31
20
  @messages << @message.to_json_hash
32
- if @item && !@item.empty?
33
- @message.add_item(@item.to_json_hash)
34
- end
35
- @item = nil
36
- end
37
- @message = Eancom::Edifact::Message.new()
38
- end
39
-
40
- def start_item(segment)
41
- if @item && !@item.empty?
42
- @message.add_item(@item.to_json_hash)
43
21
  end
44
- @item = Eancom::Edifact::Item.new()
22
+ @message = Eancom::Edifact::Message.new
45
23
  end
46
24
 
47
25
  private
48
26
 
49
- def merge(hash, segment)
50
- if name = group_name(segment)
51
- hash[name] = [] if hash[name].nil?
52
- hash[name] << segment.to_json_hash
53
- else
54
- hash.merge!(segment.to_json_hash)
55
- end
27
+ def group_name(segment)
28
+ segment.group_name
56
29
  end
57
30
 
58
- def group_name(segment)
59
- return segment.group_name
31
+ def build_json_hash
32
+ @segments.each do |segment|
33
+ start_message if segment.starts_message?
34
+ @message << segment
35
+ end
36
+ messages << @message.to_json_hash
37
+
38
+ {
39
+ body: hash.merge({ messages: messages })
40
+ }
60
41
  end
61
42
  end
62
43
  end
@@ -1,25 +1,25 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Eancom
2
4
  module Edifact
3
5
  class Document
4
-
5
- attr_accessor :header_element,
6
- :body_element, :footer_element
6
+ attr_accessor :header_element, :body_element, :footer_element
7
7
 
8
8
  def initialize
9
- @header_element = Header.new()
10
- @body_element = Body.new()
11
- @footer_element = Footer.new()
9
+ @header_element = Header.new
10
+ @body_element = Body.new
11
+ @footer_element = Footer.new
12
12
  end
13
13
 
14
- def header(&block)
14
+ def header
15
15
  yield(@header_element)
16
16
  end
17
17
 
18
- def body(&block)
18
+ def body
19
19
  yield(@body_element)
20
20
  end
21
21
 
22
- def footer(&block)
22
+ def footer
23
23
  yield(@footer_element)
24
24
  end
25
25
 
@@ -42,18 +42,14 @@ module Eancom
42
42
  end
43
43
 
44
44
  def to_s(debug: false)
45
- stream = ''
46
- stream << @header_element.to_s(debug: debug)
47
- stream << @body_element.to_s(debug: debug)
48
- stream << @footer_element.to_s(debug: debug)
49
- stream
50
- end
51
-
52
- def to_json
53
- hash = {}
54
- hash.merge! @header_element.to_json_hash
55
- hash.merge! @body_element.to_json_hash
56
- hash.to_json
45
+ "#{@header_element.to_s(debug: debug)}#{@body_element.to_s(debug: debug)}#{@footer_element.to_s(debug: debug)}"
46
+ end
47
+
48
+ def to_json(_obj = nil)
49
+ {
50
+ **@header_element.to_json_hash,
51
+ **@body_element.to_json_hash
52
+ }.to_json
57
53
  end
58
54
  end
59
55
  end
@@ -4,7 +4,7 @@ module Eancom
4
4
 
5
5
  attr_accessor :hash
6
6
 
7
- def initialize()
7
+ def initialize
8
8
  @hash = {}
9
9
  end
10
10
 
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eancom
4
+ module Edifact
5
+ class Location
6
+ attr_accessor :items, :hash
7
+
8
+ def initialize
9
+ @items = []
10
+ @hash = {}
11
+ end
12
+
13
+ def add_item(item)
14
+ @items << item
15
+ end
16
+
17
+ def <<(segment)
18
+ start_item if segment.starts_item?
19
+
20
+ if @item
21
+ @item << segment
22
+ elsif name = group_name(segment)
23
+ @hash[name] = [] if @hash[name].nil?
24
+ @hash[name] << segment.to_json_hash
25
+ else
26
+ @hash.merge!(segment.to_json_hash)
27
+ end
28
+ end
29
+
30
+ def to_json_hash
31
+ if @item && !@item.empty?
32
+ add_item(@item.to_json_hash)
33
+ end
34
+
35
+ @hash.merge({ items: @items })
36
+ end
37
+
38
+ def empty?
39
+ @hash.empty?
40
+ end
41
+
42
+ def start_item
43
+ if @item && !@item.empty?
44
+ add_item(@item.to_json_hash)
45
+ end
46
+ @item = Eancom::Edifact::Item.new
47
+ end
48
+
49
+ private
50
+
51
+ def group_name(segment)
52
+ segment.item_group_name || segment.group_name
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,20 +1,33 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Eancom
2
4
  module Edifact
3
5
  class Message
6
+ attr_accessor :locations, :items, :hash
4
7
 
5
- attr_accessor :items, :hash
6
-
7
- def initialize()
8
+ def initialize
9
+ @locations = []
8
10
  @items = []
9
11
  @hash = {}
10
12
  end
11
13
 
14
+ def add_location(location)
15
+ @locations << location
16
+ end
17
+
12
18
  def add_item(item)
13
19
  @items << item
14
20
  end
15
21
 
16
- def << (segment)
17
- if name = group_name(segment)
22
+ def <<(segment)
23
+ start_location if segment.starts_location?
24
+ start_item if segment.starts_item?
25
+
26
+ if @location
27
+ @location << segment
28
+ elsif @item
29
+ @item << segment
30
+ elsif name = group_name(segment)
18
31
  @hash[name] = [] if @hash[name].nil?
19
32
  @hash[name] << segment.to_json_hash
20
33
  else
@@ -23,10 +36,16 @@ module Eancom
23
36
  end
24
37
 
25
38
  def to_json_hash
39
+ if @location && !@location.empty?
40
+ add_location(@location.to_json_hash)
41
+ end
42
+ if @item && !@item.empty?
43
+ add_item(@item.to_json_hash)
44
+ end
26
45
  message_hash = {}
27
- items_hash = { items: @items }
28
46
  message_hash.merge!(@hash)
29
- message_hash.merge!(items_hash)
47
+ message_hash.merge!({ locations: @locations }) if !@locations.empty?
48
+ message_hash.merge!({ items: @items }) if !@items.empty?
30
49
  message_hash
31
50
  end
32
51
 
@@ -36,6 +55,19 @@ module Eancom
36
55
  segment.group_name
37
56
  end
38
57
 
58
+ def start_location
59
+ if @location && !@location.empty?
60
+ add_location(@location.to_json_hash)
61
+ end
62
+ @location = Eancom::Edifact::Location.new
63
+ end
64
+
65
+ def start_item
66
+ if @item && !@item.empty?
67
+ add_item(@item.to_json_hash)
68
+ end
69
+ @item = Eancom::Edifact::Item.new
70
+ end
39
71
  end
40
72
  end
41
73
  end
@@ -83,6 +83,10 @@ module Eancom
83
83
  false
84
84
  end
85
85
 
86
+ def starts_location?
87
+ false
88
+ end
89
+
86
90
  def starts_item?
87
91
  false
88
92
  end
@@ -34,6 +34,10 @@ module Eancom
34
34
  outer_hash
35
35
  end
36
36
 
37
+ def starts_location?
38
+ true
39
+ end
40
+
37
41
  def segment_type
38
42
  TYPE
39
43
  end
@@ -2,7 +2,7 @@ module Eancom
2
2
  module Edifact
3
3
  class UNS < Segment
4
4
  TAG = 'UNS'.freeze
5
- TYPE = :header.freeze
5
+ TYPE = :body.freeze
6
6
 
7
7
  def initialize(
8
8
  tag: nil,
@@ -3,6 +3,7 @@ require 'eancom/edifact/segment'
3
3
  require 'eancom/edifact/document_element'
4
4
  require 'eancom/edifact/header'
5
5
  require 'eancom/edifact/message'
6
+ require 'eancom/edifact/location'
6
7
  require 'eancom/edifact/item'
7
8
  require 'eancom/edifact/body'
8
9
  require 'eancom/edifact/footer'
@@ -27,7 +27,7 @@ module Eancom
27
27
  elsif segment.is_body?
28
28
  @document.add_to_body(segment)
29
29
  elsif segment.is_footer?
30
- @document.add_to_body(segment)
30
+ @document.add_to_footer(segment)
31
31
  else
32
32
  raise SegmentTypeNotDefined.new
33
33
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Eancom
4
- VERSION = '1.5.7'
4
+ VERSION = '1.6.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eancom
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.7
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fadendaten GmbH
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-01 00:00:00.000000000 Z
11
+ date: 2023-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -190,6 +190,7 @@ files:
190
190
  - lib/eancom/edifact/footer.rb
191
191
  - lib/eancom/edifact/header.rb
192
192
  - lib/eancom/edifact/item.rb
193
+ - lib/eancom/edifact/location.rb
193
194
  - lib/eancom/edifact/message.rb
194
195
  - lib/eancom/edifact/segment.rb
195
196
  - lib/eancom/edifact/segments/alc.rb