Class: Comee::Core::BeoService

Inherits:
Object
  • Object
show all
Defined in:
app/services/comee/core/beo_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(ids) ⇒ BeoService

Returns a new instance of BeoService.



6
7
8
9
10
11
# File 'app/services/comee/core/beo_service.rb', line 6

def initialize(ids)
  @atlas_token = ENV.fetch("ATLAS_TOKEN")
  @atlas_base_url = ENV.fetch("ATLAS_BASE_URL")
  @encoded_auth = ENV.fetch("ENCODED_AUTH")
  @ids = ids
end

Instance Method Details

#generate_xmlObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'app/services/comee/core/beo_service.rb', line 239

def generate_xml
  Nokogiri::XML::Builder.new(encoding: "Windows-1252") do |xml|
    xml.BEO_ATLAS("xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
                  "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema") do
      xml.Nachricht do
        xml.Zollnummer_Sender "4789741"
        xml.Nachrichten_Typ "DEXPDF"
        xml.BEO_Atlas_KundenID
        xml.Anwendung
        xml.Name "MAVEKO"
      end
      xml.Kopf do
        xml.EORI_Number "DE47897410000"
        xml.Art_der_Ausfuhranmeldung @customs_details[0].export_declaration_type.split(" ")[0]
        xml.Ausfuhrland "DE"
        xml.Bestimmungsland @customs_details[0].destination_country.split(" ")[0]
        xml.Container @customs_details[0].containerized == true ? 1 : 0
        xml.send("Beteiligten-Konstellation", @customs_details[0].participant_constellation.split(" ")[0])
        xml.send("Gesamt-Rohmasse", @total_weight)
        xml.Kennnummer_der_Sendung @customs_details.map(&:sales_order).map(&:customer_order).map(&:consignee).join(" ")
        xml.Bezugsnummer @customs_details.map(&:sales_order).map(&:customer_order).map(&:order_number).join(" ")
        xml.inlandischerVerkehrszweig @customs_details[0].mode_of_transport.split(" ")[0]
        xml.verkehrszweigAnDerGrenze @customs_details[0].mode_of_transport_at_border.split(" ")[0]
        xml.Befoerderungsmittel_an_der_Grenze do
          xml.Art @customs_details[0].mode_of_transport_type.split(" ")[0]
          xml.Kennzeichen "UNBEKANNT"
        end
        xml.Befoerderungsmittel_am_Abgang do
          xml.sequenznummer "1"
          xml.Art @customs_details[0].type_of_identification.split(" ")[0]
          xml.Kennzeichen "UNBEKANNT"
          xml.Staatszugehoerigkeit @customs_details[0].nationality.split(" ")[0]
        end
        xml.Ausfuhrzollstelle do
          xml.Dienststellennummer @customs_details[0].export_customs_office.split(" ")[0]
        end
        xml.Vorgesehene_Ausgangszollstelle do
          xml.Dienststellennummer @customs_details[0].customs_office_of_exit.split(" ")[0]
        end
        xml.Geschaeftsvorgang do
          xml.Art "11"
          xml.Rechnungspreis @customs_details[0].sales_order.total_price.round(2)
          xml.Waehrung "EUR"
        end
        xml.Befoerderungsroute do
          @transportation_routes.each do |tr|
            xml.Land tr
          end
        end
        xml.Empfaenger do
          xml.Identifikationsart
          xml.TIN
          xml.Niederlassungsnummer
          xml.Name @customs_details[0].sales_order.customer_order.client.name
          xml.Strasse @address.street
          xml.PLZ @address.postal_code
          xml.Ort @address.city
          xml.Land @address.country.code
        end
        xml.Lieferbedingung do
          xml.send("Incoterm-Code", @customs_details[0].delivery_term_code[0, 3])
          xml.Ort "Hamburg"
          xml.Land "DE"
        end
      end
      xml.Waren do
        xml_position_data(xml)
      end
    end
  end.to_xml
end

#position_dataObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/services/comee/core/beo_service.rb', line 58

