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( syntax_identifier: 'UNOC', syntax_version_number: '3', interchange_sender_identification: config.header.interchange_sender_identification, sender_identifiction_code_qualifier: '14', 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', test_indicator: '0' ) header.segment Eancom::Edifact::UNH.new( message_reference_number: config.header.message_reference_number, message_type: config.header.message_type, message_version_number: config.header.message_version_number, message_release_number: config.header.message_release_number, controlling_agency: config.header.controlling_agency, association_assigned_code: config.header.association_assigned_code ) end document.body do |body| # Messages messages = config.body.messages messages.each do |message| body.segment Eancom::Edifact::BGM.new( document_name_code: '351', document_identifier: message.document_identifier, message_function_code: '9' ) # Document Date body.segment Eancom::Edifact::DTM.new( type: '137', date_time: message.document_message_date_time.date_time, format: '204' ) # Delivery Date Estimated if date_time = message.delivery_date_time_estimated.date_time body.segment Eancom::Edifact::DTM.new( type: '17', date_time: date_time, format: '204' ) end # Order Reference if reference_identifier = message.order_number.reference_identifier body.segment Eancom::Edifact::RFF.new( reference_code_qualifier: 'ON', reference_identifier: reference_identifier ) 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' ) end # Supplier if supplier = message.supplier body.segment Eancom::Edifact::NAD.new( party_function_code_qualifier: 'SU', party_identifier: supplier.party_identifier, code_list_responsible_agancy_code_1: '9' ) end # Manufacturer ILN if manufacturer = message.manufacturer body.segment Eancom::Edifact::NAD.new( party_function_code_qualifier: 'MF', party_identifier: manufacturer.party_identifier, code_list_responsible_agancy_code_1: '9' ) end # Delivery Party if delivery_party = message.delivery_party 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 ) end # Ultimate consignee if ultimate_consignee = message.ultimate_consignee 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 ) end body.segment Eancom::Edifact::TDT.new( transport_stage_code_qualifier: '20', transport_mode_name_code: '30' ) # Delivery Note Mode body.segment Eancom::Edifact::CPS.new( hierarchical_structure_level_identifier: '1' ) body.segment Eancom::Edifact::PAC.new( package_quantity: '1' ) body.segment Eancom::Edifact::CPS.new( hierarchical_structure_level_identifier: '2', hierarchical_structure_parent_identifier: '1' ) # Items total_quantity = 0 items = message.items items.each_with_index do |item, index| total_quantity += item.quantity.to_i body.segment Eancom::Edifact::LIN.new( line_item_identifier_1: "#{index + 1}", item_identifier: item.ean, item_type_identification_code: 'SRV' ) 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 end end body.segment Eancom::Edifact::IMD.new( description_format_code: 'C', item_description_code: 'CU', code_list_responsible_agency_code_1: '9' ) body.segment Eancom::Edifact::QTY.new( quantity_type_code_qualifier: '12', quantity: "#{item.despatch_quantity.quantity}" ) body.segment Eancom::Edifact::QTY.new( quantity_type_code_qualifier: '59', quantity: "#{item.despatch_quantity.quantity}" ) if order_number = message.order_number body.segment Eancom::Edifact::RFF.new( reference_code_qualifier: 'ON', reference_identifier: order_number.reference_identifier, document_line_identifier: "#{(index + 1) * 10}" ) end end body.segment Eancom::Edifact::UNT.new( number_of_segments_in_message: "#{document.total_segments + 2}", message_reference_number: '1' ) end end document.footer do |footer| footer.segment Eancom::Edifact::UNZ.new( interchange_control_count: '1', interchange_control_reference: "#{config.header.interchange_control_reference}" ) end end