definitions/desadv/d96a.rb in eancom-1.6.3 vs definitions/desadv/d96a.rb in eancom-2.0.0
- old
+ new
@@ -1,19 +1,21 @@
-# frozen_string_literal: true
-
Eancom::Definition.create(name: 'D96A', type: :desadv) do |document, config|
document.header do |header|
+ header.segment Eancom::Edifact::UNA.new
+
header.segment Eancom::Edifact::UNB.new(
- syntax_identifier: 'UNOA',
- syntax_version_number: '2',
+ syntax_identifier: 'UNOC',
+ syntax_version_number: '3',
interchange_sender_identification: config.header.interchange_sender_identification,
- sender_identifiction_code_qualifier: config.header.sender_identification_code_qualifier || '14',
+ sender_identifiction_code_qualifier: '14',
interchange_recipient_identification: config.header.interchange_recipient_identification,
- recipient_identifiction_code_qualifier: config.header.recipient_identification_code_qualifier || '14',
+ recipient_identifiction_code_qualifier: '14',
date: config.header.date,
time: config.header.time,
- interchange_control_reference: config.header.interchange_control_reference
+ interchange_control_reference: config.header.interchange_control_reference,
+ application_reference: config.header.application_reference,
+ interchange_agreement_identifier: config.header.interchange_agreement_identifier
)
header.segment Eancom::Edifact::UNH.new(
message_reference_number: config.header.message_reference_number,
message_type: config.header.message_type,
@@ -24,141 +26,202 @@
)
end
document.body do |body|
messages = config.body.messages
-
messages.each do |message|
body.segment Eancom::Edifact::BGM.new(
- document_name_code: '351',
+ document_name_code: message.document_name_code,
document_identifier: message.document_identifier,
- message_function_code: '9'
+ message_function_code: message.message_function_code
)
- # Document Date
- body.segment Eancom::Edifact::DTM.new(
- type: '137',
- date_time: message.document_message_date_time.date_time,
- format: '102'
- )
+ date_time_messages = message.date_time_messages
+ date_time_messages.each_with_index do |dtm, index|
+ next unless index == 0
- # Shipping Date
- body.segment Eancom::Edifact::DTM.new(
- type: '11',
- date_time: message.despatch_date_and_or_time.date_time,
- format: '102'
- )
+ structure = Eancom.find_structure(tag: 'DTM')
+ key = dtm.identifier.intern.to_sym
+ body.segment Eancom::Edifact::DTM.new(
+ type: structure.dictionary_lookup(:type, dtm.identifier),
+ date_time: dtm[key].date_time,
+ format: '102'
+ )
+ end
- # Order Reference
- if order_number_buyer = message.order_number_buyer
- body.segment Eancom::Edifact::RFF.new(
- reference_code_qualifier: 'ON',
- reference_identifier: order_number_buyer.reference_identifier
+ date_time_messages = message.date_time_messages
+ date_time_messages.each_with_index do |dtm, index|
+ next unless index == 1
+
+ structure = Eancom.find_structure(tag: 'DTM')
+ key = dtm.identifier.intern.to_sym
+ body.segment Eancom::Edifact::DTM.new(
+ type: structure.dictionary_lookup(:type, dtm.identifier),
+ date_time: dtm[key].date_time,
+ format: '102'
)
end
- if order_number_supplier = message.order_number_supplier
+ references = message.references
+ references.each_with_index do |rff, index|
+ next unless index == 0
+
+ structure = Eancom.find_structure(tag: 'RFF')
+ key = rff.identifier.intern.to_sym
body.segment Eancom::Edifact::RFF.new(
- reference_code_qualifier: 'VN',
- reference_identifier: order_number_supplier.reference_identifier
+ reference_code_qualifier: structure.dictionary_lookup(:reference_code_qualifier, rff.identifier),
+ reference_identifier: rff[key].reference_identifier
)
end
- if (supplier = message.supplier)
- # Sender ILN
+ # Sender ILN
+ nads = message.nads
+ nads.each_with_index do |nad, index|
+ next unless index == 0
+
+ structure = Eancom.find_structure(tag: 'NAD')
+ key = nad.identifier.intern.to_sym
body.segment Eancom::Edifact::NAD.new(
- party_function_code_qualifier: 'SU',
- party_identifier: supplier.party_identifier,
- code_list_responsible_agancy_code_1: '9'
+ party_function_code_qualifier: structure.dictionary_lookup(:party_function_code_qualifier, nad.identifier),
+ party_identifier: nad[key].party_identifier,
+ code_list_responsible_agency_code_1: '9'
)
-
end
- if (manufacturer = message.manufacturer)
- # Manufacturer ILN
+ # Manufacturer ILN
+ nads = message.nads
+ nads.each_with_index do |nad, index|
+ next unless index == 1
+
+ structure = Eancom.find_structure(tag: 'NAD')
+ key = nad.identifier.intern.to_sym
body.segment Eancom::Edifact::NAD.new(
- party_function_code_qualifier: 'MF',
- party_identifier: manufacturer.party_identifier,
- code_list_responsible_agancy_code_1: '9'
+ party_function_code_qualifier: structure.dictionary_lookup(:party_function_code_qualifier, nad.identifier),
+ party_identifier: nad[key].party_identifier,
+ code_list_responsible_agency_code_1: '9'
)
end
# Reciver ILN
- if (buyer = message.buyer)
+ nads = message.nads
+ nads.each_with_index do |nad, index|
+ next unless index == 2
+
+ structure = Eancom.find_structure(tag: 'NAD')
+ key = nad.identifier.intern.to_sym
body.segment Eancom::Edifact::NAD.new(
- party_function_code_qualifier: 'BY',
- party_identifier: buyer.party_identifier,
- code_list_responsible_agancy_code_1: '9'
+ party_function_code_qualifier: structure.dictionary_lookup(:party_function_code_qualifier, nad.identifier),
+ party_identifier: nad[key].party_identifier,
+ code_list_responsible_agency_code_1: '9'
)
end
- # Delivery Party ILN
- if (delivery_party = message.delivery_party)
+ # Destination ILN
+ nads = message.nads
+ nads.each_with_index do |nad, index|
+ next unless index == 3
+
+ structure = Eancom.find_structure(tag: 'NAD')
+ key = nad.identifier.intern.to_sym
body.segment Eancom::Edifact::NAD.new(
- party_function_code_qualifier: 'DP',
- party_identifier: delivery_party.party_identifier,
- code_list_responsible_agancy_code_1: '9'
+ party_function_code_qualifier: structure.dictionary_lookup(:party_function_code_qualifier, nad.identifier),
+ party_identifier: nad[key].party_identifier,
+ code_list_responsible_agency_code_1: '9'
)
end
# Final Recipient ILN
- if (ultimate_consignee = message.ultimate_consignee)
+ nads = message.nads
+ nads.each_with_index do |nad, index|
+ next unless index == 4
+
+ structure = Eancom.find_structure(tag: 'NAD')
+ key = nad.identifier.intern.to_sym
body.segment Eancom::Edifact::NAD.new(
- party_function_code_qualifier: 'UC',
- party_identifier: ultimate_consignee.party_identifier,
- code_list_responsible_agancy_code_1: '9'
+ party_function_code_qualifier: structure.dictionary_lookup(:party_function_code_qualifier, nad.identifier),
+ party_identifier: nad[key].party_identifier,
+ code_list_responsible_agency_code_1: '9'
)
end
# Delivery Note Mode
body.segment Eancom::Edifact::CPS.new(
- hierarchical_structure_level_identifier: '1'
+ hierarchical_structure_level_identifier: message.hierarchical_structure_level_identifier
)
# Items
total_quantity = 0
items = message.items
- items.each_with_index do |item, _index|
- dispatch_quantity = item.despatch_quantity
- total_quantity += dispatch_quantity.quantity.to_i
+ items.each_with_index do |item, index|
+ total_quantity += item.quantity.to_i
+ structure = Eancom.find_structure(tag: 'LIN')
body.segment Eancom::Edifact::LIN.new(
- line_item_identifier_1: item.line_item_identifier_1.to_s,
+ line_item_identifier_1: (index + 1).to_s,
item_identifier: item.ean,
- item_type_identification_code: 'EN'
+ item_type_identification_code: structure.dictionary_lookup(:item_type_identification_code,
+ item.item_type_identification_code)
)
- body.segment Eancom::Edifact::QTY.new(
- quantity_type_code_qualifier: '12',
- quantity: dispatch_quantity.quantity.to_s,
- measurement_unit_code: 'PCE'
- )
+ quantities = item.quantities
+ quantities.each_with_index do |qty, index|
+ structure = Eancom.find_structure(tag: 'QTY')
+ key = qty.identifier.intern.to_sym
+ body.segment Eancom::Edifact::QTY.new(
+ quantity_type_code_qualifier: structure.dictionary_lookup(:quantity_type_code_qualifier, qty.identifier),
+ quantity: qty[key].quantity
+ )
+ end
end
+ end
+ end
- # Position Count
- body.segment Eancom::Edifact::CNT.new(
- control_total_type_code_quantifier: '2',
- control_total_value: items.count.to_s
- )
+ document.footer do |footer|
+ messages = config.body.messages
+ messages.each do |message|
+ items = message.items
+ control_total_value = 0
- # Total Quantity
- body.segment Eancom::Edifact::CNT.new(
- control_total_type_code_quantifier: '1',
- control_total_value: total_quantity.to_s
- )
+ items.each_with_index do |item, index|
+ quantities = item.quantities
+ quantities.each do |qty|
+ key = qty.identifier.intern.to_sym
+ control_total_value += qty[key].quantity.to_i
+ end
+ next unless index == 1
+ counts = item.counts
+ counts.each_with_index do |cnt, cnt_index|
+ next unless cnt_index == 0
+ structure = Eancom.find_structure(tag: 'CNT')
+ footer.segment Eancom::Edifact::CNT.new(
+ control_total_type_code_quantifier: structure.dictionary_lookup(:control_total_type_code_quantifier,
+ cnt.control_total_type_code_quantifier),
+ control_total_value: control_total_value.to_s
+ )
+ end
+
+
+ counts.each_with_index do |cnt, cnt_index|
+ next unless cnt_index == 1
+ structure = Eancom.find_structure(tag: 'CNT')
+ footer.segment Eancom::Edifact::CNT.new(
+ control_total_type_code_quantifier: structure.dictionary_lookup(:control_total_type_code_quantifier,
+ cnt.control_total_type_code_quantifier),
+ control_total_value: items.count.to_s
+ )
+ end
+ end
end
- body.segment Eancom::Edifact::UNT.new(
- number_of_segments_in_message: (document.total_segments + 2).to_s,
- message_reference_number: config.header.interchange_control_reference.to_s
+ footer.segment Eancom::Edifact::UNT.new(
+ number_of_segments_in_message: document.total_segments.to_s,
+ message_reference_number: config.header.message_reference_number
)
- end
- document.footer do |footer|
footer.segment Eancom::Edifact::UNZ.new(
- interchange_control_count: '1',
+ interchange_control_count: messages.size.to_s,
interchange_control_reference: config.header.interchange_control_reference.to_s
)
end
end