Class: Maintenance::RmaMaintenance

Inherits:
BaseService show all
Defined in:
app/services/maintenance/rma_maintenance.rb

Overview

Per venu 3/19/21
Void RMAs in AWAITING_RETURN state and created before 12/31/2019 if...

  1. The customer is E-tailer

or

  1. The sum of open invoices dated prior to 3/31/2020 for the customer is less than $50

This should clean most old rmas

Instance Attribute Summary collapse

Instance Method Summary collapse

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 BaseService

Instance Attribute Details

#last_resultsObject (readonly)

Returns the value of attribute last_results.



14
15
16
# File 'app/services/maintenance/rma_maintenance.rb', line 14

def last_results
  @last_results
end

Instance Method Details

#process(prior_to_date: 1.year.ago, limit: nil) ⇒ Object



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
# File 'app/services/maintenance/rma_maintenance.rb', line 16

def process(prior_to_date: 1.year.ago, limit: nil)
  @last_results = {}
  PaperTrail.request(whodunnit: 'Maintenance::RmaMaintenance') do

    base_rma_scope = Rma.where(state: 'awaiting_return').where(Rma[:created_at].lteq(6.months.ago))
    if limit
      base_rma_scope = base_rma_scope.limit(limit)
    end
    # 1. The customer is E-tailer
    etailers_rmas = base_rma_scope.joins(customer: :profile).where("profiles.report_grouping = 'E-Tailers'")
    etailers_rmas.find_each do |rma|
      log_info "Voiding RMA #{rma.to_s}"
      void_rma(rma)
    end

    # 2. The sum of open invoices dated prior to 3/31/2020 is less than $50
    # i'm going to use 4 months after the rma date
    small_open_invoices_rmas = base_rma_scope.where("(select COALESCE(SUM(total),0) from invoices where invoices.customer_id = rmas.customer_id and invoices.state = 'unpaid' and invoices.created_at < (rmas.created_at + INTERVAL '4 months')) <= 50")
    small_open_invoices_rmas.find_each do |rma|
      log_info "Voiding RMA #{rma.to_s}"
      void_rma(rma)
    end
  end
  last_results
end

#void_rma(rma) ⇒ Object



42
43
44
45
46
47
48
49
# File 'app/services/maintenance/rma_maintenance.rb', line 42

def void_rma(rma)
  rma.void_all_items_and_self
  if rma.errors.present?
    last_results[rma.rma_number] = { status: :error, errors: rma.errors.full_messages }
  else
    last_results[rma.rma_number] = { status: :ok, new_state: rma.state }
  end
end