definitions/slsrpt/d96a.rb in eancom-1.6.3 vs definitions/slsrpt/d96a.rb in eancom-2.0.0

- old
+ new

@@ -1,19 +1,20 @@ Eancom::Definition.create(name: 'D96A', type: :slsrpt) do |document, config| + # Creates a template for an slsrpt d01b formatted edifact document document.header do |header| - header.segment Eancom::Edifact::UNA.new() + header.segment Eancom::Edifact::UNA.new header.segment Eancom::Edifact::UNB.new( - syntax_identifier: config.header.syntax_identifier || 'UNOA', - syntax_version_number: config.header.syntax_version_number || '2', + syntax_identifier: 'UNOA', + syntax_version_number: '2', 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 ) header.segment Eancom::Edifact::UNH.new( message_reference_number: config.header.message_reference_number, message_type: config.header.message_type, @@ -23,174 +24,176 @@ 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: '11', - document_identifier: message.document_identifier, + document_name_code: message.document_name_code, + document_identifier: message.document_identifier ) - # Document Date - body.segment Eancom::Edifact::DTM.new( - type: '137', - date_time: message.document_message_date_time.date_time, - format: '102' - ) - - # Report Start Date - if date_time = message.report_start_date.date_time + 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: '90', - date_time: date_time, + type: structure.dictionary_lookup(:type, dtm.identifier), + date_time: dtm[key].date_time, format: '102' ) end - # Report End Date - if date_time = message.report_end_date.date_time - body.segment Eancom::Edifact::DTM.new( - type: '91', - date_time: 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 + 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 + + # Supplier - if supplier = message.supplier - code_list_responsible_agency_code_1 = if supplier.code_list_responsible_agancy_code_1 - structure = Eancom.find_structure(tag: 'NAD') - structure.dictionary_lookup( - :code_list_responsible_agancy_code_1, - supplier.code_list_responsible_agancy_code_1 - ) - else - '9' - 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: 'SU', - party_identifier: supplier.party_identifier, - code_list_responsible_agancy_code_1: code_list_responsible_agency_code_1 + 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 # Buyer - if buyer = message.buyer - code_list_responsible_agency_code_1 = if supplier.code_list_responsible_agancy_code_1 - structure = Eancom.find_structure(tag: 'NAD') - structure.dictionary_lookup( - :code_list_responsible_agancy_code_1, - supplier.code_list_responsible_agancy_code_1 - ) - else - '9' - 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: 'BY', - party_identifier: buyer.party_identifier, - code_list_responsible_agancy_code_1: code_list_responsible_agency_code_1 + 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 # Reference Currency if currency = message.currency + structure = Eancom.find_structure(tag: 'CUX') body.segment Eancom::Edifact::CUX.new( currency_identification_code_1: currency.currency_identification_code_1, - currency_type_code_qualifier_1: '10', - currency_usage_code_qualifier_1: '2' + currency_type_code_qualifier_1: structure.dictionary_lookup(:currency_type_code_qualifier_1, + currency.currency_type_code_qualifier_1), + currency_usage_code_qualifier_1: structure.dictionary_lookup(:currency_usage_code_qualifier_1, currency.currency_usage_code_qualifier_1) ) end - item_counter = 0 + # Location + if location = message.location + structure = Eancom.find_structure(tag: 'LOC') + body.segment Eancom::Edifact::LOC.new( + code_list_identification_code: location.code_list_identification_code, + code_list_responsible_agency_code: structure.dictionary_lookup(:code_list_responsible_agency_code, location.code_list_responsible_agency_code), + location_function_qualifier: structure.dictionary_lookup(:location_function_qualifier, location.location_function_qualifier), + location_name_code: location.location_name_code + ) + end - message.locations.each do |location_group| + 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: '203' + ) + end - # Location - if location = location_group.location - code_list_responsible_agency_code = if location.code_list_responsible_agancy_code - structure = Eancom.find_structure(tag: 'LOC') - structure.dictionary_lookup( - :code_list_responsible_agancy_code, - location.code_list_responsible_agancy_code - ) - else - '9' - end - body.segment Eancom::Edifact::LOC.new( - code_list_identification_code: location.code_list_identification_code, - code_list_responsible_agancy_code: code_list_responsible_agency_code, - location_function_qualifier: '150', - location_name_code: location.location_name_code - ) - end + # Items + total_quantity = 0 - # Sales Date - if date_time = location_group.sales_date_and_or_time_and_or_period&.date_time - body.segment Eancom::Edifact::DTM.new( - type: '356', - date_time: date_time, - format: '203' + 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: (index + 1).to_s, + item_identifier: item.ean, + item_type_identification_code: structure.dictionary_lookup(:item_type_identification_code, item.item_type_identification_code) + ) + + 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 - # Items - location_group.items.each do |item| - item_counter += 1 + prices = item.prices + prices.each do |price| + next unless price = price.price - body.segment Eancom::Edifact::LIN.new( - line_item_identifier_1: item_counter.to_s, - item_identifier: item.ean, - item_type_identification_code: 'EN' + structure = Eancom.find_structure(tag: 'PRI') + body.segment Eancom::Edifact::PRI.new( + price_amount: price.price_amount, + price_code_qualifier: structure.dictionary_lookup(:price_code_qualifier, price.price_code_qualifier), + price_specification_code: structure.dictionary_lookup(:price_specification_code, + price.price_specification_code), + price_type_code: price.price_type_code ) + end - if transaction_reference_number = item.transaction_reference_number - body.segment Eancom::Edifact::RFF.new( - reference_code_qualifier: 'TN', - reference_identifier: transaction_reference_number.reference_identifier - ) - end - - prices = item.prices - prices.each do |price| - if price = price.price - structure = Eancom.find_structure(tag: 'PRI') - body.segment Eancom::Edifact::PRI.new( - price_amount: price.price_amount, - price_code_qualifier: structure.dictionary_lookup(:price_code_qualifier, price.price_code_qualifier), - price_specification_code: structure.dictionary_lookup(:price_specification_code, price.price_specification_code), - price_type_code: price.price_type_code - ) - end - end - - # Quantity - if minimum_production_batch = item.minimum_production_batch - body.segment Eancom::Edifact::QTY.new( - quantity_type_code_qualifier: '160', - quantity: minimum_production_batch.quantity, - measurement_unit_code: 'PCE' - ) - end + qty = item.quantities + qty.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 end - body.segment Eancom::Edifact::UNT.new( - number_of_segments_in_message: "#{document.total_segments + 2}", - message_reference_number: config.header.message_reference_number, + number_of_segments_in_message: (document.total_segments + 2).to_s, + message_reference_number: config.header.message_reference_number ) 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 + document.footer do |footer| + messages = config.body.messages + + footer.segment Eancom::Edifact::UNZ.new( + interchange_control_count: messages.size.to_s, + interchange_control_reference: config.header.interchange_control_reference.to_s + ) end end