def position_data
  position_items = []
  @line_items.each_with_index do |line_item, index|
    si = line_item.shipment_item
    customs_detail = @customs_details.find_by(sales_order_id: si.sales_order_item.sales_order_id)
    mp = Comee::Core::MasterPrice.includes(:country_of_origin).find_by(primary: true,
                                                                       product_id: si
                                                                       .sales_order_item.product.id)

    position_items << {
      "warePositionsnummer": index + 1,
      "wareWarennummerKN8": si.sales_order_item.product.hs_code,
      "wareWarenbezeichnung": si.sales_order_item.product.customs_description,
      "wareRegistriernummerFremdsystem": customs_detail.sales_order.customer_order.order_number,
      "wareUrsprungsbundesland": mp.state_of_origin.split(" ")[0],
      "wareEigenmasse": (line_item.weight * line_item.quantity).round(2),
      "wareRohmasse": index.zero? ? @total_weight : 0,
      "ausfuhrLand": "DE",
      "ursprungsland": mp.country_of_origin.code,
      "beantragtesVerfahren": "10",
      "vorhergehendesVerfahren": "00",
      "zusatzlichesVerfahren": "F61",
      "aussenhandelsstatistikMenge": si.sales_order_item.quantity,
      "aussenhandelsstatistikWert": (si.sales_order_item.quantity *
                                      si.sales_order_item.price * 1.05).round(2).to_i,
      "packstuck": [
        {
          "packstuckNummer": index + 1,
          "packstuckAnzahl": index.zero? ? @no_of_packages : 0,
          "packstuckVerpackungsart": @package_type,
          "packstuckZeichenNummern": customs_detail.sales_order.customer_order.order_number
        }
      ]
    }
  end

  position_items
end

#prepare_dataObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/services/comee/core/beo_service.rb', line 97

def prepare_data
  {
    "dataIdentifier": "",
    "kundenNumber": "",
    "mandantId": "",
    "module": {
      "ausfuhr": {
        "data": "true",
        "email": "",
        "autosend": "false"
      }
    },
    "messageData": {
      "ausfuhr": {
        "expdat": [
          {
            "kopf": {
              "eoriNiederlassungsnummer": "DE47897410000",
              "artderAnmeldung": @customs_details[0].registration_type.split(" ")[0],
              "artderAnmeldungAusfuhr": @customs_details[0].export_declaration_type.split(" ")[0],
              "ausfuhrLand": "DE",
              "beteiligtenKonstellation": @customs_details[0].participant_constellation.split(" ")[0],
              "sicherheit": @customs_details[0].registration_type.split(" ")[0] == "CO" ? 0 : 2,
              "container": @customs_details[0].containerized == true ? 1 : 0,
              "bestimmungsLand": @customs_details[0].destination_country.split(" ")[0],
              "referenznummerUCR": @customs_details.map(&:sales_order).map(&:customer_order).map(&:consignee).join(" "),
              "lrn": @customs_details.map(&:sales_order).map(&:customer_order).map(&:order_number).join(" "),
              "beforderungsmittelImInlandVerkehrszweig": @customs_details[0].mode_of_transport.split(" ")[0],
              "beforderungsmittelderGrenzeVerkehrszweig": @customs_details[0].mode_of_transport_at_border.split(" ")[0],
              "beforderungsmittelderGrenzeArt": @customs_details[0].mode_of_transport_type.split(" ")[0],
              "beforderungsmittelderGrenzeKennzeichen": "UNBEKANNT",
              "beforderungsmittelderGrenzeStaatszugehorigkeit": @customs_details[0].mode_of_transport_nationality.split(" ")[0],
              "gesamtRohmasse": @total_weight,
              "beforderungsmittelBeimAbgang": [
                {
                  "sequenznummer": "1",
                  "artderIdentifikation": @customs_details[0].type_of_identification.split(" ")[0],
                  "kennzeichen": "UNBEKANNT",
                  "staatszugehorigkeit": @customs_details[0].nationality.split(" ")[0]
                }
              ],
              "ausfuhrzollstelleDienststellennummer": @customs_details[0].export_customs_office.split(" ")[0],
              "vorgeseheneAusgangszollstelleDienststellennummer": @customs_details[0].customs_office_of_exit.split(" ")[0],
              "geschaftsvorgangArt": "11",
              "geschaftsvorgangRechnungspreis": @customs_details[0].sales_order.total_price.round(2),
              "geschaftsvorgangWahrung": "EUR",
              "beforderungsroute": {
                "ausgewahlteLander": @transportation_routes,
                "beforderungsVon": "DE",
                "beforderungsBis": @customs_details[0].destination_country.split(" ")[0]
              },
              "empfanger": {
                "tin": "",
                "niederlassungsNummer": "",
                "name": @customs_details[0].sales_order.customer_order.client.name,
                "strasse": @address.street,
                "plz": @address.postal_code,
                "ort": @address.city,
                "land": @address.country.code
              },
              "warenortArtdesOrtes": "B",
              "warenortArtDerOrtsbestimmung": "Y",
              "warenortZusatzlicheKennung": @customs_details[0].additional_identifier.split(" ")[0],
              "lieferbedingungIncotermCode": @customs_details[0].delivery_term_code[0, 3],
              "lieferbedingungOrt": "Hamburg"
            },
            "position": position_data
          }
        ]
      }
    }
  }
