Class: Maintenance::AmazonVendorCostUpdateFromSpreadsheet

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/maintenance/amazon_vendor_cost_update_from_spreadsheet.rb

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#process(path = nil) ⇒ Object



3
4
5
6
7
8
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
# File 'app/services/maintenance/amazon_vendor_cost_update_from_spreadsheet.rb', line 3

def process(path = nil)
  require 'roo'
  path ||= '/Users/cbillen/Downloads/cost_updates2.xlsx'
  xlsx = Roo::Spreadsheet.open(path)
  rows = xlsx.sheet(0).each.to_a
  # 0 - Vendor code
  # 1 - Asin
  # 2 - Current Cost on Amazon
  # 3 - Recommended Cost Decrease
  row_results = []
  PaperTrail.request(whodunnit: 'Maintenance::AmazonVendorCostUpdateFromSpreadsheet') do
    CatalogItem.transaction do
      # Data starts at line 2
      rows[1..].each do |row|
        logger.debug('Processing row')
        row_result = [:skipped]
        row_result << vendor_code = row[0]
        row_result << asin = row[1]
        row_result << current_cost_amz = row[2]&.to_f
        row_result << recommended_cost_amz = row[3]&.to_f
        catalog_id = CatalogConstants::AMAZON_VENDOR_CODE_TO_CATALOG_ID[vendor_code]
        ci = CatalogItem.joins(store_item: :item).find_by(catalog_id:, items: { amazon_asin: asin })
        if ci
          row_result << ci.amount
          row_result << "Catalog Item #{ci.id} asin #{asin} original price #{ci.amount} does not match amazon's cost #{current_cost_amz}" if ci.amount != current_cost_amz
          if ci.amount == recommended_cost_amz
            row_result << "Pricing requested #{recommended_cost_amz} is the same on catalog item #{ci.id} asin #{asin}"
          else
            row_result << "Updating cost from #{ci.amount} to #{recommended_cost_amz} on catalog item #{ci.id} asin #{asin}"
            ci.update!(amount: recommended_cost_amz)
            row_result[0] = :updated
          end
        else
          row_result[0] = :missing
          row_result << nil
          row_result << "Catalog item not found for #{asin} in catalog id #{catalog_id}"
        end
        row_results << row_result
      end
    end
  end
  row_results
end