eancom 1.5.7 → 1.6.2

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: 8b5fc9413e1aae3d7677f8e3ae1a779ef9127d4d0432301534022d9b121d35b5
4
+ data.tar.gz: b29cc881d48fa8544cbb6e3779144db51ffbd88d5fe47dbf56bb7f38b9f4c6dd
5
5
  SHA512:
6
- metadata.gz: 719ec7a34fe12a4fc3738d97bb732dd36bc793e965b3e21b85bce5007c0e1f2ee6a4694901faf2a55721980da3058c0374a4be4bd2cd81098968e66be6070902
7
- data.tar.gz: 2935634f2d295dfbe11b213eee756d7ed6268cd266afebd2717f35b5d29b145cfeacc250745763f185e0d346fe538fe28eddbfd11b36c7db28cb72692b05c868
6
+ metadata.gz: 3caae6575a4b5276ff6914e6f59688a4237077bbc8ca4227d29a1f527788526c5b9d850d37ef22a8f9e6032e8a1f15f7c88d32be4e85fa3d8e13b464c101effb
7
+ data.tar.gz: 29e2a100a09c4b15dc9e566742306548547688c2ceabd0696fb6ca2247b7a6b37e051f2eeeb894afecedff58579f4c0bc832a42393cb57b4cdb43b86787b0edb
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- eancom (1.5.6)
4
+ eancom (1.6.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -0,0 +1,155 @@
1
+ tag = 'PCD'
2
+ Eancom.register_segment(tag: tag, klass: Eancom::Edifact::PCD)
3
+
4
+ structure = Eancom::Edifact::Structure.new(tag: tag)
5
+ #==============================================================================
6
+ # Tag #
7
+ #==============================================================================
8
+ tag = Eancom::Edifact::Composite.new
9
+ data = Eancom::Edifact::Data.new(
10
+ type: String,
11
+ length: 3,
12
+ dictionary: nil,
13
+ description: "Message Header",
14
+ required: true
15
+ )
16
+ tag.add(:tag, data)
17
+
18
+ structure << tag
19
+ #==============================================================================
20
+ # Product group type code #
21
+ #==============================================================================
22
+ percentage_details = Eancom::Edifact::Composite.new
23
+
24
+ data = Eancom::Edifact::Data.new(
25
+ type: String,
26
+ length: 1..3,
27
+ dictionary: {
28
+ '1' => {
29
+ description: 'Allowance',
30
+ identifier: 'allowance'
31
+ },
32
+ '2' => {
33
+ description: 'Charge',
34
+ identifier: 'charge'
35
+ },
36
+ '3' => {
37
+ description: 'Allowance or Charge',
38
+ identifier: 'allowance_or_charge'
39
+ },
40
+ '7' => {
41
+ description: 'Percentage of invoice',
42
+ identifier: 'percentage_of_invoice'
43
+ },
44
+ '9' => {
45
+ description: 'Adjustment',
46
+ identifier: 'adjustment'
47
+ },
48
+ '12' => {
49
+ description: 'Discount',
50
+ identifier: 'discount'
51
+ },
52
+ '15' => {
53
+ description: 'Penalty percentage',
54
+ identifier: 'penalty_percentage'
55
+ },
56
+ '16' => {
57
+ description: 'Interest percentage',
58
+ identifier: 'interest_percentage'
59
+ },
60
+ '18' => {
61
+ description: 'Percentage credit note',
62
+ identifier: 'percentage_credit_note'
63
+ },
64
+ '19' => {
65
+ description: 'Percentage debit note',
66
+ identifier: 'percentage_debit_note'
67
+ },
68
+ '68' => {
69
+ description: 'Percentage of due amount',
70
+ identifier: 'percentage_of_due_amount'
71
+ },
72
+ '159' => {
73
+ description: 'Nestable percentage',
74
+ identifier: 'nestable_percentage'
75
+ },
76
+ '161' => {
77
+ description: 'Transport weight completed',
78
+ identifier: 'transport_weight_completed'
79
+ },
80
+ '162' => {
81
+ description: 'Transport volume completed',
82
+ identifier: 'transport_volume_completed'
83
+ },
84
+ '163' => {
85
+ description: 'Percentage of order',
86
+ identifier: 'percentage_of_order'
87
+ },
88
+ },
89
+ description: 'Percentage type code qualifier',
90
+ required: true
91
+ )
92
+ percentage_details.add(:percentage_type_code_qualifier, data)
93
+
94
+ data = Eancom::Edifact::Data.new(
95
+ type: Integer,
96
+ length: 0..10,
97
+ dictionary: nil,
98
+ description: 'Percentage',
99
+ required: true
100
+ )
101
+ percentage_details.add(:percentage, data)
102
+
103
+ data = Eancom::Edifact::Data.new(
104
+ type: String,
105
+ length: 0..3,
106
+ dictionary: {
107
+ '1' => {
108
+ description: 'Per unit',
109
+ identifier: 'per_unit'
110
+ },
111
+ '13' => {
112
+ description: 'Invoice value',
113
+ identifier: 'invoice_value'
114
+ },
115
+ },
116
+ description: 'Percentage',
117
+ required: true
118
+ )
119
+ percentage_details.add(:percentage_basis_identification_code, data)
120
+
121
+ data = Eancom::Edifact::Data.new(
122
+ type: String,
123
+ length: 0..17,
124
+ dictionary: nil,
125
+ description: 'Code list identification code',
126
+ required: false
127
+ )
128
+ percentage_details.add(:code_list_idenfitifaction_code, data)
129
+
130
+ data = Eancom::Edifact::Data.new(
131
+ type: String,
132
+ length: 0..3,
133
+ dictionary: {
134
+ '9' => {
135
+ description: 'GS1',
136
+ identifier: 'gsi',
137
+ },
138
+ '91' => {
139
+ description: 'Assigned by supplier or suppliers agent',
140
+ identifier: 'assigned_by_supplier_or_suppliers_agent'
141
+ },
142
+ '92' => {
143
+ description: 'Assigned by buyer or buyers agent',
144
+ identifier: 'assigned_by_buyer_or_buyers_agent'
145
+ }
146
+ },
147
+ description: 'Code list responsible agancy code',
148
+ required: false
149
+ )
150
+ percentage_details.add(:code_list_responsibility_agency_code, data)
151
+
152
+ structure << percentage_details
153
+
154
+ #==============================================================================
155
+ Eancom.register_structure(tag: 'PCD', structure: structure)
@@ -142,7 +142,7 @@ data = Eancom::Edifact::Data.new(
142
142
  )
143
143
  product_group.add(:product_group_name, data)
144
144
 
145
-
146
145
  structure << product_group
146
+
147
147
  #==============================================================================
148
148
  Eancom.register_structure(tag: 'PGI', structure: structure)
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ tag = 'QVR'
4
+ Eancom.register_segment(tag: tag, klass: Eancom::Edifact::QVR)
5
+
6
+ structure = Eancom::Edifact::Structure.new(tag: tag)
7
+ #==============================================================================
8
+ # Tag #
9
+ #==============================================================================
10
+ tag = Eancom::Edifact::Composite.new
11
+ data = Eancom::Edifact::Data.new(
12
+ type: String,
13
+ length: 3,
14
+ dictionary: nil,
15
+ description: "Message Header",
16
+ required: true
17
+ )
18
+ tag.add(:tag, data)
19
+
20
+ structure << tag
21
+ #==============================================================================
22
+ # Quantity difference information
23
+ #==============================================================================
24
+ quantity_difference_information = Eancom::Edifact::Composite.new
25
+
26
+ data = Eancom::Edifact::Data.new(
27
+ type: Integer,
28
+ length: 1..15,
29
+ dictionary: nil,
30
+ description: "Specify the actual variance amount here.",
31
+ required: true
32
+ )
33
+ quantity_difference_information.add(:quantity_variance_value, data)
34
+
35
+ data = Eancom::Edifact::Data.new(
36
+ type: String,
37
+ length: 1..3,
38
+ dictionary: {
39
+ '21' => {
40
+ description: 'Ordered quantity',
41
+ identifier: 'ordered_quantity'
42
+ },
43
+ '66' => {
44
+ description: 'Committed quantity',
45
+ identifier: 'committed_quantity'
46
+ }
47
+ },
48
+ description: "Quantity type code qualifier",
49
+ required: true
50
+ )
51
+ quantity_difference_information.add(:quantity_type_code_qualifier, data)
52
+
53
+ structure << quantity_difference_information
54
+ #==============================================================================
55
+ # Discrepancy nature identification code
56
+ #==============================================================================
57
+ discrepancy_nature_identification_code = Eancom::Edifact::Composite.new
58
+
59
+ data = Eancom::Edifact::Data.new(
60
+ type: String,
61
+ length: 0..3,
62
+ dictionary: {
63
+ 'BP' => {
64
+ description: 'Shipment partial - back order to follow',
65
+ identifier: 'shipment_partial_back_order_to_follow',
66
+ },
67
+ 'CP' => {
68
+ description: 'Shipment partial - considered complete, no backorder',
69
+ identifier: 'shipment_partial_considered_complete_no_backorder',
70
+ },
71
+ },
72
+ description: "",
73
+ required: true
74
+ )
75
+ discrepancy_nature_identification_code.add(:discrepancy_nature_identification_code, data)
76
+
77
+ structure << discrepancy_nature_identification_code
78
+ #==============================================================================
79
+ # Reason for change
80
+ #==============================================================================
81
+ reason_for_change = Eancom::Edifact::Composite.new
82
+
83
+ data = Eancom::Edifact::Data.new(
84
+ type: String,
85
+ length: 1..3,
86
+ dictionary: {
87
+ 'WR' => {
88
+ description: 'Temporarily unavailable',
89
+ identifier: 'temporarily_unavailable',
90
+ },
91
+ },
92
+ description: "Change reason description code",
93
+ required: false
94
+ )
95
+ reason_for_change.add(:change_reason_description_code, data)
96
+
97
+ data = Eancom::Edifact::Data.new(
98
+ type: String,
99
+ length: 0..17,
100
+ dictionary: nil,
101
+ description: 'Code list identification code',
102
+ required: false
103
+ )
104
+ reason_for_change.add(:code_list_idenfitifaction_code, data)
105
+
106
+ data = Eancom::Edifact::Data.new(
107
+ type: String,
108
+ length: 0..3,
109
+ dictionary: {
110
+ '9' => {
111
+ description: 'GS1',
112
+ identifier: 'gsi',
113
+ },
114
+ '91' => {
115
+ description: 'Assigned by supplier or suppliers agent',
116
+ identifier: 'assigned_by_supplier_or_suppliers_agent'
117
+ },
118
+ '92' => {
119
+ description: 'Assigned by buyer or buyers agent',
120
+ identifier: 'assigned_by_buyer_or_buyers_agent'
121
+ }
122
+ },
123
+ description: 'Code list responsible agancy code',
124
+ required: false
125
+ )
126
+ reason_for_change.add(:code_list_responsibility_agency_code, data)
127
+
128
+ data = Eancom::Edifact::Data.new(
129
+ type: String,
130
+ length: 1..35,
131
+ dictionary: nil,
132
+ description: "Change reason description",
133
+ required: false
134
+ )
135
+ reason_for_change.add(:change_reason_description, data)
136
+
137
+ structure << reason_for_change
138
+
139
+ #==============================================================================
140
+ Eancom.register_structure(tag: 'QVR', structure: structure)
@@ -80,6 +80,6 @@ data = Eancom::Edifact::Data.new(
80
80
  composite.add(:una6, data)
81
81
  structure << composite
82
82
 
83
- Eancom.register_structure(tag: tag, structure: structure)
83
+ Eancom.register_structure(tag: 'UNA', structure: structure)
84
84
 
85
85
 
@@ -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 if @message
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
@@ -0,0 +1,44 @@
1
+ module Eancom
2
+ module Edifact
3
+ class PCD < Segment
4
+ TAG = 'PCD'.freeze
5
+ TYPE = :body.freeze
6
+
7
+ attr_reader :segment_type
8
+
9
+ def initialize(
10
+ tag: nil,
11
+ percentage_type_code_qualifier:,
12
+ percentage:,
13
+ code_list_identification_code: nil,
14
+ code_list_responsibility_agency_code: nil
15
+ )
16
+ @tag = tag
17
+ @percentage_type_code_qualifier = percentage_type_code_qualifier
18
+ @percentage = percentage
19
+ @code_list_identification_code = code_list_identification_code
20
+ @code_list_responsibility_agency_code = code_list_responsibility_agency_code
21
+
22
+ super(tag: tag || TAG)
23
+ end
24
+
25
+ def to_json_hash
26
+ hash = {
27
+ 'percentage_type_code_qualifier': percentage_type,
28
+ 'percentage': percentage
29
+ }
30
+ end
31
+
32
+ def segment_type
33
+ TYPE
34
+ end
35
+
36
+ private
37
+
38
+ def percentage_type
39
+ @percentage_type ||= find_identifier(:percentage_type_code_qualifier)
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Eancom
4
+ module Edifact
5
+ class QVR < Segment
6
+ TAG = 'QVR'
7
+ TYPE = :body
8
+
9
+ def initialize(
10
+ tag: nil,
11
+ quantity_variance_value:,
12
+ quantity_type_code_qualifier:,
13
+ discrepancy_nature_identification_code: nil,
14
+ change_reason_description_code: nil,
15
+ code_list_identification_code: nil,
16
+ code_list_responsible_agency_code: nil,
17
+ change_reason_description: nil
18
+ )
19
+ @tag = tag
20
+
21
+ @quantity_variance_value = quantity_variance_value
22
+ @quantity_type_code_qualifier = quantity_type_code_qualifier
23
+ @discrepancy_nature_identification_code = discrepancy_nature_identification_code
24
+ @change_reason_description_code = change_reason_description_code
25
+ @code_list_identification_code = code_list_identification_code
26
+ @code_list_responsible_agency_code = code_list_responsible_agency_code
27
+ @change_reason_description = change_reason_description
28
+
29
+ super(tag: tag || TAG)
30
+ end
31
+
32
+ def to_json_hash
33
+ hash = {}
34
+ hash.merge!(quantity_variance_value: quantity_variance_value)
35
+ hash.merge!(quantity_type_code_qualifier: find_identifier(:quantity_type_code_qualifier))
36
+ hash.merge!(discrepancy_nature_identification_code: find_identifier(:discrepancy_nature_identification_code)) if @discrepancy_nature_identification_code
37
+ hash.merge!(change_reason_description: @change_reason_description) if @change_reason_description
38
+ hash
39
+ end
40
+
41
+ def group_name
42
+ 'quantity_variances'
43
+ end
44
+
45
+ def segment_type
46
+ TYPE
47
+ end
48
+ end
49
+ end
50
+ 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'
@@ -38,9 +39,11 @@ require 'eancom/edifact/segments/moa'
38
39
  require 'eancom/edifact/segments/nad'
39
40
  require 'eancom/edifact/segments/pac'
40
41
  require 'eancom/edifact/segments/pgi'
42
+ require 'eancom/edifact/segments/pcd'
41
43
  require 'eancom/edifact/segments/pia'
42
44
  require 'eancom/edifact/segments/pri'
43
45
  require 'eancom/edifact/segments/qty'
46
+ require 'eancom/edifact/segments/qvr'
44
47
  require 'eancom/edifact/segments/rff'
45
48
  require 'eancom/edifact/segments/tax'
46
49
  require 'eancom/edifact/segments/tdt'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Eancom
2
4
  module Parser
3
5
  class Document
@@ -10,9 +12,14 @@ module Eancom
10
12
  @document = Eancom::Edifact::Document.new
11
13
  end
12
14
 
13
- def parse
15
+ def parse(header_only: false)
14
16
  content.split(segment_delimiter).each do |segment_string|
15
17
  segment = Parser::Segment.new(segment_string)
18
+
19
+ if header_only && segment.segment_class::TYPE != :header
20
+ next
21
+ end
22
+
16
23
  segment = segment.parse
17
24
  add(segment)
18
25
  end
@@ -27,9 +34,9 @@ module Eancom
27
34
  elsif segment.is_body?
28
35
  @document.add_to_body(segment)
29
36
  elsif segment.is_footer?
30
- @document.add_to_body(segment)
37
+ @document.add_to_footer(segment)
31
38
  else
32
- raise SegmentTypeNotDefined.new
39
+ raise SegmentTypeNotDefined
33
40
  end
34
41
  end
35
42
 
@@ -53,16 +60,14 @@ module Eancom
53
60
  end
54
61
 
55
62
  def convert(string)
56
- string = string.encode(
63
+ string.encode(
57
64
  'UTF-8',
58
65
  'binary',
59
66
  invalid: :replace,
60
67
  undef: :replace,
61
68
  replace: ''
62
69
  )
63
- string
64
70
  end
65
-
66
71
  end
67
72
  end
68
73
  end
@@ -2,11 +2,13 @@ module Eancom
2
2
  module Parser
3
3
  class Segment
4
4
 
5
- attr_accessor :tag, :components, :string
5
+ attr_accessor :tag, :string, :components, :segment_class
6
6
 
7
7
  def initialize(string)
8
8
  @string = string
9
9
  @components = []
10
+ @tag = una? ? 'UNA' : string.split(data_delimiter)[0]
11
+ @segment_class = find_segment_class
10
12
  end
11
13
 
12
14
  def parse
@@ -19,13 +21,13 @@ module Eancom
19
21
  private
20
22
 
21
23
  def parse_component
22
- if string =~ /^UNA/
23
- parse_una
24
- else
24
+ if una?
25
+ parse_una
26
+ else
25
27
  string.split(data_delimiter).each do |data|
26
28
  components << parse_data(data)
27
29
  end
28
- end
30
+ end
29
31
  end
30
32
 
31
33
  def parse_data(data)
@@ -46,10 +48,6 @@ module Eancom
46
48
  components << [DELIMITERS[:segment]]
47
49
  end
48
50
 
49
- def tag
50
- components.first.first
51
- end
52
-
53
51
  def find_segment_class
54
52
  Eancom.find_segment(tag: tag)
55
53
  end
@@ -66,6 +64,9 @@ module Eancom
66
64
  DELIMITERS[:component]
67
65
  end
68
66
 
67
+ def una?
68
+ string =~ /^UNA/
69
+ end
69
70
  end
70
71
  end
71
72
  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.2'
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.2
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-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -163,10 +163,12 @@ files:
163
163
  - definitions/segments/moa.rb
164
164
  - definitions/segments/nad.rb
165
165
  - definitions/segments/pac.rb
166
+ - definitions/segments/pcd.rb
166
167
  - definitions/segments/pgi.rb
167
168
  - definitions/segments/pia.rb
168
169
  - definitions/segments/pri.rb
169
170
  - definitions/segments/qty.rb
171
+ - definitions/segments/qvr.rb
170
172
  - definitions/segments/rff.rb
171
173
  - definitions/segments/tax.rb
172
174
  - definitions/segments/tdt.rb
@@ -190,6 +192,7 @@ files:
190
192
  - lib/eancom/edifact/footer.rb
191
193
  - lib/eancom/edifact/header.rb
192
194
  - lib/eancom/edifact/item.rb
195
+ - lib/eancom/edifact/location.rb
193
196
  - lib/eancom/edifact/message.rb
194
197
  - lib/eancom/edifact/segment.rb
195
198
  - lib/eancom/edifact/segments/alc.rb
@@ -209,10 +212,12 @@ files:
209
212
  - lib/eancom/edifact/segments/moa.rb
210
213
  - lib/eancom/edifact/segments/nad.rb
211
214
  - lib/eancom/edifact/segments/pac.rb
215
+ - lib/eancom/edifact/segments/pcd.rb
212
216
  - lib/eancom/edifact/segments/pgi.rb
213
217
  - lib/eancom/edifact/segments/pia.rb
214
218
  - lib/eancom/edifact/segments/pri.rb
215
219
  - lib/eancom/edifact/segments/qty.rb
220
+ - lib/eancom/edifact/segments/qvr.rb
216
221
  - lib/eancom/edifact/segments/rff.rb
217
222
  - lib/eancom/edifact/segments/tax.rb
218
223
  - lib/eancom/edifact/segments/tdt.rb