definitions/desadv/d01b.rb in eancom-1.6.3 vs definitions/desadv/d01b.rb in eancom-2.0.0
- old
+ new
@@ -1,7 +1,5 @@
-# frozen_string_literal: true
-
Eancom::Definition.create(name: 'D01B', type: :desadv) do |document, config|
document.header do |header|
header.segment Eancom::Edifact::UNA.new
header.segment Eancom::Edifact::UNB.new(
@@ -12,12 +10,12 @@
interchange_recipient_identification: config.header.interchange_recipient_identification,
recipient_identifiction_code_qualifier: '14',
date: config.header.date,
time: config.header.time,
interchange_control_reference: config.header.interchange_control_reference,
- application_reference: 'DESADV0720',
- interchange_agreement_identifier: 'EANCOM76170270000007613067000000900',
+ application_reference: config.header.application_reference,
+ interchange_agreement_identifier: config.header.interchange_agreement_identifier,
test_indicator: '0'
)
header.segment Eancom::Edifact::UNH.new(
message_reference_number: config.header.message_reference_number,
@@ -33,172 +31,221 @@
# Messages
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: '204'
- )
+ date_time_messages = message.date_time_messages
+ date_time_messages.each_with_index do |dtm, index|
+ next unless index == 0
- # Delivery Date Estimated
- if date_time = message.delivery_date_time_estimated.date_time
+ structure = Eancom.find_structure(tag: 'DTM')
+ key = dtm.identifier.intern.to_sym
body.segment Eancom::Edifact::DTM.new(
- type: '17',
- date_time: date_time,
+ type: structure.dictionary_lookup(:type, dtm.identifier),
+ date_time: dtm[key].date_time,
format: '204'
)
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.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: '204'
)
end
- # Buyer
- if buyer = message.buyer
- body.segment Eancom::Edifact::NAD.new(
- party_function_code_qualifier: 'BY',
- party_identifier: buyer.party_identifier,
- code_list_responsible_agancy_code_1: '9'
+ 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: structure.dictionary_lookup(:reference_code_qualifier, rff.identifier),
+ reference_identifier: rff[key].reference_identifier
)
end
# Supplier
- if supplier = message.supplier
+ 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
# Manufacturer ILN
- if manufacturer = message.manufacturer
+ 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
- # Delivery Party
- if delivery_party = message.delivery_party
+ 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: 'DP',
- party_identifier: delivery_party.party_identifier,
- code_list_responsible_agancy_code_1: '9',
- city_name: delivery_party.city_name,
- party_name_1: delivery_party.party_name_1,
- postal_identification_code: delivery_party.postal_identification_code,
- street_and_number_1: delivery_party.street_and_number_1,
- country_name_code: delivery_party.country_name_code
+ 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',
+ city_name: nad[key].city_name,
+ party_name_1: nad[key].party_name_1,
+ postal_identification_code: nad[key].postal_identification_code,
+ street_and_number_1: nad[key].street_and_number_1,
+ country_name_code: nad[key].country_name_code
)
end
# Ultimate consignee
- if ultimate_consignee = message.ultimate_consignee
+ 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: 'UC',
- party_identifier: ultimate_consignee.party_identifier,
- code_list_responsible_agancy_code_1: '9',
- city_name: ultimate_consignee.city_name,
- party_name_1: ultimate_consignee.party_name_1,
- postal_identification_code: ultimate_consignee.postal_identification_code,
- street_and_number_1: ultimate_consignee.street_and_number_1,
- country_name_code: ultimate_consignee.country_name_code
+ 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',
+ city_name: nad[key].city_name,
+ party_name_1: nad[key].party_name_1,
+ postal_identification_code: nad[key].postal_identification_code,
+ street_and_number_1: nad[key].street_and_number_1,
+ country_name_code: nad[key].country_name_code
)
end
+ structure = Eancom.find_structure(tag: 'TDT')
+ qualifier = structure.dictionary_lookup(:transport_stage_code_qualifier, message.transport_stage_code_qualifier)
body.segment Eancom::Edifact::TDT.new(
- transport_stage_code_qualifier: '20',
- transport_mode_name_code: '30'
+ transport_stage_code_qualifier: qualifier,
+ transport_mode_name_code: message.transport_mode_name_code
)
- # Delivery Note Mode
+ # CPS can remain hardcoded.
body.segment Eancom::Edifact::CPS.new(
hierarchical_structure_level_identifier: '1'
)
body.segment Eancom::Edifact::PAC.new(
- package_quantity: '1'
+ package_quantity: message.package_quantity
)
body.segment Eancom::Edifact::CPS.new(
hierarchical_structure_level_identifier: '2',
- hierarchical_structure_parent_identifier: '1'
+ hierarchical_structure_parent_identifier: message.hierarchical_structure_parent_identifier
)
# Items
total_quantity = 0
items = message.items
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 || index,
+ line_item_identifier_1: (index + 1).to_s,
item_identifier: item.ean,
- item_type_identification_code: 'SRV'
+ item_type_identification_code: structure.dictionary_lookup(:item_type_identification_code, item.item_type_identification_code)
)
if additional_product_id = item.additional_product_id
additional_product_id.each do |id|
- if (additional_information = id.additional_information)
- body.segment Eancom::Edifact::PIA.new(
- product_identifier_code_qualifier: '1',
- item_identifier_1: additional_information.item_identifier_1,
- item_type_identification_code_1: 'IN'
- )
- end
+ next unless additional_information = id.additional_information
+
+ structure = Eancom.find_structure(tag: 'PIA')
+ body.segment Eancom::Edifact::PIA.new(
+ product_identifier_code_qualifier: structure.dictionary_lookup(:product_identifier_code_qualifier, additional_information.product_identifier_code_qualifier),
+ item_identifier_1: additional_information.item_identifier_1,
+ item_type_identification_code_1: structure.dictionary_lookup(:item_type_identification_code_1,
+ additional_information.item_type_identification_code_1)
+ )
end
end
- body.segment Eancom::Edifact::IMD.new(
- description_format_code: 'C',
- item_description_code: 'CU',
- code_list_responsible_agency_code_1: '9'
- )
+ item_descriptions = item.item_descriptions
+ item_descriptions.each do |description|
+ structure = Eancom.find_structure(tag: 'IMD')
+ body.segment Eancom::Edifact::IMD.new(
+ description_format_code: structure.dictionary_lookup(:description_format_code,
+ description.description_format_code),
+ item_description_code: structure.dictionary_lookup(:item_description_code,
+ description.item_description_code),
+ code_list_responsible_agency_code_1: structure.dictionary_lookup(:code_list_responsible_agency_code_1, description.code_list_responsible_agency_code_1)
+ )
+ end
- body.segment Eancom::Edifact::QTY.new(
- quantity_type_code_qualifier: '12',
- quantity: item.despatch_quantity.quantity.to_s
- )
+ quantities = item.quantities
+ quantities.each_with_index do |qty, index|
+ next unless index == 0
+ 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.to_s
+ )
+ end
- body.segment Eancom::Edifact::QTY.new(
- quantity_type_code_qualifier: '59',
- quantity: item.despatch_quantity.quantity.to_s
- )
+ quantities.each_with_index do |qty, index|
+ next unless index == 1
+ 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.to_s
+ )
+ end
- if (order_number_buyer = message.order_number_buyer)
+
+ references = item.references
+ references.each_with_index do |rff, index|
+ structure = Eancom.find_structure(tag: 'RFF')
+ key = rff.identifier.intern.to_sym
body.segment Eancom::Edifact::RFF.new(
- reference_code_qualifier: 'ON',
- reference_identifier: order_number_buyer.reference_identifier,
- document_line_identifier: ((index + 1) * 10).to_s
+ reference_code_qualifier: structure.dictionary_lookup(:reference_code_qualifier, rff.identifier),
+ reference_identifier: rff[key].reference_identifier,
+ document_line_identifier: rff[key].document_line_identifier
)
end
end
body.segment Eancom::Edifact::UNT.new(
number_of_segments_in_message: (document.total_segments + 2).to_s,
- message_reference_number: '1'
+ message_reference_number: config.header.message_reference_number
)
end
end
+ messages = config.body.messages
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