Class: Edi::AmazonVc::InvoiceMessageProcessor

Inherits:
BaseEdiService show all
Defined in:
app/services/edi/amazon_vc/invoice_message_processor.rb

Constant Summary

Constants included from Edi::AddressAbbreviator

Edi::AddressAbbreviator::MAX_LENGTH

Instance Attribute Summary

Attributes inherited from BaseEdiService

#orchestrator

Instance Method Summary collapse

Methods inherited from BaseEdiService

#duplicate_po_already_notified?, #initialize, #mark_duplicate_po_as_notified, #report_order_creation_issues, #safe_process_edi_communication_log

Methods included from Edi::AddressAbbreviator

#abbreviate_street, #collect_street_originals, #record_address_abbreviation_notes

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

This class inherits a constructor from Edi::BaseEdiService

Instance Method Details

#build_invoice_message_from_invoice(invoice) ⇒ Object



9
10
11
12
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
57
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
96
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/services/edi/amazon_vc/invoice_message_processor.rb', line 9

def build_invoice_message_from_invoice(invoice)
  order = invoice.order
  invoice_line_items = get_invoice_line_items(invoice)
  #       {
  #         "itemSequenceNumber": "1",
  #         "buyerProductIdentifier": "B00IVLAABC",
  #         "invoicedQuantity": {
  #           "amount": 1,
  #           "unitOfMeasure": "Each"
  #         },
  #         "netCost": {
  #           "currencyCode": "EUR",
  #           "amount": "1200.00"
  #         },
  #         "purchaseOrderNumber": "D3rC3KTxG",
  #         "vendorOrderNumber": "0092590411",
  #         "hsnCode": "76.06.92.99.00",
  #         "taxDetails": [
  #           {
  #             "taxType": "CGST",
  #             "taxRate": "0.19",
  #             "taxAmount": {
  #               "currencyCode": "EUR",
  #               "amount": "228.00"
  #             },
  #             "taxableAmount": {
  #               "currencyCode": "EUR",
  #               "amount": "1200.00"
  #             }
  #           }
  #         ]
  #       }

  inv_lines = invoice_line_items.map do |li|
    {
      itemSequenceNumber: "#{li.edi_line_number}", # NOTE: in invoices, itemSequenceNumber is a string, in shipmentConfirmations it is an integer
      buyerProductIdentifier: li.catalog_item.amazon_asin,
      vendorProductIdentifier: li.catalog_item.sku,
      invoicedQuantity: {
        amount: li.quantity,
        unitOfMeasure: 'Each'
      },
      netCost: {
        currencyCode: invoice.currency,
        amount: li.total
      },
      purchaseOrderNumber: invoice.po_number,
      vendorOrderNumber: order.reference_number,
      taxDetails: if orchestrator.tax_registration_number.present?
                    [
                      {
                        taxType: li.tax_type.upcase,
                        taxRate: li.tax_rate,
                        taxAmount: {
                          currencyCode: invoice.currency,
                          amount: li.tax_total
                        },
                        taxableAmount: {
                          currencyCode: invoice.currency,
                          amount: li.taxable_total
                        }
                      }
                    ]
                  else
                    []
                  end
    }
  end

  # "invoices": [
  #   {
  #     "invoiceNumber": "0092590411",
  #     "invoiceDate": "2020-03-13T11:16:24Z",
  #     "remitToParty": {
  #       "partyId": "YourVendorCode",
  #       "address": {
  #         "name": "vendor name",
  #         "addressLine1": "vendor address 1",
  #         "addressLine2": "vendor address 2",
  #         "addressLine3": "vendor address 3",
  #         "city": "DECity",
  #         "county": "Schwabing",
  #         "district": "München",
  #         "stateOrRegion": "Bayern",
  #         "postalCode": "DEPostCode",
  #         "countryCode": "DE"
  #       },
  #       "taxRegistrationDetails": [
  #         {
  #           "taxRegistrationType": "VAT",
  #           "taxRegistrationNumber": "DE123456789"
  #         }
  #       ]
  #     },
  #     "shipFromParty": {
  #       "partyId": "ABCD"
  #     },
  #     ...
  #     "shipToCountryCode": "DE",
  #     "paymentTermsCode": "Basic",
  #     "invoiceTotal": {
  #       "currencyCode": "EUR",
  #       "amount": "1428.00"
  #     },
  #     "taxTotals": [
  #       {
  #         "taxType": "CGST",
  #         "taxRate": "0.19",
  #         "taxAmount": {
  #           "currencyCode": "EUR",
  #           "amount": "228.00"
  #         },
  #         "taxableAmount": {
  #           "currencyCode": "EUR",
  #           "amount": "1200.00"
  #         }
  #       }
  #     ],
  #     "items": [
  #       ...
  #     ]
  #   }
  # ]

  remit_to_tax_registration_details_arr = []
  if orchestrator.tax_registration_number.present?
    remit_to_tax_registration_details_arr << {
      taxRegistrationType: orchestrator.tax_registration_type,
      taxRegistrationNumber: orchestrator.tax_registration_number
    }
  end

  {
    invoices: [{
      invoiceNumber: invoice.reference_number,
      invoiceDate: Time.current.iso8601,
      referenceNumber: order.reference_number,
      remitToParty: {
        partyId: orchestrator.vendor_code,
        address: { # match existing Vendor Central Direct Fulfillment invoices exactly
          name: orchestrator.vendor_name,
          addressLine1: orchestrator.vendor_address_street1,
          addressLine2: orchestrator.vendor_address_street2,
          city: orchestrator.vendor_address_city,
          stateOrRegion: orchestrator.vendor_address_state,
          postalCode: orchestrator.vendor_address_postal_code,
          countryCode: orchestrator.vendor_address_country
        },
        taxRegistrationDetails: remit_to_tax_registration_details_arr
      },
      shipFromParty: {
        partyId: orchestrator.warehouse_code
      },
      invoiceTotal: {
        currencyCode: invoice.currency,
        amount: invoice.total
      },
      taxTotals: if orchestrator.tax_registration_number.present?
                   [
                     {
                       taxType: invoice_line_items.first.tax_type.upcase,
                       taxRate: invoice_line_items.first.tax_rate,
                       taxAmount: {
                         currencyCode: invoice.currency,
                         amount: invoice.tax_total
                       },
                       taxableAmount: {
                         currencyCode: invoice.currency,
                         amount: invoice_line_items.sum { |li| li.taxable_total }
                       }
                     }
                   ]
                 else
                   []
                 end,
      items: inv_lines
    }]
  }