end

#preprocess_data(xml: false) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/services/comee/core/beo_service.rb', line 13

def preprocess_data(xml: false)
  @ids.each do |id|
    @line_items = if xml
                    Comee::Core::ShipmentInstructionItem.includes(shipment_item: {sales_order_item: :sales_order})
                                                        .where(delivery_note_id: id)
                  else
                    Comee::Core::ShipmentInstructionItem.includes(shipment_item: {sales_order_item: :sales_order})
                                                        .where(shipment_instruction_id: id)
                  end
    sales_order_ids = @line_items.map { |item| item.shipment_item.sales_order_item.sales_order.id }

    @customs_details = Comee::Core::CustomsDetail.includes(sales_order: :customer_order).where(sales_order_id: sales_order_ids)
    @transportation_routes = []
    @position_data = []

    packaging_types = @line_items.map do |line_item|
      line_item.shipment_item.package_type.split(" ")[0]
    end.uniq

    @no_of_packages = packaging_types.count
    @package_type = packaging_types.count == 1 && packaging_types.first == "PX" ? "PX" : "PK"
    @total_weight = @line_items.sum do |line_item|
      (line_item.quantity * line_item.weight).round(2)
    end

    raise(StandardError, "No customs detail filed for sales order") unless @customs_details.present?

    @address = @customs_details[0].sales_order.delivery_address
    @customs_details[0].transportation_route.each_with_index do |tr, index|
      if xml
        @transportation_routes << tr["route"][0, 2]
      else
        @transportation_routes << tr["route"][0, 2] unless index.zero?
      end
    end

    @data = xml ? generate_xml : prepare_data
  end
  if xml
    {xml_data: @data, file_name: "#{@customs_details[0].sales_order.customer_order.order_number}_Ausfuhr"}
  else
    JSON(@data)
  end
end

#send_customs_detailsObject



171
172
173
174
175
176
177
178
179
180
181
# File 'app/services/comee/core/beo_service.rb', line 171

def send_customs_details
  response = HTTParty.post("#{@atlas_base_url}/PutData", headers: {'Content-Type': "application/json",
                                                                   'token': @atlas_token,
                                                                   'bearertoken': bearer_token}, body: preprocess_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

#xml_position_data(xml) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'app/services/comee/core/beo_service.rb', line 183

def xml_position_data(xml)
  @line_items.each_with_index do |line_item, index|
    sales_order_item = line_item.shipment_item.sales_order_item
    customs_detail = @customs_details.find_by(sales_order_id: sales_order_item.sales_order_id)
    mp = Comee::Core::MasterPrice.includes(:country_of_origin).find_by(primary: true,
                                                                       product_id: sales_order_item.product.id)

    xml.Position do
      xml.Positionsnummer (index + 1).to_s
      xml.Warenbezeichnung sales_order_item.product.customs_description
      xml.Registriernummer_Fremdsystem customs_detail.sales_order.customer_order.order_number
      xml.Kennnummer_der_Sendung @customs_details.map(&:sales_order).map(&:customer_order).map(&:consignee).join(" ")
      xml.Ursprungsbundesland mp.state_of_origin.split(" ")[0]
      xml.Ursprungsland mp.country_of_origin.code
      xml.Eigenmasse (sales_order_item.product.weight * sales_order_item.quantity).round(2)
      xml.Rohmasse index.zero? ? @total_weight : 0
      xml.Gefahrgutnummer_UNDG
      xml.Warennummer_KN8 sales_order_item.product.hs_code
      xml.Verfahren do
        xml.angemeldetes "10"
        xml.vorangegangenes "00"
        xml.weiteres "F61"
        xml.Ausfuhrerstattung
      end
      xml.Aussenhandelsstatistik do
        xml.Menge sales_order_item.quantity
        xml.Wert (sales_order_item.quantity *
                                      sales_order_item.price * 1.05).round(2).to_i
      end
      xml.Vorpapier do
        xml.Typ
        xml.Referenz
        xml.Zusatz
      end
      xml.Packstuecke do
        xml.Anzahl index.zero? ? @no_of_packages : 0
        xml.Nummer index + 1
        xml.Verpackungsart @package_type
        xml.Zeichen_Nummern customs_detail.sales_order.customer_order.order_number
      end
      xml.Unterlage do
        xml.Qualifizierung
        xml.Typ
        xml.Referenz
        xml.Zusatz
        xml.Detail
        xml.Datum_der_Ausstellung
        xml.Datum_des_Gueltigkeitsendes
        xml.Wert
        xml.Masseinheit
        xml.Aschreibungsmenge
      end
    end
  end
end