Eancom::Definition.create(name: 'D01B', type: :invoic) 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: config.header.application_reference, interchange_agreement_identifier: config.header.interchange_agreement_identifier, test_indicator: nil ) 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 = config.body.messages messages.each do |message| body.segment Eancom::Edifact::BGM.new( document_name_code: message.document_name_code, document_identifier: message.document_identifier, message_function_code: message.message_function_code ) date_time_messages = message.date_time_messages date_time_messages.each_with_index do |dtm, index| next unless index == 0 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 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 free_texts = message.free_texts free_texts.each do |free_text| free_text_function_code = if free_text.free_text_function_code.nil? '' else free_text.free_text_function_code end free_text_value_code = if free_text.free_text_value_code.nil? '' else free_text.free_text_value_code end code_list_identification_code = if free_text.code_list_identification_code.nil? '' else free_text.code_list_identification_code end structure = Eancom.find_structure(tag: 'FTX') body.segment Eancom::Edifact::FTX.new( text_subject_code_qualifier: free_text.text_subject_code_qualifier, free_text_function_code: free_text_function_code, free_text_value_code: free_text_value_code, code_list_identification_code: code_list_identification_code, free_text_value_1: free_text.free_text_value_1 ) end 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 date_time_messages.each_with_index do |dtm, index| next unless index == 2 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 references.each_with_index do |rff, index| next unless index == 1 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 date_time_messages.each_with_index do |dtm, index| next unless index == 3 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 references.each_with_index do |rff, index| next unless index == 2 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 date_time_messages.each_with_index do |dtm, index| next unless index == 4 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 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: structure.dictionary_lookup(:party_function_code_qualifier, nad.identifier), party_identifier: nad[key].party_identifier, code_list_responsible_agency_code_1: '9', name_and_address_description_1: nad[key].name_and_address_description_1 ) end 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: structure.dictionary_lookup(:party_function_code_qualifier, nad.identifier), party_identifier: nad[key].party_identifier, code_list_responsible_agency_code_1: '9', name_and_address_description_1: nad[key].name_and_address_description_1 ) end references.each_with_index do |rff, index| next unless index == 3 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 references.each_with_index do |rff, index| next unless index == 4 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 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: structure.dictionary_lookup(:party_function_code_qualifier, nad.identifier), party_identifier: nad[key].party_identifier, code_list_responsible_agency_code_1: '9', name_and_address_description_1: nad[key].name_and_address_description_1 ) end 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: structure.dictionary_lookup(:party_function_code_qualifier, nad.identifier), party_identifier: nad[key].party_identifier, code_list_responsible_agency_code_1: '9', name_and_address_description_1: nad[key].name_and_address_description_1 ) end if taxes = message.taxes taxes.each do |tax| next unless tax = tax.tax structure = Eancom.find_structure(tag: 'TAX') body.segment Eancom::Edifact::TAX.new( duty_or_tax_or_fee_function_code_qualifier: structure.dictionary_lookup( :duty_or_tax_or_fee_function_code_qualifier, tax.duty_or_tax_or_fee_function_code_qualifier ), duty_or_tax_or_fee_name_code: structure.dictionary_lookup(:duty_or_tax_or_fee_name_code, tax.duty_or_tax_or_fee_name_code), duty_or_tax_or_fee_account_code: tax.duty_or_tax_or_fee_account_code, duty_tax_fee_rate_code: tax.duty_tax_fee_rate_code, code_list_identification_code_3: tax.code_list_identification_code_3, code_list_responsibility_agency_code_3: tax.code_list_responsibility_agency_code_3, code_list_responsibility_agency_code_4: '9', duty_or_tax_or_fee_category_code: 'S' ) end end structure = Eancom.find_structure(tag: 'CUX') body.segment Eancom::Edifact::CUX.new( currency_usage_code_qualifier_1: structure.dictionary_lookup(:currency_usage_code_qualifier_1, message.currency.currency_usage_code_qualifier_1), currency_identification_code_1: message.currency.currency_identification_code_1, currency_type_code_qualifier_1: structure.dictionary_lookup(:currency_type_code_qualifier_1, message.currency.currency_type_code_qualifier_1) ) if allowances_or_charges = message.allowances_or_charges allowances_or_charges.each do |allowance_or_charge| allowance_or_charge_identifier = if allowance_or_charge.allowance_or_charge_identifier.nil? '' else allowance_or_charge.allowance_or_charge end allowance_or_charge_identification_code = if allowance_or_charge.allowance_or_charge_identification_code.nil? '' else allowance_or_charge.allowance_or_charge_identification_code end settlement_mean_code = if allowance_or_charge.settlement_mean_code.nil? '' else allowance_or_charge.settlement_mean_code end structure = Eancom.find_structure(tag: 'ALC') body.segment Eancom::Edifact::ALC.new( allowance_or_charge_code_qualifier: structure.dictionary_lookup(:allowance_or_charge_code_qualifier, allowance_or_charge.allowance_or_charge_code_qualifier), allowance_or_charge_identifier: allowance_or_charge_identifier, allowance_or_charge_identification_code: allowance_or_charge_identification_code, settlement_mean_code: settlement_mean_code, special_service_description_code: structure.dictionary_lookup(:special_service_description_code, allowance_or_charge.special_service_description_code) ) end end monetary_amounts = message.monetary_amounts monetary_amounts.each do |amount| monetary_amount = if amount.monetary_amount.nil? '' else amount.monetary_amount end structure = Eancom.find_structure(tag: 'MOA') body.segment Eancom::Edifact::MOA.new( monetary_amount_type_code_qualifier: structure.dictionary_lookup(:monetary_amount_type_code_qualifier, amount.monetary_amount_type_code_qualifier), monetary_amount: monetary_amount ) end items = message.items items.each_with_index do |item, index| structure = Eancom.find_structure(tag: 'LIN') body.segment Eancom::Edifact::LIN.new( line_item_identifier_1: (index + 1).to_s, item_identifier: item.ean, 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| 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 if 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_characteristic_code: structure.dictionary_lookup(:item_characteristic_code, description.item_characteristic_code), item_description_code: description.item_description_code, code_list_identification_code_1: description.item_description_code, code_list_responsible_agency_code_1: description.item_description_code, item_description_1: description.item_description_1 ) end end 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, measurement_unit_code: structure.dictionary_lookup(:measurement_unit_code, qty.measurement_unit_code) ) end monetary_amounts = item.monetary_amounts monetary_amounts.each_with_index do |amount, index| if index == 0 monetary_amount = if amount.monetary_amount.nil? '' else amount.monetary_amount end structure = Eancom.find_structure(tag: 'MOA') body.segment Eancom::Edifact::MOA.new( monetary_amount_type_code_qualifier: structure.dictionary_lookup(:monetary_amount_type_code_qualifier, amount.monetary_amount_type_code_qualifier), monetary_amount: monetary_amount ) else next end end prices = item.prices prices.each do |price| next unless price = price.price structure = Eancom.find_structure(tag: 'PRI') body.segment Eancom::Edifact::PRI.new( price_code_qualifier: structure.dictionary_lookup(:price_code_qualifier, price.price_code_qualifier), price_amount: price.price_amount, price_type_code: structure.dictionary_lookup(:price_type_code, price.price_type_code), price_specification_code: structure.dictionary_lookup(:price_specification_code, price.price_specification_code) ) end 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: structure.dictionary_lookup(:reference_code_qualifier, rff.identifier), reference_identifier: rff[key].reference_identifier ) end date_time_messages = item.date_time_messages date_time_messages.each_with_index do |dtm, index| key = dtm.identifier.intern.to_sym structure = Eancom.find_structure(tag: 'DTM') body.segment Eancom::Edifact::DTM.new( type: structure.dictionary_lookup(:type, dtm.identifier), date_time: dtm[key].date_time, format: '102' ) end end end document.footer do |footer| footer.segment Eancom::Edifact::UNS.new( section_identification: 'S' ) messages = config.body.messages messages.each do |message| items = message.items control_total_value = 0 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 == items.size - 1 counts = item.counts counts.each_with_index do |cnt, index| 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 - 1).to_s ) end end items = message.items items.each do |item| monetary_amounts = item.monetary_amounts monetary_amount = 0 monetary_amounts.each_with_index do |amount, index| next unless index >= items.size - 4 && index <= items.size - 2 monetary_amount = if amount.monetary_amount.nil? '' else amount.monetary_amount end structure = Eancom.find_structure(tag: 'MOA') footer.segment Eancom::Edifact::MOA.new( monetary_amount_type_code_qualifier: structure.dictionary_lookup(:monetary_amount_type_code_qualifier, amount.monetary_amount_type_code_qualifier), monetary_amount: monetary_amount ) end end if taxes = message.taxes taxes.each do |tax| next unless tax = tax.tax structure = Eancom.find_structure(tag: 'TAX') footer.segment Eancom::Edifact::TAX.new( duty_or_tax_or_fee_function_code_qualifier: '7', duty_or_tax_or_fee_name_code: structure.dictionary_lookup(:duty_or_tax_or_fee_name_code, tax.duty_or_tax_or_fee_name_code), duty_or_tax_or_fee_account_code: tax.duty_or_tax_or_fee_account_code, duty_tax_fee_rate_code: tax.duty_tax_fee_rate_code, code_list_identification_code_3: tax.code_list_identification_code_3, code_list_responsibility_agency_code_3: tax.code_list_responsibility_agency_code_3, code_list_responsibility_agency_code_4: '9', duty_or_tax_or_fee_category_code: 'S' ) end end items.each do |item| monetary_amounts = item.monetary_amounts monetary_amount = 0 monetary_amounts.each_with_index do |amount, index| next unless index == items.size - 1 monetary_amount = if amount.monetary_amount.nil? '' else amount.monetary_amount end structure = Eancom.find_structure(tag: 'MOA') footer.segment Eancom::Edifact::MOA.new( monetary_amount_type_code_qualifier: structure.dictionary_lookup(:monetary_amount_type_code_qualifier, amount.monetary_amount_type_code_qualifier), monetary_amount: monetary_amount ) end end next unless allowances_or_charges = message.allowances_or_charges allowances_or_charges.each do |allowance_or_charge| allowance_or_charge_identifier = if allowance_or_charge.allowance_or_charge_identifier.nil? '' else allowance_or_charge.allowance_or_charge_identifier end structure = Eancom.find_structure(tag: 'ALC') footer.segment Eancom::Edifact::ALC.new( allowance_or_charge_code_qualifier: structure.dictionary_lookup(:allowance_or_charge_code_qualifier, allowance_or_charge.allowance_or_charge_code_qualifier), allowance_or_charge_identifier: allowance_or_charge_identifier, allowance_or_charge_identification_code: '64' ) end items.each do |item| monetary_amounts = item.monetary_amounts monetary_amount = 0 monetary_amounts.each_with_index do |amount, index| next unless index == items.size monetary_amount = if amount.monetary_amount.nil? '' else amount.monetary_amount end structure = Eancom.find_structure(tag: 'MOA') footer.segment Eancom::Edifact::MOA.new( monetary_amount_type_code_qualifier: structure.dictionary_lookup(:monetary_amount_type_code_qualifier, amount.monetary_amount_type_code_qualifier), monetary_amount: monetary_amount ) end end end footer.segment Eancom::Edifact::UNT.new( number_of_segments_in_message: (document.total_segments + 2).to_s, message_reference_number: config.header.message_reference_number ) footer.segment Eancom::Edifact::UNZ.new( interchange_control_count: messages.size.to_s, interchange_control_reference: config.header.interchange_control_reference ) end end end