end

#create_invoice(invoice, options = {}) ⇒ Object



189
190
191
192
# File 'app/services/edi/amazon_vc/invoice_message_processor.rb', line 189

def create_invoice(invoice, options = {})
  m = build_invoice_message_from_invoice(invoice)
  process(m, options.merge({ order: invoice.order, invoice: invoice }))
end

#get_invoice_line_items(invoice) ⇒ Object



5
6
7
# File 'app/services/edi/amazon_vc/invoice_message_processor.rb', line 5

def get_invoice_line_items(invoice)
  invoice.line_items.goods.where.not(edi_reference: nil).order(:edi_line_number)
end

#process(invoice_message, options) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/services/edi/amazon_vc/invoice_message_processor.rb', line 194

def process(invoice_message, options)
  order = options[:order]
  invoice = options[:invoice]
  transmit_after = options[:transmit_after]
  edi_log = nil
  EdiCommunicationLog.transaction do
    edi_log = EdiCommunicationLog.create! partner: orchestrator.partner,
                                          category: :invoice,
                                          data: invoice_message.to_json,
                                          data_type: 'json',
                                          file_info: { lines_confirmed: invoice_message[:invoices].first[:items].size },
                                          transmit_after: transmit_after,
                                          transmit_datetime: Time.current
  end
  if invoice
    edi_log.edi_documents.create(invoice: invoice)
  elsif order
    edi_log.edi_documents.create(order: order)
  end
  edi_log
end