require "httparty" module Comee module Core class BeoService def initialize(id) @atlas_token = ENV.fetch("ATLAS_TOKEN") @atlas_base_url = ENV.fetch("ATLAS_BASE_URL") @encoded_auth = ENV.fetch("ENCODED_AUTH") @customs_detail = Comee::Core::CustomsDetail.includes(sales_order: :customer_order).find_by(sales_order_id: id) @line_items = Comee::Core::SalesOrderItem.includes(:product, :unit, :customer_order_item, :source, :purchase_order_item).where(sales_order_id: id) @transportation_routes = [] @position_data = [] raise(StandardError, "No customs detail filed for sales order") unless @customs_detail.present? @address = @customs_detail.sales_order.customer_order.client.address.split(", ") @customs_detail.transportation_route.each do |tr| @transportation_routes << tr["route"][0, 2] end end def prepare_data @line_items.each do |line_item, index| mp = Comee::Core::MasterPrice.includes(:country_of_origin).find_by(primary: true, product_id: line_item.product.id) sii = Comee::Core::ShipmentItem.find_by(sales_order_item_id: line_item.id) @position_data << { "warePositionsnummer": index, "wareWarennummerKN8": line_item.product.hs_code, "wareWarenbezeichnung": line_item.product.hs_description, "wareRegistriernummerFremdsystem": @customs_detail.sales_order.order_number, "wareUrsprungsbundesland": mp.state_of_origin, "wareEigenmasse": line_item.product.weight * line_item.quantity, "wareRohmasse": line_item.product.weight * line_item.quantity, "ausfuhrLand": "DE", "ursprungsland": mp.country_of_origin.code, "beantragtesVerfahren": "10", "vorhergehendesVerfahren": "00", "zusatzlichesVerfahren": "F61", "aussenhandelsstatistikMenge": line_item.quantity, "aussenhandelsstatistikWert": line_item.quantity * line_item.price, "packstuck": [ { "packstuckNummer": "1", "packstuckAnzahl": sii.pallet_no, "packstuckVerpackungsart": sii.package_type.split(" ")[0], "packstuckZeichenNummern": @customs_detail.sales_order.order_number } ] } end data = { "dataIdentifier": "", "kundenNumber": "", "mandantId": "", "module": { "ausfuhr": { "data": "true", "email": "", "autosend": "false" } }, "messageData": { "ausfuhr": { "expdat": [ { "kopf": { "eoriNiederlassungsnummer": "DE47897410000", "dienststellennummer": "DE014851", "artderAnmeldung": @customs_detail.registration_type.split(" ")[0], "artderAnmeldungAusfuhr": @customs_detail.export_declaration_type, "ausfuhrLand": "DE", "beteiligtenKonstellation": @customs_detail.participant_constellation.split(" ")[0], "sicherheit": @customs_detail.registration_type.split(" ")[0] == "CO" ? 0 : 2, "container": @customs_detail.containerized == true ? 1 : 0, "bestimmungsLand": @customs_detail.destination_country, "referenznummerUCR": @customs_detail.sales_order.customer_order.consignee, "lrn": @customs_detail.sales_order.order_number, "beforderungsmittelImInlandVerkehrszweig": @customs_detail.mode_of_transport, "beforderungsmittelderGrenzeVerkehrszweig": @customs_detail.mode_of_transport_at_border, "beforderungsmittelderGrenzeArt": @customs_detail.mode_of_transport_type, "beforderungsmittelderGrenzeKennzeichen": "UNBEKANNT", "beforderungsmittelderGrenzeStaatszugehorigkeit": @customs_detail.mode_of_transport_nationality.split(" ")[0], "beforderungsmittelBeimAbgang": [ { "sequenznummer": "1", "artderIdentifikation": @customs_detail.type_of_identification.split(" ")[0], "kennzeichen": @customs_detail.additional_identifier.split(" ")[0], "staatszugehorigkeit": @customs_detail.nationality.split(" ")[0] } ], "ausfuhrzollstelleDienststellennummer": @customs_detail.export_customs_office.split(" ")[0], "vorgeseheneAusgangszollstelleDienststellennummer": @customs_detail.customs_office_of_exit.split(" ")[0], "geschaftsvorgangArt": "11", "geschaftsvorgangRechnungspreis": @customs_detail.sales_order.total_price, "geschaftsvorgangWahrung": "EUR", "beforderungsroute": { "ausgewahlteLander": @transportation_routes, "beforderungsVon": @customs_detail.transportation_route[0]["route"].split(" ")[0], "beforderungsBis": @customs_detail.transportation_route[-1]["route"].split(" ")[0] }, "empfanger": { "tin": "", "niederlassungsNummer": "", "name": @customs_detail.sales_order.customer_order.client.name, "strasse": @address[0], "plz": @address[-1], "ort": "#{@address[1].split(' ')[-3]} #{@address[1].split(' ')[-2]}", "land": @address[1].split(" ")[-1] }, "warenortArtdesOrtes": "B", "warenortArtDerOrtsbestimmung": "Y", "warenortZusatzlicheKennung": @customs_detail.additional_identifier.split(" ")[0], "lieferbedingungIncotermCode": @customs_detail.delivery_term_code[0, 3], "lieferbedingungOrt": "Hamburg" }, "position": @position_data } ] } } } JSON(data) end def send_customs_details response = HTTParty.post("#{@atlas_base_url}/PutData", headers: {'Content-Type': "application/json", 'token': @atlas_token, 'bearertoken': bearer_token}, body: prepare_data) unless [200, 201].include?(response.code) raise(StandardError, "Failed to send POST request with token: #{response.code} - #{response.body}") end JSON.parse(response.body) end private def bearer_token response = HTTParty.post("#{@atlas_base_url}/authenticate", headers: { 'Authorization': "Basic #{@encoded_auth}", 'Accept': "application/json" }) raise(StandardError, "Failed to obtain bearer token: #{response.code} - #{response.body}") unless response.code == 200 JSON.parse(response.body)["accessToken"] end end end end