Class: Catalog::AssignCatalogToCustomer

Inherits:
BaseService show all
Defined in:
app/services/catalog/assign_catalog_to_customer.rb

Overview

Operation to assign a catalog to a customer

Defined Under Namespace

Classes: Result

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

#initialize(options = {}) ⇒ AssignCatalogToCustomer

Returns a new instance of AssignCatalogToCustomer.



5
6
7
# File 'app/services/catalog/assign_catalog_to_customer.rb', line 5

def initialize(options = {})
  @catalog_item_assigner = options[:catalog_item_assigner] || Catalog::AssignCatalogItem.new
end

Instance Method Details

#error_check(customer, target_catalog) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/services/catalog/assign_catalog_to_customer.rb', line 60

def error_check(customer, target_catalog)
  # Don't allow catalog change to different currency
  if customer.catalog.currency != target_catalog.currency &&
     (customer.orders.not_open_for_change.any? ||
       customer.orders.co_only.any? ||
       customer.invoices.any?)
    ["Target catalog is in different currency and customer has orders not open for changes or invoices"]
  elsif customer.catalog.currency != target_catalog.currency &&
        customer.orders.where.not(state: %w[partially_invoiced invoiced]).joins(:payments).merge(Payment.where(category: [Payment::CREDIT_CARD, Payment::ECHECK]).all_authorized).exists?
    ["Target catalog is in different currency and customer has orders with unvoided credit card payments. Please void any open payments first."]
  else
    []
  end
end

#process(customer, target_catalog) ⇒ Object

customer, a customer to convert
target_catalog, a target_catalog to use



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
# File 'app/services/catalog/assign_catalog_to_customer.rb', line 17

def process(customer, target_catalog)
  # Do a pre-check, if the customer or catalog can actually change
  return Result.new(catalog_assigned: false, messages: ['Already assigned to this catalog']) if customer.catalog_id == target_catalog.id

  errors = error_check(customer, target_catalog)
  return Result.new(catalog_assigned: false, messages: errors) if errors.present?

  # Create a map so we can easily merge later on
  # items = Item.joins(:line_items).merge(customer.all_line_items)
  begin
    # target_catalog.transaction do
    #   items.distinct.each do |item|
    #     process_item(target_catalog, item, customer, messages)
    #   end
    # end
    currency_changed = customer.catalog.currency != target_catalog.currency
    # Now assign customer the catalog safely!
    customer.catalog_id = target_catalog.id
    # If our catalog has a discount assigned, we reset the customer's discount to catalog
    customer.tier2_program_pricing = Coupon::Tier2ProgramPricing.customer_default if target_catalog.parent_catalog_discount

    # clear out stripe_customer_id and delete stored cards if switching to a different currency
    if currency_changed
      customer.stripe_customer_id = nil
      customer.credit_card_vaults.destroy
    end

    customer.save!
    customer.reload
    # Switch quick estimators
    customer.quick_estimators.each { |qe| qe.update_attribute!(:catalog_id, customer.catalog_id) }

    # Switch currency on quotes, rooms, orders
    customer.quotes.each(&:fix_catalog)
    customer.orders.where.not(state: %w[partially_invoiced invoiced]).find_each(&:fix_catalog)
    customer.room_configurations.each(&:fix_catalog)

    Result.new catalog_assigned: true, messages: ["Catalog remapped"]
  rescue StandardError => e
    Result.new catalog_assigned: false, messages: [e.to_s] + e.backtrace
  end
end